Skip to content

Commit c3d4cf3

Browse files
authoredJul 23, 2024··
fix: ensure files in subdirectories are returned as buffers when calling toJSON with asBuffer (#1041)
* Volume.toJSON: correctly propagate asBuffer in recursive calls * Add a test for volume.toJSON when serializing files as Buffers
1 parent 5f9ed10 commit c3d4cf3

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
 

‎src/__tests__/volume.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ describe('volume', () => {
196196
const result = vol.toJSON('/', {}, false, true)['/file'];
197197
expect(result).toStrictEqual(buffer);
198198
});
199+
200+
it('Outputs files in subdirectories as buffers too', () => {
201+
const buffer = Buffer.from('Hello');
202+
const vol = new Volume();
203+
vol.mkdirSync('/dir');
204+
vol.writeFileSync('/dir/file', buffer);
205+
const result = vol.toJSON('/', {}, false, true)['/dir/file'];
206+
expect(result).toStrictEqual(buffer);
207+
});
199208
});
200209

201210
describe('.fromJSON(json[, cwd])', () => {

‎src/volume.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
554554
if (path) filename = relative(path, filename);
555555
json[filename] = asBuffer ? node.getBuffer() : node.getString();
556556
} else if (node.isDirectory()) {
557-
this._toJSON(child, json, path);
557+
this._toJSON(child, json, path, asBuffer);
558558
}
559559
}
560560

0 commit comments

Comments
 (0)
Please sign in to comment.