@bytecodealliance/preview2-shim 0.0.11 → 0.0.13

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 (37) hide show
  1. package/lib/browser/{cli-base.js → cli.js} +30 -6
  2. package/lib/browser/filesystem.js +16 -2
  3. package/lib/browser/index.js +2 -2
  4. package/lib/nodejs/cli.js +65 -0
  5. package/lib/nodejs/filesystem.js +93 -3
  6. package/lib/nodejs/index.js +2 -2
  7. package/package.json +1 -1
  8. package/lib/nodejs/cli-base.js +0 -99
  9. package/types/exports/wasi-http-incoming-handler.d.ts +0 -7
  10. package/types/imports/environment.d.ts +0 -17
  11. package/types/imports/exit.d.ts +0 -7
  12. package/types/imports/filesystem.d.ts +0 -857
  13. package/types/imports/handler.d.ts +0 -40
  14. package/types/imports/insecure-seed.d.ts +0 -22
  15. package/types/imports/insecure.d.ts +0 -20
  16. package/types/imports/instance-network.d.ts +0 -8
  17. package/types/imports/ip-name-lookup.d.ts +0 -76
  18. package/types/imports/monotonic-clock.d.ts +0 -24
  19. package/types/imports/network.d.ts +0 -212
  20. package/types/imports/outgoing-handler.d.ts +0 -9
  21. package/types/imports/poll.d.ts +0 -41
  22. package/types/imports/preopens.d.ts +0 -12
  23. package/types/imports/random.d.ts +0 -22
  24. package/types/imports/stderr.d.ts +0 -5
  25. package/types/imports/stdin.d.ts +0 -5
  26. package/types/imports/stdout.d.ts +0 -5
  27. package/types/imports/streams.d.ts +0 -180
  28. package/types/imports/tcp-create-socket.d.ts +0 -33
  29. package/types/imports/tcp.d.ts +0 -285
  30. package/types/imports/timezone.d.ts +0 -71
  31. package/types/imports/types.d.ts +0 -119
  32. package/types/imports/udp-create-socket.d.ts +0 -33
  33. package/types/imports/udp.d.ts +0 -219
  34. package/types/imports/wall-clock.d.ts +0 -31
  35. package/types/wasi-command.d.ts +0 -23
  36. package/types/wasi-proxy.d.ts +0 -10
  37. package/types/wasi-reactor.d.ts +0 -23
@@ -23,12 +23,6 @@ export const exit = {
23
23
  }
24
24
  };
25
25
 
26
- export const preopens = {
27
- getDirectories () {
28
- return [];
29
- }
30
- }
31
-
32
26
  export const stdin = {
33
27
  getStdin () {
34
28
  return 0;
@@ -46,3 +40,33 @@ export const stderr = {
46
40
  return 2;
47
41
  }
48
42
  };
43
+
44
+ export const terminalInput = {
45
+ dropTerminalInput () {
46
+
47
+ }
48
+ };
49
+
50
+ export const terminalOutput = {
51
+ dropTerminalOutput () {
52
+
53
+ }
54
+ };
55
+
56
+ export const terminalStderr = {
57
+ getTerminalStderr () {
58
+ return 0;
59
+ }
60
+ };
61
+
62
+ export const terminalStdin = {
63
+ getTerminalStdin () {
64
+ return 1;
65
+ }
66
+ };
67
+
68
+ export const terminalStdout = {
69
+ getTerminalStdout () {
70
+ return 2;
71
+ }
72
+ };
@@ -1,4 +1,10 @@
1
- export const filesystem = {
1
+ export const preopens = {
2
+ getDirectories () {
3
+ return [];
4
+ }
5
+ }
6
+
7
+ export const types = {
2
8
  readViaStream(fd, offset) {
3
9
  console.log(`[filesystem] READ STREAM ${fd} ${offset}`);
4
10
  },
@@ -137,7 +143,15 @@ export const filesystem = {
137
143
 
138
144
  dropDirectoryEntryStream(stream) {
139
145
  console.log(`[filesystem] DROP DIRECTORY ENTRY`, stream);
146
+ },
147
+
148
+ metadataHash(fd) {
149
+ console.log(`[filesystem] METADATA HASH`, fd);
150
+ },
151
+
152
+ metadataHashAt(fd, pathFlags, path) {
153
+ console.log(`[filesystem] METADATA HASH AT `, fd, pathFlags, path);
140
154
  }
141
155
  };
142
156
 
143
- export { filesystem as filesystemFilesystem }
157
+ export { types as filesystemTypes }
@@ -6,7 +6,7 @@ import * as logging from "./logging.js";
6
6
  import * as poll from "./poll.js";
7
7
  import * as random from "./random.js";
8
8
  import * as sockets from "./sockets.js";
9
- import * as cliBase from "./cli-base.js";
9
+ import * as cli from "./cli.js";
10
10
 
11
11
  export const importObject = {
12
12
  clocks,
@@ -17,7 +17,7 @@ export const importObject = {
17
17
  poll,
18
18
  random,
19
19
  sockets,
20
- cliBase,
20
+ cli,
21
21
  };
22
22
 
23
23
  export { WasiHttp } from "../http/wasi-http.js";
@@ -0,0 +1,65 @@
1
+ let _env;
2
+ export function _setEnv (envObj) {
3
+ _env = Object.entries(envObj);
4
+ }
5
+
6
+ export const environment = {
7
+ getEnvironment () {
8
+ if (!_env) _setEnv(process.env);
9
+ return _env;
10
+ }
11
+ };
12
+
13
+ export const exit = {
14
+ exit (status) {
15
+ process.exit(status.tag === 'err' ? 1 : 0);
16
+ }
17
+ };
18
+
19
+ export const stdin = {
20
+ getStdin () {
21
+ return 0;
22
+ }
23
+ };
24
+
25
+ export const stdout = {
26
+ getStdout () {
27
+ return 1;
28
+ }
29
+ };
30
+
31
+ export const stderr = {
32
+ getStderr () {
33
+ return 2;
34
+ }
35
+ };
36
+
37
+ export const terminalInput = {
38
+ dropTerminalInput () {
39
+
40
+ }
41
+ };
42
+
43
+ export const terminalOutput = {
44
+ dropTerminalOutput () {
45
+
46
+ }
47
+ };
48
+
49
+ export const terminalStderr = {
50
+ getTerminalStderr () {
51
+ return 0;
52
+ }
53
+ };
54
+
55
+ export const terminalStdin = {
56
+ getTerminalStdin () {
57
+ return 1;
58
+ }
59
+ };
60
+
61
+ export const terminalStdout = {
62
+ getTerminalStdout () {
63
+ return 2;
64
+ }
65
+ };
@@ -1,7 +1,70 @@
1
1
  import { openSync, constants, statSync, lstatSync, fstatSync, closeSync, readdirSync } from 'node:fs';
2
- import { _descriptors, _addOpenedDescriptor, _removeOpenedDescriptor, _getDescriptorType, _setSubdescriptorType, _setDescriptorType, _getFullPath } from './cli-base.js';
3
2
  import { _createFsStream, _dropFsStream, _getFsStreamContext } from './io.js';
4
3
 
4
+ // default is full permissions
5
+ let preopenCnt = 4;
6
+ export let _descriptors = {
7
+ 3: { type: 'directory', path: '/', parent: null, subpathTypes: {} }
8
+ };
9
+ let directories = [[3, '/']];
10
+
11
+ export function _setPreopens (preopens) {
12
+ _descriptors = {};
13
+ directories = [,,];
14
+ for (const [virtualPath, path] of Object.entries(preopens)) {
15
+ _descriptors[preopenCnt] = { type: 'directory', path, parent: null, subpathTypes: {} };
16
+ directories.push([preopenCnt++, virtualPath]);
17
+ }
18
+ }
19
+
20
+ export function _getFullPath (fd) {
21
+ let path = '';
22
+ while (fd) {
23
+ path = _descriptors[fd].path + path;
24
+ fd = _descriptors[fd].parent;
25
+ }
26
+ return path;
27
+ }
28
+
29
+ export function _getDescriptorType (fd) {
30
+ return _descriptors[fd].type;
31
+ }
32
+
33
+ export function _setDescriptorType (fd, type) {
34
+ _descriptors[fd].type = type;
35
+ }
36
+
37
+ export function _setSubdescriptorType (fd, path, type) {
38
+ while (_descriptors[fd].parent) {
39
+ path = _descriptors[fd].path + path;
40
+ fd = _descriptors[fd].parent;
41
+ }
42
+ _descriptors[fd].subpathTypes[path] = type;
43
+ }
44
+
45
+ export function _addOpenedDescriptor (fd, path, parentFd) {
46
+ if (fd < preopenCnt || _descriptors[fd])
47
+ throw 'bad-descriptor';
48
+ let type = null;
49
+ for (const [_path, _type] of Object.entries(_descriptors[parentFd].subpathTypes)) {
50
+ if (_path === path)
51
+ type = _type;
52
+ }
53
+ _descriptors[fd] = { path, type, parent: parentFd, subpathTypes: {} };
54
+ }
55
+
56
+ export function _removeOpenedDescriptor (fd) {
57
+ if (fd < preopenCnt)
58
+ throw 'eperm';
59
+ delete _descriptors[fd];
60
+ }
61
+
62
+ export const preopens = {
63
+ getDirectories () {
64
+ return directories;
65
+ }
66
+ }
67
+
5
68
  const nsMagnitude = 1_000_000_000_000n;
6
69
  function nsToDateTime (ns) {
7
70
  const seconds = ns / nsMagnitude;
@@ -71,7 +134,7 @@ function _lookupType (obj) {
71
134
  return 'unknown';
72
135
  }
73
136
 
74
- export const filesystem = {
137
+ export const types = {
75
138
  readViaStream(fd, offset) {
76
139
  if (Number(offset) !== 0)
77
140
  throw new Error('Read streams with non-zero offset not currently supported');
@@ -102,7 +165,7 @@ export const filesystem = {
102
165
  getType(fd) {
103
166
  let type = _getDescriptorType(fd);
104
167
  if (type === null) {
105
- filesystem.stat(fd);
168
+ types.stat(fd);
106
169
  type = _getDescriptorType(fd);
107
170
  }
108
171
  return type;
@@ -307,5 +370,32 @@ export const filesystem = {
307
370
 
308
371
  dropDirectoryEntryStream(stream) {
309
372
  _dropFsStream(stream);
373
+ },
374
+
375
+ metadataHash(fd) {
376
+ let stats;
377
+ try {
378
+ stats = fstatSync(fd, { bigint: true });
379
+ }
380
+ catch (e) {
381
+ _convertFsError(e);
382
+ }
383
+ const type = _lookupType(stats);
384
+ _setDescriptorType(fd, type);
385
+ return { upper: BigInt(stats.size), lower: stats.mtimeNs };
386
+ },
387
+
388
+ metadataHashAt(fd, { symlinkFollow }, path) {
389
+ const fullPath = _descriptors[fd].path + path;
390
+ let stats;
391
+ try {
392
+ stats = (symlinkFollow ? statSync : lstatSync)(fullPath, { bigint: true });
393
+ }
394
+ catch (e) {
395
+ _convertFsError(e);
396
+ }
397
+ const type = _lookupType(stats);
398
+ _setSubdescriptorType(fd, path, type);
399
+ return { upper: BigInt(stats.size), lower: stats.mtimeNs };
310
400
  }
311
401
  };
@@ -6,7 +6,7 @@ import * as logging from "./logging.js";
6
6
  import * as poll from "./poll.js";
7
7
  import * as random from "./random.js";
8
8
  import * as sockets from "./sockets.js";
9
- import * as cliBase from "./cli-base.js";
9
+ import * as cli from "./cli.js";
10
10
 
11
11
  export const importObject = {
12
12
  clocks,
@@ -17,7 +17,7 @@ export const importObject = {
17
17
  poll,
18
18
  random,
19
19
  sockets,
20
- cliBase,
20
+ cli,
21
21
  };
22
22
 
23
23
  export { WasiHttp } from "../http/wasi-http.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
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",
@@ -1,99 +0,0 @@
1
- // default is full permissions
2
- let preopenCnt = 4;
3
- export let _descriptors = {
4
- 3: { type: 'directory', path: '/', parent: null, subpathTypes: {} }
5
- };
6
- let directories = [[3, '/']];
7
-
8
- export function _getFullPath (fd) {
9
- let path = '';
10
- while (fd) {
11
- path = _descriptors[fd].path + path;
12
- fd = _descriptors[fd].parent;
13
- }
14
- return path;
15
- }
16
-
17
- export function _getDescriptorType (fd) {
18
- return _descriptors[fd].type;
19
- }
20
-
21
- export function _setDescriptorType (fd, type) {
22
- _descriptors[fd].type = type;
23
- }
24
-
25
- export function _setSubdescriptorType (fd, path, type) {
26
- while (_descriptors[fd].parent) {
27
- path = _descriptors[fd].path + path;
28
- fd = _descriptors[fd].parent;
29
- }
30
- _descriptors[fd].subpathTypes[path] = type;
31
- }
32
-
33
- export function _addOpenedDescriptor (fd, path, parentFd) {
34
- if (fd < preopenCnt || _descriptors[fd])
35
- throw 'bad-descriptor';
36
- let type = null;
37
- for (const [_path, _type] of Object.entries(_descriptors[parentFd].subpathTypes)) {
38
- if (_path === path)
39
- type = _type;
40
- }
41
- _descriptors[fd] = { path, type, parent: parentFd, subpathTypes: {} };
42
- }
43
-
44
- export function _removeOpenedDescriptor (fd) {
45
- if (fd < preopenCnt)
46
- throw 'eperm';
47
- delete _descriptors[fd];
48
- }
49
-
50
- export function _setPreopens (preopens) {
51
- _descriptors = {};
52
- directories = [,,];
53
- for (const [virtualPath, path] of Object.entries(preopens)) {
54
- _descriptors[preopenCnt] = { type: 'directory', path, parent: null, subpathTypes: {} };
55
- directories.push([preopenCnt++, virtualPath]);
56
- }
57
- }
58
-
59
- let _env;
60
- export function _setEnv (envObj) {
61
- _env = Object.entries(envObj);
62
- }
63
-
64
- export const environment = {
65
- getEnvironment () {
66
- if (!_env) _setEnv(process.env);
67
- return _env;
68
- }
69
- };
70
-
71
- export const exit = {
72
- exit (status) {
73
- process.exit(status.tag === 'err' ? 1 : 0);
74
- }
75
- };
76
-
77
- export const preopens = {
78
- getDirectories () {
79
- return directories;
80
- }
81
- }
82
-
83
- export const stdin = {
84
- getStdin () {
85
- return 0;
86
- }
87
- };
88
-
89
- export const stdout = {
90
- getStdout () {
91
- return 1;
92
- }
93
- };
94
-
95
- export const stderr = {
96
- getStderr () {
97
- return 2;
98
- }
99
- };
@@ -1,7 +0,0 @@
1
- export namespace ExportsWasiHttpIncomingHandler {
2
- export function handle(request: IncomingRequest, responseOut: ResponseOutparam): void;
3
- }
4
- import type { IncomingRequest } from '../imports/types';
5
- export { IncomingRequest };
6
- import type { ResponseOutparam } from '../imports/types';
7
- export { ResponseOutparam };
@@ -1,17 +0,0 @@
1
- export namespace ImportsEnvironment {
2
- /**
3
- * Get the POSIX-style environment variables.
4
- *
5
- * Each environment variable is provided as a pair of string variable names
6
- * and string value.
7
- *
8
- * Morally, these are a value import, but until value imports are available
9
- * in the component model, this import function should return the same
10
- * values each time it is called.
11
- */
12
- export function getEnvironment(): [string, string][];
13
- /**
14
- * Get the POSIX-style arguments to the program.
15
- */
16
- export function getArguments(): string[];
17
- }
@@ -1,7 +0,0 @@
1
- export namespace ImportsExit {
2
- /**
3
- * Exit the curerent instance and any linked instances.
4
- */
5
- export function exit(status: Result<void, void>): void;
6
- }
7
- export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };