@ziex/cli 0.1.0-dev.865 → 0.1.0-test.1
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/package.json +10 -29
- package/README.md +0 -195
- package/app.d.ts +0 -112
- package/aws-lambda/index.d.ts +0 -96
- package/aws-lambda/index.js +0 -126
- package/build.zig +0 -5
- package/build.zig.zon +0 -7
- package/cloudflare/app.d.ts +0 -2
- package/cloudflare/do.d.ts +0 -48
- package/cloudflare/index.d.ts +0 -4
- package/cloudflare/index.js +0 -1707
- package/cloudflare/kv.d.ts +0 -2
- package/cloudflare/worker.d.ts +0 -3
- package/index.d.ts +0 -2
- package/index.js +0 -739
- package/kv.d.ts +0 -27
- package/react/dom.d.ts +0 -21
- package/react/index.d.ts +0 -2
- package/react/index.js +0 -144
- package/react/types.d.ts +0 -190
- package/runtime.d.ts +0 -70
- package/vercel/index.d.ts +0 -26
- package/vercel/index.js +0 -18
- package/wasi.d.ts +0 -61
- package/wasm/core.d.ts +0 -78
- package/wasm/dom.d.ts +0 -4
- package/wasm/index.d.ts +0 -46
- package/wasm/index.js +0 -895
- package/wasm/init.d.ts +0 -1
- package/wasm/init.js +0 -873
- package/wasm/types.d.ts +0 -1
- package/wasm/wasi.d.ts +0 -28
- package/zx.d.ts +0 -1
package/index.js
DELETED
|
@@ -1,739 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, {
|
|
5
|
-
get: all[name],
|
|
6
|
-
enumerable: true,
|
|
7
|
-
configurable: true,
|
|
8
|
-
set: (newValue) => all[name] = () => newValue
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// src/runtime.ts
|
|
13
|
-
var exports_runtime = {};
|
|
14
|
-
__export(exports_runtime, {
|
|
15
|
-
run: () => run,
|
|
16
|
-
buildWsImports: () => buildWsImports,
|
|
17
|
-
attachWebSocket: () => attachWebSocket
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// src/wasm/wasi.ts
|
|
21
|
-
var decoder = new TextDecoder;
|
|
22
|
-
var encoder = new TextEncoder;
|
|
23
|
-
|
|
24
|
-
class ZxWasiBridge {
|
|
25
|
-
#alloc;
|
|
26
|
-
#fetchCompleteHandler;
|
|
27
|
-
#memory;
|
|
28
|
-
#cb;
|
|
29
|
-
#intervals = new Map;
|
|
30
|
-
#memView = null;
|
|
31
|
-
#memBuf = null;
|
|
32
|
-
constructor(exports) {
|
|
33
|
-
this.#memory = exports.memory;
|
|
34
|
-
this.#alloc = exports.__zx_alloc;
|
|
35
|
-
this.#fetchCompleteHandler = exports.__zx_fetch_complete;
|
|
36
|
-
this.#cb = exports.__zx_cb;
|
|
37
|
-
}
|
|
38
|
-
#view() {
|
|
39
|
-
const buf = this.#memory.buffer;
|
|
40
|
-
if (buf !== this.#memBuf) {
|
|
41
|
-
this.#memBuf = buf;
|
|
42
|
-
this.#memView = new Uint8Array(buf);
|
|
43
|
-
}
|
|
44
|
-
return this.#memView;
|
|
45
|
-
}
|
|
46
|
-
#readString(ptr, len) {
|
|
47
|
-
return decoder.decode(this.#view().subarray(ptr, ptr + len));
|
|
48
|
-
}
|
|
49
|
-
#writeBytes(ptr, data) {
|
|
50
|
-
this.#view().set(data, ptr);
|
|
51
|
-
}
|
|
52
|
-
log(level, ptr, len) {
|
|
53
|
-
const msg = decoder.decode(this.#view().subarray(ptr, ptr + len));
|
|
54
|
-
switch (level) {
|
|
55
|
-
case 0:
|
|
56
|
-
console.error(msg);
|
|
57
|
-
break;
|
|
58
|
-
case 1:
|
|
59
|
-
console.warn(msg);
|
|
60
|
-
break;
|
|
61
|
-
case 3:
|
|
62
|
-
console.debug(msg);
|
|
63
|
-
break;
|
|
64
|
-
default:
|
|
65
|
-
console.log(msg);
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
fetchAsync(urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) {
|
|
70
|
-
const url = this.#readString(urlPtr, urlLen);
|
|
71
|
-
const method = methodLen > 0 ? this.#readString(methodPtr, methodLen) : "GET";
|
|
72
|
-
const headersJson = headersLen > 0 ? this.#readString(headersPtr, headersLen) : "{}";
|
|
73
|
-
const body = bodyLen > 0 ? this.#readString(bodyPtr, bodyLen) : undefined;
|
|
74
|
-
let headers = {};
|
|
75
|
-
try {
|
|
76
|
-
headers = JSON.parse(headersJson);
|
|
77
|
-
} catch {
|
|
78
|
-
for (const line of headersJson.split(`
|
|
79
|
-
`)) {
|
|
80
|
-
const i = line.indexOf(":");
|
|
81
|
-
if (i > 0)
|
|
82
|
-
headers[line.slice(0, i)] = line.slice(i + 1);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
const controller = new AbortController;
|
|
86
|
-
const timeout = timeoutMs > 0 ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
87
|
-
fetch(url, {
|
|
88
|
-
method,
|
|
89
|
-
headers: Object.keys(headers).length > 0 ? headers : undefined,
|
|
90
|
-
body: method !== "GET" && method !== "HEAD" ? body : undefined,
|
|
91
|
-
signal: controller.signal
|
|
92
|
-
}).then(async (res) => {
|
|
93
|
-
if (timeout)
|
|
94
|
-
clearTimeout(timeout);
|
|
95
|
-
this.#notifyFetchComplete(fetchId, res.status, await res.text(), false);
|
|
96
|
-
}).catch((err) => {
|
|
97
|
-
if (timeout)
|
|
98
|
-
clearTimeout(timeout);
|
|
99
|
-
const msg = err.name === "AbortError" ? "Request timeout" : err.message ?? "Fetch failed";
|
|
100
|
-
this.#notifyFetchComplete(fetchId, 0, msg, true);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
#notifyFetchComplete(fetchId, status, body, isError) {
|
|
104
|
-
const encoded = encoder.encode(body);
|
|
105
|
-
const ptr = this.#alloc(encoded.length);
|
|
106
|
-
this.#writeBytes(ptr, encoded);
|
|
107
|
-
this.#fetchCompleteHandler(fetchId, status, ptr, encoded.length, isError ? 1 : 0);
|
|
108
|
-
}
|
|
109
|
-
setTimeout(callbackId, delayMs) {
|
|
110
|
-
setTimeout(() => this.#cb?.(3, callbackId, 0n), delayMs);
|
|
111
|
-
}
|
|
112
|
-
setInterval(callbackId, intervalMs) {
|
|
113
|
-
const handle = setInterval(() => this.#cb?.(4, callbackId, 0n), intervalMs);
|
|
114
|
-
this.#intervals.set(callbackId, handle);
|
|
115
|
-
}
|
|
116
|
-
clearInterval(callbackId) {
|
|
117
|
-
const handle = this.#intervals.get(callbackId);
|
|
118
|
-
if (handle !== undefined) {
|
|
119
|
-
clearInterval(handle);
|
|
120
|
-
this.#intervals.delete(callbackId);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
static createImportObject(bridgeRef) {
|
|
124
|
-
return {
|
|
125
|
-
__zx: {
|
|
126
|
-
_log: (level, ptr, len) => {
|
|
127
|
-
bridgeRef.current?.log(level, ptr, len);
|
|
128
|
-
},
|
|
129
|
-
_fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
|
|
130
|
-
bridgeRef.current?.fetchAsync(urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId);
|
|
131
|
-
},
|
|
132
|
-
_setTimeout: (callbackId, delayMs) => {
|
|
133
|
-
bridgeRef.current?.setTimeout(callbackId, delayMs);
|
|
134
|
-
},
|
|
135
|
-
_setInterval: (callbackId, intervalMs) => {
|
|
136
|
-
bridgeRef.current?.setInterval(callbackId, intervalMs);
|
|
137
|
-
},
|
|
138
|
-
_clearInterval: (callbackId) => {
|
|
139
|
-
bridgeRef.current?.clearInterval(callbackId);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// src/kv.ts
|
|
147
|
-
var exports_kv = {};
|
|
148
|
-
__export(exports_kv, {
|
|
149
|
-
createMemoryKV: () => createMemoryKV,
|
|
150
|
-
createKVImports: () => createKVImports
|
|
151
|
-
});
|
|
152
|
-
function createMemoryKV() {
|
|
153
|
-
const store = new Map;
|
|
154
|
-
return {
|
|
155
|
-
async get(key) {
|
|
156
|
-
return store.get(key) ?? null;
|
|
157
|
-
},
|
|
158
|
-
async put(key, value) {
|
|
159
|
-
store.set(key, value);
|
|
160
|
-
},
|
|
161
|
-
async delete(key) {
|
|
162
|
-
store.delete(key);
|
|
163
|
-
},
|
|
164
|
-
async list(options) {
|
|
165
|
-
const keys = [...store.keys()].filter((k) => !options?.prefix || k.startsWith(options.prefix)).map((name) => ({ name }));
|
|
166
|
-
return { keys };
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
function createKVImports(bindings, getMemory) {
|
|
171
|
-
const encoder2 = new TextEncoder;
|
|
172
|
-
const decoder2 = new TextDecoder;
|
|
173
|
-
function readStr(ptr, len) {
|
|
174
|
-
return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
|
|
175
|
-
}
|
|
176
|
-
function writeBytes(buf_ptr, buf_max, data) {
|
|
177
|
-
if (data.length > buf_max)
|
|
178
|
-
return -2;
|
|
179
|
-
new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
|
|
180
|
-
return data.length;
|
|
181
|
-
}
|
|
182
|
-
function binding(ns) {
|
|
183
|
-
return bindings[ns] ?? bindings["default"] ?? null;
|
|
184
|
-
}
|
|
185
|
-
const Suspending = WebAssembly.Suspending;
|
|
186
|
-
if (typeof Suspending !== "function") {
|
|
187
|
-
return {
|
|
188
|
-
kv_get: (_ns, _nsLen, _key, _keyLen, _buf, _max) => -1,
|
|
189
|
-
kv_put: (_ns, _nsLen, _key, _keyLen, _val, _valLen) => 0,
|
|
190
|
-
kv_delete: (_ns, _nsLen, _key, _keyLen) => 0,
|
|
191
|
-
kv_list: (_ns, _nsLen, _pfx, _pfxLen, buf_ptr, buf_max) => writeBytes(buf_ptr, buf_max, encoder2.encode("[]"))
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
return {
|
|
195
|
-
kv_get: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, buf_ptr, buf_max) => {
|
|
196
|
-
const b = binding(readStr(ns_ptr, ns_len));
|
|
197
|
-
if (!b)
|
|
198
|
-
return -1;
|
|
199
|
-
const value = await b.get(readStr(key_ptr, key_len));
|
|
200
|
-
if (value === null)
|
|
201
|
-
return -1;
|
|
202
|
-
return writeBytes(buf_ptr, buf_max, encoder2.encode(value));
|
|
203
|
-
}),
|
|
204
|
-
kv_put: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len) => {
|
|
205
|
-
const b = binding(readStr(ns_ptr, ns_len));
|
|
206
|
-
if (!b)
|
|
207
|
-
return -1;
|
|
208
|
-
await b.put(readStr(key_ptr, key_len), readStr(val_ptr, val_len));
|
|
209
|
-
return 0;
|
|
210
|
-
}),
|
|
211
|
-
kv_delete: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len) => {
|
|
212
|
-
const b = binding(readStr(ns_ptr, ns_len));
|
|
213
|
-
if (!b)
|
|
214
|
-
return -1;
|
|
215
|
-
await b.delete(readStr(key_ptr, key_len));
|
|
216
|
-
return 0;
|
|
217
|
-
}),
|
|
218
|
-
kv_list: new Suspending(async (ns_ptr, ns_len, prefix_ptr, prefix_len, buf_ptr, buf_max) => {
|
|
219
|
-
const b = binding(readStr(ns_ptr, ns_len));
|
|
220
|
-
if (!b)
|
|
221
|
-
return writeBytes(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
222
|
-
const prefix = readStr(prefix_ptr, prefix_len);
|
|
223
|
-
const result = await b.list(prefix.length > 0 ? { prefix } : undefined);
|
|
224
|
-
return writeBytes(buf_ptr, buf_max, encoder2.encode(JSON.stringify(result.keys.map((k) => k.name))));
|
|
225
|
-
})
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// src/wasi.ts
|
|
230
|
-
class ProcExit extends Error {
|
|
231
|
-
code;
|
|
232
|
-
constructor(code) {
|
|
233
|
-
super(`proc_exit(${code})`);
|
|
234
|
-
this.code = code;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function createWasiImports({
|
|
238
|
-
request,
|
|
239
|
-
stdinData,
|
|
240
|
-
onStdout
|
|
241
|
-
}) {
|
|
242
|
-
const encoder2 = new TextEncoder;
|
|
243
|
-
const url = new URL(request.url);
|
|
244
|
-
const argStrings = [
|
|
245
|
-
"wasm",
|
|
246
|
-
"--pathname",
|
|
247
|
-
url.pathname,
|
|
248
|
-
"--method",
|
|
249
|
-
request.method,
|
|
250
|
-
"--search",
|
|
251
|
-
url.search
|
|
252
|
-
];
|
|
253
|
-
request.headers.forEach((value, name) => {
|
|
254
|
-
if (value)
|
|
255
|
-
argStrings.push("--header", `${name}:${value}`);
|
|
256
|
-
});
|
|
257
|
-
const encodedArgs = argStrings.map((a) => encoder2.encode(a + "\x00"));
|
|
258
|
-
const argBufSize = encodedArgs.reduce((s, a) => s + a.length, 0);
|
|
259
|
-
let wasmMemory = null;
|
|
260
|
-
const setMemory = (m2) => {
|
|
261
|
-
wasmMemory = m2;
|
|
262
|
-
};
|
|
263
|
-
const stdoutChunks = [];
|
|
264
|
-
let stderrMeta = "";
|
|
265
|
-
let stderrPartial = "";
|
|
266
|
-
const stderrDecoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
267
|
-
function processStderrChunk(chunk) {
|
|
268
|
-
const text = stderrDecoder.decode(chunk, { stream: true });
|
|
269
|
-
const lines = (stderrPartial + text).split(`
|
|
270
|
-
`);
|
|
271
|
-
stderrPartial = lines.pop() ?? "";
|
|
272
|
-
for (const line of lines) {
|
|
273
|
-
if (line.startsWith("__EDGE_META__:")) {
|
|
274
|
-
stderrMeta += line + `
|
|
275
|
-
`;
|
|
276
|
-
} else if (line.length > 0) {
|
|
277
|
-
console.error("[ziex]", line);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
let stdinOffset = 0;
|
|
282
|
-
function v() {
|
|
283
|
-
return new DataView(wasmMemory.buffer);
|
|
284
|
-
}
|
|
285
|
-
function m() {
|
|
286
|
-
return new Uint8Array(wasmMemory.buffer);
|
|
287
|
-
}
|
|
288
|
-
const wasiImport = {
|
|
289
|
-
args_sizes_get(argc_ptr, argv_buf_size_ptr) {
|
|
290
|
-
v().setUint32(argc_ptr, encodedArgs.length, true);
|
|
291
|
-
v().setUint32(argv_buf_size_ptr, argBufSize, true);
|
|
292
|
-
return 0;
|
|
293
|
-
},
|
|
294
|
-
args_get(argv_ptr, argv_buf_ptr) {
|
|
295
|
-
const dv = v();
|
|
296
|
-
const mem = m();
|
|
297
|
-
let offset = argv_buf_ptr;
|
|
298
|
-
for (const arg of encodedArgs) {
|
|
299
|
-
dv.setUint32(argv_ptr, offset, true);
|
|
300
|
-
mem.set(arg, offset);
|
|
301
|
-
argv_ptr += 4;
|
|
302
|
-
offset += arg.length;
|
|
303
|
-
}
|
|
304
|
-
return 0;
|
|
305
|
-
},
|
|
306
|
-
environ_sizes_get(count_ptr, buf_size_ptr) {
|
|
307
|
-
v().setUint32(count_ptr, 0, true);
|
|
308
|
-
v().setUint32(buf_size_ptr, 0, true);
|
|
309
|
-
return 0;
|
|
310
|
-
},
|
|
311
|
-
environ_get(_environ_ptr, _environ_buf_ptr) {
|
|
312
|
-
return 0;
|
|
313
|
-
},
|
|
314
|
-
fd_write(fd, iovs_ptr, iovs_len, nwritten_ptr) {
|
|
315
|
-
const dv = v();
|
|
316
|
-
const mem = m();
|
|
317
|
-
let written = 0;
|
|
318
|
-
for (let i = 0;i < iovs_len; i++) {
|
|
319
|
-
const buf_ptr = dv.getUint32(iovs_ptr + i * 8, true);
|
|
320
|
-
const buf_len = dv.getUint32(iovs_ptr + i * 8 + 4, true);
|
|
321
|
-
const chunk = mem.slice(buf_ptr, buf_ptr + buf_len);
|
|
322
|
-
if (fd === 1) {
|
|
323
|
-
if (onStdout)
|
|
324
|
-
onStdout(chunk);
|
|
325
|
-
else
|
|
326
|
-
stdoutChunks.push(chunk);
|
|
327
|
-
} else if (fd === 2)
|
|
328
|
-
processStderrChunk(chunk);
|
|
329
|
-
written += buf_len;
|
|
330
|
-
}
|
|
331
|
-
dv.setUint32(nwritten_ptr, written, true);
|
|
332
|
-
return 0;
|
|
333
|
-
},
|
|
334
|
-
fd_read(fd, iovs_ptr, iovs_len, nread_ptr) {
|
|
335
|
-
const dv = v();
|
|
336
|
-
const mem = m();
|
|
337
|
-
const stdin = stdinData ?? new Uint8Array(0);
|
|
338
|
-
let totalRead = 0;
|
|
339
|
-
for (let i = 0;i < iovs_len; i++) {
|
|
340
|
-
const buf_ptr = dv.getUint32(iovs_ptr + i * 8, true);
|
|
341
|
-
const buf_len = dv.getUint32(iovs_ptr + i * 8 + 4, true);
|
|
342
|
-
if (fd === 0 && stdinOffset < stdin.length) {
|
|
343
|
-
const toRead = Math.min(buf_len, stdin.length - stdinOffset);
|
|
344
|
-
mem.set(stdin.subarray(stdinOffset, stdinOffset + toRead), buf_ptr);
|
|
345
|
-
stdinOffset += toRead;
|
|
346
|
-
totalRead += toRead;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
dv.setUint32(nread_ptr, totalRead, true);
|
|
350
|
-
return 0;
|
|
351
|
-
},
|
|
352
|
-
fd_fdstat_get(_fd, fdstat_ptr) {
|
|
353
|
-
const dv = v();
|
|
354
|
-
dv.setUint8(fdstat_ptr, 2);
|
|
355
|
-
dv.setUint8(fdstat_ptr + 1, 0);
|
|
356
|
-
dv.setUint16(fdstat_ptr + 2, 0, true);
|
|
357
|
-
dv.setUint32(fdstat_ptr + 4, 0, true);
|
|
358
|
-
dv.setBigUint64(fdstat_ptr + 8, 0n, true);
|
|
359
|
-
dv.setBigUint64(fdstat_ptr + 16, 0n, true);
|
|
360
|
-
return 0;
|
|
361
|
-
},
|
|
362
|
-
fd_prestat_get(_fd, _bufptr) {
|
|
363
|
-
return 8;
|
|
364
|
-
},
|
|
365
|
-
fd_prestat_dir_name(_fd, _path, _path_len) {
|
|
366
|
-
return 8;
|
|
367
|
-
},
|
|
368
|
-
fd_close(_fd) {
|
|
369
|
-
return 0;
|
|
370
|
-
},
|
|
371
|
-
fd_pread(_fd, _iovs, _iovs_len, _offset, nread_ptr) {
|
|
372
|
-
v().setUint32(nread_ptr, 0, true);
|
|
373
|
-
return 0;
|
|
374
|
-
},
|
|
375
|
-
fd_pwrite(_fd, _iovs, _iovs_len, _offset, nwritten_ptr) {
|
|
376
|
-
v().setUint32(nwritten_ptr, 0, true);
|
|
377
|
-
return 0;
|
|
378
|
-
},
|
|
379
|
-
fd_filestat_get(_fd, filestat_ptr) {
|
|
380
|
-
const dv = v();
|
|
381
|
-
dv.setBigUint64(filestat_ptr, 0n, true);
|
|
382
|
-
dv.setBigUint64(filestat_ptr + 8, 0n, true);
|
|
383
|
-
dv.setUint8(filestat_ptr + 16, 2);
|
|
384
|
-
dv.setBigUint64(filestat_ptr + 24, 1n, true);
|
|
385
|
-
dv.setBigUint64(filestat_ptr + 32, 0n, true);
|
|
386
|
-
dv.setBigUint64(filestat_ptr + 40, 0n, true);
|
|
387
|
-
dv.setBigUint64(filestat_ptr + 48, 0n, true);
|
|
388
|
-
dv.setBigUint64(filestat_ptr + 56, 0n, true);
|
|
389
|
-
return 0;
|
|
390
|
-
},
|
|
391
|
-
fd_seek(_fd, _offset, _whence, newoffset_ptr) {
|
|
392
|
-
v().setBigInt64(newoffset_ptr, 0n, true);
|
|
393
|
-
return 0;
|
|
394
|
-
},
|
|
395
|
-
proc_exit(code) {
|
|
396
|
-
throw new ProcExit(code);
|
|
397
|
-
},
|
|
398
|
-
sched_yield() {
|
|
399
|
-
return 0;
|
|
400
|
-
},
|
|
401
|
-
clock_time_get(_id, _precision, time_ptr) {
|
|
402
|
-
v().setBigUint64(time_ptr, BigInt(Date.now()) * 1000000n, true);
|
|
403
|
-
return 0;
|
|
404
|
-
},
|
|
405
|
-
random_get(buf_ptr, buf_len) {
|
|
406
|
-
crypto.getRandomValues(new Uint8Array(wasmMemory.buffer, buf_ptr, buf_len));
|
|
407
|
-
return 0;
|
|
408
|
-
},
|
|
409
|
-
path_open(_fd, _dirflags, _path, _path_len, _oflags, _rights_base, _rights_inheriting, _fdflags, opened_fd_ptr) {
|
|
410
|
-
v().setInt32(opened_fd_ptr, -1, true);
|
|
411
|
-
return 76;
|
|
412
|
-
},
|
|
413
|
-
path_create_directory(_fd, _path, _path_len) {
|
|
414
|
-
return 76;
|
|
415
|
-
},
|
|
416
|
-
path_unlink_file(_fd, _path, _path_len) {
|
|
417
|
-
return 76;
|
|
418
|
-
},
|
|
419
|
-
path_remove_directory(_fd, _path, _path_len) {
|
|
420
|
-
return 76;
|
|
421
|
-
},
|
|
422
|
-
path_rename(_fd, _old_path, _old_path_len, _new_fd, _new_path, _new_path_len) {
|
|
423
|
-
return 76;
|
|
424
|
-
},
|
|
425
|
-
path_filestat_get(_fd, _flags, _path, _path_len, filestat_ptr) {
|
|
426
|
-
new Uint8Array(wasmMemory.buffer, filestat_ptr, 64).fill(0);
|
|
427
|
-
return 76;
|
|
428
|
-
},
|
|
429
|
-
path_readlink(_fd, _path, _path_len, _buf, _buf_len, nread_ptr) {
|
|
430
|
-
v().setUint32(nread_ptr, 0, true);
|
|
431
|
-
return 76;
|
|
432
|
-
},
|
|
433
|
-
fd_readdir(_fd, _buf, _buf_len, _cookie, bufused_ptr) {
|
|
434
|
-
v().setUint32(bufused_ptr, 0, true);
|
|
435
|
-
return 76;
|
|
436
|
-
},
|
|
437
|
-
poll_oneoff(_in, _out, _nsubscriptions, nevents_ptr) {
|
|
438
|
-
v().setUint32(nevents_ptr, 0, true);
|
|
439
|
-
return 0;
|
|
440
|
-
}
|
|
441
|
-
};
|
|
442
|
-
function collectOutput() {
|
|
443
|
-
const remaining = stderrDecoder.decode(undefined, { stream: false });
|
|
444
|
-
const tail = stderrPartial + remaining;
|
|
445
|
-
if (tail.length > 0) {
|
|
446
|
-
if (tail.startsWith("__EDGE_META__:"))
|
|
447
|
-
stderrMeta += tail;
|
|
448
|
-
else
|
|
449
|
-
console.error("[ziex]", tail);
|
|
450
|
-
stderrPartial = "";
|
|
451
|
-
}
|
|
452
|
-
return {
|
|
453
|
-
stdout: mergeUint8Arrays(stdoutChunks),
|
|
454
|
-
stderrText: stderrMeta
|
|
455
|
-
};
|
|
456
|
-
}
|
|
457
|
-
return { wasiImport, setMemory, collectOutput };
|
|
458
|
-
}
|
|
459
|
-
function mergeUint8Arrays(arrays) {
|
|
460
|
-
const totalLen = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
461
|
-
const result = new Uint8Array(totalLen);
|
|
462
|
-
let offset = 0;
|
|
463
|
-
for (const arr of arrays) {
|
|
464
|
-
result.set(arr, offset);
|
|
465
|
-
offset += arr.length;
|
|
466
|
-
}
|
|
467
|
-
return result;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
// src/runtime.ts
|
|
471
|
-
function buildWsImports(Suspending, mem, decoder2, ws) {
|
|
472
|
-
const readStr = (ptr, len) => decoder2.decode(new Uint8Array(mem().buffer, ptr, len));
|
|
473
|
-
return {
|
|
474
|
-
ws_upgrade: () => {
|
|
475
|
-
ws.upgraded = true;
|
|
476
|
-
},
|
|
477
|
-
ws_write: (ptr, len) => {
|
|
478
|
-
const data = new Uint8Array(mem().buffer, ptr, len).slice();
|
|
479
|
-
if (!ws.server) {
|
|
480
|
-
ws.pendingWrites.push(data);
|
|
481
|
-
} else {
|
|
482
|
-
ws.server.send(data);
|
|
483
|
-
}
|
|
484
|
-
},
|
|
485
|
-
ws_close: (code, reason_ptr, reason_len) => {
|
|
486
|
-
ws.server?.close(code, decoder2.decode(new Uint8Array(mem().buffer, reason_ptr, reason_len)));
|
|
487
|
-
},
|
|
488
|
-
ws_recv: Suspending ? new Suspending(async (buf_ptr, buf_max) => {
|
|
489
|
-
if (ws._resolveFirstSuspend) {
|
|
490
|
-
const fn = ws._resolveFirstSuspend;
|
|
491
|
-
ws._resolveFirstSuspend = undefined;
|
|
492
|
-
fn();
|
|
493
|
-
}
|
|
494
|
-
const deliver = (bytes) => {
|
|
495
|
-
if (bytes === null)
|
|
496
|
-
return -1;
|
|
497
|
-
const n = Math.min(bytes.length, buf_max);
|
|
498
|
-
new Uint8Array(mem().buffer, buf_ptr, n).set(bytes.subarray(0, n));
|
|
499
|
-
return n;
|
|
500
|
-
};
|
|
501
|
-
if (ws.messageQueue.length > 0)
|
|
502
|
-
return deliver(ws.messageQueue.shift());
|
|
503
|
-
return new Promise((resolve) => {
|
|
504
|
-
ws.recvResolve = (bytes) => resolve(deliver(bytes));
|
|
505
|
-
});
|
|
506
|
-
}) : (_buf_ptr, _buf_max) => -1,
|
|
507
|
-
ws_subscribe: (ptr, len) => {
|
|
508
|
-
ws.subscribe?.(readStr(ptr, len));
|
|
509
|
-
},
|
|
510
|
-
ws_unsubscribe: (ptr, len) => {
|
|
511
|
-
ws.unsubscribe?.(readStr(ptr, len));
|
|
512
|
-
},
|
|
513
|
-
ws_publish: (topic_ptr, topic_len, data_ptr, data_len) => {
|
|
514
|
-
const topic = readStr(topic_ptr, topic_len);
|
|
515
|
-
const data = new Uint8Array(mem().buffer, data_ptr, data_len).slice();
|
|
516
|
-
return ws.publish?.(topic, data) ?? 0;
|
|
517
|
-
},
|
|
518
|
-
ws_is_subscribed: (ptr, len) => ws.isSubscribed?.(readStr(ptr, len)) ? 1 : 0
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
function attachWebSocket(ws) {
|
|
522
|
-
const WebSocketPairCtor = globalThis.WebSocketPair;
|
|
523
|
-
const pair = new WebSocketPairCtor;
|
|
524
|
-
const client = pair[0];
|
|
525
|
-
const server = pair[1];
|
|
526
|
-
ws.server = server;
|
|
527
|
-
server.accept();
|
|
528
|
-
for (const data of ws.pendingWrites)
|
|
529
|
-
server.send(data);
|
|
530
|
-
ws.pendingWrites = [];
|
|
531
|
-
server.addEventListener("message", (event) => {
|
|
532
|
-
const data = typeof event.data === "string" ? new TextEncoder().encode(event.data) : new Uint8Array(event.data);
|
|
533
|
-
if (ws.recvResolve) {
|
|
534
|
-
const res = ws.recvResolve;
|
|
535
|
-
ws.recvResolve = null;
|
|
536
|
-
res(data);
|
|
537
|
-
} else {
|
|
538
|
-
ws.messageQueue.push(data);
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
server.addEventListener("close", () => {
|
|
542
|
-
if (ws.recvResolve) {
|
|
543
|
-
const res = ws.recvResolve;
|
|
544
|
-
ws.recvResolve = null;
|
|
545
|
-
res(null);
|
|
546
|
-
}
|
|
547
|
-
});
|
|
548
|
-
return { client };
|
|
549
|
-
}
|
|
550
|
-
function buildSysImports(jspi, Suspending) {
|
|
551
|
-
return {
|
|
552
|
-
sleep_ms: jspi ? new Suspending(async (ms) => new Promise((r) => setTimeout(r, ms))) : (_ms) => {}
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
function executeWasm(instance, jspi, Suspending, wsState) {
|
|
556
|
-
if (!jspi) {
|
|
557
|
-
try {
|
|
558
|
-
instance.exports._start();
|
|
559
|
-
} catch (e) {
|
|
560
|
-
if (!(e instanceof ProcExit))
|
|
561
|
-
throw e;
|
|
562
|
-
}
|
|
563
|
-
return Promise.resolve();
|
|
564
|
-
}
|
|
565
|
-
const start = WebAssembly.promising(instance.exports._start);
|
|
566
|
-
return start().catch((e) => {
|
|
567
|
-
if (e instanceof Error && e.message.startsWith("proc_exit"))
|
|
568
|
-
return;
|
|
569
|
-
throw e;
|
|
570
|
-
}).finally(() => {
|
|
571
|
-
if (wsState.recvResolve) {
|
|
572
|
-
const res = wsState.recvResolve;
|
|
573
|
-
wsState.recvResolve = null;
|
|
574
|
-
res(null);
|
|
575
|
-
}
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
function parseEdgeMeta(stderrText) {
|
|
579
|
-
const meta = { status: 200, headers: new Headers, streaming: false };
|
|
580
|
-
const metaPrefix = "__EDGE_META__:";
|
|
581
|
-
const metaLine = stderrText.split(`
|
|
582
|
-
`).find((line) => line.startsWith(metaPrefix));
|
|
583
|
-
if (metaLine) {
|
|
584
|
-
try {
|
|
585
|
-
const parsed = JSON.parse(metaLine.slice(metaPrefix.length));
|
|
586
|
-
if (parsed.status)
|
|
587
|
-
meta.status = parsed.status;
|
|
588
|
-
if (parsed.streaming === true)
|
|
589
|
-
meta.streaming = true;
|
|
590
|
-
if (Array.isArray(parsed.headers)) {
|
|
591
|
-
for (const [name, value] of parsed.headers) {
|
|
592
|
-
meta.headers.append(name, value);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
} catch {}
|
|
596
|
-
}
|
|
597
|
-
return meta;
|
|
598
|
-
}
|
|
599
|
-
async function run({
|
|
600
|
-
request,
|
|
601
|
-
env,
|
|
602
|
-
ctx,
|
|
603
|
-
module,
|
|
604
|
-
kv: kvBindings,
|
|
605
|
-
imports,
|
|
606
|
-
wasi,
|
|
607
|
-
websocket: doNamespace
|
|
608
|
-
}) {
|
|
609
|
-
if (doNamespace && request.headers.get("upgrade")?.toLowerCase() === "websocket") {
|
|
610
|
-
const id = doNamespace.idFromName(new URL(request.url).pathname);
|
|
611
|
-
return doNamespace.get(id).fetch(request);
|
|
612
|
-
}
|
|
613
|
-
const stdinData = request.body ? new Uint8Array(await request.arrayBuffer()) : undefined;
|
|
614
|
-
const stdoutChunks = [];
|
|
615
|
-
let streamWriter = null;
|
|
616
|
-
const { wasiImport, setMemory, collectOutput } = createWasiImports({
|
|
617
|
-
request,
|
|
618
|
-
stdinData,
|
|
619
|
-
onStdout: (chunk) => {
|
|
620
|
-
if (streamWriter)
|
|
621
|
-
streamWriter.write(chunk);
|
|
622
|
-
else
|
|
623
|
-
stdoutChunks.push(chunk);
|
|
624
|
-
}
|
|
625
|
-
});
|
|
626
|
-
let wasmMemory = null;
|
|
627
|
-
const mem = () => wasmMemory;
|
|
628
|
-
const bridgeRef = { current: null };
|
|
629
|
-
const Suspending = WebAssembly.Suspending;
|
|
630
|
-
const jspi = typeof Suspending === "function";
|
|
631
|
-
const wsState = {
|
|
632
|
-
upgraded: false,
|
|
633
|
-
server: null,
|
|
634
|
-
pendingWrites: [],
|
|
635
|
-
messageQueue: [],
|
|
636
|
-
recvResolve: null
|
|
637
|
-
};
|
|
638
|
-
const instance = new WebAssembly.Instance(module, {
|
|
639
|
-
wasi_snapshot_preview1: { ...wasi?.wasiImport, ...wasiImport },
|
|
640
|
-
__zx_sys: buildSysImports(jspi, Suspending),
|
|
641
|
-
__zx_ws: buildWsImports(jspi ? Suspending : null, mem, new TextDecoder, wsState),
|
|
642
|
-
__zx_kv: createKVImports(kvBindings ?? { default: createMemoryKV() }, mem),
|
|
643
|
-
...imports ? imports(mem) : {},
|
|
644
|
-
...ZxWasiBridge.createImportObject(bridgeRef)
|
|
645
|
-
});
|
|
646
|
-
wasmMemory = instance.exports.memory;
|
|
647
|
-
setMemory(wasmMemory);
|
|
648
|
-
bridgeRef.current = new ZxWasiBridge(instance.exports);
|
|
649
|
-
const wasmPromise = executeWasm(instance, jspi, Suspending, wsState);
|
|
650
|
-
if (wsState.upgraded) {
|
|
651
|
-
const server = attachWebSocket(wsState);
|
|
652
|
-
ctx?.waitUntil(wasmPromise);
|
|
653
|
-
return new Response(null, { status: 101, webSocket: server.client });
|
|
654
|
-
}
|
|
655
|
-
const { stderrText } = collectOutput();
|
|
656
|
-
const meta = parseEdgeMeta(stderrText);
|
|
657
|
-
if (meta.streaming) {
|
|
658
|
-
const { readable, writable } = new TransformStream;
|
|
659
|
-
streamWriter = writable.getWriter();
|
|
660
|
-
for (const chunk of stdoutChunks)
|
|
661
|
-
streamWriter.write(chunk);
|
|
662
|
-
stdoutChunks.length = 0;
|
|
663
|
-
wasmPromise.finally(() => streamWriter?.close());
|
|
664
|
-
return new Response(readable, { status: meta.status, headers: meta.headers });
|
|
665
|
-
}
|
|
666
|
-
await wasmPromise;
|
|
667
|
-
const body = mergeUint8Arrays(stdoutChunks);
|
|
668
|
-
meta.headers.delete("transfer-encoding");
|
|
669
|
-
if (!meta.headers.has("content-length"))
|
|
670
|
-
meta.headers.set("content-length", String(body.byteLength));
|
|
671
|
-
return new Response(body.buffer, { status: meta.status, headers: meta.headers });
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
// src/app.ts
|
|
675
|
-
async function resolveModule(input) {
|
|
676
|
-
if (typeof input === "string") {
|
|
677
|
-
if (input.startsWith("http://") || input.startsWith("https://")) {
|
|
678
|
-
return WebAssembly.compileStreaming(fetch(input));
|
|
679
|
-
}
|
|
680
|
-
const url = input.startsWith("/") ? `file://${input}` : input;
|
|
681
|
-
return WebAssembly.compile(await fetch(url).then((r) => r.arrayBuffer()));
|
|
682
|
-
}
|
|
683
|
-
if (input instanceof URL) {
|
|
684
|
-
return WebAssembly.compileStreaming(fetch(input));
|
|
685
|
-
}
|
|
686
|
-
if (input instanceof Response) {
|
|
687
|
-
return WebAssembly.compileStreaming(input);
|
|
688
|
-
}
|
|
689
|
-
if (input instanceof ArrayBuffer) {
|
|
690
|
-
return WebAssembly.compile(input);
|
|
691
|
-
}
|
|
692
|
-
if (ArrayBuffer.isView(input)) {
|
|
693
|
-
return WebAssembly.compile(input.buffer);
|
|
694
|
-
}
|
|
695
|
-
return input;
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
class Ziex {
|
|
699
|
-
options;
|
|
700
|
-
resolved = null;
|
|
701
|
-
constructor(options) {
|
|
702
|
-
this.options = options;
|
|
703
|
-
}
|
|
704
|
-
async getModule() {
|
|
705
|
-
if (!this.resolved)
|
|
706
|
-
this.resolved = await resolveModule(this.options.module);
|
|
707
|
-
return this.resolved;
|
|
708
|
-
}
|
|
709
|
-
resolveKV(env) {
|
|
710
|
-
const { kv } = this.options;
|
|
711
|
-
if (kv === undefined)
|
|
712
|
-
return;
|
|
713
|
-
if (typeof kv === "object" && kv !== null) {
|
|
714
|
-
const result = {};
|
|
715
|
-
for (const [name, key] of Object.entries(kv)) {
|
|
716
|
-
result[name] = env[key];
|
|
717
|
-
}
|
|
718
|
-
return result;
|
|
719
|
-
}
|
|
720
|
-
return { default: env[kv] };
|
|
721
|
-
}
|
|
722
|
-
fetch = async (request, env, ctx) => {
|
|
723
|
-
const module = await this.getModule();
|
|
724
|
-
const { wasi, imports, websocket } = this.options;
|
|
725
|
-
return run({
|
|
726
|
-
request,
|
|
727
|
-
env,
|
|
728
|
-
ctx,
|
|
729
|
-
module,
|
|
730
|
-
wasi,
|
|
731
|
-
imports,
|
|
732
|
-
kv: this.resolveKV(env),
|
|
733
|
-
websocket: websocket !== undefined ? env[websocket] : undefined
|
|
734
|
-
});
|
|
735
|
-
};
|
|
736
|
-
}
|
|
737
|
-
export {
|
|
738
|
-
Ziex
|
|
739
|
-
};
|