@bytecodealliance/preview2-shim 0.17.8 → 0.17.9
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.
- package/README.md +18 -18
- package/lib/browser/cli.js +71 -71
- package/lib/browser/clocks.js +32 -32
- package/lib/browser/config.js +3 -3
- package/lib/browser/environment.js +15 -15
- package/lib/browser/filesystem.js +382 -396
- package/lib/browser/http.js +578 -576
- package/lib/browser/index.js +7 -7
- package/lib/browser/io.js +187 -181
- package/lib/browser/random.js +42 -44
- package/lib/browser/sockets.js +52 -52
- package/lib/common/instantiation.js +147 -151
- package/lib/io/calls.js +7 -9
- package/lib/io/worker-http.js +167 -178
- package/lib/io/worker-io.js +338 -406
- package/lib/io/worker-socket-tcp.js +259 -271
- package/lib/io/worker-socket-udp.js +437 -491
- package/lib/io/worker-sockets.js +230 -251
- package/lib/io/worker-thread.js +774 -822
- package/lib/nodejs/cli.js +53 -65
- package/lib/nodejs/clocks.js +49 -52
- package/lib/nodejs/filesystem.js +726 -785
- package/lib/nodejs/http.js +651 -697
- package/lib/nodejs/index.js +7 -7
- package/lib/nodejs/io.js +1 -1
- package/lib/nodejs/random.js +32 -32
- package/lib/nodejs/sockets.js +468 -538
- package/lib/synckit/index.js +85 -94
- package/package.json +11 -10
package/lib/synckit/index.js
CHANGED
|
@@ -23,114 +23,105 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
23
23
|
SOFTWARE.
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
import path from
|
|
26
|
+
import path from "node:path";
|
|
27
27
|
import {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
} from
|
|
28
|
+
MessageChannel,
|
|
29
|
+
Worker,
|
|
30
|
+
receiveMessageOnPort,
|
|
31
|
+
workerData,
|
|
32
|
+
parentPort,
|
|
33
|
+
} from "node:worker_threads";
|
|
34
34
|
|
|
35
35
|
const DEFAULT_WORKER_BUFFER_SIZE = 1024;
|
|
36
36
|
|
|
37
37
|
function extractProperties(object) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
return properties;
|
|
38
|
+
if (object && typeof object === "object") {
|
|
39
|
+
const properties = {};
|
|
40
|
+
for (const key in object) {
|
|
41
|
+
properties[key] = object[key];
|
|
44
42
|
}
|
|
43
|
+
return properties;
|
|
44
|
+
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
const CALL_TIMEOUT = undefined;
|
|
48
48
|
|
|
49
49
|
export function createSyncFn(workerPath, debug, callbackHandler) {
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
if (!path.isAbsolute(workerPath)) {
|
|
51
|
+
throw new Error("`workerPath` must be absolute");
|
|
52
|
+
}
|
|
53
|
+
const { port1: mainPort, port2: workerPort } = new MessageChannel();
|
|
54
|
+
const worker = new Worker(workerPath, {
|
|
55
|
+
workerData: { workerPort, debug },
|
|
56
|
+
transferList: [workerPort],
|
|
57
|
+
execArgv: [],
|
|
58
|
+
});
|
|
59
|
+
worker.on("message", ({ type, id, payload }) => {
|
|
60
|
+
if (!type) {
|
|
61
|
+
throw new Error("Internal error: Expected a type of a worker callback");
|
|
52
62
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
callbackHandler(type, id, payload);
|
|
66
|
-
});
|
|
67
|
-
let nextID = 0;
|
|
68
|
-
const syncFn = (...args) => {
|
|
69
|
-
const cid = nextID++;
|
|
70
|
-
const sharedBuffer = new SharedArrayBuffer(DEFAULT_WORKER_BUFFER_SIZE);
|
|
71
|
-
const sharedBufferView = new Int32Array(sharedBuffer);
|
|
72
|
-
const msg = { sharedBuffer, cid, args };
|
|
73
|
-
worker.postMessage(msg);
|
|
74
|
-
const status = Atomics.wait(sharedBufferView, 0, 0, CALL_TIMEOUT);
|
|
75
|
-
if (!['ok', 'not-equal'].includes(status)) {
|
|
76
|
-
throw new Error('Internal error: Atomics.wait() failed: ' + status);
|
|
77
|
-
}
|
|
78
|
-
const {
|
|
79
|
-
cid: cid2,
|
|
80
|
-
result,
|
|
81
|
-
error,
|
|
82
|
-
properties,
|
|
83
|
-
} = receiveMessageOnPort(mainPort).message;
|
|
84
|
-
if (cid !== cid2) {
|
|
85
|
-
throw new Error(
|
|
86
|
-
`Internal error: Expected id ${cid} but got id ${cid2}`
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
if (error) {
|
|
90
|
-
if (error instanceof Error) {
|
|
91
|
-
throw Object.assign(error, properties);
|
|
92
|
-
}
|
|
93
|
-
throw error;
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
};
|
|
97
|
-
if (worker.unref) {
|
|
98
|
-
worker.unref();
|
|
63
|
+
callbackHandler(type, id, payload);
|
|
64
|
+
});
|
|
65
|
+
let nextID = 0;
|
|
66
|
+
const syncFn = (...args) => {
|
|
67
|
+
const cid = nextID++;
|
|
68
|
+
const sharedBuffer = new SharedArrayBuffer(DEFAULT_WORKER_BUFFER_SIZE);
|
|
69
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
|
70
|
+
const msg = { sharedBuffer, cid, args };
|
|
71
|
+
worker.postMessage(msg);
|
|
72
|
+
const status = Atomics.wait(sharedBufferView, 0, 0, CALL_TIMEOUT);
|
|
73
|
+
if (!["ok", "not-equal"].includes(status)) {
|
|
74
|
+
throw new Error("Internal error: Atomics.wait() failed: " + status);
|
|
99
75
|
}
|
|
100
|
-
|
|
76
|
+
const { cid: cid2, result, error, properties } = receiveMessageOnPort(mainPort).message;
|
|
77
|
+
if (cid !== cid2) {
|
|
78
|
+
throw new Error(`Internal error: Expected id ${cid} but got id ${cid2}`);
|
|
79
|
+
}
|
|
80
|
+
if (error) {
|
|
81
|
+
if (error instanceof Error) {
|
|
82
|
+
throw Object.assign(error, properties);
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
};
|
|
88
|
+
if (worker.unref) {
|
|
89
|
+
worker.unref();
|
|
90
|
+
}
|
|
91
|
+
return syncFn;
|
|
101
92
|
}
|
|
102
93
|
|
|
103
94
|
export function runAsWorker(fn) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
95
|
+
if (!workerData) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const { workerPort, debug } = workerData;
|
|
99
|
+
try {
|
|
100
|
+
parentPort.on("message", ({ sharedBuffer, cid, args }) => {
|
|
101
|
+
(async () => {
|
|
102
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
|
103
|
+
let msg;
|
|
104
|
+
try {
|
|
105
|
+
msg = { cid, result: await fn(...args) };
|
|
106
|
+
} catch (error) {
|
|
107
|
+
msg = { cid, error, properties: extractProperties(error) };
|
|
108
|
+
}
|
|
109
|
+
workerPort.postMessage(msg);
|
|
110
|
+
Atomics.add(sharedBufferView, 0, 1);
|
|
111
|
+
Atomics.notify(sharedBufferView, 0);
|
|
112
|
+
})();
|
|
113
|
+
});
|
|
114
|
+
} catch (error) {
|
|
115
|
+
parentPort.on("message", ({ sharedBuffer, cid }) => {
|
|
116
|
+
const sharedBufferView = new Int32Array(sharedBuffer);
|
|
117
|
+
workerPort.postMessage({
|
|
118
|
+
cid,
|
|
119
|
+
error,
|
|
120
|
+
properties: extractProperties(error),
|
|
121
|
+
});
|
|
122
|
+
Atomics.add(sharedBufferView, 0, 1);
|
|
123
|
+
Atomics.notify(sharedBufferView, 0);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return debug;
|
|
136
127
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bytecodealliance/preview2-shim",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.9",
|
|
4
4
|
"description": "WASI Preview2 shim for JS environments",
|
|
5
|
+
"homepage": "https://github.com/bytecodealliance/jco#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/bytecodealliance/jco/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "(Apache-2.0 WITH LLVM-exception)",
|
|
5
10
|
"author": "Guy Bedford, Eduardo Rodrigues<16357187+eduardomourar@users.noreply.github.com>",
|
|
6
11
|
"contributors": [
|
|
7
12
|
{
|
|
@@ -20,17 +25,12 @@
|
|
|
20
25
|
"type": "git",
|
|
21
26
|
"url": "git+https://github.com/bytecodealliance/jco.git"
|
|
22
27
|
},
|
|
23
|
-
"license": "(Apache-2.0 WITH LLVM-exception)",
|
|
24
|
-
"bugs": {
|
|
25
|
-
"url": "https://github.com/bytecodealliance/jco/issues"
|
|
26
|
-
},
|
|
27
|
-
"homepage": "https://github.com/bytecodealliance/jco#readme",
|
|
28
|
-
"type": "module",
|
|
29
|
-
"types": "./types/index.d.ts",
|
|
30
28
|
"files": [
|
|
31
29
|
"types",
|
|
32
30
|
"lib"
|
|
33
31
|
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"types": "./types/index.d.ts",
|
|
34
34
|
"exports": {
|
|
35
35
|
".": {
|
|
36
36
|
"types": "./types/index.d.ts",
|
|
@@ -50,8 +50,9 @@
|
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"types:check": "tsc --noEmit",
|
|
53
|
-
"fmt": "
|
|
54
|
-
"
|
|
53
|
+
"fmt": "oxfmt",
|
|
54
|
+
"fmt:check": "oxfmt --check",
|
|
55
|
+
"lint": "oxlint",
|
|
55
56
|
"lint:fix": "npm run lint -- --fix",
|
|
56
57
|
"test": "vitest run -c test/vitest.ts"
|
|
57
58
|
},
|