Skip to content

Commit

Permalink
Decode file path URL in filesystem operations
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Nov 13, 2023
1 parent 63b801a commit 9fa8051
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-swans-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Decode file path URL in filesystem operations
16 changes: 8 additions & 8 deletions packages/runtime/src/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Canvas, CanvasRenderingContext2D, ctxInternal } from './canvas';
import { FontFaceSet } from './polyfills/font';
import { type Callback, INTERNAL_SYMBOL, type Opaque } from './internal';
import { inspect } from './inspect';
import { bufferSourceToArrayBuffer, toPromise } from './utils';
import { bufferSourceToArrayBuffer, toPromise, pathToString } from './utils';
import { setTimeout, clearTimeout } from './timers';
import { encoder } from './polyfills/text-encoder';
import { Socket, connect, createServer, parseAddress } from './tcp';
Expand Down Expand Up @@ -534,7 +534,7 @@ export class SwitchClass extends EventTarget {
* ```
*/
chdir(dir: PathLike) {
return this.native.chdir(String(dir));
return this.native.chdir(pathToString(dir));
}

/**
Expand Down Expand Up @@ -562,7 +562,7 @@ export class SwitchClass extends EventTarget {
* ```
*/
readFile(path: PathLike) {
return toPromise(this.native.readFile, String(path));
return toPromise(this.native.readFile, pathToString(path));
}

/**
Expand All @@ -577,7 +577,7 @@ export class SwitchClass extends EventTarget {
* ```
*/
readDirSync(path: PathLike) {
return this.native.readDirSync(String(path));
return this.native.readDirSync(pathToString(path));
}

/**
Expand All @@ -592,7 +592,7 @@ export class SwitchClass extends EventTarget {
* ```
*/
readFileSync(path: PathLike) {
return this.native.readFileSync(String(path));
return this.native.readFileSync(pathToString(path));
}

/**
Expand All @@ -606,22 +606,22 @@ export class SwitchClass extends EventTarget {
writeFileSync(path: PathLike, data: string | BufferSource) {
const d = typeof data === 'string' ? encoder.encode(data) : data;
const ab = bufferSourceToArrayBuffer(d);
return this.native.writeFileSync(String(path), ab);
return this.native.writeFileSync(pathToString(path), ab);
}

/**
* Removes the file or directory specified by `path`.
*/
remove(path: PathLike) {
return toPromise(this.native.remove, String(path));
return toPromise(this.native.remove, pathToString(path));
}

/**
* Returns a Promise which resolves to an object containing
* information about the file pointed to by `path`.
*/
stat(path: PathLike) {
return toPromise(this.native.stat, String(path));
return toPromise(this.native.stat, pathToString(path));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BufferSource } from './types';
import type { BufferSource, PathLike } from './types';
import {
INTERNAL_SYMBOL,
type Callback,
Expand Down Expand Up @@ -60,6 +60,10 @@ export function assertInternalConstructor(a: ArrayLike<any>) {
if (a[0] !== INTERNAL_SYMBOL) throw new TypeError('Illegal constructor');
}

export function pathToString(p: PathLike): string {
return typeof p === 'string' ? p : decodeURI(p.href);
}

export class Deferred<T> {
pending = true;
promise: Promise<T>;
Expand Down

0 comments on commit 9fa8051

Please sign in to comment.