Skip to content

Commit 1c9c57f

Browse files
committed
fix: fix URL support for path argument of readFile and readFileSync
1 parent f6e8baf commit 1c9c57f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/__tests__/volume.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import {URL} from 'url';
12
import {Link, Node, Stats, Dirent} from "../node";
23
import {Volume, filenameToSteps, StatWatcher} from "../volume";
34

4-
55
describe('volume', () => {
66
describe('filenameToSteps(filename): string[]', () => {
77
it('/ -> []', () => {
@@ -432,6 +432,10 @@ describe('volume', () => {
432432
expect(buf).toBeInstanceOf(Buffer);
433433
expect(str).toBe(data);
434434
});
435+
it('Read file with path passed as URL', () => {
436+
const str = vol.readFileSync(new URL('file:///text.txt')).toString();
437+
expect(str).toBe(data);
438+
});
435439
it('Specify encoding as string', () => {
436440
const str = vol.readFileSync('/text.txt', 'utf8');
437441
expect(str).toBe(data);

src/volume.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,13 +1018,14 @@ export class Volume {
10181018
private readFileBase(id: TFileId, flagsNum: number, encoding: TEncoding): Buffer | string {
10191019
let result: Buffer | string;
10201020

1021-
const userOwnsFd = isFd(id);
1021+
const isUserFd = typeof id === 'number';
1022+
let userOwnsFd: boolean = isUserFd && isFd(id);
10221023
let fd: number;
10231024

1024-
if(userOwnsFd) {
1025-
fd = id as number;
1026-
} else {
1027-
const steps = filenameToSteps(id as string);
1025+
if(userOwnsFd) fd = id as number;
1026+
else {
1027+
const filename = pathToFilename(id as TFilePath);
1028+
const steps = filenameToSteps(filename);
10281029
const link: Link = this.getResolvedLink(steps);
10291030

10301031
if(link) {

0 commit comments

Comments
 (0)