Skip to content

Commit

Permalink
fix: ensure files in subdirectories are returned as buffers when call…
Browse files Browse the repository at this point in the history
…ing `toJSON` with `asBuffer` (#1041)

* Volume.toJSON: correctly propagate asBuffer in recursive calls

* Add a test for volume.toJSON when serializing files as Buffers
  • Loading branch information
elegaanz authored Jul 23, 2024
1 parent 5f9ed10 commit c3d4cf3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ describe('volume', () => {
const result = vol.toJSON('/', {}, false, true)['/file'];
expect(result).toStrictEqual(buffer);
});

it('Outputs files in subdirectories as buffers too', () => {
const buffer = Buffer.from('Hello');
const vol = new Volume();
vol.mkdirSync('/dir');
vol.writeFileSync('/dir/file', buffer);
const result = vol.toJSON('/', {}, false, true)['/dir/file'];
expect(result).toStrictEqual(buffer);
});
});

describe('.fromJSON(json[, cwd])', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
if (path) filename = relative(path, filename);
json[filename] = asBuffer ? node.getBuffer() : node.getString();
} else if (node.isDirectory()) {
this._toJSON(child, json, path);
this._toJSON(child, json, path, asBuffer);
}
}

Expand Down

0 comments on commit c3d4cf3

Please sign in to comment.