@bytecodealliance/preview2-shim 0.0.19 → 0.0.21

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 (40) hide show
  1. package/LICENSE +220 -0
  2. package/README.md +4 -6
  3. package/lib/browser/cli.js +79 -12
  4. package/lib/browser/filesystem.js +168 -198
  5. package/lib/browser/index.js +1 -5
  6. package/lib/browser/io.js +173 -29
  7. package/lib/common/io.js +6 -4
  8. package/lib/{http → common}/make-request.js +1 -1
  9. package/lib/nodejs/cli.js +5 -4
  10. package/lib/nodejs/filesystem.js +106 -73
  11. package/lib/nodejs/http.js +309 -3
  12. package/lib/nodejs/index.js +0 -5
  13. package/package.json +1 -1
  14. package/types/interfaces/wasi-cli-stderr.d.ts +1 -1
  15. package/types/interfaces/wasi-cli-stdin.d.ts +1 -1
  16. package/types/interfaces/wasi-cli-stdout.d.ts +1 -1
  17. package/types/interfaces/wasi-cli-terminal-stderr.d.ts +1 -1
  18. package/types/interfaces/wasi-cli-terminal-stdin.d.ts +1 -1
  19. package/types/interfaces/wasi-cli-terminal-stdout.d.ts +1 -1
  20. package/types/interfaces/wasi-clocks-monotonic-clock.d.ts +1 -1
  21. package/types/interfaces/wasi-clocks-timezone.d.ts +1 -1
  22. package/types/interfaces/wasi-filesystem-preopens.d.ts +1 -1
  23. package/types/interfaces/wasi-filesystem-types.d.ts +8 -8
  24. package/types/interfaces/wasi-http-incoming-handler.d.ts +19 -0
  25. package/types/interfaces/wasi-http-outgoing-handler.d.ts +23 -0
  26. package/types/interfaces/wasi-http-types.d.ts +371 -0
  27. package/types/interfaces/wasi-io-streams.d.ts +13 -21
  28. package/types/interfaces/wasi-sockets-instance-network.d.ts +1 -1
  29. package/types/interfaces/wasi-sockets-ip-name-lookup.d.ts +5 -5
  30. package/types/interfaces/wasi-sockets-tcp-create-socket.d.ts +4 -4
  31. package/types/interfaces/wasi-sockets-tcp.d.ts +7 -7
  32. package/types/interfaces/wasi-sockets-udp-create-socket.d.ts +4 -4
  33. package/types/interfaces/wasi-sockets-udp.d.ts +5 -5
  34. package/types/wasi-cli-command.d.ts +3 -3
  35. package/types/wasi-http-proxy.d.ts +13 -0
  36. package/lib/browser/poll.js +0 -53
  37. package/lib/http/wasi-http.js +0 -382
  38. package/lib/nodejs/poll.js +0 -9
  39. /package/lib/{http/synckit → synckit}/index.d.ts +0 -0
  40. /package/lib/{http/synckit → synckit}/index.js +0 -0
@@ -1,15 +1,7 @@
1
- import { _io } from './io.js';
1
+ import { streams } from './io.js';
2
2
  import { environment } from './cli.js';
3
3
 
4
- const { createStream, getStream, dropStream } = _io;
5
-
6
- let _preopens = [[3, '/']], _rootPreopen = _preopens[0];
7
-
8
- export function _setPreopens (preopens) {
9
- _preopens = preopens;
10
- descriptorCnt = 3 + _preopens.length;
11
- _rootPreopen = _preopens.find(preopen => preopen[1] === '/');
12
- }
4
+ const { InputStream, OutputStream } = streams;
13
5
 
14
6
  let _cwd = null;
15
7
 
@@ -19,11 +11,7 @@ export function _setCwd (cwd) {
19
11
 
20
12
  export function _setFileData (fileData) {
21
13
  _fileData = fileData;
22
- _setPreopens(Object.keys(fileData).map((key) => {
23
- const fd = descriptorCnt++;
24
- descriptorTable[fd] = { entry: fileData[key] };
25
- return [fd, key];
26
- }));
14
+ _rootPreopen[0] = new Descriptor(fileData);
27
15
  const cwd = environment.initialCwd();
28
16
  _setCwd(cwd || '/');
29
17
  }
@@ -32,34 +20,20 @@ export function _getFileData () {
32
20
  return JSON.stringify(_fileData);
33
21
  }
34
22
 
35
- let _fileData = {};
36
-
37
- let descriptorCnt = 4;
38
- const descriptorTable = {
39
- 0: { stream: 0 },
40
- 1: { stream: 1 },
41
- 2: { stream: 2 },
42
- 3: { entry: { dir: {} } },
43
- };
23
+ let _fileData = { dir: {} };
44
24
 
45
25
  const timeZero = {
46
26
  seconds: BigInt(0),
47
27
  nanoseconds: 0
48
28
  };
49
29
 
50
- function getDescriptor (fd) {
51
- const descriptor = descriptorTable[fd];
52
- if (!descriptor) throw 'bad-descriptor';
53
- return descriptor;
54
- }
55
-
56
- function getChildEntry (fd, subpath, openFlags) {
57
- if (subpath === '.' && _rootPreopen && _rootPreopen[0] === fd) {
30
+ function getChildEntry (parentEntry, subpath, openFlags) {
31
+ if (subpath === '.' && _rootPreopen && descriptorGetEntry(_rootPreopen[0]) === parentEntry) {
58
32
  subpath = _cwd;
59
- if (subpath.startsWith('/'))
33
+ if (subpath.startsWith('/') && subpath !== '/')
60
34
  subpath = subpath.slice(1);
61
35
  }
62
- let entry = getDescriptor(fd)?.entry;
36
+ let entry = parentEntry;
63
37
  let segmentIdx;
64
38
  do {
65
39
  if (!entry || !entry.dir) throw 'not-directory';
@@ -76,13 +50,6 @@ function getChildEntry (fd, subpath, openFlags) {
76
50
  return entry;
77
51
  }
78
52
 
79
- function createChildDescriptor (fd, subpath, openFlags) {
80
- const entry = getChildEntry(fd, subpath, openFlags);
81
- const childFd = descriptorCnt++;
82
- descriptorTable[childFd] = { entry };
83
- return childFd;
84
- }
85
-
86
53
  function getSource (fileEntry) {
87
54
  if (typeof fileEntry.source === 'string') {
88
55
  fileEntry.source = new TextEncoder().encode(fileEntry.source);
@@ -90,12 +57,12 @@ function getSource (fileEntry) {
90
57
  return fileEntry.source;
91
58
  }
92
59
 
93
- class DirStream {
60
+ class DirectoryEntryStream {
94
61
  constructor (entries) {
95
62
  this.idx = 0;
96
63
  this.entries = entries;
97
64
  }
98
- next () {
65
+ readDirectoryEntry () {
99
66
  if (this.idx === this.entries.length)
100
67
  return null;
101
68
  const [name, entry] = this.entries[this.idx];
@@ -107,117 +74,120 @@ class DirStream {
107
74
  }
108
75
  }
109
76
 
110
- export const preopens = {
111
- getDirectories () {
112
- return _preopens;
77
+ class Descriptor {
78
+ #stream;
79
+ #entry;
80
+ #mtime = 0;
81
+
82
+ _getEntry (descriptor) {
83
+ return descriptor.#entry;
113
84
  }
114
- }
115
85
 
116
- export const types = {
117
- readViaStream(fd, offset) {
118
- const descriptor = getDescriptor(fd);
119
- const source = getSource(descriptor.entry);
120
- return createStream({
121
- i: Number(offset),
122
- source,
123
- read (len) {
124
- const bytes = this.source.slice(this.i, this.i + Number(len));
125
- this.i += bytes.byteLength;
126
- return [bytes, this.i === this.source.byteLength ? 'ended' : 'open'];
86
+ constructor (entry, isStream) {
87
+ if (isStream)
88
+ this.#stream = entry;
89
+ else
90
+ this.#entry = entry;
91
+ }
92
+
93
+ readViaStream(_offset) {
94
+ const source = getSource(this.#entry);
95
+ let offset = Number(_offset);
96
+ return new InputStream({
97
+ blockingRead (len) {
98
+ if (offset === source.byteLength)
99
+ throw { tag: 'closed' };
100
+ const bytes = source.slice(offset, offset + Number(len));
101
+ offset += bytes.byteLength;
102
+ return bytes;
127
103
  }
128
104
  });
129
- },
105
+ }
130
106
 
131
- writeViaStream(fd, offset) {
132
- const descriptor = getDescriptor(fd);
133
- return createStream({
134
- i: Number(offset),
135
- entry: descriptor.entry,
107
+ writeViaStream(_offset) {
108
+ const entry = this.#entry;
109
+ let offset = Number(_offset);
110
+ return new OutputStream({
136
111
  write (buf) {
137
- const newSource = new Uint8Array(buf.byteLength + this.entry.source.byteLength);
138
- newSource.set(this.entry.source, 0);
139
- newSource.set(buf, this.i);
140
- this.i += buf.byteLength;
141
- this.entry.source = newSource;
112
+ const newSource = new Uint8Array(buf.byteLength + entry.source.byteLength);
113
+ newSource.set(entry.source, 0);
114
+ newSource.set(buf, offset);
115
+ offset += buf.byteLength;
116
+ entry.source = newSource;
142
117
  return buf.byteLength;
143
118
  }
144
119
  });
145
- },
146
-
147
- appendViaStream(fd) {
148
- console.log(`[filesystem] APPEND STREAM ${fd}`);
149
- },
150
-
151
- advise(fd, offset, length, advice) {
152
- console.log(`[filesystem] ADVISE`, fd, offset, length, advice);
153
- },
154
-
155
- syncData(fd) {
156
- console.log(`[filesystem] SYNC DATA ${fd}`);
157
- },
158
-
159
- getFlags(fd) {
160
- console.log(`[filesystem] FLAGS FOR ${fd}`);
161
- },
162
-
163
- getType(fd) {
164
- if (fd < 3) return 'fifo';
165
- const descriptor = getDescriptor(fd);
166
- if (descriptor.stream) return 'fifo';
167
- if (descriptor.entry.dir) return 'directory';
168
- if (descriptor.entry.source) return 'regular-file';
120
+ }
121
+
122
+ appendViaStream() {
123
+ console.log(`[filesystem] APPEND STREAM`);
124
+ }
125
+
126
+ advise(descriptor, offset, length, advice) {
127
+ console.log(`[filesystem] ADVISE`, descriptor, offset, length, advice);
128
+ }
129
+
130
+ syncData() {
131
+ console.log(`[filesystem] SYNC DATA`);
132
+ }
133
+
134
+ getFlags() {
135
+ console.log(`[filesystem] FLAGS FOR`);
136
+ }
137
+
138
+ getType() {
139
+ if (this.#stream) return 'fifo';
140
+ if (this.#entry.dir) return 'directory';
141
+ if (this.#entry.source) return 'regular-file';
169
142
  return 'unknown';
170
- },
143
+ }
171
144
 
172
- setFlags(fd, flags) {
173
- console.log(`[filesystem] SET FLAGS ${fd} ${JSON.stringify(flags)}`);
174
- },
145
+ setFlags(flags) {
146
+ console.log(`[filesystem] SET FLAGS ${JSON.stringify(flags)}`);
147
+ }
175
148
 
176
- setSize(fd, size) {
177
- console.log(`[filesystem] SET SIZE`, fd, size);
178
- },
149
+ setSize(size) {
150
+ console.log(`[filesystem] SET SIZE`, size);
151
+ }
179
152
 
180
- setTimes(fd, dataAccessTimestamp, dataModificationTimestamp) {
181
- console.log(`[filesystem] SET TIMES`, fd, dataAccessTimestamp, dataModificationTimestamp);
182
- },
153
+ setTimes(dataAccessTimestamp, dataModificationTimestamp) {
154
+ console.log(`[filesystem] SET TIMES`, dataAccessTimestamp, dataModificationTimestamp);
155
+ }
183
156
 
184
- read(fd, length, offset) {
185
- const descriptor = getDescriptor(fd);
186
- const source = getSource(descriptor.entry);
157
+ read(length, offset) {
158
+ const source = getSource(this.#entry);
187
159
  return [source.slice(offset, offset + length), offset + length >= source.byteLength];
188
- },
160
+ }
189
161
 
190
- write(fd, buffer, offset) {
191
- const descriptor = getDescriptor(fd);
162
+ write(buffer, offset) {
192
163
  if (offset !== 0) throw 'invalid-seek';
193
- descriptor.entry.source = buffer;
164
+ this.#entry.source = buffer;
194
165
  return buffer.byteLength;
195
- },
166
+ }
196
167
 
197
- readDirectory(fd) {
198
- const descriptor = getDescriptor(fd);
199
- if (!descriptor?.entry?.dir) throw 'bad-descriptor';
200
- return createStream(new DirStream(Object.entries(descriptor.entry.dir).sort(([a], [b]) => a > b ? 1 : -1)));
201
- },
168
+ readDirectory() {
169
+ if (!this.#entry?.dir)
170
+ throw 'bad-descriptor';
171
+ return new DirectoryEntryStream(Object.entries(this.#entry.dir).sort(([a], [b]) => a > b ? 1 : -1));
172
+ }
202
173
 
203
- sync(fd) {
204
- console.log(`[filesystem] SYNC`, fd);
205
- },
174
+ sync() {
175
+ console.log(`[filesystem] SYNC`);
176
+ }
206
177
 
207
- createDirectoryAt(fd, path) {
208
- const entry = getChildEntry(fd, path, { create: true, directory: true });
178
+ createDirectoryAt(path) {
179
+ const entry = getChildEntry(this.#entry, path, { create: true, directory: true });
209
180
  if (entry.source) throw 'exist';
210
- },
181
+ }
211
182
 
212
- stat(fd) {
213
- const descriptor = getDescriptor(fd);
183
+ stat() {
214
184
  let type = 'unknown', size = BigInt(0);
215
- if (descriptor.entry.source) {
185
+ if (this.#entry.source) {
216
186
  type = 'directory';
217
187
  }
218
- else if (descriptor.entry.dir) {
188
+ else if (this.#entry.dir) {
219
189
  type = 'regular-file';
220
- const source = getSource(descriptor.entry);
190
+ const source = getSource(this.#entry);
221
191
  size = BigInt(source.byteLength);
222
192
  }
223
193
  return {
@@ -228,10 +198,10 @@ export const types = {
228
198
  dataModificationTimestamp: timeZero,
229
199
  statusChangeTimestamp: timeZero,
230
200
  }
231
- },
201
+ }
232
202
 
233
- statAt(fd, pathFlags, path) {
234
- const entry = getChildEntry(fd, path);
203
+ statAt(_pathFlags, path) {
204
+ const entry = getChildEntry(this.#entry, path);
235
205
  let type = 'unknown', size = BigInt(0);
236
206
  if (entry.source) {
237
207
  type = 'regular-file';
@@ -249,95 +219,95 @@ export const types = {
249
219
  dataModificationTimestamp: timeZero,
250
220
  statusChangeTimestamp: timeZero,
251
221
  };
252
- },
253
-
254
- setTimesAt(fd) {
255
- console.log(`[filesystem] SET TIMES AT`, fd);
256
- },
257
-
258
- linkAt(fd) {
259
- console.log(`[filesystem] LINK AT`, fd);
260
- },
261
-
262
- openAt(fd, _pathFlags, path, openFlags, _descriptorFlags, _modes) {
263
- return createChildDescriptor(fd, path, openFlags);
264
- },
222
+ }
265
223
 
266
- readlinkAt(fd) {
267
- console.log(`[filesystem] READLINK AT`, fd);
268
- },
224
+ setTimesAt() {
225
+ console.log(`[filesystem] SET TIMES AT`);
226
+ }
269
227
 
270
- removeDirectoryAt(fd) {
271
- console.log(`[filesystem] REMOVE DIR AT`, fd);
272
- },
228
+ linkAt() {
229
+ console.log(`[filesystem] LINK AT`);
230
+ }
273
231
 
274
- renameAt(fd) {
275
- console.log(`[filesystem] RENAME AT`, fd);
276
- },
232
+ openAt(_pathFlags, path, openFlags, _descriptorFlags, _modes) {
233
+ const childEntry = getChildEntry(this.#entry, path, openFlags);
234
+ return new Descriptor(childEntry);
235
+ }
277
236
 
278
- symlinkAt(fd) {
279
- console.log(`[filesystem] SYMLINK AT`, fd);
280
- },
237
+ readlinkAt() {
238
+ console.log(`[filesystem] READLINK AT`);
239
+ }
281
240
 
282
- unlinkFileAt(fd) {
283
- console.log(`[filesystem] UNLINK FILE AT`, fd);
284
- },
241
+ removeDirectoryAt() {
242
+ console.log(`[filesystem] REMOVE DIR AT`);
243
+ }
285
244
 
286
- changeFilePermissionsAt(fd) {
287
- console.log(`[filesystem] CHANGE FILE PERMISSIONS AT`, fd);
288
- },
245
+ renameAt() {
246
+ console.log(`[filesystem] RENAME AT`);
247
+ }
289
248
 
290
- changeDirectoryPermissionsAt(fd) {
291
- console.log(`[filesystem] CHANGE DIR PERMISSIONS AT`, fd);
292
- },
249
+ symlinkAt() {
250
+ console.log(`[filesystem] SYMLINK AT`);
251
+ }
293
252
 
294
- lockShared(fd) {
295
- console.log(`[filesystem] LOCK SHARED`, fd);
296
- },
253
+ unlinkFileAt() {
254
+ console.log(`[filesystem] UNLINK FILE AT`);
255
+ }
297
256
 
298
- lockExclusive(fd) {
299
- console.log(`[filesystem] LOCK EXCLUSIVE`, fd);
300
- },
257
+ changeFilePermissionsAt() {
258
+ console.log(`[filesystem] CHANGE FILE PERMISSIONS AT`);
259
+ }
301
260
 
302
- tryLockShared(fd) {
303
- console.log(`[filesystem] TRY LOCK SHARED`, fd);
304
- },
261
+ changeDirectoryPermissionsAt() {
262
+ console.log(`[filesystem] CHANGE DIR PERMISSIONS AT`);
263
+ }
305
264
 
306
- tryLockExclusive(fd) {
307
- console.log(`[filesystem] TRY LOCK EXCLUSIVE`, fd);
308
- },
265
+ lockShared() {
266
+ console.log(`[filesystem] LOCK SHARED`);
267
+ }
309
268
 
310
- unlock(fd) {
311
- console.log(`[filesystem] UNLOCK`, fd);
312
- },
269
+ lockExclusive() {
270
+ console.log(`[filesystem] LOCK EXCLUSIVE`);
271
+ }
313
272
 
314
- dropDescriptor(fd) {
315
- if (fd < _preopens.length + 3)
316
- return;
317
- delete descriptorTable[fd];
318
- },
273
+ tryLockShared() {
274
+ console.log(`[filesystem] TRY LOCK SHARED`);
275
+ }
319
276
 
320
- readDirectoryEntry(sid) {
321
- return getStream(sid).next();
322
- },
277
+ tryLockExclusive() {
278
+ console.log(`[filesystem] TRY LOCK EXCLUSIVE`);
279
+ }
323
280
 
324
- dropDirectoryEntryStream(sid) {
325
- dropStream(sid);
326
- },
281
+ unlock() {
282
+ console.log(`[filesystem] UNLOCK`);
283
+ }
327
284
 
328
- metadataHash(fd) {
329
- const descriptor = getDescriptor(fd);
285
+ metadataHash() {
330
286
  let upper = BigInt(0);
331
- upper += BigInt(descriptor.mtime || 0);
287
+ upper += BigInt(this.#mtime);
332
288
  return { upper, lower: BigInt(0) };
333
- },
289
+ }
334
290
 
335
- metadataHashAt(fd, _pathFlags, _path) {
336
- const descriptor = getDescriptor(fd);
291
+ metadataHashAt(_pathFlags, _path) {
337
292
  let upper = BigInt(0);
338
- upper += BigInt(descriptor.mtime || 0);
293
+ upper += BigInt(this.#mtime);
339
294
  return { upper, lower: BigInt(0) };
340
295
  }
296
+ }
297
+ const descriptorGetEntry = Descriptor.prototype._getEntry;
298
+ delete Descriptor.prototype._getEntry;
299
+
300
+ let _preopens = [[new Descriptor(_fileData), '/']], _rootPreopen = _preopens[0];
301
+
302
+ export const preopens = {
303
+ getDirectories () {
304
+ return _preopens;
305
+ }
306
+ }
307
+
308
+ export const types = {
309
+ Descriptor,
310
+ DirectoryEntryStream
341
311
  };
342
312
 
343
- export { types as filesystemTypes }
313
+ export { types as filesystemTypes }
@@ -1,8 +1,7 @@
1
1
  import * as clocks from "./clocks.js";
2
2
  import * as filesystem from "./filesystem.js";
3
3
  import * as http from "./http.js";
4
- import * as io from "../common/io.js";
5
- import * as poll from "./poll.js";
4
+ import * as io from "./io.js";
6
5
  import * as random from "./random.js";
7
6
  import * as sockets from "./sockets.js";
8
7
  import * as cli from "./cli.js";
@@ -12,10 +11,7 @@ export {
12
11
  filesystem,
13
12
  http,
14
13
  io,
15
- poll,
16
14
  random,
17
15
  sockets,
18
16
  cli,
19
17
  }
20
-
21
- export { WasiHttp } from "../http/wasi-http.js";