mcbe-ipc 3.0.0 → 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/dist/direct.ipc.d.ts +61 -0
- package/dist/direct.ipc.js +369 -0
- package/dist/ipc.d.ts +58 -94
- package/dist/ipc.js +330 -711
- 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/dist/ipc.js
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
|
|
@@ -23,12 +23,18 @@
|
|
|
23
23
|
* SOFTWARE.
|
|
24
24
|
*/
|
|
25
25
|
import { ScriptEventSource, system, world } from '@minecraft/server';
|
|
26
|
-
export var
|
|
27
|
-
(function (
|
|
28
|
-
class
|
|
29
|
-
get
|
|
26
|
+
export var PROTO;
|
|
27
|
+
(function (PROTO) {
|
|
28
|
+
class ByteQueue {
|
|
29
|
+
get end() {
|
|
30
30
|
return this._length + this._offset;
|
|
31
31
|
}
|
|
32
|
+
get front() {
|
|
33
|
+
return this._offset;
|
|
34
|
+
}
|
|
35
|
+
get data_view() {
|
|
36
|
+
return this._data_view;
|
|
37
|
+
}
|
|
32
38
|
constructor(size = 256) {
|
|
33
39
|
this._buffer = new Uint8Array(size);
|
|
34
40
|
this._data_view = new DataView(this._buffer.buffer);
|
|
@@ -36,8 +42,8 @@ export var SERDE;
|
|
|
36
42
|
this._offset = 0;
|
|
37
43
|
}
|
|
38
44
|
write(...values) {
|
|
39
|
-
this.
|
|
40
|
-
this._buffer.set(values, this.
|
|
45
|
+
this.ensure_capacity(values.length);
|
|
46
|
+
this._buffer.set(values, this.end);
|
|
41
47
|
this._length += values.length;
|
|
42
48
|
}
|
|
43
49
|
read(amount = 1) {
|
|
@@ -46,248 +52,229 @@ export var SERDE;
|
|
|
46
52
|
const values = this._buffer.subarray(this._offset, this._offset + max_amount);
|
|
47
53
|
this._length -= max_amount;
|
|
48
54
|
this._offset += max_amount;
|
|
49
|
-
return Array.from(values);
|
|
55
|
+
return globalThis.Array.from(values);
|
|
50
56
|
}
|
|
51
57
|
return [];
|
|
52
58
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
read_uint8() {
|
|
59
|
-
if (this._length >= 1) {
|
|
60
|
-
const value = this._data_view.getUint8(this._offset);
|
|
61
|
-
this._length -= 1;
|
|
62
|
-
this._offset += 1;
|
|
63
|
-
return value;
|
|
64
|
-
}
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
write_uint16(value) {
|
|
68
|
-
this._ensure_capacity(this._end + 2);
|
|
69
|
-
this._data_view.setUint16(this._end, value);
|
|
70
|
-
this._length += 2;
|
|
71
|
-
}
|
|
72
|
-
read_uint16() {
|
|
73
|
-
if (this._length >= 2) {
|
|
74
|
-
const value = this._data_view.getUint16(this._offset);
|
|
75
|
-
this._length -= 2;
|
|
76
|
-
this._offset += 2;
|
|
77
|
-
return value;
|
|
78
|
-
}
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
write_uint32(value) {
|
|
82
|
-
this._ensure_capacity(this._end + 4);
|
|
83
|
-
this._data_view.setUint32(this._end, value);
|
|
84
|
-
this._length += 4;
|
|
85
|
-
}
|
|
86
|
-
read_uint32() {
|
|
87
|
-
if (this._length >= 4) {
|
|
88
|
-
const value = this._data_view.getUint32(this._offset);
|
|
89
|
-
this._length -= 4;
|
|
90
|
-
this._offset += 4;
|
|
91
|
-
return value;
|
|
92
|
-
}
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
write_int8(value) {
|
|
96
|
-
this._ensure_capacity(this._end + 1);
|
|
97
|
-
this._data_view.setInt8(this._end, value);
|
|
98
|
-
this._length += 1;
|
|
99
|
-
}
|
|
100
|
-
read_int8() {
|
|
101
|
-
if (this._length >= 1) {
|
|
102
|
-
const value = this._data_view.getInt8(this._offset);
|
|
103
|
-
this._length -= 1;
|
|
104
|
-
this._offset += 1;
|
|
105
|
-
return value;
|
|
106
|
-
}
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
write_int16(value) {
|
|
110
|
-
this._ensure_capacity(this._end + 2);
|
|
111
|
-
this._data_view.setInt16(this._end, value);
|
|
112
|
-
this._length += 2;
|
|
113
|
-
}
|
|
114
|
-
read_int16() {
|
|
115
|
-
if (this._length >= 2) {
|
|
116
|
-
const value = this._data_view.getInt16(this._offset);
|
|
117
|
-
this._length -= 2;
|
|
118
|
-
this._offset += 2;
|
|
119
|
-
return value;
|
|
120
|
-
}
|
|
121
|
-
return undefined;
|
|
122
|
-
}
|
|
123
|
-
write_int32(value) {
|
|
124
|
-
this._ensure_capacity(this._end + 4);
|
|
125
|
-
this._data_view.setInt32(this._end, value);
|
|
126
|
-
this._length += 4;
|
|
127
|
-
}
|
|
128
|
-
read_int32() {
|
|
129
|
-
if (this._length >= 4) {
|
|
130
|
-
const value = this._data_view.getInt32(this._offset);
|
|
131
|
-
this._length -= 4;
|
|
132
|
-
this._offset += 4;
|
|
133
|
-
return value;
|
|
134
|
-
}
|
|
135
|
-
return undefined;
|
|
136
|
-
}
|
|
137
|
-
write_f32(value) {
|
|
138
|
-
this._ensure_capacity(this._end + 4);
|
|
139
|
-
this._data_view.setFloat32(this._end, value);
|
|
140
|
-
this._length += 4;
|
|
141
|
-
}
|
|
142
|
-
read_f32() {
|
|
143
|
-
if (this._length >= 4) {
|
|
144
|
-
const value = this._data_view.getFloat32(this._offset);
|
|
145
|
-
this._length -= 4;
|
|
146
|
-
this._offset += 4;
|
|
147
|
-
return value;
|
|
148
|
-
}
|
|
149
|
-
return undefined;
|
|
150
|
-
}
|
|
151
|
-
write_f64(value) {
|
|
152
|
-
this._ensure_capacity(this._end + 8);
|
|
153
|
-
this._data_view.setFloat64(this._end, value);
|
|
154
|
-
this._length += 8;
|
|
155
|
-
}
|
|
156
|
-
read_f64() {
|
|
157
|
-
if (this._length >= 8) {
|
|
158
|
-
const value = this._data_view.getFloat64(this._offset);
|
|
159
|
-
this._length -= 8;
|
|
160
|
-
this._offset += 8;
|
|
161
|
-
return value;
|
|
162
|
-
}
|
|
163
|
-
return undefined;
|
|
164
|
-
}
|
|
165
|
-
_ensure_capacity(size) {
|
|
166
|
-
if (size > this._buffer.length) {
|
|
167
|
-
const larger_buffer = new Uint8Array(size * 2);
|
|
168
|
-
larger_buffer.set(this._buffer.subarray(this._offset, this._end), 0);
|
|
59
|
+
ensure_capacity(size) {
|
|
60
|
+
if (this.end + size > this._buffer.length) {
|
|
61
|
+
const larger_buffer = new Uint8Array((this.end + size) * 2);
|
|
62
|
+
larger_buffer.set(this._buffer.subarray(this._offset, this.end), 0);
|
|
169
63
|
this._buffer = larger_buffer;
|
|
170
64
|
this._offset = 0;
|
|
171
65
|
this._data_view = new DataView(this._buffer.buffer);
|
|
172
66
|
}
|
|
173
67
|
}
|
|
174
68
|
static from_uint8array(array) {
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
return
|
|
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;
|
|
181
75
|
}
|
|
182
76
|
to_uint8array() {
|
|
183
|
-
return this._buffer.subarray(this._offset, this.
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (acc_size + char_size > max_size) {
|
|
197
|
-
result.push(acc_str);
|
|
198
|
-
acc_str = '';
|
|
199
|
-
acc_size = 0;
|
|
200
|
-
}
|
|
201
|
-
if (char_code > 0xff) {
|
|
202
|
-
acc_str += String.fromCharCode(char_code);
|
|
203
|
-
acc_size += utf16_size;
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
acc_str += `?${char_code.toString(16).padStart(2, '0')}`;
|
|
207
|
-
acc_size += 3;
|
|
77
|
+
return this._buffer.subarray(this._offset, this.end);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
PROTO.ByteQueue = ByteQueue;
|
|
81
|
+
let MIPS;
|
|
82
|
+
(function (MIPS) {
|
|
83
|
+
function* serialize(byte_queue) {
|
|
84
|
+
const uint8array = byte_queue.to_uint8array();
|
|
85
|
+
let str = '(0x';
|
|
86
|
+
for (let i = 0; i < uint8array.length; i++) {
|
|
87
|
+
const hex = uint8array[i].toString(16).padStart(2, '0').toUpperCase();
|
|
88
|
+
str += hex;
|
|
89
|
+
yield;
|
|
208
90
|
}
|
|
209
|
-
|
|
91
|
+
str += ')';
|
|
92
|
+
return str;
|
|
210
93
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const char_code = str.charCodeAt(j);
|
|
221
|
-
if (char_code === '?'.charCodeAt(0) && j + 2 < str.length) {
|
|
222
|
-
const hex = str.slice(j + 1, j + 3);
|
|
223
|
-
const hex_code = parseInt(hex, 16);
|
|
224
|
-
result.push(hex_code & 0xff);
|
|
225
|
-
result.push(hex_code >> 8);
|
|
226
|
-
j += 2;
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
result.push(char_code & 0xff);
|
|
230
|
-
result.push(char_code >> 8);
|
|
94
|
+
MIPS.serialize = serialize;
|
|
95
|
+
function* deserialize(str) {
|
|
96
|
+
if (str.startsWith('(0x') && str.endsWith(')')) {
|
|
97
|
+
const result = [];
|
|
98
|
+
const hex_str = str.slice(3, str.length - 1);
|
|
99
|
+
for (let i = 0; i < hex_str.length; i++) {
|
|
100
|
+
const hex = hex_str[i] + hex_str[++i];
|
|
101
|
+
result.push(parseInt(hex, 16));
|
|
102
|
+
yield;
|
|
231
103
|
}
|
|
104
|
+
return ByteQueue.from_uint8array(new Uint8Array(result));
|
|
105
|
+
}
|
|
106
|
+
return new ByteQueue();
|
|
107
|
+
}
|
|
108
|
+
MIPS.deserialize = deserialize;
|
|
109
|
+
})(MIPS = PROTO.MIPS || (PROTO.MIPS = {}));
|
|
110
|
+
PROTO.Void = {
|
|
111
|
+
*serialize() { },
|
|
112
|
+
*deserialize() { }
|
|
113
|
+
};
|
|
114
|
+
PROTO.Int8 = {
|
|
115
|
+
*serialize(value, stream) {
|
|
116
|
+
const length = 1;
|
|
117
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
118
|
+
stream.data_view.setInt8(stream.end - length, value);
|
|
119
|
+
},
|
|
120
|
+
*deserialize(stream) {
|
|
121
|
+
const value = stream.data_view.getInt8(stream.front);
|
|
122
|
+
stream.read(1);
|
|
123
|
+
return value;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
PROTO.Int16 = {
|
|
127
|
+
*serialize(value, stream) {
|
|
128
|
+
const length = 2;
|
|
129
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
130
|
+
stream.data_view.setInt16(stream.end - length, value);
|
|
131
|
+
},
|
|
132
|
+
*deserialize(stream) {
|
|
133
|
+
const value = stream.data_view.getInt16(stream.front);
|
|
134
|
+
stream.read(2);
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
PROTO.Int32 = {
|
|
139
|
+
*serialize(value, stream) {
|
|
140
|
+
const length = 4;
|
|
141
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
142
|
+
stream.data_view.setInt32(stream.end - length, value);
|
|
143
|
+
},
|
|
144
|
+
*deserialize(stream) {
|
|
145
|
+
const value = stream.data_view.getInt32(stream.front);
|
|
146
|
+
stream.read(4);
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
PROTO.UInt8 = {
|
|
151
|
+
*serialize(value, stream) {
|
|
152
|
+
const length = 1;
|
|
153
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
154
|
+
stream.data_view.setUint8(stream.end - length, value);
|
|
155
|
+
},
|
|
156
|
+
*deserialize(stream) {
|
|
157
|
+
const value = stream.data_view.getUint8(stream.front);
|
|
158
|
+
stream.read(1);
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
PROTO.UInt16 = {
|
|
163
|
+
*serialize(value, stream) {
|
|
164
|
+
const length = 2;
|
|
165
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
166
|
+
stream.data_view.setUint16(stream.end - length, value);
|
|
167
|
+
},
|
|
168
|
+
*deserialize(stream) {
|
|
169
|
+
const value = stream.data_view.getUint16(stream.front);
|
|
170
|
+
stream.read(2);
|
|
171
|
+
return value;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
PROTO.UInt32 = {
|
|
175
|
+
*serialize(value, stream) {
|
|
176
|
+
const length = 4;
|
|
177
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
178
|
+
stream.data_view.setUint32(stream.end - length, value);
|
|
179
|
+
},
|
|
180
|
+
*deserialize(stream) {
|
|
181
|
+
const value = stream.data_view.getUint32(stream.front);
|
|
182
|
+
stream.read(4);
|
|
183
|
+
return value;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
PROTO.UVarInt32 = {
|
|
187
|
+
*serialize(value, stream) {
|
|
188
|
+
while (value >= 0x80) {
|
|
189
|
+
stream.write((value & 0x7f) | 0x80);
|
|
190
|
+
value >>= 7;
|
|
232
191
|
yield;
|
|
233
192
|
}
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
193
|
+
stream.write(value);
|
|
194
|
+
},
|
|
195
|
+
*deserialize(stream) {
|
|
196
|
+
let value = 0;
|
|
197
|
+
let size = 0;
|
|
198
|
+
let byte;
|
|
199
|
+
do {
|
|
200
|
+
byte = stream.read()[0];
|
|
201
|
+
value |= (byte & 0x7f) << (size * 7);
|
|
202
|
+
size += 1;
|
|
203
|
+
yield;
|
|
204
|
+
} while ((byte & 0x80) !== 0 && size < 10);
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
PROTO.Float32 = {
|
|
209
|
+
*serialize(value, stream) {
|
|
210
|
+
const length = 4;
|
|
211
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
212
|
+
stream.data_view.setFloat32(stream.end - length, value);
|
|
213
|
+
},
|
|
214
|
+
*deserialize(stream) {
|
|
215
|
+
const value = stream.data_view.getFloat32(stream.front);
|
|
216
|
+
stream.read(4);
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
PROTO.Float64 = {
|
|
221
|
+
*serialize(value, stream) {
|
|
222
|
+
const length = 8;
|
|
223
|
+
stream.write(...globalThis.Array(length).fill(0));
|
|
224
|
+
stream.data_view.setFloat64(stream.end - length, value);
|
|
225
|
+
},
|
|
226
|
+
*deserialize(stream) {
|
|
227
|
+
const value = stream.data_view.getFloat64(stream.front);
|
|
228
|
+
stream.read(8);
|
|
229
|
+
return value;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
PROTO.String = {
|
|
233
|
+
*serialize(value, stream) {
|
|
234
|
+
yield* PROTO.UVarInt32.serialize(value.length, stream);
|
|
235
|
+
for (let i = 0; i < value.length; i++) {
|
|
236
|
+
const code = value.charCodeAt(i);
|
|
237
|
+
yield* PROTO.UVarInt32.serialize(code, stream);
|
|
252
238
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
239
|
+
},
|
|
240
|
+
*deserialize(stream) {
|
|
241
|
+
const length = yield* PROTO.UVarInt32.deserialize(stream);
|
|
242
|
+
let value = '';
|
|
243
|
+
for (let i = 0; i < length; i++) {
|
|
244
|
+
const code = yield* PROTO.UVarInt32.deserialize(stream);
|
|
245
|
+
value += globalThis.String.fromCharCode(code);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
PROTO.Boolean = {
|
|
251
|
+
*serialize(value, stream) {
|
|
252
|
+
stream.write(value ? 1 : 0);
|
|
253
|
+
},
|
|
254
|
+
*deserialize(stream) {
|
|
255
|
+
const value = stream.read()[0];
|
|
256
|
+
return value === 1;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
PROTO.UInt8Array = {
|
|
260
|
+
*serialize(value, stream) {
|
|
261
|
+
yield* PROTO.UVarInt32.serialize(value.length, stream);
|
|
262
|
+
stream.write(...value);
|
|
263
|
+
},
|
|
264
|
+
*deserialize(stream) {
|
|
265
|
+
const length = yield* PROTO.UVarInt32.deserialize(stream);
|
|
266
|
+
return new Uint8Array(stream.read(length));
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
PROTO.Date = {
|
|
270
|
+
*serialize(value, stream) {
|
|
271
|
+
yield* PROTO.Float64.serialize(value.getTime(), stream);
|
|
272
|
+
},
|
|
273
|
+
*deserialize(stream) {
|
|
274
|
+
return new globalThis.Date(yield* PROTO.Float64.deserialize(stream));
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
function Object(obj) {
|
|
291
278
|
return {
|
|
292
279
|
*serialize(value, stream) {
|
|
293
280
|
for (const key in obj) {
|
|
@@ -303,68 +290,73 @@ export class Proto {
|
|
|
303
290
|
}
|
|
304
291
|
};
|
|
305
292
|
}
|
|
306
|
-
|
|
293
|
+
PROTO.Object = Object;
|
|
294
|
+
function Array(value) {
|
|
307
295
|
return {
|
|
308
|
-
*serialize(
|
|
309
|
-
yield*
|
|
310
|
-
for (const item of
|
|
311
|
-
yield*
|
|
296
|
+
*serialize(array, stream) {
|
|
297
|
+
yield* PROTO.UVarInt32.serialize(array.length, stream);
|
|
298
|
+
for (const item of array) {
|
|
299
|
+
yield* value.serialize(item, stream);
|
|
312
300
|
}
|
|
313
301
|
},
|
|
314
302
|
*deserialize(stream) {
|
|
315
303
|
const result = [];
|
|
316
|
-
const length = yield*
|
|
304
|
+
const length = yield* PROTO.UVarInt32.deserialize(stream);
|
|
317
305
|
for (let i = 0; i < length; i++) {
|
|
318
|
-
result[i] = yield*
|
|
306
|
+
result[i] = yield* value.deserialize(stream);
|
|
319
307
|
}
|
|
320
308
|
return result;
|
|
321
309
|
}
|
|
322
310
|
};
|
|
323
311
|
}
|
|
324
|
-
|
|
312
|
+
PROTO.Array = Array;
|
|
313
|
+
function Tuple(...values) {
|
|
325
314
|
return {
|
|
326
|
-
*serialize(
|
|
327
|
-
for (let i = 0; i <
|
|
328
|
-
yield*
|
|
315
|
+
*serialize(tuple, stream) {
|
|
316
|
+
for (let i = 0; i < values.length; i++) {
|
|
317
|
+
yield* values[i].serialize(tuple[i], stream);
|
|
329
318
|
}
|
|
330
319
|
},
|
|
331
320
|
*deserialize(stream) {
|
|
332
321
|
const result = [];
|
|
333
|
-
for (let i = 0; i <
|
|
334
|
-
result[i] = yield*
|
|
322
|
+
for (let i = 0; i < values.length; i++) {
|
|
323
|
+
result[i] = yield* values[i].deserialize(stream);
|
|
335
324
|
}
|
|
336
325
|
return result;
|
|
337
326
|
}
|
|
338
327
|
};
|
|
339
328
|
}
|
|
340
|
-
|
|
329
|
+
PROTO.Tuple = Tuple;
|
|
330
|
+
function Optional(value) {
|
|
341
331
|
return {
|
|
342
|
-
*serialize(
|
|
343
|
-
yield*
|
|
344
|
-
if (
|
|
345
|
-
yield*
|
|
332
|
+
*serialize(optional, stream) {
|
|
333
|
+
yield* PROTO.Boolean.serialize(value !== undefined, stream);
|
|
334
|
+
if (optional !== undefined) {
|
|
335
|
+
yield* value.serialize(optional, stream);
|
|
346
336
|
}
|
|
347
337
|
},
|
|
348
338
|
*deserialize(stream) {
|
|
349
|
-
const defined = yield*
|
|
339
|
+
const defined = yield* PROTO.Boolean.deserialize(stream);
|
|
350
340
|
if (defined) {
|
|
351
|
-
return yield*
|
|
341
|
+
return yield* value.deserialize(stream);
|
|
352
342
|
}
|
|
343
|
+
return undefined;
|
|
353
344
|
}
|
|
354
345
|
};
|
|
355
346
|
}
|
|
356
|
-
|
|
347
|
+
PROTO.Optional = Optional;
|
|
348
|
+
function Map(key, value) {
|
|
357
349
|
return {
|
|
358
350
|
*serialize(map, stream) {
|
|
359
|
-
yield*
|
|
351
|
+
yield* PROTO.UVarInt32.serialize(map.size, stream);
|
|
360
352
|
for (const [k, v] of map.entries()) {
|
|
361
353
|
yield* key.serialize(k, stream);
|
|
362
354
|
yield* value.serialize(v, stream);
|
|
363
355
|
}
|
|
364
356
|
},
|
|
365
357
|
*deserialize(stream) {
|
|
366
|
-
const size = yield*
|
|
367
|
-
const result = new Map();
|
|
358
|
+
const size = yield* PROTO.UVarInt32.deserialize(stream);
|
|
359
|
+
const result = new globalThis.Map();
|
|
368
360
|
for (let i = 0; i < size; i++) {
|
|
369
361
|
const k = yield* key.deserialize(stream);
|
|
370
362
|
const v = yield* value.deserialize(stream);
|
|
@@ -374,17 +366,18 @@ export class Proto {
|
|
|
374
366
|
}
|
|
375
367
|
};
|
|
376
368
|
}
|
|
377
|
-
|
|
369
|
+
PROTO.Map = Map;
|
|
370
|
+
function Set(value) {
|
|
378
371
|
return {
|
|
379
372
|
*serialize(set, stream) {
|
|
380
|
-
yield*
|
|
373
|
+
yield* PROTO.UVarInt32.serialize(set.size, stream);
|
|
381
374
|
for (const [_, v] of set.entries()) {
|
|
382
375
|
yield* value.serialize(v, stream);
|
|
383
376
|
}
|
|
384
377
|
},
|
|
385
378
|
*deserialize(stream) {
|
|
386
|
-
const size = yield*
|
|
387
|
-
const result = new Set();
|
|
379
|
+
const size = yield* PROTO.UVarInt32.deserialize(stream);
|
|
380
|
+
const result = new globalThis.Set();
|
|
388
381
|
for (let i = 0; i < size; i++) {
|
|
389
382
|
const v = yield* value.deserialize(stream);
|
|
390
383
|
result.add(v);
|
|
@@ -393,164 +386,80 @@ export class Proto {
|
|
|
393
386
|
}
|
|
394
387
|
};
|
|
395
388
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
}
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
},
|
|
433
|
-
*deserialize(stream) {
|
|
434
|
-
return stream.read_uint16();
|
|
435
|
-
}
|
|
436
|
-
};
|
|
437
|
-
Proto.UInt32 = {
|
|
438
|
-
*serialize(value, stream) {
|
|
439
|
-
stream.write_uint32(value);
|
|
440
|
-
},
|
|
441
|
-
*deserialize(stream) {
|
|
442
|
-
return stream.read_uint32();
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
Proto.Float32 = {
|
|
446
|
-
*serialize(value, stream) {
|
|
447
|
-
stream.write_f32(value);
|
|
448
|
-
},
|
|
449
|
-
*deserialize(stream) {
|
|
450
|
-
return stream.read_f32();
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
Proto.Float64 = {
|
|
454
|
-
*serialize(value, stream) {
|
|
455
|
-
stream.write_f64(value);
|
|
456
|
-
},
|
|
457
|
-
*deserialize(stream) {
|
|
458
|
-
return stream.read_f64();
|
|
459
|
-
}
|
|
460
|
-
};
|
|
461
|
-
Proto.VarInt = {
|
|
462
|
-
*serialize(value, stream) {
|
|
463
|
-
while (value >= 0x80) {
|
|
464
|
-
stream.write((value & 0x7f) | 0x80);
|
|
465
|
-
value >>= 7;
|
|
466
|
-
yield;
|
|
467
|
-
}
|
|
468
|
-
stream.write(value);
|
|
469
|
-
},
|
|
470
|
-
*deserialize(stream) {
|
|
471
|
-
let value = 0;
|
|
472
|
-
let shift = 0;
|
|
473
|
-
let byte;
|
|
474
|
-
do {
|
|
475
|
-
byte = stream.read()[0];
|
|
476
|
-
value |= (byte & 0x7f) << shift;
|
|
477
|
-
shift += 7;
|
|
389
|
+
PROTO.Set = Set;
|
|
390
|
+
PROTO.Endpoint = PROTO.String;
|
|
391
|
+
PROTO.Header = PROTO.Object({
|
|
392
|
+
guid: PROTO.String,
|
|
393
|
+
encoding: PROTO.String,
|
|
394
|
+
index: PROTO.UVarInt32,
|
|
395
|
+
final: PROTO.Boolean
|
|
396
|
+
});
|
|
397
|
+
})(PROTO || (PROTO = {}));
|
|
398
|
+
export var NET;
|
|
399
|
+
(function (NET) {
|
|
400
|
+
const FRAG_MAX = 2048;
|
|
401
|
+
const ENCODING = 'mcbe-ipc:v3';
|
|
402
|
+
const ENDPOINTS = new Map();
|
|
403
|
+
function* serialize(byte_queue, max_size = Infinity) {
|
|
404
|
+
const uint8array = byte_queue.to_uint8array();
|
|
405
|
+
const result = [];
|
|
406
|
+
let acc_str = '';
|
|
407
|
+
let acc_size = 0;
|
|
408
|
+
for (let i = 0; i < uint8array.length; i++) {
|
|
409
|
+
const char_code = uint8array[i] | (uint8array[++i] << 8);
|
|
410
|
+
const utf16_size = char_code <= 0x7f ? 1 : char_code <= 0x7ff ? 2 : char_code <= 0xffff ? 3 : 4;
|
|
411
|
+
const char_size = char_code > 0xff ? utf16_size : 3;
|
|
412
|
+
if (acc_size + char_size > max_size) {
|
|
413
|
+
result.push(acc_str);
|
|
414
|
+
acc_str = '';
|
|
415
|
+
acc_size = 0;
|
|
416
|
+
}
|
|
417
|
+
if (char_code > 0xff) {
|
|
418
|
+
acc_str += String.fromCharCode(char_code);
|
|
419
|
+
acc_size += utf16_size;
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
acc_str += char_code.toString(16).padStart(2, '0').toUpperCase();
|
|
423
|
+
acc_size += 2;
|
|
424
|
+
}
|
|
478
425
|
yield;
|
|
479
|
-
} while (byte & 0x80);
|
|
480
|
-
return value;
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
Proto.String = {
|
|
484
|
-
*serialize(value, stream) {
|
|
485
|
-
yield* Proto.VarInt.serialize(value.length, stream);
|
|
486
|
-
for (let i = 0; i < value.length; i++) {
|
|
487
|
-
const code = value.charCodeAt(i);
|
|
488
|
-
yield* Proto.VarInt.serialize(code, stream);
|
|
489
426
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
const length = yield* Proto.VarInt.deserialize(stream);
|
|
493
|
-
let value = '';
|
|
494
|
-
for (let i = 0; i < length; i++) {
|
|
495
|
-
const code = yield* Proto.VarInt.deserialize(stream);
|
|
496
|
-
value += String.fromCharCode(code);
|
|
497
|
-
}
|
|
498
|
-
return value;
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
Proto.Boolean = {
|
|
502
|
-
*serialize(value, stream) {
|
|
503
|
-
stream.write(value ? 1 : 0);
|
|
504
|
-
},
|
|
505
|
-
*deserialize(stream) {
|
|
506
|
-
const value = stream.read()[0];
|
|
507
|
-
return value === 1;
|
|
427
|
+
result.push(acc_str);
|
|
428
|
+
return result;
|
|
508
429
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
430
|
+
NET.serialize = serialize;
|
|
431
|
+
function* deserialize(strings) {
|
|
432
|
+
const result = [];
|
|
433
|
+
for (let i = 0; i < strings.length; i++) {
|
|
434
|
+
const str = strings[i];
|
|
435
|
+
for (let j = 0; j < str.length; j++) {
|
|
436
|
+
const char_code = str.charCodeAt(j);
|
|
437
|
+
if (char_code <= 0xff) {
|
|
438
|
+
const hex = str[j] + str[++j];
|
|
439
|
+
const hex_code = parseInt(hex, 16);
|
|
440
|
+
result.push(hex_code & 0xff);
|
|
441
|
+
result.push(hex_code >> 8);
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
result.push(char_code & 0xff);
|
|
445
|
+
result.push(char_code >> 8);
|
|
446
|
+
}
|
|
447
|
+
yield;
|
|
448
|
+
}
|
|
515
449
|
yield;
|
|
516
450
|
}
|
|
517
|
-
|
|
518
|
-
*deserialize(stream) {
|
|
519
|
-
const length = yield* Proto.VarInt.deserialize(stream);
|
|
520
|
-
const result = new Uint8Array(length);
|
|
521
|
-
for (let i = 0; i < length; i++) {
|
|
522
|
-
result[i] = stream.read_uint8();
|
|
523
|
-
}
|
|
524
|
-
return result;
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
Proto.Date = {
|
|
528
|
-
*serialize(value, stream) {
|
|
529
|
-
yield* Proto.VarInt.serialize(value.getTime(), stream);
|
|
530
|
-
},
|
|
531
|
-
*deserialize(stream) {
|
|
532
|
-
return new Date(yield* Proto.VarInt.deserialize(stream));
|
|
451
|
+
return PROTO.ByteQueue.from_uint8array(new Uint8Array(result));
|
|
533
452
|
}
|
|
534
|
-
|
|
535
|
-
export var NET;
|
|
536
|
-
(function (NET) {
|
|
537
|
-
var ByteArray = SERDE.ByteArray;
|
|
538
|
-
const FRAG_MAX = 2048;
|
|
539
|
-
const Header = Proto.Object({
|
|
540
|
-
guid: Proto.String,
|
|
541
|
-
index: Proto.VarInt,
|
|
542
|
-
final: Proto.Boolean
|
|
543
|
-
});
|
|
544
|
-
const endpoint_map = new Map();
|
|
453
|
+
NET.deserialize = deserialize;
|
|
545
454
|
system.afterEvents.scriptEventReceive.subscribe(event => {
|
|
546
455
|
system.runJob((function* () {
|
|
547
456
|
const [serialized_endpoint, serialized_header] = event.id.split(':');
|
|
548
|
-
const endpoint_stream = yield*
|
|
549
|
-
const endpoint = yield*
|
|
550
|
-
const listeners =
|
|
457
|
+
const endpoint_stream = yield* PROTO.MIPS.deserialize(serialized_endpoint);
|
|
458
|
+
const endpoint = yield* PROTO.Endpoint.deserialize(endpoint_stream);
|
|
459
|
+
const listeners = ENDPOINTS.get(endpoint);
|
|
551
460
|
if (event.sourceType === ScriptEventSource.Server && listeners) {
|
|
552
|
-
const header_stream = yield*
|
|
553
|
-
const header = yield* Header.deserialize(header_stream);
|
|
461
|
+
const header_stream = yield* PROTO.MIPS.deserialize(serialized_header);
|
|
462
|
+
const header = yield* PROTO.Header.deserialize(header_stream);
|
|
554
463
|
for (let i = 0; i < listeners.length; i++) {
|
|
555
464
|
yield* listeners[i](header, event.message);
|
|
556
465
|
}
|
|
@@ -558,10 +467,10 @@ export var NET;
|
|
|
558
467
|
})());
|
|
559
468
|
});
|
|
560
469
|
function create_listener(endpoint, listener) {
|
|
561
|
-
let listeners =
|
|
470
|
+
let listeners = ENDPOINTS.get(endpoint);
|
|
562
471
|
if (!listeners) {
|
|
563
472
|
listeners = new Array();
|
|
564
|
-
|
|
473
|
+
ENDPOINTS.set(endpoint, listeners);
|
|
565
474
|
}
|
|
566
475
|
listeners.push(listener);
|
|
567
476
|
return () => {
|
|
@@ -569,7 +478,7 @@ export var NET;
|
|
|
569
478
|
if (idx !== -1)
|
|
570
479
|
listeners.splice(idx, 1);
|
|
571
480
|
if (listeners.length === 0) {
|
|
572
|
-
|
|
481
|
+
ENDPOINTS.delete(endpoint);
|
|
573
482
|
}
|
|
574
483
|
};
|
|
575
484
|
}
|
|
@@ -582,23 +491,23 @@ export var NET;
|
|
|
582
491
|
}
|
|
583
492
|
function* emit(endpoint, serializer, value) {
|
|
584
493
|
const guid = generate_id();
|
|
585
|
-
const endpoint_stream = new
|
|
586
|
-
yield*
|
|
587
|
-
const
|
|
494
|
+
const endpoint_stream = new PROTO.ByteQueue();
|
|
495
|
+
yield* PROTO.Endpoint.serialize(endpoint, endpoint_stream);
|
|
496
|
+
const serialized_endpoint = yield* PROTO.MIPS.serialize(endpoint_stream);
|
|
588
497
|
const RUN = function* (header, serialized_packet) {
|
|
589
|
-
const header_stream = new
|
|
590
|
-
yield* Header.serialize(header, header_stream);
|
|
591
|
-
const
|
|
498
|
+
const header_stream = new PROTO.ByteQueue();
|
|
499
|
+
yield* PROTO.Header.serialize(header, header_stream);
|
|
500
|
+
const serialized_header = yield* PROTO.MIPS.serialize(header_stream);
|
|
592
501
|
world
|
|
593
502
|
.getDimension('overworld')
|
|
594
503
|
.runCommand(`scriptevent ${serialized_endpoint}:${serialized_header} ${serialized_packet}`);
|
|
595
504
|
};
|
|
596
|
-
const packet_stream = new
|
|
505
|
+
const packet_stream = new PROTO.ByteQueue();
|
|
597
506
|
yield* serializer.serialize(value, packet_stream);
|
|
598
|
-
const serialized_packets = yield*
|
|
507
|
+
const serialized_packets = yield* serialize(packet_stream, FRAG_MAX);
|
|
599
508
|
for (let i = 0; i < serialized_packets.length; i++) {
|
|
600
509
|
const serialized_packet = serialized_packets[i];
|
|
601
|
-
yield* RUN({ guid, index: i, final: i === serialized_packets.length - 1 }, serialized_packet);
|
|
510
|
+
yield* RUN({ guid, encoding: ENCODING, index: i, final: i === serialized_packets.length - 1 }, serialized_packet);
|
|
602
511
|
}
|
|
603
512
|
}
|
|
604
513
|
NET.emit = emit;
|
|
@@ -616,7 +525,7 @@ export var NET;
|
|
|
616
525
|
fragment.serialized_packets[payload.index] = serialized_packet;
|
|
617
526
|
fragment.data_size += payload.index + 1;
|
|
618
527
|
if (fragment.size !== -1 && fragment.data_size === (fragment.size * (fragment.size + 1)) / 2) {
|
|
619
|
-
const stream = yield*
|
|
528
|
+
const stream = yield* deserialize(fragment.serialized_packets);
|
|
620
529
|
const value = yield* serializer.deserialize(stream);
|
|
621
530
|
yield* callback(value);
|
|
622
531
|
buffer.delete(payload.guid);
|
|
@@ -628,296 +537,6 @@ export var NET;
|
|
|
628
537
|
})(NET || (NET = {}));
|
|
629
538
|
export var IPC;
|
|
630
539
|
(function (IPC) {
|
|
631
|
-
const ConnectionSerializer = Proto.Object({
|
|
632
|
-
from: Proto.String,
|
|
633
|
-
bytes: Proto.UInt8Array
|
|
634
|
-
});
|
|
635
|
-
const HandshakeSynchronizeSerializer = Proto.Object({
|
|
636
|
-
from: Proto.String,
|
|
637
|
-
encryption_enabled: Proto.Boolean,
|
|
638
|
-
encryption_public_key: Proto.String,
|
|
639
|
-
encryption_prime: Proto.VarInt,
|
|
640
|
-
encryption_modulus: Proto.VarInt
|
|
641
|
-
});
|
|
642
|
-
const HandshakeAcknowledgeSerializer = Proto.Object({
|
|
643
|
-
from: Proto.String,
|
|
644
|
-
encryption_enabled: Proto.Boolean,
|
|
645
|
-
encryption_public_key: Proto.String
|
|
646
|
-
});
|
|
647
|
-
class Connection {
|
|
648
|
-
*MAYBE_ENCRYPT(bytes) {
|
|
649
|
-
return this._enc !== false ? yield* CRYPTO.encrypt(bytes, this._enc) : bytes;
|
|
650
|
-
}
|
|
651
|
-
*MAYBE_DECRYPT(bytes) {
|
|
652
|
-
return this._enc !== false ? yield* CRYPTO.decrypt(bytes, this._enc) : bytes;
|
|
653
|
-
}
|
|
654
|
-
get from() {
|
|
655
|
-
return this._from;
|
|
656
|
-
}
|
|
657
|
-
get to() {
|
|
658
|
-
return this._to;
|
|
659
|
-
}
|
|
660
|
-
constructor(from, to, encryption) {
|
|
661
|
-
this._from = from;
|
|
662
|
-
this._to = to;
|
|
663
|
-
this._enc = encryption;
|
|
664
|
-
this._terminators = new Array();
|
|
665
|
-
}
|
|
666
|
-
terminate(notify = true) {
|
|
667
|
-
const $ = this;
|
|
668
|
-
$._terminators.forEach(terminate => terminate());
|
|
669
|
-
$._terminators.length = 0;
|
|
670
|
-
if (notify) {
|
|
671
|
-
system.runJob(NET.emit(`ipc:${$._to}:terminate`, Proto.String, $._from));
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
send(channel, serializer, value) {
|
|
675
|
-
const $ = this;
|
|
676
|
-
system.runJob((function* () {
|
|
677
|
-
const stream = new SERDE.ByteArray();
|
|
678
|
-
yield* serializer.serialize(value, stream);
|
|
679
|
-
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array());
|
|
680
|
-
yield* NET.emit(`ipc:${$._to}:${channel}:send`, ConnectionSerializer, {
|
|
681
|
-
from: $._from,
|
|
682
|
-
bytes
|
|
683
|
-
});
|
|
684
|
-
})());
|
|
685
|
-
}
|
|
686
|
-
invoke(channel, serializer, value, deserializer) {
|
|
687
|
-
const $ = this;
|
|
688
|
-
system.runJob((function* () {
|
|
689
|
-
const stream = new SERDE.ByteArray();
|
|
690
|
-
yield* serializer.serialize(value, stream);
|
|
691
|
-
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array());
|
|
692
|
-
yield* NET.emit(`ipc:${$._to}:${channel}:invoke`, ConnectionSerializer, {
|
|
693
|
-
from: $._from,
|
|
694
|
-
bytes
|
|
695
|
-
});
|
|
696
|
-
})());
|
|
697
|
-
return new Promise(resolve => {
|
|
698
|
-
const terminate = NET.listen(`ipc:${$._from}:${channel}:handle`, ConnectionSerializer, function* (data) {
|
|
699
|
-
if (data.from === $._to) {
|
|
700
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
701
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
702
|
-
const value = yield* deserializer.deserialize(stream);
|
|
703
|
-
resolve(value);
|
|
704
|
-
terminate();
|
|
705
|
-
}
|
|
706
|
-
});
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
on(channel, deserializer, listener) {
|
|
710
|
-
const $ = this;
|
|
711
|
-
const terminate = NET.listen(`ipc:${$._from}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
712
|
-
if (data.from === $._to) {
|
|
713
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
714
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
715
|
-
const value = yield* deserializer.deserialize(stream);
|
|
716
|
-
listener(value);
|
|
717
|
-
}
|
|
718
|
-
});
|
|
719
|
-
$._terminators.push(terminate);
|
|
720
|
-
return terminate;
|
|
721
|
-
}
|
|
722
|
-
once(channel, deserializer, listener) {
|
|
723
|
-
const $ = this;
|
|
724
|
-
const terminate = NET.listen(`ipc:${$._from}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
725
|
-
if (data.from === $._to) {
|
|
726
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
727
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
728
|
-
const value = yield* deserializer.deserialize(stream);
|
|
729
|
-
listener(value);
|
|
730
|
-
terminate();
|
|
731
|
-
}
|
|
732
|
-
});
|
|
733
|
-
$._terminators.push(terminate);
|
|
734
|
-
return terminate;
|
|
735
|
-
}
|
|
736
|
-
handle(channel, deserializer, serializer, listener) {
|
|
737
|
-
const $ = this;
|
|
738
|
-
const terminate = NET.listen(`ipc:${$._from}:${channel}:invoke`, ConnectionSerializer, function* (data) {
|
|
739
|
-
if (data.from === $._to) {
|
|
740
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
|
|
741
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
742
|
-
const value = yield* deserializer.deserialize(stream);
|
|
743
|
-
const result = listener(value);
|
|
744
|
-
const return_stream = new SERDE.ByteArray();
|
|
745
|
-
yield* serializer.serialize(result, return_stream);
|
|
746
|
-
const return_bytes = yield* $.MAYBE_ENCRYPT(return_stream.to_uint8array());
|
|
747
|
-
yield* NET.emit(`ipc:${$._to}:${channel}:handle`, ConnectionSerializer, {
|
|
748
|
-
from: $._from,
|
|
749
|
-
bytes: return_bytes
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
|
-
});
|
|
753
|
-
$._terminators.push(terminate);
|
|
754
|
-
return terminate;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
IPC.Connection = Connection;
|
|
758
|
-
class ConnectionManager {
|
|
759
|
-
*MAYBE_ENCRYPT(bytes, encryption) {
|
|
760
|
-
return encryption !== false ? yield* CRYPTO.encrypt(bytes, encryption) : bytes;
|
|
761
|
-
}
|
|
762
|
-
*MAYBE_DECRYPT(bytes, encryption) {
|
|
763
|
-
return encryption !== false ? yield* CRYPTO.decrypt(bytes, encryption) : bytes;
|
|
764
|
-
}
|
|
765
|
-
get id() {
|
|
766
|
-
return this._id;
|
|
767
|
-
}
|
|
768
|
-
constructor(id, force_encryption = false) {
|
|
769
|
-
const $ = this;
|
|
770
|
-
this._id = id;
|
|
771
|
-
this._enc_map = new Map();
|
|
772
|
-
this._con_map = new Map();
|
|
773
|
-
this._enc_force = force_encryption;
|
|
774
|
-
NET.listen(`ipc:${this._id}:handshake:synchronize`, HandshakeSynchronizeSerializer, function* (data) {
|
|
775
|
-
const secret = CRYPTO.make_secret(data.encryption_modulus);
|
|
776
|
-
const public_key = yield* CRYPTO.make_public(secret, data.encryption_modulus, data.encryption_prime);
|
|
777
|
-
const enc = data.encryption_enabled || $._enc_force
|
|
778
|
-
? yield* CRYPTO.make_shared(secret, data.encryption_public_key, data.encryption_prime)
|
|
779
|
-
: false;
|
|
780
|
-
$._enc_map.set(data.from, enc);
|
|
781
|
-
yield* NET.emit(`ipc:${data.from}:handshake:acknowledge`, HandshakeAcknowledgeSerializer, {
|
|
782
|
-
from: $._id,
|
|
783
|
-
encryption_public_key: public_key,
|
|
784
|
-
encryption_enabled: $._enc_force
|
|
785
|
-
});
|
|
786
|
-
});
|
|
787
|
-
NET.listen(`ipc:${this._id}:terminate`, Proto.String, function* (value) {
|
|
788
|
-
$._enc_map.delete(value);
|
|
789
|
-
});
|
|
790
|
-
}
|
|
791
|
-
connect(to, encrypted = false, timeout = 20) {
|
|
792
|
-
const $ = this;
|
|
793
|
-
return new Promise((resolve, reject) => {
|
|
794
|
-
const con = this._con_map.get(to);
|
|
795
|
-
if (con !== undefined) {
|
|
796
|
-
con.terminate(false);
|
|
797
|
-
resolve(con);
|
|
798
|
-
}
|
|
799
|
-
else {
|
|
800
|
-
const secret = CRYPTO.make_secret();
|
|
801
|
-
system.runJob((function* () {
|
|
802
|
-
const public_key = yield* CRYPTO.make_public(secret);
|
|
803
|
-
yield* NET.emit(`ipc:${to}:handshake:synchronize`, HandshakeSynchronizeSerializer, {
|
|
804
|
-
from: $._id,
|
|
805
|
-
encryption_enabled: encrypted,
|
|
806
|
-
encryption_public_key: public_key,
|
|
807
|
-
encryption_prime: CRYPTO.PRIME,
|
|
808
|
-
encryption_modulus: CRYPTO.MOD
|
|
809
|
-
});
|
|
810
|
-
})());
|
|
811
|
-
function clear() {
|
|
812
|
-
terminate();
|
|
813
|
-
system.clearRun(timeout_handle);
|
|
814
|
-
}
|
|
815
|
-
const timeout_handle = system.runTimeout(() => {
|
|
816
|
-
reject();
|
|
817
|
-
clear();
|
|
818
|
-
}, timeout);
|
|
819
|
-
const terminate = NET.listen(`ipc:${this._id}:handshake:acknowledge`, HandshakeAcknowledgeSerializer, function* (data) {
|
|
820
|
-
if (data.from === to) {
|
|
821
|
-
const enc = data.encryption_enabled || encrypted
|
|
822
|
-
? yield* CRYPTO.make_shared(secret, data.encryption_public_key)
|
|
823
|
-
: false;
|
|
824
|
-
const new_con = new Connection($._id, to, enc);
|
|
825
|
-
$._con_map.set(to, new_con);
|
|
826
|
-
resolve(new_con);
|
|
827
|
-
clear();
|
|
828
|
-
}
|
|
829
|
-
});
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
send(channel, serializer, value) {
|
|
834
|
-
const $ = this;
|
|
835
|
-
system.runJob((function* () {
|
|
836
|
-
for (const [key, enc] of $._enc_map) {
|
|
837
|
-
const stream = new SERDE.ByteArray();
|
|
838
|
-
yield* serializer.serialize(value, stream);
|
|
839
|
-
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array(), enc);
|
|
840
|
-
yield* NET.emit(`ipc:${key}:${channel}:send`, ConnectionSerializer, {
|
|
841
|
-
from: $._id,
|
|
842
|
-
bytes
|
|
843
|
-
});
|
|
844
|
-
}
|
|
845
|
-
})());
|
|
846
|
-
}
|
|
847
|
-
invoke(channel, serializer, value, deserializer) {
|
|
848
|
-
const $ = this;
|
|
849
|
-
const promises = [];
|
|
850
|
-
for (const [key, enc] of $._enc_map) {
|
|
851
|
-
system.runJob((function* () {
|
|
852
|
-
const stream = new SERDE.ByteArray();
|
|
853
|
-
yield* serializer.serialize(value, stream);
|
|
854
|
-
const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array(), enc);
|
|
855
|
-
yield* NET.emit(`ipc:${key}:${channel}:invoke`, ConnectionSerializer, {
|
|
856
|
-
from: $._id,
|
|
857
|
-
bytes
|
|
858
|
-
});
|
|
859
|
-
})());
|
|
860
|
-
promises.push(new Promise(resolve => {
|
|
861
|
-
const terminate = NET.listen(`ipc:${$._id}:${channel}:handle`, ConnectionSerializer, function* (data) {
|
|
862
|
-
if (data.from === key) {
|
|
863
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
864
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
865
|
-
const value = yield* deserializer.deserialize(stream);
|
|
866
|
-
resolve(value);
|
|
867
|
-
terminate();
|
|
868
|
-
}
|
|
869
|
-
});
|
|
870
|
-
}));
|
|
871
|
-
}
|
|
872
|
-
return promises;
|
|
873
|
-
}
|
|
874
|
-
on(channel, deserializer, listener) {
|
|
875
|
-
const $ = this;
|
|
876
|
-
return NET.listen(`ipc:${$._id}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
877
|
-
const enc = $._enc_map.get(data.from);
|
|
878
|
-
if (enc !== undefined) {
|
|
879
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
880
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
881
|
-
const value = yield* deserializer.deserialize(stream);
|
|
882
|
-
listener(value);
|
|
883
|
-
}
|
|
884
|
-
});
|
|
885
|
-
}
|
|
886
|
-
once(channel, deserializer, listener) {
|
|
887
|
-
const $ = this;
|
|
888
|
-
const terminate = NET.listen(`ipc:${$._id}:${channel}:send`, ConnectionSerializer, function* (data) {
|
|
889
|
-
const enc = $._enc_map.get(data.from);
|
|
890
|
-
if (enc !== undefined) {
|
|
891
|
-
const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
892
|
-
const stream = SERDE.ByteArray.from_uint8array(bytes);
|
|
893
|
-
const value = yield* deserializer.deserialize(stream);
|
|
894
|
-
listener(value);
|
|
895
|
-
terminate();
|
|
896
|
-
}
|
|
897
|
-
});
|
|
898
|
-
return terminate;
|
|
899
|
-
}
|
|
900
|
-
handle(channel, deserializer, serializer, listener) {
|
|
901
|
-
const $ = this;
|
|
902
|
-
return NET.listen(`ipc:${$._id}:${channel}:invoke`, ConnectionSerializer, function* (data) {
|
|
903
|
-
const enc = $._enc_map.get(data.from);
|
|
904
|
-
if (enc !== undefined) {
|
|
905
|
-
const input_bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
|
|
906
|
-
const input_stream = SERDE.ByteArray.from_uint8array(input_bytes);
|
|
907
|
-
const input_value = yield* deserializer.deserialize(input_stream);
|
|
908
|
-
const result = listener(input_value);
|
|
909
|
-
const output_stream = new SERDE.ByteArray();
|
|
910
|
-
yield* serializer.serialize(result, output_stream);
|
|
911
|
-
const output_bytes = yield* $.MAYBE_ENCRYPT(output_stream.to_uint8array(), enc);
|
|
912
|
-
yield* NET.emit(`ipc:${data.from}:${channel}:handle`, ConnectionSerializer, {
|
|
913
|
-
from: $._id,
|
|
914
|
-
bytes: output_bytes
|
|
915
|
-
});
|
|
916
|
-
}
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
IPC.ConnectionManager = ConnectionManager;
|
|
921
540
|
/** Sends a message with `args` to `channel` */
|
|
922
541
|
function send(channel, serializer, value) {
|
|
923
542
|
system.runJob(NET.emit(`ipc:${channel}:send`, serializer, value));
|