@tybys/wasm-util 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,141 @@
1
1
  import { validateString } from "./util.mjs";
2
2
  const CHAR_DOT = 46; /* . */
3
3
  const CHAR_FORWARD_SLASH = 47; /* / */
4
+ const CHAR_BACKWARD_SLASH = 92; /* \ */
5
+ const CHAR_COLON = 58; /* : */
6
+ const CHAR_UPPERCASE_A = 65; /* A */
7
+ const CHAR_UPPERCASE_Z = 90; /* Z */
8
+ const CHAR_LOWERCASE_A = 97; /* a */
9
+ const CHAR_LOWERCASE_Z = 122; /* z */
10
+ function isPathSeparatorWin(code) {
11
+ return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
12
+ }
13
+ function isWindowsDeviceRoot(code) {
14
+ return (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
15
+ (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
16
+ }
17
+ const _isWin32 = typeof process !== 'undefined' && process.platform === 'win32';
18
+ /**
19
+ * Windows variant of `resolve()`. Mirrors Node's `path.win32.resolve`
20
+ * semantics enough for the WASI shim's needs (drive-letter absolutes
21
+ * + UNC paths + per-drive cwd lookups via `process.env`/`process.cwd`).
22
+ *
23
+ * Why this is needed: on Windows, `FileDescriptor.realPath` is the
24
+ * host realpath returned by `fs.realpathSync(realPath, 'utf8')` — the
25
+ * backslash form `D:\…`. The POSIX-only `resolveImpl` below only
26
+ * treats `/` as a separator, reads `D` as non-`/`, decides realPath
27
+ * is "relative", and produces a garbage joined path. Downstream
28
+ * `fs.openSync(garbage)` then returns `EINVAL` and the WASI caller
29
+ * sees a permanent failure. This function gives us the correct
30
+ * Windows-style resolution without taking a Node-only `path` import
31
+ * (which would break browser bundles of this package).
32
+ *
33
+ * Cribbed from Node's `lib/path.js` `win32.resolve()` (MIT-licensed),
34
+ * trimmed to what WASI realpath resolution actually exercises.
35
+ */
36
+ function resolveWin32(args) {
37
+ let resolvedDevice = '';
38
+ let resolvedTail = '';
39
+ let resolvedAbsolute = false;
40
+ for (let i = args.length - 1; i >= -1; i--) {
41
+ let path;
42
+ if (i >= 0) {
43
+ path = args[i];
44
+ validateString(path, 'path');
45
+ if (path.length === 0)
46
+ continue;
47
+ }
48
+ else if (resolvedDevice.length === 0) {
49
+ path = (typeof process !== 'undefined' && typeof process.cwd === 'function')
50
+ ? process.cwd()
51
+ : '';
52
+ }
53
+ else {
54
+ // Look up per-drive cwd via the `=X:` env var convention; fall back
55
+ // to the global cwd if absent.
56
+ const envKey = `=${resolvedDevice}`;
57
+ const env = (typeof process !== 'undefined') ? process.env : undefined;
58
+ path = (env && typeof env[envKey] === 'string')
59
+ ? env[envKey]
60
+ : (typeof process !== 'undefined' && typeof process.cwd === 'function')
61
+ ? process.cwd()
62
+ : '';
63
+ if (path === undefined ||
64
+ (path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
65
+ path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) {
66
+ path = `${resolvedDevice}\\`;
67
+ }
68
+ }
69
+ const len = path.length;
70
+ let rootEnd = 0;
71
+ let device = '';
72
+ let isAbsolute = false;
73
+ const code = path.charCodeAt(0);
74
+ if (len === 1) {
75
+ if (isPathSeparatorWin(code)) {
76
+ rootEnd = 1;
77
+ isAbsolute = true;
78
+ }
79
+ }
80
+ else if (isPathSeparatorWin(code)) {
81
+ isAbsolute = true;
82
+ if (isPathSeparatorWin(path.charCodeAt(1))) {
83
+ // UNC path: `\\server\share\…`
84
+ let j = 2;
85
+ let last = j;
86
+ while (j < len && !isPathSeparatorWin(path.charCodeAt(j)))
87
+ j++;
88
+ if (j < len && j !== last) {
89
+ const firstPart = path.slice(last, j);
90
+ last = j;
91
+ while (j < len && isPathSeparatorWin(path.charCodeAt(j)))
92
+ j++;
93
+ if (j < len && j !== last) {
94
+ last = j;
95
+ while (j < len && !isPathSeparatorWin(path.charCodeAt(j)))
96
+ j++;
97
+ if (j === len || j !== last) {
98
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
99
+ rootEnd = j;
100
+ }
101
+ }
102
+ }
103
+ }
104
+ else {
105
+ rootEnd = 1;
106
+ }
107
+ }
108
+ else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
109
+ device = path.slice(0, 2);
110
+ rootEnd = 2;
111
+ if (len > 2 && isPathSeparatorWin(path.charCodeAt(2))) {
112
+ isAbsolute = true;
113
+ rootEnd = 3;
114
+ }
115
+ }
116
+ if (device.length > 0) {
117
+ if (resolvedDevice.length > 0) {
118
+ if (device.toLowerCase() !== resolvedDevice.toLowerCase())
119
+ continue;
120
+ }
121
+ else {
122
+ resolvedDevice = device;
123
+ }
124
+ }
125
+ if (resolvedAbsolute) {
126
+ if (resolvedDevice.length > 0)
127
+ break;
128
+ }
129
+ else {
130
+ resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
131
+ resolvedAbsolute = isAbsolute;
132
+ if (isAbsolute && resolvedDevice.length > 0)
133
+ break;
134
+ }
135
+ }
136
+ resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\', isPathSeparatorWin);
137
+ return resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail || '.';
138
+ }
4
139
  function isPosixPathSeparator(code) {
5
140
  return code === CHAR_FORWARD_SLASH;
6
141
  }
@@ -78,6 +213,13 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
78
213
  return res;
79
214
  }
80
215
  export function resolve(...args) {
216
+ // On Windows, host paths are `D:\…` style. The POSIX-only resolver
217
+ // below treats `D` as a non-`/` character and produces garbage; route
218
+ // to the Windows-aware variant. POSIX hosts (Linux/macOS/browser)
219
+ // run the original code path unchanged — `_isWin32` is constant-folded
220
+ // away on those targets.
221
+ if (_isWin32)
222
+ return resolveWin32(args);
81
223
  let resolvedPath = '';
82
224
  let resolvedAbsolute = false;
83
225
  for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
@@ -1,5 +1,25 @@
1
1
  import { _WebAssembly } from "../webassembly.mjs";
2
2
  import { resolve } from "./path.mjs";
3
+ // Linux fcntl flag bits ↔ Windows libuv flag bits. `pathOpen()` builds
4
+ // `flagsRes` using Linux constants (O_CREAT=0x40, O_EXCL=0x80,
5
+ // O_APPEND=0x400). Windows libuv expects different bits (O_CREAT=0x100,
6
+ // O_EXCL=0x400, O_APPEND=0x8). Without translation,
7
+ // `fs.openSync(path, 0x241 /* L:WRONLY|CREAT|TRUNC */)` is rejected
8
+ // with EINVAL because Linux's 0x40 happens to mean O_TEMPORARY on
9
+ // Windows — and O_TEMPORARY without O_CREAT is invalid.
10
+ const _isWin32Flags = typeof process !== 'undefined' && process.platform === 'win32';
11
+ function _toWinOpenFlags(f) {
12
+ let r = f & 3; // RDONLY/WRONLY/RDWR — same on both
13
+ if ((f & 0x40) !== 0)
14
+ r |= 0x100; // O_CREAT: Linux 0x40 -> Windows 0x100
15
+ if ((f & 0x80) !== 0)
16
+ r |= 0x400; // O_EXCL: Linux 0x80 -> Windows 0x400
17
+ if ((f & 0x200) !== 0)
18
+ r |= 0x200; // O_TRUNC: same value on both
19
+ if ((f & 0x400) !== 0)
20
+ r |= 0x8; // O_APPEND: Linux 0x400 -> Windows 0x8
21
+ return r;
22
+ }
3
23
  import { WasiErrno, WasiRights, FileControlFlag, WasiFileControlFlag, WasiFdFlag, WasiFileType, WasiClockid, WasiFstFlag, WasiEventType, WasiSubclockflags } from "./types.mjs";
4
24
  import { concatBuffer, toFileStat, AsyncTable, SyncTable } from "./fd.mjs";
5
25
  import { WasiError } from "./error.mjs";
@@ -65,26 +85,43 @@ function defineName(name, f) {
65
85
  Object.defineProperty(f, 'name', { value: name });
66
86
  return f;
67
87
  }
88
+ function tryCall(f, wasi, args) {
89
+ let r;
90
+ try {
91
+ r = f.apply(wasi, args);
92
+ }
93
+ catch (err) {
94
+ return handleError(err);
95
+ }
96
+ if (isPromiseLike(r)) {
97
+ return r.then(_ => _, handleError);
98
+ }
99
+ return r;
100
+ }
68
101
  function syscallWrap(self, name, f) {
69
- return defineName(name, function () {
70
- if (process.env.NODE_DEBUG_NATIVE === 'wasi') {
102
+ let debug = false;
103
+ const NODE_DEBUG_NATIVE = (() => {
104
+ try {
105
+ return process.env.NODE_DEBUG_NATIVE;
106
+ }
107
+ catch (_) {
108
+ return undefined;
109
+ }
110
+ })();
111
+ if (typeof NODE_DEBUG_NATIVE === 'string' && NODE_DEBUG_NATIVE.split(',').includes('wasi')) {
112
+ debug = true;
113
+ }
114
+ return debug
115
+ ? defineName(name, function () {
71
116
  const args = Array.prototype.slice.call(arguments);
72
117
  let debugArgs = [`${name}(${Array.from({ length: arguments.length }).map(() => '%d').join(', ')})`];
73
118
  debugArgs = debugArgs.concat(args);
74
119
  console.debug.apply(console, debugArgs);
75
- }
76
- let r;
77
- try {
78
- r = f.apply(self, arguments);
79
- }
80
- catch (err) {
81
- return handleError(err);
82
- }
83
- if (isPromiseLike(r)) {
84
- return r.then(_ => _, handleError);
85
- }
86
- return r;
87
- });
120
+ return tryCall(f, self, args);
121
+ })
122
+ : defineName(name, function () {
123
+ return tryCall(f, self, arguments);
124
+ });
88
125
  }
89
126
  function resolvePathSync(fs, fileDescriptor, path, flags) {
90
127
  let resolvedPath = resolve(fileDescriptor.realPath, path);
@@ -1246,7 +1283,7 @@ export class WASI {
1246
1283
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1247
1284
  const fs = getFs(this);
1248
1285
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
1249
- const r = fs.openSync(resolved_path, flagsRes, 0o666);
1286
+ const r = fs.openSync(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
1250
1287
  const filetype = wasi.fds.getFileTypeByFd(r);
1251
1288
  if ((filetype !== WasiFileType.DIRECTORY) &&
1252
1289
  ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0 ||
@@ -1282,7 +1319,7 @@ export class WASI {
1282
1319
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1283
1320
  const fs = getFs(this);
1284
1321
  const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
1285
- const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
1322
+ const r = await fs.promises.open(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
1286
1323
  const filetype = await wasi.fds.getFileTypeByFd(r);
1287
1324
  if ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0 && filetype !== WasiFileType.DIRECTORY) {
1288
1325
  return WasiErrno.ENOTDIR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tybys/wasm-util",
3
- "version": "0.10.0",
3
+ "version": "0.10.2",
4
4
  "description": "WASI polyfill for browser and some wasm util",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./dist/wasm-util.esm-bundler.js",