@aresdefencelabs/wasm-http-runtime 0.1.7 → 0.1.9
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.
|
@@ -104,10 +104,6 @@ export function createWasmHttpRuntime(config) {
|
|
|
104
104
|
view.setUint32(start + 4, 0, true);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
function headersToObject(headers) {
|
|
108
|
-
return Object.fromEntries(headers.entries());
|
|
109
|
-
}
|
|
110
|
-
|
|
111
107
|
function utf8ByteLength(value) {
|
|
112
108
|
return encoder.encode(value).length;
|
|
113
109
|
}
|
|
@@ -120,6 +116,35 @@ export function createWasmHttpRuntime(config) {
|
|
|
120
116
|
return out;
|
|
121
117
|
}
|
|
122
118
|
|
|
119
|
+
function searchParamsToObject(searchParams) {
|
|
120
|
+
const out = {};
|
|
121
|
+
for (const [key, value] of searchParams.entries()) {
|
|
122
|
+
out[key] = value;
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function readRequestBodyAsText(request) {
|
|
128
|
+
if (request.method === "GET" || request.method === "HEAD") {
|
|
129
|
+
return "";
|
|
130
|
+
}
|
|
131
|
+
return await request.text();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function buildInboundEnvelope(request) {
|
|
135
|
+
const url = new URL(request.url);
|
|
136
|
+
const bodyText = await readRequestBodyAsText(request);
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
method: request.method,
|
|
140
|
+
url: request.url,
|
|
141
|
+
path: url.pathname,
|
|
142
|
+
body: bodyText,
|
|
143
|
+
headers: normalizeHeaders(request.headers),
|
|
144
|
+
query: searchParamsToObject(url.searchParams),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
123
148
|
function estimateResponseBytes(resp) {
|
|
124
149
|
let total = utf8ByteLength(resp.bodyText);
|
|
125
150
|
for (const [k, v] of Object.entries(resp.headers)) {
|
|
@@ -207,7 +232,6 @@ export function createWasmHttpRuntime(config) {
|
|
|
207
232
|
}
|
|
208
233
|
|
|
209
234
|
const heap = getHeapU8(mod);
|
|
210
|
-
|
|
211
235
|
if (ptr + encoded.length + 1 > heap.length) {
|
|
212
236
|
throw new Error("Allocated CString buffer exceeds wasm memory bounds");
|
|
213
237
|
}
|
|
@@ -274,6 +298,27 @@ export function createWasmHttpRuntime(config) {
|
|
|
274
298
|
});
|
|
275
299
|
}
|
|
276
300
|
|
|
301
|
+
function normalizeResponseEnvelope(responseEnvelope) {
|
|
302
|
+
const status = Number.isInteger(responseEnvelope?.status)
|
|
303
|
+
? responseEnvelope.status
|
|
304
|
+
: 200;
|
|
305
|
+
|
|
306
|
+
const headers =
|
|
307
|
+
responseEnvelope?.headers &&
|
|
308
|
+
typeof responseEnvelope.headers === "object" &&
|
|
309
|
+
!Array.isArray(responseEnvelope.headers)
|
|
310
|
+
? responseEnvelope.headers
|
|
311
|
+
: { "content-type": "application/json; charset=utf-8" };
|
|
312
|
+
|
|
313
|
+
let body = responseEnvelope?.body ?? "";
|
|
314
|
+
|
|
315
|
+
if (typeof body !== "string") {
|
|
316
|
+
body = JSON.stringify(body);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return { status, headers, body };
|
|
320
|
+
}
|
|
321
|
+
|
|
277
322
|
function makeBaseImports() {
|
|
278
323
|
return {
|
|
279
324
|
env: {
|
|
@@ -318,7 +363,6 @@ export function createWasmHttpRuntime(config) {
|
|
|
318
363
|
|
|
319
364
|
instancePromise = (async () => {
|
|
320
365
|
const baseImports = makeBaseImports();
|
|
321
|
-
|
|
322
366
|
let mod = null;
|
|
323
367
|
|
|
324
368
|
mod = await createModule({
|
|
@@ -366,34 +410,25 @@ export function createWasmHttpRuntime(config) {
|
|
|
366
410
|
|
|
367
411
|
try {
|
|
368
412
|
const requestJson = readCStringFromModule(mod, requestJsonCstrPtr);
|
|
369
|
-
log("[AresHost] requestJson =", requestJson);
|
|
370
|
-
|
|
371
413
|
const outbound = JSON.parse(requestJson);
|
|
372
|
-
log("[AresHost] parsed outbound =", outbound);
|
|
373
414
|
|
|
374
415
|
if (typeof state.options.beforeOutboundFetch === "function") {
|
|
375
416
|
state.options.beforeOutboundFetch(outbound);
|
|
376
417
|
}
|
|
377
418
|
|
|
378
|
-
log("[AresHost] before fetch:", outbound.url);
|
|
379
|
-
|
|
380
419
|
const response = await fetch(outbound.url, {
|
|
381
420
|
method: outbound.method ?? "GET",
|
|
382
|
-
headers: outbound.headers,
|
|
421
|
+
headers: outbound.headers ?? {},
|
|
383
422
|
body: outbound.body ?? undefined,
|
|
384
423
|
});
|
|
385
424
|
|
|
386
|
-
log("[AresHost] fetch returned, status =", response.status);
|
|
387
|
-
|
|
388
425
|
const bodyText = await response.text();
|
|
389
|
-
log("[AresHost] response.text resolved, length =", bodyText.length);
|
|
390
426
|
|
|
391
427
|
if (typeof state.options.afterOutboundFetch === "function") {
|
|
392
428
|
state.options.afterOutboundFetch(response, bodyText, outbound);
|
|
393
429
|
}
|
|
394
430
|
|
|
395
431
|
const bodyBytes = utf8ByteLength(bodyText);
|
|
396
|
-
|
|
397
432
|
if (bodyBytes > state.options.maxResponseBodyBytes) {
|
|
398
433
|
throw new Error(
|
|
399
434
|
`HTTP response body too large: ${bodyBytes} bytes exceeds maxResponseBodyBytes=${state.options.maxResponseBodyBytes}`
|
|
@@ -417,8 +452,6 @@ export function createWasmHttpRuntime(config) {
|
|
|
417
452
|
estimatedBytes,
|
|
418
453
|
});
|
|
419
454
|
|
|
420
|
-
log("[AresHost] stored responseId =", responseId);
|
|
421
|
-
|
|
422
455
|
return responseId >>> 0;
|
|
423
456
|
} catch (error) {
|
|
424
457
|
console.error("[AresHost] __aresAbiHttpFetchBlockingAsync failed:", error);
|
|
@@ -495,19 +528,11 @@ export function createWasmHttpRuntime(config) {
|
|
|
495
528
|
throw new Error("WASM runtime method ccall() is not available");
|
|
496
529
|
}
|
|
497
530
|
|
|
498
|
-
const
|
|
499
|
-
const
|
|
531
|
+
const inboundEnvelope = await buildInboundEnvelope(request);
|
|
532
|
+
const requestJson = JSON.stringify(inboundEnvelope);
|
|
500
533
|
|
|
501
|
-
|
|
502
|
-
url: request.url,
|
|
503
|
-
path: url.pathname,
|
|
504
|
-
query: url.search,
|
|
505
|
-
method: request.method,
|
|
506
|
-
headers: headersToObject(request.headers),
|
|
507
|
-
body: bodyText,
|
|
508
|
-
};
|
|
534
|
+
log("[AresHost] inbound envelope =", inboundEnvelope);
|
|
509
535
|
|
|
510
|
-
const requestJson = JSON.stringify(inboundEnvelope);
|
|
511
536
|
const written = writeCString(mod, requestJson);
|
|
512
537
|
requestPtr = written.ptr;
|
|
513
538
|
requestSize = written.size;
|
|
@@ -520,26 +545,22 @@ export function createWasmHttpRuntime(config) {
|
|
|
520
545
|
{ async: true }
|
|
521
546
|
);
|
|
522
547
|
|
|
523
|
-
log("[AresHost] ccall(handle_http) returned:", responsePtr);
|
|
524
|
-
|
|
525
548
|
if (!responsePtr) {
|
|
526
549
|
throw new Error("WASM returned null response pointer");
|
|
527
550
|
}
|
|
528
551
|
|
|
529
552
|
const responseJson = readCStringFromModule(mod, responsePtr);
|
|
530
553
|
const responseEnvelope = JSON.parse(responseJson);
|
|
554
|
+
const normalized = normalizeResponseEnvelope(responseEnvelope);
|
|
531
555
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
},
|
|
541
|
-
}
|
|
542
|
-
);
|
|
556
|
+
log("[AresHost] response envelope =", normalized);
|
|
557
|
+
|
|
558
|
+
return new Response(normalized.body, {
|
|
559
|
+
responseCode: normalized.responseCode,
|
|
560
|
+
errorMessage: normalized.errorMessage,
|
|
561
|
+
isSuccess: normalized.isSuccess,
|
|
562
|
+
headers: normalized.headers,
|
|
563
|
+
});
|
|
543
564
|
} catch (err) {
|
|
544
565
|
if (typeof state.options.onError === "function") {
|
|
545
566
|
try {
|
package/package.json
CHANGED