Skip to content

Commit e13a19b

Browse files
refactor: make windows patch fixing reusable
Signed-off-by: Victor Adossi <[email protected]>
1 parent 7aa0182 commit e13a19b

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/componentize.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@bytecodealliance/jco';
99
import { spawnSync } from 'node:child_process';
1010
import { tmpdir } from 'node:os';
11-
import { resolve, join, dirname } from 'node:path';
11+
import { join, dirname } from 'node:path';
1212
import { readFile, writeFile, mkdir, rm } from 'node:fs/promises';
1313
import { rmSync, existsSync } from 'node:fs';
1414
import { createHash } from 'node:crypto';
@@ -17,26 +17,13 @@ import {
1717
stubWasi,
1818
} from '../lib/spidermonkey-embedding-splicer.js';
1919
import { fileURLToPath } from 'node:url';
20-
import { cwd, stdout, platform } from 'node:process';
20+
import { cwd, stdout } from 'node:process';
21+
22+
import { maybeWindowsPath } from './platform.js';
23+
2124
export const { version } = JSON.parse(
2225
await readFile(new URL('../package.json', import.meta.url), 'utf8'),
2326
);
24-
const isWindows = platform === 'win32';
25-
26-
function maybeWindowsPath(path) {
27-
if (!path) return path;
28-
const resolvedPath = resolve(path);
29-
if (!isWindows) return resolvedPath;
30-
31-
// Strip any existing UNC prefix check both the format we add as well as what
32-
// the windows API returns when using path.resolve
33-
let cleanPath = resolvedPath;
34-
while (cleanPath.startsWith('\\\\?\\') || cleanPath.startsWith('//?/')) {
35-
cleanPath = cleanPath.substring(4);
36-
}
37-
38-
return '//?/' + cleanPath.replace(/\\/g, '/');
39-
}
4027

4128
/**
4229
* Clean up the given input string by removing the given patterns if

src/platform.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { resolve } from 'node:path';
2+
import { platform } from 'node:process';
3+
4+
export const isWindows = platform === 'win32';
5+
6+
export function maybeWindowsPath(path) {
7+
if (!path) return path;
8+
const resolvedPath = resolve(path);
9+
if (!isWindows) return resolvedPath;
10+
11+
// Strip any existing UNC prefix check both the format we add as well as what
12+
// the windows API returns when using path.resolve
13+
let cleanPath = resolvedPath;
14+
while (cleanPath.startsWith('\\\\?\\') || cleanPath.startsWith('//?/')) {
15+
cleanPath = cleanPath.substring(4);
16+
}
17+
18+
return '//?/' + cleanPath.replace(/\\/g, '/');
19+
}

test/builtins/fetch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { createServer } from 'node:http';
33

44
import { strictEqual, ok } from 'node:assert';
55

6+
import { maybeWindowsPath } from '../../src/platform.js';
7+
68
const FETCH_URL = 'http://localhost';
79

810
export const state = async () => {
911
const { getRandomPort } = await import(
10-
fileURLToPath(new URL('../util.js', import.meta.url))
12+
maybeWindowsPath(new URL('../util.js', import.meta.url))
1113
);
1214
const port = await getRandomPort();
1315
return { port };

0 commit comments

Comments
 (0)