@bytecodealliance/preview2-shim 0.0.4 → 0.0.6

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 (45) hide show
  1. package/README.md +10 -20
  2. package/lib/browser/environment-preopens.js +4 -0
  3. package/lib/browser/filesystem.js +121 -22
  4. package/lib/browser/http.js +12 -7
  5. package/lib/browser/index.js +5 -7
  6. package/lib/browser/instance-monotonic-clock.js +4 -0
  7. package/lib/browser/instance-wall-clock.js +4 -0
  8. package/lib/browser/io.js +10 -23
  9. package/lib/browser/monotonic-clock.js +2 -2
  10. package/lib/browser/preopens.js +11 -0
  11. package/lib/browser/random.js +14 -1
  12. package/lib/browser/stderr.js +1 -0
  13. package/lib/browser/streams.js +17 -2
  14. package/lib/browser/timezone.js +12 -0
  15. package/lib/browser/wall-clock.js +2 -2
  16. package/lib/http/make-request.js +2 -2
  17. package/lib/http/wasi-http.js +335 -0
  18. package/lib/nodejs/environment-preopens.js +4 -0
  19. package/lib/nodejs/filesystem.js +109 -23
  20. package/lib/nodejs/index.js +6 -8
  21. package/lib/nodejs/instance-monotonic-clock.js +4 -0
  22. package/lib/nodejs/instance-wall-clock.js +4 -0
  23. package/lib/nodejs/io.js +16 -24
  24. package/lib/nodejs/monotonic-clock.js +6 -10
  25. package/lib/nodejs/preopens.js +11 -0
  26. package/lib/nodejs/random.js +14 -1
  27. package/lib/nodejs/stderr.js +1 -0
  28. package/lib/nodejs/streams.js +22 -4
  29. package/lib/nodejs/timezone.js +12 -0
  30. package/lib/nodejs/wall-clock.js +2 -6
  31. package/package.json +1 -1
  32. package/types/imports/environment.d.ts +1 -0
  33. package/types/imports/monotonic-clock.d.ts +3 -5
  34. package/types/imports/preopens.d.ts +15 -0
  35. package/types/imports/wall-clock.d.ts +2 -4
  36. package/types/wasi-reactor.d.ts +8 -8
  37. package/lib/browser/clocks.js +0 -47
  38. package/lib/browser/default-clocks.js +0 -7
  39. package/lib/nodejs/default-clocks.js +0 -7
  40. package/types/imports/environment-preopens.d.ts +0 -5
  41. package/types/imports/instance-monotonic-clock.d.ts +0 -5
  42. package/types/imports/instance-wall-clock.d.ts +0 -5
  43. package/types/imports/stderr.d.ts +0 -3
  44. /package/lib/browser/{console.js → logging.js} +0 -0
  45. /package/lib/nodejs/{console.js → logging.js} +0 -0
package/README.md CHANGED
@@ -6,26 +6,16 @@ Currently supports Node.js and browser versions, but alternative implementations
6
6
 
7
7
  ## Implementation Status
8
8
 
9
- | Interface | Node.js |Browser |
10
- | --------------- | ---------------------:|---------------------:|
11
- | Command | :x: | :x: |
12
- | Default Clocks | :heavy_check_mark: | :heavy_check_mark: |
13
- | DNS | :x: | :x: |
14
- | Environment | :x: | :x: |
15
- | Exit | :heavy_check_mark: | :heavy_check_mark: |
16
- | Filesystem | :x: | :x: |
17
- | Http | Experimental | Experimental |
18
- | IO | Partial | Partial |
19
- | IP | :x: | :x: |
20
- | Logging | :heavy_check_mark: | :heavy_check_mark: |
21
- | Monotonic Clock | Missing `resolution` | Missing `resolution` |
22
- | Net | :x: | :x: |
23
- | Poll | :x: | :x: |
24
- | Random | :heavy_check_mark: | :heavy_check_mark: |
25
- | Stderr | :heavy_check_mark: | :heavy_check_mark: |
26
- | TCP | :x: | :x: |
27
- | Timezone | :x: | :x: |
28
- | Wall Clock | Missing `resolution` | Missing `resolution` |
9
+ | Interface | Node.js | Browser |
10
+ | --------------- | ----------------------------:|-----------------------------:|
11
+ | Clocks | Pending timezone, resolution | Pending timezone, resolution |
12
+ | Filesystem | :x: | :x: |
13
+ | HTTP | :x: | :x: |
14
+ | IO | :x: | :x: |
15
+ | Logging | :heavy_check_mark: | :heavy_check_mark: |
16
+ | Poll | :x: | :x: |
17
+ | Random | :heavy_check_mark: | :heavy_check_mark: |
18
+ | Sockets | :x: | :x: |
29
19
 
30
20
  # License
31
21
 
@@ -0,0 +1,4 @@
1
+ // TODO: remove
2
+ export function preopens () {
3
+ return [];
4
+ }
@@ -1,51 +1,150 @@
1
- export function flags(fd) {
1
+ // export interface DescriptorStat {
2
+ // dev: Device,
3
+ // ino: Inode,
4
+ // type: DescriptorType,
5
+ // nlink: Linkcount,
6
+ // size: Filesize,
7
+ // atim: Timestamp,
8
+ // mtim: Timestamp,
9
+ // ctim: Timestamp,
10
+ // }
11
+
12
+ export function readViaStream(fd, offset) {
13
+ console.log(`[filesystem] READ STREAM ${fd} ${offset}`);
14
+ }
15
+
16
+ export function writeViaStream(fd, offset) {
17
+ console.log(`[filesystem] WRITE STREAM ${fd} ${offset}`);
18
+ }
19
+
20
+ export function appendViaStream(fd) {
21
+ console.log(`[filesystem] APPEND STREAM ${fd}`);
22
+ }
23
+
24
+ export function advise(fd, offset, length, advice) {
25
+ console.log(`[filesystem] ADVISE`, fd, offset, length, advice);
26
+ }
27
+
28
+ export function syncData(fd) {
29
+ console.log(`[filesystem] SYNC DATA ${fd}`);
30
+ }
31
+
32
+ export function getFlags(fd) {
2
33
  console.log(`[filesystem] FLAGS FOR ${fd}`);
3
34
  }
4
35
 
36
+ export function getType(fd) {
37
+ console.log(`[filesystem] GET TYPE ${fd}`);
38
+ }
39
+
5
40
  export function setFlags(fd, flags) {
6
41
  console.log(`[filesystem] SET FLAGS ${fd} ${JSON.stringify(flags)}`);
7
42
  }
8
43
 
9
- export function dropDescriptor(fd) {
10
- console.log(`[filesystem] CLOSE: ${fd}`);
44
+ export function setSize(fd, size) {
45
+ console.log(`[filesystem] SET SIZE`, fd, size);
11
46
  }
12
47
 
13
- export function removeDirectoryAt(fd, path) {
14
- console.log(`[filesystem] RM DIR: ${fd} ${path}`);
48
+ export function setTimes(fd, dataAccessTimestamp, dataModificationTimestamp) {
49
+ console.log(`[filesystem] SET TIMES`, fd, dataAccessTimestamp, dataModificationTimestamp);
15
50
  }
16
51
 
17
- export function unlinkFileAt(fd, path) {
18
- console.log(`[filesystem] UNLINK: ${fd} ${path}`);
52
+ export function read(fd, length, offset) {
53
+ console.log(`[filesystem] READ`, fd, length, offset);
19
54
  }
20
55
 
21
- export function writeViaStream(fd, offset) {
22
- console.log(`[filesystem] WRITE STREAM ${fd} ${offset}`);
56
+ export function write(fd, buffer, offset) {
57
+ console.log(`[filesystem] WRITE`, fd, buffer, offset);
23
58
  }
24
59
 
25
- export function appendViaStream(fd, offset) {
26
- console.log(`[filesystem] APPEND STREAM ${fd} ${offset}`);
60
+ export function readDirectory(fd) {
61
+ console.log(`[filesystem] READ DIR`, fd);
27
62
  }
28
63
 
29
- export function readViaStream(fd, offset) {
30
- console.log(`[filesystem] READ STREAM ${fd} ${offset}`);
64
+ export function sync(fd) {
65
+ console.log(`[filesystem] SYNC`, fd);
31
66
  }
32
67
 
33
- export function openAt(fd, atFlags, path, offset) {
34
- console.log(`[filesystem] OPEN AT ${fd}`, atFlags, path, offset);
68
+ export function createDirectoryAt(fd, path) {
69
+ console.log(`[filesystem] CREATE DIRECTORY`, fd, path);
35
70
  }
36
71
 
37
72
  export function stat(fd) {
38
- console.log(`[filesystem] STAT: ${fd}`);
73
+ console.log(`[filesystem] STAT`, fd);
74
+ }
75
+
76
+ export function statAt(fd, pathFlags, path) {
77
+ console.log(`[filesystem] STAT`, fd, pathFlags, path);
78
+ }
79
+
80
+ export function setTimesAt(fd) {
81
+ console.log(`[filesystem] SET TIMES AT`, fd);
82
+ }
83
+
84
+ export function linkAt(fd) {
85
+ console.log(`[filesystem] LINK AT`, fd);
39
86
  }
40
87
 
41
- export function todoType(fd) {
42
- console.log(`[filesystem] TODO TYPE: ${fd}`);
88
+ export function openAt(fd) {
89
+ console.log(`[filesystem] OPEN AT`, fd);
90
+ }
91
+
92
+ export function readlinkAt(fd) {
93
+ console.log(`[filesystem] READLINK AT`, fd);
94
+ }
95
+
96
+ export function removeDirectoryAt(fd) {
97
+ console.log(`[filesystem] REMOVE DIR AT`, fd);
98
+ }
99
+
100
+ export function renameAt(fd) {
101
+ console.log(`[filesystem] RENAME AT`, fd);
102
+ }
103
+
104
+ export function symlinkAt(fd) {
105
+ console.log(`[filesystem] SYMLINK AT`, fd);
106
+ }
107
+
108
+ export function unlinkFileAt(fd) {
109
+ console.log(`[filesystem] UNLINK FILE AT`, fd);
110
+ }
111
+
112
+ export function changeFilePermissionsAt(fd) {
113
+ console.log(`[filesystem] CHANGE FILE PERMISSIONS AT`, fd);
114
+ }
115
+
116
+ export function changeDirectoryPermissionsAt(fd) {
117
+ console.log(`[filesystem] CHANGE DIR PERMISSIONS AT`, fd);
118
+ }
119
+
120
+ export function lockShared(fd) {
121
+ console.log(`[filesystem] LOCK SHARED`, fd);
122
+ }
123
+
124
+ export function lockExclusive(fd) {
125
+ console.log(`[filesystem] LOCK EXCLUSIVE`, fd);
126
+ }
127
+
128
+ export function tryLockShared(fd) {
129
+ console.log(`[filesystem] TRY LOCK SHARED`, fd);
130
+ }
131
+
132
+ export function tryLockExclusive(fd) {
133
+ console.log(`[filesystem] TRY LOCK EXCLUSIVE`, fd);
134
+ }
135
+
136
+ export function unlock(fd) {
137
+ console.log(`[filesystem] UNLOCK`, fd);
138
+ }
139
+
140
+ export function dropDescriptor(fd) {
141
+ console.log(`[filesystem] DROP DESCRIPTOR`, fd);
43
142
  }
44
143
 
45
- export function dropDirEntryStream(s) {
46
- console.log(`[filesystem] CLOSE DIR ENTRY STREAM: ${s}`);
144
+ export function readDirectoryEntry(stream) {
145
+ console.log(`[filesystem] READ DIRECTRY ENTRY`, stream);
47
146
  }
48
147
 
49
- export function getPreopens () {
50
- console.log(`[filesystem] GET PREOPENS`);
148
+ export function dropDirectoryEntryStream(stream) {
149
+ console.log(`[filesystem] DROP DIRECTORY ENTRY`, stream);
51
150
  }
@@ -9,19 +9,24 @@ export function send(req) {
9
9
  try {
10
10
  const xhr = new XMLHttpRequest();
11
11
  xhr.open(req.method.toString(), req.uri, false);
12
- for (let [name, value] of req.headers) {
13
- if (name !== "user-agent") {
12
+ const requestHeaders = new Headers(req.headers);
13
+ for (let [name, value] of requestHeaders.entries()) {
14
+ if (name !== "user-agent" && name !== "host") {
14
15
  xhr.setRequestHeader(name, value);
15
16
  }
16
17
  }
17
- xhr.send(req.body.length > 0 ? req.body : null);
18
+ xhr.send(req.body && req.body.length > 0 ? req.body : null);
18
19
  const body = xhr.response ? new TextEncoder().encode(xhr.response) : undefined;
20
+ const headers = [];
21
+ xhr.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach((line) => {
22
+ var parts = line.split(': ');
23
+ var key = parts.shift();
24
+ var value = parts.join(': ');
25
+ headers.push([key, value]);
26
+ });
19
27
  return {
20
28
  status: xhr.status,
21
- headers: xhr
22
- .getAllResponseHeaders()
23
- .trim()
24
- .split(/[\r\n]+/),
29
+ headers,
25
30
  body,
26
31
  };
27
32
  } catch (err) {
@@ -1,35 +1,33 @@
1
- import * as defaultClocks from "./default-clocks.js";
1
+ import * as logging from "./logging.js";
2
2
  import * as defaultOutgoingHttp from "./default-outgoing-HTTP.js";
3
3
  import * as environment from "./environment.js";
4
4
  import * as exit from "./exit.js";
5
5
  import * as filesystem from "./filesystem.js";
6
6
  import * as http from "./http.js";
7
- import * as io from "./io.js";
8
- import * as logging from "./console.js";
9
7
  import * as monotonicClock from "./monotonic-clock.js";
10
8
  import * as poll from "./poll.js";
9
+ import * as preopens from "./preopens.js";
11
10
  import * as random from "./random.js";
12
- import * as stderr from "./stderr.js";
13
11
  import * as streams from "./streams.js";
14
12
  import * as types from "./types.js";
15
13
  import * as wallClock from "./wall-clock.js";
16
14
 
17
15
  export const importObject = {
18
- "default-clocks": defaultClocks,
19
16
  "default-outgoing-HTTP": defaultOutgoingHttp,
20
17
  "environment": environment,
21
18
  "exit": exit,
22
19
  "filesystem": filesystem,
23
20
  "http": http,
24
- "io": io,
25
21
  "logging": logging,
26
22
  "monotonic-clock": monotonicClock,
27
23
  "poll": poll,
24
+ "preopens": preopens,
28
25
  "random": random,
29
- "stderr": stderr,
30
26
  "streams": streams,
31
27
  "types": types,
32
28
  "wall-clock": wallClock,
33
29
  };
34
30
 
31
+ export { WasiHttp } from "../http/wasi-http.js";
32
+
35
33
  export default importObject;
@@ -0,0 +1,4 @@
1
+ // TODO: remove
2
+ export function instanceMonotonicClock() {
3
+ return 0;
4
+ }
@@ -0,0 +1,4 @@
1
+ // TODO: remove
2
+ export function instanceWallClock() {
3
+ return 1;
4
+ }
package/lib/browser/io.js CHANGED
@@ -1,17 +1,9 @@
1
- export function dropInputStream(f) {
2
- console.log(`[io] Drop input stream ${f}`);
1
+ export function read(s, len) {
2
+ console.log(`[io] Read ${s} ${len}`);
3
3
  }
4
4
 
5
- export function dropOutputStream(f) {
6
- console.log(`[io] Drop output stream ${f}`);
7
- }
8
-
9
- export function read(src, len) {
10
- console.log(`[io] Read ${src} ${len}`);
11
- }
12
-
13
- export function write(dst, buf) {
14
- switch (dst) {
5
+ export function write(s, buf) {
6
+ switch (s) {
15
7
  case 0:
16
8
  throw new Error(`TODO: write stdin`);
17
9
  case 1: {
@@ -19,17 +11,12 @@ export function write(dst, buf) {
19
11
  console.log(decoder.decode(buf));
20
12
  return BigInt(buf.byteLength);
21
13
  }
22
- case 2:
23
- throw new Error(`TODO: write stdout`);
14
+ case 2: {
15
+ const decoder = new TextDecoder();
16
+ console.error(decoder.decode(buf));
17
+ return BigInt(buf.byteLength);
18
+ }
24
19
  default:
25
- throw new Error(`TODO: write ${dst}`);
20
+ throw new Error(`TODO: write ${s}`);
26
21
  }
27
22
  }
28
-
29
- export function skip(src, len) {
30
- console.log(`[io] Skip ${src}`, len);
31
- }
32
-
33
- export function write_repeated(dst, byte, len) {
34
- console.log(`[io] Write repeated ${dst}`, byte, len);
35
- }
@@ -1,6 +1,6 @@
1
1
 
2
2
  export function resolution(clock) {
3
- console.log(`[clocks] Monotonic clock resolution ${clock}`);
3
+ console.log(`[monotonic-clock] Monotonic clock resolution ${clock}`);
4
4
  }
5
5
 
6
6
  let hrStart = hrtimeBigint();
@@ -9,7 +9,7 @@ export function now(clock) {
9
9
  if (clock === 0) {
10
10
  return hrtimeBigint() - hrStart;
11
11
  }
12
- console.log("UNKNOWN CLOCK");
12
+ console.log(`[monotonic clock] Unknown clock ${clock}`);
13
13
  }
14
14
 
15
15
  function hrtime(previousTimestamp) {
@@ -0,0 +1,11 @@
1
+ export function getStdio () {
2
+ return {
3
+ stdin: 0,
4
+ stdout: 1,
5
+ stderr: 2,
6
+ };
7
+ }
8
+
9
+ export function getDirectories () {
10
+ return [];
11
+ }
@@ -1,7 +1,7 @@
1
1
  const MAX_BYTES = 65536;
2
2
 
3
3
  export function getRandomBytes(len) {
4
- const bytes = new Uint8Array(len);
4
+ const bytes = new Uint8Array(Number(len));
5
5
 
6
6
  if (len > MAX_BYTES) {
7
7
  // this is the max bytes crypto.getRandomValues
@@ -17,3 +17,16 @@ export function getRandomBytes(len) {
17
17
 
18
18
  return bytes;
19
19
  }
20
+
21
+ export function getRandomU64 () {
22
+ return crypto.getRandomValues(new BigUint64Array(1))[0];
23
+ }
24
+
25
+ let insecureRandomValue1, insecureRandomValue2;
26
+ export function insecureRandom () {
27
+ if (insecureRandomValue1 === undefined) {
28
+ insecureRandomValue1 = getRandomU64();
29
+ insecureRandomValue2 = getRandomU64();
30
+ }
31
+ return [insecureRandomValue1, insecureRandomValue2];
32
+ }
@@ -1,3 +1,4 @@
1
+ // TODO: remove
1
2
  export function print(message) {
2
3
  console.error(message);
3
4
  }
@@ -16,8 +16,23 @@ export function subscribeToInputStream(s) {
16
16
  export function dropInputStream(s) {
17
17
  console.log(`[streams] Drop input stream ${s}`);
18
18
  }
19
- export function write(s, _buf) {
20
- console.log(`[streams] Write ${s}`);
19
+ export function write(s, buf) {
20
+ switch (s) {
21
+ case 0:
22
+ throw new Error(`TODO: write stdin`);
23
+ case 1: {
24
+ const decoder = new TextDecoder();
25
+ console.log(decoder.decode(buf));
26
+ return BigInt(buf.byteLength);
27
+ }
28
+ case 2: {
29
+ const decoder = new TextDecoder();
30
+ console.error(decoder.decode(buf));
31
+ return BigInt(buf.byteLength);
32
+ }
33
+ default:
34
+ throw new Error(`TODO: write ${s}`);
35
+ }
21
36
  }
22
37
  export function blockingWrite(s, _buf) {
23
38
  console.log(`[streams] Blocking write ${s}`);
@@ -0,0 +1,12 @@
1
+ export function display (timezone, when) {
2
+ console.log(`[timezone] DISPLAY ${timezone} ${when}`);
3
+ }
4
+
5
+ export function utcOffset (timezone, when) {
6
+ console.log(`[timezone] UTC OFFSET ${timezone} ${when}`);
7
+ return 0;
8
+ }
9
+
10
+ export function dropTimezone (timezone) {
11
+ console.log(`[timezone] DROP ${timezone}`);
12
+ }
@@ -4,9 +4,9 @@ export function now(clock) {
4
4
  const nanoseconds = (Date.now() % 1000) * 1000 * 1000;
5
5
  return { seconds, nanoseconds };
6
6
  }
7
- console.log("[clocks] UNKNOWN CLOCK");
7
+ console.log(`[wall-clock] Unknown clock ${clock}`);
8
8
  }
9
9
 
10
10
  export function resolution(clock) {
11
- console.log(`[clocks] Wall clock resolution ${clock}`);
11
+ console.log(`[wall-clock] Wall clock resolution ${clock}`);
12
12
  }
@@ -10,12 +10,12 @@ async function makeRequest(req) {
10
10
  const resp = await fetch(req.uri, {
11
11
  method: req.method.toString(),
12
12
  headers,
13
- body: req.body.length > 0 ? req.body : undefined,
13
+ body: req.body && req.body.length > 0 ? req.body : undefined,
14
14
  });
15
15
  let arrayBuffer = await resp.arrayBuffer();
16
16
  return JSON.stringify({
17
17
  status: resp.status,
18
- headers: resp.headers,
18
+ headers: Array.from(resp.headers.entries()),
19
19
  body:
20
20
  arrayBuffer.byteLength > 0
21
21
  ? Buffer.from(arrayBuffer).toString("base64")