socket-function 0.97.0 → 0.99.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 +14 -2
- package/src/misc.ts +39 -1
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import zlib from "zlib";
|
|
|
8
8
|
import { cacheLimited, lazy } from "../src/caching";
|
|
9
9
|
import { formatNumber } from "../src/formatting/format";
|
|
10
10
|
import { requireMain } from "./require";
|
|
11
|
+
import path from "path";
|
|
11
12
|
|
|
12
13
|
const COMPRESS_CACHE_SIZE = 1024 * 1024 * 128;
|
|
13
14
|
|
|
@@ -136,6 +137,11 @@ class RequireControllerBase {
|
|
|
136
137
|
requireCalls?: string[];
|
|
137
138
|
cacheTime?: number;
|
|
138
139
|
}) {
|
|
140
|
+
if (!this.rootResolvePath) {
|
|
141
|
+
let dir = path.resolve(".");
|
|
142
|
+
dir = dir.replaceAll("\\", "/");
|
|
143
|
+
this.rootResolvePath = dir;
|
|
144
|
+
}
|
|
139
145
|
let { requireCalls, cacheTime } = config || {};
|
|
140
146
|
let result = resolvedHTMLFile();
|
|
141
147
|
if (beforeEntryText.length > 0) {
|
|
@@ -405,8 +411,14 @@ declare global {
|
|
|
405
411
|
}
|
|
406
412
|
|
|
407
413
|
let baseController = new RequireControllerBase();
|
|
408
|
-
|
|
409
|
-
|
|
414
|
+
/** @deprecated, not needed, as this defaults to ".", which is a lot easier to reason about anyways. */
|
|
415
|
+
export function setRequireBootRequire(dir: string) {
|
|
416
|
+
dir = path.resolve(dir);
|
|
417
|
+
dir = dir.replaceAll("\\", "/");
|
|
418
|
+
if (!dir.endsWith("/")) {
|
|
419
|
+
dir += "/";
|
|
420
|
+
}
|
|
421
|
+
baseController.rootResolvePath = dir;
|
|
410
422
|
}
|
|
411
423
|
|
|
412
424
|
export const RequireController = SocketFunction.register(
|
package/src/misc.ts
CHANGED
|
@@ -406,4 +406,42 @@ export function removeFromSortedList<T>(list: T[], map: (val: T) => string | num
|
|
|
406
406
|
let index = binarySearchIndex(list.length, i => compare(map(list[i]), searchValue));
|
|
407
407
|
if (index < 0) return;
|
|
408
408
|
list.splice(index, 1);
|
|
409
|
-
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
export function timeoutToError<T>(time: number, p: Promise<T>, err: () => Error) {
|
|
414
|
+
return new Promise<T>((resolve, reject) => {
|
|
415
|
+
let timeout = setTimeout(() => reject(err()), time);
|
|
416
|
+
p.then(resolve, reject).finally(() => clearTimeout(timeout));
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// NOTE: Both errors and timeouts are converted to undefined
|
|
421
|
+
export function timeoutToUndefined<T>(time: number, p: Promise<T>) {
|
|
422
|
+
return new Promise<T | undefined>((resolve, reject) => {
|
|
423
|
+
let timeout = setTimeout(() => {
|
|
424
|
+
console.error(`timeoutToUndefined timed out after ${time}`);
|
|
425
|
+
resolve(undefined);
|
|
426
|
+
}, time);
|
|
427
|
+
p.then(resolve,
|
|
428
|
+
(err) => {
|
|
429
|
+
console.error(`timeoutToUndefined error: ${err.stack}`);
|
|
430
|
+
resolve(undefined);
|
|
431
|
+
}
|
|
432
|
+
).finally(() => clearTimeout(timeout));
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
export function timeoutToUndefinedSilent<T>(time: number, p: Promise<T>) {
|
|
436
|
+
return new Promise<T | undefined>((resolve, reject) => {
|
|
437
|
+
let timeout = setTimeout(() => {
|
|
438
|
+
resolve(undefined);
|
|
439
|
+
}, time);
|
|
440
|
+
p.then(
|
|
441
|
+
resolve,
|
|
442
|
+
(err) => {
|
|
443
|
+
resolve(undefined);
|
|
444
|
+
}
|
|
445
|
+
).finally(() => clearTimeout(timeout));
|
|
446
|
+
});
|
|
447
|
+
}
|