Skip to content

Commit

Permalink
fix: fix URL support for path argument of readFile and readFileSync
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzafroide committed Nov 6, 2018
1 parent f6e8baf commit 1c9c57f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {URL} from 'url';
import {Link, Node, Stats, Dirent} from "../node";
import {Volume, filenameToSteps, StatWatcher} from "../volume";


describe('volume', () => {
describe('filenameToSteps(filename): string[]', () => {
it('/ -> []', () => {
Expand Down Expand Up @@ -432,6 +432,10 @@ describe('volume', () => {
expect(buf).toBeInstanceOf(Buffer);
expect(str).toBe(data);
});
it('Read file with path passed as URL', () => {
const str = vol.readFileSync(new URL('file:///text.txt')).toString();
expect(str).toBe(data);
});
it('Specify encoding as string', () => {
const str = vol.readFileSync('/text.txt', 'utf8');
expect(str).toBe(data);
Expand Down
11 changes: 6 additions & 5 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,13 +1018,14 @@ export class Volume {
private readFileBase(id: TFileId, flagsNum: number, encoding: TEncoding): Buffer | string {
let result: Buffer | string;

const userOwnsFd = isFd(id);
const isUserFd = typeof id === 'number';
let userOwnsFd: boolean = isUserFd && isFd(id);
let fd: number;

if(userOwnsFd) {
fd = id as number;
} else {
const steps = filenameToSteps(id as string);
if(userOwnsFd) fd = id as number;
else {
const filename = pathToFilename(id as TFilePath);
const steps = filenameToSteps(filename);
const link: Link = this.getResolvedLink(steps);

if(link) {
Expand Down

0 comments on commit 1c9c57f

Please sign in to comment.