Skip to content

Commit

Permalink
fix: allow UInt8Array in pathToFilename
Browse files Browse the repository at this point in the history
This seems reasonable, considering memfs supports the browser,
and users are more likely to have instances of Uint8Array there.
  • Loading branch information
kylecarbs committed Jan 9, 2025
1 parent 4702811 commit 2d5ab1b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ describe('volume', () => {
expect(fd).toBeGreaterThan(0);
expect(oldMtime).not.toBe(newMtime);
});
it('Create new file with Uint8Array path', () => {
const path = new TextEncoder().encode('/test.txt');
const fd = vol.openSync(path, 'w');
expect(typeof fd).toBe('number');
expect(fd).toBeGreaterThan(0);
});
it('Error on file not found', () => {
try {
vol.openSync('/non-existing-file.txt', 'r');
Expand All @@ -404,7 +410,7 @@ describe('volume', () => {
throw Error('This should not throw');
} catch (err) {
expect(err).toBeInstanceOf(TypeError);
expect(err.message).toBe('path must be a string or Buffer');
expect(err.message).toBe('path must be a string, Buffer, or Uint8Array');
}
});
it('Invalid flags correct error code', () => {
Expand Down Expand Up @@ -477,7 +483,7 @@ describe('volume', () => {
throw Error('This should not throw');
} catch (err) {
expect(err).toBeInstanceOf(TypeError);
expect(err.message).toBe('path must be a string or Buffer');
expect(err.message).toBe('path must be a string, Buffer, or Uint8Array');
done();
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/volume/__snapshots__/renameSync.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

exports[`renameSync(fromPath, toPath) Throws on only one param 1`] = `"path must be a string or Buffer"`;
exports[`renameSync(fromPath, toPath) Throws on only one param 1`] = `"path must be a string, Buffer, or Uint8Array"`;
2 changes: 1 addition & 1 deletion src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const enum MODE {
}

export const ERRSTR = {
PATH_STR: 'path must be a string or Buffer',
PATH_STR: 'path must be a string, Buffer, or Uint8Array',
// FD: 'file descriptor must be a unsigned 32-bit integer',
FD: 'fd must be a file descriptor',
MODE_INT: 'mode must be an int',
Expand Down
5 changes: 3 additions & 2 deletions src/node/types/misc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PathLike, symlink } from 'fs';
import type { PathLike as NodePathLike, symlink } from 'fs';
import type { constants } from '../../constants';
import type { EventEmitter } from 'events';
import type { TSetTimeout } from '../../setTimeoutUnref';
Expand All @@ -13,8 +13,9 @@ import type {
} from './options';
import type { Readable, Writable } from 'stream';

export { PathLike, symlink };
export { symlink };

export type PathLike = NodePathLike | Uint8Array; // For browser support we add Uint8Array.
export type TDataOut = string | Buffer; // Data formats we give back to users.
export type TEncodingExtended = BufferEncoding | 'buffer';
export type TFileId = PathLike | number; // Number is used as a file descriptor.
Expand Down
3 changes: 3 additions & 0 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ function getPathFromURLPosix(url): string {
}

export function pathToFilename(path: misc.PathLike): string {
if (path instanceof Uint8Array) {
path = bufferFrom(path);
}
if (typeof path !== 'string' && !Buffer.isBuffer(path)) {
try {
if (!(path instanceof require('url').URL)) throw new TypeError(ERRSTR.PATH_STR);
Expand Down
5 changes: 2 additions & 3 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ import {
getWriteSyncArgs,
unixify,
} from './node/util';
import type { PathLike, symlink } from 'fs';
import type { symlink } from 'fs';
import type { PathLike } from './node/types/misc';
import type { FsPromisesApi, FsSynchronousApi } from './node/types';
import { fsSynchronousApiList } from './node/lists/fsSynchronousApiList';
import { Dir } from './Dir';

const resolveCrossPlatform = pathModule.resolve;
Expand Down Expand Up @@ -1327,7 +1327,6 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
}

private realpathBase(filename: string, encoding: TEncodingExtended | undefined): TDataOut {
debugger;
const realLink = this.getResolvedLinkOrThrow(filename, 'realpath');

return strToEncoding(realLink.getPath() || '/', encoding);
Expand Down

0 comments on commit 2d5ab1b

Please sign in to comment.