mcbe-ipc 3.0.4 → 3.1.3

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/README.md CHANGED
@@ -1,20 +1,26 @@
1
1
  # MCBE-IPC 📡
2
2
 
3
- An IPC[^1] system for MCBE Script API projects
3
+ An IPC[^1] system for Minecraft Bedrock Edition Script API projects
4
4
 
5
- [^1]: Inter-Pack Communication
5
+ ## 🔗 Dependencies
6
6
 
7
- ## Dependencies
7
+ | Package | Version |
8
+ |----------------------|---------|
9
+ | `@minecraft/server` | 1.18.0 |
8
10
 
9
- | Name | Version |
10
- |---|---|
11
- | `@minecraft/server` | Any |
11
+ ## 🚀 Installation
12
12
 
13
- ## Installation
14
- **JavaScript**
15
- 1. Download `ipc.js` and `ipc.d.ts` from the latest [release](https://github.com/OmniacDev/MCBE-IPC/releases/latest)
16
- 2. Copy the files into your project
13
+ ### 📦 NPM
14
+ ```bash
15
+ npm install mcbe-ipc
16
+ ```
17
17
 
18
- **TypeScript**
19
- 1. Download `ipc.ts` from the latest [release](https://github.com/OmniacDev/MCBE-IPC/releases/latest)
20
- 2. Copy file into your project
18
+ ### 🛠 Manual
19
+ 1. Download the applicable file(s) for your language from the latest [release](https://github.com/OmniacDev/MCBE-IPC/releases/latest):
20
+ - For **JavaScript**: `ipc.js` and `ipc.d.ts`
21
+ - For **TypeScript**: `ipc.ts`
22
+ 2. Add the downloaded file(s) to your project directory.
23
+
24
+
25
+
26
+ [^1]: Inter-Pack Communication
package/dist/ipc.d.ts CHANGED
@@ -24,10 +24,10 @@
24
24
  */
25
25
  export declare namespace PROTO {
26
26
  interface Serializable<T> {
27
- serialize(value: T, stream: ByteQueue): Generator<void, void, void>;
28
- deserialize(stream: ByteQueue): Generator<void, T, void>;
27
+ serialize(value: T, stream: Buffer): Generator<void, void, void>;
28
+ deserialize(stream: Buffer): Generator<void, T, void>;
29
29
  }
30
- class ByteQueue {
30
+ class Buffer {
31
31
  private _buffer;
32
32
  private _data_view;
33
33
  private _length;
@@ -39,12 +39,12 @@ export declare namespace PROTO {
39
39
  write(...values: number[]): void;
40
40
  read(amount?: number): number[];
41
41
  ensure_capacity(size: number): void;
42
- static from_uint8array(array: Uint8Array): ByteQueue;
42
+ static from_uint8array(array: Uint8Array): Buffer;
43
43
  to_uint8array(): Uint8Array;
44
44
  }
45
45
  namespace MIPS {
46
- function serialize(byte_queue: PROTO.ByteQueue): Generator<void, string, void>;
47
- function deserialize(str: string): Generator<void, PROTO.ByteQueue, void>;
46
+ function serialize(stream: PROTO.Buffer): Generator<void, string, void>;
47
+ function deserialize(str: string): Generator<void, PROTO.Buffer, void>;
48
48
  }
49
49
  const Void: PROTO.Serializable<void>;
50
50
  const Null: PROTO.Serializable<null>;
@@ -83,8 +83,9 @@ export declare namespace PROTO {
83
83
  const Header: PROTO.Serializable<Header>;
84
84
  }
85
85
  export declare namespace NET {
86
- function serialize(byte_queue: PROTO.ByteQueue, max_size?: number): Generator<void, string[], void>;
87
- function deserialize(strings: string[]): Generator<void, PROTO.ByteQueue, void>;
86
+ let FRAG_MAX: number;
87
+ function serialize(buffer: PROTO.Buffer, max_size?: number): Generator<void, string[], void>;
88
+ function deserialize(strings: string[]): Generator<void, PROTO.Buffer, void>;
88
89
  function emit<S extends PROTO.Serializable<T>, T>(endpoint: string, serializer: S & PROTO.Serializable<T>, value: T): Generator<void, void, void>;
89
90
  function listen<T, S extends PROTO.Serializable<T>>(endpoint: string, serializer: S & PROTO.Serializable<T>, callback: (value: T) => Generator<void, void, void>): () => void;
90
91
  }
package/dist/ipc.js CHANGED
@@ -22,10 +22,10 @@
22
22
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
23
  * SOFTWARE.
24
24
  */
25
- import { ScriptEventSource, system, world } from '@minecraft/server';
25
+ import { ScriptEventSource, system } from '@minecraft/server';
26
26
  export var PROTO;
27
27
  (function (PROTO) {
28
- class ByteQueue {
28
+ class Buffer {
29
29
  get end() {
30
30
  return this._length + this._offset;
31
31
  }
@@ -66,22 +66,22 @@ export var PROTO;
66
66
  }
67
67
  }
68
68
  static from_uint8array(array) {
69
- const byte_queue = new ByteQueue();
70
- byte_queue._buffer = array;
71
- byte_queue._length = array.length;
72
- byte_queue._offset = 0;
73
- byte_queue._data_view = new DataView(array.buffer);
74
- return byte_queue;
69
+ const buffer = new Buffer();
70
+ buffer._buffer = array;
71
+ buffer._length = array.length;
72
+ buffer._offset = 0;
73
+ buffer._data_view = new DataView(array.buffer);
74
+ return buffer;
75
75
  }
76
76
  to_uint8array() {
77
77
  return this._buffer.subarray(this._offset, this.end);
78
78
  }
79
79
  }
80
- PROTO.ByteQueue = ByteQueue;
80
+ PROTO.Buffer = Buffer;
81
81
  let MIPS;
82
82
  (function (MIPS) {
83
- function* serialize(byte_queue) {
84
- const uint8array = byte_queue.to_uint8array();
83
+ function* serialize(stream) {
84
+ const uint8array = stream.to_uint8array();
85
85
  let str = '(0x';
86
86
  for (let i = 0; i < uint8array.length; i++) {
87
87
  const hex = uint8array[i].toString(16).padStart(2, '0').toUpperCase();
@@ -94,16 +94,16 @@ export var PROTO;
94
94
  MIPS.serialize = serialize;
95
95
  function* deserialize(str) {
96
96
  if (str.startsWith('(0x') && str.endsWith(')')) {
97
- const result = [];
97
+ const buffer = new Buffer();
98
98
  const hex_str = str.slice(3, str.length - 1);
99
99
  for (let i = 0; i < hex_str.length; i++) {
100
100
  const hex = hex_str[i] + hex_str[++i];
101
- result.push(parseInt(hex, 16));
101
+ buffer.write(parseInt(hex, 16));
102
102
  yield;
103
103
  }
104
- return ByteQueue.from_uint8array(new Uint8Array(result));
104
+ return buffer;
105
105
  }
106
- return new ByteQueue();
106
+ return new Buffer();
107
107
  }
108
108
  MIPS.deserialize = deserialize;
109
109
  })(MIPS = PROTO.MIPS || (PROTO.MIPS = {}));
@@ -409,11 +409,11 @@ export var PROTO;
409
409
  })(PROTO || (PROTO = {}));
410
410
  export var NET;
411
411
  (function (NET) {
412
- const FRAG_MAX = 2048;
412
+ NET.FRAG_MAX = 2048;
413
413
  const ENCODING = 'mcbe-ipc:v3';
414
414
  const ENDPOINTS = new Map();
415
- function* serialize(byte_queue, max_size = Infinity) {
416
- const uint8array = byte_queue.to_uint8array();
415
+ function* serialize(buffer, max_size = Infinity) {
416
+ const uint8array = buffer.to_uint8array();
417
417
  const result = [];
418
418
  let acc_str = '';
419
419
  let acc_size = 0;
@@ -441,7 +441,7 @@ export var NET;
441
441
  }
442
442
  NET.serialize = serialize;
443
443
  function* deserialize(strings) {
444
- const result = [];
444
+ const buffer = new PROTO.Buffer();
445
445
  for (let i = 0; i < strings.length; i++) {
446
446
  const str = strings[i];
447
447
  for (let j = 0; j < str.length; j++) {
@@ -449,18 +449,18 @@ export var NET;
449
449
  if (char_code <= 0xff) {
450
450
  const hex = str[j] + str[++j];
451
451
  const hex_code = parseInt(hex, 16);
452
- result.push(hex_code & 0xff);
453
- result.push(hex_code >> 8);
452
+ buffer.write(hex_code & 0xff);
453
+ buffer.write(hex_code >> 8);
454
454
  }
455
455
  else {
456
- result.push(char_code & 0xff);
457
- result.push(char_code >> 8);
456
+ buffer.write(char_code & 0xff);
457
+ buffer.write(char_code >> 8);
458
458
  }
459
459
  yield;
460
460
  }
461
461
  yield;
462
462
  }
463
- return PROTO.ByteQueue.from_uint8array(new Uint8Array(result));
463
+ return buffer;
464
464
  }
465
465
  NET.deserialize = deserialize;
466
466
  system.afterEvents.scriptEventReceive.subscribe(event => {
@@ -503,23 +503,19 @@ export var NET;
503
503
  }
504
504
  function* emit(endpoint, serializer, value) {
505
505
  const guid = generate_id();
506
- const endpoint_stream = new PROTO.ByteQueue();
506
+ const endpoint_stream = new PROTO.Buffer();
507
507
  yield* PROTO.Endpoint.serialize(endpoint, endpoint_stream);
508
508
  const serialized_endpoint = yield* PROTO.MIPS.serialize(endpoint_stream);
509
- const RUN = function* (header, serialized_packet) {
510
- const header_stream = new PROTO.ByteQueue();
511
- yield* PROTO.Header.serialize(header, header_stream);
512
- const serialized_header = yield* PROTO.MIPS.serialize(header_stream);
513
- world
514
- .getDimension('overworld')
515
- .runCommand(`scriptevent ${serialized_endpoint}:${serialized_header} ${serialized_packet}`);
516
- };
517
- const packet_stream = new PROTO.ByteQueue();
509
+ const packet_stream = new PROTO.Buffer();
518
510
  yield* serializer.serialize(value, packet_stream);
519
- const serialized_packets = yield* serialize(packet_stream, FRAG_MAX);
511
+ const serialized_packets = yield* serialize(packet_stream, NET.FRAG_MAX);
520
512
  for (let i = 0; i < serialized_packets.length; i++) {
521
513
  const serialized_packet = serialized_packets[i];
522
- yield* RUN({ guid, encoding: ENCODING, index: i, final: i === serialized_packets.length - 1 }, serialized_packet);
514
+ const header = { guid, encoding: ENCODING, index: i, final: i === serialized_packets.length - 1 };
515
+ const header_stream = new PROTO.Buffer();
516
+ yield* PROTO.Header.serialize(header, header_stream);
517
+ const serialized_header = yield* PROTO.MIPS.serialize(header_stream);
518
+ system.sendScriptEvent(`${serialized_endpoint}:${serialized_header}`, serialized_packet);
523
519
  }
524
520
  }
525
521
  NET.emit = emit;
package/package.json CHANGED
@@ -3,15 +3,15 @@
3
3
  "author": "OmniacDev",
4
4
  "description": "IPC system for MCBE Script API projects",
5
5
  "license": "MIT",
6
- "version": "3.0.4",
6
+ "version": "3.1.3",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/OmniacDev/mcbe-ipc.git"
9
+ "url": "git+https://github.com/OmniacDev/MCBE-IPC.git"
10
10
  },
11
11
  "bugs": {
12
- "url": "https://github.com/OmniacDev/mcbe-ipc/issues"
12
+ "url": "https://github.com/OmniacDev/MCBE-IPC/issues"
13
13
  },
14
- "homepage": "https://github.com/OmniacDev/mcbe-ipc",
14
+ "homepage": "https://github.com/OmniacDev/MCBE-IPC",
15
15
  "type": "module",
16
16
  "main": "dist/ipc.js",
17
17
  "types": "dist/ipc.d.ts",
@@ -28,6 +28,6 @@
28
28
  "typescript": "^5.5.4"
29
29
  },
30
30
  "dependencies": {
31
- "@minecraft/server": "^1.13.0"
31
+ "@minecraft/server": "^1.18.0"
32
32
  }
33
33
  }
@@ -1,61 +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 { 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, mod?: number, prime?: 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;
@@ -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
- }
package/dist/proto.js DELETED
@@ -1,408 +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 var PROTO;
26
- (function (PROTO) {
27
- class ByteQueue {
28
- get end() {
29
- return this._length + this._offset;
30
- }
31
- get front() {
32
- return this._offset;
33
- }
34
- get data_view() {
35
- return this._data_view;
36
- }
37
- constructor(size = 256) {
38
- this._buffer = new Uint8Array(size);
39
- this._data_view = new DataView(this._buffer.buffer);
40
- this._length = 0;
41
- this._offset = 0;
42
- }
43
- write(...values) {
44
- this.ensure_capacity(values.length);
45
- this._buffer.set(values, this.end);
46
- this._length += values.length;
47
- }
48
- read(amount = 1) {
49
- if (this._length > 0) {
50
- const max_amount = amount > this._length ? this._length : amount;
51
- const values = this._buffer.subarray(this._offset, this._offset + max_amount);
52
- this._length -= max_amount;
53
- this._offset += max_amount;
54
- return globalThis.Array.from(values);
55
- }
56
- return [];
57
- }
58
- ensure_capacity(size) {
59
- if (this.end + size > this._buffer.length) {
60
- const larger_buffer = new Uint8Array((this.end + size) * 2);
61
- larger_buffer.set(this._buffer.subarray(this._offset, this.end), 0);
62
- this._buffer = larger_buffer;
63
- this._offset = 0;
64
- this._data_view = new DataView(this._buffer.buffer);
65
- }
66
- }
67
- static from_uint8array(array) {
68
- const byte_queue = new ByteQueue();
69
- byte_queue._buffer = array;
70
- byte_queue._length = array.length;
71
- byte_queue._offset = 0;
72
- byte_queue._data_view = new DataView(array.buffer);
73
- return byte_queue;
74
- }
75
- to_uint8array() {
76
- return this._buffer.subarray(this._offset, this.end);
77
- }
78
- }
79
- PROTO.ByteQueue = ByteQueue;
80
- let MIPS;
81
- (function (MIPS) {
82
- function* serialize(byte_queue) {
83
- const uint8array = byte_queue.to_uint8array();
84
- let str = '(0x';
85
- for (let i = 0; i < uint8array.length; i++) {
86
- const hex = uint8array[i].toString(16).padStart(2, '0').toUpperCase();
87
- str += hex;
88
- yield;
89
- }
90
- str += ')';
91
- return str;
92
- }
93
- MIPS.serialize = serialize;
94
- function* deserialize(str) {
95
- if (str.startsWith('(0x') && str.endsWith(')')) {
96
- const result = [];
97
- const hex_str = str.slice(3, str.length - 1);
98
- for (let i = 0; i < hex_str.length; i++) {
99
- const hex = hex_str[i] + hex_str[++i];
100
- result.push(parseInt(hex, 16));
101
- yield;
102
- }
103
- return ByteQueue.from_uint8array(new Uint8Array(result));
104
- }
105
- return new ByteQueue();
106
- }
107
- MIPS.deserialize = deserialize;
108
- })(MIPS = PROTO.MIPS || (PROTO.MIPS = {}));
109
- PROTO.Void = {
110
- *serialize() { },
111
- *deserialize() { }
112
- };
113
- PROTO.Null = {
114
- *serialize() { },
115
- *deserialize() {
116
- return null;
117
- }
118
- };
119
- PROTO.Undefined = {
120
- *serialize() { },
121
- *deserialize() {
122
- return undefined;
123
- }
124
- };
125
- PROTO.Int8 = {
126
- *serialize(value, stream) {
127
- const length = 1;
128
- stream.write(...globalThis.Array(length).fill(0));
129
- stream.data_view.setInt8(stream.end - length, value);
130
- },
131
- *deserialize(stream) {
132
- const value = stream.data_view.getInt8(stream.front);
133
- stream.read(1);
134
- return value;
135
- }
136
- };
137
- PROTO.Int16 = {
138
- *serialize(value, stream) {
139
- const length = 2;
140
- stream.write(...globalThis.Array(length).fill(0));
141
- stream.data_view.setInt16(stream.end - length, value);
142
- },
143
- *deserialize(stream) {
144
- const value = stream.data_view.getInt16(stream.front);
145
- stream.read(2);
146
- return value;
147
- }
148
- };
149
- PROTO.Int32 = {
150
- *serialize(value, stream) {
151
- const length = 4;
152
- stream.write(...globalThis.Array(length).fill(0));
153
- stream.data_view.setInt32(stream.end - length, value);
154
- },
155
- *deserialize(stream) {
156
- const value = stream.data_view.getInt32(stream.front);
157
- stream.read(4);
158
- return value;
159
- }
160
- };
161
- PROTO.UInt8 = {
162
- *serialize(value, stream) {
163
- const length = 1;
164
- stream.write(...globalThis.Array(length).fill(0));
165
- stream.data_view.setUint8(stream.end - length, value);
166
- },
167
- *deserialize(stream) {
168
- const value = stream.data_view.getUint8(stream.front);
169
- stream.read(1);
170
- return value;
171
- }
172
- };
173
- PROTO.UInt16 = {
174
- *serialize(value, stream) {
175
- const length = 2;
176
- stream.write(...globalThis.Array(length).fill(0));
177
- stream.data_view.setUint16(stream.end - length, value);
178
- },
179
- *deserialize(stream) {
180
- const value = stream.data_view.getUint16(stream.front);
181
- stream.read(2);
182
- return value;
183
- }
184
- };
185
- PROTO.UInt32 = {
186
- *serialize(value, stream) {
187
- const length = 4;
188
- stream.write(...globalThis.Array(length).fill(0));
189
- stream.data_view.setUint32(stream.end - length, value);
190
- },
191
- *deserialize(stream) {
192
- const value = stream.data_view.getUint32(stream.front);
193
- stream.read(4);
194
- return value;
195
- }
196
- };
197
- PROTO.UVarInt32 = {
198
- *serialize(value, stream) {
199
- while (value >= 0x80) {
200
- stream.write((value & 0x7f) | 0x80);
201
- value >>= 7;
202
- yield;
203
- }
204
- stream.write(value);
205
- },
206
- *deserialize(stream) {
207
- let value = 0;
208
- let size = 0;
209
- let byte;
210
- do {
211
- byte = stream.read()[0];
212
- value |= (byte & 0x7f) << (size * 7);
213
- size += 1;
214
- yield;
215
- } while ((byte & 0x80) !== 0 && size < 10);
216
- return value;
217
- }
218
- };
219
- PROTO.Float32 = {
220
- *serialize(value, stream) {
221
- const length = 4;
222
- stream.write(...globalThis.Array(length).fill(0));
223
- stream.data_view.setFloat32(stream.end - length, value);
224
- },
225
- *deserialize(stream) {
226
- const value = stream.data_view.getFloat32(stream.front);
227
- stream.read(4);
228
- return value;
229
- }
230
- };
231
- PROTO.Float64 = {
232
- *serialize(value, stream) {
233
- const length = 8;
234
- stream.write(...globalThis.Array(length).fill(0));
235
- stream.data_view.setFloat64(stream.end - length, value);
236
- },
237
- *deserialize(stream) {
238
- const value = stream.data_view.getFloat64(stream.front);
239
- stream.read(8);
240
- return value;
241
- }
242
- };
243
- PROTO.String = {
244
- *serialize(value, stream) {
245
- yield* PROTO.UVarInt32.serialize(value.length, stream);
246
- for (let i = 0; i < value.length; i++) {
247
- const code = value.charCodeAt(i);
248
- yield* PROTO.UVarInt32.serialize(code, stream);
249
- }
250
- },
251
- *deserialize(stream) {
252
- const length = yield* PROTO.UVarInt32.deserialize(stream);
253
- let value = '';
254
- for (let i = 0; i < length; i++) {
255
- const code = yield* PROTO.UVarInt32.deserialize(stream);
256
- value += globalThis.String.fromCharCode(code);
257
- }
258
- return value;
259
- }
260
- };
261
- PROTO.Boolean = {
262
- *serialize(value, stream) {
263
- stream.write(value ? 1 : 0);
264
- },
265
- *deserialize(stream) {
266
- const value = stream.read()[0];
267
- return value === 1;
268
- }
269
- };
270
- PROTO.UInt8Array = {
271
- *serialize(value, stream) {
272
- yield* PROTO.UVarInt32.serialize(value.length, stream);
273
- stream.write(...value);
274
- },
275
- *deserialize(stream) {
276
- const length = yield* PROTO.UVarInt32.deserialize(stream);
277
- return new Uint8Array(stream.read(length));
278
- }
279
- };
280
- PROTO.Date = {
281
- *serialize(value, stream) {
282
- yield* PROTO.Float64.serialize(value.getTime(), stream);
283
- },
284
- *deserialize(stream) {
285
- return new globalThis.Date(yield* PROTO.Float64.deserialize(stream));
286
- }
287
- };
288
- function Object(obj) {
289
- return {
290
- *serialize(value, stream) {
291
- for (const key in obj) {
292
- yield* obj[key].serialize(value[key], stream);
293
- }
294
- },
295
- *deserialize(stream) {
296
- const result = {};
297
- for (const key in obj) {
298
- result[key] = yield* obj[key].deserialize(stream);
299
- }
300
- return result;
301
- }
302
- };
303
- }
304
- PROTO.Object = Object;
305
- function Array(value) {
306
- return {
307
- *serialize(array, stream) {
308
- yield* PROTO.UVarInt32.serialize(array.length, stream);
309
- for (const item of array) {
310
- yield* value.serialize(item, stream);
311
- }
312
- },
313
- *deserialize(stream) {
314
- const result = [];
315
- const length = yield* PROTO.UVarInt32.deserialize(stream);
316
- for (let i = 0; i < length; i++) {
317
- result[i] = yield* value.deserialize(stream);
318
- }
319
- return result;
320
- }
321
- };
322
- }
323
- PROTO.Array = Array;
324
- function Tuple(...values) {
325
- return {
326
- *serialize(tuple, stream) {
327
- for (let i = 0; i < values.length; i++) {
328
- yield* values[i].serialize(tuple[i], stream);
329
- }
330
- },
331
- *deserialize(stream) {
332
- const result = [];
333
- for (let i = 0; i < values.length; i++) {
334
- result[i] = yield* values[i].deserialize(stream);
335
- }
336
- return result;
337
- }
338
- };
339
- }
340
- PROTO.Tuple = Tuple;
341
- function Optional(value) {
342
- return {
343
- *serialize(optional, stream) {
344
- yield* PROTO.Boolean.serialize(optional !== undefined, stream);
345
- if (optional !== undefined) {
346
- yield* value.serialize(optional, stream);
347
- }
348
- },
349
- *deserialize(stream) {
350
- const defined = yield* PROTO.Boolean.deserialize(stream);
351
- if (defined) {
352
- return yield* value.deserialize(stream);
353
- }
354
- return undefined;
355
- }
356
- };
357
- }
358
- PROTO.Optional = Optional;
359
- function Map(key, value) {
360
- return {
361
- *serialize(map, stream) {
362
- yield* PROTO.UVarInt32.serialize(map.size, stream);
363
- for (const [k, v] of map.entries()) {
364
- yield* key.serialize(k, stream);
365
- yield* value.serialize(v, stream);
366
- }
367
- },
368
- *deserialize(stream) {
369
- const size = yield* PROTO.UVarInt32.deserialize(stream);
370
- const result = new globalThis.Map();
371
- for (let i = 0; i < size; i++) {
372
- const k = yield* key.deserialize(stream);
373
- const v = yield* value.deserialize(stream);
374
- result.set(k, v);
375
- }
376
- return result;
377
- }
378
- };
379
- }
380
- PROTO.Map = Map;
381
- function Set(value) {
382
- return {
383
- *serialize(set, stream) {
384
- yield* PROTO.UVarInt32.serialize(set.size, stream);
385
- for (const [_, v] of set.entries()) {
386
- yield* value.serialize(v, stream);
387
- }
388
- },
389
- *deserialize(stream) {
390
- const size = yield* PROTO.UVarInt32.deserialize(stream);
391
- const result = new globalThis.Set();
392
- for (let i = 0; i < size; i++) {
393
- const v = yield* value.deserialize(stream);
394
- result.add(v);
395
- }
396
- return result;
397
- }
398
- };
399
- }
400
- PROTO.Set = Set;
401
- PROTO.Endpoint = PROTO.String;
402
- PROTO.Header = PROTO.Object({
403
- guid: PROTO.String,
404
- encoding: PROTO.String,
405
- index: PROTO.UVarInt32,
406
- final: PROTO.Boolean
407
- });
408
- })(PROTO || (PROTO = {}));