mcbe-ipc 2.0.1 → 3.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/LICENSE +1 -1
- package/README.md +1 -71
- package/dist/direct.ipc.d.ts +61 -0
- package/dist/direct.ipc.js +369 -0
- package/dist/ipc.d.ts +69 -56
- package/dist/ipc.js +492 -426
- package/dist/proto.d.ts +82 -0
- package/dist/proto.js +396 -0
- package/package.json +7 -7
- package/dist/ipc.min.js +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -17,74 +17,4 @@ An IPC[^1] system for MCBE Script API projects
|
|
|
17
17
|
|
|
18
18
|
**TypeScript**
|
|
19
19
|
1. Download `ipc.ts` from the latest [release](https://github.com/OmniacDev/MCBE-IPC/releases/latest)
|
|
20
|
-
2. Copy file into your project
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
### Sending & Receiving
|
|
25
|
-
|
|
26
|
-
`IPC.send()` and `IPC.on()` can be used to send messages or data between packs.
|
|
27
|
-
|
|
28
|
-
_Pack 1_
|
|
29
|
-
```js
|
|
30
|
-
import IPC from 'ipc.js'
|
|
31
|
-
|
|
32
|
-
IPC.on('message_channel', (args) => {
|
|
33
|
-
console.log(`Message: ${args}`)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
IPC.on('data_channel', (args) => {
|
|
37
|
-
console.log(`Data: ${args.example_bool}, ${args.example_number}`)
|
|
38
|
-
})
|
|
39
|
-
```
|
|
40
|
-
_Pack 2_
|
|
41
|
-
```js
|
|
42
|
-
import IPC from 'ipc.js'
|
|
43
|
-
|
|
44
|
-
IPC.send('message_channel', 'Example Message')
|
|
45
|
-
|
|
46
|
-
IPC.send('data_channel', { example_number: 100, example_bool: true })
|
|
47
|
-
```
|
|
48
|
-
_Console Output_
|
|
49
|
-
```
|
|
50
|
-
Message: Example Message
|
|
51
|
-
Data: true, 100
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Requesting & Serving
|
|
55
|
-
|
|
56
|
-
`IPC.invoke()` and `IPC.handle()` can be used to request and serve data between packs.
|
|
57
|
-
|
|
58
|
-
_Pack 1_
|
|
59
|
-
```js
|
|
60
|
-
import IPC from 'ipc.js'
|
|
61
|
-
|
|
62
|
-
IPC.handle('request_channel', (args) => {
|
|
63
|
-
switch (args) {
|
|
64
|
-
case 'status':
|
|
65
|
-
return 'inactive'
|
|
66
|
-
case 'size':
|
|
67
|
-
return 100
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
```
|
|
71
|
-
_Pack 2_
|
|
72
|
-
```js
|
|
73
|
-
import IPC from 'ipc.js'
|
|
74
|
-
|
|
75
|
-
IPC.invoke('request_channel', 'status').then(result => {
|
|
76
|
-
console.log(`Status: ${result}`)
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
IPC.invoke('request_channel', 'size').then(result => {
|
|
80
|
-
console.log(`Size: ${result}`)
|
|
81
|
-
})
|
|
82
|
-
```
|
|
83
|
-
_Console Output_
|
|
84
|
-
```
|
|
85
|
-
Status: inactive
|
|
86
|
-
Size: 100
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
20
|
+
2. Copy file into your project
|
|
@@ -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<T>(channel: string, serializer: PROTO.Serializable<T>, value: T): void;
|
|
39
|
+
invoke<T, R>(channel: string, serializer: PROTO.Serializable<T>, value: T, deserializer: PROTO.Serializable<R>): Promise<R>;
|
|
40
|
+
on<T>(channel: string, deserializer: PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
41
|
+
once<T>(channel: string, deserializer: PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
42
|
+
handle<T, R>(channel: string, deserializer: PROTO.Serializable<T>, serializer: 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<T>(channel: string, serializer: PROTO.Serializable<T>, value: T): void;
|
|
55
|
+
invoke<T, R>(channel: string, serializer: PROTO.Serializable<T>, value: T, deserializer: PROTO.Serializable<R>): Promise<R>[];
|
|
56
|
+
on<T>(channel: string, deserializer: PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
57
|
+
once<T>(channel: string, deserializer: PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
58
|
+
handle<T, R>(channel: string, deserializer: PROTO.Serializable<T>, serializer: 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,67 +22,80 @@
|
|
|
22
22
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
23
|
* SOFTWARE.
|
|
24
24
|
*/
|
|
25
|
-
declare namespace
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export class Connection {
|
|
30
|
-
private readonly _from;
|
|
31
|
-
private readonly _to;
|
|
32
|
-
private readonly _enc;
|
|
33
|
-
private readonly _terminators;
|
|
34
|
-
private MAYBE_ENCRYPT;
|
|
35
|
-
private MAYBE_DECRYPT;
|
|
36
|
-
get from(): string;
|
|
37
|
-
get to(): string;
|
|
38
|
-
constructor(from: string, to: string, encryption: string | false);
|
|
39
|
-
terminate(notify?: boolean): void;
|
|
40
|
-
send<T extends any[]>(channel: string, ...args: T): void;
|
|
41
|
-
invoke<T extends any[], R extends any>(channel: string, ...args: T): Promise<R>;
|
|
42
|
-
on<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
43
|
-
once<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
44
|
-
handle<T extends any[], R extends any>(channel: string, listener: (...args: T) => R): () => void;
|
|
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>;
|
|
45
29
|
}
|
|
46
|
-
|
|
47
|
-
private
|
|
48
|
-
private
|
|
49
|
-
private
|
|
50
|
-
private
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
get
|
|
54
|
-
constructor(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
handle<T extends any[], R extends any>(channel: string, listener: (...args: T) => R): () => void;
|
|
30
|
+
class ByteQueue {
|
|
31
|
+
private _buffer;
|
|
32
|
+
private _data_view;
|
|
33
|
+
private _length;
|
|
34
|
+
private _offset;
|
|
35
|
+
get end(): number;
|
|
36
|
+
get front(): number;
|
|
37
|
+
get data_view(): DataView;
|
|
38
|
+
constructor(size?: number);
|
|
39
|
+
write(...values: number[]): void;
|
|
40
|
+
read(amount?: number): number[];
|
|
41
|
+
ensure_capacity(size: number): void;
|
|
42
|
+
static from_uint8array(array: Uint8Array): ByteQueue;
|
|
43
|
+
to_uint8array(): Uint8Array;
|
|
61
44
|
}
|
|
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 Int8: PROTO.Serializable<number>;
|
|
51
|
+
const Int16: PROTO.Serializable<number>;
|
|
52
|
+
const Int32: PROTO.Serializable<number>;
|
|
53
|
+
const UInt8: PROTO.Serializable<number>;
|
|
54
|
+
const UInt16: PROTO.Serializable<number>;
|
|
55
|
+
const UInt32: PROTO.Serializable<number>;
|
|
56
|
+
const UVarInt32: PROTO.Serializable<number>;
|
|
57
|
+
const Float32: PROTO.Serializable<number>;
|
|
58
|
+
const Float64: PROTO.Serializable<number>;
|
|
59
|
+
const String: PROTO.Serializable<string>;
|
|
60
|
+
const Boolean: PROTO.Serializable<boolean>;
|
|
61
|
+
const UInt8Array: PROTO.Serializable<Uint8Array>;
|
|
62
|
+
const Date: PROTO.Serializable<Date>;
|
|
63
|
+
function Object<T extends object>(obj: {
|
|
64
|
+
[K in keyof T]: PROTO.Serializable<T[K]>;
|
|
65
|
+
}): PROTO.Serializable<T>;
|
|
66
|
+
function Array<T>(value: PROTO.Serializable<T>): PROTO.Serializable<T[]>;
|
|
67
|
+
function Tuple<T extends any[]>(...values: {
|
|
68
|
+
[K in keyof T]: PROTO.Serializable<T[K]>;
|
|
69
|
+
}): PROTO.Serializable<T>;
|
|
70
|
+
function Optional<T>(value: PROTO.Serializable<T>): PROTO.Serializable<T | undefined>;
|
|
71
|
+
function Map<K, V>(key: PROTO.Serializable<K>, value: PROTO.Serializable<V>): PROTO.Serializable<Map<K, V>>;
|
|
72
|
+
function Set<V>(value: PROTO.Serializable<V>): PROTO.Serializable<Set<V>>;
|
|
73
|
+
type Endpoint = string;
|
|
74
|
+
type Header = {
|
|
75
|
+
guid: string;
|
|
76
|
+
encoding: string;
|
|
77
|
+
index: number;
|
|
78
|
+
final: boolean;
|
|
79
|
+
};
|
|
80
|
+
const Endpoint: PROTO.Serializable<Endpoint>;
|
|
81
|
+
const Header: PROTO.Serializable<Header>;
|
|
82
|
+
}
|
|
83
|
+
export declare namespace NET {
|
|
84
|
+
function serialize(byte_queue: PROTO.ByteQueue, max_size?: number): Generator<void, string[], void>;
|
|
85
|
+
function deserialize(strings: string[]): Generator<void, PROTO.ByteQueue, void>;
|
|
86
|
+
function emit<T>(endpoint: string, serializer: PROTO.Serializable<T>, value: T): Generator<void, void, void>;
|
|
87
|
+
function listen<T>(endpoint: string, serializer: PROTO.Serializable<T>, callback: (value: T) => Generator<void, void, void>): () => void;
|
|
88
|
+
}
|
|
89
|
+
export declare namespace IPC {
|
|
62
90
|
/** Sends a message with `args` to `channel` */
|
|
63
|
-
|
|
64
|
-
/** Sends a message with `args` to `channel` */
|
|
65
|
-
export function send<T extends any[]>(channel: string, ...args: T): void;
|
|
66
|
-
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
67
|
-
export function invoke<C extends keyof (HandleTypes & InvokeTypes)>(channel: C, ...args: InvokeTypes[C]): Promise<HandleTypes[C]>;
|
|
91
|
+
function send<T>(channel: string, serializer: PROTO.Serializable<T>, value: T): void;
|
|
68
92
|
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
69
|
-
|
|
93
|
+
function invoke<T, R>(channel: string, serializer: PROTO.Serializable<T>, value: T, deserializer: PROTO.Serializable<R>): Promise<R>;
|
|
70
94
|
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
71
|
-
|
|
72
|
-
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
73
|
-
export function on<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
74
|
-
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
75
|
-
export function once<C extends keyof SendTypes>(channel: C, listener: (...args: SendTypes[C]) => void): () => void;
|
|
95
|
+
function on<T>(channel: string, deserializer: PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
76
96
|
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
77
|
-
|
|
78
|
-
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
79
|
-
export function handle<C extends keyof (HandleTypes & InvokeTypes)>(channel: C, listener: (...args: InvokeTypes[C]) => HandleTypes[C]): () => void;
|
|
97
|
+
function once<T>(channel: string, deserializer: PROTO.Serializable<T>, listener: (value: T) => void): () => void;
|
|
80
98
|
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
81
|
-
|
|
82
|
-
export {};
|
|
99
|
+
function handle<T, R>(channel: string, deserializer: PROTO.Serializable<T>, serializer: PROTO.Serializable<R>, listener: (value: T) => R): () => void;
|
|
83
100
|
}
|
|
84
101
|
export default IPC;
|
|
85
|
-
export declare namespace NET {
|
|
86
|
-
function emit<T = any>(namespace: string, channel: string, args: T): Generator<void, void, void>;
|
|
87
|
-
function listen<T = any>(namespace: string, channel: string, callback: (args: T) => Generator<void, void, void>): () => void;
|
|
88
|
-
}
|