mcbe-ipc 3.3.1 → 3.4.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/dist/ipc.d.ts CHANGED
@@ -51,6 +51,7 @@ export declare namespace PROTO {
51
51
  to_uint8array(): Uint8Array;
52
52
  }
53
53
  namespace MIPS {
54
+ function is_valid(str: string): boolean;
54
55
  function serialize(stream: PROTO.Buffer): Generator<void, string, void>;
55
56
  function deserialize(str: string): Generator<void, PROTO.Buffer, void>;
56
57
  }
@@ -81,6 +82,7 @@ export declare namespace PROTO {
81
82
  function Optional<T>(s: PROTO.Serializable<T>): PROTO.Serializable<T | undefined>;
82
83
  function Map<K, V>(kS: PROTO.Serializable<K>, vS: PROTO.Serializable<V>): PROTO.Serializable<Map<K, V>>;
83
84
  function Set<V>(s: PROTO.Serializable<V>): PROTO.Serializable<Set<V>>;
85
+ function Cached<V>(s: PROTO.Serializable<V>, depth?: number): PROTO.Serializable<V>;
84
86
  }
85
87
  export declare namespace NET {
86
88
  type Meta = {
package/dist/ipc.js CHANGED
@@ -107,6 +107,10 @@ export var PROTO;
107
107
  PROTO.Buffer = Buffer;
108
108
  let MIPS;
109
109
  (function (MIPS) {
110
+ function is_valid(str) {
111
+ return str.startsWith('(0x') && str.endsWith(')');
112
+ }
113
+ MIPS.is_valid = is_valid;
110
114
  function* serialize(stream) {
111
115
  const uint8array = stream.to_uint8array();
112
116
  let str = '(0x';
@@ -120,7 +124,7 @@ export var PROTO;
120
124
  }
121
125
  MIPS.serialize = serialize;
122
126
  function* deserialize(str) {
123
- if (str.startsWith('(0x') && str.endsWith(')')) {
127
+ if (is_valid(str)) {
124
128
  const buffer = new Buffer();
125
129
  const hex_str = str.slice(3, str.length - 1);
126
130
  for (let i = 0; i < hex_str.length; i++) {
@@ -402,6 +406,34 @@ export var PROTO;
402
406
  };
403
407
  }
404
408
  PROTO.Set = Set;
409
+ function Cached(s, depth = 16) {
410
+ const cache = new globalThis.Map();
411
+ return {
412
+ *serialize(value, stream) {
413
+ const hit = cache.get(value);
414
+ if (hit !== undefined) {
415
+ stream.write(hit);
416
+ cache.delete(value);
417
+ cache.set(value, hit);
418
+ }
419
+ else {
420
+ const buffer = new PROTO.Buffer();
421
+ yield* s.serialize(value, buffer);
422
+ const bytes = buffer.to_uint8array();
423
+ stream.write(bytes);
424
+ cache.set(value, bytes);
425
+ if (cache.size > depth) {
426
+ const first = cache.keys().next().value;
427
+ cache.delete(first);
428
+ }
429
+ }
430
+ },
431
+ *deserialize(stream) {
432
+ return yield* s.deserialize(stream);
433
+ }
434
+ };
435
+ }
436
+ PROTO.Cached = Cached;
405
437
  })(PROTO || (PROTO = {}));
406
438
  export var NET;
407
439
  (function (NET) {
@@ -474,10 +506,12 @@ export var NET;
474
506
  if (event.sourceType !== ScriptEventSource.Server)
475
507
  return;
476
508
  const [serialized_endpoint, serialized_header] = event.id.split(':');
509
+ if (!PROTO.MIPS.is_valid(serialized_endpoint))
510
+ return;
477
511
  const endpoint_stream = yield* PROTO.MIPS.deserialize(serialized_endpoint);
478
512
  const endpoint = yield* Endpoint.deserialize(endpoint_stream);
479
513
  const listeners = LISTENERS.get(endpoint);
480
- if (listeners !== undefined) {
514
+ if (listeners !== undefined && PROTO.MIPS.is_valid(serialized_header)) {
481
515
  const header_stream = yield* PROTO.MIPS.deserialize(serialized_header);
482
516
  const header = yield* Header.deserialize(header_stream);
483
517
  const errors = [];
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.3.1",
6
+ "version": "3.4.1",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/OmniacDev/MCBE-IPC.git"