@bytecodealliance/preview2-shim 0.0.1 → 0.0.2

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.
@@ -0,0 +1,47 @@
1
+ export function wallClockNow(clock) {
2
+ if (clock === 1) {
3
+ const seconds = BigInt(Math.floor(Date.now() / 1000));
4
+ const nanoseconds = (Date.now() % 1000) * 1000 * 1000;
5
+ return { seconds, nanoseconds };
6
+ }
7
+ console.log("[clocks] UNKNOWN CLOCK");
8
+ }
9
+
10
+ export function monotonicClockResolution(clock) {
11
+ console.log(`[clocks] Monotonic clock resolution ${clock}`);
12
+ }
13
+
14
+ export function wallClockResolution(clock) {
15
+ console.log(`[clocks] Wall clock resolution ${clock}`);
16
+ }
17
+
18
+ let hrStart = hrtimeBigint();
19
+
20
+ export function monotonicClockNow(clock) {
21
+ if (clock === 0) {
22
+ return hrtimeBigint() - hrStart;
23
+ }
24
+ console.log("UNKNOWN CLOCK");
25
+ }
26
+
27
+ function hrtime(previousTimestamp) {
28
+ const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
29
+ const clocktime = performance.now() * 1e-3;
30
+ let seconds = Math.floor(clocktime) + baseNow;
31
+ let nanoseconds = Math.floor((clocktime % 1) * 1e9);
32
+
33
+ if (previousTimestamp) {
34
+ seconds = seconds - previousTimestamp[0];
35
+ nanoseconds = nanoseconds - previousTimestamp[1];
36
+ if (nanoseconds < 0) {
37
+ seconds--;
38
+ nanoseconds += 1e9;
39
+ }
40
+ }
41
+ return [seconds, nanoseconds];
42
+ }
43
+
44
+ function hrtimeBigint(time) {
45
+ const diff = hrtime(time);
46
+ return BigInt(diff[0] * 1e9 + diff[1]);
47
+ }
@@ -0,0 +1,7 @@
1
+ export function defaultMonotonicClock() {
2
+ return 0;
3
+ }
4
+
5
+ export function defaultWallClock() {
6
+ return 1;
7
+ }
@@ -0,0 +1,21 @@
1
+ class FailureExit extends Error {
2
+ code = 1;
3
+ constructor() {
4
+ super("failure-exit");
5
+ }
6
+ }
7
+
8
+ class SuccessfulExit extends Error {
9
+ code = 0;
10
+ constructor() {
11
+ super("successful-exit");
12
+ }
13
+ }
14
+
15
+ export function exit(status) {
16
+ console.log(`[exit] Exit: ${JSON.stringify(status)}`);
17
+ if (status.tag === "err") {
18
+ throw new FailureExit();
19
+ }
20
+ throw new SuccessfulExit();
21
+ }
@@ -0,0 +1,47 @@
1
+ export function flags(fd) {
2
+ console.log(`[filesystem] FLAGS FOR ${fd}`);
3
+ }
4
+
5
+ export function setFlags(fd, flags) {
6
+ console.log(`[filesystem] SET FLAGS ${fd} ${JSON.stringify(flags)}`);
7
+ }
8
+
9
+ export function dropDescriptor(fd) {
10
+ console.log(`[filesystem] CLOSE: ${fd}`);
11
+ }
12
+
13
+ export function removeDirectoryAt(fd, path) {
14
+ console.log(`[filesystem] RM DIR: ${fd} ${path}`);
15
+ }
16
+
17
+ export function unlinkFileAt(fd, path) {
18
+ console.log(`[filesystem] UNLINK: ${fd} ${path}`);
19
+ }
20
+
21
+ export function writeViaStream(fd, offset) {
22
+ console.log(`[filesystem] WRITE STREAM ${fd} ${offset}`);
23
+ }
24
+
25
+ export function appendViaStream(fd, offset) {
26
+ console.log(`[filesystem] APPEND STREAM ${fd} ${offset}`);
27
+ }
28
+
29
+ export function readViaStream(fd, offset) {
30
+ console.log(`[filesystem] READ STREAM ${fd} ${offset}`);
31
+ }
32
+
33
+ export function openAt(fd, atFlags, path, offset) {
34
+ console.log(`[filesystem] OPEN AT ${fd}`, atFlags, path, offset);
35
+ }
36
+
37
+ export function stat(fd) {
38
+ console.log(`[filesystem] STAT: ${fd}`);
39
+ }
40
+
41
+ export function todoType(fd) {
42
+ console.log(`[filesystem] TODO TYPE: ${fd}`);
43
+ }
44
+
45
+ export function dropDirEntryStream(s) {
46
+ console.log(`[filesystem] CLOSE DIR ENTRY STREAM: ${s}`);
47
+ }
@@ -0,0 +1,28 @@
1
+ import { UnexpectedError } from "../http/error.js";
2
+
3
+ /**
4
+ * @param {import("../types/wasi-http").Request} req
5
+ * @returns {string}
6
+ */
7
+ export function send(req) {
8
+ console.log(`[http] Send (browser) ${req.uri}`);
9
+ try {
10
+ const xhr = new XMLHttpRequest();
11
+ xhr.open(req.method.toString(), req.uri, false);
12
+ xhr.responseType = "arraybuffer";
13
+ for (let [name, value] of req.headers) {
14
+ xhr.setRequestHeader(name, value);
15
+ }
16
+ xhr.send(req.body.length > 0 ? req.body : null);
17
+ return {
18
+ status: xhr.status,
19
+ headers: xhr
20
+ .getAllResponseHeaders()
21
+ .trim()
22
+ .split(/[\r\n]+/),
23
+ body: xhr.response.byteLength > 0 ? xhr.response : undefined,
24
+ };
25
+ } catch (err) {
26
+ throw new UnexpectedError(err.message);
27
+ }
28
+ }
@@ -0,0 +1,21 @@
1
+ import * as clocks from "./clocks.js";
2
+ import * as defaultClocks from "./default-clocks.js";
3
+ import * as exit from "./exit.js";
4
+ import * as filesystem from "./filesystem.js";
5
+ import * as io from "./io.js";
6
+ import * as logging from "./logging.js";
7
+ import * as poll from "./poll.js";
8
+ import * as random from "./random.js";
9
+ import * as stderr from "./stderr.js";
10
+
11
+ export const importObject = {
12
+ "wasi-clocks": clocks,
13
+ "wasi-default-clocks": defaultClocks,
14
+ "wasi-exit": exit,
15
+ "wasi-filesystem": filesystem,
16
+ "wasi-io": io,
17
+ "wasi-logging": logging,
18
+ "wasi-poll": poll,
19
+ "wasi-random": random,
20
+ "wasi-stderr": stderr,
21
+ };
@@ -0,0 +1,35 @@
1
+ export function dropInputStream(f) {
2
+ console.log(`[io] Drop input stream ${f}`);
3
+ }
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) {
15
+ case 0:
16
+ throw new Error(`TODO: write stdin`);
17
+ case 1: {
18
+ const decoder = new TextDecoder();
19
+ console.log(decoder.decode(buf));
20
+ return BigInt(buf.byteLength);
21
+ }
22
+ case 2:
23
+ throw new Error(`TODO: write stdout`);
24
+ default:
25
+ throw new Error(`TODO: write ${dst}`);
26
+ }
27
+ }
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
+ }
@@ -0,0 +1,12 @@
1
+ const levels = ["trace", "debug", "info", "warn", "error"];
2
+
3
+ let logLevel = levels.indexOf("warn");
4
+
5
+ export function setLevel(level) {
6
+ logLevel = levels.indexOf(level);
7
+ }
8
+
9
+ export function log(level, context, msg) {
10
+ if (logLevel > levels.indexOf(level)) return;
11
+ console[level](`(${context}) ${msg}\n`);
12
+ }
@@ -0,0 +1,35 @@
1
+
2
+ export function resolution(clock) {
3
+ console.log(`[clocks] Monotonic clock resolution ${clock}`);
4
+ }
5
+
6
+ let hrStart = hrtimeBigint();
7
+
8
+ export function now(clock) {
9
+ if (clock === 0) {
10
+ return hrtimeBigint() - hrStart;
11
+ }
12
+ console.log("UNKNOWN CLOCK");
13
+ }
14
+
15
+ function hrtime(previousTimestamp) {
16
+ const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
17
+ const clocktime = performance.now() * 1e-3;
18
+ let seconds = Math.floor(clocktime) + baseNow;
19
+ let nanoseconds = Math.floor((clocktime % 1) * 1e9);
20
+
21
+ if (previousTimestamp) {
22
+ seconds = seconds - previousTimestamp[0];
23
+ nanoseconds = nanoseconds - previousTimestamp[1];
24
+ if (nanoseconds < 0) {
25
+ seconds--;
26
+ nanoseconds += 1e9;
27
+ }
28
+ }
29
+ return [seconds, nanoseconds];
30
+ }
31
+
32
+ function hrtimeBigint(time) {
33
+ const diff = hrtime(time);
34
+ return BigInt(diff[0] * 1e9 + diff[1]);
35
+ }
@@ -0,0 +1,3 @@
1
+ export function pollOneoff(f) {
2
+ console.log(`[poll] Poll oneoff ${f}`);
3
+ }
@@ -0,0 +1,19 @@
1
+ const MAX_BYTES = 65536;
2
+
3
+ export function getRandomBytes(len) {
4
+ const bytes = new Uint8Array(len);
5
+
6
+ if (len > MAX_BYTES) {
7
+ // this is the max bytes crypto.getRandomValues
8
+ // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
9
+ for (var generated = 0; generated < len; generated += MAX_BYTES) {
10
+ // buffer.slice automatically checks if the end is past the end of
11
+ // the buffer so we don't have to here
12
+ crypto.getRandomValues(bytes.slice(generated, generated + MAX_BYTES));
13
+ }
14
+ } else {
15
+ crypto.getRandomValues(bytes);
16
+ }
17
+
18
+ return bytes;
19
+ }
@@ -0,0 +1,3 @@
1
+ export function print(message) {
2
+ console.error(message);
3
+ }
@@ -0,0 +1,12 @@
1
+ export function now(clock) {
2
+ if (clock === 1) {
3
+ const seconds = BigInt(Math.floor(Date.now() / 1000));
4
+ const nanoseconds = (Date.now() % 1000) * 1000 * 1000;
5
+ return { seconds, nanoseconds };
6
+ }
7
+ console.log("[clocks] UNKNOWN CLOCK");
8
+ }
9
+
10
+ export function resolution(clock) {
11
+ console.log(`[clocks] Wall clock resolution ${clock}`);
12
+ }
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ export function defaultMonotonicClock() {
2
+ return 0;
3
+ }
4
+
5
+ export function defaultWallClock() {
6
+ return 1;
7
+ }
@@ -0,0 +1,4 @@
1
+ export function exit(status) {
2
+ console.log(`[exit] Exit: ${JSON.stringify(status)}`);
3
+ process.exit(status.tag === 'err' ? 1 : 0);
4
+ }
@@ -0,0 +1,58 @@
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 flags(fd) {
13
+ console.log(`[filesystem] FLAGS FOR ${fd}`);
14
+ }
15
+
16
+ export function setFlags(fd, flags) {
17
+ console.log(`[filesystem] SET FLAGS ${fd} ${JSON.stringify(flags)}`);
18
+ }
19
+
20
+ export function dropDescriptor(fd) {
21
+ console.log(`[filesystem] CLOSE: ${fd}`);
22
+ }
23
+
24
+ export function removeDirectoryAt(fd, path) {
25
+ console.log(`[filesystem] RM DIR: ${fd} ${path}`);
26
+ }
27
+
28
+ export function unlinkFileAt(fd, path) {
29
+ console.log(`[filesystem] UNLINK: ${fd} ${path}`);
30
+ }
31
+
32
+ export function writeViaStream(fd, offset) {
33
+ console.log(`[filesystem] WRITE STREAM ${fd} ${offset}`);
34
+ }
35
+
36
+ export function appendViaStream(fd, offset) {
37
+ console.log(`[filesystem] APPEND STREAM ${fd} ${offset}`);
38
+ }
39
+
40
+ export function readViaStream(fd, offset) {
41
+ console.log(`[filesystem] READ STREAM ${fd} ${offset}`);
42
+ }
43
+
44
+ export function openAt(fd, atFlags, path, offset) {
45
+ console.log(`[filesystem] OPEN AT ${fd}`, atFlags, path, offset);
46
+ }
47
+
48
+ export function stat(fd) {
49
+ console.log(`[filesystem] STAT: ${fd}`);
50
+ }
51
+
52
+ export function todoType(fd) {
53
+ console.log(`[filesystem] TODO TYPE: ${fd}`);
54
+ }
55
+
56
+ export function dropDirEntryStream(s) {
57
+ console.log(`[filesystem] CLOSE DIR ENTRY STREAM ${s}`);
58
+ }
@@ -0,0 +1,19 @@
1
+ import { createSyncFn } from "synckit";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { UnexpectedError } from "../http/error.js";
5
+
6
+ export function send(req) {
7
+ console.log(`[http] Send (nodejs) ${req.uri}`);
8
+ const dirname = path.dirname(fileURLToPath(import.meta.url));
9
+ const syncFn = createSyncFn(path.resolve(dirname, "../http/make-request.js"));
10
+ let rawResponse = syncFn(req);
11
+ let response = JSON.parse(rawResponse);
12
+ if (response.status) {
13
+ return {
14
+ ...response,
15
+ body: response.body ? Buffer.from(response.body, "base64") : undefined,
16
+ };
17
+ }
18
+ throw new UnexpectedError(response);
19
+ }
@@ -0,0 +1,21 @@
1
+ import * as clocks from "./clocks.js";
2
+ import * as defaultClocks from "./default-clocks.js";
3
+ import * as exit from "./exit.js";
4
+ import * as filesystem from "./filesystem.js";
5
+ import * as io from "./io.js";
6
+ import * as logging from "./logging.js";
7
+ import * as poll from "./poll.js";
8
+ import * as random from "./random.js";
9
+ import * as stderr from "./stderr.js";
10
+
11
+ export const importObject = {
12
+ "wasi-clocks": clocks,
13
+ "wasi-default-clocks": defaultClocks,
14
+ "wasi-exit": exit,
15
+ "wasi-filesystem": filesystem,
16
+ "wasi-io": io,
17
+ "wasi-logging": logging,
18
+ "wasi-poll": poll,
19
+ "wasi-random": random,
20
+ "wasi-stderr": stderr,
21
+ };
@@ -0,0 +1,33 @@
1
+ export function dropInputStream(f) {
2
+ console.log(`[io] Drop input stream ${f}`);
3
+ }
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) {
15
+ case 0:
16
+ throw new Error(`TODO: write stdin`);
17
+ case 1:
18
+ process.stdout.write(buf);
19
+ return BigInt(buf.byteLength);
20
+ case 2:
21
+ throw new Error(`TODO: write stdout`);
22
+ default:
23
+ throw new Error(`TODO: write ${dst}`);
24
+ }
25
+ }
26
+
27
+ export function skip(src, len) {
28
+ console.log(`[io] Skip ${src}`, len);
29
+ }
30
+
31
+ export function write_repeated(dst, byte, len) {
32
+ console.log(`[io] Write repeated ${dst}`, byte, len);
33
+ }
@@ -0,0 +1,12 @@
1
+ const levels = ["trace", "debug", "info", "warn", "error"];
2
+
3
+ let logLevel = levels.indexOf("warn");
4
+
5
+ export function setLevel(level) {
6
+ logLevel = levels.indexOf(level);
7
+ }
8
+
9
+ export function log(level, context, msg) {
10
+ if (logLevel > levels.indexOf(level)) return;
11
+ process.stdout.write(`${level}: (${context}) ${msg}\n`);
12
+ }
@@ -0,0 +1,14 @@
1
+ import { hrtime } from "node:process";
2
+
3
+ export function resolution(clock) {
4
+ console.log(`[clocks] Monotonic clock resolution ${clock}`);
5
+ }
6
+
7
+ let hrStart = hrtime.bigint();
8
+
9
+ export function now(clock) {
10
+ if (clock === 0) {
11
+ return hrtime.bigint() - hrStart;
12
+ }
13
+ console.log("[clocks] UNKNOWN CLOCK");
14
+ }
@@ -0,0 +1,3 @@
1
+ export function pollOneoff(f) {
2
+ console.log(`[poll] Poll oneoff ${f}`);
3
+ }
@@ -0,0 +1,5 @@
1
+ import { randomBytes } from "node:crypto";
2
+
3
+ export function getRandomBytes(len) {
4
+ return randomBytes(len);
5
+ }
@@ -0,0 +1,3 @@
1
+ export function print(message) {
2
+ process.stderr.write(message);
3
+ }
@@ -0,0 +1,12 @@
1
+ export function now(clock) {
2
+ if (clock === 1) {
3
+ const seconds = BigInt(Math.floor(Date.now() / 1000));
4
+ const nanoseconds = (Date.now() % 1000) * 1000 * 1000;
5
+ return { seconds, nanoseconds };
6
+ }
7
+ console.log("[clocks] UNKNOWN CLOCK");
8
+ }
9
+
10
+ export function resolution(clock) {
11
+ console.log(`[clocks] Wall clock resolution ${clock}`);
12
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.0.1",
4
- "description": "WASI Preview2 shim in JavaScript environments",
3
+ "version": "0.0.2",
4
+ "description": "WASI Preview2 shim for JS environments",
5
5
  "author": "Guy Bedford, Eduardo Rodrigues<16357187+eduardomourar@users.noreply.github.com>",
6
6
  "type": "module",
7
7
  "exports": {
@@ -15,12 +15,8 @@
15
15
  "test": "mocha -u tdd test/test.js --timeout 10000"
16
16
  },
17
17
  "files": [
18
- "index.js",
19
- "index.d.ts",
20
- "browser/",
21
- "http/",
22
- "nodejs/",
23
- "types/"
18
+ "types",
19
+ "lib"
24
20
  ],
25
21
  "devDependencies": {
26
22
  "mocha": "^10.2.0"