@bytecodealliance/preview2-shim 0.18.1 → 0.20.0

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.
Files changed (82) hide show
  1. package/README.md +15 -15
  2. package/dist/browser/cli.d.ts +16 -0
  3. package/dist/browser/cli.js +109 -0
  4. package/dist/browser/clocks.d.ts +3 -0
  5. package/dist/browser/clocks.js +36 -0
  6. package/dist/browser/config.d.ts +2 -0
  7. package/{lib → dist}/browser/config.js +2 -4
  8. package/dist/browser/environment.d.ts +5 -0
  9. package/dist/browser/environment.js +24 -0
  10. package/dist/browser/filesystem.d.ts +132 -0
  11. package/dist/browser/filesystem.js +488 -0
  12. package/dist/browser/http.d.ts +4 -0
  13. package/dist/browser/http.js +643 -0
  14. package/dist/browser/index.d.ts +7 -0
  15. package/dist/browser/index.js +7 -0
  16. package/dist/browser/io.d.ts +58 -0
  17. package/dist/browser/io.js +205 -0
  18. package/dist/browser/random.d.ts +4 -0
  19. package/dist/browser/random.js +49 -0
  20. package/dist/browser/sockets.d.ts +8 -0
  21. package/dist/browser/sockets.js +66 -0
  22. package/dist/common/instantiation.d.ts +97 -0
  23. package/dist/common/instantiation.js +228 -0
  24. package/dist/io/calls.d.ts +91 -0
  25. package/{lib → dist}/io/calls.js +4 -19
  26. package/dist/io/worker-http.d.ts +17 -0
  27. package/dist/io/worker-http.js +192 -0
  28. package/dist/io/worker-io.d.ts +84 -0
  29. package/dist/io/worker-io.js +381 -0
  30. package/dist/io/worker-socket-tcp.d.ts +34 -0
  31. package/dist/io/worker-socket-tcp.js +251 -0
  32. package/dist/io/worker-socket-udp.d.ts +54 -0
  33. package/dist/io/worker-socket-udp.js +493 -0
  34. package/dist/io/worker-sockets.d.ts +32 -0
  35. package/dist/io/worker-sockets.js +265 -0
  36. package/dist/io/worker-thread.d.ts +73 -0
  37. package/dist/io/worker-thread.js +794 -0
  38. package/dist/nodejs/cli.d.ts +30 -0
  39. package/dist/nodejs/cli.js +86 -0
  40. package/dist/nodejs/clocks.d.ts +3 -0
  41. package/dist/nodejs/clocks.js +61 -0
  42. package/dist/nodejs/filesystem.d.ts +113 -0
  43. package/dist/nodejs/filesystem.js +761 -0
  44. package/dist/nodejs/http.d.ts +82 -0
  45. package/dist/nodejs/http.js +674 -0
  46. package/dist/nodejs/index.d.ts +7 -0
  47. package/dist/nodejs/index.js +7 -0
  48. package/dist/nodejs/io.d.ts +1 -0
  49. package/dist/nodejs/io.js +1 -0
  50. package/dist/nodejs/random.d.ts +4 -0
  51. package/dist/nodejs/random.js +42 -0
  52. package/dist/nodejs/sockets.d.ts +21 -0
  53. package/dist/nodejs/sockets.js +482 -0
  54. package/dist/synckit/index.d.ts +2 -0
  55. package/dist/synckit/index.js +117 -0
  56. package/package.json +28 -15
  57. package/types/instantiation.d.ts +7 -7
  58. package/lib/browser/cli.js +0 -135
  59. package/lib/browser/clocks.js +0 -39
  60. package/lib/browser/environment.js +0 -29
  61. package/lib/browser/filesystem.js +0 -503
  62. package/lib/browser/http.js +0 -707
  63. package/lib/browser/index.js +0 -9
  64. package/lib/browser/io.js +0 -262
  65. package/lib/browser/random.js +0 -56
  66. package/lib/browser/sockets.js +0 -88
  67. package/lib/common/instantiation.js +0 -253
  68. package/lib/io/worker-http.js +0 -206
  69. package/lib/io/worker-io.js +0 -468
  70. package/lib/io/worker-socket-tcp.js +0 -331
  71. package/lib/io/worker-socket-udp.js +0 -587
  72. package/lib/io/worker-sockets.js +0 -364
  73. package/lib/io/worker-thread.js +0 -1051
  74. package/lib/nodejs/cli.js +0 -109
  75. package/lib/nodejs/clocks.js +0 -67
  76. package/lib/nodejs/filesystem.js +0 -829
  77. package/lib/nodejs/http.js +0 -764
  78. package/lib/nodejs/index.js +0 -9
  79. package/lib/nodejs/io.js +0 -1
  80. package/lib/nodejs/random.js +0 -49
  81. package/lib/nodejs/sockets.js +0 -565
  82. package/lib/synckit/index.js +0 -127
@@ -1,127 +0,0 @@
1
- // Based on: https://github.com/un-ts/synckit
2
- /*
3
- MIT License
4
-
5
- Copyright (c) 2021 UnTS
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in all
15
- copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- SOFTWARE.
24
- */
25
-
26
- import path from "node:path";
27
- import {
28
- MessageChannel,
29
- Worker,
30
- receiveMessageOnPort,
31
- workerData,
32
- parentPort,
33
- } from "node:worker_threads";
34
-
35
- const DEFAULT_WORKER_BUFFER_SIZE = 1024;
36
-
37
- function extractProperties(object) {
38
- if (object && typeof object === "object") {
39
- const properties = {};
40
- for (const key in object) {
41
- properties[key] = object[key];
42
- }
43
- return properties;
44
- }
45
- }
46
-
47
- const CALL_TIMEOUT = undefined;
48
-
49
- export function createSyncFn(workerPath, debug, callbackHandler) {
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");
62
- }
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);
75
- }
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;
92
- }
93
-
94
- export function runAsWorker(fn) {
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;
127
- }