socket-function 0.65.0 → 0.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/require/RequireController.ts +8 -2
- package/require/require.ts +147 -41
- package/src/callHTTPHandler.ts +5 -0
package/package.json
CHANGED
|
@@ -347,8 +347,14 @@ class RequireControllerBase {
|
|
|
347
347
|
}]),
|
|
348
348
|
};
|
|
349
349
|
let key = sha256Hash(JSON.stringify(simplifiedResult));
|
|
350
|
-
let
|
|
351
|
-
|
|
350
|
+
let uncompressedBuffer = Buffer.from(JSON.stringify(result));
|
|
351
|
+
let buffer = await compressCached(key, () => uncompressedBuffer);
|
|
352
|
+
setHTTPResultHeaders(buffer, {
|
|
353
|
+
"Content-Type": "application/json",
|
|
354
|
+
"Content-Encoding": "gzip",
|
|
355
|
+
"Content-Length": buffer.length.toString(),
|
|
356
|
+
"X-Uncompressed-Content-Length": uncompressedBuffer.length.toString(),
|
|
357
|
+
});
|
|
352
358
|
return buffer as any;
|
|
353
359
|
}
|
|
354
360
|
|
package/require/require.ts
CHANGED
|
@@ -1,11 +1,86 @@
|
|
|
1
1
|
import { GetModulesArgs, SerializedModule } from "./RequireController";
|
|
2
2
|
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
var onProgressHandler: undefined | ((progress: {
|
|
6
|
+
type: string;
|
|
7
|
+
addValue: number;
|
|
8
|
+
addMax: number;
|
|
9
|
+
}) => void);
|
|
10
|
+
var onErrorHandler: undefined | ((error: string) => void);
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var BOOT_TIME: number;
|
|
14
|
+
var builtInModuleExports: {
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
3
19
|
export function requireMain() {
|
|
4
|
-
|
|
20
|
+
async function requestText(endpoint: string, values: any) {
|
|
21
|
+
let url = new URL(endpoint);
|
|
22
|
+
|
|
23
|
+
let json = JSON.stringify(values);
|
|
24
|
+
let response: Response;
|
|
25
|
+
if (json.length < 6000) {
|
|
26
|
+
// NOTE: Try to use a GET, as GETs can be cached! However, if the data is too large,
|
|
27
|
+
// we have to use a post, or else the request url will be too large
|
|
28
|
+
for (let key in values) {
|
|
29
|
+
url.searchParams.set(key, JSON.stringify(values[key]));
|
|
30
|
+
}
|
|
31
|
+
response = await fetch(url.toString(), {
|
|
32
|
+
method: "GET",
|
|
33
|
+
credentials: "include",
|
|
34
|
+
});
|
|
35
|
+
} else {
|
|
36
|
+
response = await fetch(url.toString(), {
|
|
37
|
+
method: "POST",
|
|
38
|
+
body: json,
|
|
39
|
+
credentials: "include",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let compressionRatio = 1;
|
|
44
|
+
let uncompressedLength = response.headers.get("X-Uncompressed-Content-Length");
|
|
45
|
+
let compressedLength = response.headers.get("Content-Length");
|
|
46
|
+
if (uncompressedLength && compressedLength) {
|
|
47
|
+
compressionRatio = +uncompressedLength / +compressedLength;
|
|
48
|
+
}
|
|
49
|
+
let totalLength = +(uncompressedLength || compressedLength || 0);
|
|
50
|
+
if (totalLength) {
|
|
51
|
+
globalThis.onProgressHandler?.({
|
|
52
|
+
type: "Download",
|
|
53
|
+
addValue: 0,
|
|
54
|
+
addMax: Math.floor(totalLength / compressionRatio),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// If it's an error, set requestError
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
globalThis.onErrorHandler?.(response.statusText);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Stream it, so we can get progrss
|
|
64
|
+
let reader = response.body!.getReader();
|
|
65
|
+
let result = "";
|
|
66
|
+
while (true) {
|
|
67
|
+
let { done, value } = await reader.read();
|
|
68
|
+
if (done) break;
|
|
69
|
+
result += new TextDecoder().decode(value);
|
|
70
|
+
let cur = (value?.length || 0) / compressionRatio;
|
|
71
|
+
globalThis.onProgressHandler?.({
|
|
72
|
+
type: "Download",
|
|
73
|
+
addValue: cur,
|
|
74
|
+
addMax: 0,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
5
80
|
|
|
6
81
|
const g = globalThis as any;
|
|
7
82
|
let startTime = Date.now();
|
|
8
|
-
|
|
83
|
+
globalThis.BOOT_TIME = startTime;
|
|
9
84
|
|
|
10
85
|
(Symbol as any).dispose = Symbol.dispose || Symbol("dispose");
|
|
11
86
|
(Symbol as any).asyncDispose = Symbol.asyncDispose || Symbol("asyncDispose");
|
|
@@ -54,7 +129,8 @@ export function requireMain() {
|
|
|
54
129
|
child_process: {},
|
|
55
130
|
events: {},
|
|
56
131
|
};
|
|
57
|
-
|
|
132
|
+
globalThis.builtInModuleExports = globalThis.builtInModuleExports || {};
|
|
133
|
+
Object.assign(globalThis.builtInModuleExports, builtInModuleExports);
|
|
58
134
|
|
|
59
135
|
let lastTime = 0;
|
|
60
136
|
function nextTime() {
|
|
@@ -286,6 +362,12 @@ export function requireMain() {
|
|
|
286
362
|
rootResolveCache[requests[i]] = requestsResolvedPaths[i];
|
|
287
363
|
}
|
|
288
364
|
|
|
365
|
+
globalThis.onProgressHandler?.({
|
|
366
|
+
type: "Compile",
|
|
367
|
+
addValue: 0,
|
|
368
|
+
addMax: Object.keys(modules).length,
|
|
369
|
+
});
|
|
370
|
+
|
|
289
371
|
// Store the function, so we only call it if it exists BEFORE we import
|
|
290
372
|
// (which means we already have something loading, so this is likely hot reloading...)
|
|
291
373
|
let observerOnHotReload = g.observerOnHotReload;
|
|
@@ -300,13 +382,13 @@ export function requireMain() {
|
|
|
300
382
|
let requireModuleCount = Object.values(modules).filter((x) => !x.source).length;
|
|
301
383
|
let dependenciesOnlyText = requireModuleCount ? ` (+${requireModuleCount} dependencies only)` : "";
|
|
302
384
|
console.log(
|
|
303
|
-
`%cimport(${requests.join(", ")}) finished download ${time}ms, ${Math.ceil(
|
|
385
|
+
`%cimport(${requests.join(", ")}) finished download ${time.toFixed(0)}ms, ${Math.ceil(
|
|
304
386
|
rawText.length / 1024
|
|
305
|
-
)}KB, ${moduleCount} modules${dependenciesOnlyText} at ${Date.now() - startTime}ms`,
|
|
306
|
-
"color:
|
|
387
|
+
)}KB, ${moduleCount} modules${dependenciesOnlyText} at ${(Date.now() - startTime).toFixed(0)}ms`,
|
|
388
|
+
"color: lightgreen"
|
|
307
389
|
);
|
|
308
390
|
|
|
309
|
-
|
|
391
|
+
|
|
310
392
|
|
|
311
393
|
if (alreadyHave?.requireSeqNumProcessId !== requireSeqNumProcessId) {
|
|
312
394
|
alreadyHave = undefined;
|
|
@@ -319,13 +401,37 @@ export function requireMain() {
|
|
|
319
401
|
serializedModules[id] = module;
|
|
320
402
|
}
|
|
321
403
|
|
|
404
|
+
time = Date.now();
|
|
405
|
+
let lastWaitTime = time;
|
|
406
|
+
for (let key of Object.keys(modules)) {
|
|
407
|
+
getModule(key, "preload");
|
|
408
|
+
// Wait, so we can render progress (and in generals, so the UI remains somewhat responsive)
|
|
409
|
+
if (Date.now() - lastWaitTime > 10) {
|
|
410
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
411
|
+
lastWaitTime = Date.now();
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
time = Date.now() - time;
|
|
416
|
+
console.log(
|
|
417
|
+
`%cimport(${requests.join(", ")}) finished compile ${time.toFixed(0)}ms (${moduleCount} modules) at ${(Date.now() - startTime).toFixed(0)}ms`,
|
|
418
|
+
"color: hotpink"
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
globalThis.onProgressHandler?.({
|
|
423
|
+
type: "Evaluate",
|
|
424
|
+
addValue: 0,
|
|
425
|
+
addMax: Object.keys(modules).length,
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
time = Date.now();
|
|
322
429
|
try {
|
|
323
430
|
return requestsResolvedPaths.map((x) => getModule(x));
|
|
324
431
|
} finally {
|
|
325
432
|
time = Date.now() - time;
|
|
326
433
|
console.log(
|
|
327
|
-
`%cimport(${requests.join(", ")}) finished evaluate ${time}ms (${moduleCount} modules) at ${Date.now() - startTime
|
|
328
|
-
}ms`,
|
|
434
|
+
`%cimport(${requests.join(", ")}) finished evaluate ${time.toFixed(0)}ms (${moduleCount} modules) at ${(Date.now() - startTime).toFixed(0)}ms`,
|
|
329
435
|
"color: lightblue"
|
|
330
436
|
);
|
|
331
437
|
}
|
|
@@ -392,7 +498,6 @@ export function requireMain() {
|
|
|
392
498
|
"color: red",
|
|
393
499
|
"color: unset"
|
|
394
500
|
);
|
|
395
|
-
debugger;
|
|
396
501
|
}
|
|
397
502
|
return rootRequire(resolvedPath);
|
|
398
503
|
}
|
|
@@ -495,12 +600,17 @@ export function requireMain() {
|
|
|
495
600
|
|
|
496
601
|
let currentModuleEvaluationStack: string[] = [];
|
|
497
602
|
// See https://nodejs.org/api/modules.html
|
|
498
|
-
function getModule(resolvedId: string) {
|
|
603
|
+
function getModule(resolvedId: string, preload?: "preload") {
|
|
499
604
|
if (resolvedId === "") {
|
|
500
605
|
return {};
|
|
501
606
|
}
|
|
502
607
|
if (resolvedId in moduleCache) {
|
|
503
|
-
|
|
608
|
+
let module = moduleCache[resolvedId];
|
|
609
|
+
if (!preload && !module.loaded) {
|
|
610
|
+
module.loaded = true;
|
|
611
|
+
module.load();
|
|
612
|
+
}
|
|
613
|
+
return module;
|
|
504
614
|
}
|
|
505
615
|
|
|
506
616
|
let serializedModule = serializedModules[resolvedId];
|
|
@@ -513,13 +623,25 @@ export function requireMain() {
|
|
|
513
623
|
module.exports.default = module.exports;
|
|
514
624
|
module.children = [];
|
|
515
625
|
for (let key in serializedModule.flags || {}) {
|
|
626
|
+
if (key === "loaded") continue;
|
|
516
627
|
module[key] = true;
|
|
517
628
|
}
|
|
518
629
|
|
|
519
630
|
module.load = load;
|
|
520
631
|
|
|
521
|
-
|
|
522
|
-
module.
|
|
632
|
+
let originalSource = serializedModule.source;
|
|
633
|
+
let moduleFnc = wrapSafe(module.id, originalSource);
|
|
634
|
+
|
|
635
|
+
globalThis.onProgressHandler?.({
|
|
636
|
+
type: "Compile",
|
|
637
|
+
addValue: 1,
|
|
638
|
+
addMax: 0,
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
if (!preload) {
|
|
642
|
+
module.loaded = true;
|
|
643
|
+
module.load();
|
|
644
|
+
}
|
|
523
645
|
|
|
524
646
|
function load() {
|
|
525
647
|
let serializedModule = serializedModules[resolvedId];
|
|
@@ -560,7 +682,10 @@ export function requireMain() {
|
|
|
560
682
|
module.size = source.length;
|
|
561
683
|
module.source = source;
|
|
562
684
|
|
|
563
|
-
|
|
685
|
+
if (source !== originalSource) {
|
|
686
|
+
originalSource = source;
|
|
687
|
+
moduleFnc = wrapSafe(module.id, originalSource);
|
|
688
|
+
}
|
|
564
689
|
|
|
565
690
|
let dirname = module.filename.replace(/\\/g, "/").split("/").slice(0, -1).join("/");
|
|
566
691
|
|
|
@@ -597,34 +722,15 @@ export function requireMain() {
|
|
|
597
722
|
} finally {
|
|
598
723
|
module.isPreloading = false;
|
|
599
724
|
currentModuleEvaluationStack.pop();
|
|
725
|
+
|
|
726
|
+
globalThis.onProgressHandler?.({
|
|
727
|
+
type: "Evaluate",
|
|
728
|
+
addValue: 1,
|
|
729
|
+
addMax: 0,
|
|
730
|
+
});
|
|
600
731
|
}
|
|
601
732
|
}
|
|
602
733
|
|
|
603
734
|
return module;
|
|
604
735
|
}
|
|
605
|
-
|
|
606
|
-
async function requestText(endpoint: string, values: any) {
|
|
607
|
-
let url = new URL(endpoint);
|
|
608
|
-
|
|
609
|
-
let json = JSON.stringify(values);
|
|
610
|
-
if (json.length < 6000) {
|
|
611
|
-
// NOTE: Try to use a GET, as GETs can be cached! However, if the data is too large,
|
|
612
|
-
// we have to use a post, or else the request url will be too large
|
|
613
|
-
for (let key in values) {
|
|
614
|
-
url.searchParams.set(key, JSON.stringify(values[key]));
|
|
615
|
-
}
|
|
616
|
-
let response = await fetch(url.toString(), {
|
|
617
|
-
method: "GET",
|
|
618
|
-
credentials: "include",
|
|
619
|
-
});
|
|
620
|
-
return await response.text();
|
|
621
|
-
} else {
|
|
622
|
-
let response = await fetch(url.toString(), {
|
|
623
|
-
method: "POST",
|
|
624
|
-
body: json,
|
|
625
|
-
credentials: "include",
|
|
626
|
-
});
|
|
627
|
-
return await response.text();
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
}
|
|
736
|
+
}
|
package/src/callHTTPHandler.ts
CHANGED
|
@@ -189,6 +189,7 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
|
|
|
189
189
|
return;
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
|
+
let uncompressedLength = resultBuffer.length;
|
|
192
193
|
if (SocketFunction.HTTP_COMPRESS && request.headers["accept-encoding"]?.includes("gzip") && !headers?.["Content-Encoding"]) {
|
|
193
194
|
// NOTE: This is a BIT slow. To speed it up, functions can use an internal cache, according to their function,
|
|
194
195
|
// and return a Buffer (which they can as any cast to make the returned type allowed, as returned Buffers will
|
|
@@ -205,6 +206,10 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
|
|
|
205
206
|
});
|
|
206
207
|
});
|
|
207
208
|
}
|
|
209
|
+
response.setHeader("Content-Length", resultBuffer.length.toString());
|
|
210
|
+
if (!response.getHeader("X-Uncompressed-Content-Length")) {
|
|
211
|
+
response.setHeader("X-Uncompressed-Content-Length", uncompressedLength.toString());
|
|
212
|
+
}
|
|
208
213
|
response.write(resultBuffer);
|
|
209
214
|
if (SocketFunction.logMessages) {
|
|
210
215
|
console.log(`HTTP response ${formatNumberSuffixed(resultBuffer.length)}B (${request.method}) ${url}`);
|