@ricsam/quickjs-test-utils 1.0.18 → 1.0.19

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.
@@ -34,6 +34,7 @@ __export(exports_src, {
34
34
  useFetchTestContext: () => import_fetch_context.useFetchTestContext,
35
35
  typecheckQuickJSCode: () => import_typecheck.typecheckQuickJSCode,
36
36
  startIntegrationServer: () => import_integration_server.startIntegrationServer,
37
+ runTestCode: () => import_native_input_test.runTestCode,
37
38
  formatTypecheckErrors: () => import_typecheck.formatTypecheckErrors,
38
39
  evalCodeAsync: () => import_eval.evalCodeAsync,
39
40
  evalCode: () => import_eval.evalCode,
@@ -50,9 +51,10 @@ module.exports = __toCommonJS(exports_src);
50
51
  var import_context = require("./context.cjs");
51
52
  var import_fetch_context = require("./fetch-context.cjs");
52
53
  var import_eval = require("./eval.cjs");
54
+ var import_native_input_test = require("./native-input-test.cjs");
53
55
  var import_integration_server = require("./integration-server.cjs");
54
56
  var import_typecheck = require("./typecheck.cjs");
55
57
  var import_quickjs_types = require("./quickjs-types.cjs");
56
58
  })
57
59
 
58
- //# debugId=58D73AEF6D5595F264756E2164756E21
60
+ //# debugId=2D2B04FA8DA88D0664756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": [
5
- "export {\n createTestContext,\n disposeTestContext,\n useTestContext,\n type TestContext,\n} from \"./context.cjs\";\n\nexport {\n createFetchTestContext,\n disposeFetchTestContext,\n useFetchTestContext,\n type FetchTestContext,\n type CreateFetchTestContextOptions,\n} from \"./fetch-context.cjs\";\n\nexport { evalCode, evalCodeAsync } from \"./eval.cjs\";\n\nexport {\n startIntegrationServer,\n type IntegrationTestServer,\n type StartIntegrationServerOptions,\n} from \"./integration-server.cjs\";\n\nexport {\n typecheckQuickJSCode,\n formatTypecheckErrors,\n type TypecheckResult,\n type TypecheckError,\n type TypecheckOptions,\n type LibraryTypes,\n type LibraryTypeFile,\n} from \"./typecheck.cjs\";\n\nexport {\n CORE_TYPES,\n FETCH_TYPES,\n FS_TYPES,\n TYPE_DEFINITIONS,\n type TypeDefinitionKey,\n} from \"./quickjs-types.cjs\";\n\n// For fs and runtime contexts, import from subpaths:\n// import { createFsTestContext } from \"@ricsam/quickjs-test-utils/fs\";\n// import { createRuntimeTestContext } from \"@ricsam/quickjs-test-utils/runtime\";\n"
5
+ "export {\n createTestContext,\n disposeTestContext,\n useTestContext,\n type TestContext,\n} from \"./context.cjs\";\n\nexport {\n createFetchTestContext,\n disposeFetchTestContext,\n useFetchTestContext,\n type FetchTestContext,\n type CreateFetchTestContextOptions,\n} from \"./fetch-context.cjs\";\n\nexport { evalCode, evalCodeAsync } from \"./eval.cjs\";\n\nexport {\n runTestCode,\n type TestRuntime,\n type TestRunner,\n} from \"./native-input-test.cjs\";\n\nexport {\n startIntegrationServer,\n type IntegrationTestServer,\n type StartIntegrationServerOptions,\n} from \"./integration-server.cjs\";\n\nexport {\n typecheckQuickJSCode,\n formatTypecheckErrors,\n type TypecheckResult,\n type TypecheckError,\n type TypecheckOptions,\n type LibraryTypes,\n type LibraryTypeFile,\n} from \"./typecheck.cjs\";\n\nexport {\n CORE_TYPES,\n FETCH_TYPES,\n FS_TYPES,\n TYPE_DEFINITIONS,\n type TypeDefinitionKey,\n} from \"./quickjs-types.cjs\";\n\n// For fs and runtime contexts, import from subpaths:\n// import { createFsTestContext } from \"@ricsam/quickjs-test-utils/fs\";\n// import { createRuntimeTestContext } from \"@ricsam/quickjs-test-utils/runtime\";\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKO,IALP;AAaO,IANP;AAQwC,IAAxC;AAMO,IAJP;AAcO,IARP;AAgBO,IANP;",
8
- "debugId": "58D73AEF6D5595F264756E2164756E21",
7
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKO,IALP;AAaO,IANP;AAQwC,IAAxC;AAMO,IAJP;AAUO,IAJP;AAcO,IARP;AAgBO,IANP;",
8
+ "debugId": "2D2B04FA8DA88D0664756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -0,0 +1,565 @@
1
+ // @bun @bun-cjs
2
+ (function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __moduleCache = /* @__PURE__ */ new WeakMap;
7
+ var __toCommonJS = (from) => {
8
+ var entry = __moduleCache.get(from), desc;
9
+ if (entry)
10
+ return entry;
11
+ entry = __defProp({}, "__esModule", { value: true });
12
+ if (from && typeof from === "object" || typeof from === "function")
13
+ __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
14
+ get: () => from[key],
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ }));
17
+ __moduleCache.set(from, entry);
18
+ return entry;
19
+ };
20
+ var __export = (target, all) => {
21
+ for (var name in all)
22
+ __defProp(target, name, {
23
+ get: all[name],
24
+ enumerable: true,
25
+ configurable: true,
26
+ set: (newValue) => all[name] = () => newValue
27
+ });
28
+ };
29
+
30
+ // packages/test-utils/src/native-input-test.ts
31
+ var exports_native_input_test = {};
32
+ __export(exports_native_input_test, {
33
+ runTestCode: () => runTestCode
34
+ });
35
+ module.exports = __toCommonJS(exports_native_input_test);
36
+ var import_quickjs_core = require("@ricsam/quickjs-core");
37
+ function runTestCode(context, code) {
38
+ return {
39
+ input(inputs) {
40
+ const logs = {};
41
+ const logFn = context.newFunction("log", (tagHandle, valueHandle) => {
42
+ const tag = context.getString(tagHandle);
43
+ const value = unmarshalWithClassConversion(context, valueHandle);
44
+ logs[tag] = value;
45
+ });
46
+ context.setProp(context.global, "log", logFn);
47
+ logFn.dispose();
48
+ const inputHandle = marshalWithClassConversion(context, inputs);
49
+ context.setProp(context.global, "testingInput", inputHandle);
50
+ inputHandle.dispose();
51
+ const result = context.evalCode(code);
52
+ let returnValue = undefined;
53
+ if (result.error) {
54
+ const error = context.dump(result.error);
55
+ result.error.dispose();
56
+ context.setProp(context.global, "testingInput", context.undefined);
57
+ context.setProp(context.global, "log", context.undefined);
58
+ throw new Error(`Test code failed: ${JSON.stringify(error)}`);
59
+ } else {
60
+ returnValue = context.dump(result.value);
61
+ result.value.dispose();
62
+ }
63
+ context.setProp(context.global, "testingInput", context.undefined);
64
+ context.setProp(context.global, "log", context.undefined);
65
+ return { logs, result: returnValue };
66
+ }
67
+ };
68
+ }
69
+ function marshalWithClassConversion(context, value) {
70
+ if (value instanceof Headers) {
71
+ return createQJSHeadersFromNative(context, value);
72
+ }
73
+ if (value instanceof Request) {
74
+ return createQJSRequestFromNative(context, value);
75
+ }
76
+ if (value instanceof Response) {
77
+ return createQJSResponseFromNative(context, value);
78
+ }
79
+ if (value instanceof URL) {
80
+ return createQJSURLFromNative(context, value);
81
+ }
82
+ if (value instanceof Blob && !(value instanceof File)) {
83
+ return createQJSBlobFromNative(context, value);
84
+ }
85
+ if (value instanceof File) {
86
+ return createQJSFileFromNative(context, value);
87
+ }
88
+ if (value instanceof FormData) {
89
+ return createQJSFormDataFromNative(context, value);
90
+ }
91
+ if (Array.isArray(value)) {
92
+ const arr = context.newArray();
93
+ for (let i = 0;i < value.length; i++) {
94
+ const itemHandle = marshalWithClassConversion(context, value[i]);
95
+ context.setProp(arr, i, itemHandle);
96
+ itemHandle.dispose();
97
+ }
98
+ return arr;
99
+ }
100
+ if (value && typeof value === "object" && value.constructor === Object) {
101
+ const obj = context.newObject();
102
+ for (const [key, val] of Object.entries(value)) {
103
+ const valHandle = marshalWithClassConversion(context, val);
104
+ context.setProp(obj, key, valHandle);
105
+ valHandle.dispose();
106
+ }
107
+ return obj;
108
+ }
109
+ return import_quickjs_core.marshal(context, value);
110
+ }
111
+ function createQJSHeadersFromNative(context, native) {
112
+ const pairs = [];
113
+ native.forEach((value, key) => {
114
+ pairs.push([key, value]);
115
+ });
116
+ const pairsJson = JSON.stringify(pairs);
117
+ const result = context.evalCode(`new Headers(${pairsJson})`);
118
+ if (result.error) {
119
+ const err = context.dump(result.error);
120
+ result.error.dispose();
121
+ throw new Error(`Failed to create Headers from native: ${JSON.stringify(err)}`);
122
+ }
123
+ return result.value;
124
+ }
125
+ function createQJSRequestFromNative(context, native) {
126
+ const headersHandle = createQJSHeadersFromNative(context, native.headers);
127
+ context.setProp(context.global, "__nativeRequestHeaders__", headersHandle);
128
+ headersHandle.dispose();
129
+ const urlJson = JSON.stringify(native.url);
130
+ const methodJson = JSON.stringify(native.method);
131
+ const modeJson = JSON.stringify(native.mode);
132
+ const credentialsJson = JSON.stringify(native.credentials);
133
+ const cacheJson = JSON.stringify(native.cache);
134
+ const redirectJson = JSON.stringify(native.redirect);
135
+ const referrerJson = JSON.stringify(native.referrer);
136
+ const referrerPolicyJson = JSON.stringify(native.referrerPolicy);
137
+ const integrityJson = JSON.stringify(native.integrity);
138
+ const result = context.evalCode(`
139
+ (function() {
140
+ const req = new Request(${urlJson}, {
141
+ method: ${methodJson},
142
+ headers: __nativeRequestHeaders__,
143
+ mode: ${modeJson},
144
+ credentials: ${credentialsJson},
145
+ cache: ${cacheJson},
146
+ redirect: ${redirectJson},
147
+ referrer: ${referrerJson},
148
+ referrerPolicy: ${referrerPolicyJson},
149
+ integrity: ${integrityJson},
150
+ });
151
+ delete globalThis.__nativeRequestHeaders__;
152
+ return req;
153
+ })()
154
+ `);
155
+ if (result.error) {
156
+ const err = context.dump(result.error);
157
+ result.error.dispose();
158
+ throw new Error(`Failed to create Request from native: ${JSON.stringify(err)}`);
159
+ }
160
+ return result.value;
161
+ }
162
+ function createQJSResponseFromNative(context, native) {
163
+ const headersHandle = createQJSHeadersFromNative(context, native.headers);
164
+ context.setProp(context.global, "__nativeResponseHeaders__", headersHandle);
165
+ headersHandle.dispose();
166
+ const statusJson = JSON.stringify(native.status);
167
+ const statusTextJson = JSON.stringify(native.statusText);
168
+ const result = context.evalCode(`
169
+ (function() {
170
+ const res = new Response(null, {
171
+ status: ${statusJson},
172
+ statusText: ${statusTextJson},
173
+ headers: __nativeResponseHeaders__,
174
+ });
175
+ delete globalThis.__nativeResponseHeaders__;
176
+ return res;
177
+ })()
178
+ `);
179
+ if (result.error) {
180
+ const err = context.dump(result.error);
181
+ result.error.dispose();
182
+ throw new Error(`Failed to create Response from native: ${JSON.stringify(err)}`);
183
+ }
184
+ return result.value;
185
+ }
186
+ function createQJSURLFromNative(context, native) {
187
+ const hrefJson = JSON.stringify(native.href);
188
+ const result = context.evalCode(`new URL(${hrefJson})`);
189
+ if (result.error) {
190
+ const err = context.dump(result.error);
191
+ result.error.dispose();
192
+ throw new Error(`Failed to create URL from native: ${JSON.stringify(err)}`);
193
+ }
194
+ return result.value;
195
+ }
196
+ function createQJSBlobFromNative(context, native) {
197
+ const typeJson = JSON.stringify(native.type);
198
+ const result = context.evalCode(`new Blob([], { type: ${typeJson} })`);
199
+ if (result.error) {
200
+ const err = context.dump(result.error);
201
+ result.error.dispose();
202
+ throw new Error(`Failed to create Blob from native: ${JSON.stringify(err)}`);
203
+ }
204
+ return result.value;
205
+ }
206
+ function createQJSFileFromNative(context, native) {
207
+ const nameJson = JSON.stringify(native.name);
208
+ const typeJson = JSON.stringify(native.type);
209
+ const lastModifiedJson = JSON.stringify(native.lastModified);
210
+ const result = context.evalCode(`
211
+ new File([], ${nameJson}, { type: ${typeJson}, lastModified: ${lastModifiedJson} })
212
+ `);
213
+ if (result.error) {
214
+ const err = context.dump(result.error);
215
+ result.error.dispose();
216
+ throw new Error(`Failed to create File from native: ${JSON.stringify(err)}`);
217
+ }
218
+ return result.value;
219
+ }
220
+ function createQJSFormDataFromNative(context, native) {
221
+ const result = context.evalCode(`new FormData()`);
222
+ if (result.error) {
223
+ const err = context.dump(result.error);
224
+ result.error.dispose();
225
+ throw new Error(`Failed to create FormData from native: ${JSON.stringify(err)}`);
226
+ }
227
+ const formDataHandle = result.value;
228
+ for (const [key, entryValue] of native.entries()) {
229
+ const keyJson = JSON.stringify(key);
230
+ if (typeof entryValue !== "string") {
231
+ const fileValue = entryValue;
232
+ const nameJson = JSON.stringify(fileValue.name);
233
+ const typeJson = JSON.stringify(fileValue.type);
234
+ const appendResult = context.evalCode(`
235
+ (function(fd) {
236
+ const file = new File([], ${nameJson}, { type: ${typeJson} });
237
+ fd.append(${keyJson}, file);
238
+ return fd;
239
+ })
240
+ `);
241
+ if (appendResult.error) {
242
+ appendResult.error.dispose();
243
+ formDataHandle.dispose();
244
+ throw new Error("Failed to create append function for File");
245
+ }
246
+ const appendFn = appendResult.value;
247
+ const callResult = context.callFunction(appendFn, context.undefined, formDataHandle);
248
+ appendFn.dispose();
249
+ if (callResult.error) {
250
+ callResult.error.dispose();
251
+ formDataHandle.dispose();
252
+ throw new Error("Failed to append File to FormData");
253
+ }
254
+ callResult.value.dispose();
255
+ } else {
256
+ const valueJson = JSON.stringify(entryValue);
257
+ const appendResult = context.evalCode(`
258
+ (function(fd) {
259
+ fd.append(${keyJson}, ${valueJson});
260
+ return fd;
261
+ })
262
+ `);
263
+ if (appendResult.error) {
264
+ appendResult.error.dispose();
265
+ formDataHandle.dispose();
266
+ throw new Error("Failed to create append function");
267
+ }
268
+ const appendFn = appendResult.value;
269
+ const callResult = context.callFunction(appendFn, context.undefined, formDataHandle);
270
+ appendFn.dispose();
271
+ if (callResult.error) {
272
+ callResult.error.dispose();
273
+ formDataHandle.dispose();
274
+ throw new Error("Failed to append to FormData");
275
+ }
276
+ callResult.value.dispose();
277
+ }
278
+ }
279
+ return formDataHandle;
280
+ }
281
+ function unmarshalWithClassConversion(context, handle) {
282
+ if (isInstanceOf(context, handle, "Headers")) {
283
+ return createNativeHeadersFromQJS(context, handle);
284
+ }
285
+ if (isInstanceOf(context, handle, "Request")) {
286
+ return createNativeRequestFromQJS(context, handle);
287
+ }
288
+ if (isInstanceOf(context, handle, "Response")) {
289
+ return createNativeResponseFromQJS(context, handle);
290
+ }
291
+ if (isInstanceOf(context, handle, "URL")) {
292
+ return createNativeURLFromQJS(context, handle);
293
+ }
294
+ if (isInstanceOf(context, handle, "File")) {
295
+ return createNativeFileFromQJS(context, handle);
296
+ }
297
+ if (isInstanceOf(context, handle, "Blob")) {
298
+ return createNativeBlobFromQJS(context, handle);
299
+ }
300
+ if (isInstanceOf(context, handle, "FormData")) {
301
+ return createNativeFormDataFromQJS(context, handle);
302
+ }
303
+ const typeofResult = context.typeof(handle);
304
+ if (typeofResult === "object") {
305
+ const isArrayResult = context.evalCode(`(function(obj) { return Array.isArray(obj); })`);
306
+ if (!isArrayResult.error) {
307
+ const checkResult = context.callFunction(isArrayResult.value, context.undefined, handle);
308
+ isArrayResult.value.dispose();
309
+ if (!checkResult.error && context.dump(checkResult.value) === true) {
310
+ checkResult.value.dispose();
311
+ return unmarshalArray(context, handle);
312
+ }
313
+ if (checkResult.error)
314
+ checkResult.error.dispose();
315
+ else if (checkResult.value)
316
+ checkResult.value.dispose();
317
+ } else {
318
+ isArrayResult.error.dispose();
319
+ }
320
+ const isPlainObjectResult = context.evalCode(`(function(obj) {
321
+ return obj !== null && typeof obj === 'object' && obj.constructor === Object;
322
+ })`);
323
+ if (!isPlainObjectResult.error) {
324
+ const checkResult = context.callFunction(isPlainObjectResult.value, context.undefined, handle);
325
+ isPlainObjectResult.value.dispose();
326
+ if (!checkResult.error && context.dump(checkResult.value) === true) {
327
+ checkResult.value.dispose();
328
+ return unmarshalObject(context, handle);
329
+ }
330
+ if (checkResult.error)
331
+ checkResult.error.dispose();
332
+ else if (checkResult.value)
333
+ checkResult.value.dispose();
334
+ } else {
335
+ isPlainObjectResult.error.dispose();
336
+ }
337
+ }
338
+ return context.dump(handle);
339
+ }
340
+ function isInstanceOf(context, handle, className) {
341
+ const checkResult = context.evalCode(`(function(obj) { return obj instanceof ${className}; })`);
342
+ if (checkResult.error) {
343
+ checkResult.error.dispose();
344
+ return false;
345
+ }
346
+ const result = context.callFunction(checkResult.value, context.undefined, handle);
347
+ checkResult.value.dispose();
348
+ if (result.error) {
349
+ result.error.dispose();
350
+ return false;
351
+ }
352
+ const isInstance = context.dump(result.value) === true;
353
+ result.value.dispose();
354
+ return isInstance;
355
+ }
356
+ function unmarshalArray(context, handle) {
357
+ const lengthHandle = context.getProp(handle, "length");
358
+ const length = context.dump(lengthHandle);
359
+ lengthHandle.dispose();
360
+ const result = [];
361
+ for (let i = 0;i < length; i++) {
362
+ const itemHandle = context.getProp(handle, i);
363
+ result.push(unmarshalWithClassConversion(context, itemHandle));
364
+ itemHandle.dispose();
365
+ }
366
+ return result;
367
+ }
368
+ function unmarshalObject(context, handle) {
369
+ const keysResult = context.evalCode(`(function(obj) { return Object.keys(obj); })`);
370
+ if (keysResult.error) {
371
+ keysResult.error.dispose();
372
+ return context.dump(handle);
373
+ }
374
+ const keysCallResult = context.callFunction(keysResult.value, context.undefined, handle);
375
+ keysResult.value.dispose();
376
+ if (keysCallResult.error) {
377
+ keysCallResult.error.dispose();
378
+ return context.dump(handle);
379
+ }
380
+ const keys = context.dump(keysCallResult.value);
381
+ keysCallResult.value.dispose();
382
+ const result = {};
383
+ for (const key of keys) {
384
+ const propHandle = context.getProp(handle, key);
385
+ result[key] = unmarshalWithClassConversion(context, propHandle);
386
+ propHandle.dispose();
387
+ }
388
+ return result;
389
+ }
390
+ function createNativeHeadersFromQJS(context, qjsHandle) {
391
+ const extractResult = context.evalCode(`
392
+ (function(headers) {
393
+ const pairs = [];
394
+ for (const [key, value] of headers) {
395
+ pairs.push([key, value]);
396
+ }
397
+ return pairs;
398
+ })
399
+ `);
400
+ if (extractResult.error) {
401
+ extractResult.error.dispose();
402
+ throw new Error("Failed to create extract function for Headers");
403
+ }
404
+ const result = context.callFunction(extractResult.value, context.undefined, qjsHandle);
405
+ extractResult.value.dispose();
406
+ if (result.error) {
407
+ result.error.dispose();
408
+ throw new Error("Failed to call header extraction function");
409
+ }
410
+ const pairs = context.dump(result.value);
411
+ result.value.dispose();
412
+ const native = new Headers;
413
+ for (const [key, value] of pairs) {
414
+ native.append(key, value);
415
+ }
416
+ return native;
417
+ }
418
+ function createNativeRequestFromQJS(context, qjsHandle) {
419
+ const extractResult = context.evalCode(`
420
+ (function(req) {
421
+ const headers = [];
422
+ for (const [key, value] of req.headers) {
423
+ headers.push([key, value]);
424
+ }
425
+ return {
426
+ url: req.url,
427
+ method: req.method,
428
+ headers: headers,
429
+ mode: req.mode,
430
+ credentials: req.credentials,
431
+ cache: req.cache,
432
+ redirect: req.redirect,
433
+ referrer: req.referrer,
434
+ referrerPolicy: req.referrerPolicy,
435
+ integrity: req.integrity,
436
+ };
437
+ })
438
+ `);
439
+ if (extractResult.error) {
440
+ extractResult.error.dispose();
441
+ throw new Error("Failed to create extract function for Request");
442
+ }
443
+ const result = context.callFunction(extractResult.value, context.undefined, qjsHandle);
444
+ extractResult.value.dispose();
445
+ if (result.error) {
446
+ result.error.dispose();
447
+ throw new Error("Failed to call request extraction function");
448
+ }
449
+ const props = context.dump(result.value);
450
+ result.value.dispose();
451
+ const headers = new Headers;
452
+ for (const [key, value] of props.headers) {
453
+ headers.append(key, value);
454
+ }
455
+ return new Request(props.url, {
456
+ method: props.method,
457
+ headers,
458
+ mode: props.mode,
459
+ credentials: props.credentials,
460
+ cache: props.cache,
461
+ redirect: props.redirect,
462
+ referrer: props.referrer,
463
+ referrerPolicy: props.referrerPolicy,
464
+ integrity: props.integrity
465
+ });
466
+ }
467
+ function createNativeResponseFromQJS(context, qjsHandle) {
468
+ const extractResult = context.evalCode(`
469
+ (function(res) {
470
+ const headers = [];
471
+ for (const [key, value] of res.headers) {
472
+ headers.push([key, value]);
473
+ }
474
+ return {
475
+ status: res.status,
476
+ statusText: res.statusText,
477
+ headers: headers,
478
+ };
479
+ })
480
+ `);
481
+ if (extractResult.error) {
482
+ extractResult.error.dispose();
483
+ throw new Error("Failed to create extract function for Response");
484
+ }
485
+ const result = context.callFunction(extractResult.value, context.undefined, qjsHandle);
486
+ extractResult.value.dispose();
487
+ if (result.error) {
488
+ result.error.dispose();
489
+ throw new Error("Failed to call response extraction function");
490
+ }
491
+ const props = context.dump(result.value);
492
+ result.value.dispose();
493
+ const headers = new Headers;
494
+ for (const [key, value] of props.headers) {
495
+ headers.append(key, value);
496
+ }
497
+ return new Response(null, {
498
+ status: props.status,
499
+ statusText: props.statusText,
500
+ headers
501
+ });
502
+ }
503
+ function createNativeURLFromQJS(context, qjsHandle) {
504
+ const hrefHandle = context.getProp(qjsHandle, "href");
505
+ const href = context.dump(hrefHandle);
506
+ hrefHandle.dispose();
507
+ return new URL(href);
508
+ }
509
+ function createNativeBlobFromQJS(context, qjsHandle) {
510
+ const typeHandle = context.getProp(qjsHandle, "type");
511
+ const type = context.dump(typeHandle);
512
+ typeHandle.dispose();
513
+ return new Blob([], { type });
514
+ }
515
+ function createNativeFileFromQJS(context, qjsHandle) {
516
+ const nameHandle = context.getProp(qjsHandle, "name");
517
+ const name = context.dump(nameHandle);
518
+ nameHandle.dispose();
519
+ const typeHandle = context.getProp(qjsHandle, "type");
520
+ const type = context.dump(typeHandle);
521
+ typeHandle.dispose();
522
+ const lastModifiedHandle = context.getProp(qjsHandle, "lastModified");
523
+ const lastModified = context.dump(lastModifiedHandle);
524
+ lastModifiedHandle.dispose();
525
+ return new File([], name, { type, lastModified });
526
+ }
527
+ function createNativeFormDataFromQJS(context, qjsHandle) {
528
+ const extractResult = context.evalCode(`
529
+ (function(fd) {
530
+ const entries = [];
531
+ for (const [key, value] of fd) {
532
+ if (value instanceof File) {
533
+ entries.push([key, { type: 'file', name: value.name, fileType: value.type, lastModified: value.lastModified }]);
534
+ } else {
535
+ entries.push([key, { type: 'string', value: value }]);
536
+ }
537
+ }
538
+ return entries;
539
+ })
540
+ `);
541
+ if (extractResult.error) {
542
+ extractResult.error.dispose();
543
+ throw new Error("Failed to create extract function for FormData");
544
+ }
545
+ const result = context.callFunction(extractResult.value, context.undefined, qjsHandle);
546
+ extractResult.value.dispose();
547
+ if (result.error) {
548
+ result.error.dispose();
549
+ throw new Error("Failed to call formdata extraction function");
550
+ }
551
+ const entries = context.dump(result.value);
552
+ result.value.dispose();
553
+ const formData = new FormData;
554
+ for (const [key, entry] of entries) {
555
+ if (entry.type === "file") {
556
+ formData.append(key, new File([], entry.name, { type: entry.fileType, lastModified: entry.lastModified }));
557
+ } else {
558
+ formData.append(key, entry.value);
559
+ }
560
+ }
561
+ return formData;
562
+ }
563
+ })
564
+
565
+ //# debugId=A14BDC5D5B1C027164756E2164756E21