mcbe-ipc 3.0.0 → 3.0.2
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/LICENSE +1 -1
- package/dist/direct.ipc.d.ts +61 -0
- package/dist/direct.ipc.js +369 -0
- package/dist/ipc.d.ts +60 -94
- package/dist/ipc.js +342 -711
- package/dist/proto.d.ts +84 -0
- package/dist/proto.js +408 -0
- package/package.json +7 -7
- package/dist/ipc.min.js +0 -1
package/LICENSE
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2025 OmniacDev
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
import { PROTO } from './ipc';
|
|
26
|
+
export declare namespace DirectIPC {
|
|
27
|
+
class Connection {
|
|
28
|
+
private readonly _from;
|
|
29
|
+
private readonly _to;
|
|
30
|
+
private readonly _enc;
|
|
31
|
+
private readonly _terminators;
|
|
32
|
+
private MAYBE_ENCRYPT;
|
|
33
|
+
private MAYBE_DECRYPT;
|
|
34
|
+
get from(): string;
|
|
35
|
+
get to(): string;
|
|
36
|
+
constructor(from: string, to: string, encryption: string | false);
|
|
37
|
+
terminate(notify?: boolean): void;
|
|
38
|
+
send<S extends PROTO.Serializable<T>, T>(channel: string, serializer: S & PROTO.Serializable<T>, value: T): void;
|
|
39
|
+
invoke<TS extends PROTO.Serializable<T>, T, RS extends PROTO.Serializable<R>, R>(channel: string, serializer: TS & PROTO.Serializable<T>, value: T, deserializer: RS & PROTO.Serializable<R>): Promise<R>;
|
|
40
|
+
on<S extends PROTO.Serializable<T>, T>(channel: string, deserializer: S & PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
41
|
+
once<S extends PROTO.Serializable<T>, T>(channel: string, deserializer: S & PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
42
|
+
handle<TS extends PROTO.Serializable<T>, T, RS extends PROTO.Serializable<R>, R>(channel: string, deserializer: TS & PROTO.Serializable<T>, serializer: RS & PROTO.Serializable<R>, listener: (value: T) => R): () => void;
|
|
43
|
+
}
|
|
44
|
+
class ConnectionManager {
|
|
45
|
+
private readonly _id;
|
|
46
|
+
private readonly _enc_map;
|
|
47
|
+
private readonly _con_map;
|
|
48
|
+
private readonly _enc_force;
|
|
49
|
+
private MAYBE_ENCRYPT;
|
|
50
|
+
private MAYBE_DECRYPT;
|
|
51
|
+
get id(): string;
|
|
52
|
+
constructor(id: string, force_encryption?: boolean);
|
|
53
|
+
connect(to: string, encrypted?: boolean, timeout?: number): Promise<Connection>;
|
|
54
|
+
send<S extends PROTO.Serializable<T>, T>(channel: string, serializer: S & PROTO.Serializable<T>, value: T): void;
|
|
55
|
+
invoke<TS extends PROTO.Serializable<T>, T, RS extends PROTO.Serializable<R>, R>(channel: string, serializer: TS & PROTO.Serializable<T>, value: T, deserializer: RS & PROTO.Serializable<R>): Promise<R>[];
|
|
56
|
+
on<S extends PROTO.Serializable<T>, T>(channel: string, deserializer: S & PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
57
|
+
once<S extends PROTO.Serializable<T>, T>(channel: string, deserializer: S & PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
58
|
+
handle<TS extends PROTO.Serializable<T>, T, RS extends PROTO.Serializable<R>, R>(channel: string, deserializer: TS & PROTO.Serializable<T>, serializer: RS & PROTO.Serializable<R>, listener: (value: T) => R): () => void;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export default DirectIPC;
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2025 OmniacDev
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
import { system } from '@minecraft/server';
|
|
26
|
+
import { NET, PROTO } from './ipc';
|
|
27
|
+
var CRYPTO;
|
|
28
|
+
(function (CRYPTO) {
|
|
29
|
+
CRYPTO.PRIME = 19893121;
|
|
30
|
+
CRYPTO.MOD = 341;
|
|
31
|
+
const to_HEX = (n) => n.toString(16).toUpperCase();
|
|
32
|
+
const to_NUM = (h) => parseInt(h, 16);
|
|
33
|
+
function* mod_exp(base, exp, mod) {
|
|
34
|
+
let result = 1;
|
|
35
|
+
let b = base % mod;
|
|
36
|
+
for (let e = exp; e > 0; e = Math.floor(e / 2)) {
|
|
37
|
+
if (e % 2 === 1) {
|
|
38
|
+
result = (result * b) % mod;
|
|
39
|
+
}
|
|
40
|
+
b = (b * b) % mod;
|
|
41
|
+
yield;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
function make_secret(mod = CRYPTO.MOD) {
|
|
46
|
+
return Math.floor(Math.random() * (mod - 1)) + 1;
|
|
47
|
+
}
|
|
48
|
+
CRYPTO.make_secret = make_secret;
|
|
49
|
+
function* make_public(secret, mod = CRYPTO.MOD, prime = CRYPTO.PRIME) {
|
|
50
|
+
return to_HEX(yield* mod_exp(mod, secret, prime));
|
|
51
|
+
}
|
|
52
|
+
CRYPTO.make_public = make_public;
|
|
53
|
+
function* make_shared(secret, other, prime = CRYPTO.PRIME) {
|
|
54
|
+
return to_HEX(yield* mod_exp(to_NUM(other), secret, prime));
|
|
55
|
+
}
|
|
56
|
+
CRYPTO.make_shared = make_shared;
|
|
57
|
+
function* encrypt(raw, key) {
|
|
58
|
+
let encrypted = new Uint8Array(raw.length);
|
|
59
|
+
for (let i = 0; i < raw.length; i++) {
|
|
60
|
+
encrypted[i] = raw[i] ^ key.charCodeAt(i % key.length);
|
|
61
|
+
yield;
|
|
62
|
+
}
|
|
63
|
+
return encrypted;
|
|
64
|
+
}
|
|
65
|
+
CRYPTO.encrypt = encrypt;
|
|
66
|
+
function* decrypt(encrypted, key) {
|
|
67
|
+
let decrypted = new Uint8Array(encrypted.length);
|
|
68
|
+
for (let i = 0; i < encrypted.length; i++) {
|
|
69
|
+
decrypted[i] = encrypted[i] ^ key.charCodeAt(i % key.length);
|
|
70
|
+
yield;
|
|
71
|
+
}
|
|
72
|
+
return decrypted;
|
|
73
|
+
}
|
|
74
|
+
CRYPTO.decrypt = decrypt;
|
|
75
|
+
})(CRYPTO || (CRYPTO = {}));
|
|
76
|
+
export var DirectIPC;
|
|
77
|
+
(function (DirectIPC) {
|
|
78
|
+
const ConnectionSerializer = PROTO.Object({
|
|
79
|
+
from: PROTO.String,
|
|
80
|
+
bytes: PROTO.UInt8Array
|
|
81
|
+
});
|
|
82
|
+
const HandshakeSynchronizeSerializer = PROTO.Object({
|
|
83
|
+
from: PROTO.String,
|
|
84
|
+
encryption_enabled: PROTO.Boolean,
|
|
85
|
+
encryption_public_key: PROTO.String,
|
|
86
|
+
encryption_prime: PROTO.UVarInt32,
|
|
87
|
+
encryption_modulus: PROTO.UVarInt32
|
|
88
|
+
});
|
|
89
|
+
const HandshakeAcknowledgeSerializer = PROTO.Object({
|
|
90
|
+
from: PROTO.String,
|
|
91
|
+
encryption_enabled: PROTO.Boolean,
|
|
92
|
+
encryption_public_key: PROTO.String
|
|
93
|
+
});
|
|
94
|
+
class Connection {
|
|
95
|
+
*MAYBE_ENCRYPT(bytes) {
|
|
96
|
+
return this._enc !== false ? yield* CRYPTO.encrypt(bytes, this._enc) : bytes;
|
|
97
|
+
}
|
|
98
|
+
*MAYBE_DECRYPT(bytes) {
|
|
99
|
+
return this._enc !== false ? yield* CRYPTO.decrypt(bytes, this._enc) : bytes;
|
|
100
|
+
}
|
|
101
|
+
get from() {
|
|
102
|
+
return this._from;
|
|
103
|
+
}
|
|
104
|
+
get to() {
|
|
105
|
+
return this._to;
|
|
106
|
+
}
|
|
107
|
+
constructor(from, to, encryption) {
|
|
108
|
+
this._from = from;
|
|
109
|
+
this._to = to;
|
|
110
|
+
this._enc = encryption;
|
|
111
|
+
this._terminators = new Array();
|
|
112
|
+
}
|
|
113
|
+
terminate(notify = true) {
|
|
114
|
+
const $ = this;
|
|
115
|
+
$._terminators.forEach(terminate => terminate());
|
|
116
|
+
$._terminators.length = 0;
|
|
117
|
+
if (notify) {
|
|
118
|
+
system.runJob(NET.emit(`ipc:${$._to}:terminate`, PROTO.String, $._from));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
send(channel, serializer, value) {
|
|
122
|
+
const $ = this;
|
|
123
|
+
system.runJob((function* () {
|
|
124
|
+
const stream = new PROTO.ByteQueue();
|
|
125
|
+
yield* serializer.serialize(value, stream);
|
|
126
|
+
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array());
|
|
127
|
+
yield* NET.emit(`ipc:${$._to}:${channel}:send`, ConnectionSerializer, {
|
|
128
|
+
from: $._from,
|
|
129
|
+
bytes
|
|
130
|
+
});
|
|
131
|
+
})());
|
|
132
|
+
}
|
|
133
|
+
invoke(channel, serializer, value, deserializer) {
|
|
134
|
+
const $ = this;
|
|
135
|
+
system.runJob((function* () {
|
|
136
|
+
const stream = new PROTO.ByteQueue();
|
|
137
|
+
yield* serializer.serialize(value, stream);
|
|
138
|
+
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array());
|
|
139
|
+
yield* NET.emit(`ipc:${$._to}:${channel}:invoke`, ConnectionSerializer, {
|
|
140
|
+
from: $._from,
|
|
141
|
+
bytes
|
|
142
|
+
});
|
|
143
|
+
})());
|
|
144
|
+
return new Promise(resolve => {
|
|
145
|
+
const terminate = NET.listen(`ipc:${$._from}:${channel}:handle`, ConnectionSerializer, function* (data) {
|
|
146
|
+
if (data.from === $._to) {
|
|
147
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
148
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
149
|
+
const value = yield* deserializer.deserialize(stream);
|
|
150
|
+
resolve(value);
|
|
151
|
+
terminate();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
on(channel, deserializer, listener) {
|
|
157
|
+
const $ = this;
|
|
158
|
+
const terminate = NET.listen(`ipc:${$._from}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
159
|
+
if (data.from === $._to) {
|
|
160
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
161
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
162
|
+
const value = yield* deserializer.deserialize(stream);
|
|
163
|
+
listener(value);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
$._terminators.push(terminate);
|
|
167
|
+
return terminate;
|
|
168
|
+
}
|
|
169
|
+
once(channel, deserializer, listener) {
|
|
170
|
+
const $ = this;
|
|
171
|
+
const terminate = NET.listen(`ipc:${$._from}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
172
|
+
if (data.from === $._to) {
|
|
173
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
174
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
175
|
+
const value = yield* deserializer.deserialize(stream);
|
|
176
|
+
listener(value);
|
|
177
|
+
terminate();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
$._terminators.push(terminate);
|
|
181
|
+
return terminate;
|
|
182
|
+
}
|
|
183
|
+
handle(channel, deserializer, serializer, listener) {
|
|
184
|
+
const $ = this;
|
|
185
|
+
const terminate = NET.listen(`ipc:${$._from}:${channel}:invoke`, ConnectionSerializer, function* (data) {
|
|
186
|
+
if (data.from === $._to) {
|
|
187
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
188
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
189
|
+
const value = yield* deserializer.deserialize(stream);
|
|
190
|
+
const result = listener(value);
|
|
191
|
+
const return_stream = new PROTO.ByteQueue();
|
|
192
|
+
yield* serializer.serialize(result, return_stream);
|
|
193
|
+
const return_bytes = yield* $.MAYBE_ENCRYPT(return_stream.to_uint8array());
|
|
194
|
+
yield* NET.emit(`ipc:${$._to}:${channel}:handle`, ConnectionSerializer, {
|
|
195
|
+
from: $._from,
|
|
196
|
+
bytes: return_bytes
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
$._terminators.push(terminate);
|
|
201
|
+
return terminate;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
DirectIPC.Connection = Connection;
|
|
205
|
+
class ConnectionManager {
|
|
206
|
+
*MAYBE_ENCRYPT(bytes, encryption) {
|
|
207
|
+
return encryption !== false ? yield* CRYPTO.encrypt(bytes, encryption) : bytes;
|
|
208
|
+
}
|
|
209
|
+
*MAYBE_DECRYPT(bytes, encryption) {
|
|
210
|
+
return encryption !== false ? yield* CRYPTO.decrypt(bytes, encryption) : bytes;
|
|
211
|
+
}
|
|
212
|
+
get id() {
|
|
213
|
+
return this._id;
|
|
214
|
+
}
|
|
215
|
+
constructor(id, force_encryption = false) {
|
|
216
|
+
const $ = this;
|
|
217
|
+
this._id = id;
|
|
218
|
+
this._enc_map = new Map();
|
|
219
|
+
this._con_map = new Map();
|
|
220
|
+
this._enc_force = force_encryption;
|
|
221
|
+
NET.listen(`ipc:${this._id}:handshake:synchronize`, HandshakeSynchronizeSerializer, function* (data) {
|
|
222
|
+
const secret = CRYPTO.make_secret(data.encryption_modulus);
|
|
223
|
+
const public_key = yield* CRYPTO.make_public(secret, data.encryption_modulus, data.encryption_prime);
|
|
224
|
+
const enc = data.encryption_enabled || $._enc_force
|
|
225
|
+
? yield* CRYPTO.make_shared(secret, data.encryption_public_key, data.encryption_prime)
|
|
226
|
+
: false;
|
|
227
|
+
$._enc_map.set(data.from, enc);
|
|
228
|
+
yield* NET.emit(`ipc:${data.from}:handshake:acknowledge`, HandshakeAcknowledgeSerializer, {
|
|
229
|
+
from: $._id,
|
|
230
|
+
encryption_public_key: public_key,
|
|
231
|
+
encryption_enabled: $._enc_force
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
NET.listen(`ipc:${this._id}:terminate`, PROTO.String, function* (value) {
|
|
235
|
+
$._enc_map.delete(value);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
connect(to, encrypted = false, timeout = 20) {
|
|
239
|
+
const $ = this;
|
|
240
|
+
return new Promise((resolve, reject) => {
|
|
241
|
+
const con = this._con_map.get(to);
|
|
242
|
+
if (con !== undefined) {
|
|
243
|
+
con.terminate(false);
|
|
244
|
+
resolve(con);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
const secret = CRYPTO.make_secret();
|
|
248
|
+
system.runJob((function* () {
|
|
249
|
+
const public_key = yield* CRYPTO.make_public(secret);
|
|
250
|
+
yield* NET.emit(`ipc:${to}:handshake:synchronize`, HandshakeSynchronizeSerializer, {
|
|
251
|
+
from: $._id,
|
|
252
|
+
encryption_enabled: encrypted,
|
|
253
|
+
encryption_public_key: public_key,
|
|
254
|
+
encryption_prime: CRYPTO.PRIME,
|
|
255
|
+
encryption_modulus: CRYPTO.MOD
|
|
256
|
+
});
|
|
257
|
+
})());
|
|
258
|
+
function clear() {
|
|
259
|
+
terminate();
|
|
260
|
+
system.clearRun(timeout_handle);
|
|
261
|
+
}
|
|
262
|
+
const timeout_handle = system.runTimeout(() => {
|
|
263
|
+
reject();
|
|
264
|
+
clear();
|
|
265
|
+
}, timeout);
|
|
266
|
+
const terminate = NET.listen(`ipc:${this._id}:handshake:acknowledge`, HandshakeAcknowledgeSerializer, function* (data) {
|
|
267
|
+
if (data.from === to) {
|
|
268
|
+
const enc = data.encryption_enabled || encrypted
|
|
269
|
+
? yield* CRYPTO.make_shared(secret, data.encryption_public_key)
|
|
270
|
+
: false;
|
|
271
|
+
const new_con = new Connection($._id, to, enc);
|
|
272
|
+
$._con_map.set(to, new_con);
|
|
273
|
+
resolve(new_con);
|
|
274
|
+
clear();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
send(channel, serializer, value) {
|
|
281
|
+
const $ = this;
|
|
282
|
+
system.runJob((function* () {
|
|
283
|
+
for (const [key, enc] of $._enc_map) {
|
|
284
|
+
const stream = new PROTO.ByteQueue();
|
|
285
|
+
yield* serializer.serialize(value, stream);
|
|
286
|
+
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array(), enc);
|
|
287
|
+
yield* NET.emit(`ipc:${key}:${channel}:send`, ConnectionSerializer, {
|
|
288
|
+
from: $._id,
|
|
289
|
+
bytes
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
})());
|
|
293
|
+
}
|
|
294
|
+
invoke(channel, serializer, value, deserializer) {
|
|
295
|
+
const $ = this;
|
|
296
|
+
const promises = [];
|
|
297
|
+
for (const [key, enc] of $._enc_map) {
|
|
298
|
+
system.runJob((function* () {
|
|
299
|
+
const stream = new PROTO.ByteQueue();
|
|
300
|
+
yield* serializer.serialize(value, stream);
|
|
301
|
+
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array(), enc);
|
|
302
|
+
yield* NET.emit(`ipc:${key}:${channel}:invoke`, ConnectionSerializer, {
|
|
303
|
+
from: $._id,
|
|
304
|
+
bytes
|
|
305
|
+
});
|
|
306
|
+
})());
|
|
307
|
+
promises.push(new Promise(resolve => {
|
|
308
|
+
const terminate = NET.listen(`ipc:${$._id}:${channel}:handle`, ConnectionSerializer, function* (data) {
|
|
309
|
+
if (data.from === key) {
|
|
310
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
311
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
312
|
+
const value = yield* deserializer.deserialize(stream);
|
|
313
|
+
resolve(value);
|
|
314
|
+
terminate();
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}));
|
|
318
|
+
}
|
|
319
|
+
return promises;
|
|
320
|
+
}
|
|
321
|
+
on(channel, deserializer, listener) {
|
|
322
|
+
const $ = this;
|
|
323
|
+
return NET.listen(`ipc:${$._id}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
324
|
+
const enc = $._enc_map.get(data.from);
|
|
325
|
+
if (enc !== undefined) {
|
|
326
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
327
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
328
|
+
const value = yield* deserializer.deserialize(stream);
|
|
329
|
+
listener(value);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
once(channel, deserializer, listener) {
|
|
334
|
+
const $ = this;
|
|
335
|
+
const terminate = NET.listen(`ipc:${$._id}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
336
|
+
const enc = $._enc_map.get(data.from);
|
|
337
|
+
if (enc !== undefined) {
|
|
338
|
+
const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
339
|
+
const stream = PROTO.ByteQueue.from_uint8array(bytes);
|
|
340
|
+
const value = yield* deserializer.deserialize(stream);
|
|
341
|
+
listener(value);
|
|
342
|
+
terminate();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
return terminate;
|
|
346
|
+
}
|
|
347
|
+
handle(channel, deserializer, serializer, listener) {
|
|
348
|
+
const $ = this;
|
|
349
|
+
return NET.listen(`ipc:${$._id}:${channel}:invoke`, ConnectionSerializer, function* (data) {
|
|
350
|
+
const enc = $._enc_map.get(data.from);
|
|
351
|
+
if (enc !== undefined) {
|
|
352
|
+
const input_bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
353
|
+
const input_stream = PROTO.ByteQueue.from_uint8array(input_bytes);
|
|
354
|
+
const input_value = yield* deserializer.deserialize(input_stream);
|
|
355
|
+
const result = listener(input_value);
|
|
356
|
+
const output_stream = new PROTO.ByteQueue();
|
|
357
|
+
yield* serializer.serialize(result, output_stream);
|
|
358
|
+
const output_bytes = yield* $.MAYBE_ENCRYPT(output_stream.to_uint8array(), enc);
|
|
359
|
+
yield* NET.emit(`ipc:${data.from}:${channel}:handle`, ConnectionSerializer, {
|
|
360
|
+
from: $._id,
|
|
361
|
+
bytes: output_bytes
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
DirectIPC.ConnectionManager = ConnectionManager;
|
|
368
|
+
})(DirectIPC || (DirectIPC = {}));
|
|
369
|
+
export default DirectIPC;
|
package/dist/ipc.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @license
|
|
3
3
|
* MIT License
|
|
4
4
|
*
|
|
5
|
-
* Copyright (c)
|
|
5
|
+
* Copyright (c) 2025 OmniacDev
|
|
6
6
|
*
|
|
7
7
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
8
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,116 +22,82 @@
|
|
|
22
22
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
23
|
* SOFTWARE.
|
|
24
24
|
*/
|
|
25
|
-
export declare namespace
|
|
26
|
-
|
|
25
|
+
export declare namespace PROTO {
|
|
26
|
+
interface Serializable<T> {
|
|
27
|
+
serialize(value: T, stream: ByteQueue): Generator<void, void, void>;
|
|
28
|
+
deserialize(stream: ByteQueue): Generator<void, T, void>;
|
|
29
|
+
}
|
|
30
|
+
class ByteQueue {
|
|
27
31
|
private _buffer;
|
|
28
32
|
private _data_view;
|
|
29
33
|
private _length;
|
|
30
34
|
private _offset;
|
|
31
|
-
|
|
35
|
+
get end(): number;
|
|
36
|
+
get front(): number;
|
|
37
|
+
get data_view(): DataView;
|
|
32
38
|
constructor(size?: number);
|
|
33
39
|
write(...values: number[]): void;
|
|
34
40
|
read(amount?: number): number[];
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
write_uint16(value: number): void;
|
|
38
|
-
read_uint16(): number | undefined;
|
|
39
|
-
write_uint32(value: number): void;
|
|
40
|
-
read_uint32(): number | undefined;
|
|
41
|
-
write_int8(value: number): void;
|
|
42
|
-
read_int8(): number | undefined;
|
|
43
|
-
write_int16(value: number): void;
|
|
44
|
-
read_int16(): number | undefined;
|
|
45
|
-
write_int32(value: number): void;
|
|
46
|
-
read_int32(): number | undefined;
|
|
47
|
-
write_f32(value: number): void;
|
|
48
|
-
read_f32(): number | undefined;
|
|
49
|
-
write_f64(value: number): void;
|
|
50
|
-
read_f64(): number | undefined;
|
|
51
|
-
private _ensure_capacity;
|
|
52
|
-
static from_uint8array(array: Uint8Array): ByteArray;
|
|
41
|
+
ensure_capacity(size: number): void;
|
|
42
|
+
static from_uint8array(array: Uint8Array): ByteQueue;
|
|
53
43
|
to_uint8array(): Uint8Array;
|
|
54
44
|
}
|
|
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
|
-
|
|
45
|
+
namespace MIPS {
|
|
46
|
+
function serialize(byte_queue: PROTO.ByteQueue): Generator<void, string, void>;
|
|
47
|
+
function deserialize(str: string): Generator<void, PROTO.ByteQueue, void>;
|
|
48
|
+
}
|
|
49
|
+
const Void: PROTO.Serializable<void>;
|
|
50
|
+
const Null: PROTO.Serializable<null>;
|
|
51
|
+
const Undefined: PROTO.Serializable<undefined>;
|
|
52
|
+
const Int8: PROTO.Serializable<number>;
|
|
53
|
+
const Int16: PROTO.Serializable<number>;
|
|
54
|
+
const Int32: PROTO.Serializable<number>;
|
|
55
|
+
const UInt8: PROTO.Serializable<number>;
|
|
56
|
+
const UInt16: PROTO.Serializable<number>;
|
|
57
|
+
const UInt32: PROTO.Serializable<number>;
|
|
58
|
+
const UVarInt32: PROTO.Serializable<number>;
|
|
59
|
+
const Float32: PROTO.Serializable<number>;
|
|
60
|
+
const Float64: PROTO.Serializable<number>;
|
|
61
|
+
const String: PROTO.Serializable<string>;
|
|
62
|
+
const Boolean: PROTO.Serializable<boolean>;
|
|
63
|
+
const UInt8Array: PROTO.Serializable<Uint8Array>;
|
|
64
|
+
const Date: PROTO.Serializable<Date>;
|
|
65
|
+
function Object<T extends object>(obj: {
|
|
66
|
+
[K in keyof T]: PROTO.Serializable<T[K]>;
|
|
67
|
+
}): PROTO.Serializable<T>;
|
|
68
|
+
function Array<T>(value: PROTO.Serializable<T>): PROTO.Serializable<T[]>;
|
|
69
|
+
function Tuple<T extends any[]>(...values: {
|
|
70
|
+
[K in keyof T]: PROTO.Serializable<T[K]>;
|
|
71
|
+
}): PROTO.Serializable<T>;
|
|
72
|
+
function Optional<T>(value: PROTO.Serializable<T>): PROTO.Serializable<T | undefined>;
|
|
73
|
+
function Map<K, V>(key: PROTO.Serializable<K>, value: PROTO.Serializable<V>): PROTO.Serializable<Map<K, V>>;
|
|
74
|
+
function Set<V>(value: PROTO.Serializable<V>): PROTO.Serializable<Set<V>>;
|
|
75
|
+
type Endpoint = string;
|
|
76
|
+
type Header = {
|
|
77
|
+
guid: string;
|
|
78
|
+
encoding: string;
|
|
79
|
+
index: number;
|
|
80
|
+
final: boolean;
|
|
81
|
+
};
|
|
82
|
+
const Endpoint: PROTO.Serializable<Endpoint>;
|
|
83
|
+
const Header: PROTO.Serializable<Header>;
|
|
82
84
|
}
|
|
83
85
|
export declare namespace NET {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
import ByteArray = SERDE.ByteArray;
|
|
89
|
-
function emit<T>(endpoint: string, serializer: Serializable<T>, value: T): Generator<void, void, void>;
|
|
90
|
-
function listen<T>(endpoint: string, serializer: Serializable<T>, callback: (value: T) => Generator<void, void, void>): () => void;
|
|
86
|
+
function serialize(byte_queue: PROTO.ByteQueue, max_size?: number): Generator<void, string[], void>;
|
|
87
|
+
function deserialize(strings: string[]): Generator<void, PROTO.ByteQueue, void>;
|
|
88
|
+
function emit<S extends PROTO.Serializable<T>, T>(endpoint: string, serializer: S & PROTO.Serializable<T>, value: T): Generator<void, void, void>;
|
|
89
|
+
function listen<T, S extends PROTO.Serializable<T>>(endpoint: string, serializer: S & PROTO.Serializable<T>, callback: (value: T) => Generator<void, void, void>): () => void;
|
|
91
90
|
}
|
|
92
91
|
export declare namespace IPC {
|
|
93
|
-
class Connection {
|
|
94
|
-
private readonly _from;
|
|
95
|
-
private readonly _to;
|
|
96
|
-
private readonly _enc;
|
|
97
|
-
private readonly _terminators;
|
|
98
|
-
private MAYBE_ENCRYPT;
|
|
99
|
-
private MAYBE_DECRYPT;
|
|
100
|
-
get from(): string;
|
|
101
|
-
get to(): string;
|
|
102
|
-
constructor(from: string, to: string, encryption: string | false);
|
|
103
|
-
terminate(notify?: boolean): void;
|
|
104
|
-
send<T>(channel: string, serializer: NET.Serializable<T>, value: T): void;
|
|
105
|
-
invoke<T, R>(channel: string, serializer: NET.Serializable<T>, value: T, deserializer: NET.Serializable<R>): Promise<R>;
|
|
106
|
-
on<T>(channel: string, deserializer: NET.Serializable<T>, listener: (value: T) => void): () => void;
|
|
107
|
-
once<T>(channel: string, deserializer: NET.Serializable<T>, listener: (value: T) => void): () => void;
|
|
108
|
-
handle<T, R>(channel: string, deserializer: NET.Serializable<T>, serializer: NET.Serializable<R>, listener: (value: T) => R): () => void;
|
|
109
|
-
}
|
|
110
|
-
class ConnectionManager {
|
|
111
|
-
private readonly _id;
|
|
112
|
-
private readonly _enc_map;
|
|
113
|
-
private readonly _con_map;
|
|
114
|
-
private readonly _enc_force;
|
|
115
|
-
private MAYBE_ENCRYPT;
|
|
116
|
-
private MAYBE_DECRYPT;
|
|
117
|
-
get id(): string;
|
|
118
|
-
constructor(id: string, force_encryption?: boolean);
|
|
119
|
-
connect(to: string, encrypted?: boolean, timeout?: number): Promise<Connection>;
|
|
120
|
-
send<T>(channel: string, serializer: NET.Serializable<T>, value: T): void;
|
|
121
|
-
invoke<T, R>(channel: string, serializer: NET.Serializable<T>, value: T, deserializer: NET.Serializable<R>): Promise<R>[];
|
|
122
|
-
on<T>(channel: string, deserializer: NET.Serializable<T>, listener: (value: T) => void): () => void;
|
|
123
|
-
once<T>(channel: string, deserializer: NET.Serializable<T>, listener: (value: T) => void): () => void;
|
|
124
|
-
handle<T, R>(channel: string, deserializer: NET.Serializable<T>, serializer: NET.Serializable<R>, listener: (value: T) => R): () => void;
|
|
125
|
-
}
|
|
126
92
|
/** Sends a message with `args` to `channel` */
|
|
127
|
-
function send<T>(channel: string, serializer:
|
|
93
|
+
function send<S extends PROTO.Serializable<T>, T>(channel: string, serializer: S & PROTO.Serializable<T>, value: T): void;
|
|
128
94
|
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
129
|
-
function invoke<T, R>(channel: string, serializer:
|
|
95
|
+
function invoke<TS extends PROTO.Serializable<T>, T, RS extends PROTO.Serializable<R>, R>(channel: string, serializer: TS & PROTO.Serializable<T>, value: T, deserializer: RS & PROTO.Serializable<R>): Promise<R>;
|
|
130
96
|
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
131
|
-
function on<T>(channel: string, deserializer:
|
|
97
|
+
function on<S extends PROTO.Serializable<T>, T>(channel: string, deserializer: S & PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
132
98
|
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
133
|
-
function once<T>(channel: string, deserializer:
|
|
99
|
+
function once<S extends PROTO.Serializable<T>, T>(channel: string, deserializer: S & PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
134
100
|
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
135
|
-
function handle<T, R>(channel: string, deserializer:
|
|
101
|
+
function handle<TS extends PROTO.Serializable<T>, T, RS extends PROTO.Serializable<R>, R>(channel: string, deserializer: TS & PROTO.Serializable<T>, serializer: RS & PROTO.Serializable<R>, listener: (value: T) => R): () => void;
|
|
136
102
|
}
|
|
137
103
|
export default IPC;
|