@tybys/wasm-util 0.7.0 → 0.8.0
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 +193 -193
- package/dist/tsdoc-metadata.json +1 -1
- package/dist/wasm-util.d.ts +12 -6
- package/dist/wasm-util.esm-bundler.js +2639 -2607
- package/dist/wasm-util.esm.js +2639 -2607
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +2639 -2606
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/asyncify.js +157 -157
- package/lib/cjs/index.js +12 -12
- package/lib/cjs/jspi.js +45 -45
- package/lib/cjs/load.js +96 -85
- package/lib/cjs/memfs.js +2689 -0
- package/lib/cjs/memory.js +34 -34
- package/lib/cjs/wasi/error.js +102 -102
- package/lib/cjs/wasi/fd.js +267 -267
- package/lib/cjs/wasi/fs.js +2 -2
- package/lib/cjs/wasi/index.js +193 -171
- package/lib/cjs/wasi/path.js +173 -173
- package/lib/cjs/wasi/preview1.js +1478 -1478
- package/lib/cjs/wasi/rights.js +139 -139
- package/lib/cjs/wasi/types.js +219 -219
- package/lib/cjs/wasi/util.js +121 -121
- package/lib/cjs/webassembly.js +12 -12
- package/lib/mjs/asyncify.mjs +153 -153
- package/lib/mjs/index.mjs +9 -9
- package/lib/mjs/jspi.mjs +39 -39
- package/lib/mjs/load.mjs +89 -78
- package/lib/mjs/memfs.mjs +2688 -0
- package/lib/mjs/memory.mjs +28 -28
- package/lib/mjs/wasi/error.mjs +97 -97
- package/lib/mjs/wasi/fd.mjs +256 -256
- package/lib/mjs/wasi/fs.mjs +1 -1
- package/lib/mjs/wasi/index.mjs +188 -167
- package/lib/mjs/wasi/path.mjs +168 -168
- package/lib/mjs/wasi/preview1.mjs +1474 -1474
- package/lib/mjs/wasi/rights.mjs +134 -134
- package/lib/mjs/wasi/types.mjs +216 -216
- package/lib/mjs/wasi/util.mjs +108 -108
- package/lib/mjs/webassembly.mjs +9 -9
- package/package.json +58 -58
package/lib/mjs/wasi/fd.mjs
CHANGED
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
import { WasiErrno, FileControlFlag, WasiFileType, WasiWhence } from "./types.mjs";
|
|
2
|
-
import { getRights } from "./rights.mjs";
|
|
3
|
-
import { WasiError } from "./error.mjs";
|
|
4
|
-
export function concatBuffer(buffers, size) {
|
|
5
|
-
let total = 0;
|
|
6
|
-
if (typeof size === 'number' && size >= 0) {
|
|
7
|
-
total = size;
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
for (let i = 0; i < buffers.length; i++) {
|
|
11
|
-
const buffer = buffers[i];
|
|
12
|
-
total += buffer.length;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
let pos = 0;
|
|
16
|
-
const ret = new Uint8Array(total);
|
|
17
|
-
for (let i = 0; i < buffers.length; i++) {
|
|
18
|
-
const buffer = buffers[i];
|
|
19
|
-
ret.set(buffer, pos);
|
|
20
|
-
pos += buffer.length;
|
|
21
|
-
}
|
|
22
|
-
return ret;
|
|
23
|
-
}
|
|
24
|
-
export class FileDescriptor {
|
|
25
|
-
constructor(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
26
|
-
this.id = id;
|
|
27
|
-
this.fd = fd;
|
|
28
|
-
this.path = path;
|
|
29
|
-
this.realPath = realPath;
|
|
30
|
-
this.type = type;
|
|
31
|
-
this.rightsBase = rightsBase;
|
|
32
|
-
this.rightsInheriting = rightsInheriting;
|
|
33
|
-
this.preopen = preopen;
|
|
34
|
-
this.pos = BigInt(0);
|
|
35
|
-
this.size = BigInt(0);
|
|
36
|
-
}
|
|
37
|
-
seek(offset, whence) {
|
|
38
|
-
if (whence === WasiWhence.SET) {
|
|
39
|
-
this.pos = BigInt(offset);
|
|
40
|
-
}
|
|
41
|
-
else if (whence === WasiWhence.CUR) {
|
|
42
|
-
this.pos += BigInt(offset);
|
|
43
|
-
}
|
|
44
|
-
else if (whence === WasiWhence.END) {
|
|
45
|
-
this.pos = BigInt(this.size) - BigInt(offset);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
throw new WasiError('Unknown whence', WasiErrno.EIO);
|
|
49
|
-
}
|
|
50
|
-
return this.pos;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export class StandardOutput extends FileDescriptor {
|
|
54
|
-
constructor(log, id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
55
|
-
super(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
56
|
-
this._log = log;
|
|
57
|
-
this._buf = null;
|
|
58
|
-
}
|
|
59
|
-
write(buffer) {
|
|
60
|
-
const originalBuffer = buffer;
|
|
61
|
-
if (this._buf) {
|
|
62
|
-
buffer = concatBuffer([this._buf, buffer]);
|
|
63
|
-
this._buf = null;
|
|
64
|
-
}
|
|
65
|
-
if (buffer.indexOf(10) === -1) {
|
|
66
|
-
this._buf = buffer;
|
|
67
|
-
return originalBuffer.byteLength;
|
|
68
|
-
}
|
|
69
|
-
let written = 0;
|
|
70
|
-
let lastBegin = 0;
|
|
71
|
-
let index;
|
|
72
|
-
while ((index = buffer.indexOf(10, written)) !== -1) {
|
|
73
|
-
const str = new TextDecoder().decode(buffer.subarray(lastBegin, index));
|
|
74
|
-
this._log(str);
|
|
75
|
-
written += index - lastBegin + 1;
|
|
76
|
-
lastBegin = index + 1;
|
|
77
|
-
}
|
|
78
|
-
if (written < buffer.length) {
|
|
79
|
-
this._buf = buffer.slice(written);
|
|
80
|
-
}
|
|
81
|
-
return originalBuffer.byteLength;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
export function toFileType(stat) {
|
|
85
|
-
if (stat.isBlockDevice())
|
|
86
|
-
return WasiFileType.BLOCK_DEVICE;
|
|
87
|
-
if (stat.isCharacterDevice())
|
|
88
|
-
return WasiFileType.CHARACTER_DEVICE;
|
|
89
|
-
if (stat.isDirectory())
|
|
90
|
-
return WasiFileType.DIRECTORY;
|
|
91
|
-
if (stat.isSocket())
|
|
92
|
-
return WasiFileType.SOCKET_STREAM;
|
|
93
|
-
if (stat.isFile())
|
|
94
|
-
return WasiFileType.REGULAR_FILE;
|
|
95
|
-
if (stat.isSymbolicLink())
|
|
96
|
-
return WasiFileType.SYMBOLIC_LINK;
|
|
97
|
-
return WasiFileType.UNKNOWN;
|
|
98
|
-
}
|
|
99
|
-
export function toFileStat(view, buf, stat) {
|
|
100
|
-
view.setBigUint64(buf, stat.dev, true);
|
|
101
|
-
view.setBigUint64(buf + 8, stat.ino, true);
|
|
102
|
-
view.setBigUint64(buf + 16, BigInt(toFileType(stat)), true);
|
|
103
|
-
view.setBigUint64(buf + 24, stat.nlink, true);
|
|
104
|
-
view.setBigUint64(buf + 32, stat.size, true);
|
|
105
|
-
view.setBigUint64(buf + 40, stat.atimeMs * BigInt(1000000), true);
|
|
106
|
-
view.setBigUint64(buf + 48, stat.mtimeMs * BigInt(1000000), true);
|
|
107
|
-
view.setBigUint64(buf + 56, stat.ctimeMs * BigInt(1000000), true);
|
|
108
|
-
}
|
|
109
|
-
export class FileDescriptorTable {
|
|
110
|
-
constructor(options) {
|
|
111
|
-
this.used = 0;
|
|
112
|
-
this.size = options.size;
|
|
113
|
-
this.fds = Array(options.size);
|
|
114
|
-
this.stdio = [options.in, options.out, options.err];
|
|
115
|
-
this.print = options.print;
|
|
116
|
-
this.printErr = options.printErr;
|
|
117
|
-
this.insertStdio(options.in, 0, '<stdin>');
|
|
118
|
-
this.insertStdio(options.out, 1, '<stdout>');
|
|
119
|
-
this.insertStdio(options.err, 2, '<stderr>');
|
|
120
|
-
}
|
|
121
|
-
insertStdio(fd, expected, name) {
|
|
122
|
-
const type = WasiFileType.CHARACTER_DEVICE;
|
|
123
|
-
const { base, inheriting } = getRights(this.stdio, fd, FileControlFlag.O_RDWR, type);
|
|
124
|
-
const wrap = this.insert(fd, name, name, type, base, inheriting, 0);
|
|
125
|
-
if (wrap.id !== expected) {
|
|
126
|
-
throw new WasiError(`id: ${wrap.id} !== expected: ${expected}`, WasiErrno.EBADF);
|
|
127
|
-
}
|
|
128
|
-
return wrap;
|
|
129
|
-
}
|
|
130
|
-
insert(fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
131
|
-
var _a, _b;
|
|
132
|
-
let index = -1;
|
|
133
|
-
if (this.used >= this.size) {
|
|
134
|
-
const newSize = this.size * 2;
|
|
135
|
-
this.fds.length = newSize;
|
|
136
|
-
index = this.size;
|
|
137
|
-
this.size = newSize;
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
for (let i = 0; i < this.size; ++i) {
|
|
141
|
-
if (this.fds[i] == null) {
|
|
142
|
-
index = i;
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
let entry;
|
|
148
|
-
if (mappedPath === '<stdout>') {
|
|
149
|
-
entry = new StandardOutput((_a = this.print) !== null && _a !== void 0 ? _a : console.log, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
150
|
-
}
|
|
151
|
-
else if (mappedPath === '<stderr>') {
|
|
152
|
-
entry = new StandardOutput((_b = this.printErr) !== null && _b !== void 0 ? _b : console.error, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
entry = new FileDescriptor(index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
156
|
-
}
|
|
157
|
-
this.fds[index] = entry;
|
|
158
|
-
this.used++;
|
|
159
|
-
return entry;
|
|
160
|
-
}
|
|
161
|
-
get(id, base, inheriting) {
|
|
162
|
-
if (id >= this.size) {
|
|
163
|
-
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
164
|
-
}
|
|
165
|
-
const entry = this.fds[id];
|
|
166
|
-
if (!entry || entry.id !== id) {
|
|
167
|
-
throw new WasiError('Bad file descriptor', WasiErrno.EBADF);
|
|
168
|
-
}
|
|
169
|
-
/* Validate that the fd has the necessary rights. */
|
|
170
|
-
if ((~entry.rightsBase & base) !== BigInt(0) || (~entry.rightsInheriting & inheriting) !== BigInt(0)) {
|
|
171
|
-
throw new WasiError('Capabilities insufficient', WasiErrno.ENOTCAPABLE);
|
|
172
|
-
}
|
|
173
|
-
return entry;
|
|
174
|
-
}
|
|
175
|
-
remove(id) {
|
|
176
|
-
if (id >= this.size) {
|
|
177
|
-
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
178
|
-
}
|
|
179
|
-
const entry = this.fds[id];
|
|
180
|
-
if (!entry || entry.id !== id) {
|
|
181
|
-
throw new WasiError('Bad file descriptor', WasiErrno.EBADF);
|
|
182
|
-
}
|
|
183
|
-
this.fds[id] = undefined;
|
|
184
|
-
this.used--;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
export class SyncTable extends FileDescriptorTable {
|
|
188
|
-
constructor(options) {
|
|
189
|
-
super(options);
|
|
190
|
-
this.fs = options.fs;
|
|
191
|
-
}
|
|
192
|
-
getFileTypeByFd(fd) {
|
|
193
|
-
const stats = this.fs.fstatSync(fd, { bigint: true });
|
|
194
|
-
return toFileType(stats);
|
|
195
|
-
}
|
|
196
|
-
insertPreopen(fd, mappedPath, realPath) {
|
|
197
|
-
const type = this.getFileTypeByFd(fd);
|
|
198
|
-
if (type !== WasiFileType.DIRECTORY) {
|
|
199
|
-
throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, WasiErrno.ENOTDIR);
|
|
200
|
-
}
|
|
201
|
-
const result = getRights(this.stdio, fd, 0, type);
|
|
202
|
-
return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
|
|
203
|
-
}
|
|
204
|
-
renumber(dst, src) {
|
|
205
|
-
if (dst === src)
|
|
206
|
-
return;
|
|
207
|
-
if (dst >= this.size || src >= this.size) {
|
|
208
|
-
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
209
|
-
}
|
|
210
|
-
const dstEntry = this.fds[dst];
|
|
211
|
-
const srcEntry = this.fds[src];
|
|
212
|
-
if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
|
|
213
|
-
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
214
|
-
}
|
|
215
|
-
this.fs.closeSync(dstEntry.fd);
|
|
216
|
-
this.fds[dst] = this.fds[src];
|
|
217
|
-
this.fds[dst].id = dst;
|
|
218
|
-
this.fds[src] = undefined;
|
|
219
|
-
this.used--;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
export class AsyncTable extends FileDescriptorTable {
|
|
223
|
-
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
224
|
-
constructor(options) {
|
|
225
|
-
super(options);
|
|
226
|
-
}
|
|
227
|
-
async getFileTypeByFd(fd) {
|
|
228
|
-
const stats = await fd.stat({ bigint: true });
|
|
229
|
-
return toFileType(stats);
|
|
230
|
-
}
|
|
231
|
-
async insertPreopen(fd, mappedPath, realPath) {
|
|
232
|
-
const type = await this.getFileTypeByFd(fd);
|
|
233
|
-
if (type !== WasiFileType.DIRECTORY) {
|
|
234
|
-
throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, WasiErrno.ENOTDIR);
|
|
235
|
-
}
|
|
236
|
-
const result = getRights(this.stdio, fd.fd, 0, type);
|
|
237
|
-
return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
|
|
238
|
-
}
|
|
239
|
-
async renumber(dst, src) {
|
|
240
|
-
if (dst === src)
|
|
241
|
-
return;
|
|
242
|
-
if (dst >= this.size || src >= this.size) {
|
|
243
|
-
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
244
|
-
}
|
|
245
|
-
const dstEntry = this.fds[dst];
|
|
246
|
-
const srcEntry = this.fds[src];
|
|
247
|
-
if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
|
|
248
|
-
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
249
|
-
}
|
|
250
|
-
await dstEntry.fd.close();
|
|
251
|
-
this.fds[dst] = this.fds[src];
|
|
252
|
-
this.fds[dst].id = dst;
|
|
253
|
-
this.fds[src] = undefined;
|
|
254
|
-
this.used--;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
1
|
+
import { WasiErrno, FileControlFlag, WasiFileType, WasiWhence } from "./types.mjs";
|
|
2
|
+
import { getRights } from "./rights.mjs";
|
|
3
|
+
import { WasiError } from "./error.mjs";
|
|
4
|
+
export function concatBuffer(buffers, size) {
|
|
5
|
+
let total = 0;
|
|
6
|
+
if (typeof size === 'number' && size >= 0) {
|
|
7
|
+
total = size;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
for (let i = 0; i < buffers.length; i++) {
|
|
11
|
+
const buffer = buffers[i];
|
|
12
|
+
total += buffer.length;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
let pos = 0;
|
|
16
|
+
const ret = new Uint8Array(total);
|
|
17
|
+
for (let i = 0; i < buffers.length; i++) {
|
|
18
|
+
const buffer = buffers[i];
|
|
19
|
+
ret.set(buffer, pos);
|
|
20
|
+
pos += buffer.length;
|
|
21
|
+
}
|
|
22
|
+
return ret;
|
|
23
|
+
}
|
|
24
|
+
export class FileDescriptor {
|
|
25
|
+
constructor(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
26
|
+
this.id = id;
|
|
27
|
+
this.fd = fd;
|
|
28
|
+
this.path = path;
|
|
29
|
+
this.realPath = realPath;
|
|
30
|
+
this.type = type;
|
|
31
|
+
this.rightsBase = rightsBase;
|
|
32
|
+
this.rightsInheriting = rightsInheriting;
|
|
33
|
+
this.preopen = preopen;
|
|
34
|
+
this.pos = BigInt(0);
|
|
35
|
+
this.size = BigInt(0);
|
|
36
|
+
}
|
|
37
|
+
seek(offset, whence) {
|
|
38
|
+
if (whence === WasiWhence.SET) {
|
|
39
|
+
this.pos = BigInt(offset);
|
|
40
|
+
}
|
|
41
|
+
else if (whence === WasiWhence.CUR) {
|
|
42
|
+
this.pos += BigInt(offset);
|
|
43
|
+
}
|
|
44
|
+
else if (whence === WasiWhence.END) {
|
|
45
|
+
this.pos = BigInt(this.size) - BigInt(offset);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
throw new WasiError('Unknown whence', WasiErrno.EIO);
|
|
49
|
+
}
|
|
50
|
+
return this.pos;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class StandardOutput extends FileDescriptor {
|
|
54
|
+
constructor(log, id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
55
|
+
super(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
56
|
+
this._log = log;
|
|
57
|
+
this._buf = null;
|
|
58
|
+
}
|
|
59
|
+
write(buffer) {
|
|
60
|
+
const originalBuffer = buffer;
|
|
61
|
+
if (this._buf) {
|
|
62
|
+
buffer = concatBuffer([this._buf, buffer]);
|
|
63
|
+
this._buf = null;
|
|
64
|
+
}
|
|
65
|
+
if (buffer.indexOf(10) === -1) {
|
|
66
|
+
this._buf = buffer;
|
|
67
|
+
return originalBuffer.byteLength;
|
|
68
|
+
}
|
|
69
|
+
let written = 0;
|
|
70
|
+
let lastBegin = 0;
|
|
71
|
+
let index;
|
|
72
|
+
while ((index = buffer.indexOf(10, written)) !== -1) {
|
|
73
|
+
const str = new TextDecoder().decode(buffer.subarray(lastBegin, index));
|
|
74
|
+
this._log(str);
|
|
75
|
+
written += index - lastBegin + 1;
|
|
76
|
+
lastBegin = index + 1;
|
|
77
|
+
}
|
|
78
|
+
if (written < buffer.length) {
|
|
79
|
+
this._buf = buffer.slice(written);
|
|
80
|
+
}
|
|
81
|
+
return originalBuffer.byteLength;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function toFileType(stat) {
|
|
85
|
+
if (stat.isBlockDevice())
|
|
86
|
+
return WasiFileType.BLOCK_DEVICE;
|
|
87
|
+
if (stat.isCharacterDevice())
|
|
88
|
+
return WasiFileType.CHARACTER_DEVICE;
|
|
89
|
+
if (stat.isDirectory())
|
|
90
|
+
return WasiFileType.DIRECTORY;
|
|
91
|
+
if (stat.isSocket())
|
|
92
|
+
return WasiFileType.SOCKET_STREAM;
|
|
93
|
+
if (stat.isFile())
|
|
94
|
+
return WasiFileType.REGULAR_FILE;
|
|
95
|
+
if (stat.isSymbolicLink())
|
|
96
|
+
return WasiFileType.SYMBOLIC_LINK;
|
|
97
|
+
return WasiFileType.UNKNOWN;
|
|
98
|
+
}
|
|
99
|
+
export function toFileStat(view, buf, stat) {
|
|
100
|
+
view.setBigUint64(buf, stat.dev, true);
|
|
101
|
+
view.setBigUint64(buf + 8, stat.ino, true);
|
|
102
|
+
view.setBigUint64(buf + 16, BigInt(toFileType(stat)), true);
|
|
103
|
+
view.setBigUint64(buf + 24, stat.nlink, true);
|
|
104
|
+
view.setBigUint64(buf + 32, stat.size, true);
|
|
105
|
+
view.setBigUint64(buf + 40, stat.atimeMs * BigInt(1000000), true);
|
|
106
|
+
view.setBigUint64(buf + 48, stat.mtimeMs * BigInt(1000000), true);
|
|
107
|
+
view.setBigUint64(buf + 56, stat.ctimeMs * BigInt(1000000), true);
|
|
108
|
+
}
|
|
109
|
+
export class FileDescriptorTable {
|
|
110
|
+
constructor(options) {
|
|
111
|
+
this.used = 0;
|
|
112
|
+
this.size = options.size;
|
|
113
|
+
this.fds = Array(options.size);
|
|
114
|
+
this.stdio = [options.in, options.out, options.err];
|
|
115
|
+
this.print = options.print;
|
|
116
|
+
this.printErr = options.printErr;
|
|
117
|
+
this.insertStdio(options.in, 0, '<stdin>');
|
|
118
|
+
this.insertStdio(options.out, 1, '<stdout>');
|
|
119
|
+
this.insertStdio(options.err, 2, '<stderr>');
|
|
120
|
+
}
|
|
121
|
+
insertStdio(fd, expected, name) {
|
|
122
|
+
const type = WasiFileType.CHARACTER_DEVICE;
|
|
123
|
+
const { base, inheriting } = getRights(this.stdio, fd, FileControlFlag.O_RDWR, type);
|
|
124
|
+
const wrap = this.insert(fd, name, name, type, base, inheriting, 0);
|
|
125
|
+
if (wrap.id !== expected) {
|
|
126
|
+
throw new WasiError(`id: ${wrap.id} !== expected: ${expected}`, WasiErrno.EBADF);
|
|
127
|
+
}
|
|
128
|
+
return wrap;
|
|
129
|
+
}
|
|
130
|
+
insert(fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
131
|
+
var _a, _b;
|
|
132
|
+
let index = -1;
|
|
133
|
+
if (this.used >= this.size) {
|
|
134
|
+
const newSize = this.size * 2;
|
|
135
|
+
this.fds.length = newSize;
|
|
136
|
+
index = this.size;
|
|
137
|
+
this.size = newSize;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
for (let i = 0; i < this.size; ++i) {
|
|
141
|
+
if (this.fds[i] == null) {
|
|
142
|
+
index = i;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
let entry;
|
|
148
|
+
if (mappedPath === '<stdout>') {
|
|
149
|
+
entry = new StandardOutput((_a = this.print) !== null && _a !== void 0 ? _a : console.log, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
150
|
+
}
|
|
151
|
+
else if (mappedPath === '<stderr>') {
|
|
152
|
+
entry = new StandardOutput((_b = this.printErr) !== null && _b !== void 0 ? _b : console.error, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
entry = new FileDescriptor(index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
156
|
+
}
|
|
157
|
+
this.fds[index] = entry;
|
|
158
|
+
this.used++;
|
|
159
|
+
return entry;
|
|
160
|
+
}
|
|
161
|
+
get(id, base, inheriting) {
|
|
162
|
+
if (id >= this.size) {
|
|
163
|
+
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
164
|
+
}
|
|
165
|
+
const entry = this.fds[id];
|
|
166
|
+
if (!entry || entry.id !== id) {
|
|
167
|
+
throw new WasiError('Bad file descriptor', WasiErrno.EBADF);
|
|
168
|
+
}
|
|
169
|
+
/* Validate that the fd has the necessary rights. */
|
|
170
|
+
if ((~entry.rightsBase & base) !== BigInt(0) || (~entry.rightsInheriting & inheriting) !== BigInt(0)) {
|
|
171
|
+
throw new WasiError('Capabilities insufficient', WasiErrno.ENOTCAPABLE);
|
|
172
|
+
}
|
|
173
|
+
return entry;
|
|
174
|
+
}
|
|
175
|
+
remove(id) {
|
|
176
|
+
if (id >= this.size) {
|
|
177
|
+
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
178
|
+
}
|
|
179
|
+
const entry = this.fds[id];
|
|
180
|
+
if (!entry || entry.id !== id) {
|
|
181
|
+
throw new WasiError('Bad file descriptor', WasiErrno.EBADF);
|
|
182
|
+
}
|
|
183
|
+
this.fds[id] = undefined;
|
|
184
|
+
this.used--;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export class SyncTable extends FileDescriptorTable {
|
|
188
|
+
constructor(options) {
|
|
189
|
+
super(options);
|
|
190
|
+
this.fs = options.fs;
|
|
191
|
+
}
|
|
192
|
+
getFileTypeByFd(fd) {
|
|
193
|
+
const stats = this.fs.fstatSync(fd, { bigint: true });
|
|
194
|
+
return toFileType(stats);
|
|
195
|
+
}
|
|
196
|
+
insertPreopen(fd, mappedPath, realPath) {
|
|
197
|
+
const type = this.getFileTypeByFd(fd);
|
|
198
|
+
if (type !== WasiFileType.DIRECTORY) {
|
|
199
|
+
throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, WasiErrno.ENOTDIR);
|
|
200
|
+
}
|
|
201
|
+
const result = getRights(this.stdio, fd, 0, type);
|
|
202
|
+
return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
|
|
203
|
+
}
|
|
204
|
+
renumber(dst, src) {
|
|
205
|
+
if (dst === src)
|
|
206
|
+
return;
|
|
207
|
+
if (dst >= this.size || src >= this.size) {
|
|
208
|
+
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
209
|
+
}
|
|
210
|
+
const dstEntry = this.fds[dst];
|
|
211
|
+
const srcEntry = this.fds[src];
|
|
212
|
+
if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
|
|
213
|
+
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
214
|
+
}
|
|
215
|
+
this.fs.closeSync(dstEntry.fd);
|
|
216
|
+
this.fds[dst] = this.fds[src];
|
|
217
|
+
this.fds[dst].id = dst;
|
|
218
|
+
this.fds[src] = undefined;
|
|
219
|
+
this.used--;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
export class AsyncTable extends FileDescriptorTable {
|
|
223
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
224
|
+
constructor(options) {
|
|
225
|
+
super(options);
|
|
226
|
+
}
|
|
227
|
+
async getFileTypeByFd(fd) {
|
|
228
|
+
const stats = await fd.stat({ bigint: true });
|
|
229
|
+
return toFileType(stats);
|
|
230
|
+
}
|
|
231
|
+
async insertPreopen(fd, mappedPath, realPath) {
|
|
232
|
+
const type = await this.getFileTypeByFd(fd);
|
|
233
|
+
if (type !== WasiFileType.DIRECTORY) {
|
|
234
|
+
throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, WasiErrno.ENOTDIR);
|
|
235
|
+
}
|
|
236
|
+
const result = getRights(this.stdio, fd.fd, 0, type);
|
|
237
|
+
return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
|
|
238
|
+
}
|
|
239
|
+
async renumber(dst, src) {
|
|
240
|
+
if (dst === src)
|
|
241
|
+
return;
|
|
242
|
+
if (dst >= this.size || src >= this.size) {
|
|
243
|
+
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
244
|
+
}
|
|
245
|
+
const dstEntry = this.fds[dst];
|
|
246
|
+
const srcEntry = this.fds[src];
|
|
247
|
+
if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
|
|
248
|
+
throw new WasiError('Invalid fd', WasiErrno.EBADF);
|
|
249
|
+
}
|
|
250
|
+
await dstEntry.fd.close();
|
|
251
|
+
this.fds[dst] = this.fds[src];
|
|
252
|
+
this.fds[dst].id = dst;
|
|
253
|
+
this.fds[src] = undefined;
|
|
254
|
+
this.used--;
|
|
255
|
+
}
|
|
256
|
+
}
|
package/lib/mjs/wasi/fs.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|