ziex 0.1.0-dev.1360 → 0.1.0-dev.1377
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/aws-lambda/index.js +20 -29
- package/build.zig +118 -2
- package/build.zig.zon +13 -2
- package/cloudflare/index.js +1768 -1954
- package/index.js +190 -218
- package/{pkg/ziex/src/react → jsx}/dom.d.ts +5 -9
- package/{pkg/ziex/src/react → jsx}/index.d.ts +1 -1
- package/{react → jsx}/index.js +19 -25
- package/{pkg/ziex/src/react → jsx}/types.d.ts +14 -9
- package/package.json +6 -14
- package/vercel/index.js +0 -15
- package/wasm/index.js +259 -239
- package/wasm/init.dev.js +249 -191
- package/wasm/init.js +3 -3
- package/pkg/ziex/src/zx.d.ts +0 -1
- package/vendor/jsz/js/src/index.d.ts +0 -1
- package/vendor/jsz/js/src/zigjs.d.ts +0 -107
- /package/{pkg/ziex/src/app.d.ts → app.d.ts} +0 -0
- /package/{pkg/ziex/src/aws-lambda → aws-lambda}/index.d.ts +0 -0
- /package/{pkg/ziex/src/browser → browser}/kv.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/app.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/db.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/do.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/index.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/kv.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/worker.d.ts +0 -0
- /package/{pkg/ziex/src/db.d.ts → db.d.ts} +0 -0
- /package/{pkg/ziex/src/fetch.d.ts → fetch.d.ts} +0 -0
- /package/{pkg/ziex/src/index.d.ts → index.d.ts} +0 -0
- /package/{pkg/ziex/src/kv.d.ts → kv.d.ts} +0 -0
- /package/{pkg/ziex/src/runtime.d.ts → runtime.d.ts} +0 -0
- /package/{pkg/ziex/src/vercel → vercel}/index.d.ts +0 -0
- /package/{pkg/ziex/src/wasi.d.ts → wasi.d.ts} +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/core.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/dom.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/index.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/init.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/types.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/wasi.d.ts +0 -0
package/index.js
CHANGED
|
@@ -1,36 +1,13 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __returnValue = (v) => v;
|
|
3
|
-
function __exportSetter(name, newValue) {
|
|
4
|
-
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
-
}
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, {
|
|
9
|
-
get: all[name],
|
|
10
|
-
enumerable: true,
|
|
11
|
-
configurable: true,
|
|
12
|
-
set: __exportSetter.bind(all, name)
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// src/runtime.ts
|
|
17
|
-
var exports_runtime = {};
|
|
18
|
-
__export(exports_runtime, {
|
|
19
|
-
run: () => run,
|
|
20
|
-
buildWsImports: () => buildWsImports,
|
|
21
|
-
attachWebSocket: () => attachWebSocket
|
|
22
|
-
});
|
|
23
|
-
|
|
24
1
|
// src/wasm/wasi.ts
|
|
25
|
-
var decoder = new TextDecoder;
|
|
26
|
-
var encoder = new TextEncoder;
|
|
27
|
-
|
|
28
|
-
class ZxWasiBridge {
|
|
2
|
+
var decoder = new TextDecoder();
|
|
3
|
+
var encoder = new TextEncoder();
|
|
4
|
+
var ZxWasiBridge = class {
|
|
29
5
|
#alloc;
|
|
30
6
|
#fetchCompleteHandler;
|
|
31
7
|
#memory;
|
|
32
8
|
#cb;
|
|
33
|
-
#intervals = new Map;
|
|
9
|
+
#intervals = /* @__PURE__ */ new Map();
|
|
10
|
+
// Cached memory view - invalidated when the WASM buffer grows.
|
|
34
11
|
#memView = null;
|
|
35
12
|
#memBuf = null;
|
|
36
13
|
constructor(exports) {
|
|
@@ -74,33 +51,29 @@ class ZxWasiBridge {
|
|
|
74
51
|
const url = this.#readString(urlPtr, urlLen);
|
|
75
52
|
const method = methodLen > 0 ? this.#readString(methodPtr, methodLen) : "GET";
|
|
76
53
|
const headersJson = headersLen > 0 ? this.#readString(headersPtr, headersLen) : "{}";
|
|
77
|
-
const body = bodyLen > 0 ? this.#view().subarray(bodyPtr, bodyPtr + bodyLen) :
|
|
54
|
+
const body = bodyLen > 0 ? this.#view().subarray(bodyPtr, bodyPtr + bodyLen) : void 0;
|
|
78
55
|
let headers = {};
|
|
79
56
|
try {
|
|
80
57
|
headers = JSON.parse(headersJson);
|
|
81
58
|
} catch {
|
|
82
|
-
for (const line of headersJson.split(
|
|
83
|
-
`)) {
|
|
59
|
+
for (const line of headersJson.split("\n")) {
|
|
84
60
|
const i = line.indexOf(":");
|
|
85
|
-
if (i > 0)
|
|
86
|
-
headers[line.slice(0, i)] = line.slice(i + 1);
|
|
61
|
+
if (i > 0) headers[line.slice(0, i)] = line.slice(i + 1);
|
|
87
62
|
}
|
|
88
63
|
}
|
|
89
|
-
const controller = new AbortController;
|
|
64
|
+
const controller = new AbortController();
|
|
90
65
|
const timeout = timeoutMs > 0 ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
91
66
|
fetch(url, {
|
|
92
67
|
method,
|
|
93
|
-
headers: Object.keys(headers).length > 0 ? headers :
|
|
94
|
-
body: method !== "GET" && method !== "HEAD" ? body :
|
|
68
|
+
headers: Object.keys(headers).length > 0 ? headers : void 0,
|
|
69
|
+
body: method !== "GET" && method !== "HEAD" ? body : void 0,
|
|
95
70
|
signal: controller.signal
|
|
96
71
|
}).then(async (res) => {
|
|
97
|
-
if (timeout)
|
|
98
|
-
clearTimeout(timeout);
|
|
72
|
+
if (timeout) clearTimeout(timeout);
|
|
99
73
|
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
100
74
|
this.#notifyFetchComplete(fetchId, res.status, bytes, false);
|
|
101
75
|
}).catch((err) => {
|
|
102
|
-
if (timeout)
|
|
103
|
-
clearTimeout(timeout);
|
|
76
|
+
if (timeout) clearTimeout(timeout);
|
|
104
77
|
const msg = err.name === "AbortError" ? "Request timeout" : err.message ?? "Fetch failed";
|
|
105
78
|
this.#notifyFetchComplete(fetchId, 0, msg, true);
|
|
106
79
|
});
|
|
@@ -120,11 +93,17 @@ class ZxWasiBridge {
|
|
|
120
93
|
}
|
|
121
94
|
clearInterval(callbackId) {
|
|
122
95
|
const handle = this.#intervals.get(callbackId);
|
|
123
|
-
if (handle !==
|
|
96
|
+
if (handle !== void 0) {
|
|
124
97
|
clearInterval(handle);
|
|
125
98
|
this.#intervals.delete(callbackId);
|
|
126
99
|
}
|
|
127
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Create the WASI import object for WASM instantiation.
|
|
103
|
+
*
|
|
104
|
+
* Returns only the `__zx` namespace (log, fetch, timers).
|
|
105
|
+
* Does NOT include jsz.importObject() - the server binary does not use jsz.
|
|
106
|
+
*/
|
|
128
107
|
static createImportObject(bridgeRef) {
|
|
129
108
|
return {
|
|
130
109
|
__zx: {
|
|
@@ -132,7 +111,18 @@ class ZxWasiBridge {
|
|
|
132
111
|
bridgeRef.current?.log(level, ptr, len);
|
|
133
112
|
},
|
|
134
113
|
_fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
|
|
135
|
-
bridgeRef.current?.fetchAsync(
|
|
114
|
+
bridgeRef.current?.fetchAsync(
|
|
115
|
+
urlPtr,
|
|
116
|
+
urlLen,
|
|
117
|
+
methodPtr,
|
|
118
|
+
methodLen,
|
|
119
|
+
headersPtr,
|
|
120
|
+
headersLen,
|
|
121
|
+
bodyPtr,
|
|
122
|
+
bodyLen,
|
|
123
|
+
timeoutMs,
|
|
124
|
+
fetchId
|
|
125
|
+
);
|
|
136
126
|
},
|
|
137
127
|
_setTimeout: (callbackId, delayMs) => {
|
|
138
128
|
bridgeRef.current?.setTimeout(callbackId, delayMs);
|
|
@@ -146,28 +136,48 @@ class ZxWasiBridge {
|
|
|
146
136
|
}
|
|
147
137
|
};
|
|
148
138
|
}
|
|
149
|
-
}
|
|
139
|
+
};
|
|
150
140
|
|
|
151
141
|
// src/kv.ts
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
142
|
+
function putOptions(ttlSeconds) {
|
|
143
|
+
if (ttlSeconds < 60) return void 0;
|
|
144
|
+
return { expirationTtl: ttlSeconds };
|
|
145
|
+
}
|
|
157
146
|
function createMemoryKV() {
|
|
158
|
-
const store = new Map;
|
|
147
|
+
const store = /* @__PURE__ */ new Map();
|
|
148
|
+
function read(key) {
|
|
149
|
+
const entry = store.get(key);
|
|
150
|
+
if (!entry) return null;
|
|
151
|
+
if (entry.expiresAt !== void 0 && Date.now() >= entry.expiresAt) {
|
|
152
|
+
store.delete(key);
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
return entry.value;
|
|
156
|
+
}
|
|
157
|
+
function write(key, value, options) {
|
|
158
|
+
let expiresAt;
|
|
159
|
+
if (options?.expiration !== void 0) {
|
|
160
|
+
expiresAt = options.expiration * 1e3;
|
|
161
|
+
} else if (options?.expirationTtl !== void 0) {
|
|
162
|
+
expiresAt = Date.now() + options.expirationTtl * 1e3;
|
|
163
|
+
}
|
|
164
|
+
store.set(key, { value, expiresAt });
|
|
165
|
+
}
|
|
159
166
|
return {
|
|
160
167
|
async get(key) {
|
|
161
|
-
return
|
|
168
|
+
return read(key);
|
|
162
169
|
},
|
|
163
|
-
async put(key, value) {
|
|
164
|
-
|
|
170
|
+
async put(key, value, options) {
|
|
171
|
+
write(key, value, options);
|
|
165
172
|
},
|
|
166
173
|
async delete(key) {
|
|
167
174
|
store.delete(key);
|
|
168
175
|
},
|
|
169
176
|
async list(options) {
|
|
170
|
-
const keys = [...store.keys()].filter((k) =>
|
|
177
|
+
const keys = [...store.keys()].filter((k) => {
|
|
178
|
+
if (options?.prefix && !k.startsWith(options.prefix)) return false;
|
|
179
|
+
return read(k) !== null;
|
|
180
|
+
}).map((name) => ({ name }));
|
|
171
181
|
return { keys };
|
|
172
182
|
}
|
|
173
183
|
};
|
|
@@ -177,14 +187,13 @@ function isSyncKVNamespace(binding) {
|
|
|
177
187
|
return typeof candidate.getSync === "function" && typeof candidate.putSync === "function" && typeof candidate.deleteSync === "function" && typeof candidate.listSync === "function";
|
|
178
188
|
}
|
|
179
189
|
function createKVImports(bindings, getMemory) {
|
|
180
|
-
const encoder2 = new TextEncoder;
|
|
181
|
-
const decoder2 = new TextDecoder;
|
|
190
|
+
const encoder2 = new TextEncoder();
|
|
191
|
+
const decoder2 = new TextDecoder();
|
|
182
192
|
function readStr(ptr, len) {
|
|
183
193
|
return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
|
|
184
194
|
}
|
|
185
195
|
function writeBytes(buf_ptr, buf_max, data) {
|
|
186
|
-
if (data.length > buf_max)
|
|
187
|
-
return -2;
|
|
196
|
+
if (data.length > buf_max) return -2;
|
|
188
197
|
new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
|
|
189
198
|
return data.length;
|
|
190
199
|
}
|
|
@@ -193,40 +202,36 @@ function createKVImports(bindings, getMemory) {
|
|
|
193
202
|
}
|
|
194
203
|
const Suspending = WebAssembly.Suspending;
|
|
195
204
|
if (typeof Suspending !== "function") {
|
|
196
|
-
let
|
|
205
|
+
let syncBinding2 = function(ns) {
|
|
197
206
|
const candidate = binding(ns);
|
|
198
207
|
return candidate && isSyncKVNamespace(candidate) ? candidate : null;
|
|
199
208
|
};
|
|
209
|
+
var syncBinding = syncBinding2;
|
|
200
210
|
return {
|
|
201
211
|
kv_get: (ns_ptr, ns_len, key_ptr, key_len, buf_ptr, buf_max) => {
|
|
202
|
-
const b =
|
|
203
|
-
if (!b)
|
|
204
|
-
return -1;
|
|
212
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
213
|
+
if (!b) return -1;
|
|
205
214
|
const value = b.getSync(readStr(key_ptr, key_len));
|
|
206
|
-
if (value === null)
|
|
207
|
-
return -1;
|
|
215
|
+
if (value === null) return -1;
|
|
208
216
|
return writeBytes(buf_ptr, buf_max, encoder2.encode(value));
|
|
209
217
|
},
|
|
210
|
-
kv_put: (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len) => {
|
|
211
|
-
const b =
|
|
212
|
-
if (!b)
|
|
213
|
-
|
|
214
|
-
b.putSync(readStr(key_ptr, key_len), readStr(val_ptr, val_len));
|
|
218
|
+
kv_put: (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len, ttl_seconds) => {
|
|
219
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
220
|
+
if (!b) return 0;
|
|
221
|
+
b.putSync(readStr(key_ptr, key_len), readStr(val_ptr, val_len), putOptions(ttl_seconds));
|
|
215
222
|
return 0;
|
|
216
223
|
},
|
|
217
224
|
kv_delete: (ns_ptr, ns_len, key_ptr, key_len) => {
|
|
218
|
-
const b =
|
|
219
|
-
if (!b)
|
|
220
|
-
return 0;
|
|
225
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
226
|
+
if (!b) return 0;
|
|
221
227
|
b.deleteSync(readStr(key_ptr, key_len));
|
|
222
228
|
return 0;
|
|
223
229
|
},
|
|
224
230
|
kv_list: (ns_ptr, ns_len, pfx_ptr, pfx_len, buf_ptr, buf_max) => {
|
|
225
|
-
const b =
|
|
226
|
-
if (!b)
|
|
227
|
-
return writeBytes(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
231
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
232
|
+
if (!b) return writeBytes(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
228
233
|
const prefix = readStr(pfx_ptr, pfx_len);
|
|
229
|
-
const result = b.listSync(prefix.length > 0 ? { prefix } :
|
|
234
|
+
const result = b.listSync(prefix.length > 0 ? { prefix } : void 0);
|
|
230
235
|
return writeBytes(buf_ptr, buf_max, encoder2.encode(JSON.stringify(result.keys.map((k) => k.name))));
|
|
231
236
|
}
|
|
232
237
|
};
|
|
@@ -234,33 +239,28 @@ function createKVImports(bindings, getMemory) {
|
|
|
234
239
|
return {
|
|
235
240
|
kv_get: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, buf_ptr, buf_max) => {
|
|
236
241
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
237
|
-
if (!b)
|
|
238
|
-
return -1;
|
|
242
|
+
if (!b) return -1;
|
|
239
243
|
const value = await b.get(readStr(key_ptr, key_len));
|
|
240
|
-
if (value === null)
|
|
241
|
-
return -1;
|
|
244
|
+
if (value === null) return -1;
|
|
242
245
|
return writeBytes(buf_ptr, buf_max, encoder2.encode(value));
|
|
243
246
|
}),
|
|
244
|
-
kv_put: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len) => {
|
|
247
|
+
kv_put: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len, ttl_seconds) => {
|
|
245
248
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
246
|
-
if (!b)
|
|
247
|
-
|
|
248
|
-
await b.put(readStr(key_ptr, key_len), readStr(val_ptr, val_len));
|
|
249
|
+
if (!b) return -1;
|
|
250
|
+
await b.put(readStr(key_ptr, key_len), readStr(val_ptr, val_len), putOptions(ttl_seconds));
|
|
249
251
|
return 0;
|
|
250
252
|
}),
|
|
251
253
|
kv_delete: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len) => {
|
|
252
254
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
253
|
-
if (!b)
|
|
254
|
-
return -1;
|
|
255
|
+
if (!b) return -1;
|
|
255
256
|
await b.delete(readStr(key_ptr, key_len));
|
|
256
257
|
return 0;
|
|
257
258
|
}),
|
|
258
259
|
kv_list: new Suspending(async (ns_ptr, ns_len, prefix_ptr, prefix_len, buf_ptr, buf_max) => {
|
|
259
260
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
260
|
-
if (!b)
|
|
261
|
-
return writeBytes(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
261
|
+
if (!b) return writeBytes(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
262
262
|
const prefix = readStr(prefix_ptr, prefix_len);
|
|
263
|
-
const result = await b.list(prefix.length > 0 ? { prefix } :
|
|
263
|
+
const result = await b.list(prefix.length > 0 ? { prefix } : void 0);
|
|
264
264
|
return writeBytes(buf_ptr, buf_max, encoder2.encode(JSON.stringify(result.keys.map((k) => k.name))));
|
|
265
265
|
})
|
|
266
266
|
};
|
|
@@ -268,14 +268,13 @@ function createKVImports(bindings, getMemory) {
|
|
|
268
268
|
|
|
269
269
|
// src/fetch.ts
|
|
270
270
|
function createFetchImports(getMemory) {
|
|
271
|
-
const encoder2 = new TextEncoder;
|
|
272
|
-
const decoder2 = new TextDecoder;
|
|
271
|
+
const encoder2 = new TextEncoder();
|
|
272
|
+
const decoder2 = new TextDecoder();
|
|
273
273
|
function readStr(ptr, len) {
|
|
274
274
|
return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
|
|
275
275
|
}
|
|
276
276
|
function writeBytes(buf_ptr, buf_max, data) {
|
|
277
|
-
if (data.length > buf_max)
|
|
278
|
-
return -2;
|
|
277
|
+
if (data.length > buf_max) return -2;
|
|
279
278
|
new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
|
|
280
279
|
return data.length;
|
|
281
280
|
}
|
|
@@ -287,8 +286,7 @@ function createFetchImports(getMemory) {
|
|
|
287
286
|
try {
|
|
288
287
|
headers = JSON.parse(headersJson);
|
|
289
288
|
} catch {
|
|
290
|
-
for (const line of headersJson.split(
|
|
291
|
-
`)) {
|
|
289
|
+
for (const line of headersJson.split("\n")) {
|
|
292
290
|
const colonIdx = line.indexOf(":");
|
|
293
291
|
if (colonIdx > 0) {
|
|
294
292
|
headers[line.slice(0, colonIdx)] = line.slice(colonIdx + 1);
|
|
@@ -301,25 +299,23 @@ function createFetchImports(getMemory) {
|
|
|
301
299
|
const url = readStr(url_ptr, url_len);
|
|
302
300
|
const method = method_len > 0 ? readStr(method_ptr, method_len) : "GET";
|
|
303
301
|
const headersJson = headers_len > 0 ? readStr(headers_ptr, headers_len) : "{}";
|
|
304
|
-
const body = body_len > 0 ? readBytes(body_ptr, body_len) :
|
|
302
|
+
const body = body_len > 0 ? readBytes(body_ptr, body_len) : void 0;
|
|
305
303
|
const headers = parseHeaders(headersJson);
|
|
306
|
-
const controller = new AbortController;
|
|
304
|
+
const controller = new AbortController();
|
|
307
305
|
const timeout = timeout_ms > 0 ? setTimeout(() => controller.abort(), timeout_ms) : null;
|
|
308
306
|
try {
|
|
309
307
|
const response = await fetch(url, {
|
|
310
308
|
method,
|
|
311
|
-
headers: Object.keys(headers).length > 0 ? headers :
|
|
312
|
-
body: method !== "GET" && method !== "HEAD" ? body :
|
|
309
|
+
headers: Object.keys(headers).length > 0 ? headers : void 0,
|
|
310
|
+
body: method !== "GET" && method !== "HEAD" ? body : void 0,
|
|
313
311
|
signal: controller.signal
|
|
314
312
|
});
|
|
315
|
-
if (timeout)
|
|
316
|
-
clearTimeout(timeout);
|
|
313
|
+
if (timeout) clearTimeout(timeout);
|
|
317
314
|
new DataView(getMemory().buffer).setUint16(status_out, response.status, true);
|
|
318
315
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
319
316
|
return writeBytes(buf_ptr, buf_max, bytes);
|
|
320
317
|
} catch (err) {
|
|
321
|
-
if (timeout)
|
|
322
|
-
clearTimeout(timeout);
|
|
318
|
+
if (timeout) clearTimeout(timeout);
|
|
323
319
|
const msg = err instanceof Error ? err.message : String(err);
|
|
324
320
|
console.error(`wasm fetch failed: ${url} (${msg})`);
|
|
325
321
|
return -1;
|
|
@@ -340,8 +336,7 @@ function createFetchImports(getMemory) {
|
|
|
340
336
|
function decodeBlob(base64) {
|
|
341
337
|
const binary = atob(base64);
|
|
342
338
|
const bytes = new Uint8Array(binary.length);
|
|
343
|
-
for (let i = 0;i < binary.length; i++)
|
|
344
|
-
bytes[i] = binary.charCodeAt(i);
|
|
339
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
345
340
|
return bytes;
|
|
346
341
|
}
|
|
347
342
|
function toD1Value(value) {
|
|
@@ -371,27 +366,22 @@ function toPositionalBindings(json) {
|
|
|
371
366
|
}
|
|
372
367
|
}
|
|
373
368
|
function toWireValue(value) {
|
|
374
|
-
if (value === null || value ===
|
|
375
|
-
|
|
376
|
-
if (typeof value === "
|
|
377
|
-
return { kind: "text", text: value };
|
|
378
|
-
if (typeof value === "boolean")
|
|
379
|
-
return { kind: "boolean", boolean: value };
|
|
369
|
+
if (value === null || value === void 0) return { kind: "null" };
|
|
370
|
+
if (typeof value === "string") return { kind: "text", text: value };
|
|
371
|
+
if (typeof value === "boolean") return { kind: "boolean", boolean: value };
|
|
380
372
|
if (typeof value === "number") {
|
|
381
373
|
return Number.isInteger(value) ? { kind: "integer", integer: value } : { kind: "float", float: value };
|
|
382
374
|
}
|
|
383
375
|
if (value instanceof ArrayBuffer) {
|
|
384
376
|
const bytes = new Uint8Array(value);
|
|
385
377
|
let binary = "";
|
|
386
|
-
for (const byte of bytes)
|
|
387
|
-
binary += String.fromCharCode(byte);
|
|
378
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
388
379
|
return { kind: "blob", blob: btoa(binary) };
|
|
389
380
|
}
|
|
390
381
|
if (ArrayBuffer.isView(value)) {
|
|
391
382
|
const bytes = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
392
383
|
let binary = "";
|
|
393
|
-
for (const byte of bytes)
|
|
394
|
-
binary += String.fromCharCode(byte);
|
|
384
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
395
385
|
return { kind: "blob", blob: btoa(binary) };
|
|
396
386
|
}
|
|
397
387
|
return { kind: "text", text: String(value) };
|
|
@@ -408,15 +398,14 @@ function valuesToWireRows(rows) {
|
|
|
408
398
|
return rows.map((row) => row.map((value) => toWireValue(value)));
|
|
409
399
|
}
|
|
410
400
|
function createD1Imports(bindings, getMemory) {
|
|
411
|
-
const encoder2 = new TextEncoder;
|
|
412
|
-
const decoder2 = new TextDecoder;
|
|
401
|
+
const encoder2 = new TextEncoder();
|
|
402
|
+
const decoder2 = new TextDecoder();
|
|
413
403
|
function readStr(ptr, len) {
|
|
414
404
|
return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
|
|
415
405
|
}
|
|
416
406
|
function writeJson(buf_ptr, buf_max, value) {
|
|
417
407
|
const data = encoder2.encode(JSON.stringify(value));
|
|
418
|
-
if (data.length > buf_max)
|
|
419
|
-
return -2;
|
|
408
|
+
if (data.length > buf_max) return -2;
|
|
420
409
|
new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
|
|
421
410
|
return data.length;
|
|
422
411
|
}
|
|
@@ -425,8 +414,7 @@ function createD1Imports(bindings, getMemory) {
|
|
|
425
414
|
}
|
|
426
415
|
async function statement(ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len) {
|
|
427
416
|
const database = binding(readStr(ns_ptr, ns_len));
|
|
428
|
-
if (!database)
|
|
429
|
-
return null;
|
|
417
|
+
if (!database) return null;
|
|
430
418
|
const sql = readStr(sql_ptr, sql_len);
|
|
431
419
|
const bindingsJson = JSON.parse(readStr(bindings_ptr, bindings_len));
|
|
432
420
|
return database.prepare(sql).bind(...toPositionalBindings(bindingsJson));
|
|
@@ -445,8 +433,7 @@ function createD1Imports(bindings, getMemory) {
|
|
|
445
433
|
db_open: (ns_ptr, ns_len) => binding(readStr(ns_ptr, ns_len)) ? 0 : -1,
|
|
446
434
|
db_run: new Suspending(async (ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len, buf_ptr, buf_max) => {
|
|
447
435
|
const stmt = await statement(ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len);
|
|
448
|
-
if (!stmt)
|
|
449
|
-
return -1;
|
|
436
|
+
if (!stmt) return -1;
|
|
450
437
|
const result = await stmt.run();
|
|
451
438
|
return writeJson(buf_ptr, buf_max, {
|
|
452
439
|
last_insert_rowid: result.meta?.last_row_id ?? 0,
|
|
@@ -455,24 +442,20 @@ function createD1Imports(bindings, getMemory) {
|
|
|
455
442
|
}),
|
|
456
443
|
db_get: new Suspending(async (ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len, buf_ptr, buf_max) => {
|
|
457
444
|
const stmt = await statement(ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len);
|
|
458
|
-
if (!stmt)
|
|
459
|
-
return -1;
|
|
445
|
+
if (!stmt) return -1;
|
|
460
446
|
const row = await stmt.first();
|
|
461
|
-
if (!row)
|
|
462
|
-
return 0;
|
|
447
|
+
if (!row) return 0;
|
|
463
448
|
return writeJson(buf_ptr, buf_max, [objectToWireRow(row)]);
|
|
464
449
|
}),
|
|
465
450
|
db_all: new Suspending(async (ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len, buf_ptr, buf_max) => {
|
|
466
451
|
const stmt = await statement(ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len);
|
|
467
|
-
if (!stmt)
|
|
468
|
-
return -1;
|
|
452
|
+
if (!stmt) return -1;
|
|
469
453
|
const result = await stmt.all();
|
|
470
454
|
return writeJson(buf_ptr, buf_max, (result.results ?? []).map(objectToWireRow));
|
|
471
455
|
}),
|
|
472
456
|
db_values: new Suspending(async (ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len, buf_ptr, buf_max) => {
|
|
473
457
|
const stmt = await statement(ns_ptr, ns_len, sql_ptr, sql_len, bindings_ptr, bindings_len);
|
|
474
|
-
if (!stmt)
|
|
475
|
-
return -1;
|
|
458
|
+
if (!stmt) return -1;
|
|
476
459
|
const rows = await stmt.raw();
|
|
477
460
|
return writeJson(buf_ptr, buf_max, valuesToWireRows(rows));
|
|
478
461
|
})
|
|
@@ -480,19 +463,18 @@ function createD1Imports(bindings, getMemory) {
|
|
|
480
463
|
}
|
|
481
464
|
|
|
482
465
|
// src/wasi.ts
|
|
483
|
-
|
|
484
|
-
code;
|
|
466
|
+
var ProcExit = class extends Error {
|
|
485
467
|
constructor(code) {
|
|
486
468
|
super(`proc_exit(${code})`);
|
|
487
469
|
this.code = code;
|
|
488
470
|
}
|
|
489
|
-
}
|
|
471
|
+
};
|
|
490
472
|
function createWasiImports({
|
|
491
473
|
request,
|
|
492
474
|
stdinData,
|
|
493
475
|
onStdout
|
|
494
476
|
}) {
|
|
495
|
-
const encoder2 = new TextEncoder;
|
|
477
|
+
const encoder2 = new TextEncoder();
|
|
496
478
|
const url = new URL(request.url);
|
|
497
479
|
const argStrings = [
|
|
498
480
|
"wasm",
|
|
@@ -506,10 +488,9 @@ function createWasiImports({
|
|
|
506
488
|
url.href
|
|
507
489
|
];
|
|
508
490
|
request.headers.forEach((value, name) => {
|
|
509
|
-
if (value)
|
|
510
|
-
argStrings.push("--header", `${name}:${value}`);
|
|
491
|
+
if (value) argStrings.push("--header", `${name}:${value}`);
|
|
511
492
|
});
|
|
512
|
-
const encodedArgs = argStrings.map((a) => encoder2.encode(a + "\
|
|
493
|
+
const encodedArgs = argStrings.map((a) => encoder2.encode(a + "\0"));
|
|
513
494
|
const argBufSize = encodedArgs.reduce((s, a) => s + a.length, 0);
|
|
514
495
|
let wasmMemory = null;
|
|
515
496
|
const setMemory = (m2) => {
|
|
@@ -521,13 +502,11 @@ function createWasiImports({
|
|
|
521
502
|
const stderrDecoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
522
503
|
function processStderrChunk(chunk) {
|
|
523
504
|
const text = stderrDecoder.decode(chunk, { stream: true });
|
|
524
|
-
const lines = (stderrPartial + text).split(
|
|
525
|
-
`);
|
|
505
|
+
const lines = (stderrPartial + text).split("\n");
|
|
526
506
|
stderrPartial = lines.pop() ?? "";
|
|
527
507
|
for (const line of lines) {
|
|
528
|
-
if (line.startsWith("
|
|
529
|
-
stderrMeta += line +
|
|
530
|
-
`;
|
|
508
|
+
if (line.startsWith("__ZIEX_META__:")) {
|
|
509
|
+
stderrMeta += line + "\n";
|
|
531
510
|
} else if (line.length > 0) {
|
|
532
511
|
console.error("[ziex]", line);
|
|
533
512
|
}
|
|
@@ -570,17 +549,14 @@ function createWasiImports({
|
|
|
570
549
|
const dv = v();
|
|
571
550
|
const mem = m();
|
|
572
551
|
let written = 0;
|
|
573
|
-
for (let i = 0;i < iovs_len; i++) {
|
|
552
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
574
553
|
const buf_ptr = dv.getUint32(iovs_ptr + i * 8, true);
|
|
575
554
|
const buf_len = dv.getUint32(iovs_ptr + i * 8 + 4, true);
|
|
576
555
|
const chunk = mem.slice(buf_ptr, buf_ptr + buf_len);
|
|
577
556
|
if (fd === 1) {
|
|
578
|
-
if (onStdout)
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
stdoutChunks.push(chunk);
|
|
582
|
-
} else if (fd === 2)
|
|
583
|
-
processStderrChunk(chunk);
|
|
557
|
+
if (onStdout) onStdout(chunk);
|
|
558
|
+
else stdoutChunks.push(chunk);
|
|
559
|
+
} else if (fd === 2) processStderrChunk(chunk);
|
|
584
560
|
written += buf_len;
|
|
585
561
|
}
|
|
586
562
|
dv.setUint32(nwritten_ptr, written, true);
|
|
@@ -591,7 +567,7 @@ function createWasiImports({
|
|
|
591
567
|
const mem = m();
|
|
592
568
|
const stdin = stdinData ?? new Uint8Array(0);
|
|
593
569
|
let totalRead = 0;
|
|
594
|
-
for (let i = 0;i < iovs_len; i++) {
|
|
570
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
595
571
|
const buf_ptr = dv.getUint32(iovs_ptr + i * 8, true);
|
|
596
572
|
const buf_len = dv.getUint32(iovs_ptr + i * 8 + 4, true);
|
|
597
573
|
if (fd === 0 && stdinOffset < stdin.length) {
|
|
@@ -748,19 +724,18 @@ function createWasiImports({
|
|
|
748
724
|
v().setUint32(bufused_ptr, 0, true);
|
|
749
725
|
return 76;
|
|
750
726
|
},
|
|
727
|
+
// Stub for poll_oneoff - no blocking I/O in edge runtimes.
|
|
751
728
|
poll_oneoff(_in, _out, _nsubscriptions, nevents_ptr) {
|
|
752
729
|
v().setUint32(nevents_ptr, 0, true);
|
|
753
730
|
return 0;
|
|
754
731
|
}
|
|
755
732
|
};
|
|
756
733
|
function collectOutput() {
|
|
757
|
-
const remaining = stderrDecoder.decode(
|
|
734
|
+
const remaining = stderrDecoder.decode(void 0, { stream: false });
|
|
758
735
|
const tail = stderrPartial + remaining;
|
|
759
736
|
if (tail.length > 0) {
|
|
760
|
-
if (tail.startsWith("
|
|
761
|
-
|
|
762
|
-
else
|
|
763
|
-
console.error("[ziex]", tail);
|
|
737
|
+
if (tail.startsWith("__ZIEX_META__:")) stderrMeta += tail;
|
|
738
|
+
else console.error("[ziex]", tail);
|
|
764
739
|
stderrPartial = "";
|
|
765
740
|
}
|
|
766
741
|
return {
|
|
@@ -802,22 +777,21 @@ function buildWsImports(Suspending, mem, decoder2, ws) {
|
|
|
802
777
|
ws_recv: Suspending ? new Suspending(async (buf_ptr, buf_max) => {
|
|
803
778
|
if (ws._resolveFirstSuspend) {
|
|
804
779
|
const fn = ws._resolveFirstSuspend;
|
|
805
|
-
ws._resolveFirstSuspend =
|
|
780
|
+
ws._resolveFirstSuspend = void 0;
|
|
806
781
|
fn();
|
|
807
782
|
}
|
|
808
783
|
const deliver = (bytes) => {
|
|
809
|
-
if (bytes === null)
|
|
810
|
-
return -1;
|
|
784
|
+
if (bytes === null) return -1;
|
|
811
785
|
const n = Math.min(bytes.length, buf_max);
|
|
812
786
|
new Uint8Array(mem().buffer, buf_ptr, n).set(bytes.subarray(0, n));
|
|
813
787
|
return n;
|
|
814
788
|
};
|
|
815
|
-
if (ws.messageQueue.length > 0)
|
|
816
|
-
return deliver(ws.messageQueue.shift());
|
|
789
|
+
if (ws.messageQueue.length > 0) return deliver(ws.messageQueue.shift());
|
|
817
790
|
return new Promise((resolve) => {
|
|
818
791
|
ws.recvResolve = (bytes) => resolve(deliver(bytes));
|
|
819
792
|
});
|
|
820
793
|
}) : (_buf_ptr, _buf_max) => -1,
|
|
794
|
+
// Pub/sub - delegates to optional callbacks (real in DO, no-ops otherwise)
|
|
821
795
|
ws_subscribe: (ptr, len) => {
|
|
822
796
|
ws.subscribe?.(readStr(ptr, len));
|
|
823
797
|
},
|
|
@@ -834,13 +808,12 @@ function buildWsImports(Suspending, mem, decoder2, ws) {
|
|
|
834
808
|
}
|
|
835
809
|
function attachWebSocket(ws) {
|
|
836
810
|
const WebSocketPairCtor = globalThis.WebSocketPair;
|
|
837
|
-
const pair = new WebSocketPairCtor;
|
|
811
|
+
const pair = new WebSocketPairCtor();
|
|
838
812
|
const client = pair[0];
|
|
839
813
|
const server = pair[1];
|
|
840
814
|
ws.server = server;
|
|
841
815
|
server.accept();
|
|
842
|
-
for (const data of ws.pendingWrites)
|
|
843
|
-
server.send(data);
|
|
816
|
+
for (const data of ws.pendingWrites) server.send(data);
|
|
844
817
|
ws.pendingWrites = [];
|
|
845
818
|
server.addEventListener("message", (event) => {
|
|
846
819
|
const data = typeof event.data === "string" ? new TextEncoder().encode(event.data) : new Uint8Array(event.data);
|
|
@@ -863,7 +836,8 @@ function attachWebSocket(ws) {
|
|
|
863
836
|
}
|
|
864
837
|
function buildSysImports(jspi, Suspending) {
|
|
865
838
|
return {
|
|
866
|
-
sleep_ms: jspi ? new Suspending(async (ms) => new Promise((r) => setTimeout(r, ms))) : (_ms) => {
|
|
839
|
+
sleep_ms: jspi ? new Suspending(async (ms) => new Promise((r) => setTimeout(r, ms))) : (_ms) => {
|
|
840
|
+
}
|
|
867
841
|
};
|
|
868
842
|
}
|
|
869
843
|
function executeWasm(instance, jspi, Suspending, wsState) {
|
|
@@ -871,15 +845,13 @@ function executeWasm(instance, jspi, Suspending, wsState) {
|
|
|
871
845
|
try {
|
|
872
846
|
instance.exports._start();
|
|
873
847
|
} catch (e) {
|
|
874
|
-
if (!(e instanceof ProcExit))
|
|
875
|
-
throw e;
|
|
848
|
+
if (!(e instanceof ProcExit)) throw e;
|
|
876
849
|
}
|
|
877
850
|
return Promise.resolve();
|
|
878
851
|
}
|
|
879
852
|
const start = WebAssembly.promising(instance.exports._start);
|
|
880
853
|
return start().catch((e) => {
|
|
881
|
-
if (e instanceof Error && e.message.startsWith("proc_exit"))
|
|
882
|
-
return;
|
|
854
|
+
if (e instanceof Error && e.message.startsWith("proc_exit")) return;
|
|
883
855
|
throw e;
|
|
884
856
|
}).finally(() => {
|
|
885
857
|
if (wsState.recvResolve) {
|
|
@@ -889,24 +861,22 @@ function executeWasm(instance, jspi, Suspending, wsState) {
|
|
|
889
861
|
}
|
|
890
862
|
});
|
|
891
863
|
}
|
|
892
|
-
function
|
|
893
|
-
const meta = { status: 200, headers: new Headers, streaming: false };
|
|
894
|
-
const metaPrefix = "
|
|
895
|
-
const metaLine = stderrText.split(
|
|
896
|
-
`).find((line) => line.startsWith(metaPrefix));
|
|
864
|
+
function parseZiexMeta(stderrText) {
|
|
865
|
+
const meta = { status: 200, headers: new Headers(), streaming: false };
|
|
866
|
+
const metaPrefix = "__ZIEX_META__:";
|
|
867
|
+
const metaLine = stderrText.split("\n").find((line) => line.startsWith(metaPrefix));
|
|
897
868
|
if (metaLine) {
|
|
898
869
|
try {
|
|
899
870
|
const parsed = JSON.parse(metaLine.slice(metaPrefix.length));
|
|
900
|
-
if (parsed.status)
|
|
901
|
-
|
|
902
|
-
if (parsed.streaming === true)
|
|
903
|
-
meta.streaming = true;
|
|
871
|
+
if (parsed.status) meta.status = parsed.status;
|
|
872
|
+
if (parsed.streaming === true) meta.streaming = true;
|
|
904
873
|
if (Array.isArray(parsed.headers)) {
|
|
905
874
|
for (const [name, value] of parsed.headers) {
|
|
906
875
|
meta.headers.append(name, value);
|
|
907
876
|
}
|
|
908
877
|
}
|
|
909
|
-
} catch {
|
|
878
|
+
} catch {
|
|
879
|
+
}
|
|
910
880
|
}
|
|
911
881
|
return meta;
|
|
912
882
|
}
|
|
@@ -925,17 +895,15 @@ async function run({
|
|
|
925
895
|
const id = doNamespace.idFromName(new URL(request.url).pathname);
|
|
926
896
|
return doNamespace.get(id).fetch(request);
|
|
927
897
|
}
|
|
928
|
-
const stdinData = request.body ? new Uint8Array(await request.arrayBuffer()) :
|
|
898
|
+
const stdinData = request.body ? new Uint8Array(await request.arrayBuffer()) : void 0;
|
|
929
899
|
const stdoutChunks = [];
|
|
930
900
|
let streamWriter = null;
|
|
931
901
|
const { wasiImport, setMemory, collectOutput } = createWasiImports({
|
|
932
902
|
request,
|
|
933
903
|
stdinData,
|
|
934
904
|
onStdout: (chunk) => {
|
|
935
|
-
if (streamWriter)
|
|
936
|
-
|
|
937
|
-
else
|
|
938
|
-
stdoutChunks.push(chunk);
|
|
905
|
+
if (streamWriter) void streamWriter.write(chunk);
|
|
906
|
+
else stdoutChunks.push(chunk);
|
|
939
907
|
}
|
|
940
908
|
});
|
|
941
909
|
let wasmMemory = null;
|
|
@@ -953,7 +921,7 @@ async function run({
|
|
|
953
921
|
const instance = new WebAssembly.Instance(module, {
|
|
954
922
|
wasi_snapshot_preview1: { ...wasi?.wasiImport, ...wasiImport },
|
|
955
923
|
__zx_sys: buildSysImports(jspi, Suspending),
|
|
956
|
-
__zx_ws: buildWsImports(jspi ? Suspending : null, mem, new TextDecoder, wsState),
|
|
924
|
+
__zx_ws: buildWsImports(jspi ? Suspending : null, mem, new TextDecoder(), wsState),
|
|
957
925
|
__zx_kv: createKVImports(kvBindings ?? { default: createMemoryKV() }, mem),
|
|
958
926
|
__zx_db: createD1Imports(dbBindings ?? {}, mem),
|
|
959
927
|
__zx_net: createFetchImports(mem),
|
|
@@ -970,23 +938,26 @@ async function run({
|
|
|
970
938
|
return new Response(null, { status: 101, webSocket: server.client });
|
|
971
939
|
}
|
|
972
940
|
const { stderrText: earlyStderrText } = collectOutput();
|
|
973
|
-
const earlyMeta =
|
|
941
|
+
const earlyMeta = parseZiexMeta(earlyStderrText);
|
|
974
942
|
if (earlyMeta.streaming) {
|
|
975
|
-
const { readable, writable } = new TransformStream;
|
|
943
|
+
const { readable, writable } = new TransformStream();
|
|
976
944
|
streamWriter = writable.getWriter();
|
|
977
|
-
for (const chunk of stdoutChunks)
|
|
978
|
-
streamWriter.write(chunk);
|
|
945
|
+
for (const chunk of stdoutChunks) void streamWriter.write(chunk);
|
|
979
946
|
stdoutChunks.length = 0;
|
|
980
|
-
wasmPromise.finally(() => streamWriter?.close());
|
|
947
|
+
void wasmPromise.finally(() => streamWriter?.close());
|
|
981
948
|
return new Response(readable, { status: earlyMeta.status, headers: earlyMeta.headers });
|
|
982
949
|
}
|
|
983
950
|
await wasmPromise;
|
|
984
951
|
const { stderrText } = collectOutput();
|
|
985
|
-
const meta =
|
|
952
|
+
const meta = parseZiexMeta(stderrText);
|
|
986
953
|
const body = mergeUint8Arrays(stdoutChunks);
|
|
987
954
|
meta.headers.delete("transfer-encoding");
|
|
988
|
-
|
|
989
|
-
|
|
955
|
+
const nullBody = meta.status === 101 || meta.status === 204 || meta.status === 205 || meta.status === 304;
|
|
956
|
+
if (nullBody) {
|
|
957
|
+
meta.headers.delete("content-length");
|
|
958
|
+
return new Response(null, { status: meta.status, headers: meta.headers });
|
|
959
|
+
}
|
|
960
|
+
if (!meta.headers.has("content-length")) meta.headers.set("content-length", String(body.byteLength));
|
|
990
961
|
return new Response(body.buffer, { status: meta.status, headers: meta.headers });
|
|
991
962
|
}
|
|
992
963
|
|
|
@@ -1013,22 +984,39 @@ async function resolveModule(input) {
|
|
|
1013
984
|
}
|
|
1014
985
|
return input;
|
|
1015
986
|
}
|
|
1016
|
-
|
|
1017
|
-
class Ziex {
|
|
1018
|
-
options;
|
|
1019
|
-
resolved = null;
|
|
987
|
+
var Ziex = class {
|
|
1020
988
|
constructor(options) {
|
|
989
|
+
this.resolved = null;
|
|
990
|
+
/**
|
|
991
|
+
* Fetch handler called by the runtime on every request.
|
|
992
|
+
*
|
|
993
|
+
* Arrow function so `this` is always the class instance, even when the
|
|
994
|
+
* runtime extracts `fetch` from the exported object (e.g. Bun).
|
|
995
|
+
*/
|
|
996
|
+
this.fetch = async (request, env, ctx) => {
|
|
997
|
+
const module = await this.getModule();
|
|
998
|
+
const { wasi, imports, websocket } = this.options;
|
|
999
|
+
return run({
|
|
1000
|
+
request,
|
|
1001
|
+
env,
|
|
1002
|
+
ctx,
|
|
1003
|
+
module,
|
|
1004
|
+
wasi,
|
|
1005
|
+
imports,
|
|
1006
|
+
kv: this.resolveKV(env),
|
|
1007
|
+
db: this.resolveDB(env),
|
|
1008
|
+
websocket: websocket !== void 0 ? env[websocket] : void 0
|
|
1009
|
+
});
|
|
1010
|
+
};
|
|
1021
1011
|
this.options = options;
|
|
1022
1012
|
}
|
|
1023
1013
|
async getModule() {
|
|
1024
|
-
if (!this.resolved)
|
|
1025
|
-
this.resolved = await resolveModule(this.options.module);
|
|
1014
|
+
if (!this.resolved) this.resolved = await resolveModule(this.options.module);
|
|
1026
1015
|
return this.resolved;
|
|
1027
1016
|
}
|
|
1028
1017
|
resolveKV(env) {
|
|
1029
1018
|
const { kv } = this.options;
|
|
1030
|
-
if (kv ===
|
|
1031
|
-
return;
|
|
1019
|
+
if (kv === void 0) return void 0;
|
|
1032
1020
|
if (typeof kv === "object" && kv !== null) {
|
|
1033
1021
|
const result = {};
|
|
1034
1022
|
for (const [name, key] of Object.entries(kv)) {
|
|
@@ -1040,8 +1028,7 @@ class Ziex {
|
|
|
1040
1028
|
}
|
|
1041
1029
|
resolveDB(env) {
|
|
1042
1030
|
const { db } = this.options;
|
|
1043
|
-
if (db ===
|
|
1044
|
-
return;
|
|
1031
|
+
if (db === void 0) return void 0;
|
|
1045
1032
|
if (typeof db === "object" && db !== null) {
|
|
1046
1033
|
const result = {};
|
|
1047
1034
|
for (const [name, key] of Object.entries(db)) {
|
|
@@ -1051,22 +1038,7 @@ class Ziex {
|
|
|
1051
1038
|
}
|
|
1052
1039
|
return { default: env[db] };
|
|
1053
1040
|
}
|
|
1054
|
-
|
|
1055
|
-
const module = await this.getModule();
|
|
1056
|
-
const { wasi, imports, websocket } = this.options;
|
|
1057
|
-
return run({
|
|
1058
|
-
request,
|
|
1059
|
-
env,
|
|
1060
|
-
ctx,
|
|
1061
|
-
module,
|
|
1062
|
-
wasi,
|
|
1063
|
-
imports,
|
|
1064
|
-
kv: this.resolveKV(env),
|
|
1065
|
-
db: this.resolveDB(env),
|
|
1066
|
-
websocket: websocket !== undefined ? env[websocket] : undefined
|
|
1067
|
-
});
|
|
1068
|
-
};
|
|
1069
|
-
}
|
|
1041
|
+
};
|
|
1070
1042
|
export {
|
|
1071
1043
|
Ziex
|
|
1072
1044
|
};
|