mcbe-ipc 3.0.4 → 3.1.4

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.
@@ -1,369 +0,0 @@
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) {
46
- return Math.floor(Math.random() * (mod - 1)) + 1;
47
- }
48
- CRYPTO.make_secret = make_secret;
49
- function* make_public(secret, mod, 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) {
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}:manager:${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}:manager:${channel}:invoke`, ConnectionSerializer, {
140
- from: $._from,
141
- bytes
142
- });
143
- })());
144
- return new Promise(resolve => {
145
- const terminate = NET.listen(`ipc:${$._from}:connection:${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}:connection:${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}:connection:${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}:connection:${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}:manager:${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, mod = CRYPTO.MOD, prime = CRYPTO.PRIME) {
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(mod);
248
- system.runJob((function* () {
249
- const public_key = yield* CRYPTO.make_public(secret, mod, prime);
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: prime,
255
- encryption_modulus: 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, prime)
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}:connection:${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}:connection:${channel}:invoke`, ConnectionSerializer, {
303
- from: $._id,
304
- bytes
305
- });
306
- })());
307
- promises.push(new Promise(resolve => {
308
- const terminate = NET.listen(`ipc:${$._id}:manager:${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}:manager:${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}:manager:${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}:manager:${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}:connection:${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/proto.d.ts DELETED
@@ -1,84 +0,0 @@
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
- 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 {
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;
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 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>;
84
- }