@isoloop/codec 0.0.0-stage → 0.1.0-next.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sabin Pandelovitch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Decodes bytes written by `encode`. Throws a `CodecError` with code
3
+ * `malformed` if they're truncated, carry an unknown tag or have bytes left
4
+ * over.
5
+ */
6
+ export declare function decode(bytes: Uint8Array): unknown;
7
+ /**
8
+ * Encodes `value` to bytes (`isoloop-codec@1`, see format.ts). Throws a
9
+ * `CodecError` with code `not_encodable` for a function, a symbol, a class
10
+ * instance or a cycle.
11
+ *
12
+ * The result is a view into a chunk shared with other results: copy it, or
13
+ * read it through its `byteOffset`, before transferring its `buffer`.
14
+ */
15
+ export declare function encode(value: unknown): Uint8Array;
16
+ /** Why the codec refused a value or a byte sequence. */
17
+ type CodecErrorCode = "not_encodable" | "malformed";
18
+ /** Thrown by `encode` and `decode`. */
19
+ export declare class CodecError extends Error {
20
+ readonly code: CodecErrorCode;
21
+ constructor(code: CodecErrorCode, message: string);
22
+ }
23
+ export type { CodecErrorCode };
package/dist/index.js ADDED
@@ -0,0 +1,553 @@
1
+ // \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, flags) => {
4
+ let target = Object.setPrototypeOf({}, null);
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return flags & 2 ? target : Object.preventExtensions(target);
11
+ };
12
+ // src/errors.ts
13
+ /** Thrown by `encode` and `decode`. */
14
+ var CodecError = class extends Error {
15
+ code;
16
+ constructor(code, message) {
17
+ super(`${code}: ${message}`);
18
+ this.name = "CodecError";
19
+ this.code = code;
20
+ }
21
+ };
22
+ // src/format.ts
23
+ var format_exports = /* @__PURE__ */ __exportAll({
24
+ ARRAY: () => 200,
25
+ ARRAYBUFFER: () => 207,
26
+ BIGINT: () => 202,
27
+ DATE: () => 204,
28
+ FALSE: () => 194,
29
+ FIXARRAY: () => 160,
30
+ FIXARRAY_MAX: () => 15,
31
+ FIXOBJECT: () => 176,
32
+ FIXOBJECT_MAX: () => 15,
33
+ FIXSTR: () => 128,
34
+ FIXSTR_MAX: () => 31,
35
+ FLOAT64: () => 198,
36
+ MAP: () => 205,
37
+ MAX_DEPTH: () => 512,
38
+ NBIGINT: () => 203,
39
+ NEG_FIXINT: () => 224,
40
+ NINT: () => 197,
41
+ NULL: () => 193,
42
+ OBJECT: () => 201,
43
+ SET: () => 206,
44
+ STR: () => 199,
45
+ TRUE: () => 195,
46
+ UINT: () => 196,
47
+ UNDEFINED: () => 192,
48
+ VIEW: () => 208,
49
+ VIEW_KINDS: () => VIEW_KINDS$2
50
+ });
51
+ /** The kind byte after VIEW is the index in this list. Append only. */
52
+ const VIEW_KINDS$2 = [
53
+ "Int8Array",
54
+ "Uint8Array",
55
+ "Uint8ClampedArray",
56
+ "Int16Array",
57
+ "Uint16Array",
58
+ "Int32Array",
59
+ "Uint32Array",
60
+ "Float32Array",
61
+ "Float64Array",
62
+ "BigInt64Array",
63
+ "BigUint64Array",
64
+ "DataView"
65
+ ];
66
+ // src/decode.ts
67
+ const { FIXSTR: FIXSTR$1, FIXARRAY: FIXARRAY$1, FIXOBJECT: FIXOBJECT$1, UNDEFINED: UNDEFINED$1, NULL: NULL$1, FALSE: FALSE$1, TRUE: TRUE$1, UINT: UINT$1, NINT: NINT$1, FLOAT64: FLOAT64$1, STR: STR$1, ARRAY: ARRAY$1, OBJECT: OBJECT$1, NEG_FIXINT, FIXSTR_MAX: FIXSTR_MAX$1, MAX_DEPTH: MAX_DEPTH$1, BIGINT: BIGINT$1, NBIGINT: NBIGINT$1, DATE: DATE$1, MAP: MAP$1, SET: SET$1, ARRAYBUFFER: ARRAYBUFFER$1, VIEW: VIEW$1, VIEW_KINDS: VIEW_KINDS$1 } = format_exports;
68
+ const utf8$1 = new TextDecoder("utf-8", { fatal: true });
69
+ let src = /* @__PURE__ */ new Uint8Array(0);
70
+ let pos$1 = 0;
71
+ let end = 0;
72
+ /**
73
+ * Decodes bytes written by `encode`. Throws a `CodecError` with code
74
+ * `malformed` if they're truncated, carry an unknown tag or have bytes left
75
+ * over.
76
+ */
77
+ function decode(bytes) {
78
+ src = bytes;
79
+ pos$1 = 0;
80
+ end = bytes.length;
81
+ const v = read(0);
82
+ if (pos$1 !== end) throw new CodecError("malformed", "trailing bytes");
83
+ return v;
84
+ }
85
+ const f64 = /* @__PURE__ */ new Float64Array(1);
86
+ const f64b = new Uint8Array(f64.buffer);
87
+ if (!(new Uint8Array(new Uint16Array([1]).buffer)[0] === 1)) throw new Error("@isoloop/codec needs a little-endian platform");
88
+ const truncated = () => new CodecError("malformed", "truncated");
89
+ function byte() {
90
+ if (pos$1 >= end) throw truncated();
91
+ return src[pos$1++] ?? 0;
92
+ }
93
+ function varint$1() {
94
+ let b = byte();
95
+ if (b < 128) return b;
96
+ let n = b & 127;
97
+ for (let shift = 7; shift < 28; shift += 7) {
98
+ b = byte();
99
+ n |= (b & 127) << shift;
100
+ if (b < 128) return n;
101
+ }
102
+ let scale = 268435456;
103
+ for (let i = 4; i < 8; i++) {
104
+ b = byte();
105
+ n += (b & 127) * scale;
106
+ if (b < 128) {
107
+ if (n > Number.MAX_SAFE_INTEGER) break;
108
+ return n;
109
+ }
110
+ scale *= 128;
111
+ }
112
+ throw new CodecError("malformed", "varint out of range");
113
+ }
114
+ function float64() {
115
+ if (pos$1 + 8 > end) throw truncated();
116
+ for (let i = 0; i < 8; i++) f64b[i] = src[pos$1 + i] ?? 0;
117
+ pos$1 += 8;
118
+ return f64[0] ?? 0;
119
+ }
120
+ function read(depth) {
121
+ const tag = byte();
122
+ if (tag < FIXSTR$1) return tag;
123
+ if (tag < FIXARRAY$1) return string$1(tag - FIXSTR$1);
124
+ if (tag < FIXOBJECT$1) return array$1(tag - FIXARRAY$1, depth);
125
+ if (tag < UNDEFINED$1) return object$1(tag - FIXOBJECT$1, depth);
126
+ if (tag >= NEG_FIXINT) return tag - 256;
127
+ switch (tag) {
128
+ case FLOAT64$1: return float64();
129
+ case UNDEFINED$1: return;
130
+ case NULL$1: return null;
131
+ case FALSE$1: return false;
132
+ case TRUE$1: return true;
133
+ case UINT$1: return varint$1();
134
+ case NINT$1: return -varint$1() - 1;
135
+ case STR$1: return string$1(varint$1());
136
+ case ARRAY$1: return array$1(varint$1(), depth);
137
+ case OBJECT$1: return object$1(varint$1(), depth);
138
+ case BIGINT$1: return bigint$1();
139
+ case NBIGINT$1: return -bigint$1();
140
+ case DATE$1: return new Date(float64());
141
+ case MAP$1: return map(varint$1(), depth);
142
+ case SET$1: return set(varint$1(), depth);
143
+ case ARRAYBUFFER$1: return copy(varint$1());
144
+ case VIEW$1: return typed();
145
+ default: throw new CodecError("malformed", `unknown tag 0x${tag.toString(16)}`);
146
+ }
147
+ }
148
+ function utf8Decode(start, stop) {
149
+ try {
150
+ return utf8$1.decode(src.subarray(start, stop));
151
+ } catch {
152
+ throw new CodecError("malformed", "invalid UTF-8");
153
+ }
154
+ }
155
+ const SLOTS = 4096;
156
+ const CACHED = 16;
157
+ const slotBytes = new Uint8Array(SLOTS * CACHED);
158
+ const slotLength = new Uint8Array(SLOTS);
159
+ const slotString = Array.from({ length: SLOTS }, () => "");
160
+ function string$1(n) {
161
+ const start = pos$1;
162
+ const stop = start + n;
163
+ if (stop > end) throw truncated();
164
+ pos$1 = stop;
165
+ if (n === 0) return "";
166
+ if (n > CACHED) return n > 32 ? utf8Decode(start, stop) : ascii(start, stop);
167
+ let h = n;
168
+ for (let i = start; i < stop; i++) h = Math.imul(h, 31) + (src[i] ?? 0) | 0;
169
+ const slot = (h ^ h >>> 12) & 4095;
170
+ const base = slot * CACHED;
171
+ if (slotLength[slot] === n) {
172
+ let i = 0;
173
+ while (i < n && slotBytes[base + i] === src[start + i]) i++;
174
+ if (i === n) return slotString[slot] ?? "";
175
+ }
176
+ const s = ascii(start, stop);
177
+ slotBytes.set(src.subarray(start, stop), base);
178
+ slotLength[slot] = n;
179
+ slotString[slot] = s;
180
+ return s;
181
+ }
182
+ const fcc = String.fromCharCode;
183
+ /** Up to 32 bytes: by hand when ASCII, which beats TextDecoder's call overhead. */
184
+ function ascii(start, stop) {
185
+ let s = "";
186
+ let i = start;
187
+ for (; i + 4 <= stop; i += 4) {
188
+ const a = src[i] ?? 0;
189
+ const b = src[i + 1] ?? 0;
190
+ const c = src[i + 2] ?? 0;
191
+ const d = src[i + 3] ?? 0;
192
+ if ((a | b | c | d) >= 128) return utf8Decode(start, stop);
193
+ s += fcc(a, b, c, d);
194
+ }
195
+ for (; i < stop; i++) {
196
+ const a = src[i] ?? 0;
197
+ if (a >= 128) return utf8Decode(start, stop);
198
+ s += fcc(a);
199
+ }
200
+ return s;
201
+ }
202
+ function key() {
203
+ const tag = byte();
204
+ if (tag >= FIXSTR$1 && tag <= FIXSTR$1 + FIXSTR_MAX$1) return string$1(tag - FIXSTR$1);
205
+ if (tag === STR$1) return string$1(varint$1());
206
+ throw new CodecError("malformed", "object key is not a string");
207
+ }
208
+ function nest(depth) {
209
+ if (depth >= MAX_DEPTH$1) throw new CodecError("malformed", `nested deeper than ${MAX_DEPTH$1}`);
210
+ return depth + 1;
211
+ }
212
+ function array$1(n, depth) {
213
+ const d = nest(depth);
214
+ if (pos$1 + n > end) throw truncated();
215
+ const a = [];
216
+ for (let i = 0; i < n; i++) a.push(read(d));
217
+ return a;
218
+ }
219
+ function object$1(n, depth) {
220
+ const d = nest(depth);
221
+ if (pos$1 + n * 2 > end) throw truncated();
222
+ const o = {};
223
+ for (let i = 0; i < n; i++) {
224
+ const k = key();
225
+ const v = read(d);
226
+ if (k === "__proto__") Object.defineProperty(o, k, {
227
+ value: v,
228
+ writable: true,
229
+ enumerable: true,
230
+ configurable: true
231
+ });
232
+ else o[k] = v;
233
+ }
234
+ return o;
235
+ }
236
+ /** n bytes, copied into their own ArrayBuffer. */
237
+ function copy(n) {
238
+ if (pos$1 + n > end) throw truncated();
239
+ const out = new ArrayBuffer(n);
240
+ new Uint8Array(out).set(src.subarray(pos$1, pos$1 + n));
241
+ pos$1 += n;
242
+ return out;
243
+ }
244
+ function bigint$1() {
245
+ const n = varint$1();
246
+ if (pos$1 + n > end) throw truncated();
247
+ let v = 0n;
248
+ for (let i = pos$1 + n - 1; i >= pos$1; i--) v = v << 8n | BigInt(src[i] ?? 0);
249
+ pos$1 += n;
250
+ return v;
251
+ }
252
+ function map(n, depth) {
253
+ const d = nest(depth);
254
+ if (pos$1 + n * 2 > end) throw truncated();
255
+ const m = /* @__PURE__ */ new Map();
256
+ for (let i = 0; i < n; i++) {
257
+ const k = read(d);
258
+ m.set(k, read(d));
259
+ }
260
+ return m;
261
+ }
262
+ function set(n, depth) {
263
+ const d = nest(depth);
264
+ if (pos$1 + n > end) throw truncated();
265
+ const s = /* @__PURE__ */ new Set();
266
+ for (let i = 0; i < n; i++) s.add(read(d));
267
+ return s;
268
+ }
269
+ const VIEWS = {
270
+ Int8Array: (b) => new Int8Array(b),
271
+ Uint8Array: (b) => new Uint8Array(b),
272
+ Uint8ClampedArray: (b) => new Uint8ClampedArray(b),
273
+ Int16Array: (b) => new Int16Array(b),
274
+ Uint16Array: (b) => new Uint16Array(b),
275
+ Int32Array: (b) => new Int32Array(b),
276
+ Uint32Array: (b) => new Uint32Array(b),
277
+ Float32Array: (b) => new Float32Array(b),
278
+ Float64Array: (b) => new Float64Array(b),
279
+ BigInt64Array: (b) => new BigInt64Array(b),
280
+ BigUint64Array: (b) => new BigUint64Array(b),
281
+ DataView: (b) => new DataView(b)
282
+ };
283
+ function typed() {
284
+ const kind = VIEW_KINDS$1[byte()];
285
+ if (kind === void 0) throw new CodecError("malformed", "unknown typed array kind");
286
+ const b = copy(varint$1());
287
+ try {
288
+ return VIEWS[kind](b);
289
+ } catch {
290
+ throw new CodecError("malformed", `${kind} byte length ${b.byteLength}`);
291
+ }
292
+ }
293
+ // src/encode.ts
294
+ const { FIXSTR, FIXARRAY, FIXOBJECT, UNDEFINED, NULL, FALSE, TRUE, UINT, NINT, FLOAT64, STR, ARRAY, OBJECT, FIXSTR_MAX, FIXARRAY_MAX, FIXOBJECT_MAX, MAX_DEPTH, BIGINT, NBIGINT, DATE, MAP, SET, ARRAYBUFFER, VIEW, VIEW_KINDS } = format_exports;
295
+ const utf8 = new TextEncoder();
296
+ const SLACK = 16;
297
+ const CHUNK = 16384;
298
+ let buf = new Uint8Array(CHUNK);
299
+ let view = new DataView(buf.buffer);
300
+ let start = 0;
301
+ let pos = 0;
302
+ /** Makes room for n more bytes (plus SLACK), moving to a new chunk if needed. */
303
+ function room(n) {
304
+ if (pos + n + SLACK <= buf.length) return;
305
+ const used = pos - start;
306
+ let size = CHUNK;
307
+ while (size < used + n + SLACK) size *= 2;
308
+ const next = new Uint8Array(size);
309
+ next.set(buf.subarray(start, pos));
310
+ buf = next;
311
+ view = new DataView(next.buffer);
312
+ start = 0;
313
+ pos = used;
314
+ }
315
+ /** Unsigned LEB128, for 0 ≤ n ≤ 2^53 - 1; the caller made room. */
316
+ function varint(n) {
317
+ if (n > 4294967295) {
318
+ const lo = n >>> 0;
319
+ const hi = (n - lo) / 4294967296;
320
+ buf[pos++] = lo & 127 | 128;
321
+ buf[pos++] = lo >>> 7 & 127 | 128;
322
+ buf[pos++] = lo >>> 14 & 127 | 128;
323
+ buf[pos++] = lo >>> 21 & 127 | 128;
324
+ n = lo >>> 28 | hi << 4;
325
+ }
326
+ while (n > 127) {
327
+ buf[pos++] = n & 127 | 128;
328
+ n >>>= 7;
329
+ }
330
+ buf[pos++] = n;
331
+ }
332
+ /** A tag that holds counts up to `max` itself, else the long tag and a varint. */
333
+ function head(fix, max, long, n) {
334
+ if (n <= max) buf[pos++] = fix + n;
335
+ else {
336
+ buf[pos++] = long;
337
+ varint(n);
338
+ }
339
+ }
340
+ const varintSize = (n) => {
341
+ let size = 1;
342
+ while (n >= 128) {
343
+ n = Math.floor(n / 128);
344
+ size++;
345
+ }
346
+ return size;
347
+ };
348
+ const isPlainObject = (v) => {
349
+ const proto = Object.getPrototypeOf(v);
350
+ return proto === Object.prototype || proto === null;
351
+ };
352
+ const describe = (v) => {
353
+ if (typeof v === "function") return "a function";
354
+ if (typeof v === "symbol") return "a symbol";
355
+ const ctor = typeof v === "object" && v !== null ? Reflect.get(v, "constructor") : void 0;
356
+ const name = typeof ctor === "function" ? ctor.name : void 0;
357
+ return typeof name === "string" && name !== "" ? `an instance of ${name}` : "an object";
358
+ };
359
+ let busy = false;
360
+ /**
361
+ * Encodes `value` to bytes (`isoloop-codec@1`, see format.ts). Throws a
362
+ * `CodecError` with code `not_encodable` for a function, a symbol, a class
363
+ * instance or a cycle.
364
+ *
365
+ * The result is a view into a chunk shared with other results: copy it, or
366
+ * read it through its `byteOffset`, before transferring its `buffer`.
367
+ */
368
+ function encode(value) {
369
+ if (busy) {
370
+ const outer = {
371
+ buf,
372
+ view,
373
+ start,
374
+ pos
375
+ };
376
+ buf = /* @__PURE__ */ new Uint8Array(256);
377
+ view = new DataView(buf.buffer);
378
+ start = 0;
379
+ pos = 0;
380
+ try {
381
+ write(value, 0);
382
+ return buf.subarray(0, pos);
383
+ } finally {
384
+ ({buf, view, start, pos} = outer);
385
+ }
386
+ }
387
+ busy = true;
388
+ start = pos;
389
+ try {
390
+ write(value, 0);
391
+ const out = buf.subarray(start, pos);
392
+ start = pos;
393
+ return out;
394
+ } catch (e) {
395
+ pos = start;
396
+ throw e;
397
+ } finally {
398
+ busy = false;
399
+ }
400
+ }
401
+ function write(v, depth) {
402
+ if (pos + SLACK > buf.length) room(0);
403
+ switch (typeof v) {
404
+ case "number": return number(v);
405
+ case "string": return string(v);
406
+ case "boolean":
407
+ buf[pos++] = v ? TRUE : FALSE;
408
+ return;
409
+ case "undefined":
410
+ buf[pos++] = UNDEFINED;
411
+ return;
412
+ case "object":
413
+ if (v === null) {
414
+ buf[pos++] = NULL;
415
+ return;
416
+ }
417
+ if (depth >= MAX_DEPTH) throw new CodecError("not_encodable", `nested deeper than ${MAX_DEPTH}, or a cycle`);
418
+ if (Array.isArray(v)) return array(v, depth + 1);
419
+ if (isPlainObject(v)) return object(v, depth + 1);
420
+ return special(v, depth + 1);
421
+ case "bigint": return bigint(v);
422
+ case "function":
423
+ case "symbol": throw new CodecError("not_encodable", `${describe(v)} can't be encoded`);
424
+ }
425
+ }
426
+ function number(v) {
427
+ if (v >>> 0 === v && (v !== 0 || 1 / v > 0)) {
428
+ if (v < 128) buf[pos++] = v;
429
+ else {
430
+ buf[pos++] = UINT;
431
+ varint(v);
432
+ }
433
+ return;
434
+ }
435
+ if (v !== 0 && ((v | 0) === v || Math.trunc(v) === v && v <= Number.MAX_SAFE_INTEGER && v >= -Number.MAX_SAFE_INTEGER)) {
436
+ if (v >= 0) {
437
+ buf[pos++] = UINT;
438
+ varint(v);
439
+ } else if (v >= -32) buf[pos++] = 256 + v;
440
+ else {
441
+ buf[pos++] = NINT;
442
+ varint(-v - 1);
443
+ }
444
+ return;
445
+ }
446
+ buf[pos] = FLOAT64;
447
+ view.setFloat64(pos + 1, v, true);
448
+ pos += 9;
449
+ }
450
+ function string(s) {
451
+ const n = s.length;
452
+ if (n <= 64) {
453
+ room(n);
454
+ const at = pos;
455
+ if (n <= FIXSTR_MAX) pos += 1;
456
+ else pos += 2;
457
+ let i = 0;
458
+ for (; i < n; i++) {
459
+ const c = s.charCodeAt(i);
460
+ if (c >= 128) break;
461
+ buf[pos++] = c;
462
+ }
463
+ if (i === n) {
464
+ if (n <= FIXSTR_MAX) buf[at] = FIXSTR + n;
465
+ else {
466
+ buf[at] = STR;
467
+ buf[at + 1] = n;
468
+ }
469
+ return;
470
+ }
471
+ pos = at;
472
+ }
473
+ const max = n * 3;
474
+ const gap = 1 + varintSize(max);
475
+ room(gap + max);
476
+ const at = pos;
477
+ const { written } = utf8.encodeInto(s, buf.subarray(at + gap));
478
+ const size = written <= FIXSTR_MAX ? 1 : 1 + varintSize(written);
479
+ if (size !== gap) buf.copyWithin(at + size, at + gap, at + gap + written);
480
+ head(FIXSTR, FIXSTR_MAX, STR, written);
481
+ pos = at + size + written;
482
+ }
483
+ function item(v, depth) {
484
+ if (typeof v === "number") {
485
+ if (pos + SLACK > buf.length) room(0);
486
+ number(v);
487
+ } else if (typeof v === "string") string(v);
488
+ else write(v, depth);
489
+ }
490
+ function array(a, depth) {
491
+ head(FIXARRAY, FIXARRAY_MAX, ARRAY, a.length);
492
+ for (let i = 0; i < a.length; i++) item(a[i], depth);
493
+ }
494
+ function object(o, depth) {
495
+ const keys = Object.keys(o);
496
+ head(FIXOBJECT, FIXOBJECT_MAX, OBJECT, keys.length);
497
+ for (const k of keys) {
498
+ string(k);
499
+ item(o[k], depth);
500
+ }
501
+ }
502
+ const viewKind = (v) => {
503
+ const name = Object.prototype.toString.call(v).slice(8, -1);
504
+ return VIEW_KINDS.indexOf(name);
505
+ };
506
+ /** The structured-clone types beyond plain data. */
507
+ function special(v, depth) {
508
+ if (v instanceof Date) {
509
+ buf[pos] = DATE;
510
+ view.setFloat64(pos + 1, v.getTime(), true);
511
+ pos += 9;
512
+ return;
513
+ }
514
+ if (v instanceof Map) {
515
+ head(MAP, -1, MAP, v.size);
516
+ for (const [k, x] of v) {
517
+ item(k, depth);
518
+ item(x, depth);
519
+ }
520
+ return;
521
+ }
522
+ if (v instanceof Set) {
523
+ head(SET, -1, SET, v.size);
524
+ for (const x of v) item(x, depth);
525
+ return;
526
+ }
527
+ if (v instanceof ArrayBuffer) return bytes(ARRAYBUFFER, -1, new Uint8Array(v));
528
+ if (ArrayBuffer.isView(v)) {
529
+ const kind = viewKind(v);
530
+ if (kind < 0 || !(v.buffer instanceof ArrayBuffer)) throw new CodecError("not_encodable", `${describe(v)} can't be encoded`);
531
+ return bytes(VIEW, kind, new Uint8Array(v.buffer, v.byteOffset, v.byteLength));
532
+ }
533
+ throw new CodecError("not_encodable", `${describe(v)} can't be encoded`);
534
+ }
535
+ /** A tag, an optional kind byte, a varint length and the bytes. */
536
+ function bytes(tag, kind, b) {
537
+ room(b.length);
538
+ buf[pos++] = tag;
539
+ if (kind >= 0) buf[pos++] = kind;
540
+ varint(b.length);
541
+ buf.set(b, pos);
542
+ pos += b.length;
543
+ }
544
+ function bigint(v) {
545
+ let m = v < 0n ? -v : v;
546
+ const out = [];
547
+ while (m > 0n) {
548
+ out.push(Number(m & 255n));
549
+ m >>= 8n;
550
+ }
551
+ bytes(v < 0n ? NBIGINT : BIGINT, -1, Uint8Array.from(out));
552
+ }
553
+ export { CodecError, decode, encode };
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@isoloop/codec",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-next.0",
4
+ "description": "Binary codec for Isoloop topic records: the structured-clone types topics allow, no dependencies.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/isoloop/isoloop.git",
11
+ "directory": "packages/codec"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "devDependencies": {
23
+ "fast-check": "4.10.2",
24
+ "typescript": "7.0.2"
25
+ },
26
+ "scripts": {
27
+ "build": "tsdown --platform neutral --dts",
28
+ "typecheck": "tsc --noEmit"
29
+ }
6
30
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.