socket-function 0.18.0 → 0.19.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/require.js +1 -1
- package/src/batching.ts +1 -1
- package/src/misc.ts +5 -5
- package/test.ts +12 -12
- package/time/trueTimeShim.ts +26 -1
package/package.json
CHANGED
package/require/require.js
CHANGED
|
@@ -240,7 +240,7 @@
|
|
|
240
240
|
return requestsResolvedPaths.map(x => getModule(x));
|
|
241
241
|
} finally {
|
|
242
242
|
time = Date.now() - time;
|
|
243
|
-
console.log(`%cimport(${requests.join(", ")}) finished evaluate ${time}ms (${moduleCount} modules) at ${Date.now() - startTime}ms`, "color:
|
|
243
|
+
console.log(`%cimport(${requests.join(", ")}) finished evaluate ${time}ms (${moduleCount} modules) at ${Date.now() - startTime}ms`, "color: lightblue");
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
246
|
|
package/src/batching.ts
CHANGED
|
@@ -226,7 +226,7 @@ export async function runInfinitePollCallAtStart(
|
|
|
226
226
|
try {
|
|
227
227
|
await fnc();
|
|
228
228
|
} catch (e: any) {
|
|
229
|
-
console.error(`Error in infinite poll ${fnc.name} (continuing poll loop)\n${e.stack}`);
|
|
229
|
+
console.error(`Error in infinite poll ${fnc.name || fnc.toString().slice(0, 100)} (continuing poll loop)\n${e.stack}`);
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
})();
|
package/src/misc.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as crypto from "crypto";
|
|
2
1
|
import { canHaveChildren, MaybePromise } from "./types";
|
|
3
2
|
import { formatNumber } from "./formatting/format";
|
|
4
3
|
|
|
@@ -18,16 +17,17 @@ export function convertErrorStackToError(error: string): Error {
|
|
|
18
17
|
return errorObj;
|
|
19
18
|
}
|
|
20
19
|
|
|
20
|
+
|
|
21
21
|
export function sha256Hash(buffer: Buffer | string): string {
|
|
22
|
-
return crypto.createHash("sha256").update(buffer).digest("hex");
|
|
22
|
+
return require("crypto").createHash("sha256").update(buffer).digest("hex");
|
|
23
23
|
}
|
|
24
24
|
export function sha256HashBuffer(buffer: Buffer | string): Buffer {
|
|
25
|
-
return crypto.createHash("sha256").update(buffer).digest();
|
|
25
|
+
return require("crypto").createHash("sha256").update(buffer).digest();
|
|
26
26
|
}
|
|
27
27
|
/** Async, but works both clientside and serverside. */
|
|
28
28
|
export async function sha256HashPromise(buffer: Buffer) {
|
|
29
29
|
if (isNode()) {
|
|
30
|
-
return crypto.createHash("sha256").update(buffer).digest("hex");
|
|
30
|
+
return require("crypto").createHash("sha256").update(buffer).digest("hex");
|
|
31
31
|
} else {
|
|
32
32
|
let buf = await window.crypto.subtle.digest("SHA-256", buffer);
|
|
33
33
|
return Buffer.from(buf).toString("hex");
|
|
@@ -35,7 +35,7 @@ export async function sha256HashPromise(buffer: Buffer) {
|
|
|
35
35
|
}
|
|
36
36
|
export async function sha256BufferPromise(buffer: Buffer): Promise<Buffer> {
|
|
37
37
|
if (isNode()) {
|
|
38
|
-
return crypto.createHash("sha256").update(buffer).digest();
|
|
38
|
+
return require("crypto").createHash("sha256").update(buffer).digest();
|
|
39
39
|
} else {
|
|
40
40
|
let buf = await window.crypto.subtle.digest("SHA-256", buffer);
|
|
41
41
|
return Buffer.from(buf);
|
package/test.ts
CHANGED
|
@@ -8,23 +8,23 @@ import { forwardPort } from "./src/forwardPort";
|
|
|
8
8
|
|
|
9
9
|
// Usage example:
|
|
10
10
|
async function main() {
|
|
11
|
-
const externalPort =
|
|
11
|
+
const externalPort = 11300;
|
|
12
12
|
const internalPort = externalPort;
|
|
13
13
|
|
|
14
14
|
await forwardPort({ externalPort, internalPort });
|
|
15
15
|
|
|
16
16
|
// Listen on the external port
|
|
17
|
-
const server = http.createServer((req, res) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
server.listen(externalPort, "0.0.0.0");
|
|
22
|
-
|
|
23
|
-
{
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
17
|
+
// const server = http.createServer((req, res) => {
|
|
18
|
+
// console.log("Request received");
|
|
19
|
+
// res.end("Hello, world!");
|
|
20
|
+
// });
|
|
21
|
+
// server.listen(externalPort, "0.0.0.0");
|
|
22
|
+
|
|
23
|
+
// {
|
|
24
|
+
// const externalIP = await getExternalIP();
|
|
25
|
+
// let test = await fetch(`http://${externalIP}:${externalPort}`);
|
|
26
|
+
// console.log(await test.text());
|
|
27
|
+
// }
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
//await createPortMapping({ externalPort, internalPort, gateWayIP, internalIP, });
|
package/time/trueTimeShim.ts
CHANGED
|
@@ -15,6 +15,10 @@ const UPDATE_SMEAR_TICK_DURATION = 100;
|
|
|
15
15
|
const UPDATE_SMEAR_TICK_COUNT = 100;
|
|
16
16
|
const UPDATE_VERIFY_COUNT = 3;
|
|
17
17
|
|
|
18
|
+
// Time can never go backwards, but we can run at a slower rate until the output time allows
|
|
19
|
+
// the real time to catch up with it.
|
|
20
|
+
const MINIMUM_TIME_RATE = 0.5;
|
|
21
|
+
|
|
18
22
|
let trueTimeOffset = 0;
|
|
19
23
|
let didFirstTimeSync = false;
|
|
20
24
|
let onFirstTimeSync!: () => void;
|
|
@@ -23,8 +27,29 @@ let firstTimeSyncPromise = new Promise<void>((resolve) => {
|
|
|
23
27
|
});
|
|
24
28
|
|
|
25
29
|
const baseGetTime = Date.now;
|
|
30
|
+
let lastTime = 0;
|
|
31
|
+
let lastBaseTime = 0;
|
|
26
32
|
export function getTrueTime() {
|
|
27
|
-
|
|
33
|
+
let baseTime = baseGetTime();
|
|
34
|
+
let time = baseTime + trueTimeOffset;
|
|
35
|
+
// Only adjust time once we have a time offset. Otherwise systems with a really bad clock
|
|
36
|
+
// might take days be correct. It is better for the time to jump once at startup, rather
|
|
37
|
+
// than be off by days, for days at a time.
|
|
38
|
+
if (lastTime && trueTimeOffset) {
|
|
39
|
+
if (time < lastTime) {
|
|
40
|
+
let diff = baseTime - lastBaseTime;
|
|
41
|
+
if (diff >= 0) {
|
|
42
|
+
// Some time passed, so we have a baseline for how much to increase the time by.
|
|
43
|
+
// This allows the real time to catch up with our time naturally.
|
|
44
|
+
time = lastTime + diff * MINIMUM_TIME_RATE;
|
|
45
|
+
} else {
|
|
46
|
+
// The issue is the system time going backwards. In this case, allow the time to change
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
lastTime = time;
|
|
51
|
+
lastBaseTime = baseTime;
|
|
52
|
+
return time;
|
|
28
53
|
}
|
|
29
54
|
export function getTrueTimeOffset() {
|
|
30
55
|
return trueTimeOffset;
|