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

feat: support UInt8Array in place of Buffer #1083

Merged
merged 2 commits into from
Jan 9, 2025
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
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
4 changes: 1 addition & 3 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ import {
getWriteSyncArgs,
unixify,
} from './node/util';
import type { PathLike, symlink } from 'fs';
import type { PathLike, symlink } 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 +1326,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
Loading