@bytecodealliance/preview2-shim 0.0.16 → 0.0.17
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.
- package/lib/browser/filesystem.js +4 -2
- package/lib/browser/index.js +2 -4
- package/lib/browser/io.js +8 -99
- package/lib/common/io.js +119 -0
- package/lib/http/wasi-http.js +346 -334
- package/lib/nodejs/cli.js +2 -12
- package/lib/nodejs/filesystem.js +419 -340
- package/lib/nodejs/http.js +4 -129
- package/lib/nodejs/index.js +3 -4
- package/lib/nodejs/io.js +11 -155
- package/package.json +1 -1
- package/lib/http/error.js +0 -11
package/lib/nodejs/http.js
CHANGED
|
@@ -1,130 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { createSyncFn } from "../http/synckit/index.js";
|
|
1
|
+
import { WasiHttp } from '../http/wasi-http.js';
|
|
2
|
+
import { _io } from './io.js';
|
|
4
3
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
export function send(req) {
|
|
8
|
-
console.log(`[http] Send (nodejs) ${req.uri}`);
|
|
9
|
-
const syncFn = createSyncFn(workerPath);
|
|
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.message);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const incomingHandler = {
|
|
22
|
-
handle () {
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const outgoingHandler = {
|
|
28
|
-
handle () {
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const types = {
|
|
34
|
-
dropFields(_fields) {
|
|
35
|
-
console.log("[types] Drop fields");
|
|
36
|
-
},
|
|
37
|
-
newFields(_entries) {
|
|
38
|
-
console.log("[types] New fields");
|
|
39
|
-
},
|
|
40
|
-
fieldsGet(_fields, _name) {
|
|
41
|
-
console.log("[types] Fields get");
|
|
42
|
-
},
|
|
43
|
-
fieldsSet(_fields, _name, _value) {
|
|
44
|
-
console.log("[types] Fields set");
|
|
45
|
-
},
|
|
46
|
-
fieldsDelete(_fields, _name) {
|
|
47
|
-
console.log("[types] Fields delete");
|
|
48
|
-
},
|
|
49
|
-
fieldsAppend(_fields, _name, _value) {
|
|
50
|
-
console.log("[types] Fields append");
|
|
51
|
-
},
|
|
52
|
-
fieldsEntries(_fields) {
|
|
53
|
-
console.log("[types] Fields entries");
|
|
54
|
-
},
|
|
55
|
-
fieldsClone(_fields) {
|
|
56
|
-
console.log("[types] Fields clone");
|
|
57
|
-
},
|
|
58
|
-
finishIncomingStream(s) {
|
|
59
|
-
console.log(`[types] Finish incoming stream ${s}`);
|
|
60
|
-
},
|
|
61
|
-
finishOutgoingStream(s, _trailers) {
|
|
62
|
-
console.log(`[types] Finish outgoing stream ${s}`);
|
|
63
|
-
},
|
|
64
|
-
dropIncomingRequest(_req) {
|
|
65
|
-
console.log("[types] Drop incoming request");
|
|
66
|
-
},
|
|
67
|
-
dropOutgoingRequest(_req) {
|
|
68
|
-
console.log("[types] Drop outgoing request");
|
|
69
|
-
},
|
|
70
|
-
incomingRequestMethod(_req) {
|
|
71
|
-
console.log("[types] Incoming request method");
|
|
72
|
-
},
|
|
73
|
-
incomingRequestPathWithQuery(_req) {
|
|
74
|
-
console.log("[types] Incoming request path with query");
|
|
75
|
-
},
|
|
76
|
-
incomingRequestScheme(_req) {
|
|
77
|
-
console.log("[types] Incoming request scheme");
|
|
78
|
-
},
|
|
79
|
-
incomingRequestAuthority(_req) {
|
|
80
|
-
console.log("[types] Incoming request authority");
|
|
81
|
-
},
|
|
82
|
-
incomingRequestHeaders(_req) {
|
|
83
|
-
console.log("[types] Incoming request headers");
|
|
84
|
-
},
|
|
85
|
-
incomingRequestConsume(_req) {
|
|
86
|
-
console.log("[types] Incoming request consume");
|
|
87
|
-
},
|
|
88
|
-
newOutgoingRequest(_method, _pathWithQuery, _scheme, _authority, _headers) {
|
|
89
|
-
console.log("[types] New outgoing request");
|
|
90
|
-
},
|
|
91
|
-
outgoingRequestWrite(_req) {
|
|
92
|
-
console.log("[types] Outgoing request write");
|
|
93
|
-
},
|
|
94
|
-
dropResponseOutparam(_res) {
|
|
95
|
-
console.log("[types] Drop response outparam");
|
|
96
|
-
},
|
|
97
|
-
setResponseOutparam(_response) {
|
|
98
|
-
console.log("[types] Drop fields");
|
|
99
|
-
},
|
|
100
|
-
dropIncomingResponse(_res) {
|
|
101
|
-
console.log("[types] Drop incoming response");
|
|
102
|
-
},
|
|
103
|
-
dropOutgoingResponse(_res) {
|
|
104
|
-
console.log("[types] Drop outgoing response");
|
|
105
|
-
},
|
|
106
|
-
incomingResponseStatus(_res) {
|
|
107
|
-
console.log("[types] Incoming response status");
|
|
108
|
-
},
|
|
109
|
-
incomingResponseHeaders(_res) {
|
|
110
|
-
console.log("[types] Incoming response headers");
|
|
111
|
-
},
|
|
112
|
-
incomingResponseConsume(_res) {
|
|
113
|
-
console.log("[types] Incoming response consume");
|
|
114
|
-
},
|
|
115
|
-
newOutgoingResponse(_statusCode, _headers) {
|
|
116
|
-
console.log("[types] New outgoing response");
|
|
117
|
-
},
|
|
118
|
-
outgoingResponseWrite(_res) {
|
|
119
|
-
console.log("[types] Outgoing response write");
|
|
120
|
-
},
|
|
121
|
-
dropFutureIncomingResponse(_f) {
|
|
122
|
-
console.log("[types] Drop future incoming response");
|
|
123
|
-
},
|
|
124
|
-
futureIncomingResponseGet(_f) {
|
|
125
|
-
console.log("[types] Future incoming response get");
|
|
126
|
-
},
|
|
127
|
-
listenToFutureIncomingResponse(_f) {
|
|
128
|
-
console.log("[types] Listen to future incoming response");
|
|
129
|
-
}
|
|
130
|
-
};
|
|
4
|
+
const http = new WasiHttp(_io);
|
|
5
|
+
export const { incomingHandler, outgoingHandler, types } = http;
|
package/lib/nodejs/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import * as random from "./random.js";
|
|
|
8
8
|
import * as sockets from "./sockets.js";
|
|
9
9
|
import * as cli from "./cli.js";
|
|
10
10
|
|
|
11
|
-
export
|
|
11
|
+
export {
|
|
12
12
|
clocks,
|
|
13
13
|
filesystem,
|
|
14
14
|
http,
|
|
@@ -17,9 +17,8 @@ export const importObject = {
|
|
|
17
17
|
poll,
|
|
18
18
|
random,
|
|
19
19
|
sockets,
|
|
20
|
-
cli
|
|
21
|
-
}
|
|
20
|
+
cli
|
|
21
|
+
}
|
|
22
22
|
|
|
23
23
|
export { WasiHttp } from "../http/wasi-http.js";
|
|
24
24
|
|
|
25
|
-
export default importObject;
|
package/lib/nodejs/io.js
CHANGED
|
@@ -1,160 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Io } from '../common/io.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
case 'EWOULDBLOCK': throw 'would-block';
|
|
8
|
-
case 'EALREADY': throw 'already';
|
|
9
|
-
case 'EBADF': throw 'bad-descriptor';
|
|
10
|
-
case 'EBUSY': throw 'busy';
|
|
11
|
-
case 'EDEADLK': throw 'deadlock';
|
|
12
|
-
case 'EDQUOT': throw 'quota';
|
|
13
|
-
case 'EEXIST': throw 'exist';
|
|
14
|
-
case 'EFBIG': throw 'file-too-large';
|
|
15
|
-
case 'EILSEQ': throw 'illegal-byte-sequence';
|
|
16
|
-
case 'EINPROGRESS': throw 'in-progress';
|
|
17
|
-
case 'EINTR': throw 'interrupted';
|
|
18
|
-
case 'EINVAL': throw 'invalid';
|
|
19
|
-
case 'EIO': throw 'io';
|
|
20
|
-
case 'EISDIR': throw 'is-directory';
|
|
21
|
-
case 'ELOOP': throw 'loop';
|
|
22
|
-
case 'EMLINK': throw 'too-many-links';
|
|
23
|
-
case 'EMSGSIZE': throw 'message-size';
|
|
24
|
-
case 'ENAMETOOLONG': throw 'name-too-long'
|
|
25
|
-
case 'ENODEV': throw 'no-device';
|
|
26
|
-
case 'ENOENT': throw 'no-entry';
|
|
27
|
-
case 'ENOLCK': throw 'no-lock';
|
|
28
|
-
case 'ENOMEM': throw 'insufficient-memory';
|
|
29
|
-
case 'ENOSPC': throw 'insufficient-space';
|
|
30
|
-
case 'ENOTDIR': throw 'not-directory';
|
|
31
|
-
case 'ENOTEMPTY': throw 'not-empty';
|
|
32
|
-
case 'ENOTRECOVERABLE': throw 'not-recoverable';
|
|
33
|
-
case 'ENOTSUP': throw 'unsupported';
|
|
34
|
-
case 'ENOTTY': throw 'no-tty';
|
|
35
|
-
case 'ENXIO': throw 'no-such-device';
|
|
36
|
-
case 'EOVERFLOW': throw 'overflow';
|
|
37
|
-
case 'EPERM': throw 'not-permitted';
|
|
38
|
-
case 'EPIPE': throw 'pipe';
|
|
39
|
-
case 'EROFS': throw 'read-only';
|
|
40
|
-
case 'ESPIPE': throw 'invalid-seek';
|
|
41
|
-
case 'ETXTBSY': throw 'text-file-busy';
|
|
42
|
-
case 'EXDEV': throw 'cross-device';
|
|
43
|
-
default: throw e;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export let _streams = {};
|
|
48
|
-
let streamCnt = 0;
|
|
49
|
-
export function _createFsStream(fd, type, context) {
|
|
50
|
-
_streams[streamCnt] = {
|
|
51
|
-
type,
|
|
52
|
-
fd,
|
|
53
|
-
context
|
|
54
|
-
};
|
|
55
|
-
return streamCnt++;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function _getFsStreamContext(stream, type) {
|
|
59
|
-
const entry = _streams[stream];
|
|
60
|
-
if (!entry)
|
|
61
|
-
throw new Error(`No '${type}' stream found at stream ${stream}`);
|
|
62
|
-
if (entry.type !== type)
|
|
63
|
-
throw new Error(`Unexpected '${entry.type}' stream found at stream ${stream}, expected '${type}'`);
|
|
64
|
-
return entry.context;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function _dropFsStream(stream) {
|
|
68
|
-
// TODO: recycling?
|
|
69
|
-
delete _streams[stream];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export const streams = {
|
|
73
|
-
read(s, len) {
|
|
74
|
-
return streams.blockingRead(s, len);
|
|
75
|
-
},
|
|
76
|
-
blockingRead(s, len) {
|
|
77
|
-
len = Number(len);
|
|
78
|
-
const stream = _streams[s];
|
|
79
|
-
switch (stream?.type) {
|
|
80
|
-
case 'file': {
|
|
81
|
-
const buf = Buffer.alloc(Number(len));
|
|
82
|
-
try {
|
|
83
|
-
const readBytes = fsReadSync(stream.fd, buf, 0, Number(len));
|
|
84
|
-
if (readBytes < Number(len)) {
|
|
85
|
-
return [new Uint8Array(buf.buffer, 0, readBytes), 'ended'];
|
|
86
|
-
}
|
|
87
|
-
return [new Uint8Array(buf.buffer, 0, readBytes), 'open'];
|
|
88
|
-
}
|
|
89
|
-
catch (e) {
|
|
90
|
-
_convertFsError(e);
|
|
91
|
-
}
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
3
|
+
export const _io = new Io(
|
|
4
|
+
{
|
|
5
|
+
write (buf) {
|
|
6
|
+
process.stdout.write(buf);
|
|
94
7
|
}
|
|
95
|
-
throw null;
|
|
96
|
-
},
|
|
97
|
-
skip(s, _len) {
|
|
98
|
-
console.log(`[streams] Skip ${s}`);
|
|
99
|
-
},
|
|
100
|
-
blockingSkip(s, _len) {
|
|
101
|
-
console.log(`[streams] Blocking skip ${s}`);
|
|
102
|
-
},
|
|
103
|
-
subscribeToInputStream(s) {
|
|
104
|
-
console.log(`[streams] Subscribe to input stream ${s}`);
|
|
105
|
-
},
|
|
106
|
-
dropInputStream(s) {
|
|
107
|
-
delete _streams[s];
|
|
108
|
-
},
|
|
109
|
-
checkWrite(_s) {
|
|
110
|
-
// TODO: implement
|
|
111
|
-
return 1000000n;
|
|
112
8
|
},
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
throw new Error(`TODO: write stdin`);
|
|
117
|
-
case 1: {
|
|
118
|
-
process.stdout.write(buf);
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
case 2: {
|
|
122
|
-
process.stderr.write(buf);
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
default:
|
|
126
|
-
throw new Error(`TODO: write ${s}`);
|
|
9
|
+
{
|
|
10
|
+
write (buf) {
|
|
11
|
+
process.stderr.write(buf);
|
|
127
12
|
}
|
|
128
|
-
},
|
|
129
|
-
blockingWriteAndFlush(s, buf) {
|
|
130
|
-
// TODO: implement
|
|
131
|
-
return streams.write(s, buf);
|
|
132
|
-
},
|
|
133
|
-
flush(s) {
|
|
134
|
-
return streams.blockingFlush(s);
|
|
135
|
-
},
|
|
136
|
-
blockingFlush(_s) {
|
|
137
|
-
// TODO: implement
|
|
138
|
-
},
|
|
139
|
-
writeZeroes(s, _len) {
|
|
140
|
-
console.log(`[streams] Write zeroes ${s}`);
|
|
141
|
-
},
|
|
142
|
-
blockingWriteZeroes(s, _len) {
|
|
143
|
-
console.log(`[streams] Blocking write zeroes ${s}`);
|
|
144
|
-
},
|
|
145
|
-
splice(s, _src, _len) {
|
|
146
|
-
console.log(`[streams] Splice ${s}`);
|
|
147
|
-
},
|
|
148
|
-
blockingSplice(s, _src, _len) {
|
|
149
|
-
console.log(`[streams] Blocking splice ${s}`);
|
|
150
|
-
},
|
|
151
|
-
forward(s, _src) {
|
|
152
|
-
console.log(`[streams] Forward ${s}`);
|
|
153
|
-
},
|
|
154
|
-
subscribeToOutputStream(s) {
|
|
155
|
-
console.log(`[streams] Subscribe to output stream ${s}`);
|
|
156
|
-
},
|
|
157
|
-
dropOutputStream(s) {
|
|
158
|
-
console.log(`[streams] Drop output stream ${s}`);
|
|
159
13
|
}
|
|
160
|
-
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
export const streams = _io.streams;
|
package/package.json
CHANGED
package/lib/http/error.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export class UnexpectedError extends Error {
|
|
2
|
-
/** @type { import("../../types/interfaces/wasi-http-types").ErrorUnexpectedError } */
|
|
3
|
-
payload;
|
|
4
|
-
constructor(message = "unexpected-error") {
|
|
5
|
-
super(message);
|
|
6
|
-
this.payload = {
|
|
7
|
-
tag: "unexpected-error",
|
|
8
|
-
val: message,
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
}
|