@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.
- package/README.md +10 -20
- package/lib/browser/environment-preopens.js +4 -0
- package/lib/browser/filesystem.js +121 -22
- package/lib/browser/http.js +12 -7
- package/lib/browser/index.js +5 -7
- package/lib/browser/instance-monotonic-clock.js +4 -0
- package/lib/browser/instance-wall-clock.js +4 -0
- package/lib/browser/io.js +10 -23
- package/lib/browser/monotonic-clock.js +2 -2
- package/lib/browser/preopens.js +11 -0
- package/lib/browser/random.js +14 -1
- package/lib/browser/stderr.js +1 -0
- package/lib/browser/streams.js +17 -2
- package/lib/browser/timezone.js +12 -0
- package/lib/browser/wall-clock.js +2 -2
- package/lib/http/make-request.js +2 -2
- package/lib/http/wasi-http.js +335 -0
- package/lib/nodejs/environment-preopens.js +4 -0
- package/lib/nodejs/filesystem.js +109 -23
- package/lib/nodejs/index.js +6 -8
- package/lib/nodejs/instance-monotonic-clock.js +4 -0
- package/lib/nodejs/instance-wall-clock.js +4 -0
- package/lib/nodejs/io.js +16 -24
- package/lib/nodejs/monotonic-clock.js +6 -10
- package/lib/nodejs/preopens.js +11 -0
- package/lib/nodejs/random.js +14 -1
- package/lib/nodejs/stderr.js +1 -0
- package/lib/nodejs/streams.js +22 -4
- package/lib/nodejs/timezone.js +12 -0
- package/lib/nodejs/wall-clock.js +2 -6
- package/package.json +1 -1
- package/types/imports/environment.d.ts +1 -0
- package/types/imports/monotonic-clock.d.ts +3 -5
- package/types/imports/preopens.d.ts +15 -0
- package/types/imports/wall-clock.d.ts +2 -4
- package/types/wasi-reactor.d.ts +8 -8
- package/lib/browser/clocks.js +0 -47
- package/lib/browser/default-clocks.js +0 -7
- package/lib/nodejs/default-clocks.js +0 -7
- package/types/imports/environment-preopens.d.ts +0 -5
- package/types/imports/instance-monotonic-clock.d.ts +0 -5
- package/types/imports/instance-wall-clock.d.ts +0 -5
- package/types/imports/stderr.d.ts +0 -3
- /package/lib/browser/{console.js → logging.js} +0 -0
- /package/lib/nodejs/{console.js → logging.js} +0 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
// Based on:
|
|
2
|
+
// https://github.com/bytecodealliance/wasmtime/blob/76eb40e0756d90cc9c16b26a8d8b1e0ebf75f44d/crates/wasi-http/src/http_impl.rs
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import("../../types/imports/types").Fields} Fields
|
|
6
|
+
* @typedef {import("../../types/imports/types").FutureIncomingResponse} FutureIncomingResponse
|
|
7
|
+
* @typedef {import("../../types/imports/types").Headers} Headers
|
|
8
|
+
* @typedef {import("../../types/imports/types").IncomingResponse} IncomingResponse
|
|
9
|
+
* @typedef {import("../../types/imports/types").IncomingStream} IncomingStream
|
|
10
|
+
* @typedef {import("../../types/imports/types").Method} Method
|
|
11
|
+
* @typedef {import("../../types/imports/types").OutgoingRequest} OutgoingRequest
|
|
12
|
+
* @typedef {import("../../types/imports/types").RequestOptions} RequestOptions
|
|
13
|
+
* @typedef {import("../../types/imports/types").Result} Result
|
|
14
|
+
* @typedef {import("../../types/imports/types").Scheme} Scheme
|
|
15
|
+
* @typedef {import("../../types/imports/types").StatusCode} StatusCode
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import * as io from '@bytecodealliance/preview2-shim/io';
|
|
19
|
+
import * as http from '@bytecodealliance/preview2-shim/http';
|
|
20
|
+
import { UnexpectedError } from './error.js';
|
|
21
|
+
|
|
22
|
+
export class WasiHttp {
|
|
23
|
+
requestIdBase = 1;
|
|
24
|
+
responseIdBase = 1;
|
|
25
|
+
fieldsIdBase = 1;
|
|
26
|
+
streamIdBase = 3;
|
|
27
|
+
futureIdBase = 1;
|
|
28
|
+
/** @type {Map<number,ActiveRequest>} */ requests = new Map();
|
|
29
|
+
/** @type {Map<number,ActiveResponse>} */ responses = new Map();
|
|
30
|
+
/** @type {Map<number,Map<string,string[]>>} */ fields = new Map();
|
|
31
|
+
/** @type {Map<number,Uint8Array>} */ streams = new Map();
|
|
32
|
+
/** @type {Map<number,ActiveFuture>} */ futures = new Map();
|
|
33
|
+
|
|
34
|
+
constructor() {}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {OutgoingRequest} requestId
|
|
38
|
+
* @param {RequestOptions | null} options
|
|
39
|
+
* @returns {FutureIncomingResponse}
|
|
40
|
+
*/
|
|
41
|
+
handle = (requestId, _options) => {
|
|
42
|
+
const request = this.requests.get(requestId);
|
|
43
|
+
if (!request) throw Error("not found!");
|
|
44
|
+
|
|
45
|
+
const responseId = this.responseIdBase;
|
|
46
|
+
this.responseIdBase += 1;
|
|
47
|
+
const response = new ActiveResponse(responseId);
|
|
48
|
+
|
|
49
|
+
const scheme = request.scheme.tag === "HTTP" ? "http://" : "https://";
|
|
50
|
+
|
|
51
|
+
const url = scheme + request.authority + request.path + request.query;
|
|
52
|
+
const headers = {
|
|
53
|
+
"host": request.authority,
|
|
54
|
+
};
|
|
55
|
+
for (const [key, value] of request.headers.entries()) {
|
|
56
|
+
headers[key] = Array.isArray(value) ? value.join(",") : value;
|
|
57
|
+
}
|
|
58
|
+
const body = this.streams.get(request.body);
|
|
59
|
+
|
|
60
|
+
const res = http.send({
|
|
61
|
+
method: request.method.tag,
|
|
62
|
+
uri: url,
|
|
63
|
+
headers: headers,
|
|
64
|
+
params: [],
|
|
65
|
+
body: body && body.length > 0 ? body : undefined,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
response.status = res.status;
|
|
69
|
+
for (const [key, value] of res.headers) {
|
|
70
|
+
response.responseHeaders.set(key, [value]);
|
|
71
|
+
}
|
|
72
|
+
const buf = res.body;
|
|
73
|
+
response.body = this.streamIdBase;
|
|
74
|
+
this.streamIdBase += 1;
|
|
75
|
+
this.streams.set(response.body, buf);
|
|
76
|
+
this.responses.set(responseId, response);
|
|
77
|
+
|
|
78
|
+
const futureId = this.futureIdBase;
|
|
79
|
+
this.futureIdBase += 1;
|
|
80
|
+
const future = new ActiveFuture(futureId, responseId);
|
|
81
|
+
this.futures.set(futureId, future);
|
|
82
|
+
return futureId;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {InputStream} stream
|
|
87
|
+
* @param {bigint} len
|
|
88
|
+
* @returns {[Uint8Array | ArrayBuffer, boolean]}
|
|
89
|
+
*/
|
|
90
|
+
read = (stream, len) => {
|
|
91
|
+
if (stream < 3) {
|
|
92
|
+
return io.read(stream);
|
|
93
|
+
}
|
|
94
|
+
const s = this.streams.get(stream);
|
|
95
|
+
if (!s) throw Error(`stream not found: ${stream}`);
|
|
96
|
+
const position = Number(len);
|
|
97
|
+
if (position === 0) {
|
|
98
|
+
return [new Uint8Array(), s.byteLength > 0];
|
|
99
|
+
} else if (s.byteLength > position) {
|
|
100
|
+
this.streams.set(stream, s.slice(position, s.byteLength));
|
|
101
|
+
return [s.slice(0, position), false];
|
|
102
|
+
} else {
|
|
103
|
+
return [s.slice(0, position), true];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {InputStream} stream
|
|
109
|
+
* @returns {Pollable}
|
|
110
|
+
*/
|
|
111
|
+
subscribeToInputStream = (_stream) => {
|
|
112
|
+
throw Error("unimplemented: subscribeToInputStream");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param {InputStream} stream
|
|
117
|
+
*/
|
|
118
|
+
dropInputStream = (stream) => {
|
|
119
|
+
const s = this.streams.get(stream);
|
|
120
|
+
if (!s) throw Error(`no such input stream ${stream}`);
|
|
121
|
+
s.set([]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {OutputStream} stream
|
|
126
|
+
* @param {Uint8Array} buf
|
|
127
|
+
* @returns {bigint}
|
|
128
|
+
*/
|
|
129
|
+
write = (stream, buf) => {
|
|
130
|
+
if (stream < 3) {
|
|
131
|
+
return io.write(stream, buf);
|
|
132
|
+
}
|
|
133
|
+
this.streams.set(stream, buf);
|
|
134
|
+
return BigInt(buf.byteLength);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {OutputStream} stream
|
|
139
|
+
* @returns {Pollable}
|
|
140
|
+
*/
|
|
141
|
+
subscribeToOutputStream = (_stream) => {
|
|
142
|
+
throw Error("unimplemented: subscribeToOutputStream");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param {OutputStream} stream
|
|
147
|
+
*/
|
|
148
|
+
dropOutputStream = (stream) => {
|
|
149
|
+
const s = this.streams.get(stream);
|
|
150
|
+
if (!s) throw Error(`no such output stream ${stream}`);
|
|
151
|
+
s.set([]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {Fields} fields
|
|
156
|
+
*/
|
|
157
|
+
dropFields = (fields) => {
|
|
158
|
+
this.fields.delete(fields);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param {[string, string][]} entries
|
|
163
|
+
* @returns {Fields}
|
|
164
|
+
*/
|
|
165
|
+
newFields = (entries) => {
|
|
166
|
+
const map = new Map(entries);
|
|
167
|
+
|
|
168
|
+
const id = this.fieldsIdBase;
|
|
169
|
+
this.fieldsIdBase += 1;
|
|
170
|
+
this.fields.set(id, map);
|
|
171
|
+
|
|
172
|
+
return id;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {Fields} fields
|
|
177
|
+
* @returns {[string, string][]}
|
|
178
|
+
*/
|
|
179
|
+
fieldsEntries = (fields) => {
|
|
180
|
+
return this.fields.get(fields) ?? [];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @param {OutgoingRequest} request
|
|
185
|
+
*/
|
|
186
|
+
dropOutgoingRequest = (request) => {
|
|
187
|
+
this.requests.delete(request);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @param {Method} method
|
|
192
|
+
* @param {string} path
|
|
193
|
+
* @param {string} query
|
|
194
|
+
* @param {Scheme | null} scheme
|
|
195
|
+
* @param {string} authority
|
|
196
|
+
* @param {Headers} headers
|
|
197
|
+
* @returns {number}
|
|
198
|
+
*/
|
|
199
|
+
newOutgoingRequest = (method, path, query, scheme, authority, headers) => {
|
|
200
|
+
const id = this.requestIdBase;
|
|
201
|
+
this.requestIdBase += 1;
|
|
202
|
+
|
|
203
|
+
const req = new ActiveRequest(id);
|
|
204
|
+
req.path = path;
|
|
205
|
+
req.query = query;
|
|
206
|
+
req.authority = authority;
|
|
207
|
+
req.method = method;
|
|
208
|
+
req.headers = this.fields.get(headers);
|
|
209
|
+
req.scheme = scheme;
|
|
210
|
+
this.requests.set(id, req);
|
|
211
|
+
return id;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {OutgoingRequest} request
|
|
216
|
+
* @returns {OutgoingStream}
|
|
217
|
+
*/
|
|
218
|
+
outgoingRequestWrite = (request) => {
|
|
219
|
+
const req = this.requests.get(request);
|
|
220
|
+
req.body = this.streamIdBase;
|
|
221
|
+
this.streamIdBase += 1;
|
|
222
|
+
return req.body;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {IncomingResponse} response
|
|
227
|
+
*/
|
|
228
|
+
dropIncomingResponse = (response) => {
|
|
229
|
+
this.responses.delete(response);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @param {IncomingResponse} response
|
|
234
|
+
* @returns {StatusCode}
|
|
235
|
+
*/
|
|
236
|
+
incomingResponseStatus = (response) => {
|
|
237
|
+
const r = this.responses.get(response);
|
|
238
|
+
return r.status;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param {IncomingResponse} response
|
|
243
|
+
* @returns {Headers}
|
|
244
|
+
*/
|
|
245
|
+
incomingResponseHeaders = (response) => {
|
|
246
|
+
const r = this.responses.get(response);
|
|
247
|
+
const id = this.fieldsIdBase;
|
|
248
|
+
this.fieldsIdBase += 1;
|
|
249
|
+
|
|
250
|
+
this.fields.set(id, r.responseHeaders);
|
|
251
|
+
return id;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @param {IncomingResponse} response
|
|
256
|
+
* @returns {IncomingStream}
|
|
257
|
+
*/
|
|
258
|
+
incomingResponseConsume = (response) => {
|
|
259
|
+
const r = this.responses.get(response);
|
|
260
|
+
return r.body;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @param {FutureIncomingResponse} future
|
|
265
|
+
*/
|
|
266
|
+
dropFutureIncomingResponse = (future) => {
|
|
267
|
+
return this.futures.delete(future);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* @param {FutureIncomingResponse} future
|
|
272
|
+
* @returns {Result<IncomingResponse, Error> | null}
|
|
273
|
+
*/
|
|
274
|
+
futureIncomingResponseGet = (future) => {
|
|
275
|
+
const f = this.futures.get(future);
|
|
276
|
+
if (!f) {
|
|
277
|
+
return {
|
|
278
|
+
tag: "err",
|
|
279
|
+
val: UnexpectedError(`no such future ${f}`),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
// For now this will assume the future will return
|
|
283
|
+
// the response immediately
|
|
284
|
+
const response = f.responseId;
|
|
285
|
+
const r = this.responses.get(response);
|
|
286
|
+
if (!r) {
|
|
287
|
+
return {
|
|
288
|
+
tag: "err",
|
|
289
|
+
val: UnexpectedError(`no such response ${response}`),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
tag: "ok",
|
|
294
|
+
val: response,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
class ActiveRequest {
|
|
300
|
+
/** @type {number} */ id;
|
|
301
|
+
activeRequest = false;
|
|
302
|
+
/** @type {Method} */ method = { tag: 'get' };
|
|
303
|
+
/** @type {Scheme | null} */ scheme = { tag: 'HTTP' };
|
|
304
|
+
path = "";
|
|
305
|
+
query = "";
|
|
306
|
+
authority = "";
|
|
307
|
+
/** @type {Map<string,string[]>} */ headers = new Map();
|
|
308
|
+
body = 3;
|
|
309
|
+
|
|
310
|
+
constructor(id) {
|
|
311
|
+
this.id = id;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
class ActiveResponse {
|
|
316
|
+
/** @type {number} */ id;
|
|
317
|
+
activeResponse = false;
|
|
318
|
+
status = 0;
|
|
319
|
+
body = 3;
|
|
320
|
+
/** @type {Map<string,string[]>} */ responseHeaders = new Map();
|
|
321
|
+
|
|
322
|
+
constructor(id) {
|
|
323
|
+
this.id = id;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
class ActiveFuture {
|
|
328
|
+
/** @type {number} */ id;
|
|
329
|
+
/** @type {number} */ responseId;
|
|
330
|
+
|
|
331
|
+
constructor(id, responseId) {
|
|
332
|
+
this.id = id;
|
|
333
|
+
this.responseId = responseId;
|
|
334
|
+
}
|
|
335
|
+
}
|
package/lib/nodejs/filesystem.js
CHANGED
|
@@ -9,56 +9,142 @@
|
|
|
9
9
|
// ctim: Timestamp,
|
|
10
10
|
// }
|
|
11
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
|
+
|
|
12
32
|
export function getFlags(fd) {
|
|
13
33
|
console.log(`[filesystem] FLAGS FOR ${fd}`);
|
|
14
34
|
}
|
|
15
35
|
|
|
36
|
+
export function getType(fd) {
|
|
37
|
+
console.log(`[filesystem] GET TYPE ${fd}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
16
40
|
export function setFlags(fd, flags) {
|
|
17
41
|
console.log(`[filesystem] SET FLAGS ${fd} ${JSON.stringify(flags)}`);
|
|
18
42
|
}
|
|
19
43
|
|
|
20
|
-
export function
|
|
21
|
-
console.log(`[filesystem]
|
|
44
|
+
export function setSize(fd, size) {
|
|
45
|
+
console.log(`[filesystem] SET SIZE`, fd, size);
|
|
22
46
|
}
|
|
23
47
|
|
|
24
|
-
export function
|
|
25
|
-
console.log(`[filesystem]
|
|
48
|
+
export function setTimes(fd, dataAccessTimestamp, dataModificationTimestamp) {
|
|
49
|
+
console.log(`[filesystem] SET TIMES`, fd, dataAccessTimestamp, dataModificationTimestamp);
|
|
26
50
|
}
|
|
27
51
|
|
|
28
|
-
export function
|
|
29
|
-
console.log(`[filesystem]
|
|
52
|
+
export function read(fd, length, offset) {
|
|
53
|
+
console.log(`[filesystem] READ`, fd, length, offset);
|
|
30
54
|
}
|
|
31
55
|
|
|
32
|
-
export function
|
|
33
|
-
console.log(`[filesystem] WRITE
|
|
56
|
+
export function write(fd, buffer, offset) {
|
|
57
|
+
console.log(`[filesystem] WRITE`, fd, buffer, offset);
|
|
34
58
|
}
|
|
35
59
|
|
|
36
|
-
export function
|
|
37
|
-
console.log(`[filesystem]
|
|
60
|
+
export function readDirectory(fd) {
|
|
61
|
+
console.log(`[filesystem] READ DIR`, fd);
|
|
38
62
|
}
|
|
39
63
|
|
|
40
|
-
export function
|
|
41
|
-
console.log(`[filesystem]
|
|
64
|
+
export function sync(fd) {
|
|
65
|
+
console.log(`[filesystem] SYNC`, fd);
|
|
42
66
|
}
|
|
43
67
|
|
|
44
|
-
export function
|
|
45
|
-
console.log(`[filesystem]
|
|
68
|
+
export function createDirectoryAt(fd, path) {
|
|
69
|
+
console.log(`[filesystem] CREATE DIRECTORY`, fd, path);
|
|
46
70
|
}
|
|
47
71
|
|
|
48
72
|
export function stat(fd) {
|
|
49
|
-
console.log(`[filesystem] STAT
|
|
73
|
+
console.log(`[filesystem] STAT`, fd);
|
|
50
74
|
}
|
|
51
75
|
|
|
52
|
-
export function
|
|
53
|
-
console.log(`[filesystem]
|
|
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);
|
|
86
|
+
}
|
|
87
|
+
|
|
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);
|
|
54
142
|
}
|
|
55
143
|
|
|
56
|
-
export function
|
|
57
|
-
console.log(`[filesystem]
|
|
144
|
+
export function readDirectoryEntry(stream) {
|
|
145
|
+
console.log(`[filesystem] READ DIRECTRY ENTRY`, stream);
|
|
58
146
|
}
|
|
59
147
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return [];
|
|
148
|
+
export function dropDirectoryEntryStream(stream) {
|
|
149
|
+
console.log(`[filesystem] DROP DIRECTORY ENTRY`, stream);
|
|
63
150
|
}
|
|
64
|
-
export { getFlags as flags, getType as todoType, dropDirectoryEntryStream as dropDirEntryStream }
|
package/lib/nodejs/index.js
CHANGED
|
@@ -1,35 +1,33 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as defaultClocks from "./default-clocks.js";
|
|
1
|
+
import * as logging from "./logging.js";
|
|
3
2
|
import * as defaultOutgoingHttp from "./default-outgoing-HTTP.js";
|
|
4
3
|
import * as environment from "./environment.js";
|
|
5
4
|
import * as exit from "./exit.js";
|
|
6
5
|
import * as filesystem from "./filesystem.js";
|
|
7
6
|
import * as http from "./http.js";
|
|
8
|
-
import * as io from "./io.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
|
-
"console": console,
|
|
19
|
-
"default-clocks": defaultClocks,
|
|
20
16
|
"default-outgoing-HTTP": defaultOutgoingHttp,
|
|
21
17
|
"environment": environment,
|
|
22
18
|
"exit": exit,
|
|
23
19
|
"filesystem": filesystem,
|
|
24
20
|
"http": http,
|
|
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;
|
package/lib/nodejs/io.js
CHANGED
|
@@ -1,33 +1,25 @@
|
|
|
1
|
-
export function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function read(src, len) {
|
|
10
|
-
console.log(`[io] Read ${src}`, len);
|
|
1
|
+
export function read(s, len) {
|
|
2
|
+
switch (s) {
|
|
3
|
+
case 0:
|
|
4
|
+
return [process.stdin.read(len), true];
|
|
5
|
+
default:
|
|
6
|
+
throw new Error(`TODO: write ${s}`);
|
|
7
|
+
}
|
|
11
8
|
}
|
|
12
9
|
|
|
13
|
-
export function write(
|
|
14
|
-
switch (
|
|
10
|
+
export function write(s, buf) {
|
|
11
|
+
switch (s) {
|
|
15
12
|
case 0:
|
|
16
13
|
throw new Error(`TODO: write stdin`);
|
|
17
|
-
case 1:
|
|
14
|
+
case 1: {
|
|
18
15
|
process.stdout.write(buf);
|
|
19
16
|
return BigInt(buf.byteLength);
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
}
|
|
18
|
+
case 2: {
|
|
19
|
+
process.stderr.write(buf);
|
|
20
|
+
return BigInt(buf.byteLength);
|
|
21
|
+
}
|
|
22
22
|
default:
|
|
23
|
-
throw new Error(`TODO: write ${
|
|
23
|
+
throw new Error(`TODO: write ${s}`);
|
|
24
24
|
}
|
|
25
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
|
-
}
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { hrtime } from "node:process";
|
|
2
2
|
|
|
3
|
-
export function resolution(
|
|
4
|
-
console.log(`[
|
|
3
|
+
export function resolution() {
|
|
4
|
+
console.log(`[monotonic-clock] Monotonic clock resolution`);
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
let hrStart = hrtime.bigint();
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (clock === 0) {
|
|
11
|
-
return hrtime.bigint() - hrStart;
|
|
12
|
-
}
|
|
13
|
-
console.log("[clocks] UNKNOWN CLOCK");
|
|
8
|
+
export function now() {
|
|
9
|
+
return hrtime.bigint() - hrStart;
|
|
14
10
|
}
|
|
15
11
|
|
|
16
|
-
export function
|
|
17
|
-
|
|
12
|
+
export function subscribe (_when, _absolute) {
|
|
13
|
+
console.log(`[monotonic-clock] Subscribe`);
|
|
18
14
|
}
|
package/lib/nodejs/random.js
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
export function getRandomBytes(len) {
|
|
4
|
-
return randomBytes(len);
|
|
4
|
+
return randomBytes(Number(len));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getRandomU64 () {
|
|
8
|
+
return new BigUint64Array(randomBytes(8).buffer)[0];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let insecureRandomValue1, insecureRandomValue2;
|
|
12
|
+
export function insecureRandom () {
|
|
13
|
+
if (insecureRandomValue1 === undefined) {
|
|
14
|
+
insecureRandomValue1 = getRandomU64();
|
|
15
|
+
insecureRandomValue2 = getRandomU64();
|
|
16
|
+
}
|
|
17
|
+
return [insecureRandomValue1, insecureRandomValue2];
|
|
5
18
|
}
|
package/lib/nodejs/stderr.js
CHANGED