Skip to content

Commit

Permalink
Switch to use fflate instead of JSZip
Browse files Browse the repository at this point in the history
lib.js: 106,360 => 17,079 (-84%)
nesemu.js: 211,357 => 96,476 (-54%)
  • Loading branch information
tyfkda committed Mar 2, 2024
1 parent b895ed4 commit c1de200
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 73 deletions.
2 changes: 1 addition & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
},
"dependencies": {
"@kmamal/sdl": "~0.9.5",
"jszip": "~3.10.1"
"fflate": "~0.8.2"
}
}
19 changes: 12 additions & 7 deletions nodejs/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const fs = __non_webpack_require__('fs').promises

import JSZip from 'jszip'
import {unzip, AsyncUnzipOptions, Unzipped} from 'fflate'
import {Nes, NesEvent} from '../../src/nes/nes'
import {Cartridge} from '../../src/nes/cartridge'
import {IDeltaModulationChannel, INoiseChannel, IPulseChannel, PadValue, WaveType} from '../../src/nes/apu'
import {Util} from '../../src/util/util'
import {AudioManager} from '../../src/util/audio_manager'

import {AudioContext} from './audio_context'
import util from 'util'

const sdl = __non_webpack_require__('@kmamal/sdl')

Expand Down Expand Up @@ -277,12 +278,16 @@ async function loadNesRomData(fileName: string): Promise<Uint8Array> {

case 'zip':
{
const data = await fs.readFile(fileName)
const zip = new JSZip()
const loadedZip = await zip.loadAsync(data)
for (let fn of Object.keys(loadedZip.files)) {
if (Util.getExt(fn).toLowerCase() === 'nes')
return loadedZip.files[fn].async('uint8array')
const buffer = await fs.readFile(fileName)
const options = {
filter(file: any) {
return Util.getExt(file.name).toLowerCase() === 'nes'
}
}
const loadedZip = await util.promisify<Uint8Array, AsyncUnzipOptions, Unzipped>(unzip)(buffer, options)
for (let fn of Object.keys(loadedZip)) {
// if (Util.getExt(fn).toLowerCase() === 'nes')
return loadedZip[fn]
}
return Promise.reject(`No .nes file included in ${fileName}`)
}
Expand Down
61 changes: 20 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
"dependencies": {
"core-js": "~3.30.1",
"escape-html": "~1.0.3",
"fflate": "~0.8.2",
"fscreen": "~1.2.0",
"jszip": "~3.10.1",
"md5": "~2.3.0",
"stats-js": "~1.0.1",
"stream": "~0.0.2"
Expand Down
33 changes: 25 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Util} from './util/util'
import {WindowManager} from './wnd/window_manager'
import {Nes} from './nes/nes'
import './util/polyfill'
import JSZip from 'jszip'
import {unzip, Unzipped} from 'fflate'

import audioOnImg from './res/audio_on.png'
import audioOffImg from './res/audio_off.png'
Expand Down Expand Up @@ -152,17 +152,34 @@ class Main {
return false
})
.map(async ({file, ext}) => {
function promisify(f: Function) {
return (...args: any[]) => {
return new Promise<Unzipped>((resolve, reject) => {
f(...args, (err: any, result: any) => {
if (err != null)
reject(err)
else
resolve(result)
})
})
}
}

if (ext === 'zip') {
const binary = await DomUtil.loadFile(file)
const zip = new JSZip()
const loadedZip = await zip.loadAsync(binary)
for (const fileName2 of Object.keys(loadedZip.files)) {
const ext2 = Util.getExt(fileName2).toLowerCase()
if (kTargetExts.has(ext2)) {
const unzipped = await loadedZip.files[fileName2].async('uint8array')
return {type: ext2, binary: unzipped, fileName: fileName2}
const options = {
filter(file: any) {
const ext2 = Util.getExt(file.name).toLowerCase()
return kTargetExts.has(ext2)
}
}
const loadedZip = await promisify(unzip)(binary, options)
for (const fileName2 of Object.keys(loadedZip)) {
const ext2 = Util.getExt(fileName2).toLowerCase()
console.assert(kTargetExts.has(ext2)) // Already filtered.
const unzipped = loadedZip[fileName2]
return {type: ext2, binary: unzipped, fileName: fileName2}
}
return Promise.reject(`No .nes file included: ${file.name}`)
} else if (kTargetExts.has(ext)) {
const binary = await DomUtil.loadFile(file)
Expand Down
17 changes: 10 additions & 7 deletions tools/dmc2wav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import fs from 'node:fs/promises'
import * as fsNonPromise from 'fs'
import path from 'path'
import tty from 'tty'
import JSZip from 'jszip'
import {unzip, AsyncUnzipOptions, Unzipped} from 'fflate'
import wav from 'node-wav'
import util from 'util'

import {Cartridge} from '../src/nes/cartridge'
import {DeltaModulationSampler, kDmcRateTable} from '../src/util/audio/delta_modulation_sampler'
Expand Down Expand Up @@ -103,14 +104,16 @@ async function loadNesRomData(romFileName: string): Promise<Uint8Array> {
case '.zip':
{
const buffer = await fs.readFile(romFileName)
const zip = new JSZip()
const loadedZip = await zip.loadAsync(buffer)
for (let fileName of Object.keys(loadedZip.files)) {
if (path.extname(fileName).toLowerCase() === '.nes') {
const unzipped = await loadedZip.files[fileName].async('uint8array')
return unzipped
const options = {
filter(file: any) {
return path.extname(file.name).toLowerCase() === '.nes'
}
}
const loadedZip = await util.promisify<Uint8Array, AsyncUnzipOptions, Unzipped>(unzip)(buffer, options)
for (let fileName of Object.keys(loadedZip)) {
const unzipped = loadedZip[fileName]
return unzipped
}
}
console.error(`${romFileName}: .nes not included`)
break
Expand Down
19 changes: 11 additions & 8 deletions tools/getmapperno.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises'
import path from 'path'
import JSZip from 'jszip'
import {unzip, AsyncUnzipOptions, Unzipped} from 'fflate'
import util from 'util'

import {Cartridge} from '../src/nes/cartridge'

Expand All @@ -22,15 +23,17 @@ async function dumpMapper(fn: string): Promise<void> {
case '.zip':
{
const buffer = await fs.readFile(fn)
const zip = new JSZip()
const loadedZip = await zip.loadAsync(buffer)
for (let fileName of Object.keys(loadedZip.files)) {
if (path.extname(fileName).toLowerCase() === '.nes') {
const unzipped = await loadedZip.files[fileName].async('uint8array')
console.log(`"${path.basename(fn)}"\tmapper=${getMapperNo(unzipped)}`)
return
const options = {
filter(file: any) {
return path.extname(file.name).toLowerCase() === '.nes'
}
}
const loadedZip = await util.promisify<Uint8Array, AsyncUnzipOptions, Unzipped>(unzip)(buffer, options)
for (let fileName of Object.keys(loadedZip)) {
const unzipped = loadedZip[fileName]
console.log(`"${path.basename(fn)}"\tmapper=${getMapperNo(unzipped)}`)
return
}
}
console.error(`${fn}: .nes not included`)
break
Expand Down

0 comments on commit c1de200

Please sign in to comment.