Skip to content

Commit 0d3662a

Browse files
kylecarbsG-Rath
andauthored
feat: support UInt8Array in place of Buffer (#1083)
* fix: allow UInt8Array in pathToFilename This seems reasonable, considering memfs supports the browser, and users are more likely to have instances of Uint8Array there. * refactor: merge imports --------- Co-authored-by: Gareth Jones <Jones258@Gmail.com>
1 parent 77c4a53 commit 0d3662a

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
lines changed

src/__tests__/volume.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ describe('volume', () => {
390390
expect(fd).toBeGreaterThan(0);
391391
expect(oldMtime).not.toBe(newMtime);
392392
});
393+
it('Create new file with Uint8Array path', () => {
394+
const path = new TextEncoder().encode('/test.txt');
395+
const fd = vol.openSync(path, 'w');
396+
expect(typeof fd).toBe('number');
397+
expect(fd).toBeGreaterThan(0);
398+
});
393399
it('Error on file not found', () => {
394400
try {
395401
vol.openSync('/non-existing-file.txt', 'r');
@@ -404,7 +410,7 @@ describe('volume', () => {
404410
throw Error('This should not throw');
405411
} catch (err) {
406412
expect(err).toBeInstanceOf(TypeError);
407-
expect(err.message).toBe('path must be a string or Buffer');
413+
expect(err.message).toBe('path must be a string, Buffer, or Uint8Array');
408414
}
409415
});
410416
it('Invalid flags correct error code', () => {
@@ -477,7 +483,7 @@ describe('volume', () => {
477483
throw Error('This should not throw');
478484
} catch (err) {
479485
expect(err).toBeInstanceOf(TypeError);
480-
expect(err.message).toBe('path must be a string or Buffer');
486+
expect(err.message).toBe('path must be a string, Buffer, or Uint8Array');
481487
done();
482488
}
483489
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`renameSync(fromPath, toPath) Throws if path is of wrong type 1`] = `"path must be a string or Buffer"`;
3+
exports[`renameSync(fromPath, toPath) Throws if path is of wrong type 1`] = `"path must be a string, Buffer, or Uint8Array"`;
44

5-
exports[`renameSync(fromPath, toPath) Throws on no params 1`] = `"path must be a string or Buffer"`;
5+
exports[`renameSync(fromPath, toPath) Throws on no params 1`] = `"path must be a string, Buffer, or Uint8Array"`;
66

7-
exports[`renameSync(fromPath, toPath) Throws on only one param 1`] = `"path must be a string or Buffer"`;
7+
exports[`renameSync(fromPath, toPath) Throws on only one param 1`] = `"path must be a string, Buffer, or Uint8Array"`;

src/node/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const enum MODE {
88
}
99

1010
export const ERRSTR = {
11-
PATH_STR: 'path must be a string or Buffer',
11+
PATH_STR: 'path must be a string, Buffer, or Uint8Array',
1212
// FD: 'file descriptor must be a unsigned 32-bit integer',
1313
FD: 'fd must be a file descriptor',
1414
MODE_INT: 'mode must be an int',

src/node/types/misc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PathLike, symlink } from 'fs';
1+
import type { PathLike as NodePathLike, symlink } from 'fs';
22
import type { constants } from '../../constants';
33
import type { EventEmitter } from 'events';
44
import type { TSetTimeout } from '../../setTimeoutUnref';
@@ -13,8 +13,9 @@ import type {
1313
} from './options';
1414
import type { Readable, Writable } from 'stream';
1515

16-
export { PathLike, symlink };
16+
export { symlink };
1717

18+
export type PathLike = NodePathLike | Uint8Array; // For browser support we add Uint8Array.
1819
export type TDataOut = string | Buffer; // Data formats we give back to users.
1920
export type TEncodingExtended = BufferEncoding | 'buffer';
2021
export type TFileId = PathLike | number; // Number is used as a file descriptor.

src/node/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ function getPathFromURLPosix(url): string {
7272
}
7373

7474
export function pathToFilename(path: misc.PathLike): string {
75+
if (path instanceof Uint8Array) {
76+
path = bufferFrom(path);
77+
}
7578
if (typeof path !== 'string' && !Buffer.isBuffer(path)) {
7679
try {
7780
if (!(path instanceof require('url').URL)) throw new TypeError(ERRSTR.PATH_STR);

src/volume.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ import {
5858
getWriteSyncArgs,
5959
unixify,
6060
} from './node/util';
61-
import type { PathLike, symlink } from 'fs';
61+
import type { PathLike, symlink } from './node/types/misc';
6262
import type { FsPromisesApi, FsSynchronousApi } from './node/types';
63-
import { fsSynchronousApiList } from './node/lists/fsSynchronousApiList';
6463
import { Dir } from './Dir';
6564

6665
const resolveCrossPlatform = pathModule.resolve;

0 commit comments

Comments
 (0)