wenay-common2 1.0.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/lib/Common/Color.d.ts +31 -0
- package/lib/Common/Color.js +115 -0
- package/lib/Common/Time.d.ts +206 -0
- package/lib/Common/Time.js +393 -0
- package/lib/Common/WebHook2.d.ts +0 -0
- package/lib/Common/WebHook2.js +1 -0
- package/lib/Common/WebHook3.d.ts +39 -0
- package/lib/Common/WebHook3.js +211 -0
- package/lib/Common/async/PromiseArrayListen.d.ts +14 -0
- package/lib/Common/async/PromiseArrayListen.js +41 -0
- package/lib/Common/async/createIterableObject.d.ts +6 -0
- package/lib/Common/async/createIterableObject.js +66 -0
- package/lib/Common/async/waitRun.d.ts +31 -0
- package/lib/Common/async/waitRun.js +104 -0
- package/lib/Common/core/BaseTypes.d.ts +38 -0
- package/lib/Common/core/BaseTypes.js +2 -0
- package/lib/Common/core/Decorator.d.ts +24 -0
- package/lib/Common/core/Decorator.js +50 -0
- package/lib/Common/core/MemoFunc.d.ts +19 -0
- package/lib/Common/core/MemoFunc.js +99 -0
- package/lib/Common/core/common.d.ts +253 -0
- package/lib/Common/core/common.js +773 -0
- package/lib/Common/data/ByteStream.d.ts +96 -0
- package/lib/Common/data/ByteStream.js +440 -0
- package/lib/Common/data/List.d.ts +64 -0
- package/lib/Common/data/List.js +193 -0
- package/lib/Common/data/ListNodeAnd.d.ts +46 -0
- package/lib/Common/data/ListNodeAnd.js +89 -0
- package/lib/Common/data/objectPath.d.ts +11 -0
- package/lib/Common/data/objectPath.js +59 -0
- package/lib/Common/events/Listen.d.ts +55 -0
- package/lib/Common/events/Listen.js +124 -0
- package/lib/Common/events/SocketBuffer.d.ts +41 -0
- package/lib/Common/events/SocketBuffer.js +43 -0
- package/lib/Common/events/SocketServerHook.d.ts +47 -0
- package/lib/Common/events/SocketServerHook.js +38 -0
- package/lib/Common/events/event.d.ts +34 -0
- package/lib/Common/events/event.js +77 -0
- package/lib/Common/events/joinListens.d.ts +17 -0
- package/lib/Common/events/joinListens.js +40 -0
- package/lib/Common/fsKeyVolume.d.ts +34 -0
- package/lib/Common/fsKeyVolume.js +92 -0
- package/lib/Common/funcTimeWait.d.ts +29 -0
- package/lib/Common/funcTimeWait.js +116 -0
- package/lib/Common/id-pool.d.ts +5 -0
- package/lib/Common/id-pool.js +12 -0
- package/lib/Common/inputAutoStep.d.ts +14 -0
- package/lib/Common/inputAutoStep.js +65 -0
- package/lib/Common/isProxy.d.ts +2 -0
- package/lib/Common/isProxy.js +40 -0
- package/lib/Common/math/Math.d.ts +12 -0
- package/lib/Common/math/Math.js +73 -0
- package/lib/Common/math/other.d.ts +23 -0
- package/lib/Common/math/other.js +70 -0
- package/lib/Common/math/scoreLana.d.ts +22 -0
- package/lib/Common/math/scoreLana.js +67 -0
- package/lib/Common/node_console.d.ts +6 -0
- package/lib/Common/node_console.js +170 -0
- package/lib/Common/rcp/listen-deep.d.ts +20 -0
- package/lib/Common/rcp/listen-deep.js +49 -0
- package/lib/Common/rcp/listen-socket.d.ts +25 -0
- package/lib/Common/rcp/listen-socket.js +105 -0
- package/lib/Common/rcp/rpc-client-auto.d.ts +12 -0
- package/lib/Common/rcp/rpc-client-auto.js +20 -0
- package/lib/Common/rcp/rpc-client.d.ts +30 -0
- package/lib/Common/rcp/rpc-client.js +169 -0
- package/lib/Common/rcp/rpc-dynamic.d.ts +2 -0
- package/lib/Common/rcp/rpc-dynamic.js +12 -0
- package/lib/Common/rcp/rpc-index.d.ts +8 -0
- package/lib/Common/rcp/rpc-index.js +24 -0
- package/lib/Common/rcp/rpc-limits.d.ts +14 -0
- package/lib/Common/rcp/rpc-limits.js +24 -0
- package/lib/Common/rcp/rpc-protocol.d.ts +12 -0
- package/lib/Common/rcp/rpc-protocol.js +4 -0
- package/lib/Common/rcp/rpc-server-auto.d.ts +15 -0
- package/lib/Common/rcp/rpc-server-auto.js +28 -0
- package/lib/Common/rcp/rpc-server.d.ts +27 -0
- package/lib/Common/rcp/rpc-server.js +175 -0
- package/lib/Common/rcp/rpc-walk.d.ts +8 -0
- package/lib/Common/rcp/rpc-walk.js +82 -0
- package/lib/Exchange/Bars.d.ts +181 -0
- package/lib/Exchange/Bars.js +475 -0
- package/lib/Exchange/CParams.d.ts +225 -0
- package/lib/Exchange/CParams.js +244 -0
- package/lib/client.d.ts +31 -0
- package/lib/client.js +70 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +70 -0
- package/lib/server.d.ts +3 -0
- package/lib/server.js +19 -0
- package/lib/toError/myThrow.d.ts +5 -0
- package/lib/toError/myThrow.js +23 -0
- package/lib/tsconfig.json +6 -0
- package/package.json +32 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type tCorrelationByBuffer = {
|
|
2
|
+
max: number;
|
|
3
|
+
bufferOn?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export declare function CorrelationRollingByBuffer(data: tCorrelationByBuffer): {
|
|
6
|
+
init(data: tCorrelationByBuffer): void;
|
|
7
|
+
clear(data?: tCorrelationByBuffer): void;
|
|
8
|
+
corr2(val1: number, val2: number, key1: any, key2: any): {
|
|
9
|
+
corr: number;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CorrelationRollingByBuffer = CorrelationRollingByBuffer;
|
|
4
|
+
function CorrelationRollingByBuffer(data) {
|
|
5
|
+
let setting = { ...data };
|
|
6
|
+
const map = new Map();
|
|
7
|
+
const defBuf = () => ({
|
|
8
|
+
sum1: 0, sum2: 0,
|
|
9
|
+
pow1: 0, pow2: 0,
|
|
10
|
+
mulSum: 0, step: 0,
|
|
11
|
+
history1: [], history2: []
|
|
12
|
+
});
|
|
13
|
+
const getBuffer = (key1, key2) => {
|
|
14
|
+
let a1 = map.get(key1);
|
|
15
|
+
if (!a1)
|
|
16
|
+
map.set(key1, a1 = new Map());
|
|
17
|
+
let a2 = a1.get(key2);
|
|
18
|
+
if (!a2)
|
|
19
|
+
a1.set(key2, a2 = defBuf());
|
|
20
|
+
return a2;
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
init(data) { setting = { ...data }; },
|
|
24
|
+
clear(data) {
|
|
25
|
+
map.clear();
|
|
26
|
+
if (data)
|
|
27
|
+
this.init(data);
|
|
28
|
+
},
|
|
29
|
+
corr2(val1, val2, key1, key2) {
|
|
30
|
+
const buffer = setting.bufferOn ? getBuffer(key1, key2) : defBuf();
|
|
31
|
+
buffer.history1.push(val1);
|
|
32
|
+
buffer.history2.push(val2);
|
|
33
|
+
let dropped1 = 0;
|
|
34
|
+
let dropped2 = 0;
|
|
35
|
+
if (buffer.history1.length > setting.max) {
|
|
36
|
+
dropped1 = buffer.history1.shift();
|
|
37
|
+
dropped2 = buffer.history2.shift();
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
buffer.step++;
|
|
41
|
+
}
|
|
42
|
+
buffer.sum1 = buffer.sum1 + val1 - dropped1;
|
|
43
|
+
buffer.sum2 = buffer.sum2 + val2 - dropped2;
|
|
44
|
+
buffer.pow1 = buffer.pow1 + val1 ** 2 - dropped1 ** 2;
|
|
45
|
+
buffer.pow2 = buffer.pow2 + val2 ** 2 - dropped2 ** 2;
|
|
46
|
+
buffer.mulSum = buffer.mulSum + (val1 * val2) - (dropped1 * dropped2);
|
|
47
|
+
return calculatePearson(buffer, buffer.history1.length);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function calculatePearson(buffer, count) {
|
|
52
|
+
if (count < 2)
|
|
53
|
+
return { corr: 0 };
|
|
54
|
+
const { sum1, sum2, pow1, pow2, mulSum } = buffer;
|
|
55
|
+
const { sqrt } = Math;
|
|
56
|
+
if (!Number.isFinite(sum1) || !Number.isFinite(sum2) || !Number.isFinite(mulSum)) {
|
|
57
|
+
console.error("Invalid math values in correlation calculation", { sum1, sum2, mulSum });
|
|
58
|
+
return { corr: 0 };
|
|
59
|
+
}
|
|
60
|
+
const variance1 = count * pow1 - sum1 ** 2;
|
|
61
|
+
const variance2 = count * pow2 - sum2 ** 2;
|
|
62
|
+
if (variance1 <= 0 || variance2 <= 0) {
|
|
63
|
+
return { corr: 0 };
|
|
64
|
+
}
|
|
65
|
+
const dense = sqrt(variance1 * variance2);
|
|
66
|
+
const numerator = count * mulSum - (sum1 * sum2);
|
|
67
|
+
const result = numerator / dense;
|
|
68
|
+
if (result > 1)
|
|
69
|
+
return { corr: 1 };
|
|
70
|
+
if (result < -1)
|
|
71
|
+
return { corr: -1 };
|
|
72
|
+
return { corr: result };
|
|
73
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type tDatum = {
|
|
2
|
+
price: number[];
|
|
3
|
+
key: string | object | number;
|
|
4
|
+
};
|
|
5
|
+
type tfLeaderResult = {};
|
|
6
|
+
declare function PriceTOSumPercent(price: number[]): number[];
|
|
7
|
+
declare function strategyStepOfStepAll(symbols_: tDatum[]): {
|
|
8
|
+
leadersKey: {
|
|
9
|
+
key: string | object | number;
|
|
10
|
+
score: number;
|
|
11
|
+
}[];
|
|
12
|
+
leadersId: (number)[];
|
|
13
|
+
id: number;
|
|
14
|
+
key: string | object | number;
|
|
15
|
+
}[];
|
|
16
|
+
type tStepOfStep = {
|
|
17
|
+
t1: number[];
|
|
18
|
+
t2: number[];
|
|
19
|
+
minK?: number;
|
|
20
|
+
percentM?: number;
|
|
21
|
+
onlyBuy?: boolean;
|
|
22
|
+
};
|
|
23
|
+
declare function strategyStepOfStep({ t1, t2, minK: _minK, percentM: _percentM, onlyBuy: _onlyBuy }: tStepOfStep): number;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
function PriceTOSumPercent(price) {
|
|
3
|
+
return price.map(e => e / price[0]);
|
|
4
|
+
}
|
|
5
|
+
function strategyStepOfStepAll(symbols_) {
|
|
6
|
+
const symbols = symbols_.map(e => ({ price: PriceTOSumPercent(e.price), key: e.key }));
|
|
7
|
+
const minK = 0;
|
|
8
|
+
const percentM = 1 / 100;
|
|
9
|
+
const result = [];
|
|
10
|
+
for (let i = 0; i < symbols.length; i++) {
|
|
11
|
+
const tt = { key: symbols[i].key, id: i, leadersId: [], leadersKey: [] };
|
|
12
|
+
result.push(tt);
|
|
13
|
+
for (let y = 0; y < symbols.length; y++) {
|
|
14
|
+
if (i == y)
|
|
15
|
+
continue;
|
|
16
|
+
const r = strategyStepOfStep({ t1: symbols[i].price, t2: symbols[y].price, minK, percentM });
|
|
17
|
+
if (r > 0) {
|
|
18
|
+
tt.leadersKey.push({ key: symbols[y].key, score: r });
|
|
19
|
+
tt.leadersId.push(y);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
tt.leadersKey = tt.leadersKey.sort((a, b) => b.score - a.score);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
function strategyStepOfStep({ t1, t2, minK: _minK, percentM: _percentM, onlyBuy: _onlyBuy }) {
|
|
27
|
+
const onlyBuy = !!_onlyBuy;
|
|
28
|
+
const minK = _minK ?? 0;
|
|
29
|
+
const percentM = _percentM ?? 1 / 100;
|
|
30
|
+
const conArr = (arr) => {
|
|
31
|
+
let last = arr[0], result = 0;
|
|
32
|
+
return arr.map(e => {
|
|
33
|
+
const r = e - last;
|
|
34
|
+
last = e;
|
|
35
|
+
result += r;
|
|
36
|
+
result = result * (1 - percentM);
|
|
37
|
+
return result;
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const [r1, r2] = [t1, t2].map(e => conArr(e));
|
|
41
|
+
const length = r1.length;
|
|
42
|
+
let m1 = 0, m2 = 0;
|
|
43
|
+
const result1 = [0];
|
|
44
|
+
let [lastD1, lastD2] = [0, 0];
|
|
45
|
+
for (let i = 1; i < length; i++) {
|
|
46
|
+
const [d1, d2] = [r1[i], r2[i]];
|
|
47
|
+
const [p1, p2] = [t1[i] - t1[i - 1], t2[i] - t2[i - 1]];
|
|
48
|
+
result1.push((p1) * (d2 - d1));
|
|
49
|
+
[lastD1, lastD2] = [d1, d2];
|
|
50
|
+
}
|
|
51
|
+
const day = { profitDays: 0, lossDays: 0, profit: 0, loss: 0 };
|
|
52
|
+
for (let i = 0; i < result1.length; i++) {
|
|
53
|
+
if (result1[i] > 0) {
|
|
54
|
+
day.profitDays++;
|
|
55
|
+
day.profit += result1[i];
|
|
56
|
+
}
|
|
57
|
+
if (result1[i] < 0) {
|
|
58
|
+
day.lossDays++;
|
|
59
|
+
day.loss += result1[i];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const k1 = day.profitDays / result1.length;
|
|
63
|
+
const k1b = day.loss / result1.length;
|
|
64
|
+
const k2 = day.profit / ((day.profit + Math.abs(day.loss)) || 1);
|
|
65
|
+
let flag = -1;
|
|
66
|
+
if (day.profitDays > day.lossDays && day.profit > day.loss) {
|
|
67
|
+
flag = 1;
|
|
68
|
+
}
|
|
69
|
+
return k1 * k2 * flag;
|
|
70
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type tDatumL = {
|
|
2
|
+
price: number[];
|
|
3
|
+
key: string | object | number;
|
|
4
|
+
};
|
|
5
|
+
export declare function strategyStepOfStepAllL(symbols: tDatumL[]): {
|
|
6
|
+
leadersKey: {
|
|
7
|
+
key: string | object | number;
|
|
8
|
+
score: number;
|
|
9
|
+
}[];
|
|
10
|
+
leadersId: (number)[];
|
|
11
|
+
id: number;
|
|
12
|
+
key: string | object | number;
|
|
13
|
+
}[];
|
|
14
|
+
type tStepOfStepL = {
|
|
15
|
+
t1: number[];
|
|
16
|
+
t2: number[];
|
|
17
|
+
minK?: number;
|
|
18
|
+
percentM?: number;
|
|
19
|
+
onlyBuy?: boolean;
|
|
20
|
+
};
|
|
21
|
+
export declare function strategyStepOfStepL({ t1, t2, minK: _minK, percentM: _percentM, onlyBuy: _onlyBuy }: tStepOfStepL): number;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.strategyStepOfStepAllL = strategyStepOfStepAllL;
|
|
4
|
+
exports.strategyStepOfStepL = strategyStepOfStepL;
|
|
5
|
+
function strategyStepOfStepAllL(symbols) {
|
|
6
|
+
const minK = 0;
|
|
7
|
+
const percentM = 1 / 100;
|
|
8
|
+
const result = [];
|
|
9
|
+
for (let i = 0; i < symbols.length; i++) {
|
|
10
|
+
const tt = { key: symbols[i].key, id: i, leadersId: [], leadersKey: [] };
|
|
11
|
+
result.push(tt);
|
|
12
|
+
for (let y = 0; y < symbols.length; y++) {
|
|
13
|
+
if (i == y)
|
|
14
|
+
continue;
|
|
15
|
+
const r = strategyStepOfStepL({ t1: symbols[i].price, t2: symbols[y].price, minK, percentM });
|
|
16
|
+
if (r > 0) {
|
|
17
|
+
tt.leadersKey.push({ key: symbols[y].key, score: r });
|
|
18
|
+
tt.leadersId.push(y);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
function PriceTOSumPercentL(price) {
|
|
25
|
+
return price.map(e => e / price[0]);
|
|
26
|
+
}
|
|
27
|
+
function strategyStepOfStepL({ t1, t2, minK: _minK, percentM: _percentM, onlyBuy: _onlyBuy }) {
|
|
28
|
+
const onlyBuy = !!_onlyBuy;
|
|
29
|
+
const minK = _minK ?? 0;
|
|
30
|
+
const percentM = _percentM ?? 1 / 100;
|
|
31
|
+
const conArr = (arr) => {
|
|
32
|
+
let last = arr[0], result = 0;
|
|
33
|
+
return arr.map(e => {
|
|
34
|
+
const r = e - last;
|
|
35
|
+
last = e;
|
|
36
|
+
result += r;
|
|
37
|
+
result = result * (1 - percentM);
|
|
38
|
+
return result;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const [r1, r2] = [t1, t2].map(e => conArr(e));
|
|
42
|
+
const length = r1.length;
|
|
43
|
+
let m1 = 0, m2 = 0;
|
|
44
|
+
const result1 = [0];
|
|
45
|
+
let [lastD1, lastD2] = [0, 0];
|
|
46
|
+
for (let i = 1; i < length; i++) {
|
|
47
|
+
const [d1, d2] = [r1[i], r2[i]];
|
|
48
|
+
const [p1, p2] = [t1[i] - t1[i - 1], t2[i] - t2[i - 1]];
|
|
49
|
+
const pLana = p1 - p2;
|
|
50
|
+
result1.push(p1 * d2);
|
|
51
|
+
[lastD1, lastD2] = [d1, d2];
|
|
52
|
+
}
|
|
53
|
+
const day = { profitDays: 0, lossDays: 0, profit: 0, loss: 0 };
|
|
54
|
+
for (let i = 0; i < result1.length; i++) {
|
|
55
|
+
if (result1[i] > 0) {
|
|
56
|
+
day.profitDays++;
|
|
57
|
+
day.profit += result1[i];
|
|
58
|
+
}
|
|
59
|
+
if (result1[i] < 0) {
|
|
60
|
+
day.lossDays++;
|
|
61
|
+
day.loss += result1[i];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const k1 = day.profitDays / result1.length;
|
|
65
|
+
const k2 = day.profit / ((day.profit + day.loss) || 1);
|
|
66
|
+
return k1 * k2;
|
|
67
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export {};
|
|
2
|
+
export declare function enable(flag?: boolean): void;
|
|
3
|
+
export declare function disable(): void;
|
|
4
|
+
export declare function __LineFile(lvl?: number): string;
|
|
5
|
+
export declare function __LineFile2(lvl?: number): string;
|
|
6
|
+
export declare function __LineFiles(lvlStart?: number, lvlEnd?: number | undefined): string[];
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.enable = enable;
|
|
4
|
+
exports.disable = disable;
|
|
5
|
+
exports.__LineFile = __LineFile;
|
|
6
|
+
exports.__LineFile2 = __LineFile2;
|
|
7
|
+
exports.__LineFiles = __LineFiles;
|
|
8
|
+
const { self, window } = globalThis;
|
|
9
|
+
let _enabled = false;
|
|
10
|
+
function enable(flag = true) { _enabled = flag; }
|
|
11
|
+
function disable() { _enabled = false; }
|
|
12
|
+
let wrapCallSite;
|
|
13
|
+
function setupLogs2() {
|
|
14
|
+
if (typeof self != 'object' && typeof window != "object") {
|
|
15
|
+
function moduleName(name) { return name; }
|
|
16
|
+
let inspector = require(moduleName('inspector'));
|
|
17
|
+
if (inspector.url() != undefined)
|
|
18
|
+
return;
|
|
19
|
+
try {
|
|
20
|
+
let module = require(moduleName('source-map-support'));
|
|
21
|
+
module.install();
|
|
22
|
+
wrapCallSite = module.wrapCallSite;
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
console.warn(e);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
_enabled = true;
|
|
29
|
+
const origLogMethod = console.log;
|
|
30
|
+
const origErrorMethod = console.error;
|
|
31
|
+
let _callee;
|
|
32
|
+
for (let methodName of [
|
|
33
|
+
'debug', 'info', 'log', 'warn', 'error', 'group', 'groupCollapsed', 'table', 'timeLog', 'timeEnd',
|
|
34
|
+
'count', 'assert', 'dir', 'dirxml'
|
|
35
|
+
]) {
|
|
36
|
+
const origMethod = console[methodName];
|
|
37
|
+
console[methodName] = ((...args) => {
|
|
38
|
+
if (!_enabled)
|
|
39
|
+
return origMethod(...args);
|
|
40
|
+
const originalPrepareStackTrace = Error.prepareStackTrace;
|
|
41
|
+
Error.prepareStackTrace = (_, stack) => stack;
|
|
42
|
+
let callee = new Error().stack[1];
|
|
43
|
+
Error.prepareStackTrace = originalPrepareStackTrace;
|
|
44
|
+
if (!callee) {
|
|
45
|
+
origErrorMethod("сallee is not found in node_console");
|
|
46
|
+
_enabled = false;
|
|
47
|
+
return origMethod(...args);
|
|
48
|
+
}
|
|
49
|
+
if (!methodName.match(/debug|info|log|warn|error|dirxml/)) {
|
|
50
|
+
_callee ??= callee;
|
|
51
|
+
return origMethod(...args);
|
|
52
|
+
}
|
|
53
|
+
if (_callee) {
|
|
54
|
+
callee = _callee;
|
|
55
|
+
_callee = undefined;
|
|
56
|
+
}
|
|
57
|
+
if (wrapCallSite)
|
|
58
|
+
callee = wrapCallSite(callee);
|
|
59
|
+
const fileName = callee.getFileName();
|
|
60
|
+
if (fileName?.includes("source-map-support")) {
|
|
61
|
+
origMethod(...args);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
let fileAndLine = `${fileName}:${callee.getLineNumber()}:${callee.getColumnNumber()} ` + callee.getFunctionName();
|
|
65
|
+
fileAndLine = fileAndLine.replaceAll("\\", "/");
|
|
66
|
+
fileAndLine = fileAndLine.replace("webpack:///", "");
|
|
67
|
+
fileAndLine = fileAndLine.replace("?", "");
|
|
68
|
+
if (!fileAndLine.startsWith("./"))
|
|
69
|
+
if (!fileAndLine.toLowerCase().startsWith("file:///"))
|
|
70
|
+
fileAndLine = "file:///" + fileAndLine;
|
|
71
|
+
let [firstArg, ...otherArgs] = args;
|
|
72
|
+
if (1)
|
|
73
|
+
origMethod(...args, "", fileAndLine);
|
|
74
|
+
else if (typeof firstArg === 'string') {
|
|
75
|
+
origMethod(fileAndLine + ' ' + firstArg, ...otherArgs);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
origMethod(fileAndLine, ...args);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function setupLogs() {
|
|
85
|
+
if (typeof self != 'object' && typeof window != "object") {
|
|
86
|
+
function moduleName(name) { return name; }
|
|
87
|
+
let inspector = require(moduleName('inspector'));
|
|
88
|
+
if (inspector.url() != undefined)
|
|
89
|
+
return;
|
|
90
|
+
_enabled = true;
|
|
91
|
+
const origLogMethod = console.log;
|
|
92
|
+
const origErrorMethod = console.error;
|
|
93
|
+
let _callee;
|
|
94
|
+
for (let methodName of [
|
|
95
|
+
'debug', 'info', 'log', 'warn', 'error', 'group', 'groupCollapsed', 'table', 'timeLog', 'timeEnd',
|
|
96
|
+
'count', 'assert', 'dir', 'dirxml'
|
|
97
|
+
]) {
|
|
98
|
+
const origMethod = console[methodName];
|
|
99
|
+
console[methodName] = ((...args) => {
|
|
100
|
+
if (!_enabled)
|
|
101
|
+
return origMethod(...args);
|
|
102
|
+
const stack = new Error().stack;
|
|
103
|
+
if (!stack)
|
|
104
|
+
return origMethod(...args);
|
|
105
|
+
const lines = stack.split('\n');
|
|
106
|
+
const callerLine = lines[2];
|
|
107
|
+
if (!callerLine)
|
|
108
|
+
return origMethod(...args);
|
|
109
|
+
const match = callerLine.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?/);
|
|
110
|
+
if (!match)
|
|
111
|
+
return origMethod(...args);
|
|
112
|
+
const [, functionName, fileName, lineNumber, columnNumber] = match;
|
|
113
|
+
const funcName = functionName || '<anonymous>';
|
|
114
|
+
let fileAndLine = `${fileName}:${lineNumber}:${columnNumber} ${funcName}`;
|
|
115
|
+
fileAndLine = fileAndLine.replaceAll("\\", "/");
|
|
116
|
+
fileAndLine = fileAndLine.replace("webpack:///", "");
|
|
117
|
+
fileAndLine = fileAndLine.replace("?", "");
|
|
118
|
+
if (!fileAndLine.startsWith("./"))
|
|
119
|
+
if (!fileAndLine.toLowerCase().startsWith("file:///"))
|
|
120
|
+
fileAndLine = "file:///" + fileAndLine;
|
|
121
|
+
if (!methodName.match(/debug|info|log|warn|error|dirxml/)) {
|
|
122
|
+
_callee ??= fileAndLine;
|
|
123
|
+
return origMethod(...args);
|
|
124
|
+
}
|
|
125
|
+
if (_callee) {
|
|
126
|
+
fileAndLine = _callee;
|
|
127
|
+
_callee = undefined;
|
|
128
|
+
}
|
|
129
|
+
origMethod(...args, "", fileAndLine);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (1)
|
|
135
|
+
setupLogs();
|
|
136
|
+
function __LineFile(lvl = 0) {
|
|
137
|
+
const stack = new Error().stack;
|
|
138
|
+
if (!stack)
|
|
139
|
+
return "";
|
|
140
|
+
const lines = stack.split('\n');
|
|
141
|
+
const targetLine = lines[lvl + 2];
|
|
142
|
+
if (!targetLine)
|
|
143
|
+
return "";
|
|
144
|
+
const match = targetLine.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?/);
|
|
145
|
+
if (match) {
|
|
146
|
+
const [, functionName, fileName, lineNumber, columnNumber] = match;
|
|
147
|
+
const funcName = functionName || '<anonymous>';
|
|
148
|
+
return `${fileName}:${lineNumber}:${columnNumber} ${funcName}`;
|
|
149
|
+
}
|
|
150
|
+
return targetLine.trim();
|
|
151
|
+
}
|
|
152
|
+
function __LineFile2(lvl = 0) {
|
|
153
|
+
const originalPrepareStackTrace = Error.prepareStackTrace;
|
|
154
|
+
Error.prepareStackTrace = (_, stack) => stack;
|
|
155
|
+
let e = new Error().stack[lvl + 1];
|
|
156
|
+
if (wrapCallSite)
|
|
157
|
+
e = wrapCallSite(e);
|
|
158
|
+
Error.prepareStackTrace = originalPrepareStackTrace;
|
|
159
|
+
return `${e.getFileName()}:${e.getLineNumber()}:${e.getColumnNumber()} ` + e.getFunctionName();
|
|
160
|
+
}
|
|
161
|
+
function __LineFiles(lvlStart = 0, lvlEnd = 5) {
|
|
162
|
+
const originalPrepareStackTrace = Error.prepareStackTrace;
|
|
163
|
+
Error.prepareStackTrace = (_, stack) => stack;
|
|
164
|
+
let e = new Error().stack.slice(lvlStart + 1, lvlEnd);
|
|
165
|
+
if (wrapCallSite)
|
|
166
|
+
e = e.map(e => wrapCallSite(e));
|
|
167
|
+
const msgs = e.map(e => `${e.getFileName()}:${e.getLineNumber()}:${e.getColumnNumber()} ` + e.getFunctionName());
|
|
168
|
+
Error.prepareStackTrace = originalPrepareStackTrace;
|
|
169
|
+
return msgs;
|
|
170
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { funcListenCallbackBase } from "../events/Listen";
|
|
2
|
+
import { listenSocket, listenSocketFirst, listenSocketAll, listenSocketSmart } from "./listen-socket";
|
|
3
|
+
type Obj = Record<string, any>;
|
|
4
|
+
type ListenBase<T extends any[]> = ReturnType<typeof funcListenCallbackBase<T>>;
|
|
5
|
+
type InferArgs<T> = T extends ListenBase<infer R> ? R : never;
|
|
6
|
+
type SocketResult<T extends any[]> = ReturnType<typeof listenSocket<T>>;
|
|
7
|
+
type TransformValue<V> = V extends ListenBase<any> ? SocketResult<InferArgs<V>> : V extends typeof Promise ? V : V extends (...a: any) => any ? V : V extends object ? {
|
|
8
|
+
[K in keyof V]: TransformValue<V[K]>;
|
|
9
|
+
} : V;
|
|
10
|
+
export type DeepSocketListen<T> = {
|
|
11
|
+
[K in keyof T]: TransformValue<T[K]>;
|
|
12
|
+
};
|
|
13
|
+
export declare function matchKeys<T extends Obj, T2 extends Obj>(obj1: T, obj2: T2): boolean;
|
|
14
|
+
export declare function matchKeysList<T extends Obj>(obj1: T, keys: string[]): boolean;
|
|
15
|
+
export declare function deepMapByKeysList<T, T3>(obj1: T, keys: string[], func: (a: any) => T3): T | T3 | null;
|
|
16
|
+
export declare function deepMapByKeys<T, T2 extends Obj, T3>(obj1: T, obj2: T2, func: (a: T2) => T3): T3 | T | null;
|
|
17
|
+
export declare function deepListenFirst<T>(obj: T, data?: Parameters<typeof listenSocketFirst>[1]): DeepSocketListen<T>;
|
|
18
|
+
export declare function deepListenAll<T>(obj: T, data?: Parameters<typeof listenSocketAll>[1]): DeepSocketListen<T>;
|
|
19
|
+
export declare function deepListenSmart<T>(obj: T, data?: Parameters<typeof listenSocketSmart>[1]): DeepSocketListen<T>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.matchKeys = matchKeys;
|
|
4
|
+
exports.matchKeysList = matchKeysList;
|
|
5
|
+
exports.deepMapByKeysList = deepMapByKeysList;
|
|
6
|
+
exports.deepMapByKeys = deepMapByKeys;
|
|
7
|
+
exports.deepListenFirst = deepListenFirst;
|
|
8
|
+
exports.deepListenAll = deepListenAll;
|
|
9
|
+
exports.deepListenSmart = deepListenSmart;
|
|
10
|
+
const Listen_1 = require("../events/Listen");
|
|
11
|
+
const listen_socket_1 = require("./listen-socket");
|
|
12
|
+
function isLeafValue(value) {
|
|
13
|
+
return (value == null ||
|
|
14
|
+
typeof value === "function" ||
|
|
15
|
+
value instanceof Function ||
|
|
16
|
+
typeof value !== "object");
|
|
17
|
+
}
|
|
18
|
+
function matchKeys(obj1, obj2) {
|
|
19
|
+
return matchKeysList(obj1, Object.keys(obj2));
|
|
20
|
+
}
|
|
21
|
+
function matchKeysList(obj1, keys) {
|
|
22
|
+
const k1 = Object.keys(obj1);
|
|
23
|
+
return k1.length === keys.length && new Set([...k1, ...keys]).size === keys.length;
|
|
24
|
+
}
|
|
25
|
+
function deepMapByKeysList(obj1, keys, func) {
|
|
26
|
+
if (isLeafValue(obj1))
|
|
27
|
+
return obj1;
|
|
28
|
+
if (matchKeysList(obj1, keys))
|
|
29
|
+
return func(obj1);
|
|
30
|
+
return Object.fromEntries(Object.entries(obj1).map(([k, v]) => [k, deepMapByKeysList(v, keys, func)]));
|
|
31
|
+
}
|
|
32
|
+
function deepMapByKeys(obj1, obj2, func) {
|
|
33
|
+
if (isLeafValue(obj1))
|
|
34
|
+
return obj1;
|
|
35
|
+
const keys = Object.keys(obj2);
|
|
36
|
+
if (matchKeysList(obj1, keys))
|
|
37
|
+
return func(obj1);
|
|
38
|
+
return Object.fromEntries(Object.entries(obj1).map(([k, v]) => [k, deepMapByKeysList(v, keys, func)]));
|
|
39
|
+
}
|
|
40
|
+
const NOOP_LISTEN = (0, Listen_1.funcListenCallbackBase)((_e) => { });
|
|
41
|
+
function deepListenFirst(obj, data) {
|
|
42
|
+
return deepMapByKeys(obj, NOOP_LISTEN, (e) => (0, listen_socket_1.listenSocketFirst)(e, data));
|
|
43
|
+
}
|
|
44
|
+
function deepListenAll(obj, data) {
|
|
45
|
+
return deepMapByKeys(obj, NOOP_LISTEN, (e) => (0, listen_socket_1.listenSocketAll)(e, data));
|
|
46
|
+
}
|
|
47
|
+
function deepListenSmart(obj, data) {
|
|
48
|
+
return deepMapByKeys(obj, NOOP_LISTEN, (e) => (0, listen_socket_1.listenSocketSmart)(e, data));
|
|
49
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { funcListenCallback, type Listener } from "../events/Listen";
|
|
2
|
+
type ListenCallbackResult<T extends any[] = any[]> = ReturnType<typeof funcListenCallback<T>>;
|
|
3
|
+
export declare function listenSocket<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, d?: {
|
|
4
|
+
readonly status?: () => boolean;
|
|
5
|
+
readonly addListenClose?: ListenCallbackResult<any>;
|
|
6
|
+
readonly stop?: (x: Listener<Z>) => any;
|
|
7
|
+
readonly paramsModify?: (...e: Z) => any[];
|
|
8
|
+
}): {
|
|
9
|
+
callback: (z: Listener<Z>) => Promise<void>;
|
|
10
|
+
removeCallback: () => boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare function listenSocketFirst<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof listenSocket>[1], "paramsModify">): {
|
|
13
|
+
callback: (z: (a: Z[0]) => void) => void;
|
|
14
|
+
removeCallback: () => boolean;
|
|
15
|
+
};
|
|
16
|
+
export declare function listenSocketAll<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof listenSocket>[1], "paramsModify">): {
|
|
17
|
+
callback: (z: (...args: Z) => void) => void;
|
|
18
|
+
removeCallback: () => boolean;
|
|
19
|
+
};
|
|
20
|
+
type SmartCallback<Z extends any[]> = Z extends [infer Single] ? (a: Single) => void : (...args: Z) => void;
|
|
21
|
+
export declare function listenSocketSmart<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof listenSocket>[1], "paramsModify">): {
|
|
22
|
+
callback: (z: SmartCallback<Z>) => void;
|
|
23
|
+
removeCallback: () => boolean;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listenSocket = listenSocket;
|
|
4
|
+
exports.listenSocketFirst = listenSocketFirst;
|
|
5
|
+
exports.listenSocketAll = listenSocketAll;
|
|
6
|
+
exports.listenSocketSmart = listenSocketSmart;
|
|
7
|
+
function listenSocket(e, d) {
|
|
8
|
+
const { stop, addListenClose, status, paramsModify } = d ?? {};
|
|
9
|
+
const { addListen, removeListen, eventClose, removeEventClose } = e;
|
|
10
|
+
let last = null;
|
|
11
|
+
let active = null;
|
|
12
|
+
let resolveWait = null;
|
|
13
|
+
function finish() {
|
|
14
|
+
if (resolveWait) {
|
|
15
|
+
resolveWait();
|
|
16
|
+
resolveWait = null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function removeCallback() {
|
|
20
|
+
if (last) {
|
|
21
|
+
stop?.(last);
|
|
22
|
+
last = null;
|
|
23
|
+
}
|
|
24
|
+
if (active) {
|
|
25
|
+
removeListen(active);
|
|
26
|
+
active = null;
|
|
27
|
+
}
|
|
28
|
+
addListenClose?.removeListen(removeCallback);
|
|
29
|
+
finish();
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
async function callback(z) {
|
|
33
|
+
if (last)
|
|
34
|
+
stop?.(last);
|
|
35
|
+
if (active)
|
|
36
|
+
removeListen(active);
|
|
37
|
+
if (resolveWait) {
|
|
38
|
+
resolveWait();
|
|
39
|
+
resolveWait = null;
|
|
40
|
+
}
|
|
41
|
+
last = z;
|
|
42
|
+
let handler = z;
|
|
43
|
+
if (paramsModify) {
|
|
44
|
+
const orig = handler;
|
|
45
|
+
handler = (...a) => orig(...paramsModify(...a));
|
|
46
|
+
}
|
|
47
|
+
if (status) {
|
|
48
|
+
const wrapped = handler;
|
|
49
|
+
handler = (...a) => {
|
|
50
|
+
if (status())
|
|
51
|
+
wrapped(...a);
|
|
52
|
+
else
|
|
53
|
+
removeCallback();
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const inner = handler;
|
|
57
|
+
active = (...a) => {
|
|
58
|
+
if (a[0] === "___STOP") {
|
|
59
|
+
z(...a);
|
|
60
|
+
if (last) {
|
|
61
|
+
stop?.(last);
|
|
62
|
+
}
|
|
63
|
+
last = null;
|
|
64
|
+
if (active) {
|
|
65
|
+
removeListen(active);
|
|
66
|
+
active = null;
|
|
67
|
+
}
|
|
68
|
+
addListenClose?.removeListen(removeCallback);
|
|
69
|
+
finish();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
inner(...a);
|
|
73
|
+
};
|
|
74
|
+
addListen(active, removeCallback);
|
|
75
|
+
addListenClose?.addListen(removeCallback);
|
|
76
|
+
return new Promise((resolve) => {
|
|
77
|
+
resolveWait = () => { resolve(); };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return { callback, removeCallback };
|
|
81
|
+
}
|
|
82
|
+
function listenSocketFirst(e, options) {
|
|
83
|
+
const r = listenSocket(e, {
|
|
84
|
+
...options,
|
|
85
|
+
paramsModify: ((...args) => [args[0]]),
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
callback: r.callback,
|
|
89
|
+
removeCallback: r.removeCallback,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function listenSocketAll(e, options) {
|
|
93
|
+
const r = listenSocket(e, { ...options });
|
|
94
|
+
return {
|
|
95
|
+
callback: r.callback,
|
|
96
|
+
removeCallback: r.removeCallback,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function listenSocketSmart(e, options) {
|
|
100
|
+
const r = listenSocket(e, { ...options });
|
|
101
|
+
return {
|
|
102
|
+
callback: r.callback,
|
|
103
|
+
removeCallback: r.removeCallback,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { funcListenCallbackBase } from "../events/Listen";
|
|
2
|
+
import type { DeepSocketListen } from "./listen-deep";
|
|
3
|
+
import type { ClientAPI } from "./rpc-client";
|
|
4
|
+
type ClientAutoOptions = {
|
|
5
|
+
readonly mode?: "smart" | "first" | "all";
|
|
6
|
+
readonly status?: () => boolean;
|
|
7
|
+
readonly addListenClose?: ReturnType<typeof funcListenCallbackBase<any>>;
|
|
8
|
+
};
|
|
9
|
+
export type ClientAutoResult<T> = DeepSocketListen<T>;
|
|
10
|
+
export type AutoClientAPI<T> = ClientAPI<DeepSocketListen<T>>;
|
|
11
|
+
export declare function createRpcClientAuto<T>(api: T, options?: ClientAutoOptions): ClientAutoResult<T>;
|
|
12
|
+
export {};
|