@ipld/car 4.0.0 → 4.1.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/README.md +109 -0
- package/api.ts +22 -4
- package/buffer-writer +1 -0
- package/cjs/browser-test/common.js +3 -3
- package/cjs/browser-test/node-test-large.js +8 -8
- package/cjs/browser-test/test-buffer-writer.js +330 -0
- package/cjs/browser-test/test-errors.js +2 -2
- package/cjs/browser-test/test-indexer.js +1 -1
- package/cjs/browser-test/test-reader.js +2 -2
- package/cjs/browser-test/test-writer.js +3 -3
- package/cjs/lib/buffer-writer.js +161 -0
- package/cjs/lib/decoder.js +2 -2
- package/cjs/lib/encoder.js +3 -3
- package/cjs/lib/iterator-channel.js +1 -1
- package/cjs/node-test/common.js +3 -3
- package/cjs/node-test/node-test-large.js +8 -8
- package/cjs/node-test/test-buffer-writer.js +330 -0
- package/cjs/node-test/test-errors.js +2 -2
- package/cjs/node-test/test-indexer.js +1 -1
- package/cjs/node-test/test-reader.js +2 -2
- package/cjs/node-test/test-writer.js +3 -3
- package/esm/browser-test/test-buffer-writer.js +311 -0
- package/esm/browser-test/test-indexer.js +1 -1
- package/esm/browser-test/test-reader.js +2 -2
- package/esm/browser-test/test-writer.js +3 -3
- package/esm/lib/buffer-writer.js +126 -0
- package/esm/lib/encoder.js +1 -1
- package/esm/lib/iterator-channel.js +1 -1
- package/esm/node-test/test-buffer-writer.js +311 -0
- package/esm/node-test/test-indexer.js +1 -1
- package/esm/node-test/test-reader.js +2 -2
- package/esm/node-test/test-writer.js +3 -3
- package/lib/buffer-writer.js +286 -0
- package/lib/encoder.js +1 -1
- package/lib/iterator-channel.js +1 -1
- package/package.json +14 -4
- package/test/test-buffer-writer.js +256 -0
- package/test/test-indexer.js +1 -1
- package/test/test-reader.js +2 -2
- package/test/test-writer.js +3 -3
- package/tsconfig.json +1 -0
- package/types/api.d.ts +16 -0
- package/types/api.d.ts.map +1 -1
- package/types/lib/buffer-writer.d.ts +86 -0
- package/types/lib/buffer-writer.d.ts.map +1 -0
- package/types/test/test-buffer-writer.d.ts +2 -0
- package/types/test/test-buffer-writer.d.ts.map +1 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import * as CarBufferWriter from '../lib/buffer-writer.js';
|
|
2
|
+
import { CarReader } from '../lib/reader-browser.js';
|
|
3
|
+
import { createHeader } from '../lib/encoder.js';
|
|
4
|
+
import { assert } from './common.js';
|
|
5
|
+
import {
|
|
6
|
+
CID,
|
|
7
|
+
varint
|
|
8
|
+
} from 'multiformats';
|
|
9
|
+
import * as CBOR from '@ipld/dag-cbor';
|
|
10
|
+
import {
|
|
11
|
+
sha256,
|
|
12
|
+
sha512
|
|
13
|
+
} from 'multiformats/hashes/sha2';
|
|
14
|
+
import { identity } from 'multiformats/hashes/identity';
|
|
15
|
+
import * as Raw from 'multiformats/codecs/raw';
|
|
16
|
+
import * as Block from 'multiformats/block';
|
|
17
|
+
describe('CarBufferWriter', () => {
|
|
18
|
+
const cid = CID.parse('bafkreifuosuzujyf4i6psbneqtwg2fhplc2wxptc5euspa2gn3bwhnihfu');
|
|
19
|
+
describe('calculateHeaderLength', async () => {
|
|
20
|
+
for (const count of [
|
|
21
|
+
0,
|
|
22
|
+
1,
|
|
23
|
+
10,
|
|
24
|
+
18,
|
|
25
|
+
24,
|
|
26
|
+
48,
|
|
27
|
+
124,
|
|
28
|
+
255,
|
|
29
|
+
258,
|
|
30
|
+
65536 - 1,
|
|
31
|
+
65536
|
|
32
|
+
]) {
|
|
33
|
+
it(`calculateHeaderLength(new Array(${ count }).fill(36))`, () => {
|
|
34
|
+
const roots = new Array(count).fill(cid);
|
|
35
|
+
const sizes = new Array(count).fill(cid.bytes.byteLength);
|
|
36
|
+
assert.deepEqual(CarBufferWriter.calculateHeaderLength(sizes), createHeader(roots).byteLength);
|
|
37
|
+
});
|
|
38
|
+
it(`calculateHeaderLength(new Array(${ count }).fill(36))`, () => {
|
|
39
|
+
const roots = new Array(count).fill(cid);
|
|
40
|
+
const rootLengths = roots.map(c => c.bytes.byteLength);
|
|
41
|
+
assert.deepEqual(CarBufferWriter.calculateHeaderLength(rootLengths), createHeader(roots).byteLength);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
it('estimate on large CIDs', () => {
|
|
45
|
+
const largeCID = CID.parse(`bafkqbbac${ 'a'.repeat(416) }`);
|
|
46
|
+
assert.equal(CarBufferWriter.calculateHeaderLength([
|
|
47
|
+
cid.bytes.byteLength,
|
|
48
|
+
largeCID.bytes.byteLength
|
|
49
|
+
]), createHeader([
|
|
50
|
+
cid,
|
|
51
|
+
largeCID
|
|
52
|
+
]).byteLength);
|
|
53
|
+
});
|
|
54
|
+
it('estimate on large CIDs 2', () => {
|
|
55
|
+
const largeCID = CID.createV1(Raw.code, identity.digest(new Uint8Array(512).fill(1)));
|
|
56
|
+
assert.equal(CarBufferWriter.calculateHeaderLength([
|
|
57
|
+
cid.bytes.byteLength,
|
|
58
|
+
largeCID.bytes.byteLength
|
|
59
|
+
]), createHeader([
|
|
60
|
+
cid,
|
|
61
|
+
largeCID
|
|
62
|
+
]).byteLength);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe('writer', () => {
|
|
66
|
+
it('estimate header and write blocks', async () => {
|
|
67
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(1);
|
|
68
|
+
const dataSize = 256;
|
|
69
|
+
const buffer = new ArrayBuffer(headerSize + dataSize);
|
|
70
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize });
|
|
71
|
+
const b1 = await Block.encode({
|
|
72
|
+
value: { hello: 'world' },
|
|
73
|
+
codec: CBOR,
|
|
74
|
+
hasher: sha256
|
|
75
|
+
});
|
|
76
|
+
writer.write(b1);
|
|
77
|
+
const b2 = await Block.encode({
|
|
78
|
+
value: { bye: 'world' },
|
|
79
|
+
codec: CBOR,
|
|
80
|
+
hasher: sha256
|
|
81
|
+
});
|
|
82
|
+
writer.write(b2);
|
|
83
|
+
writer.addRoot(b1.cid);
|
|
84
|
+
const bytes = writer.close();
|
|
85
|
+
const reader = await CarReader.fromBytes(bytes);
|
|
86
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid]);
|
|
87
|
+
assert.deepEqual(reader._blocks, [
|
|
88
|
+
{
|
|
89
|
+
cid: b1.cid,
|
|
90
|
+
bytes: b1.bytes
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
cid: b2.cid,
|
|
94
|
+
bytes: b2.bytes
|
|
95
|
+
}
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
it('overestimate header', async () => {
|
|
99
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(2);
|
|
100
|
+
const dataSize = 256;
|
|
101
|
+
const buffer = new ArrayBuffer(headerSize + dataSize);
|
|
102
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize });
|
|
103
|
+
const b1 = await Block.encode({
|
|
104
|
+
value: { hello: 'world' },
|
|
105
|
+
codec: CBOR,
|
|
106
|
+
hasher: sha256
|
|
107
|
+
});
|
|
108
|
+
writer.write(b1);
|
|
109
|
+
const b2 = await Block.encode({
|
|
110
|
+
value: { bye: 'world' },
|
|
111
|
+
codec: CBOR,
|
|
112
|
+
hasher: sha256
|
|
113
|
+
});
|
|
114
|
+
writer.write(b2);
|
|
115
|
+
writer.addRoot(b1.cid);
|
|
116
|
+
assert.throws(() => writer.close(), /Header size was overestimate/);
|
|
117
|
+
const bytes = writer.close({ resize: true });
|
|
118
|
+
const reader = await CarReader.fromBytes(bytes);
|
|
119
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid]);
|
|
120
|
+
assert.deepEqual(reader._blocks, [
|
|
121
|
+
{
|
|
122
|
+
cid: b1.cid,
|
|
123
|
+
bytes: b1.bytes
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
cid: b2.cid,
|
|
127
|
+
bytes: b2.bytes
|
|
128
|
+
}
|
|
129
|
+
]);
|
|
130
|
+
});
|
|
131
|
+
it('underestimate header', async () => {
|
|
132
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(2);
|
|
133
|
+
const dataSize = 300;
|
|
134
|
+
const buffer = new ArrayBuffer(headerSize + dataSize);
|
|
135
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize });
|
|
136
|
+
const b1 = await Block.encode({
|
|
137
|
+
value: { hello: 'world' },
|
|
138
|
+
codec: CBOR,
|
|
139
|
+
hasher: sha256
|
|
140
|
+
});
|
|
141
|
+
writer.write(b1);
|
|
142
|
+
writer.addRoot(b1.cid);
|
|
143
|
+
const b2 = await Block.encode({
|
|
144
|
+
value: { bye: 'world' },
|
|
145
|
+
codec: CBOR,
|
|
146
|
+
hasher: sha512
|
|
147
|
+
});
|
|
148
|
+
writer.write(b2);
|
|
149
|
+
assert.throws(() => writer.addRoot(b2.cid), /has no capacity/);
|
|
150
|
+
writer.addRoot(b2.cid, { resize: true });
|
|
151
|
+
const bytes = writer.close();
|
|
152
|
+
const reader = await CarReader.fromBytes(bytes);
|
|
153
|
+
assert.deepEqual(await reader.getRoots(), [
|
|
154
|
+
b1.cid,
|
|
155
|
+
b2.cid
|
|
156
|
+
]);
|
|
157
|
+
assert.deepEqual(reader._blocks, [
|
|
158
|
+
{
|
|
159
|
+
cid: b1.cid,
|
|
160
|
+
bytes: b1.bytes
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
cid: b2.cid,
|
|
164
|
+
bytes: b2.bytes
|
|
165
|
+
}
|
|
166
|
+
]);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
it('has no space for the root', async () => {
|
|
170
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(1);
|
|
171
|
+
const dataSize = 100;
|
|
172
|
+
const buffer = new ArrayBuffer(headerSize + dataSize);
|
|
173
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize });
|
|
174
|
+
const b1 = await Block.encode({
|
|
175
|
+
value: { hello: 'world' },
|
|
176
|
+
codec: CBOR,
|
|
177
|
+
hasher: sha256
|
|
178
|
+
});
|
|
179
|
+
writer.write(b1);
|
|
180
|
+
writer.addRoot(b1.cid);
|
|
181
|
+
const b2 = await Block.encode({
|
|
182
|
+
value: { bye: 'world' },
|
|
183
|
+
codec: CBOR,
|
|
184
|
+
hasher: sha256
|
|
185
|
+
});
|
|
186
|
+
writer.write(b2);
|
|
187
|
+
assert.throws(() => writer.addRoot(b2.cid), /Buffer has no capacity for a new root/);
|
|
188
|
+
assert.throws(() => writer.addRoot(b2.cid, { resize: true }), /Buffer has no capacity for a new root/);
|
|
189
|
+
const bytes = writer.close();
|
|
190
|
+
const reader = await CarReader.fromBytes(bytes);
|
|
191
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid]);
|
|
192
|
+
assert.deepEqual(reader._blocks, [
|
|
193
|
+
{
|
|
194
|
+
cid: b1.cid,
|
|
195
|
+
bytes: b1.bytes
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
cid: b2.cid,
|
|
199
|
+
bytes: b2.bytes
|
|
200
|
+
}
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
it('has no space for the block', async () => {
|
|
204
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(1);
|
|
205
|
+
const dataSize = 58;
|
|
206
|
+
const buffer = new ArrayBuffer(headerSize + dataSize);
|
|
207
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize });
|
|
208
|
+
const b1 = await Block.encode({
|
|
209
|
+
value: { hello: 'world' },
|
|
210
|
+
codec: CBOR,
|
|
211
|
+
hasher: sha256
|
|
212
|
+
});
|
|
213
|
+
writer.write(b1);
|
|
214
|
+
writer.addRoot(b1.cid);
|
|
215
|
+
const b2 = await Block.encode({
|
|
216
|
+
value: { bye: 'world' },
|
|
217
|
+
codec: CBOR,
|
|
218
|
+
hasher: sha256
|
|
219
|
+
});
|
|
220
|
+
assert.throws(() => writer.write(b2), /Buffer has no capacity for this block/);
|
|
221
|
+
const bytes = writer.close();
|
|
222
|
+
const reader = await CarReader.fromBytes(bytes);
|
|
223
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid]);
|
|
224
|
+
assert.deepEqual(reader._blocks, [{
|
|
225
|
+
cid: b1.cid,
|
|
226
|
+
bytes: b1.bytes
|
|
227
|
+
}]);
|
|
228
|
+
});
|
|
229
|
+
it('provide roots', async () => {
|
|
230
|
+
const b1 = await Block.encode({
|
|
231
|
+
value: { hello: 'world' },
|
|
232
|
+
codec: CBOR,
|
|
233
|
+
hasher: sha256
|
|
234
|
+
});
|
|
235
|
+
const b2 = await Block.encode({
|
|
236
|
+
value: { bye: 'world' },
|
|
237
|
+
codec: CBOR,
|
|
238
|
+
hasher: sha512
|
|
239
|
+
});
|
|
240
|
+
const buffer = new ArrayBuffer(300);
|
|
241
|
+
const writer = CarBufferWriter.createWriter(buffer, {
|
|
242
|
+
roots: [
|
|
243
|
+
b1.cid,
|
|
244
|
+
b2.cid
|
|
245
|
+
]
|
|
246
|
+
});
|
|
247
|
+
writer.write(b1);
|
|
248
|
+
writer.write(b2);
|
|
249
|
+
const bytes = writer.close();
|
|
250
|
+
const reader = await CarReader.fromBytes(bytes);
|
|
251
|
+
assert.deepEqual(await reader.getRoots(), [
|
|
252
|
+
b1.cid,
|
|
253
|
+
b2.cid
|
|
254
|
+
]);
|
|
255
|
+
assert.deepEqual(reader._blocks, [
|
|
256
|
+
{
|
|
257
|
+
cid: b1.cid,
|
|
258
|
+
bytes: b1.bytes
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
cid: b2.cid,
|
|
262
|
+
bytes: b2.bytes
|
|
263
|
+
}
|
|
264
|
+
]);
|
|
265
|
+
});
|
|
266
|
+
it('provide large CID root', async () => {
|
|
267
|
+
const bytes = new Uint8Array(512).fill(1);
|
|
268
|
+
const b1 = await Block.encode({
|
|
269
|
+
value: { hello: 'world' },
|
|
270
|
+
codec: CBOR,
|
|
271
|
+
hasher: sha256
|
|
272
|
+
});
|
|
273
|
+
const b2 = {
|
|
274
|
+
cid: CID.createV1(Raw.code, identity.digest(bytes)),
|
|
275
|
+
bytes
|
|
276
|
+
};
|
|
277
|
+
const headerSize = CBOR.encode({
|
|
278
|
+
version: 1,
|
|
279
|
+
roots: [
|
|
280
|
+
b1.cid,
|
|
281
|
+
b2.cid
|
|
282
|
+
]
|
|
283
|
+
}).byteLength;
|
|
284
|
+
const bodySize = CarBufferWriter.blockLength(b1) + CarBufferWriter.blockLength(b2);
|
|
285
|
+
const varintSize = varint.encodingLength(headerSize);
|
|
286
|
+
const writer = CarBufferWriter.createWriter(new ArrayBuffer(varintSize + headerSize + bodySize), {
|
|
287
|
+
roots: [
|
|
288
|
+
b1.cid,
|
|
289
|
+
b2.cid
|
|
290
|
+
]
|
|
291
|
+
});
|
|
292
|
+
writer.write(b1);
|
|
293
|
+
writer.write(b2);
|
|
294
|
+
const car = writer.close();
|
|
295
|
+
const reader = await CarReader.fromBytes(car);
|
|
296
|
+
assert.deepEqual(await reader.getRoots(), [
|
|
297
|
+
b1.cid,
|
|
298
|
+
b2.cid
|
|
299
|
+
]);
|
|
300
|
+
assert.deepEqual(reader._blocks, [
|
|
301
|
+
{
|
|
302
|
+
cid: b1.cid,
|
|
303
|
+
bytes: b1.bytes
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
cid: b2.cid,
|
|
307
|
+
bytes: b2.bytes
|
|
308
|
+
}
|
|
309
|
+
]);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
@@ -24,7 +24,7 @@ describe('CarIndexer fromBytes()', () => {
|
|
|
24
24
|
const indexer = await CarIndexer.fromBytes(goCarV2Bytes);
|
|
25
25
|
const roots = await indexer.getRoots();
|
|
26
26
|
assert.strictEqual(roots.length, 1);
|
|
27
|
-
assert(goCarV2Roots[0].equals(roots[0]));
|
|
27
|
+
assert.ok(goCarV2Roots[0].equals(roots[0]));
|
|
28
28
|
assert.strictEqual(indexer.version, 2);
|
|
29
29
|
const indexData = [];
|
|
30
30
|
for await (const index of indexer) {
|
|
@@ -68,13 +68,13 @@ describe('CarReader fromBytes()', () => {
|
|
|
68
68
|
const reader = await CarReader.fromBytes(goCarV2Bytes);
|
|
69
69
|
const roots = await reader.getRoots();
|
|
70
70
|
assert.strictEqual(roots.length, 1);
|
|
71
|
-
assert(goCarV2Roots[0].equals(roots[0]));
|
|
71
|
+
assert.ok(goCarV2Roots[0].equals(roots[0]));
|
|
72
72
|
assert.strictEqual(reader.version, 2);
|
|
73
73
|
for (const {cid} of goCarV2Index) {
|
|
74
74
|
const block = await reader.get(cid);
|
|
75
75
|
assert.isDefined(block);
|
|
76
76
|
if (block) {
|
|
77
|
-
assert(cid.equals(block.cid));
|
|
77
|
+
assert.ok(cid.equals(block.cid));
|
|
78
78
|
let content;
|
|
79
79
|
if (cid.code === dagPb.code) {
|
|
80
80
|
content = dagPb.decode(block.bytes);
|
|
@@ -193,9 +193,9 @@ describe('CarWriter', () => {
|
|
|
193
193
|
const rawBytes = await append(0);
|
|
194
194
|
const pbBytes = await append(1);
|
|
195
195
|
const cborBytes = await append(2);
|
|
196
|
-
assert(rawBytes.length > 0);
|
|
197
|
-
assert(pbBytes.length > 0);
|
|
198
|
-
assert(cborBytes.length > 0);
|
|
196
|
+
assert.ok(rawBytes.length > 0);
|
|
197
|
+
assert.ok(pbBytes.length > 0);
|
|
198
|
+
assert.ok(cborBytes.length > 0);
|
|
199
199
|
const reassembled = concatBytes([
|
|
200
200
|
headerBytes,
|
|
201
201
|
rawBytes,
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import varint from 'varint';
|
|
2
|
+
import {
|
|
3
|
+
Token,
|
|
4
|
+
Type
|
|
5
|
+
} from 'cborg';
|
|
6
|
+
import { tokensToLength } from 'cborg/length';
|
|
7
|
+
import * as CBOR from '@ipld/dag-cbor';
|
|
8
|
+
class CarBufferWriter {
|
|
9
|
+
constructor(bytes, headerSize) {
|
|
10
|
+
this.bytes = bytes;
|
|
11
|
+
this.byteOffset = headerSize;
|
|
12
|
+
this.roots = [];
|
|
13
|
+
this.headerSize = headerSize;
|
|
14
|
+
}
|
|
15
|
+
addRoot(root, options) {
|
|
16
|
+
addRoot(this, root, options);
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
write(block) {
|
|
20
|
+
addBlock(this, block);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
close(options) {
|
|
24
|
+
return close(this, options);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export const addRoot = (writer, root, {
|
|
28
|
+
resize = false
|
|
29
|
+
} = {}) => {
|
|
30
|
+
const {bytes, headerSize, byteOffset, roots} = writer;
|
|
31
|
+
writer.roots.push(root);
|
|
32
|
+
const size = headerLength(writer);
|
|
33
|
+
if (size > headerSize) {
|
|
34
|
+
if (size - headerSize + byteOffset < bytes.byteLength) {
|
|
35
|
+
if (resize) {
|
|
36
|
+
resizeHeader(writer, size);
|
|
37
|
+
} else {
|
|
38
|
+
roots.pop();
|
|
39
|
+
throw new RangeError(`Header of size ${ headerSize } has no capacity for new root ${ root }.
|
|
40
|
+
However there is a space in the buffer and you could call addRoot(root, { resize: root }) to resize header to make a space for this root.`);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
roots.pop();
|
|
44
|
+
throw new RangeError(`Buffer has no capacity for a new root ${ root }`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
export const blockLength = ({cid, bytes}) => {
|
|
49
|
+
const size = cid.bytes.byteLength + bytes.byteLength;
|
|
50
|
+
return varint.encodingLength(size) + size;
|
|
51
|
+
};
|
|
52
|
+
export const addBlock = (writer, {cid, bytes}) => {
|
|
53
|
+
const byteLength = cid.bytes.byteLength + bytes.byteLength;
|
|
54
|
+
const size = varint.encode(byteLength);
|
|
55
|
+
if (writer.byteOffset + size.length + byteLength > writer.bytes.byteLength) {
|
|
56
|
+
throw new RangeError('Buffer has no capacity for this block');
|
|
57
|
+
} else {
|
|
58
|
+
writeBytes(writer, size);
|
|
59
|
+
writeBytes(writer, cid.bytes);
|
|
60
|
+
writeBytes(writer, bytes);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
export const close = (writer, {
|
|
64
|
+
resize = false
|
|
65
|
+
} = {}) => {
|
|
66
|
+
const {roots, bytes, byteOffset, headerSize} = writer;
|
|
67
|
+
const headerBytes = CBOR.encode({
|
|
68
|
+
version: 1,
|
|
69
|
+
roots
|
|
70
|
+
});
|
|
71
|
+
const varintBytes = varint.encode(headerBytes.length);
|
|
72
|
+
const size = varintBytes.length + headerBytes.byteLength;
|
|
73
|
+
const offset = headerSize - size;
|
|
74
|
+
if (offset === 0) {
|
|
75
|
+
writeHeader(writer, varintBytes, headerBytes);
|
|
76
|
+
return bytes.subarray(0, byteOffset);
|
|
77
|
+
} else if (resize) {
|
|
78
|
+
resizeHeader(writer, size);
|
|
79
|
+
writeHeader(writer, varintBytes, headerBytes);
|
|
80
|
+
return bytes.subarray(0, writer.byteOffset);
|
|
81
|
+
} else {
|
|
82
|
+
throw new RangeError(`Header size was overestimated.
|
|
83
|
+
You can use close({ resize: true }) to resize header`);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
export const resizeHeader = (writer, byteLength) => {
|
|
87
|
+
const {bytes, headerSize} = writer;
|
|
88
|
+
bytes.set(bytes.subarray(headerSize, writer.byteOffset), byteLength);
|
|
89
|
+
writer.byteOffset += byteLength - headerSize;
|
|
90
|
+
writer.headerSize = byteLength;
|
|
91
|
+
};
|
|
92
|
+
const writeBytes = (writer, bytes) => {
|
|
93
|
+
writer.bytes.set(bytes, writer.byteOffset);
|
|
94
|
+
writer.byteOffset += bytes.length;
|
|
95
|
+
};
|
|
96
|
+
const writeHeader = ({bytes}, varint, header) => {
|
|
97
|
+
bytes.set(varint);
|
|
98
|
+
bytes.set(header, varint.length);
|
|
99
|
+
};
|
|
100
|
+
const headerPreludeTokens = [
|
|
101
|
+
new Token(Type.map, 2),
|
|
102
|
+
new Token(Type.string, 'version'),
|
|
103
|
+
new Token(Type.uint, 1),
|
|
104
|
+
new Token(Type.string, 'roots')
|
|
105
|
+
];
|
|
106
|
+
const CID_TAG = new Token(Type.tag, 42);
|
|
107
|
+
export const calculateHeaderLength = rootLengths => {
|
|
108
|
+
const tokens = [...headerPreludeTokens];
|
|
109
|
+
tokens.push(new Token(Type.array, rootLengths.length));
|
|
110
|
+
for (const rootLength of rootLengths) {
|
|
111
|
+
tokens.push(CID_TAG);
|
|
112
|
+
tokens.push(new Token(Type.bytes, { length: rootLength + 1 }));
|
|
113
|
+
}
|
|
114
|
+
const length = tokensToLength(tokens);
|
|
115
|
+
return varint.encodingLength(length) + length;
|
|
116
|
+
};
|
|
117
|
+
export const headerLength = ({roots}) => calculateHeaderLength(roots.map(cid => cid.bytes.byteLength));
|
|
118
|
+
export const estimateHeaderLength = (rootCount, rootByteLength = 36) => calculateHeaderLength(new Array(rootCount).fill(rootByteLength));
|
|
119
|
+
export const createWriter = (buffer, {roots = [], byteOffset = 0, byteLength = buffer.byteLength, headerSize = headerLength({ roots })} = {}) => {
|
|
120
|
+
const bytes = new Uint8Array(buffer, byteOffset, byteLength);
|
|
121
|
+
const writer = new CarBufferWriter(bytes, headerSize);
|
|
122
|
+
for (const root of roots) {
|
|
123
|
+
writer.addRoot(root);
|
|
124
|
+
}
|
|
125
|
+
return writer;
|
|
126
|
+
};
|
package/esm/lib/encoder.js
CHANGED