From d0170de8c4627fb9e2686b4308e99057805319a2 Mon Sep 17 00:00:00 2001 From: inokawa <48897392+inokawa@users.noreply.github.com> Date: Sun, 4 Jan 2026 17:28:07 +0900 Subject: [PATCH 1/2] Add support for writing binary to file system --- lib/file-pipeline/file-system.js | 2 +- test/output.js | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/file-pipeline/file-system.js b/lib/file-pipeline/file-system.js index 6d769ce..c31e068 100644 --- a/lib/file-pipeline/file-system.js +++ b/lib/file-pipeline/file-system.js @@ -55,5 +55,5 @@ export function fileSystem(context, file, next) { debug('Writing document to `%s`', destinationPath) file.stored = true - fs.writeFile(destinationPath, file.toString(), next) + fs.writeFile(destinationPath, file.value, next) } diff --git a/test/output.js b/test/output.js index 8ef2862..793fe8d 100644 --- a/test/output.js +++ b/test/output.js @@ -184,6 +184,39 @@ test('output', async function (t) { assert.equal(output, 'two') }) + await t.test('should write binary to a path', async function () { + const cwd = new URL('simple-structure/', fixtures) + const stderr = spy() + const binary = () => new Uint8Array([0xde, 0xad, 0xbe, 0xef]) + const result = await engine({ + cwd, + extensions: ['txt'], + files: ['one.txt'], + output: 'five.bin', + processor: noop().use( + /** @type {Plugin<[], Literal, Uint8Array>} */ + // @ts-expect-error: TS doesn’t get `this`. + function () { + /** @type {Compiler} */ + this.compiler = function () { + return binary() + } + } + ), + streamError: stderr.stream + }) + + const input = String(await fs.readFile(new URL('one.txt', cwd))) + const output = await fs.readFile(new URL('five.bin', cwd)) + + await fs.unlink(new URL('five.bin', cwd)) + + assert.equal(result.code, 0) + assert.equal(stderr(), 'one.txt > five.bin: written\n') + assert.equal(input, '') + assert.deepStrictEqual(new Uint8Array(output), binary()) + }) + await t.test('should write to folders and support URLs', async function () { const cwd = new URL('simple-structure/', fixtures) const stderr = spy() From 82f06c99cec1d0b2877b59a7e267222c5cdf1810 Mon Sep 17 00:00:00 2001 From: inokawa <48897392+inokawa@users.noreply.github.com> Date: Tue, 6 Jan 2026 18:41:10 +0900 Subject: [PATCH 2/2] Update test --- test/output.js | 93 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 28 deletions(-) diff --git a/test/output.js b/test/output.js index 793fe8d..2baa402 100644 --- a/test/output.js +++ b/test/output.js @@ -184,38 +184,75 @@ test('output', async function (t) { assert.equal(output, 'two') }) - await t.test('should write binary to a path', async function () { - const cwd = new URL('simple-structure/', fixtures) - const stderr = spy() - const binary = () => new Uint8Array([0xde, 0xad, 0xbe, 0xef]) - const result = await engine({ - cwd, - extensions: ['txt'], - files: ['one.txt'], - output: 'five.bin', - processor: noop().use( - /** @type {Plugin<[], Literal, Uint8Array>} */ - // @ts-expect-error: TS doesn’t get `this`. - function () { - /** @type {Compiler} */ - this.compiler = function () { - return binary() + await t.test( + 'should write binary in Uint8Array to a path', + async function () { + const cwd = new URL('simple-structure/', fixtures) + const stderr = spy() + const result = await engine({ + cwd, + extensions: ['txt'], + files: ['one.txt'], + output: 'five.bin', + processor: noop().use( + /** @type {Plugin<[], Literal, Uint8Array>} */ + // @ts-expect-error: TS doesn’t get `this`. + function () { + /** @type {Compiler} */ + this.compiler = function () { + return new Uint8Array([0xde, 0xad, 0xbe, 0xef]) + } } - } - ), - streamError: stderr.stream - }) + ), + streamError: stderr.stream + }) - const input = String(await fs.readFile(new URL('one.txt', cwd))) - const output = await fs.readFile(new URL('five.bin', cwd)) + const input = String(await fs.readFile(new URL('one.txt', cwd))) + const output = new Uint8Array(await fs.readFile(new URL('five.bin', cwd))) - await fs.unlink(new URL('five.bin', cwd)) + await fs.unlink(new URL('five.bin', cwd)) - assert.equal(result.code, 0) - assert.equal(stderr(), 'one.txt > five.bin: written\n') - assert.equal(input, '') - assert.deepStrictEqual(new Uint8Array(output), binary()) - }) + assert.equal(result.code, 0) + assert.equal(stderr(), 'one.txt > five.bin: written\n') + assert.equal(input, '') + assert.deepStrictEqual(output, new Uint8Array([0xde, 0xad, 0xbe, 0xef])) + } + ) + + await t.test( + 'should write utf-8 text in Uint8Array to a path', + async function () { + const cwd = new URL('simple-structure/', fixtures) + const stderr = spy() + const result = await engine({ + cwd, + extensions: ['txt'], + files: ['one.txt'], + output: 'six.bin', + processor: noop().use( + /** @type {Plugin<[], Literal, Uint8Array>} */ + // @ts-expect-error: TS doesn’t get `this`. + function () { + /** @type {Compiler} */ + this.compiler = function () { + return new TextEncoder().encode('Hi! 🤷‍♂️') + } + } + ), + streamError: stderr.stream + }) + + const input = String(await fs.readFile(new URL('one.txt', cwd))) + const output = new Uint8Array(await fs.readFile(new URL('six.bin', cwd))) + + await fs.unlink(new URL('six.bin', cwd)) + + assert.equal(result.code, 0) + assert.equal(stderr(), 'one.txt > six.bin: written\n') + assert.equal(input, '') + assert.deepStrictEqual(output, new TextEncoder().encode('Hi! 🤷‍♂️')) + } + ) await t.test('should write to folders and support URLs', async function () { const cwd = new URL('simple-structure/', fixtures)