microcbor 0.3.0 → 1.0.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/lib/Encoder.js ADDED
@@ -0,0 +1,291 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Encoder_instances, _Encoder_closed, _Encoder_flush;
13
+ import { Precision, getFloat16Precision, getFloat32Precision, setFloat16 } from "fp16";
14
+ import { assert } from "./utils.js";
15
+ export const FloatSize = {
16
+ f16: 16,
17
+ f32: 32,
18
+ f64: 64,
19
+ };
20
+ export class Encoder {
21
+ constructor(options = {}) {
22
+ _Encoder_instances.add(this);
23
+ _Encoder_closed.set(this, void 0);
24
+ this.encoder = new TextEncoder();
25
+ this.minFloatSize = options.minFloatSize ?? 16;
26
+ this.chunkRecycling = options.chunkRecycling ?? false;
27
+ this.chunkSize = options.chunkSize ?? Encoder.defaultChunkSize;
28
+ assert(this.chunkSize >= 8, "expected chunkSize >= 8");
29
+ this.buffer = new ArrayBuffer(this.chunkSize);
30
+ this.view = new DataView(this.buffer);
31
+ this.array = new Uint8Array(this.buffer, 0, this.chunkSize);
32
+ this.offset = 0;
33
+ __classPrivateFieldSet(this, _Encoder_closed, false, "f");
34
+ }
35
+ get closed() {
36
+ return __classPrivateFieldGet(this, _Encoder_closed, "f");
37
+ }
38
+ *allocate(size) {
39
+ assert(size <= 8, "expected size <= 8");
40
+ if (this.buffer.byteLength < this.offset + size) {
41
+ yield __classPrivateFieldGet(this, _Encoder_instances, "m", _Encoder_flush).call(this);
42
+ }
43
+ }
44
+ *float16(value) {
45
+ yield* this.allocate(2);
46
+ setFloat16(this.view, this.offset, value);
47
+ this.offset += 2;
48
+ }
49
+ *float32(value) {
50
+ yield* this.allocate(4);
51
+ this.view.setFloat32(this.offset, value);
52
+ this.offset += 4;
53
+ }
54
+ *float64(value) {
55
+ yield* this.allocate(8);
56
+ this.view.setFloat64(this.offset, value);
57
+ this.offset += 8;
58
+ }
59
+ *uint8(value) {
60
+ yield* this.allocate(1);
61
+ this.view.setUint8(this.offset, value);
62
+ this.offset += 1;
63
+ }
64
+ *uint16(value) {
65
+ yield* this.allocate(2);
66
+ this.view.setUint16(this.offset, value);
67
+ this.offset += 2;
68
+ }
69
+ *uint32(value) {
70
+ yield* this.allocate(4);
71
+ this.view.setUint32(this.offset, value);
72
+ this.offset += 4;
73
+ }
74
+ *uint64(value) {
75
+ yield* this.allocate(8);
76
+ this.view.setBigUint64(this.offset, BigInt(value));
77
+ this.offset += 8;
78
+ }
79
+ *encodeTypeAndArgument(type, argument) {
80
+ const additionalInformation = Encoder.getAdditionalInformation(argument);
81
+ yield* this.uint8((type << 5) | additionalInformation);
82
+ switch (additionalInformation) {
83
+ case 24:
84
+ return yield* this.uint8(argument);
85
+ case 25:
86
+ return yield* this.uint16(argument);
87
+ case 26:
88
+ return yield* this.uint32(argument);
89
+ case 27:
90
+ return yield* this.uint64(argument);
91
+ }
92
+ }
93
+ *encodeNumber(value) {
94
+ if (Object.is(value, 0)) {
95
+ yield* this.encodeInteger(value);
96
+ }
97
+ else if (Object.is(value, -0)) {
98
+ yield* this.encodeFloat(value);
99
+ }
100
+ else if (Math.floor(value) === value && Number.MIN_SAFE_INTEGER <= value && value <= Number.MAX_SAFE_INTEGER) {
101
+ yield* this.encodeInteger(value);
102
+ }
103
+ else {
104
+ yield* this.encodeFloat(value);
105
+ }
106
+ }
107
+ *encodeInteger(value) {
108
+ if (value < 0) {
109
+ yield* this.encodeTypeAndArgument(1, -value - 1);
110
+ }
111
+ else {
112
+ yield* this.encodeTypeAndArgument(0, value);
113
+ }
114
+ }
115
+ *encodeFloat(value) {
116
+ if (this.minFloatSize === FloatSize.f16 && getFloat16Precision(value) === Precision.Exact) {
117
+ yield* this.uint8(0xe0 | 25);
118
+ yield* this.float16(value);
119
+ }
120
+ else if (this.minFloatSize <= FloatSize.f32 && getFloat32Precision(value) === Precision.Exact) {
121
+ yield* this.uint8(0xe0 | 26);
122
+ yield* this.float32(value);
123
+ }
124
+ else {
125
+ yield* this.uint8(0xe0 | 27);
126
+ yield* this.float64(value);
127
+ }
128
+ }
129
+ *encodeString(value) {
130
+ const bytes = this.encoder.encode(value);
131
+ yield* this.encodeTypeAndArgument(3, bytes.byteLength);
132
+ yield* this.writeBytes(bytes);
133
+ // const byteLength = getByteLength(value)
134
+ // let start = 0
135
+ // while (start < value.length) {
136
+ // if (this.offset + 4 > this.buffer.byteLength) {
137
+ // yield this.#flush()
138
+ // }
139
+ // const target = new Uint8Array(this.buffer, this.offset)
140
+ // const result = this.encoder.encodeInto(value.slice(start), target)
141
+ // start += result.read
142
+ // this.offset += result.written
143
+ // assert(this.offset <= this.buffer.byteLength, "expected this.offset <= this.buffer.byteLength")
144
+ // }
145
+ }
146
+ *encodeBytes(value) {
147
+ yield* this.encodeTypeAndArgument(2, value.byteLength);
148
+ yield* this.writeBytes(value);
149
+ }
150
+ *writeBytes(value) {
151
+ let start = 0;
152
+ while (start < value.byteLength) {
153
+ if (this.offset >= this.buffer.byteLength) {
154
+ yield __classPrivateFieldGet(this, _Encoder_instances, "m", _Encoder_flush).call(this);
155
+ }
156
+ const capacity = this.buffer.byteLength - this.offset;
157
+ const remaining = value.byteLength - start;
158
+ const chunkLength = Math.min(capacity, remaining);
159
+ this.array.set(value.subarray(start, start + chunkLength), this.offset);
160
+ start += chunkLength;
161
+ this.offset += chunkLength;
162
+ }
163
+ }
164
+ *encodeValue(value) {
165
+ if (__classPrivateFieldGet(this, _Encoder_closed, "f")) {
166
+ return;
167
+ }
168
+ if (value === false) {
169
+ yield* this.uint8(0xf4);
170
+ }
171
+ else if (value === true) {
172
+ yield* this.uint8(0xf5);
173
+ }
174
+ else if (value === null) {
175
+ yield* this.uint8(0xf6);
176
+ }
177
+ else if (value === undefined) {
178
+ yield* this.uint8(0xf7);
179
+ }
180
+ else if (typeof value === "number") {
181
+ yield* this.encodeNumber(value);
182
+ }
183
+ else if (typeof value === "string") {
184
+ yield* this.encodeString(value);
185
+ }
186
+ else if (value instanceof Uint8Array) {
187
+ yield* this.encodeBytes(value);
188
+ }
189
+ else if (Array.isArray(value)) {
190
+ yield* this.encodeTypeAndArgument(4, value.length);
191
+ for (const element of value) {
192
+ yield* this.encodeValue(element);
193
+ }
194
+ }
195
+ else {
196
+ const entries = Object.entries(value)
197
+ .map(([key, value]) => [this.encoder.encode(key), value])
198
+ .sort(Encoder.compareEntries);
199
+ yield* this.encodeTypeAndArgument(5, entries.length);
200
+ for (const [key, value] of entries) {
201
+ yield* this.encodeTypeAndArgument(3, key.byteLength);
202
+ yield* this.writeBytes(key);
203
+ yield* this.encodeValue(value);
204
+ }
205
+ }
206
+ }
207
+ *flush() {
208
+ if (__classPrivateFieldGet(this, _Encoder_closed, "f")) {
209
+ return;
210
+ }
211
+ if (this.offset > 0) {
212
+ yield __classPrivateFieldGet(this, _Encoder_instances, "m", _Encoder_flush).call(this);
213
+ }
214
+ }
215
+ // Per the deterministic CBOR spec, we're supposed to sort keys
216
+ // the byte-wise lexicographic order of the key's CBOR encoding
217
+ // - ie lower major types come before higher major types.
218
+ // One thing we know for sure about strings is that a string with
219
+ // a smaller length will sort byte-wise before a string
220
+ // with a longer length, since strings are encoded with a length
221
+ // prefix (either in the additionalInformation bits, if < 24, or
222
+ // in the next serveral bytes, but in all cases the order holds).
223
+ static compareEntries([a], [b]) {
224
+ if (a.byteLength < b.byteLength)
225
+ return -1;
226
+ if (b.byteLength < a.byteLength)
227
+ return 1;
228
+ for (let i = 0; i < a.byteLength; i++) {
229
+ if (a[i] < b[i])
230
+ return -1;
231
+ if (b[i] < a[i])
232
+ return 1;
233
+ }
234
+ return 0;
235
+ }
236
+ static getAdditionalInformation(argument) {
237
+ if (argument < 24) {
238
+ return argument;
239
+ }
240
+ else if (argument < 0x100) {
241
+ return 24;
242
+ }
243
+ else if (argument < 0x10000) {
244
+ return 25;
245
+ }
246
+ else if (argument < 0x100000000) {
247
+ return 26;
248
+ }
249
+ else {
250
+ return 27;
251
+ }
252
+ }
253
+ }
254
+ _Encoder_closed = new WeakMap(), _Encoder_instances = new WeakSet(), _Encoder_flush = function _Encoder_flush() {
255
+ if (this.chunkRecycling) {
256
+ const chunk = new Uint8Array(this.buffer, 0, this.offset);
257
+ this.offset = 0;
258
+ return chunk;
259
+ }
260
+ else {
261
+ const chunk = new Uint8Array(this.offset);
262
+ chunk.set(new Uint8Array(this.buffer, 0, this.offset));
263
+ this.offset = 0;
264
+ return chunk;
265
+ }
266
+ };
267
+ Encoder.defaultChunkSize = 4096;
268
+ /**
269
+ * Encode a single CBOR value.
270
+ * options.chunkRecycling has no effect here.
271
+ */
272
+ export function encode(value, options = {}) {
273
+ const encoder = new Encoder({ ...options, chunkRecycling: false });
274
+ let byteLength = 0;
275
+ const chunks = [];
276
+ for (const chunk of encoder.encodeValue(value)) {
277
+ chunks.push(chunk);
278
+ byteLength += chunk.byteLength;
279
+ }
280
+ for (const chunk of encoder.flush()) {
281
+ chunks.push(chunk);
282
+ byteLength += chunk.byteLength;
283
+ }
284
+ const data = new Uint8Array(byteLength);
285
+ let offset = 0;
286
+ for (const chunk of chunks) {
287
+ data.set(chunk, offset);
288
+ offset += chunk.length;
289
+ }
290
+ return data;
291
+ }
@@ -0,0 +1,37 @@
1
+ import type { CBORValue } from "./types.js";
2
+ export declare class Decoder implements AsyncIterableIterator<CBORValue> {
3
+ private offset;
4
+ private byteLength;
5
+ private readonly chunks;
6
+ private readonly constantBuffer;
7
+ private readonly constantView;
8
+ private readonly iter;
9
+ private readonly onFree?;
10
+ constructor(source: AsyncIterable<Uint8Array>, options?: {
11
+ onFree?: (chunk: Uint8Array) => void;
12
+ });
13
+ [Symbol.asyncIterator]: () => this;
14
+ private allocate;
15
+ private fill;
16
+ private constant;
17
+ private float16;
18
+ private float32;
19
+ private float64;
20
+ private uint8;
21
+ private uint16;
22
+ private uint32;
23
+ private uint64;
24
+ private decodeBytes;
25
+ private decodeString;
26
+ private getArgument;
27
+ next(): Promise<{
28
+ done: true;
29
+ value: undefined;
30
+ } | {
31
+ done: false;
32
+ value: CBORValue;
33
+ }>;
34
+ private decodeValue;
35
+ }
36
+ /** Decode an async iterable of Uint8Array chunks into an async iterable of CBOR values */
37
+ export declare function decodeAsyncIterable(source: AsyncIterable<Uint8Array>): AsyncIterableIterator<CBORValue>;
@@ -1,8 +1,8 @@
1
1
  var _a;
2
2
  import { getFloat16 } from "fp16";
3
3
  import { UnsafeIntegerError, maxSafeInteger, minSafeInteger } from "./utils.js";
4
- class DecoderStream {
5
- constructor(source) {
4
+ export class Decoder {
5
+ constructor(source, options = {}) {
6
6
  this.offset = 0;
7
7
  this.byteLength = 0;
8
8
  this.chunks = [];
@@ -25,6 +25,7 @@ class DecoderStream {
25
25
  this.uint32 = this.constant(4, (view) => view.getUint32(0));
26
26
  this.uint64 = this.constant(8, (view) => view.getBigUint64(0));
27
27
  this.iter = source[Symbol.asyncIterator]();
28
+ this.onFree = options.onFree;
28
29
  }
29
30
  async allocate(size) {
30
31
  while (this.byteLength < size) {
@@ -64,6 +65,11 @@ class DecoderStream {
64
65
  this.byteLength -= capacity;
65
66
  }
66
67
  }
68
+ if (this.onFree !== undefined) {
69
+ for (let i = 0; i < deleteCount; i++) {
70
+ this.onFree(this.chunks[i]);
71
+ }
72
+ }
67
73
  this.chunks.splice(0, deleteCount);
68
74
  }
69
75
  async decodeBytes(length) {
@@ -200,6 +206,7 @@ class DecoderStream {
200
206
  }
201
207
  }
202
208
  _a = Symbol.asyncIterator;
203
- export async function* decodeStream(source) {
204
- yield* new DecoderStream(source);
209
+ /** Decode an async iterable of Uint8Array chunks into an async iterable of CBOR values */
210
+ export async function* decodeAsyncIterable(source) {
211
+ yield* new Decoder(source);
205
212
  }
@@ -0,0 +1,3 @@
1
+ import type { CBORValue } from "./types.js";
2
+ /** Decode an iterable of Uint8Array chunks into an iterable of CBOR values */
3
+ export declare function decodeIterable(source: Iterable<Uint8Array>): IterableIterator<CBORValue>;
@@ -0,0 +1,206 @@
1
+ var _a;
2
+ import { getFloat16 } from "fp16";
3
+ import { UnsafeIntegerError, maxSafeInteger, minSafeInteger } from "./utils.js";
4
+ class Decoder {
5
+ constructor(source) {
6
+ this.offset = 0;
7
+ this.byteLength = 0;
8
+ this.chunks = [];
9
+ this.constantBuffer = new ArrayBuffer(8);
10
+ this.constantView = new DataView(this.constantBuffer);
11
+ this[_a] = () => this;
12
+ this.constant = (size, f) => {
13
+ return () => {
14
+ this.allocate(size);
15
+ const array = new Uint8Array(this.constantBuffer, 0, size);
16
+ this.fill(array);
17
+ return f(this.constantView);
18
+ };
19
+ };
20
+ this.float16 = this.constant(2, (view) => getFloat16(view, 0));
21
+ this.float32 = this.constant(4, (view) => view.getFloat32(0));
22
+ this.float64 = this.constant(8, (view) => view.getFloat64(0));
23
+ this.uint8 = this.constant(1, (view) => view.getUint8(0));
24
+ this.uint16 = this.constant(2, (view) => view.getUint16(0));
25
+ this.uint32 = this.constant(4, (view) => view.getUint32(0));
26
+ this.uint64 = this.constant(8, (view) => view.getBigUint64(0));
27
+ this.iter = source[Symbol.iterator]();
28
+ }
29
+ allocate(size) {
30
+ while (this.byteLength < size) {
31
+ const { done, value } = this.iter.next();
32
+ if (done) {
33
+ throw new Error("stream ended prematurely");
34
+ }
35
+ else {
36
+ this.chunks.push(value);
37
+ this.byteLength += value.byteLength;
38
+ }
39
+ }
40
+ }
41
+ fill(target) {
42
+ if (this.byteLength < target.byteLength) {
43
+ throw new Error("internal error - please file a bug report!");
44
+ }
45
+ let byteLength = 0;
46
+ let deleteCount = 0;
47
+ for (let i = 0; byteLength < target.byteLength; i++) {
48
+ const chunk = this.chunks[i];
49
+ const capacity = target.byteLength - byteLength;
50
+ const length = chunk.byteLength - this.offset;
51
+ if (length <= capacity) {
52
+ // copy the entire remainder of the chunk
53
+ target.set(chunk.subarray(this.offset), byteLength);
54
+ byteLength += length;
55
+ deleteCount += 1;
56
+ this.offset = 0;
57
+ this.byteLength -= length;
58
+ }
59
+ else {
60
+ // fill the remainder of the target
61
+ target.set(chunk.subarray(this.offset, this.offset + capacity), byteLength);
62
+ byteLength += capacity; // equivalent to break
63
+ this.offset += capacity;
64
+ this.byteLength -= capacity;
65
+ }
66
+ }
67
+ this.chunks.splice(0, deleteCount);
68
+ }
69
+ decodeBytes(length) {
70
+ this.allocate(length);
71
+ const array = new Uint8Array(length);
72
+ this.fill(array);
73
+ return array;
74
+ }
75
+ decodeString(length) {
76
+ this.allocate(length);
77
+ const data = new Uint8Array(length);
78
+ this.fill(data);
79
+ return new TextDecoder().decode(data);
80
+ }
81
+ getArgument(additionalInformation) {
82
+ if (additionalInformation < 24) {
83
+ return { value: additionalInformation };
84
+ }
85
+ else if (additionalInformation === 24) {
86
+ return { value: this.uint8() };
87
+ }
88
+ else if (additionalInformation === 25) {
89
+ return { value: this.uint16() };
90
+ }
91
+ else if (additionalInformation === 26) {
92
+ return { value: this.uint32() };
93
+ }
94
+ else if (additionalInformation === 27) {
95
+ const uint64 = this.uint64();
96
+ const value = maxSafeInteger < uint64 ? Infinity : Number(uint64);
97
+ return { value, uint64 };
98
+ }
99
+ else if (additionalInformation === 31) {
100
+ throw new Error("microcbor does not support decoding indefinite-length items");
101
+ }
102
+ else {
103
+ throw new Error("invalid argument encoding");
104
+ }
105
+ }
106
+ next() {
107
+ while (this.byteLength === 0) {
108
+ const { done, value } = this.iter.next();
109
+ if (done) {
110
+ return { done: true, value: undefined };
111
+ }
112
+ else if (value.byteLength > 0) {
113
+ this.chunks.push(value);
114
+ this.byteLength += value.byteLength;
115
+ }
116
+ }
117
+ const value = this.decodeValue();
118
+ return { done: false, value };
119
+ }
120
+ decodeValue() {
121
+ const initialByte = this.uint8();
122
+ const majorType = initialByte >> 5;
123
+ const additionalInformation = initialByte & 0x1f;
124
+ if (majorType === 0) {
125
+ const { value, uint64 } = this.getArgument(additionalInformation);
126
+ if (uint64 !== undefined && maxSafeInteger < uint64) {
127
+ throw new UnsafeIntegerError("cannot decode integers greater than 2^53-1", uint64);
128
+ }
129
+ else {
130
+ return value;
131
+ }
132
+ }
133
+ else if (majorType === 1) {
134
+ const { value, uint64 } = this.getArgument(additionalInformation);
135
+ if (uint64 !== undefined && -1n - uint64 < minSafeInteger) {
136
+ throw new UnsafeIntegerError("cannot decode integers less than -2^53+1", -1n - uint64);
137
+ }
138
+ else {
139
+ return -1 - value;
140
+ }
141
+ }
142
+ else if (majorType === 2) {
143
+ const { value: length } = this.getArgument(additionalInformation);
144
+ return this.decodeBytes(length);
145
+ }
146
+ else if (majorType === 3) {
147
+ const { value: length } = this.getArgument(additionalInformation);
148
+ return this.decodeString(length);
149
+ }
150
+ else if (majorType === 4) {
151
+ const { value: length } = this.getArgument(additionalInformation);
152
+ const value = new Array(length);
153
+ for (let i = 0; i < length; i++) {
154
+ value[i] = this.decodeValue();
155
+ }
156
+ return value;
157
+ }
158
+ else if (majorType === 5) {
159
+ const { value: length } = this.getArgument(additionalInformation);
160
+ const value = {};
161
+ for (let i = 0; i < length; i++) {
162
+ const key = this.decodeValue();
163
+ if (typeof key !== "string") {
164
+ throw new Error("microcbor only supports string keys in objects");
165
+ }
166
+ value[key] = this.decodeValue();
167
+ }
168
+ return value;
169
+ }
170
+ else if (majorType === 6) {
171
+ throw new Error("microcbor does not support tagged data items");
172
+ }
173
+ else if (majorType === 7) {
174
+ switch (additionalInformation) {
175
+ case 20:
176
+ return false;
177
+ case 21:
178
+ return true;
179
+ case 22:
180
+ return null;
181
+ case 23:
182
+ return undefined;
183
+ case 24:
184
+ throw new Error("microcbor does not support decoding unassigned simple values");
185
+ case 25:
186
+ return this.float16();
187
+ case 26:
188
+ return this.float32();
189
+ case 27:
190
+ return this.float64();
191
+ case 31:
192
+ throw new Error("microcbor does not support decoding indefinite-length items");
193
+ default:
194
+ throw new Error("invalid simple value");
195
+ }
196
+ }
197
+ else {
198
+ throw new Error("invalid major type");
199
+ }
200
+ }
201
+ }
202
+ _a = Symbol.iterator;
203
+ /** Decode an iterable of Uint8Array chunks into an iterable of CBOR values */
204
+ export function* decodeIterable(source) {
205
+ yield* new Decoder(source);
206
+ }
@@ -0,0 +1,4 @@
1
+ import type { CBORValue } from "./types.js";
2
+ import { EncodeOptions } from "./Encoder.js";
3
+ /** Encode an async iterable of CBOR values into an async iterable of Uint8Array chunks */
4
+ export declare function encodeAsyncIterable(source: AsyncIterable<CBORValue>, options?: EncodeOptions): AsyncIterableIterator<Uint8Array>;
@@ -0,0 +1,9 @@
1
+ import { Encoder } from "./Encoder.js";
2
+ /** Encode an async iterable of CBOR values into an async iterable of Uint8Array chunks */
3
+ export async function* encodeAsyncIterable(source, options = {}) {
4
+ const encoder = new Encoder(options);
5
+ for await (const value of source) {
6
+ yield* encoder.encodeValue(value);
7
+ }
8
+ yield* encoder.flush();
9
+ }
@@ -0,0 +1,4 @@
1
+ import type { CBORValue } from "./types.js";
2
+ import { EncodeOptions } from "./Encoder.js";
3
+ /** Encode an iterable of CBOR values into an iterable of Uint8Array chunks */
4
+ export declare function encodeIterable(source: Iterable<CBORValue>, options?: EncodeOptions): IterableIterator<Uint8Array>;
@@ -0,0 +1,9 @@
1
+ import { Encoder } from "./Encoder.js";
2
+ /** Encode an iterable of CBOR values into an iterable of Uint8Array chunks */
3
+ export function* encodeIterable(source, options = {}) {
4
+ const encoder = new Encoder(options);
5
+ for (const value of source) {
6
+ yield* encoder.encodeValue(value);
7
+ }
8
+ yield* encoder.flush();
9
+ }
@@ -1,2 +1,6 @@
1
- import type { CBORValue } from "./types.js";
1
+ import { CBORValue } from "./types.js";
2
+ /**
3
+ * Calculate the byte length that a value will encode into
4
+ * without actually allocating anything.
5
+ */
2
6
  export declare function encodingLength(value: CBORValue): number;