Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure files in subdirectories are returned as buffers when calling toJSON with asBuffer #1041

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading