diff --git a/src/compiler.ts b/src/compiler.ts index 64096a4..0fa18af 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -294,28 +294,28 @@ export class NexeCompiler { return binary } const launchCode = this.code() - const vfsStream = this.bundle.toStream() + const codeSize = Buffer.byteLength(launchCode) + const sentinel = Buffer.from('') + let vfsSize = 0 - const vfsSum = new Transform({ - transform(chunk, _enc, cb) { + const streams = [binary, toStream(launchCode), this.bundle.toStream().pipe(new Transform({ + transform: (chunk, _, cb) => { + vfsSize || this.bundle.finalize() chunk && (vfsSize += chunk.length) cb(null, chunk) }, - }) - const onEnd = new Promise((resolve) => vfsSum.once('end', resolve)) - const streams = [binary, toStream(launchCode), vfsStream.pipe(vfsSum)] + }))] let done = false - return new MultiStream(async (cb) => { + return new MultiStream((cb) => { if (done) cb(null, null) else if (streams.length) cb(null, streams.shift() as Readable) else { done = true - await onEnd const trailers = Buffer.alloc(16) - trailers.writeDoubleLE(Buffer.byteLength(launchCode), 0) + trailers.writeDoubleLE(codeSize, 0) trailers.writeDoubleLE(vfsSize, 8) - cb(null, toStream(Buffer.concat([Buffer.from(''), trailers]))) + cb(null, toStream(Buffer.concat([sentinel, trailers]))) } }) } diff --git a/src/fs/bundle.ts b/src/fs/bundle.ts index 04c3be8..024b425 100644 --- a/src/fs/bundle.ts +++ b/src/fs/bundle.ts @@ -41,8 +41,11 @@ export class Bundle { } } + public finalize() { + return this.zip.finalize() + } + public toStream(): Readable { - setTimeout(() => this.zip.finalize()) return this.zip } }