@bytecodealliance/preview2-shim 0.17.7 → 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.
@@ -1,4 +1,4 @@
1
- import * as wasi from '@bytecodealliance/preview2-shim';
1
+ import * as wasi from "@bytecodealliance/preview2-shim";
2
2
 
3
3
  /**
4
4
  * (EXPERIMENTAL) A class that holds WASI shims and can be used to configure
@@ -79,129 +79,125 @@ import * as wasi from '@bytecodealliance/preview2-shim';
79
79
  * @class WASIShim
80
80
  */
81
81
  export class WASIShim {
82
- /** Object that confirms to the shim interface for `wasi:cli` */
83
- #cli;
84
- /** Object that confirms to the shim interface for `wasi:filesystem` */
85
- #filesystem;
86
- /** Object that confirms to the shim interface for `wasi:io` */
87
- #io;
88
- /** Object that confirms to the shim interface for `wasi:random` */
89
- #random;
90
- /** Object that confirms to the shim interface for `wasi:clocks` */
91
- #clocks;
92
- /** Object that confirms to the shim interface for `wasi:sockets` */
93
- #sockets;
94
- /** Object that confirms to the shim interface for `wasi:http` */
95
- #http;
96
- /** Isolated preopens for this instance */
97
- #preopens;
98
- /** Isolated environment for this instance */
99
- #environment;
100
-
101
- /**
102
- * Create a new WASIShim instance.
103
- *
104
- * @param {import('../types/instantiation.d.ts').WASIShimConfig} [config] - Configuration options
105
- */
106
- constructor(config) {
107
- // Support both old 'shims' parameter name and new 'config' style
108
- const shims = config;
109
-
110
- this.#cli = shims?.cli ?? wasi.cli;
111
- this.#filesystem = shims?.filesystem ?? wasi.filesystem;
112
- this.#io = shims?.io ?? wasi.io;
113
- this.#random = shims?.random ?? wasi.random;
114
- this.#clocks = shims?.clocks ?? wasi.clocks;
115
- this.#sockets = shims?.sockets ?? wasi.sockets;
116
- this.#http = shims?.http ?? wasi.http;
117
-
118
- // Extract sandbox options
119
- const sandbox = shims?.sandbox;
120
-
121
- // Create isolated preopens if configured
122
- if (sandbox?.preopens !== undefined) {
123
- this.#preopens = createIsolatedPreopens(sandbox.preopens);
124
- }
125
-
126
- // Create isolated environment if env or args are configured
127
- if (sandbox?.env !== undefined || sandbox?.args !== undefined) {
128
- this.#environment = createIsolatedEnvironment(
129
- sandbox?.env,
130
- sandbox?.args,
131
- this.#cli
132
- );
133
- }
134
-
135
- // Apply network restrictions if disabled
136
- if (sandbox?.enableNetwork === false) {
137
- // Use the sockets module's built-in deny functions
138
- if (this.#sockets._denyTcp) {
139
- this.#sockets._denyTcp();
140
- }
141
- if (this.#sockets._denyUdp) {
142
- this.#sockets._denyUdp();
143
- }
144
- if (this.#sockets._denyDnsLookup) {
145
- this.#sockets._denyDnsLookup();
146
- }
147
- }
82
+ /** Object that confirms to the shim interface for `wasi:cli` */
83
+ #cli;
84
+ /** Object that confirms to the shim interface for `wasi:filesystem` */
85
+ #filesystem;
86
+ /** Object that confirms to the shim interface for `wasi:io` */
87
+ #io;
88
+ /** Object that confirms to the shim interface for `wasi:random` */
89
+ #random;
90
+ /** Object that confirms to the shim interface for `wasi:clocks` */
91
+ #clocks;
92
+ /** Object that confirms to the shim interface for `wasi:sockets` */
93
+ #sockets;
94
+ /** Object that confirms to the shim interface for `wasi:http` */
95
+ #http;
96
+ /** Isolated preopens for this instance */
97
+ #preopens;
98
+ /** Isolated environment for this instance */
99
+ #environment;
100
+
101
+ /**
102
+ * Create a new WASIShim instance.
103
+ *
104
+ * @param {import('../types/instantiation.d.ts').WASIShimConfig} [config] - Configuration options
105
+ */
106
+ constructor(config) {
107
+ // Support both old 'shims' parameter name and new 'config' style
108
+ const shims = config;
109
+
110
+ this.#cli = shims?.cli ?? wasi.cli;
111
+ this.#filesystem = shims?.filesystem ?? wasi.filesystem;
112
+ this.#io = shims?.io ?? wasi.io;
113
+ this.#random = shims?.random ?? wasi.random;
114
+ this.#clocks = shims?.clocks ?? wasi.clocks;
115
+ this.#sockets = shims?.sockets ?? wasi.sockets;
116
+ this.#http = shims?.http ?? wasi.http;
117
+
118
+ // Extract sandbox options
119
+ const sandbox = shims?.sandbox;
120
+
121
+ // Create isolated preopens if configured
122
+ if (sandbox?.preopens !== undefined) {
123
+ this.#preopens = createIsolatedPreopens(sandbox.preopens);
148
124
  }
149
125
 
150
- /**
151
- * Generate an import object for the shim that can be used with
152
- * functions like `instantiate` that are exposed from a transpiled
153
- * WebAssembly component.
154
- *
155
- * @param {GetImportObjectArgs} [opts] - options for import object generation
156
- * @returns {WASIImportObject}
157
- *
158
- * @typedef {{
159
- * asVersion?: number,
160
- * }} GetImportObjectArgs
161
- */
162
- getImportObject(opts) {
163
- const versionSuffix = opts?.asVersion ? `@${opts.asVersion}` : '';
164
-
165
- const obj = {};
166
-
167
- obj[`wasi:cli/environment${versionSuffix}`] = this.#environment ?? this.#cli.environment;
168
- obj[`wasi:cli/exit${versionSuffix}`] = this.#cli.exit;
169
- obj[`wasi:cli/stderr${versionSuffix}`] = this.#cli.stderr;
170
- obj[`wasi:cli/stdin${versionSuffix}`] = this.#cli.stdin;
171
- obj[`wasi:cli/stdout${versionSuffix}`] = this.#cli.stdout;
172
- obj[`wasi:cli/terminal-input${versionSuffix}`] = this.#cli.terminalInput;
173
- obj[`wasi:cli/terminal-output${versionSuffix}`] = this.#cli.terminalOutput;
174
- obj[`wasi:cli/terminal-stderr${versionSuffix}`] = this.#cli.terminalStderr;
175
- obj[`wasi:cli/terminal-stdin${versionSuffix}`] = this.#cli.terminalStdin;
176
- obj[`wasi:cli/terminal-stdout${versionSuffix}`] = this.#cli.terminalStdout;
177
-
178
- obj[`wasi:sockets/instance-network${versionSuffix}`] = this.#sockets.instanceNetwork;
179
- obj[`wasi:sockets/ip-name-lookup${versionSuffix}`] = this.#sockets.ipNameLookup;
180
- obj[`wasi:sockets/network${versionSuffix}`] = this.#sockets.network;
181
- obj[`wasi:sockets/tcp${versionSuffix}`] = this.#sockets.tcp;
182
- obj[`wasi:sockets/tcp-create-socket${versionSuffix}`] = this.#sockets.tcpCreateSocket;
183
- obj[`wasi:sockets/udp${versionSuffix}`] = this.#sockets.udp;
184
- obj[`wasi:sockets/udp-create-socket${versionSuffix}`] = this.#sockets.udpCreateSocket;
185
-
186
- obj[`wasi:filesystem/preopens${versionSuffix}`] = this.#preopens ?? this.#filesystem.preopens;
187
- obj[`wasi:filesystem/types${versionSuffix}`] = this.#filesystem.types;
188
-
189
- obj[`wasi:io/error${versionSuffix}`] = this.#io.error;
190
- obj[`wasi:io/poll${versionSuffix}`] = this.#io.poll;
191
- obj[`wasi:io/streams${versionSuffix}`] = this.#io.streams;
192
-
193
- obj[`wasi:random/random${versionSuffix}`] = this.#random.random;
194
- obj[`wasi:random/insecure${versionSuffix}`] = this.#random.insecure;
195
- obj[`wasi:random/insecure-seed${versionSuffix}`] = this.#random.insecureSeed;
196
-
197
- obj[`wasi:clocks/monotonic-clock${versionSuffix}`] = this.#clocks.monotonicClock;
198
- obj[`wasi:clocks/wall-clock${versionSuffix}`] = this.#clocks.wallClock;
199
-
200
- obj[`wasi:http/types${versionSuffix}`] = this.#http.types;
201
- obj[`wasi:http/outgoing-handler${versionSuffix}`] = this.#http.outgoingHandler;
126
+ // Create isolated environment if env or args are configured
127
+ if (sandbox?.env !== undefined || sandbox?.args !== undefined) {
128
+ this.#environment = createIsolatedEnvironment(sandbox?.env, sandbox?.args, this.#cli);
129
+ }
202
130
 
203
- return obj;
131
+ // Apply network restrictions if disabled
132
+ if (sandbox?.enableNetwork === false) {
133
+ // Use the sockets module's built-in deny functions
134
+ if (this.#sockets._denyTcp) {
135
+ this.#sockets._denyTcp();
136
+ }
137
+ if (this.#sockets._denyUdp) {
138
+ this.#sockets._denyUdp();
139
+ }
140
+ if (this.#sockets._denyDnsLookup) {
141
+ this.#sockets._denyDnsLookup();
142
+ }
204
143
  }
144
+ }
145
+
146
+ /**
147
+ * Generate an import object for the shim that can be used with
148
+ * functions like `instantiate` that are exposed from a transpiled
149
+ * WebAssembly component.
150
+ *
151
+ * @param {GetImportObjectArgs} [opts] - options for import object generation
152
+ * @returns {WASIImportObject}
153
+ *
154
+ * @typedef {{
155
+ * asVersion?: number,
156
+ * }} GetImportObjectArgs
157
+ */
158
+ getImportObject(opts) {
159
+ const versionSuffix = opts?.asVersion ? `@${opts.asVersion}` : "";
160
+
161
+ const obj = {};
162
+
163
+ obj[`wasi:cli/environment${versionSuffix}`] = this.#environment ?? this.#cli.environment;
164
+ obj[`wasi:cli/exit${versionSuffix}`] = this.#cli.exit;
165
+ obj[`wasi:cli/stderr${versionSuffix}`] = this.#cli.stderr;
166
+ obj[`wasi:cli/stdin${versionSuffix}`] = this.#cli.stdin;
167
+ obj[`wasi:cli/stdout${versionSuffix}`] = this.#cli.stdout;
168
+ obj[`wasi:cli/terminal-input${versionSuffix}`] = this.#cli.terminalInput;
169
+ obj[`wasi:cli/terminal-output${versionSuffix}`] = this.#cli.terminalOutput;
170
+ obj[`wasi:cli/terminal-stderr${versionSuffix}`] = this.#cli.terminalStderr;
171
+ obj[`wasi:cli/terminal-stdin${versionSuffix}`] = this.#cli.terminalStdin;
172
+ obj[`wasi:cli/terminal-stdout${versionSuffix}`] = this.#cli.terminalStdout;
173
+
174
+ obj[`wasi:sockets/instance-network${versionSuffix}`] = this.#sockets.instanceNetwork;
175
+ obj[`wasi:sockets/ip-name-lookup${versionSuffix}`] = this.#sockets.ipNameLookup;
176
+ obj[`wasi:sockets/network${versionSuffix}`] = this.#sockets.network;
177
+ obj[`wasi:sockets/tcp${versionSuffix}`] = this.#sockets.tcp;
178
+ obj[`wasi:sockets/tcp-create-socket${versionSuffix}`] = this.#sockets.tcpCreateSocket;
179
+ obj[`wasi:sockets/udp${versionSuffix}`] = this.#sockets.udp;
180
+ obj[`wasi:sockets/udp-create-socket${versionSuffix}`] = this.#sockets.udpCreateSocket;
181
+
182
+ obj[`wasi:filesystem/preopens${versionSuffix}`] = this.#preopens ?? this.#filesystem.preopens;
183
+ obj[`wasi:filesystem/types${versionSuffix}`] = this.#filesystem.types;
184
+
185
+ obj[`wasi:io/error${versionSuffix}`] = this.#io.error;
186
+ obj[`wasi:io/poll${versionSuffix}`] = this.#io.poll;
187
+ obj[`wasi:io/streams${versionSuffix}`] = this.#io.streams;
188
+
189
+ obj[`wasi:random/random${versionSuffix}`] = this.#random.random;
190
+ obj[`wasi:random/insecure${versionSuffix}`] = this.#random.insecure;
191
+ obj[`wasi:random/insecure-seed${versionSuffix}`] = this.#random.insecureSeed;
192
+
193
+ obj[`wasi:clocks/monotonic-clock${versionSuffix}`] = this.#clocks.monotonicClock;
194
+ obj[`wasi:clocks/wall-clock${versionSuffix}`] = this.#clocks.wallClock;
195
+
196
+ obj[`wasi:http/types${versionSuffix}`] = this.#http.types;
197
+ obj[`wasi:http/outgoing-handler${versionSuffix}`] = this.#http.outgoingHandler;
198
+
199
+ return obj;
200
+ }
205
201
  }
206
202
 
207
203
  /**
@@ -211,23 +207,23 @@ export class WASIShim {
211
207
  * @returns {object} A preopens object with Descriptor and getDirectories()
212
208
  */
213
209
  function createIsolatedPreopens(preopensConfig) {
214
- const { types, _createPreopenDescriptor } = wasi.filesystem;
215
- const entries = [];
216
-
217
- // Populate entries using the filesystem's descriptor creation
218
- if (_createPreopenDescriptor) {
219
- for (const [virtualPath, hostPath] of Object.entries(preopensConfig)) {
220
- const descriptor = _createPreopenDescriptor(hostPath);
221
- entries.push([descriptor, virtualPath]);
222
- }
210
+ const { types, _createPreopenDescriptor } = wasi.filesystem;
211
+ const entries = [];
212
+
213
+ // Populate entries using the filesystem's descriptor creation
214
+ if (_createPreopenDescriptor) {
215
+ for (const [virtualPath, hostPath] of Object.entries(preopensConfig)) {
216
+ const descriptor = _createPreopenDescriptor(hostPath);
217
+ entries.push([descriptor, virtualPath]);
223
218
  }
224
-
225
- return {
226
- Descriptor: types.Descriptor,
227
- getDirectories() {
228
- return entries;
229
- },
230
- };
219
+ }
220
+
221
+ return {
222
+ Descriptor: types.Descriptor,
223
+ getDirectories() {
224
+ return entries;
225
+ },
226
+ };
231
227
  }
232
228
 
233
229
  /**
@@ -239,19 +235,19 @@ function createIsolatedPreopens(preopensConfig) {
239
235
  * @returns {object} An isolated CLI environment object
240
236
  */
241
237
  function createIsolatedEnvironment(env, args, baseCli) {
242
- const envEntries = env ? Object.entries(env) : null;
243
- const argsArray = args || null;
244
-
245
- return {
246
- ...baseCli.environment,
247
- getEnvironment() {
248
- return envEntries ?? baseCli.environment.getEnvironment();
249
- },
250
- getArguments() {
251
- return argsArray ?? baseCli.environment.getArguments();
252
- },
253
- initialCwd() {
254
- return baseCli.environment.initialCwd();
255
- },
256
- };
238
+ const envEntries = env ? Object.entries(env) : null;
239
+ const argsArray = args || null;
240
+
241
+ return {
242
+ ...baseCli.environment,
243
+ getEnvironment() {
244
+ return envEntries ?? baseCli.environment.getEnvironment();
245
+ },
246
+ getArguments() {
247
+ return argsArray ?? baseCli.environment.getArguments();
248
+ },
249
+ initialCwd() {
250
+ return baseCli.environment.initialCwd();
251
+ },
252
+ };
257
253
  }
package/lib/io/calls.js CHANGED
@@ -33,8 +33,7 @@ export const OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH = ++call_id << CALL_SHIFT;
33
33
  export const OUTPUT_STREAM_FLUSH = ++call_id << CALL_SHIFT;
34
34
  export const OUTPUT_STREAM_BLOCKING_FLUSH = ++call_id << CALL_SHIFT;
35
35
  export const OUTPUT_STREAM_WRITE_ZEROES = ++call_id << CALL_SHIFT;
36
- export const OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH =
37
- ++call_id << CALL_SHIFT;
36
+ export const OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH = ++call_id << CALL_SHIFT;
38
37
  export const OUTPUT_STREAM_SPLICE = ++call_id << CALL_SHIFT;
39
38
  export const OUTPUT_STREAM_BLOCKING_SPLICE = ++call_id << CALL_SHIFT;
40
39
  export const OUTPUT_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
@@ -103,8 +102,7 @@ export const SOCKET_UDP_SET_RECEIVE_BUFFER_SIZE = ++call_id << CALL_SHIFT;
103
102
  export const SOCKET_UDP_SET_SEND_BUFFER_SIZE = ++call_id << CALL_SHIFT;
104
103
  export const SOCKET_UDP_SET_UNICAST_HOP_LIMIT = ++call_id << CALL_SHIFT;
105
104
  export const SOCKET_INCOMING_DATAGRAM_STREAM_RECEIVE = ++call_id << CALL_SHIFT;
106
- export const SOCKET_OUTGOING_DATAGRAM_STREAM_CHECK_SEND =
107
- ++call_id << CALL_SHIFT;
105
+ export const SOCKET_OUTGOING_DATAGRAM_STREAM_CHECK_SEND = ++call_id << CALL_SHIFT;
108
106
  export const SOCKET_OUTGOING_DATAGRAM_STREAM_SEND = ++call_id << CALL_SHIFT;
109
107
  export const SOCKET_DATAGRAM_STREAM_SUBSCRIBE = ++call_id << CALL_SHIFT;
110
108
  export const SOCKET_DATAGRAM_STREAM_DISPOSE = ++call_id << CALL_SHIFT;
@@ -120,11 +118,11 @@ export const SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST = ++call_id << CALL_SHIFT;
120
118
 
121
119
  export const reverseMap = {};
122
120
 
123
- import * as calls from './calls.js';
121
+ import * as calls from "./calls.js";
124
122
 
125
123
  for (const name of Object.getOwnPropertyNames(calls)) {
126
- if (name === 'reverseMap') {
127
- continue;
128
- }
129
- reverseMap[calls[name]] = name;
124
+ if (name === "reverseMap") {
125
+ continue;
126
+ }
127
+ reverseMap[calls[name]] = name;
130
128
  }