socket-function 0.9.0 → 0.9.1
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/.eslintrc.js +50 -50
- package/SocketFunction.ts +280 -280
- package/SocketFunctionTypes.ts +90 -90
- package/hot/HotReloadController.ts +105 -105
- package/mobx/UrlParam.ts +39 -39
- package/mobx/observer.tsx +49 -49
- package/mobx/promiseToObservable.tsx +41 -41
- package/package.json +30 -28
- package/require/CSSShim.ts +19 -19
- package/require/RequireController.ts +252 -252
- package/require/buffer.js +2368 -2368
- package/require/compileFlags.ts +44 -44
- package/require/require.html +13 -13
- package/require/require.js +462 -456
- package/spec.txt +115 -115
- package/src/CallFactory.ts +389 -389
- package/src/JSONLACKS/JSONLACKS.generated.js +17 -17
- package/src/JSONLACKS/JSONLACKS.pegjs +247 -247
- package/src/JSONLACKS/JSONLACKS.ts +429 -375
- package/src/args.ts +21 -21
- package/src/batching.ts +170 -129
- package/src/caching.ts +318 -314
- package/src/callHTTPHandler.ts +203 -203
- package/src/callManager.ts +134 -134
- package/src/certStore.ts +29 -29
- package/src/fixLargeNetworkCalls.ts +8 -8
- package/src/formatting/colors.ts +78 -78
- package/src/formatting/format.ts +160 -156
- package/src/formatting/logColors.ts +17 -17
- package/src/misc.ts +302 -171
- package/src/nodeCache.ts +92 -92
- package/src/nodeProxy.ts +54 -54
- package/src/profiling/getOwnTime.ts +142 -142
- package/src/profiling/measure.ts +273 -244
- package/src/profiling/stats.ts +212 -212
- package/src/profiling/tcpLagProxy.ts +63 -63
- package/src/storagePath.ts +10 -10
- package/src/tlsParsing.ts +96 -96
- package/src/types.ts +8 -8
- package/src/webSocketServer.ts +250 -250
- package/test/client.css +2 -2
- package/test/client.ts +46 -46
- package/test/server.ts +43 -43
- package/test/shared.ts +52 -52
- package/tsconfig.json +26 -26
package/src/formatting/format.ts
CHANGED
|
@@ -1,157 +1,161 @@
|
|
|
1
|
-
export function formatTime(milliseconds: number | undefined): string {
|
|
2
|
-
if (typeof milliseconds !== "number") return "";
|
|
3
|
-
if (milliseconds === 0) return "0ms";
|
|
4
|
-
if (milliseconds < 0) {
|
|
5
|
-
return "-" + formatTime(-milliseconds);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return formatMaxDecimals(milliseconds, 3) + "
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
} else if (
|
|
19
|
-
return formatMaxDecimals(milliseconds / 1000
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
// let
|
|
26
|
-
// remaining
|
|
27
|
-
// remaining
|
|
28
|
-
//
|
|
29
|
-
// remaining
|
|
30
|
-
// remaining
|
|
31
|
-
//
|
|
32
|
-
// let
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
let
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
while (decimals
|
|
90
|
-
decimals
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
output
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
1
|
+
export function formatTime(milliseconds: number | undefined, maxAbsoluteValue?: number): string {
|
|
2
|
+
if (typeof milliseconds !== "number") return "";
|
|
3
|
+
if (milliseconds === 0) return "0ms";
|
|
4
|
+
if (milliseconds < 0) {
|
|
5
|
+
return "-" + formatTime(-milliseconds, maxAbsoluteValue);
|
|
6
|
+
}
|
|
7
|
+
let scale = milliseconds;
|
|
8
|
+
if (maxAbsoluteValue) {
|
|
9
|
+
scale = Math.max(scale, maxAbsoluteValue);
|
|
10
|
+
}
|
|
11
|
+
if (scale < 1 / 1000) {
|
|
12
|
+
return formatMaxDecimals(milliseconds * 1000 * 1000, 3) + "ns";
|
|
13
|
+
} else if (scale < 1) {
|
|
14
|
+
return formatMaxDecimals(milliseconds * 1000, 3) + "us";
|
|
15
|
+
} else if (scale < 1000) {
|
|
16
|
+
return formatMaxDecimals(milliseconds, 3) + "ms";
|
|
17
|
+
// Use seconds until we have 10 minutes, as decimal minutes are confusing
|
|
18
|
+
} else if (scale < 1000 * 60 * 10) {
|
|
19
|
+
return formatMaxDecimals(milliseconds / 1000, 3) + "s";
|
|
20
|
+
} else if (scale < 1000 * 60 * 60) {
|
|
21
|
+
return formatMaxDecimals(milliseconds / 1000 / 60, 3) + "m";
|
|
22
|
+
} else if (scale < 1000 * 60 * 60 * 24) {
|
|
23
|
+
return formatMaxDecimals(milliseconds / 1000 / 60 / 60, 3) + "h";
|
|
24
|
+
// } else if (milliseconds < 1000 * 60 * 60 * 24 * 10) {
|
|
25
|
+
// let remaining = Math.round(milliseconds / 1000);
|
|
26
|
+
// let seconds = remaining % 60;
|
|
27
|
+
// remaining -= seconds;
|
|
28
|
+
// remaining /= 60;
|
|
29
|
+
// let minutes = remaining % 60;
|
|
30
|
+
// remaining -= minutes;
|
|
31
|
+
// remaining /= 60;
|
|
32
|
+
// let hours = remaining;
|
|
33
|
+
// remaining -= hours;
|
|
34
|
+
// remaining /= 24;
|
|
35
|
+
// let days = remaining;
|
|
36
|
+
// let time = `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
|
|
37
|
+
// if (days > 0) {
|
|
38
|
+
// if (days === 1) {
|
|
39
|
+
// time = `1 day ${time}`;
|
|
40
|
+
// } else {
|
|
41
|
+
// time = `${days} days ${time}`;
|
|
42
|
+
// }
|
|
43
|
+
// }
|
|
44
|
+
// return time;
|
|
45
|
+
} else {
|
|
46
|
+
let days = Math.round(milliseconds / 1000 / 60 / 60 / 24);
|
|
47
|
+
return `${days} days`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getTargetDecimals(maxAbsoluteValue: number, targetDigits: number) {
|
|
52
|
+
let intDigits = Math.floor(Math.log10(maxAbsoluteValue) + 1);
|
|
53
|
+
if (intDigits < 0) intDigits = 1;
|
|
54
|
+
let decimalDigits = targetDigits - intDigits;
|
|
55
|
+
// Happens if the number is so close to having too many digits that Math.log10 rounds it over.
|
|
56
|
+
if (decimalDigits < 0) {
|
|
57
|
+
decimalDigits = 0;
|
|
58
|
+
}
|
|
59
|
+
return decimalDigits;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Adds decimal digits to reach digits. If the number is simply too large, it won't remove
|
|
63
|
+
* digits, there will instead just be no decimal point.
|
|
64
|
+
*/
|
|
65
|
+
export function formatMaxDecimals(num: number, targetDigits: number, maxAbsoluteValue?: number, exactDecimals?: number): string {
|
|
66
|
+
if (typeof num !== "number") return "0";
|
|
67
|
+
// toFixed has a max of 100 digits
|
|
68
|
+
if (targetDigits > 100) targetDigits = 100;
|
|
69
|
+
if (!Number.isFinite(num)) return num.toFixed(targetDigits);
|
|
70
|
+
|
|
71
|
+
if (num < 0) return formatMaxDecimals(-num, targetDigits, maxAbsoluteValue, exactDecimals);
|
|
72
|
+
|
|
73
|
+
// TIMING:
|
|
74
|
+
// ~50ns toString
|
|
75
|
+
// ~400ns toLocaleString
|
|
76
|
+
// ~500ns toLocaleString("en-us")
|
|
77
|
+
// ~20us toLocaleString("en-us", { maximumFractionDigits: 2 })
|
|
78
|
+
// So, we are avoiding using toLocaleString, for now.
|
|
79
|
+
|
|
80
|
+
maxAbsoluteValue = maxAbsoluteValue ?? Math.abs(num);
|
|
81
|
+
|
|
82
|
+
let targetDecimals = exactDecimals ?? getTargetDecimals(maxAbsoluteValue, targetDigits);
|
|
83
|
+
let text = num.toFixed(targetDecimals);
|
|
84
|
+
let parts = text.split(".");
|
|
85
|
+
let integer = parts[0];
|
|
86
|
+
let decimals = parts[1] ?? "";
|
|
87
|
+
|
|
88
|
+
if (exactDecimals) {
|
|
89
|
+
while (decimals.length < exactDecimals) {
|
|
90
|
+
decimals += "0";
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
while (decimals[decimals.length - 1] === "0") {
|
|
94
|
+
decimals = decimals.slice(0, -1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let output = "";
|
|
99
|
+
|
|
100
|
+
// NOTE: ONLY add comma groups if it is > 4 digits. As 4234K is easily read, and commas
|
|
101
|
+
// only really matter for numbers such as 4234523K, which is hard to read.
|
|
102
|
+
if (integer.length > 4) {
|
|
103
|
+
for (let i = integer.length; i > 0; i -= 3) {
|
|
104
|
+
let start = i - 3;
|
|
105
|
+
if (start < 0) start = 0;
|
|
106
|
+
let str = integer.slice(start, i);
|
|
107
|
+
if (output) {
|
|
108
|
+
output = "," + output;
|
|
109
|
+
}
|
|
110
|
+
output = str + output;
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
output = integer;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (decimals) {
|
|
117
|
+
output += "." + decimals;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return output;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Actually formats any number, including decimals, by using K, M and B suffixes to get smaller values
|
|
124
|
+
* TODO: Support uK, uM and uB suffixes for very small numbers?
|
|
125
|
+
*/
|
|
126
|
+
export function formatNumber(count: number | undefined, maxAbsoluteValue?: number, noDecimal?: boolean, specialCurrency?: boolean): string {
|
|
127
|
+
if (typeof count !== "number") return "0";
|
|
128
|
+
if (count < 0) {
|
|
129
|
+
return "-" + formatNumber(-count, maxAbsoluteValue, noDecimal, specialCurrency);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
maxAbsoluteValue = maxAbsoluteValue ?? Math.abs(count);
|
|
133
|
+
|
|
134
|
+
// NOTE: We don't switch units as soon as we possible can, because...
|
|
135
|
+
// 3.594 vs 3.584 is harder to quickly distinguish compared to 3594 and 3584,
|
|
136
|
+
// the decimal simply makes it harder to read, and larger.
|
|
137
|
+
const extraFactor = 10;
|
|
138
|
+
let divisor = 1;
|
|
139
|
+
let suffix = "";
|
|
140
|
+
let currencyDecimalsNeeded = false;
|
|
141
|
+
if (maxAbsoluteValue < 1000 * extraFactor) {
|
|
142
|
+
if (specialCurrency) {
|
|
143
|
+
currencyDecimalsNeeded = true;
|
|
144
|
+
}
|
|
145
|
+
} else if (maxAbsoluteValue < 1000 * 1000 * extraFactor) {
|
|
146
|
+
suffix = "K";
|
|
147
|
+
divisor = 1000;
|
|
148
|
+
} else if (maxAbsoluteValue < 1000 * 1000 * 1000 * extraFactor) {
|
|
149
|
+
suffix = "M";
|
|
150
|
+
divisor = 1000 * 1000;
|
|
151
|
+
} else {
|
|
152
|
+
suffix = "B";
|
|
153
|
+
divisor = 1000 * 1000 * 1000;
|
|
154
|
+
}
|
|
155
|
+
count /= divisor;
|
|
156
|
+
maxAbsoluteValue /= divisor;
|
|
157
|
+
|
|
158
|
+
let maxDecimals = noDecimal ? 0 : 3;
|
|
159
|
+
|
|
160
|
+
return formatMaxDecimals(count, maxDecimals, maxAbsoluteValue, currencyDecimalsNeeded ? 2 : undefined) + suffix;
|
|
157
161
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { hslToHex, hslToRGB } from "./colors";
|
|
2
|
-
|
|
3
|
-
function ansiHSL(h: number, s: number, l: number, text: string): string {
|
|
4
|
-
let { r, g, b } = hslToRGB({ h, s, l });
|
|
5
|
-
return ansiRGB(r, g, b, text);
|
|
6
|
-
}
|
|
7
|
-
function ansiRGB(r: number, g: number, b: number, text: string): string {
|
|
8
|
-
return `\x1b[38;2;${r};${g};${b}m${text}\x1b[39m`;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const lightness = 68;
|
|
12
|
-
export const blue = ansiHSL.bind(null, 235, 100, lightness);
|
|
13
|
-
export const red = ansiHSL.bind(null, 0, 100, lightness);
|
|
14
|
-
export const green = (text: string) => `\x1b[32m${text}\x1b[39m`; // chalk.hsl(120, 100, lightness).bind(chalk);
|
|
15
|
-
export const yellow = (text: string) => `\x1b[33m${text}\x1b[39m`;
|
|
16
|
-
export const white = ansiHSL.bind(null, 0, 0, 80);
|
|
17
|
-
|
|
1
|
+
import { hslToHex, hslToRGB } from "./colors";
|
|
2
|
+
|
|
3
|
+
function ansiHSL(h: number, s: number, l: number, text: string): string {
|
|
4
|
+
let { r, g, b } = hslToRGB({ h, s, l });
|
|
5
|
+
return ansiRGB(r, g, b, text);
|
|
6
|
+
}
|
|
7
|
+
function ansiRGB(r: number, g: number, b: number, text: string): string {
|
|
8
|
+
return `\x1b[38;2;${r};${g};${b}m${text}\x1b[39m`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const lightness = 68;
|
|
12
|
+
export const blue = ansiHSL.bind(null, 235, 100, lightness);
|
|
13
|
+
export const red = ansiHSL.bind(null, 0, 100, lightness);
|
|
14
|
+
export const green = (text: string) => `\x1b[32m${text}\x1b[39m`; // chalk.hsl(120, 100, lightness).bind(chalk);
|
|
15
|
+
export const yellow = (text: string) => `\x1b[33m${text}\x1b[39m`;
|
|
16
|
+
export const white = ansiHSL.bind(null, 0, 0, 80);
|
|
17
|
+
|
|
18
18
|
export const magenta = (text: string) => `\x1b[35m${text}\x1b[39m`;
|