From 9fa8051a608876e7d6d4fd642c2e89f216ca5f8c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 13 Nov 2023 01:24:43 -0800 Subject: [PATCH] Decode file path URL in filesystem operations --- .changeset/yellow-swans-flash.md | 5 +++++ packages/runtime/src/switch.ts | 16 ++++++++-------- packages/runtime/src/utils.ts | 6 +++++- 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 .changeset/yellow-swans-flash.md diff --git a/.changeset/yellow-swans-flash.md b/.changeset/yellow-swans-flash.md new file mode 100644 index 00000000..c1ac8176 --- /dev/null +++ b/.changeset/yellow-swans-flash.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Decode file path URL in filesystem operations diff --git a/packages/runtime/src/switch.ts b/packages/runtime/src/switch.ts index 0a088a7b..846a7e99 100644 --- a/packages/runtime/src/switch.ts +++ b/packages/runtime/src/switch.ts @@ -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'; @@ -534,7 +534,7 @@ export class SwitchClass extends EventTarget { * ``` */ chdir(dir: PathLike) { - return this.native.chdir(String(dir)); + return this.native.chdir(pathToString(dir)); } /** @@ -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)); } /** @@ -577,7 +577,7 @@ export class SwitchClass extends EventTarget { * ``` */ readDirSync(path: PathLike) { - return this.native.readDirSync(String(path)); + return this.native.readDirSync(pathToString(path)); } /** @@ -592,7 +592,7 @@ export class SwitchClass extends EventTarget { * ``` */ readFileSync(path: PathLike) { - return this.native.readFileSync(String(path)); + return this.native.readFileSync(pathToString(path)); } /** @@ -606,14 +606,14 @@ 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)); } /** @@ -621,7 +621,7 @@ export class SwitchClass extends EventTarget { * 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)); } /** diff --git a/packages/runtime/src/utils.ts b/packages/runtime/src/utils.ts index dce5b87c..4ef53694 100644 --- a/packages/runtime/src/utils.ts +++ b/packages/runtime/src/utils.ts @@ -1,4 +1,4 @@ -import type { BufferSource } from './types'; +import type { BufferSource, PathLike } from './types'; import { INTERNAL_SYMBOL, type Callback, @@ -60,6 +60,10 @@ export function assertInternalConstructor(a: ArrayLike) { 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 { pending = true; promise: Promise;