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