@statewalker/webrun-msgpack 0.2.2 → 0.3.2

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/index.js CHANGED
@@ -1,13 +1,540 @@
1
- import msgpack from "@ygoe/msgpack";
1
+ //#region src/msgpack-core.ts
2
+ /*!
3
+ * MessagePack serializer and deserializer.
4
+ *
5
+ * A TypeScript port of msgpack.js by Yves Goergen:
6
+ * https://github.com/ygoe/msgpack.js
7
+ * ported from commit 05733cfb43a2974cf669f0eb8693f43b548bdcd4 (2024-04-16) on `master`,
8
+ * which carries three fixes that the last npm release, `@ygoe/msgpack@1.0.3`, does not.
9
+ * Every change made to the upstream logic is listed in this package's README, section
10
+ * "Provenance and credits".
11
+ *
12
+ * Original work: Copyright © 2019, Yves Goergen, https://unclassified.software/source/msgpack-js
13
+ * TypeScript port and modifications: Copyright (c) 2026 statewalker
14
+ *
15
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
16
+ * associated documentation files (the “Software”), to deal in the Software without restriction,
17
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
18
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
19
+ * furnished to do so, subject to the following conditions:
20
+ *
21
+ * The above copyright notice and this permission notice shall be included in all copies or
22
+ * substantial portions of the Software.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
25
+ * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ */
30
+ const pow32 = 4294967296;
31
+ /**
32
+ * Serializes a value to a MessagePack byte array.
33
+ *
34
+ * The result is a view over a larger internal buffer; send the view, not its `.buffer`.
35
+ */
36
+ function serialize(data, options) {
37
+ if (options?.multiple && !Array.isArray(data)) throw new Error("Invalid argument type: Expected an Array to serialize multiple values.");
38
+ let floatBuffer;
39
+ let floatView;
40
+ let array = /* @__PURE__ */ new Uint8Array(128);
41
+ let length = 0;
42
+ if (options?.multiple) {
43
+ const values = data;
44
+ for (let i = 0; i < values.length; i++) append(values[i]);
45
+ } else append(data);
46
+ return array.subarray(0, length);
47
+ function append(data, isReplacement) {
48
+ switch (typeof data) {
49
+ case "undefined":
50
+ appendNull();
51
+ break;
52
+ case "boolean":
53
+ appendBoolean(data);
54
+ break;
55
+ case "number":
56
+ appendNumber(data);
57
+ break;
58
+ case "string":
59
+ appendString(data);
60
+ break;
61
+ case "object":
62
+ if (data === null) appendNull();
63
+ else if (data instanceof Date) appendDate(data);
64
+ else if (Array.isArray(data)) appendArray(data);
65
+ else if (data instanceof Uint8Array || data instanceof Uint8ClampedArray) appendBinArray(data);
66
+ else if (data instanceof Int8Array || data instanceof Int16Array || data instanceof Uint16Array || data instanceof Int32Array || data instanceof Uint32Array || data instanceof Float32Array || data instanceof Float64Array) appendArray(data);
67
+ else appendObject(data);
68
+ break;
69
+ default: {
70
+ const replacement = options?.invalidTypeReplacement;
71
+ if (!isReplacement && options && replacement) {
72
+ if (typeof replacement === "function") append(replacement(data), true);
73
+ else append(replacement, true);
74
+ } else throw new Error(`Invalid argument type: The type '${typeof data}' cannot be serialized.`);
75
+ }
76
+ }
77
+ }
78
+ function appendNull() {
79
+ appendByte(192);
80
+ }
81
+ function appendBoolean(data) {
82
+ appendByte(data ? 195 : 194);
83
+ }
84
+ function appendNumber(data) {
85
+ if (Number.isFinite(data) && Number.isSafeInteger(data)) {
86
+ if (data >= 0 && data <= 127) appendByte(data);
87
+ else if (data < 0 && data >= -32) appendByte(data);
88
+ else if (data > 0 && data <= 255) appendBytes([204, data]);
89
+ else if (data >= -128 && data <= 127) appendBytes([208, data]);
90
+ else if (data > 0 && data <= 65535) appendBytes([
91
+ 205,
92
+ data >>> 8,
93
+ data
94
+ ]);
95
+ else if (data >= -32768 && data <= 32767) appendBytes([
96
+ 209,
97
+ data >>> 8,
98
+ data
99
+ ]);
100
+ else if (data > 0 && data <= 4294967295) appendBytes([
101
+ 206,
102
+ data >>> 24,
103
+ data >>> 16,
104
+ data >>> 8,
105
+ data
106
+ ]);
107
+ else if (data >= -2147483648 && data <= 2147483647) appendBytes([
108
+ 210,
109
+ data >>> 24,
110
+ data >>> 16,
111
+ data >>> 8,
112
+ data
113
+ ]);
114
+ else if (data > 0 && data <= 2 ** 64) {
115
+ appendByte(207);
116
+ appendInt64(data);
117
+ } else if (data >= -(2 ** 63) && data <= 2 ** 63) {
118
+ appendByte(211);
119
+ appendInt64(data);
120
+ } else if (data < 0) appendBytes([
121
+ 211,
122
+ 128,
123
+ 0,
124
+ 0,
125
+ 0,
126
+ 0,
127
+ 0,
128
+ 0,
129
+ 0
130
+ ]);
131
+ else appendBytes([
132
+ 207,
133
+ 255,
134
+ 255,
135
+ 255,
136
+ 255,
137
+ 255,
138
+ 255,
139
+ 255,
140
+ 255
141
+ ]);
142
+ } else {
143
+ if (!floatView || !floatBuffer) {
144
+ floatBuffer = /* @__PURE__ */ new ArrayBuffer(8);
145
+ floatView = new DataView(floatBuffer);
146
+ }
147
+ floatView.setFloat64(0, data);
148
+ appendByte(203);
149
+ appendBytes(new Uint8Array(floatBuffer));
150
+ }
151
+ }
152
+ function appendString(data) {
153
+ const bytes = encodeUtf8(data);
154
+ const length = bytes.length;
155
+ if (length <= 31) appendByte(160 + length);
156
+ else if (length <= 255) appendBytes([217, length]);
157
+ else if (length <= 65535) appendBytes([
158
+ 218,
159
+ length >>> 8,
160
+ length
161
+ ]);
162
+ else appendBytes([
163
+ 219,
164
+ length >>> 24,
165
+ length >>> 16,
166
+ length >>> 8,
167
+ length
168
+ ]);
169
+ appendBytes(bytes);
170
+ }
171
+ function appendArray(data) {
172
+ const length = data.length;
173
+ if (length <= 15) appendByte(144 + length);
174
+ else if (length <= 65535) appendBytes([
175
+ 220,
176
+ length >>> 8,
177
+ length
178
+ ]);
179
+ else appendBytes([
180
+ 221,
181
+ length >>> 24,
182
+ length >>> 16,
183
+ length >>> 8,
184
+ length
185
+ ]);
186
+ for (let index = 0; index < length; index++) append(data[index]);
187
+ }
188
+ function appendBinArray(data) {
189
+ const length = data.length;
190
+ if (length <= 255) appendBytes([196, length]);
191
+ else if (length <= 65535) appendBytes([
192
+ 197,
193
+ length >>> 8,
194
+ length
195
+ ]);
196
+ else appendBytes([
197
+ 198,
198
+ length >>> 24,
199
+ length >>> 16,
200
+ length >>> 8,
201
+ length
202
+ ]);
203
+ appendBytes(data);
204
+ }
205
+ function appendObject(data) {
206
+ const keys = Object.keys(data);
207
+ let length = 0;
208
+ for (const key of keys) if (data[key] !== void 0) length++;
209
+ if (length <= 15) appendByte(128 + length);
210
+ else if (length <= 65535) appendBytes([
211
+ 222,
212
+ length >>> 8,
213
+ length
214
+ ]);
215
+ else appendBytes([
216
+ 223,
217
+ length >>> 24,
218
+ length >>> 16,
219
+ length >>> 8,
220
+ length
221
+ ]);
222
+ for (const key of keys) {
223
+ const value = data[key];
224
+ if (value !== void 0) {
225
+ append(key);
226
+ append(value);
227
+ }
228
+ }
229
+ }
230
+ function appendDate(data) {
231
+ const time = data.getTime();
232
+ if (Number.isNaN(time)) throw new Error("Invalid argument: an invalid Date cannot be serialized.");
233
+ const sec = Math.floor(time / 1e3);
234
+ const ns = (time - sec * 1e3) * 1e6;
235
+ if (ns === 0 && sec >= 0 && sec < 4294967296) appendBytes([
236
+ 214,
237
+ 255,
238
+ sec >>> 24,
239
+ sec >>> 16,
240
+ sec >>> 8,
241
+ sec
242
+ ]);
243
+ else if (sec >= 0 && sec < 17179869184) appendBytes([
244
+ 215,
245
+ 255,
246
+ ns >>> 22,
247
+ ns >>> 14,
248
+ ns >>> 6,
249
+ ns << 2 >>> 0 | sec / pow32,
250
+ sec >>> 24,
251
+ sec >>> 16,
252
+ sec >>> 8,
253
+ sec
254
+ ]);
255
+ else {
256
+ appendBytes([
257
+ 199,
258
+ 12,
259
+ 255,
260
+ ns >>> 24,
261
+ ns >>> 16,
262
+ ns >>> 8,
263
+ ns
264
+ ]);
265
+ appendInt64(sec);
266
+ }
267
+ }
268
+ function appendByte(byte) {
269
+ if (array.length < length + 1) {
270
+ let newLength = array.length * 2;
271
+ while (newLength < length + 1) newLength *= 2;
272
+ const newArray = new Uint8Array(newLength);
273
+ newArray.set(array);
274
+ array = newArray;
275
+ }
276
+ array[length] = byte;
277
+ length++;
278
+ }
279
+ function appendBytes(bytes) {
280
+ if (array.length < length + bytes.length) {
281
+ let newLength = array.length * 2;
282
+ while (newLength < length + bytes.length) newLength *= 2;
283
+ const newArray = new Uint8Array(newLength);
284
+ newArray.set(array);
285
+ array = newArray;
286
+ }
287
+ array.set(bytes, length);
288
+ length += bytes.length;
289
+ }
290
+ function appendInt64(value) {
291
+ let hi;
292
+ let lo;
293
+ if (value >= 0) {
294
+ hi = value / pow32;
295
+ lo = value % pow32;
296
+ } else {
297
+ value++;
298
+ hi = Math.abs(value) / pow32;
299
+ lo = Math.abs(value) % pow32;
300
+ hi = ~hi;
301
+ lo = ~lo;
302
+ }
303
+ appendBytes([
304
+ hi >>> 24,
305
+ hi >>> 16,
306
+ hi >>> 8,
307
+ hi,
308
+ lo >>> 24,
309
+ lo >>> 16,
310
+ lo >>> 8,
311
+ lo
312
+ ]);
313
+ }
314
+ }
315
+ /**
316
+ * Deserializes a MessagePack byte array to a value.
317
+ *
318
+ * `bin` values are returned as `Uint8Array` views into the input, not copies.
319
+ */
320
+ function deserialize(input, options) {
321
+ let pos = 0;
322
+ let array;
323
+ if (input instanceof Uint8Array) array = input;
324
+ else if (input instanceof ArrayBuffer) array = new Uint8Array(input);
325
+ else if (typeof input !== "object" || input === null || typeof input.length === "undefined") throw new Error("Invalid argument type: Expected a byte array (Array or Uint8Array) to deserialize.");
326
+ else array = new Uint8Array(input);
327
+ if (!array.length) throw new Error("Invalid argument: The byte array to deserialize is empty.");
328
+ let data;
329
+ if (options?.multiple) {
330
+ const values = [];
331
+ while (pos < array.length) values.push(read());
332
+ data = values;
333
+ } else data = read();
334
+ return data;
335
+ function need(size) {
336
+ if (pos + size > array.length) throw new RangeError(`Insufficient data: ${size} byte(s) needed at index ${pos} in the MessagePack binary data (length ${array.length}).`);
337
+ }
338
+ function read() {
339
+ need(1);
340
+ const byte = array[pos++];
341
+ if (byte >= 0 && byte <= 127) return byte;
342
+ if (byte >= 128 && byte <= 143) return readMap(byte - 128);
343
+ if (byte >= 144 && byte <= 159) return readArray(byte - 144);
344
+ if (byte >= 160 && byte <= 191) return readStr(byte - 160);
345
+ if (byte === 192) return null;
346
+ if (byte === 193) throw new Error("Invalid byte code 0xc1 found.");
347
+ if (byte === 194) return false;
348
+ if (byte === 195) return true;
349
+ if (byte === 196) return readBin(-1, 1);
350
+ if (byte === 197) return readBin(-1, 2);
351
+ if (byte === 198) return readBin(-1, 4);
352
+ if (byte === 199) return readExt(-1, 1);
353
+ if (byte === 200) return readExt(-1, 2);
354
+ if (byte === 201) return readExt(-1, 4);
355
+ if (byte === 202) return readFloat(4);
356
+ if (byte === 203) return readFloat(8);
357
+ if (byte === 204) return readUInt(1);
358
+ if (byte === 205) return readUInt(2);
359
+ if (byte === 206) return readUInt(4);
360
+ if (byte === 207) return readUInt(8);
361
+ if (byte === 208) return readInt(1);
362
+ if (byte === 209) return readInt(2);
363
+ if (byte === 210) return readInt(4);
364
+ if (byte === 211) return readInt(8);
365
+ if (byte === 212) return readExt(1);
366
+ if (byte === 213) return readExt(2);
367
+ if (byte === 214) return readExt(4);
368
+ if (byte === 215) return readExt(8);
369
+ if (byte === 216) return readExt(16);
370
+ if (byte === 217) return readStr(-1, 1);
371
+ if (byte === 218) return readStr(-1, 2);
372
+ if (byte === 219) return readStr(-1, 4);
373
+ if (byte === 220) return readArray(-1, 2);
374
+ if (byte === 221) return readArray(-1, 4);
375
+ if (byte === 222) return readMap(-1, 2);
376
+ if (byte === 223) return readMap(-1, 4);
377
+ if (byte >= 224 && byte <= 255) return byte - 256;
378
+ throw new Error(`Invalid byte value '${byte}' at index ${pos - 1} in the MessagePack binary data (length ${array.length}): Expecting a range of 0 to 255. This is not a byte array.`);
379
+ }
380
+ function readInt(size) {
381
+ need(size);
382
+ let value = 0;
383
+ let first = true;
384
+ while (size-- > 0) if (first) {
385
+ const byte = array[pos++];
386
+ value += byte & 127;
387
+ if (byte & 128) value -= 128;
388
+ first = false;
389
+ } else {
390
+ value *= 256;
391
+ value += array[pos++];
392
+ }
393
+ return value;
394
+ }
395
+ function readUInt(size) {
396
+ need(size);
397
+ let value = 0;
398
+ while (size-- > 0) {
399
+ value *= 256;
400
+ value += array[pos++];
401
+ }
402
+ return value;
403
+ }
404
+ function readFloat(size) {
405
+ need(size);
406
+ const view = new DataView(array.buffer, pos + array.byteOffset, size);
407
+ pos += size;
408
+ return size === 4 ? view.getFloat32(0, false) : view.getFloat64(0, false);
409
+ }
410
+ function readBin(size, lengthSize) {
411
+ if (size < 0) size = readUInt(lengthSize);
412
+ need(size);
413
+ const data = array.subarray(pos, pos + size);
414
+ pos += size;
415
+ return data;
416
+ }
417
+ function readMap(size, lengthSize) {
418
+ if (size < 0) size = readUInt(lengthSize);
419
+ const data = {};
420
+ while (size-- > 0) {
421
+ const key = String(read());
422
+ const value = read();
423
+ if (key === "__proto__") Object.defineProperty(data, key, {
424
+ value,
425
+ enumerable: true,
426
+ writable: true,
427
+ configurable: true
428
+ });
429
+ else data[key] = value;
430
+ }
431
+ return data;
432
+ }
433
+ function readArray(size, lengthSize) {
434
+ if (size < 0) size = readUInt(lengthSize);
435
+ const data = [];
436
+ while (size-- > 0) data.push(read());
437
+ return data;
438
+ }
439
+ function readStr(size, lengthSize) {
440
+ if (size < 0) size = readUInt(lengthSize);
441
+ need(size);
442
+ const start = pos;
443
+ pos += size;
444
+ return decodeUtf8(array, start, size);
445
+ }
446
+ function readExt(size, lengthSize) {
447
+ if (size < 0) size = readUInt(lengthSize);
448
+ const type = readUInt(1);
449
+ const data = readBin(size);
450
+ switch (type) {
451
+ case 255: return readExtDate(data);
452
+ }
453
+ return {
454
+ type,
455
+ data
456
+ };
457
+ }
458
+ function readExtDate(data) {
459
+ const b = (i) => data[i];
460
+ if (data.length === 4) {
461
+ const sec = (b(0) << 24 >>> 0) + (b(1) << 16 >>> 0) + (b(2) << 8 >>> 0) + b(3);
462
+ return /* @__PURE__ */ new Date(sec * 1e3);
463
+ }
464
+ if (data.length === 8) {
465
+ const ns = (b(0) << 22 >>> 0) + (b(1) << 14 >>> 0) + (b(2) << 6 >>> 0) + (b(3) >>> 2);
466
+ const sec = (b(3) & 3) * pow32 + (b(4) << 24 >>> 0) + (b(5) << 16 >>> 0) + (b(6) << 8 >>> 0) + b(7);
467
+ return new Date(sec * 1e3 + Math.floor(ns / 1e6));
468
+ }
469
+ if (data.length === 12) {
470
+ const ns = (b(0) << 24 >>> 0) + (b(1) << 16 >>> 0) + (b(2) << 8 >>> 0) + b(3);
471
+ pos -= 8;
472
+ const sec = readInt(8);
473
+ return new Date(sec * 1e3 + Math.floor(ns / 1e6));
474
+ }
475
+ throw new Error("Invalid data length for a date value.");
476
+ }
477
+ }
478
+ /** Encodes a string to UTF-8 bytes. */
479
+ function encodeUtf8(str) {
480
+ let ascii = true;
481
+ const length = str.length;
482
+ for (let x = 0; x < length; x++) if (str.charCodeAt(x) > 127) {
483
+ ascii = false;
484
+ break;
485
+ }
486
+ let i = 0;
487
+ const bytes = new Uint8Array(str.length * (ascii ? 1 : 4));
488
+ for (let ci = 0; ci !== length; ci++) {
489
+ let c = str.charCodeAt(ci);
490
+ if (c < 128) {
491
+ bytes[i++] = c;
492
+ continue;
493
+ }
494
+ if (c < 2048) bytes[i++] = c >> 6 | 192;
495
+ else {
496
+ const c2 = c > 55295 && c < 56320 && ci + 1 < length ? str.charCodeAt(ci + 1) : 0;
497
+ if (c2 >= 56320 && c2 <= 57343) {
498
+ ci++;
499
+ c = 65536 + ((c & 1023) << 10) + (c2 & 1023);
500
+ bytes[i++] = c >> 18 | 240;
501
+ bytes[i++] = c >> 12 & 63 | 128;
502
+ } else {
503
+ if (c > 55295 && c < 57344) c = 65533;
504
+ bytes[i++] = c >> 12 | 224;
505
+ }
506
+ bytes[i++] = c >> 6 & 63 | 128;
507
+ }
508
+ bytes[i++] = c & 63 | 128;
509
+ }
510
+ return ascii ? bytes : bytes.subarray(0, i);
511
+ }
512
+ const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
513
+ /** Decodes a string from UTF-8 bytes. */
514
+ function decodeUtf8(bytes, start, length) {
515
+ const end = start + length;
516
+ if (length <= 12) {
517
+ let str = "";
518
+ for (let i = start; i < end; i++) {
519
+ const c = bytes[i];
520
+ if (c > 127) return utf8Decoder.decode(bytes.subarray(start, end));
521
+ str += String.fromCharCode(c);
522
+ }
523
+ return str;
524
+ }
525
+ return utf8Decoder.decode(bytes.subarray(start, end));
526
+ }
527
+ //#endregion
2
528
  //#region src/msgpack.ts
3
- const { serialize: serialize$1, deserialize: deserialize$1 } = msgpack;
4
529
  /**
5
530
  * Encode each value as a length-prefixed msgpack frame.
6
531
  * Frame format: [4-byte big-endian length][msgpack bytes]
532
+ *
533
+ * The input may be synchronous (an array, a generator) or asynchronous.
7
534
  */
8
535
  async function* encodeMsgpack(input) {
9
536
  for await (const item of input) {
10
- const payload = serialize$1(item);
537
+ const payload = serialize(item);
11
538
  const frame = new Uint8Array(4 + payload.length);
12
539
  new DataView(frame.buffer).setUint32(0, payload.length, false);
13
540
  frame.set(payload, 4);
@@ -16,6 +543,8 @@ async function* encodeMsgpack(input) {
16
543
  }
17
544
  /**
18
545
  * Decode length-prefixed msgpack frames, reassembling across chunk boundaries.
546
+ *
547
+ * The input may be synchronous (an array of chunks) or asynchronous.
19
548
  */
20
549
  async function* decodeMsgpack(input) {
21
550
  let buffer = /* @__PURE__ */ new Uint8Array(0);
@@ -26,18 +555,19 @@ async function* decodeMsgpack(input) {
26
555
  if (buffer.length < 4 + frameLen) break;
27
556
  const payload = buffer.subarray(4, 4 + frameLen);
28
557
  buffer = buffer.subarray(4 + frameLen);
29
- yield deserialize$1(Uint8Array.from(payload));
558
+ yield deserialize(Uint8Array.from(payload));
30
559
  }
31
560
  }
32
561
  }
33
562
  /**
34
563
  * Encode each Float32Array as a msgpack frame.
35
564
  * The Float32Array is converted to a Uint8Array view (zero-copy) before encoding as msgpack bin.
565
+ *
566
+ * The input may be synchronous (an array, a generator) or asynchronous.
36
567
  */
37
568
  async function* encodeFloat32Arrays(input) {
38
569
  for await (const arr of input) {
39
- const bytes = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
40
- const payload = serialize$1(bytes);
570
+ const payload = serialize(new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength));
41
571
  const frame = new Uint8Array(4 + payload.length);
42
572
  new DataView(frame.buffer).setUint32(0, payload.length, false);
43
573
  frame.set(payload, 4);
@@ -47,9 +577,11 @@ async function* encodeFloat32Arrays(input) {
47
577
  /**
48
578
  * Decode msgpack frames back to Float32Array.
49
579
  * Each frame contains a msgpack bin value (Uint8Array), reinterpreted as Float32Array.
580
+ *
581
+ * The input may be synchronous (an array of chunks) or asynchronous.
50
582
  */
51
583
  async function* decodeFloat32Arrays(input) {
52
- for await (const item of decodeMsgpack(wrapIterable(input))) {
584
+ for await (const item of decodeMsgpack(input)) {
53
585
  const aligned = alignBuffer(item);
54
586
  yield new Float32Array(aligned.buffer, aligned.byteOffset, aligned.byteLength / 4);
55
587
  }
@@ -67,12 +599,8 @@ function concat(a, b) {
67
599
  result.set(b, a.length);
68
600
  return result;
69
601
  }
70
- async function* wrapIterable(input) {
71
- yield* input;
72
- }
73
602
  //#endregion
74
603
  //#region src/port-codec.ts
75
- const { serialize, deserialize } = msgpack;
76
604
  /**
77
605
  * Same shape check as `structuredCodec`'s, applied after decoding. A shared
78
606
  * transport carries traffic that is not ours, and layer 1 must not mistake it
@@ -130,4 +658,4 @@ const msgpackCodec = {
130
658
  }
131
659
  };
132
660
  //#endregion
133
- export { decodeFloat32Arrays, decodeMsgpack, encodeFloat32Arrays, encodeMsgpack, msgpackCodec };
661
+ export { decodeFloat32Arrays, decodeMsgpack, deserialize, encodeFloat32Arrays, encodeMsgpack, msgpackCodec, serialize };
@@ -0,0 +1,70 @@
1
+ /*!
2
+ * MessagePack serializer and deserializer.
3
+ *
4
+ * A TypeScript port of msgpack.js by Yves Goergen:
5
+ * https://github.com/ygoe/msgpack.js
6
+ * ported from commit 05733cfb43a2974cf669f0eb8693f43b548bdcd4 (2024-04-16) on `master`,
7
+ * which carries three fixes that the last npm release, `@ygoe/msgpack@1.0.3`, does not.
8
+ * Every change made to the upstream logic is listed in this package's README, section
9
+ * "Provenance and credits".
10
+ *
11
+ * Original work: Copyright © 2019, Yves Goergen, https://unclassified.software/source/msgpack-js
12
+ * TypeScript port and modifications: Copyright (c) 2026 statewalker
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
15
+ * associated documentation files (the “Software”), to deal in the Software without restriction,
16
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
17
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
18
+ * furnished to do so, subject to the following conditions:
19
+ *
20
+ * The above copyright notice and this permission notice shall be included in all copies or
21
+ * substantial portions of the Software.
22
+ *
23
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
24
+ * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ */
29
+ /** Options for {@link serialize}. */
30
+ export interface SerializeOptions {
31
+ /**
32
+ * Treat `data` as an array of values and concatenate their encodings, one MessagePack document
33
+ * after another, instead of encoding the array itself. Default: `false`.
34
+ */
35
+ multiple?: boolean;
36
+ /**
37
+ * What to write in place of a value whose type cannot be serialized (a function, a symbol, a
38
+ * bigint): either the replacement value itself, or a function that returns one given the
39
+ * original. When absent, such a value throws.
40
+ */
41
+ invalidTypeReplacement?: unknown;
42
+ }
43
+ /** Options for {@link deserialize}. */
44
+ export interface DeserializeOptions {
45
+ /**
46
+ * Read every concatenated MessagePack document in the input and return them as an array.
47
+ * Default: `false` — read one document and ignore any bytes after it.
48
+ */
49
+ multiple?: boolean;
50
+ }
51
+ /** The byte shapes {@link deserialize} accepts. */
52
+ export type MsgpackInput = Uint8Array | ArrayBuffer | ArrayLike<number>;
53
+ /** An extension value whose type this implementation does not interpret (anything but a timestamp). */
54
+ export interface MsgpackExtension {
55
+ type: number;
56
+ data: Uint8Array;
57
+ }
58
+ /**
59
+ * Serializes a value to a MessagePack byte array.
60
+ *
61
+ * The result is a view over a larger internal buffer; send the view, not its `.buffer`.
62
+ */
63
+ export declare function serialize(data: unknown, options?: SerializeOptions): Uint8Array<ArrayBuffer>;
64
+ /**
65
+ * Deserializes a MessagePack byte array to a value.
66
+ *
67
+ * `bin` values are returned as `Uint8Array` views into the input, not copies.
68
+ */
69
+ export declare function deserialize(input: MsgpackInput, options?: DeserializeOptions): unknown;
70
+ //# sourceMappingURL=msgpack-core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"msgpack-core.d.ts","sourceRoot":"","sources":["../src/msgpack-core.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAED,uCAAuC;AACvC,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,mDAAmD;AACnD,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,WAAW,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAExE,uGAAuG;AACvG,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;CAClB;AAID;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,UAAU,CAAC,WAAW,CAAC,CAmQ5F;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAoNtF"}