@bytecodealliance/preview2-shim 0.17.2 → 0.17.3
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/cli.js +91 -94
- package/lib/browser/clocks.js +30 -29
- package/lib/browser/filesystem.js +298 -251
- package/lib/browser/http.js +129 -128
- package/lib/browser/index.js +8 -16
- package/lib/browser/io.js +143 -135
- package/lib/browser/random.js +44 -42
- package/lib/browser/sockets.js +68 -166
- package/lib/common/instantiation.js +127 -0
- package/lib/io/calls.js +7 -5
- package/lib/io/worker-http.js +175 -157
- package/lib/io/worker-io.js +402 -386
- package/lib/io/worker-socket-tcp.js +271 -219
- package/lib/io/worker-socket-udp.js +494 -429
- package/lib/io/worker-sockets.js +276 -262
- package/lib/io/worker-thread.js +943 -864
- package/lib/nodejs/cli.js +64 -63
- package/lib/nodejs/clocks.js +51 -45
- package/lib/nodejs/filesystem.js +788 -654
- package/lib/nodejs/http.js +693 -617
- package/lib/nodejs/index.js +8 -16
- package/lib/nodejs/random.js +32 -28
- package/lib/nodejs/sockets.js +538 -474
- package/lib/synckit/index.js +94 -85
- package/package.json +9 -5
- package/types/index.d.ts +0 -1
- package/types/instantiation.d.ts +112 -0
|
@@ -3,284 +3,331 @@ import { environment } from './cli.js';
|
|
|
3
3
|
|
|
4
4
|
const { InputStream, OutputStream } = streams;
|
|
5
5
|
|
|
6
|
-
let _cwd =
|
|
6
|
+
let _cwd = '/';
|
|
7
7
|
|
|
8
|
-
export function _setCwd
|
|
9
|
-
|
|
8
|
+
export function _setCwd(cwd) {
|
|
9
|
+
_cwd = cwd;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export function _setFileData
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
export function _setFileData(fileData) {
|
|
13
|
+
_fileData = fileData;
|
|
14
|
+
_rootPreopen[0] = new Descriptor(fileData);
|
|
15
|
+
const cwd = environment.initialCwd();
|
|
16
|
+
_setCwd(cwd || '/');
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export function _getFileData
|
|
20
|
-
|
|
19
|
+
export function _getFileData() {
|
|
20
|
+
return JSON.stringify(_fileData);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
let _fileData = { dir: {} };
|
|
24
24
|
|
|
25
25
|
const timeZero = {
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
seconds: BigInt(0),
|
|
27
|
+
nanoseconds: 0,
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
function getChildEntry
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
30
|
+
function getChildEntry(parentEntry, subpath, openFlags) {
|
|
31
|
+
if (
|
|
32
|
+
subpath === '.' &&
|
|
33
|
+
_rootPreopen &&
|
|
34
|
+
descriptorGetEntry(_rootPreopen[0]) === parentEntry
|
|
35
|
+
) {
|
|
36
|
+
subpath = _cwd;
|
|
37
|
+
if (subpath.startsWith('/') && subpath !== '/') {
|
|
38
|
+
subpath = subpath.slice(1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
let entry = parentEntry;
|
|
42
|
+
let segmentIdx;
|
|
43
|
+
do {
|
|
44
|
+
if (!entry || !entry.dir) {
|
|
45
|
+
throw 'not-directory';
|
|
46
|
+
}
|
|
47
|
+
segmentIdx = subpath.indexOf('/');
|
|
48
|
+
const segment =
|
|
49
|
+
segmentIdx === -1 ? subpath : subpath.slice(0, segmentIdx);
|
|
50
|
+
if (segment === '..') {
|
|
51
|
+
throw 'no-entry';
|
|
52
|
+
}
|
|
53
|
+
if (segment === '.' || segment === '') {
|
|
54
|
+
} else if (!entry.dir[segment] && openFlags.create) {
|
|
55
|
+
entry = entry.dir[segment] = openFlags.directory
|
|
56
|
+
? { dir: {} }
|
|
57
|
+
: { source: new Uint8Array([]) };
|
|
58
|
+
} else {
|
|
59
|
+
entry = entry.dir[segment];
|
|
60
|
+
}
|
|
61
|
+
subpath = subpath.slice(segmentIdx + 1);
|
|
62
|
+
} while (segmentIdx !== -1);
|
|
63
|
+
if (!entry) {
|
|
64
|
+
throw 'no-entry';
|
|
65
|
+
}
|
|
66
|
+
return entry;
|
|
52
67
|
}
|
|
53
68
|
|
|
54
|
-
function getSource
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
69
|
+
function getSource(fileEntry) {
|
|
70
|
+
if (typeof fileEntry.source === 'string') {
|
|
71
|
+
fileEntry.source = new TextEncoder().encode(fileEntry.source);
|
|
72
|
+
}
|
|
73
|
+
return fileEntry.source;
|
|
59
74
|
}
|
|
60
75
|
|
|
61
76
|
class DirectoryEntryStream {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
constructor(entries) {
|
|
78
|
+
this.idx = 0;
|
|
79
|
+
this.entries = entries;
|
|
80
|
+
}
|
|
81
|
+
readDirectoryEntry() {
|
|
82
|
+
if (this.idx === this.entries.length) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const [name, entry] = this.entries[this.idx];
|
|
86
|
+
this.idx += 1;
|
|
87
|
+
return {
|
|
88
|
+
name,
|
|
89
|
+
type: entry.dir ? 'directory' : 'regular-file',
|
|
90
|
+
};
|
|
91
|
+
}
|
|
76
92
|
}
|
|
77
93
|
|
|
78
94
|
class Descriptor {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
95
|
+
#stream;
|
|
96
|
+
#entry;
|
|
97
|
+
#mtime = 0;
|
|
98
|
+
|
|
99
|
+
_getEntry(descriptor) {
|
|
100
|
+
return descriptor.#entry;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
constructor(entry, isStream) {
|
|
104
|
+
if (isStream) {
|
|
105
|
+
this.#stream = entry;
|
|
106
|
+
} else {
|
|
107
|
+
this.#entry = entry;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
readViaStream(_offset) {
|
|
112
|
+
const source = getSource(this.#entry);
|
|
113
|
+
let offset = Number(_offset);
|
|
114
|
+
return new InputStream({
|
|
115
|
+
blockingRead(len) {
|
|
116
|
+
if (offset === source.byteLength) {
|
|
117
|
+
throw { tag: 'closed' };
|
|
118
|
+
}
|
|
119
|
+
const bytes = source.slice(offset, offset + Number(len));
|
|
120
|
+
offset += bytes.byteLength;
|
|
121
|
+
return bytes;
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
writeViaStream(_offset) {
|
|
127
|
+
const entry = this.#entry;
|
|
128
|
+
let offset = Number(_offset);
|
|
129
|
+
return new OutputStream({
|
|
130
|
+
write(buf) {
|
|
131
|
+
const newSource = new Uint8Array(
|
|
132
|
+
buf.byteLength + entry.source.byteLength
|
|
133
|
+
);
|
|
134
|
+
newSource.set(entry.source, 0);
|
|
135
|
+
newSource.set(buf, offset);
|
|
136
|
+
offset += buf.byteLength;
|
|
137
|
+
entry.source = newSource;
|
|
138
|
+
return buf.byteLength;
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
appendViaStream() {
|
|
144
|
+
console.log(`[filesystem] APPEND STREAM`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
advise(descriptor, offset, length, advice) {
|
|
148
|
+
console.log(`[filesystem] ADVISE`, descriptor, offset, length, advice);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
syncData() {
|
|
152
|
+
console.log(`[filesystem] SYNC DATA`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
getFlags() {
|
|
156
|
+
console.log(`[filesystem] FLAGS FOR`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
getType() {
|
|
160
|
+
if (this.#stream) {
|
|
161
|
+
return 'fifo';
|
|
162
|
+
}
|
|
163
|
+
if (this.#entry.dir) {
|
|
164
|
+
return 'directory';
|
|
165
|
+
}
|
|
166
|
+
if (this.#entry.source) {
|
|
167
|
+
return 'regular-file';
|
|
168
|
+
}
|
|
169
|
+
return 'unknown';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
setSize(size) {
|
|
173
|
+
console.log(`[filesystem] SET SIZE`, size);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
setTimes(dataAccessTimestamp, dataModificationTimestamp) {
|
|
177
|
+
console.log(
|
|
178
|
+
`[filesystem] SET TIMES`,
|
|
179
|
+
dataAccessTimestamp,
|
|
180
|
+
dataModificationTimestamp
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
read(length, offset) {
|
|
185
|
+
const source = getSource(this.#entry);
|
|
186
|
+
return [
|
|
187
|
+
source.slice(offset, offset + length),
|
|
188
|
+
offset + length >= source.byteLength,
|
|
189
|
+
];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
write(buffer, offset) {
|
|
193
|
+
if (offset !== 0) {
|
|
194
|
+
throw 'invalid-seek';
|
|
195
|
+
}
|
|
196
|
+
this.#entry.source = buffer;
|
|
197
|
+
return buffer.byteLength;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
readDirectory() {
|
|
201
|
+
if (!this.#entry?.dir) {
|
|
202
|
+
throw 'bad-descriptor';
|
|
203
|
+
}
|
|
204
|
+
return new DirectoryEntryStream(
|
|
205
|
+
Object.entries(this.#entry.dir).sort(([a], [b]) => (a > b ? 1 : -1))
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
sync() {
|
|
210
|
+
console.log(`[filesystem] SYNC`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
createDirectoryAt(path) {
|
|
214
|
+
const entry = getChildEntry(this.#entry, path, {
|
|
215
|
+
create: true,
|
|
216
|
+
directory: true,
|
|
217
|
+
});
|
|
218
|
+
if (entry.source) {
|
|
219
|
+
throw 'exist';
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
stat() {
|
|
224
|
+
let type = 'unknown',
|
|
225
|
+
size = BigInt(0);
|
|
226
|
+
if (this.#entry.source) {
|
|
227
|
+
type = 'regular-file';
|
|
228
|
+
const source = getSource(this.#entry);
|
|
229
|
+
size = BigInt(source.byteLength);
|
|
230
|
+
} else if (this.#entry.dir) {
|
|
231
|
+
type = 'directory';
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
type,
|
|
235
|
+
linkCount: BigInt(0),
|
|
236
|
+
size,
|
|
237
|
+
dataAccessTimestamp: timeZero,
|
|
238
|
+
dataModificationTimestamp: timeZero,
|
|
239
|
+
statusChangeTimestamp: timeZero,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
statAt(_pathFlags, path) {
|
|
244
|
+
const entry = getChildEntry(this.#entry, path, {
|
|
245
|
+
create: false,
|
|
246
|
+
directory: false,
|
|
247
|
+
});
|
|
248
|
+
let type = 'unknown',
|
|
249
|
+
size = BigInt(0);
|
|
250
|
+
if (entry.source) {
|
|
251
|
+
type = 'regular-file';
|
|
252
|
+
const source = getSource(entry);
|
|
253
|
+
size = BigInt(source.byteLength);
|
|
254
|
+
} else if (entry.dir) {
|
|
255
|
+
type = 'directory';
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
type,
|
|
259
|
+
linkCount: BigInt(0),
|
|
260
|
+
size,
|
|
261
|
+
dataAccessTimestamp: timeZero,
|
|
262
|
+
dataModificationTimestamp: timeZero,
|
|
263
|
+
statusChangeTimestamp: timeZero,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
setTimesAt() {
|
|
268
|
+
console.log(`[filesystem] SET TIMES AT`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
linkAt() {
|
|
272
|
+
console.log(`[filesystem] LINK AT`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
openAt(_pathFlags, path, openFlags, _descriptorFlags, _modes) {
|
|
276
|
+
const childEntry = getChildEntry(this.#entry, path, openFlags);
|
|
277
|
+
return new Descriptor(childEntry);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
readlinkAt() {
|
|
281
|
+
console.log(`[filesystem] READLINK AT`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
removeDirectoryAt() {
|
|
285
|
+
console.log(`[filesystem] REMOVE DIR AT`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
renameAt() {
|
|
289
|
+
console.log(`[filesystem] RENAME AT`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
symlinkAt() {
|
|
293
|
+
console.log(`[filesystem] SYMLINK AT`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
unlinkFileAt() {
|
|
297
|
+
console.log(`[filesystem] UNLINK FILE AT`);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
isSameObject(other) {
|
|
301
|
+
return other === this;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
metadataHash() {
|
|
305
|
+
let upper = BigInt(0);
|
|
306
|
+
upper += BigInt(this.#mtime);
|
|
307
|
+
return { upper, lower: BigInt(0) };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
metadataHashAt(_pathFlags, _path) {
|
|
311
|
+
let upper = BigInt(0);
|
|
312
|
+
upper += BigInt(this.#mtime);
|
|
313
|
+
return { upper, lower: BigInt(0) };
|
|
314
|
+
}
|
|
269
315
|
}
|
|
270
316
|
const descriptorGetEntry = Descriptor.prototype._getEntry;
|
|
271
317
|
delete Descriptor.prototype._getEntry;
|
|
272
318
|
|
|
273
|
-
let _preopens = [[new Descriptor(_fileData), '/']],
|
|
319
|
+
let _preopens = [[new Descriptor(_fileData), '/']],
|
|
320
|
+
_rootPreopen = _preopens[0];
|
|
274
321
|
|
|
275
322
|
export const preopens = {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
323
|
+
getDirectories() {
|
|
324
|
+
return _preopens;
|
|
325
|
+
},
|
|
326
|
+
};
|
|
280
327
|
|
|
281
328
|
export const types = {
|
|
282
|
-
|
|
283
|
-
|
|
329
|
+
Descriptor,
|
|
330
|
+
DirectoryEntryStream,
|
|
284
331
|
};
|
|
285
332
|
|
|
286
|
-
export { types as filesystemTypes }
|
|
333
|
+
export { types as filesystemTypes };
|