@vxrn/utils 1.27.0 → 1.27.1-1789509973946
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/dist/cjs/createMessageSocket.cjs +115 -121
- package/dist/cjs/createMessageSocket.native.cjs +166 -0
- package/dist/cjs/createMessageSocket.native.js +152 -166
- package/dist/cjs/createMessageSocket.native.js.map +1 -1
- package/dist/cjs/detectPackageManager.cjs +76 -72
- package/dist/cjs/detectPackageManager.native.cjs +106 -0
- package/dist/cjs/detectPackageManager.native.js +82 -77
- package/dist/cjs/detectPackageManager.native.js.map +1 -1
- package/dist/cjs/exec.cjs +43 -45
- package/dist/cjs/exec.native.cjs +65 -0
- package/dist/cjs/exec.native.js +48 -49
- package/dist/cjs/exec.native.js.map +1 -1
- package/dist/cjs/index.cjs +9 -11
- package/dist/cjs/index.native.cjs +25 -0
- package/dist/cjs/index.native.js +9 -10
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/mustReplace.cjs +27 -35
- package/dist/cjs/mustReplace.native.cjs +60 -0
- package/dist/cjs/mustReplace.native.js +44 -60
- package/dist/cjs/mustReplace.native.js.map +1 -1
- package/dist/esm/createMessageSocket.mjs +103 -103
- package/dist/esm/createMessageSocket.mjs.map +1 -1
- package/dist/esm/createMessageSocket.native.js +139 -148
- package/dist/esm/createMessageSocket.native.js.map +1 -1
- package/dist/esm/detectPackageManager.mjs +65 -55
- package/dist/esm/detectPackageManager.mjs.map +1 -1
- package/dist/esm/detectPackageManager.native.js +71 -61
- package/dist/esm/detectPackageManager.native.js.map +1 -1
- package/dist/esm/exec.mjs +29 -27
- package/dist/esm/exec.mjs.map +1 -1
- package/dist/esm/exec.native.js +34 -32
- package/dist/esm/exec.native.js.map +1 -1
- package/dist/esm/index.js +11 -7
- package/dist/esm/index.mjs +11 -7
- package/dist/esm/index.native.js +11 -7
- package/dist/esm/mustReplace.mjs +15 -17
- package/dist/esm/mustReplace.mjs.map +1 -1
- package/dist/esm/mustReplace.native.js +32 -43
- package/dist/esm/mustReplace.native.js.map +1 -1
- package/package.json +4 -4
- package/types/createMessageSocket.d.ts +1 -1
- package/types/createMessageSocket.d.ts.map +1 -1
- package/types/detectPackageManager.d.ts.map +1 -1
- package/types/exec.d.ts.map +1 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/index.native.js.map +0 -1
|
@@ -1,108 +1,108 @@
|
|
|
1
1
|
import { WebSocketServer } from "ws";
|
|
2
|
+
|
|
2
3
|
function createMessageSocket() {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
105
|
-
return wss;
|
|
4
|
+
const PROTOCOL_VERSION = 2;
|
|
5
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
6
|
+
const clients = /* @__PURE__ */ new Map();
|
|
7
|
+
let nextClientId = 0;
|
|
8
|
+
function broadcast(broadcasterId, message) {
|
|
9
|
+
const forwarded = JSON.stringify({
|
|
10
|
+
version: PROTOCOL_VERSION,
|
|
11
|
+
method: message.method,
|
|
12
|
+
params: message.params
|
|
13
|
+
});
|
|
14
|
+
for (const [otherId, other] of clients) {
|
|
15
|
+
if (otherId !== broadcasterId) {
|
|
16
|
+
try {
|
|
17
|
+
other.ws.send(forwarded);
|
|
18
|
+
} catch {}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
wss.on("connection", (clientWs, req) => {
|
|
23
|
+
const clientId = `client#${nextClientId++}`;
|
|
24
|
+
const query = Object.fromEntries(new URL(req?.url || "/", "http://localhost").searchParams);
|
|
25
|
+
clients.set(clientId, {
|
|
26
|
+
ws: clientWs,
|
|
27
|
+
query
|
|
28
|
+
});
|
|
29
|
+
const remove = () => clients.delete(clientId);
|
|
30
|
+
clientWs.on("close", remove);
|
|
31
|
+
clientWs.on("error", remove);
|
|
32
|
+
clientWs.on("message", (data, isBinary) => {
|
|
33
|
+
if (isBinary) return;
|
|
34
|
+
let message;
|
|
35
|
+
try {
|
|
36
|
+
message = JSON.parse(data.toString());
|
|
37
|
+
} catch {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (message?.version !== PROTOCOL_VERSION) return;
|
|
41
|
+
try {
|
|
42
|
+
const isBroadcastMsg = typeof message.method === "string" && message.id === void 0 && message.target === void 0;
|
|
43
|
+
const isRequest = typeof message.method === "string" && typeof message.target === "string";
|
|
44
|
+
const isResponse = typeof message.id === "object" && message.id?.requestId !== void 0 && typeof message.id?.clientId === "string" && (message.result !== void 0 || message.error !== void 0);
|
|
45
|
+
if (isBroadcastMsg) {
|
|
46
|
+
broadcast(clientId, message);
|
|
47
|
+
} else if (isRequest) {
|
|
48
|
+
if (message.target === "server") {
|
|
49
|
+
let result;
|
|
50
|
+
if (message.method === "getid") {
|
|
51
|
+
result = clientId;
|
|
52
|
+
} else if (message.method === "getpeers") {
|
|
53
|
+
const peers = {};
|
|
54
|
+
for (const [otherId, other] of clients) {
|
|
55
|
+
if (otherId !== clientId) peers[otherId] = other.query;
|
|
56
|
+
}
|
|
57
|
+
result = peers;
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error(`unknown method: ${message.method}`);
|
|
60
|
+
}
|
|
61
|
+
clientWs.send(JSON.stringify({
|
|
62
|
+
version: PROTOCOL_VERSION,
|
|
63
|
+
result,
|
|
64
|
+
id: message.id
|
|
65
|
+
}));
|
|
66
|
+
} else {
|
|
67
|
+
const target = clients.get(message.target);
|
|
68
|
+
if (!target) {
|
|
69
|
+
throw new Error(`could not find id "${message.target}" while forwarding request`);
|
|
70
|
+
}
|
|
71
|
+
target.ws.send(JSON.stringify({
|
|
72
|
+
version: PROTOCOL_VERSION,
|
|
73
|
+
method: message.method,
|
|
74
|
+
params: message.params,
|
|
75
|
+
id: message.id === void 0 ? void 0 : {
|
|
76
|
+
requestId: message.id,
|
|
77
|
+
clientId
|
|
78
|
+
}
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
} else if (isResponse) {
|
|
82
|
+
if (!message.id) return;
|
|
83
|
+
const target = clients.get(message.id.clientId);
|
|
84
|
+
target?.ws.send(JSON.stringify({
|
|
85
|
+
version: PROTOCOL_VERSION,
|
|
86
|
+
result: message.result,
|
|
87
|
+
error: message.error,
|
|
88
|
+
id: message.id.requestId
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
} catch (err) {
|
|
92
|
+
if (message.id !== void 0) {
|
|
93
|
+
try {
|
|
94
|
+
clientWs.send(JSON.stringify({
|
|
95
|
+
version: PROTOCOL_VERSION,
|
|
96
|
+
error: String(err),
|
|
97
|
+
id: message.id
|
|
98
|
+
}));
|
|
99
|
+
} catch {}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
return wss;
|
|
106
105
|
}
|
|
106
|
+
|
|
107
107
|
export { createMessageSocket };
|
|
108
108
|
//# sourceMappingURL=createMessageSocket.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"createMessageSocket.js","names":[],"sources":["esm/createMessageSocket.js"],"sourcesContent":["import { WebSocketServer } from \"ws\";\nfunction createMessageSocket() {\n const PROTOCOL_VERSION = 2;\n const wss = new WebSocketServer({ noServer: true });\n const clients = /* @__PURE__ */ new Map();\n let nextClientId = 0;\n function broadcast(broadcasterId, message) {\n const forwarded = JSON.stringify({\n version: PROTOCOL_VERSION,\n method: message.method,\n params: message.params\n });\n for (const [otherId, other] of clients) {\n if (otherId !== broadcasterId) {\n try {\n other.ws.send(forwarded);\n } catch {\n }\n }\n }\n }\n wss.on(\"connection\", (clientWs, req) => {\n const clientId = `client#${nextClientId++}`;\n const query = Object.fromEntries(\n new URL(req?.url || \"/\", \"http://localhost\").searchParams\n );\n clients.set(clientId, { ws: clientWs, query });\n const remove = () => clients.delete(clientId);\n clientWs.on(\"close\", remove);\n clientWs.on(\"error\", remove);\n clientWs.on(\"message\", (data, isBinary) => {\n if (isBinary) return;\n let message;\n try {\n message = JSON.parse(data.toString());\n } catch {\n return;\n }\n if (message?.version !== PROTOCOL_VERSION) return;\n try {\n const isBroadcastMsg = typeof message.method === \"string\" && message.id === void 0 && message.target === void 0;\n const isRequest = typeof message.method === \"string\" && typeof message.target === \"string\";\n const isResponse = typeof message.id === \"object\" && message.id?.requestId !== void 0 && typeof message.id?.clientId === \"string\" && (message.result !== void 0 || message.error !== void 0);\n if (isBroadcastMsg) {\n broadcast(clientId, message);\n } else if (isRequest) {\n if (message.target === \"server\") {\n let result;\n if (message.method === \"getid\") {\n result = clientId;\n } else if (message.method === \"getpeers\") {\n const peers = {};\n for (const [otherId, other] of clients) {\n if (otherId !== clientId) peers[otherId] = other.query;\n }\n result = peers;\n } else {\n throw new Error(`unknown method: ${message.method}`);\n }\n clientWs.send(\n JSON.stringify({ version: PROTOCOL_VERSION, result, id: message.id })\n );\n } else {\n const target = clients.get(message.target);\n if (!target) {\n throw new Error(\n `could not find id \"${message.target}\" while forwarding request`\n );\n }\n target.ws.send(\n JSON.stringify({\n version: PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n id: message.id === void 0 ? void 0 : { requestId: message.id, clientId }\n })\n );\n }\n } else if (isResponse) {\n if (!message.id) return;\n const target = clients.get(message.id.clientId);\n target?.ws.send(\n JSON.stringify({\n version: PROTOCOL_VERSION,\n result: message.result,\n error: message.error,\n id: message.id.requestId\n })\n );\n }\n } catch (err) {\n if (message.id !== void 0) {\n try {\n clientWs.send(\n JSON.stringify({\n version: PROTOCOL_VERSION,\n error: String(err),\n id: message.id\n })\n );\n } catch {\n }\n }\n }\n });\n });\n return wss;\n}\nexport {\n createMessageSocket\n};\n//# sourceMappingURL=createMessageSocket.js.map\n"],"mappings":";;;AACA,SAAS,sBAAsB;CAC7B,MAAM,mBAAmB;CACzB,MAAM,MAAM,IAAI,gBAAgB,EAAE,UAAU,KAAK,CAAC;CAClD,MAAM,0BAA0B,IAAI,IAAI;CACxC,IAAI,eAAe;CACnB,SAAS,UAAU,eAAe,SAAS;EACzC,MAAM,YAAY,KAAK,UA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AAK,MAAM,CAAC,SAAS,UAAU,SAAS;QACtC,IAAI,YAAY,UAAU,MAAM,WAAW,MAAM;OACnD;OACA,SAAS;MACX,OAAO;OACL,MAAM,IAAI,MAAM,mBAAmB,QAAQ,QAAQ;MACrD;MACA,SAAS,KACP,KAAK,UAAU;OAAE,SAAS;OAAkB;OAAQ,IAAI,QAAQ;MAAG,CAAC,CACtE;KACF,OAAO;MACL,MAAM,SAAS,QAAQ,IAAI,QAAQ,MAAM;MACzC,IAAI,CAAC,QAAQ;OACX,MAAM,IAAI,MACR,sBAAsB,QAAQ,OAAO,2BACvC;MACF;MACA,OAAO,GAAG,KACR,KAAK,UAAU;OACb,SAAS;OACT,QAAQ,QAAQ;OAChB,QAAQ,QAAQ;OAChB,IAAI,QAAQ,OAAO,KAAK,IAAI,KAAK,IAAI;QAAE,WAAW,QAAQ;QAAI;OAAS;MACzE,CAAC,CACH;KACF;IACF,OAAO,IAAI,YAAY;KACrB,IAAI,CAAC,QAAQ,IAAI;KACjB,MAAM,SAAS,QAAQ,IAAI,QAAQ,GAAG,QAAQ;KAC9C,QAAQ,GAAG,KACT,KAAK,UAAU;MACb,SAAS;MACT,QAAQ,QAAQ;MAChB,OAAO,QAAQ;MACf,IAAI,QAAQ,GAAG;KACjB,CAAC,CACH;IACF;GACF,SAAS,KAAK;IACZ,IAAI,QAAQ,OAAO,KAAK,GAAG;KACzB,IAAI;MACF,SAAS,KACP,KAAK,UAAU;OACb,SAAS;OACT,OAAO,OAAO,GAAG;OACjB,IAAI,QAAQ;MACd,CAAC,CACH;KACF,QAAQ,CACR;IACF;GACF;EACF,CAAC;CACH,CAAC;CACD,OAAO;AACT"}
|
|
@@ -1,154 +1,145 @@
|
|
|
1
1
|
import { WebSocketServer } from "ws";
|
|
2
|
-
function _type_of(obj) {
|
|
3
|
-
"@swc/helpers - typeof";
|
|
4
2
|
|
|
5
|
-
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
6
|
-
}
|
|
7
3
|
function createMessageSocket() {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
} catch (unused) {}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
return wss;
|
|
4
|
+
var PROTOCOL_VERSION = 2;
|
|
5
|
+
var wss = new WebSocketServer({ noServer: true });
|
|
6
|
+
var clients = /* @__PURE__ */ new Map();
|
|
7
|
+
var nextClientId = 0;
|
|
8
|
+
function broadcast(broadcasterId, message) {
|
|
9
|
+
var forwarded = JSON.stringify({
|
|
10
|
+
version: PROTOCOL_VERSION,
|
|
11
|
+
method: message.method,
|
|
12
|
+
params: message.params
|
|
13
|
+
});
|
|
14
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
15
|
+
try {
|
|
16
|
+
for (var _iterator = clients[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
17
|
+
var [otherId, other] = _step.value;
|
|
18
|
+
if (otherId !== broadcasterId) {
|
|
19
|
+
try {
|
|
20
|
+
other.ws.send(forwarded);
|
|
21
|
+
} catch (unused) {}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} catch (err) {
|
|
25
|
+
_didIteratorError = true;
|
|
26
|
+
_iteratorError = err;
|
|
27
|
+
} finally {
|
|
28
|
+
try {
|
|
29
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
30
|
+
_iterator.return();
|
|
31
|
+
}
|
|
32
|
+
} finally {
|
|
33
|
+
if (_didIteratorError) {
|
|
34
|
+
throw _iteratorError;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
wss.on("connection", function(clientWs, req) {
|
|
40
|
+
var clientId = `client#${nextClientId++}`;
|
|
41
|
+
var query = Object.fromEntries(new URL((req === null || req === void 0 ? void 0 : req.url) || "/", "http://localhost").searchParams);
|
|
42
|
+
clients.set(clientId, {
|
|
43
|
+
ws: clientWs,
|
|
44
|
+
query
|
|
45
|
+
});
|
|
46
|
+
var remove = function() {
|
|
47
|
+
return clients.delete(clientId);
|
|
48
|
+
};
|
|
49
|
+
clientWs.on("close", remove);
|
|
50
|
+
clientWs.on("error", remove);
|
|
51
|
+
clientWs.on("message", function(data, isBinary) {
|
|
52
|
+
if (isBinary) return;
|
|
53
|
+
var message;
|
|
54
|
+
try {
|
|
55
|
+
message = JSON.parse(data.toString());
|
|
56
|
+
} catch (unused) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if ((message === null || message === void 0 ? void 0 : message.version) !== PROTOCOL_VERSION) return;
|
|
60
|
+
try {
|
|
61
|
+
var _message_id, _message_id1;
|
|
62
|
+
var isBroadcastMsg = typeof message.method === "string" && message.id === void 0 && message.target === void 0;
|
|
63
|
+
var isRequest = typeof message.method === "string" && typeof message.target === "string";
|
|
64
|
+
var isResponse = typeof message.id === "object" && ((_message_id = message.id) === null || _message_id === void 0 ? void 0 : _message_id.requestId) !== void 0 && typeof ((_message_id1 = message.id) === null || _message_id1 === void 0 ? void 0 : _message_id1.clientId) === "string" && (message.result !== void 0 || message.error !== void 0);
|
|
65
|
+
if (isBroadcastMsg) {
|
|
66
|
+
broadcast(clientId, message);
|
|
67
|
+
} else if (isRequest) {
|
|
68
|
+
if (message.target === "server") {
|
|
69
|
+
var result;
|
|
70
|
+
if (message.method === "getid") {
|
|
71
|
+
result = clientId;
|
|
72
|
+
} else if (message.method === "getpeers") {
|
|
73
|
+
var peers = {};
|
|
74
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
75
|
+
try {
|
|
76
|
+
for (var _iterator = clients[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
77
|
+
var [otherId, other] = _step.value;
|
|
78
|
+
if (otherId !== clientId) peers[otherId] = other.query;
|
|
79
|
+
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
_didIteratorError = true;
|
|
82
|
+
_iteratorError = err;
|
|
83
|
+
} finally {
|
|
84
|
+
try {
|
|
85
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
86
|
+
_iterator.return();
|
|
87
|
+
}
|
|
88
|
+
} finally {
|
|
89
|
+
if (_didIteratorError) {
|
|
90
|
+
throw _iteratorError;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
result = peers;
|
|
95
|
+
} else {
|
|
96
|
+
throw new Error(`unknown method: ${message.method}`);
|
|
97
|
+
}
|
|
98
|
+
clientWs.send(JSON.stringify({
|
|
99
|
+
version: PROTOCOL_VERSION,
|
|
100
|
+
result,
|
|
101
|
+
id: message.id
|
|
102
|
+
}));
|
|
103
|
+
} else {
|
|
104
|
+
var target = clients.get(message.target);
|
|
105
|
+
if (!target) {
|
|
106
|
+
throw new Error(`could not find id "${message.target}" while forwarding request`);
|
|
107
|
+
}
|
|
108
|
+
target.ws.send(JSON.stringify({
|
|
109
|
+
version: PROTOCOL_VERSION,
|
|
110
|
+
method: message.method,
|
|
111
|
+
params: message.params,
|
|
112
|
+
id: message.id === void 0 ? void 0 : {
|
|
113
|
+
requestId: message.id,
|
|
114
|
+
clientId
|
|
115
|
+
}
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
} else if (isResponse) {
|
|
119
|
+
if (!message.id) return;
|
|
120
|
+
var target1 = clients.get(message.id.clientId);
|
|
121
|
+
target1 === null || target1 === void 0 ? void 0 : target1.ws.send(JSON.stringify({
|
|
122
|
+
version: PROTOCOL_VERSION,
|
|
123
|
+
result: message.result,
|
|
124
|
+
error: message.error,
|
|
125
|
+
id: message.id.requestId
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
} catch (err) {
|
|
129
|
+
if (message.id !== void 0) {
|
|
130
|
+
try {
|
|
131
|
+
clientWs.send(JSON.stringify({
|
|
132
|
+
version: PROTOCOL_VERSION,
|
|
133
|
+
error: String(err),
|
|
134
|
+
id: message.id
|
|
135
|
+
}));
|
|
136
|
+
} catch (unused) {}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
return wss;
|
|
152
142
|
}
|
|
143
|
+
|
|
153
144
|
export { createMessageSocket };
|
|
154
145
|
//# sourceMappingURL=createMessageSocket.native.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"createMessageSocket.native.js","names":[],"sources":["esm/createMessageSocket.native.js"],"sourcesContent":["import { WebSocketServer } from \"ws\";\nfunction createMessageSocket() {\n var PROTOCOL_VERSION = 2;\n var wss = new WebSocketServer({\n noServer: true\n });\n var clients = /* @__PURE__ */ new Map();\n var nextClientId = 0;\n function broadcast(broadcasterId, message) {\n var forwarded = JSON.stringify({\n version: PROTOCOL_VERSION,\n method: message.method,\n params: message.params\n });\n var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;\n try {\n for (var _iterator = clients[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {\n var [otherId, other] = _step.value;\n if (otherId !== broadcasterId) {\n try {\n other.ws.send(forwarded);\n } catch (unused) {\n }\n }\n }\n } catch (err) {\n _didIteratorError = true;\n _iteratorError = err;\n } finally {\n try {\n if (!_iteratorNormalCompletion && _iterator.return != null) {\n _iterator.return();\n }\n } finally {\n if (_didIteratorError) {\n throw _iteratorError;\n }\n }\n }\n }\n wss.on(\"connection\", function(clientWs, req) {\n var clientId = `client#${nextClientId++}`;\n var query = Object.fromEntries(new URL((req === null || req === void 0 ? void 0 : req.url) || \"/\", \"http://localhost\").searchParams);\n clients.set(clientId, {\n ws: clientWs,\n query\n });\n var remove = function() {\n return clients.delete(clientId);\n };\n clientWs.on(\"close\", remove);\n clientWs.on(\"error\", remove);\n clientWs.on(\"message\", function(data, isBinary) {\n if (isBinary) return;\n var message;\n try {\n message = JSON.parse(data.toString());\n } catch (unused) {\n return;\n }\n if ((message === null || message === void 0 ? void 0 : message.version) !== PROTOCOL_VERSION) return;\n try {\n var _message_id, _message_id1;\n var isBroadcastMsg = typeof message.method === \"string\" && message.id === void 0 && message.target === void 0;\n var isRequest = typeof message.method === \"string\" && typeof message.target === \"string\";\n var isResponse = typeof message.id === \"object\" && ((_message_id = message.id) === null || _message_id === void 0 ? void 0 : _message_id.requestId) !== void 0 && typeof ((_message_id1 = message.id) === null || _message_id1 === void 0 ? void 0 : _message_id1.clientId) === \"string\" && (message.result !== void 0 || message.error !== void 0);\n if (isBroadcastMsg) {\n broadcast(clientId, message);\n } else if (isRequest) {\n if (message.target === \"server\") {\n var result;\n if (message.method === \"getid\") {\n result = clientId;\n } else if (message.method === \"getpeers\") {\n var peers = {};\n var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;\n try {\n for (var _iterator = clients[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {\n var [otherId, other] = _step.value;\n if (otherId !== clientId) peers[otherId] = other.query;\n }\n } catch (err) {\n _didIteratorError = true;\n _iteratorError = err;\n } finally {\n try {\n if (!_iteratorNormalCompletion && _iterator.return != null) {\n _iterator.return();\n }\n } finally {\n if (_didIteratorError) {\n throw _iteratorError;\n }\n }\n }\n result = peers;\n } else {\n throw new Error(`unknown method: ${message.method}`);\n }\n clientWs.send(JSON.stringify({\n version: PROTOCOL_VERSION,\n result,\n id: message.id\n }));\n } else {\n var target = clients.get(message.target);\n if (!target) {\n throw new Error(`could not find id \"${message.target}\" while forwarding request`);\n }\n target.ws.send(JSON.stringify({\n version: PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n id: message.id === void 0 ? void 0 : {\n requestId: message.id,\n clientId\n }\n }));\n }\n } else if (isResponse) {\n if (!message.id) return;\n var target1 = clients.get(message.id.clientId);\n target1 === null || target1 === void 0 ? void 0 : target1.ws.send(JSON.stringify({\n version: PROTOCOL_VERSION,\n result: message.result,\n error: message.error,\n id: message.id.requestId\n }));\n }\n } catch (err) {\n if (message.id !== void 0) {\n try {\n clientWs.send(JSON.stringify({\n version: PROTOCOL_VERSION,\n error: String(err),\n id: message.id\n }));\n } catch (unused) {\n }\n }\n }\n });\n });\n return wss;\n}\nexport {\n createMessageSocket\n};\n//# sourceMappingURL=createMessageSocket.js.map\n"],"mappings":";;;AACA,SAAS,sBAAsB;CAC7B,IAAI,mBAAmB;CACvB,IAAI,MAAM,IAAI,gBAAgB,EAC5B,UAAU,KACZ,CAAC;CACD,IAAI,0BAA0B,IAAI,IAAI;CACtC,IAAI,eAAe;CACnB,SAAS,UAAU,eAAe,SAAS;EACzC,IAAI,YAAY,KAAK,UAAU;GAC7B,SAAS;GACT,QAAQ,QAAQ;GAChB,QAAQ,QAAQ;EAClB,CAAC;EACD,IAAI,4BAA4B,MAAM,oBAAoB,OAAO,iBAAiB,KAAK;EACvF,IAAI;GACF,KAAK,IAAI,YAAY,QAAQ,OAAO,SAAS,CAAC,GAAG,OAAO,EAAE,6BAA6B,QAAQ,UAAU,KAAK,EAAC,CAAE,OAAO,4BAA4B,MAAM;IACxJ,IAAI,CAAC,SAAS,SAAS,MAAM;IAC7B,IAAI,YAAY,eAAe;KAC7B,IAAI;MACF,MAAM,GAAG,KAAK,SAA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