mcbe-ipc 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +9 -0
- package/dist/ipc.d.ts +26 -22
- package/dist/ipc.js +69 -45
- package/package.json +5 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -23,4 +23,13 @@ npm install mcbe-ipc
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
|
|
26
|
+
|
|
27
|
+
> [!NOTE]
|
|
28
|
+
> Official documentation is still in progress.
|
|
29
|
+
>
|
|
30
|
+
> For now, you can [](https://deepwiki.com/OmniacDev/MCBE-IPC).
|
|
31
|
+
> Please note that while it is generally accurate, some of the more advanced sections may contain small inaccuracies.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
26
35
|
[^1]: Inter-Pack Communication
|
package/dist/ipc.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @license
|
|
3
3
|
* MIT License
|
|
4
4
|
*
|
|
5
|
-
* Copyright (c)
|
|
5
|
+
* Copyright (c) 2026 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,11 +23,14 @@
|
|
|
23
23
|
* SOFTWARE.
|
|
24
24
|
*/
|
|
25
25
|
export declare namespace PROTO {
|
|
26
|
-
interface
|
|
26
|
+
interface Serializer<T> {
|
|
27
27
|
serialize(value: T, stream: Buffer): Generator<void, void, void>;
|
|
28
|
+
}
|
|
29
|
+
interface Deserializer<T> {
|
|
28
30
|
deserialize(stream: Buffer): Generator<void, T, void>;
|
|
29
31
|
}
|
|
30
|
-
|
|
32
|
+
interface Serializable<T> extends Serializer<T>, Deserializer<T> {
|
|
33
|
+
}
|
|
31
34
|
class Buffer {
|
|
32
35
|
private _buffer;
|
|
33
36
|
private _data_view;
|
|
@@ -78,33 +81,34 @@ export declare namespace PROTO {
|
|
|
78
81
|
function Optional<T>(s: PROTO.Serializable<T>): PROTO.Serializable<T | undefined>;
|
|
79
82
|
function Map<K, V>(kS: PROTO.Serializable<K>, vS: PROTO.Serializable<V>): PROTO.Serializable<Map<K, V>>;
|
|
80
83
|
function Set<V>(s: PROTO.Serializable<V>): PROTO.Serializable<Set<V>>;
|
|
81
|
-
type Endpoint = string;
|
|
82
|
-
type Header = {
|
|
83
|
-
guid: string;
|
|
84
|
-
encoding: string;
|
|
85
|
-
index: number;
|
|
86
|
-
final: boolean;
|
|
87
|
-
};
|
|
88
|
-
const Endpoint: PROTO.Serializable<Endpoint>;
|
|
89
|
-
const Header: PROTO.Serializable<Header>;
|
|
90
84
|
}
|
|
91
85
|
export declare namespace NET {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
86
|
+
type Meta = {
|
|
87
|
+
guid: string;
|
|
88
|
+
signature: string;
|
|
89
|
+
};
|
|
90
|
+
const Meta: PROTO.Serializable<Meta>;
|
|
91
|
+
export const SIGNATURE: string;
|
|
92
|
+
export let FRAG_MAX: number;
|
|
93
|
+
export function serialize(buffer: PROTO.Buffer, max_size?: number): Generator<void, string[], void>;
|
|
94
|
+
export function deserialize(strings: string[]): Generator<void, PROTO.Buffer, void>;
|
|
95
|
+
export interface EmitOptions {
|
|
96
|
+
metaOverride?: Partial<Meta>;
|
|
97
|
+
}
|
|
98
|
+
export function emit<S>(endpoint: string, serializer: PROTO.Serializer<S>, value: NoInfer<S>, options?: EmitOptions): Generator<void, void, void>;
|
|
99
|
+
export function listen<D>(endpoint: string, deserializer: PROTO.Deserializer<D>, callback: (value: NoInfer<D>, meta: Meta) => Generator<void, void, void>): () => void;
|
|
100
|
+
export {};
|
|
97
101
|
}
|
|
98
102
|
export declare namespace IPC {
|
|
99
103
|
/** Sends a message with `args` to `channel` */
|
|
100
|
-
function send<S
|
|
104
|
+
function send<S>(channel: string, serializer: PROTO.Serializer<S>, value: NoInfer<S>): void;
|
|
101
105
|
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
102
|
-
function invoke<S
|
|
106
|
+
function invoke<S, D>(channel: string, serializer: PROTO.Serializer<S>, value: NoInfer<S>, deserializer: PROTO.Deserializer<D>): Promise<NoInfer<D>>;
|
|
103
107
|
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
104
|
-
function on<D
|
|
108
|
+
function on<D>(channel: string, deserializer: PROTO.Deserializer<D>, listener: (value: NoInfer<D>) => void): () => void;
|
|
105
109
|
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
106
|
-
function once<D
|
|
110
|
+
function once<D>(channel: string, deserializer: PROTO.Deserializer<D>, listener: (value: NoInfer<D>) => void): () => void;
|
|
107
111
|
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
108
|
-
function handle<D
|
|
112
|
+
function handle<D, S>(channel: string, deserializer: PROTO.Deserializer<D>, serializer: PROTO.Serializer<S>, listener: (value: NoInfer<D>) => NoInfer<S>): () => void;
|
|
109
113
|
}
|
|
110
114
|
export default IPC;
|
package/dist/ipc.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @license
|
|
3
3
|
* MIT License
|
|
4
4
|
*
|
|
5
|
-
* Copyright (c)
|
|
5
|
+
* Copyright (c) 2026 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,6 +23,14 @@
|
|
|
23
23
|
* SOFTWARE.
|
|
24
24
|
*/
|
|
25
25
|
import { ScriptEventSource, system } from '@minecraft/server';
|
|
26
|
+
var UTIL;
|
|
27
|
+
(function (UTIL) {
|
|
28
|
+
function generate_id() {
|
|
29
|
+
const r = (Math.random() * 0x100000000) >>> 0;
|
|
30
|
+
return r.toString(16).padStart(8, '0').toUpperCase();
|
|
31
|
+
}
|
|
32
|
+
UTIL.generate_id = generate_id;
|
|
33
|
+
})(UTIL || (UTIL = {}));
|
|
26
34
|
export var PROTO;
|
|
27
35
|
(function (PROTO) {
|
|
28
36
|
class Buffer {
|
|
@@ -394,19 +402,22 @@ export var PROTO;
|
|
|
394
402
|
};
|
|
395
403
|
}
|
|
396
404
|
PROTO.Set = Set;
|
|
397
|
-
|
|
398
|
-
|
|
405
|
+
})(PROTO || (PROTO = {}));
|
|
406
|
+
export var NET;
|
|
407
|
+
(function (NET) {
|
|
408
|
+
const Endpoint = PROTO.String;
|
|
409
|
+
const Meta = PROTO.Object({
|
|
399
410
|
guid: PROTO.String,
|
|
400
|
-
|
|
411
|
+
signature: PROTO.String
|
|
412
|
+
});
|
|
413
|
+
const Header = PROTO.Object({
|
|
414
|
+
meta: Meta,
|
|
401
415
|
index: PROTO.UVarInt32,
|
|
402
416
|
final: PROTO.Boolean
|
|
403
417
|
});
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
(function (NET) {
|
|
418
|
+
const LISTENERS = new Map();
|
|
419
|
+
NET.SIGNATURE = 'mcbe-ipc:v3';
|
|
407
420
|
NET.FRAG_MAX = 2048;
|
|
408
|
-
const ENCODING = 'mcbe-ipc:v3';
|
|
409
|
-
const ENDPOINTS = new Map();
|
|
410
421
|
function* serialize(buffer, max_size = Infinity) {
|
|
411
422
|
const uint8array = buffer.to_uint8array();
|
|
412
423
|
const result = [];
|
|
@@ -464,11 +475,11 @@ export var NET;
|
|
|
464
475
|
return;
|
|
465
476
|
const [serialized_endpoint, serialized_header] = event.id.split(':');
|
|
466
477
|
const endpoint_stream = yield* PROTO.MIPS.deserialize(serialized_endpoint);
|
|
467
|
-
const endpoint = yield*
|
|
468
|
-
const listeners =
|
|
478
|
+
const endpoint = yield* Endpoint.deserialize(endpoint_stream);
|
|
479
|
+
const listeners = LISTENERS.get(endpoint);
|
|
469
480
|
if (listeners !== undefined) {
|
|
470
481
|
const header_stream = yield* PROTO.MIPS.deserialize(serialized_header);
|
|
471
|
-
const header = yield*
|
|
482
|
+
const header = yield* Header.deserialize(header_stream);
|
|
472
483
|
const errors = [];
|
|
473
484
|
for (let i = 0; i < listeners.length; i++) {
|
|
474
485
|
try {
|
|
@@ -483,11 +494,11 @@ export var NET;
|
|
|
483
494
|
}
|
|
484
495
|
})());
|
|
485
496
|
});
|
|
486
|
-
function
|
|
487
|
-
let listeners =
|
|
488
|
-
if (
|
|
497
|
+
function register(endpoint, listener) {
|
|
498
|
+
let listeners = LISTENERS.get(endpoint);
|
|
499
|
+
if (listeners === undefined) {
|
|
489
500
|
listeners = new Array();
|
|
490
|
-
|
|
501
|
+
LISTENERS.set(endpoint, listeners);
|
|
491
502
|
}
|
|
492
503
|
listeners.push(listener);
|
|
493
504
|
return () => {
|
|
@@ -495,56 +506,54 @@ export var NET;
|
|
|
495
506
|
if (idx !== -1)
|
|
496
507
|
listeners.splice(idx, 1);
|
|
497
508
|
if (listeners.length === 0) {
|
|
498
|
-
|
|
509
|
+
LISTENERS.delete(endpoint);
|
|
499
510
|
}
|
|
500
511
|
};
|
|
501
512
|
}
|
|
502
|
-
function
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
((r >> 8) & 0xff).toString(16).padStart(2, '0') +
|
|
506
|
-
((r >> 16) & 0xff).toString(16).padStart(2, '0') +
|
|
507
|
-
((r >> 24) & 0xff).toString(16).padStart(2, '0')).toUpperCase();
|
|
508
|
-
}
|
|
509
|
-
function* emit(endpoint, serializer, value) {
|
|
510
|
-
const guid = generate_id();
|
|
513
|
+
function* emit(endpoint, serializer, value, options) {
|
|
514
|
+
const guid = options?.metaOverride?.guid ?? UTIL.generate_id();
|
|
515
|
+
const signature = options?.metaOverride?.signature ?? NET.SIGNATURE;
|
|
511
516
|
const endpoint_stream = new PROTO.Buffer();
|
|
512
|
-
yield*
|
|
517
|
+
yield* Endpoint.serialize(endpoint, endpoint_stream);
|
|
513
518
|
const serialized_endpoint = yield* PROTO.MIPS.serialize(endpoint_stream);
|
|
514
519
|
const packet_stream = new PROTO.Buffer();
|
|
515
520
|
yield* serializer.serialize(value, packet_stream);
|
|
516
521
|
const serialized_packets = yield* serialize(packet_stream, NET.FRAG_MAX);
|
|
517
522
|
for (let i = 0; i < serialized_packets.length; i++) {
|
|
518
523
|
const serialized_packet = serialized_packets[i];
|
|
519
|
-
const header = {
|
|
524
|
+
const header = {
|
|
525
|
+
meta: { guid, signature },
|
|
526
|
+
index: i,
|
|
527
|
+
final: i === serialized_packets.length - 1
|
|
528
|
+
};
|
|
520
529
|
const header_stream = new PROTO.Buffer();
|
|
521
|
-
yield*
|
|
530
|
+
yield* Header.serialize(header, header_stream);
|
|
522
531
|
const serialized_header = yield* PROTO.MIPS.serialize(header_stream);
|
|
523
532
|
system.sendScriptEvent(`${serialized_endpoint}:${serialized_header}`, serialized_packet);
|
|
524
533
|
}
|
|
525
534
|
}
|
|
526
535
|
NET.emit = emit;
|
|
527
|
-
function listen(endpoint,
|
|
536
|
+
function listen(endpoint, deserializer, callback) {
|
|
528
537
|
const buffer = new Map();
|
|
529
|
-
const listener = function* (
|
|
530
|
-
let fragment = buffer.get(
|
|
538
|
+
const listener = function* (header, serialized_packet) {
|
|
539
|
+
let fragment = buffer.get(header.meta.guid);
|
|
531
540
|
if (!fragment) {
|
|
532
541
|
fragment = { size: -1, serialized_packets: [], data_size: 0 };
|
|
533
|
-
buffer.set(
|
|
542
|
+
buffer.set(header.meta.guid, fragment);
|
|
534
543
|
}
|
|
535
|
-
if (
|
|
536
|
-
fragment.size =
|
|
544
|
+
if (header.final) {
|
|
545
|
+
fragment.size = header.index + 1;
|
|
537
546
|
}
|
|
538
|
-
fragment.serialized_packets[
|
|
539
|
-
fragment.data_size +=
|
|
547
|
+
fragment.serialized_packets[header.index] = serialized_packet;
|
|
548
|
+
fragment.data_size += header.index + 1;
|
|
540
549
|
if (fragment.size !== -1 && fragment.data_size === (fragment.size * (fragment.size + 1)) / 2) {
|
|
541
550
|
const stream = yield* deserialize(fragment.serialized_packets);
|
|
542
|
-
const value = yield*
|
|
543
|
-
yield* callback(value);
|
|
544
|
-
buffer.delete(
|
|
551
|
+
const value = yield* deserializer.deserialize(stream);
|
|
552
|
+
yield* callback(value, header.meta);
|
|
553
|
+
buffer.delete(header.meta.guid);
|
|
545
554
|
}
|
|
546
555
|
};
|
|
547
|
-
return
|
|
556
|
+
return register(endpoint, listener);
|
|
548
557
|
}
|
|
549
558
|
NET.listen = listen;
|
|
550
559
|
})(NET || (NET = {}));
|
|
@@ -557,12 +566,20 @@ export var IPC;
|
|
|
557
566
|
IPC.send = send;
|
|
558
567
|
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
559
568
|
function invoke(channel, serializer, value, deserializer) {
|
|
560
|
-
|
|
569
|
+
const id = UTIL.generate_id();
|
|
561
570
|
return new Promise(resolve => {
|
|
562
|
-
const terminate = NET.listen(`ipc:${channel}:handle`, deserializer, function* (value) {
|
|
571
|
+
const terminate = NET.listen(`ipc:${channel}:handle`, deserializer, function* (value, meta) {
|
|
572
|
+
if (meta.signature.includes(`+correlation`) && meta.guid !== id)
|
|
573
|
+
return;
|
|
563
574
|
resolve(value);
|
|
564
575
|
terminate();
|
|
565
576
|
});
|
|
577
|
+
system.runJob(NET.emit(`ipc:${channel}:invoke`, serializer, value, {
|
|
578
|
+
metaOverride: {
|
|
579
|
+
guid: id,
|
|
580
|
+
signature: `${NET.SIGNATURE}+correlation`
|
|
581
|
+
}
|
|
582
|
+
}));
|
|
566
583
|
});
|
|
567
584
|
}
|
|
568
585
|
IPC.invoke = invoke;
|
|
@@ -584,9 +601,16 @@ export var IPC;
|
|
|
584
601
|
IPC.once = once;
|
|
585
602
|
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
586
603
|
function handle(channel, deserializer, serializer, listener) {
|
|
587
|
-
return NET.listen(`ipc:${channel}:invoke`, deserializer, function* (value) {
|
|
604
|
+
return NET.listen(`ipc:${channel}:invoke`, deserializer, function* (value, meta) {
|
|
588
605
|
const result = listener(value);
|
|
589
|
-
yield* NET.emit(`ipc:${channel}:handle`, serializer, result
|
|
606
|
+
yield* NET.emit(`ipc:${channel}:handle`, serializer, result, {
|
|
607
|
+
metaOverride: meta.signature.includes(`+correlation`)
|
|
608
|
+
? {
|
|
609
|
+
guid: meta.guid,
|
|
610
|
+
signature: `${NET.SIGNATURE}+correlation`
|
|
611
|
+
}
|
|
612
|
+
: undefined
|
|
613
|
+
});
|
|
590
614
|
});
|
|
591
615
|
}
|
|
592
616
|
IPC.handle = handle;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "OmniacDev",
|
|
4
4
|
"description": "IPC system for MCBE Script API projects",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "3.
|
|
6
|
+
"version": "3.3.0",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/OmniacDev/MCBE-IPC.git"
|
|
@@ -20,12 +20,15 @@
|
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc",
|
|
23
|
+
"test": "vitest",
|
|
24
|
+
"bench": "vitest bench",
|
|
23
25
|
"format": "prettier --write src",
|
|
24
26
|
"prepublishOnly": "npm run build"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"prettier": "^3.3.3",
|
|
28
|
-
"typescript": "^5.5.4"
|
|
30
|
+
"typescript": "^5.5.4",
|
|
31
|
+
"vitest": "^4.0.16"
|
|
29
32
|
},
|
|
30
33
|
"dependencies": {
|
|
31
34
|
"@minecraft/server": "^1.18.0"
|