@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ipld/car",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.2",
|
|
4
4
|
"description": "Content Addressable aRchive format reader and writer",
|
|
5
5
|
"main": "./cjs/car.js",
|
|
6
6
|
"types": "./types/car.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"test": "npm run lint && npm run test:node && npm run test:cjs && npm run test --prefix examples/",
|
|
19
19
|
"test:ci": "npm run lint && npm run test:node && npm run test:esm && npm run test:cjs && npm run test --prefix examples/",
|
|
20
20
|
"coverage": "c8 --reporter=html --reporter=text mocha test/test-*.js && npx st -d coverage -p 8888",
|
|
21
|
-
"docs": "jsdoc4readme --readme --description-only lib/reader*.js lib/indexed-reader.js lib/iterator.js lib/indexer.js lib/writer*.js lib/decoder.js"
|
|
21
|
+
"docs": "jsdoc4readme --readme --description-only lib/reader*.js lib/indexed-reader.js lib/iterator.js lib/indexer.js lib/writer*.js lib/buffer-writer.js lib/decoder.js"
|
|
22
22
|
},
|
|
23
23
|
"keywords": [
|
|
24
24
|
"car",
|
|
@@ -63,10 +63,16 @@
|
|
|
63
63
|
"browser": "./esm/lib/writer-browser.js",
|
|
64
64
|
"require": "./cjs/lib/writer.js",
|
|
65
65
|
"import": "./esm/lib/writer.js"
|
|
66
|
+
},
|
|
67
|
+
"./buffer-writer": {
|
|
68
|
+
"browser": "./esm/lib/buffer-writer.js",
|
|
69
|
+
"require": "./cjs/lib/buffer-writer.js",
|
|
70
|
+
"import": "./esm/lib/buffer-writer.js"
|
|
66
71
|
}
|
|
67
72
|
},
|
|
68
73
|
"dependencies": {
|
|
69
74
|
"@ipld/dag-cbor": "^7.0.0",
|
|
75
|
+
"cborg": "^1.9.0",
|
|
70
76
|
"multiformats": "^9.5.4",
|
|
71
77
|
"varint": "^6.0.0"
|
|
72
78
|
},
|
|
@@ -87,7 +93,7 @@
|
|
|
87
93
|
"jsdoc4readme": "^1.4.0",
|
|
88
94
|
"mocha": "^9.1.3",
|
|
89
95
|
"polendina": "~2.0.1",
|
|
90
|
-
"standard": "^
|
|
96
|
+
"standard": "^17.0.0",
|
|
91
97
|
"typescript": "~4.6.2"
|
|
92
98
|
},
|
|
93
99
|
"standard": {
|
|
@@ -126,6 +132,9 @@
|
|
|
126
132
|
"writer": [
|
|
127
133
|
"types/lib/writer.d.ts"
|
|
128
134
|
],
|
|
135
|
+
"buffer-writer": [
|
|
136
|
+
"types/lib/buffer-writer.d.ts"
|
|
137
|
+
],
|
|
129
138
|
"*": [
|
|
130
139
|
"types/*"
|
|
131
140
|
],
|
|
@@ -235,6 +244,7 @@
|
|
|
235
244
|
"./iterator": "./cjs/lib/iterator.js",
|
|
236
245
|
"./writer": "./cjs/lib/writer-browser.js",
|
|
237
246
|
"./esm/lib/writer.js": "./esm/lib/writer-browser.js",
|
|
238
|
-
"./cjs/lib/writer.js": "./cjs/lib/writer-browser.js"
|
|
247
|
+
"./cjs/lib/writer.js": "./cjs/lib/writer-browser.js",
|
|
248
|
+
"./buffer-writer": "./cjs/lib/buffer-writer.js"
|
|
239
249
|
}
|
|
240
250
|
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import * as CarBufferWriter from '@ipld/car/buffer-writer'
|
|
4
|
+
import { CarReader } from '@ipld/car/reader'
|
|
5
|
+
import { createHeader } from '../lib/encoder.js'
|
|
6
|
+
import { assert } from './common.js'
|
|
7
|
+
import { CID, varint } from 'multiformats'
|
|
8
|
+
import * as CBOR from '@ipld/dag-cbor'
|
|
9
|
+
import { sha256, sha512 } from 'multiformats/hashes/sha2'
|
|
10
|
+
import { identity } from 'multiformats/hashes/identity'
|
|
11
|
+
import * as Raw from 'multiformats/codecs/raw'
|
|
12
|
+
import * as Block from 'multiformats/block'
|
|
13
|
+
|
|
14
|
+
describe('CarBufferWriter', () => {
|
|
15
|
+
const cid = CID.parse('bafkreifuosuzujyf4i6psbneqtwg2fhplc2wxptc5euspa2gn3bwhnihfu')
|
|
16
|
+
describe('calculateHeaderLength', async () => {
|
|
17
|
+
for (const count of [0, 1, 10, 18, 24, 48, 124, 255, 258, 65536 - 1, 65536]) {
|
|
18
|
+
it(`calculateHeaderLength(new Array(${count}).fill(36))`, () => {
|
|
19
|
+
const roots = new Array(count).fill(cid)
|
|
20
|
+
const sizes = new Array(count).fill(cid.bytes.byteLength)
|
|
21
|
+
assert.deepEqual(
|
|
22
|
+
CarBufferWriter.calculateHeaderLength(sizes),
|
|
23
|
+
createHeader(roots).byteLength
|
|
24
|
+
)
|
|
25
|
+
})
|
|
26
|
+
it(`calculateHeaderLength(new Array(${count}).fill(36))`, () => {
|
|
27
|
+
const roots = new Array(count).fill(cid)
|
|
28
|
+
const rootLengths = roots.map((c) => c.bytes.byteLength)
|
|
29
|
+
assert.deepEqual(CarBufferWriter.calculateHeaderLength(rootLengths), createHeader(roots).byteLength)
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
it('estimate on large CIDs', () => {
|
|
33
|
+
const largeCID = CID.parse(`bafkqbbac${'a'.repeat(416)}`)
|
|
34
|
+
assert.equal(
|
|
35
|
+
CarBufferWriter.calculateHeaderLength([
|
|
36
|
+
cid.bytes.byteLength,
|
|
37
|
+
largeCID.bytes.byteLength
|
|
38
|
+
]),
|
|
39
|
+
createHeader([
|
|
40
|
+
cid,
|
|
41
|
+
largeCID
|
|
42
|
+
]).byteLength
|
|
43
|
+
)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('estimate on large CIDs 2', () => {
|
|
47
|
+
const largeCID = CID.createV1(Raw.code, identity.digest(new Uint8Array(512).fill(1)))
|
|
48
|
+
assert.equal(
|
|
49
|
+
CarBufferWriter.calculateHeaderLength([
|
|
50
|
+
cid.bytes.byteLength,
|
|
51
|
+
largeCID.bytes.byteLength
|
|
52
|
+
]),
|
|
53
|
+
createHeader([cid, largeCID]).byteLength
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('writer', () => {
|
|
59
|
+
it('estimate header and write blocks', async () => {
|
|
60
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(1)
|
|
61
|
+
const dataSize = 256
|
|
62
|
+
const buffer = new ArrayBuffer(headerSize + dataSize)
|
|
63
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize })
|
|
64
|
+
const b1 = await Block.encode({
|
|
65
|
+
value: { hello: 'world' },
|
|
66
|
+
codec: CBOR,
|
|
67
|
+
hasher: sha256
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
writer.write(b1)
|
|
71
|
+
|
|
72
|
+
const b2 = await Block.encode({
|
|
73
|
+
value: { bye: 'world' },
|
|
74
|
+
codec: CBOR,
|
|
75
|
+
hasher: sha256
|
|
76
|
+
})
|
|
77
|
+
writer.write(b2)
|
|
78
|
+
|
|
79
|
+
writer.addRoot(b1.cid)
|
|
80
|
+
const bytes = writer.close()
|
|
81
|
+
|
|
82
|
+
const reader = await CarReader.fromBytes(bytes)
|
|
83
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid])
|
|
84
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }, { cid: b2.cid, bytes: b2.bytes }])
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('overestimate header', async () => {
|
|
88
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(2)
|
|
89
|
+
const dataSize = 256
|
|
90
|
+
const buffer = new ArrayBuffer(headerSize + dataSize)
|
|
91
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize })
|
|
92
|
+
const b1 = await Block.encode({
|
|
93
|
+
value: { hello: 'world' },
|
|
94
|
+
codec: CBOR,
|
|
95
|
+
hasher: sha256
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
writer.write(b1)
|
|
99
|
+
|
|
100
|
+
const b2 = await Block.encode({
|
|
101
|
+
value: { bye: 'world' },
|
|
102
|
+
codec: CBOR,
|
|
103
|
+
hasher: sha256
|
|
104
|
+
})
|
|
105
|
+
writer.write(b2)
|
|
106
|
+
|
|
107
|
+
writer.addRoot(b1.cid)
|
|
108
|
+
assert.throws(() => writer.close(), /Header size was overestimate/)
|
|
109
|
+
const bytes = writer.close({ resize: true })
|
|
110
|
+
|
|
111
|
+
const reader = await CarReader.fromBytes(bytes)
|
|
112
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid])
|
|
113
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }, { cid: b2.cid, bytes: b2.bytes }])
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('underestimate header', async () => {
|
|
117
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(2)
|
|
118
|
+
const dataSize = 300
|
|
119
|
+
const buffer = new ArrayBuffer(headerSize + dataSize)
|
|
120
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize })
|
|
121
|
+
const b1 = await Block.encode({
|
|
122
|
+
value: { hello: 'world' },
|
|
123
|
+
codec: CBOR,
|
|
124
|
+
hasher: sha256
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
writer.write(b1)
|
|
128
|
+
writer.addRoot(b1.cid)
|
|
129
|
+
|
|
130
|
+
const b2 = await Block.encode({
|
|
131
|
+
value: { bye: 'world' },
|
|
132
|
+
codec: CBOR,
|
|
133
|
+
hasher: sha512
|
|
134
|
+
})
|
|
135
|
+
writer.write(b2)
|
|
136
|
+
assert.throws(() => writer.addRoot(b2.cid), /has no capacity/)
|
|
137
|
+
writer.addRoot(b2.cid, { resize: true })
|
|
138
|
+
|
|
139
|
+
const bytes = writer.close()
|
|
140
|
+
|
|
141
|
+
const reader = await CarReader.fromBytes(bytes)
|
|
142
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid, b2.cid])
|
|
143
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }, { cid: b2.cid, bytes: b2.bytes }])
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('has no space for the root', async () => {
|
|
148
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(1)
|
|
149
|
+
const dataSize = 100
|
|
150
|
+
const buffer = new ArrayBuffer(headerSize + dataSize)
|
|
151
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize })
|
|
152
|
+
const b1 = await Block.encode({
|
|
153
|
+
value: { hello: 'world' },
|
|
154
|
+
codec: CBOR,
|
|
155
|
+
hasher: sha256
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
writer.write(b1)
|
|
159
|
+
writer.addRoot(b1.cid)
|
|
160
|
+
|
|
161
|
+
const b2 = await Block.encode({
|
|
162
|
+
value: { bye: 'world' },
|
|
163
|
+
codec: CBOR,
|
|
164
|
+
hasher: sha256
|
|
165
|
+
})
|
|
166
|
+
writer.write(b2)
|
|
167
|
+
assert.throws(() => writer.addRoot(b2.cid), /Buffer has no capacity for a new root/)
|
|
168
|
+
assert.throws(() => writer.addRoot(b2.cid, { resize: true }), /Buffer has no capacity for a new root/)
|
|
169
|
+
|
|
170
|
+
const bytes = writer.close()
|
|
171
|
+
|
|
172
|
+
const reader = await CarReader.fromBytes(bytes)
|
|
173
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid])
|
|
174
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }, { cid: b2.cid, bytes: b2.bytes }])
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('has no space for the block', async () => {
|
|
178
|
+
const headerSize = CarBufferWriter.estimateHeaderLength(1)
|
|
179
|
+
const dataSize = 58
|
|
180
|
+
const buffer = new ArrayBuffer(headerSize + dataSize)
|
|
181
|
+
const writer = CarBufferWriter.createWriter(buffer, { headerSize })
|
|
182
|
+
const b1 = await Block.encode({
|
|
183
|
+
value: { hello: 'world' },
|
|
184
|
+
codec: CBOR,
|
|
185
|
+
hasher: sha256
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
writer.write(b1)
|
|
189
|
+
writer.addRoot(b1.cid)
|
|
190
|
+
|
|
191
|
+
const b2 = await Block.encode({
|
|
192
|
+
value: { bye: 'world' },
|
|
193
|
+
codec: CBOR,
|
|
194
|
+
hasher: sha256
|
|
195
|
+
})
|
|
196
|
+
assert.throws(() => writer.write(b2), /Buffer has no capacity for this block/)
|
|
197
|
+
|
|
198
|
+
const bytes = writer.close()
|
|
199
|
+
|
|
200
|
+
const reader = await CarReader.fromBytes(bytes)
|
|
201
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid])
|
|
202
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }])
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('provide roots', async () => {
|
|
206
|
+
const b1 = await Block.encode({
|
|
207
|
+
value: { hello: 'world' },
|
|
208
|
+
codec: CBOR,
|
|
209
|
+
hasher: sha256
|
|
210
|
+
})
|
|
211
|
+
const b2 = await Block.encode({
|
|
212
|
+
value: { bye: 'world' },
|
|
213
|
+
codec: CBOR,
|
|
214
|
+
hasher: sha512
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
const buffer = new ArrayBuffer(300)
|
|
218
|
+
const writer = CarBufferWriter.createWriter(buffer, { roots: [b1.cid, b2.cid] })
|
|
219
|
+
|
|
220
|
+
writer.write(b1)
|
|
221
|
+
writer.write(b2)
|
|
222
|
+
|
|
223
|
+
const bytes = writer.close()
|
|
224
|
+
|
|
225
|
+
const reader = await CarReader.fromBytes(bytes)
|
|
226
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid, b2.cid])
|
|
227
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }, { cid: b2.cid, bytes: b2.bytes }])
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
it('provide large CID root', async () => {
|
|
231
|
+
const bytes = new Uint8Array(512).fill(1)
|
|
232
|
+
const b1 = await Block.encode({
|
|
233
|
+
value: { hello: 'world' },
|
|
234
|
+
codec: CBOR,
|
|
235
|
+
hasher: sha256
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
const b2 = {
|
|
239
|
+
cid: CID.createV1(Raw.code, identity.digest(bytes)),
|
|
240
|
+
bytes
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const headerSize = CBOR.encode({ version: 1, roots: [b1.cid, b2.cid] }).byteLength
|
|
244
|
+
const bodySize = CarBufferWriter.blockLength(b1) + CarBufferWriter.blockLength(b2)
|
|
245
|
+
const varintSize = varint.encodingLength(headerSize)
|
|
246
|
+
|
|
247
|
+
const writer = CarBufferWriter.createWriter(new ArrayBuffer(varintSize + headerSize + bodySize), { roots: [b1.cid, b2.cid] })
|
|
248
|
+
|
|
249
|
+
writer.write(b1)
|
|
250
|
+
writer.write(b2)
|
|
251
|
+
const car = writer.close()
|
|
252
|
+
const reader = await CarReader.fromBytes(car)
|
|
253
|
+
assert.deepEqual(await reader.getRoots(), [b1.cid, b2.cid])
|
|
254
|
+
assert.deepEqual(reader._blocks, [{ cid: b1.cid, bytes: b1.bytes }, { cid: b2.cid, bytes: b2.bytes }])
|
|
255
|
+
})
|
|
256
|
+
})
|
package/test/test-indexer.js
CHANGED
|
@@ -30,7 +30,7 @@ describe('CarIndexer fromBytes()', () => {
|
|
|
30
30
|
const indexer = await CarIndexer.fromBytes(goCarV2Bytes)
|
|
31
31
|
const roots = await indexer.getRoots()
|
|
32
32
|
assert.strictEqual(roots.length, 1)
|
|
33
|
-
assert(goCarV2Roots[0].equals(roots[0]))
|
|
33
|
+
assert.ok(goCarV2Roots[0].equals(roots[0]))
|
|
34
34
|
assert.strictEqual(indexer.version, 2)
|
|
35
35
|
|
|
36
36
|
const indexData = []
|
package/test/test-reader.js
CHANGED
|
@@ -65,13 +65,13 @@ describe('CarReader fromBytes()', () => {
|
|
|
65
65
|
const reader = await CarReader.fromBytes(goCarV2Bytes)
|
|
66
66
|
const roots = await reader.getRoots()
|
|
67
67
|
assert.strictEqual(roots.length, 1)
|
|
68
|
-
assert(goCarV2Roots[0].equals(roots[0]))
|
|
68
|
+
assert.ok(goCarV2Roots[0].equals(roots[0]))
|
|
69
69
|
assert.strictEqual(reader.version, 2)
|
|
70
70
|
for (const { cid } of goCarV2Index) {
|
|
71
71
|
const block = await reader.get(cid)
|
|
72
72
|
assert.isDefined(block)
|
|
73
73
|
if (block) {
|
|
74
|
-
assert(cid.equals(block.cid))
|
|
74
|
+
assert.ok(cid.equals(block.cid))
|
|
75
75
|
let content
|
|
76
76
|
if (cid.code === dagPb.code) {
|
|
77
77
|
content = dagPb.decode(block.bytes)
|
package/test/test-writer.js
CHANGED
|
@@ -265,9 +265,9 @@ describe('CarWriter', () => {
|
|
|
265
265
|
const pbBytes = await append(1)
|
|
266
266
|
const cborBytes = await append(2)
|
|
267
267
|
|
|
268
|
-
assert(rawBytes.length > 0)
|
|
269
|
-
assert(pbBytes.length > 0)
|
|
270
|
-
assert(cborBytes.length > 0)
|
|
268
|
+
assert.ok(rawBytes.length > 0)
|
|
269
|
+
assert.ok(pbBytes.length > 0)
|
|
270
|
+
assert.ok(cborBytes.length > 0)
|
|
271
271
|
|
|
272
272
|
const reassembled = concatBytes([headerBytes, rawBytes, pbBytes, cborBytes])
|
|
273
273
|
|
package/tsconfig.json
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"paths": {
|
|
32
32
|
"@ipld/car": [ "car.js", "car-browser.js", "lib/" ],
|
|
33
33
|
"@ipld/car/writer": [ "./lib/writer.js" ],
|
|
34
|
+
"@ipld/car/buffer-writer": ["./lib/buffer-writer.js"],
|
|
34
35
|
"@ipld/car/reader": [ "./lib/reader.js" ],
|
|
35
36
|
"@ipld/car/indexed-reader": [ "./lib/indexed-reader.js" ],
|
|
36
37
|
"@ipld/car/iterator": [ "./lib/iterator.js" ],
|
package/types/api.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CID } from 'multiformats/cid';
|
|
2
|
+
export type { CID };
|
|
2
3
|
export declare type Block = {
|
|
3
4
|
cid: CID;
|
|
4
5
|
bytes: Uint8Array;
|
|
@@ -30,6 +31,21 @@ export interface BlockWriter {
|
|
|
30
31
|
put(block: Block): Promise<void>;
|
|
31
32
|
close(): Promise<void>;
|
|
32
33
|
}
|
|
34
|
+
export interface CarBufferWriter {
|
|
35
|
+
addRoot(root: CID, options?: {
|
|
36
|
+
resize?: boolean;
|
|
37
|
+
}): CarBufferWriter;
|
|
38
|
+
write(block: Block): CarBufferWriter;
|
|
39
|
+
close(options?: {
|
|
40
|
+
resize?: boolean;
|
|
41
|
+
}): Uint8Array;
|
|
42
|
+
}
|
|
43
|
+
export interface CarBufferWriterOptions {
|
|
44
|
+
roots?: CID[];
|
|
45
|
+
byteOffset?: number;
|
|
46
|
+
byteLength?: number;
|
|
47
|
+
headerSize?: number;
|
|
48
|
+
}
|
|
33
49
|
export interface WriterChannel {
|
|
34
50
|
writer: BlockWriter;
|
|
35
51
|
out: AsyncIterable<Uint8Array>;
|
package/types/api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAEtC,YAAY,EAAE,GAAG,EAAE,CAAA;AAGnB,oBAAY,KAAK,GAAG;IAClB,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE,UAAU,CAAA;CAClB,CAAA;AAED,oBAAY,WAAW,GAAG;IACxB,GAAG,EAAE,GAAG,CAAA;IACR,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,oBAAY,UAAU,GAAG,WAAW,GAAG;IACrC,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;CAC3B;AAED,MAAM,WAAW,aAAc,SAAQ,aAAa,CAAC,KAAK,CAAC;CAAG;AAE9D,MAAM,WAAW,WAAY,SAAQ,aAAa,CAAC,GAAG,CAAC;CAAG;AAE1D,MAAM,WAAW,WAAW;IAC1B,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAC/B,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAAA;IACzC,MAAM,IAAI,aAAa,CAAA;IACvB,IAAI,IAAI,WAAW,CAAA;CACpB;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAChC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,IAAI,EAAC,GAAG,EAAE,OAAO,CAAC,EAAC;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GAAE,eAAe,CAAA;IAChE,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,eAAe,CAAA;IACpC,KAAK,CAAC,OAAO,CAAC,EAAC;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,UAAU,CAAA;CACjD;AAED,MAAM,WAAW,sBAAsB;IACrC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAA;IACb,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,WAAW,CAAA;IACnB,GAAG,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;CAC/B;AAED,MAAM,WAAW,SAAU,SAAQ,WAAW,EAAE,WAAW;CAAG"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export function addRoot(writer: CarBufferWriter, root: CID, { resize }?: {
|
|
2
|
+
resize?: boolean | undefined;
|
|
3
|
+
} | undefined): void;
|
|
4
|
+
export function blockLength({ cid, bytes }: Block): number;
|
|
5
|
+
export function addBlock(writer: CarBufferWriter, { cid, bytes }: Block): void;
|
|
6
|
+
export function close(writer: CarBufferWriter, { resize }?: {
|
|
7
|
+
resize?: boolean | undefined;
|
|
8
|
+
} | undefined): Uint8Array;
|
|
9
|
+
export function resizeHeader(writer: CarBufferWriter, byteLength: number): void;
|
|
10
|
+
export function calculateHeaderLength(rootLengths: number[]): number;
|
|
11
|
+
export function headerLength({ roots }: {
|
|
12
|
+
roots: CID[];
|
|
13
|
+
}): number;
|
|
14
|
+
export function estimateHeaderLength(rootCount: number, rootByteLength?: number | undefined): number;
|
|
15
|
+
export function createWriter(buffer: ArrayBuffer, { roots, byteOffset, byteLength, headerSize }?: {
|
|
16
|
+
roots?: import("multiformats/cid").CID[] | undefined;
|
|
17
|
+
byteOffset?: number | undefined;
|
|
18
|
+
byteLength?: number | undefined;
|
|
19
|
+
headerSize?: number | undefined;
|
|
20
|
+
} | undefined): CarBufferWriter;
|
|
21
|
+
export type CID = import('../api').CID;
|
|
22
|
+
export type Block = import('../api').Block;
|
|
23
|
+
export type Writer = import('../api').CarBufferWriter;
|
|
24
|
+
export type Options = import('../api').CarBufferWriterOptions;
|
|
25
|
+
export type CarEncoder = import('./coding').CarEncoder;
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {import('../api').CID} CID
|
|
28
|
+
* @typedef {import('../api').Block} Block
|
|
29
|
+
* @typedef {import('../api').CarBufferWriter} Writer
|
|
30
|
+
* @typedef {import('../api').CarBufferWriterOptions} Options
|
|
31
|
+
* @typedef {import('./coding').CarEncoder} CarEncoder
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* A simple CAR writer that writes to a pre-allocated buffer.
|
|
35
|
+
*
|
|
36
|
+
* @class
|
|
37
|
+
* @name CarBufferWriter
|
|
38
|
+
* @implements {Writer}
|
|
39
|
+
*/
|
|
40
|
+
declare class CarBufferWriter implements Writer {
|
|
41
|
+
/**
|
|
42
|
+
* @param {Uint8Array} bytes
|
|
43
|
+
* @param {number} headerSize
|
|
44
|
+
*/
|
|
45
|
+
constructor(bytes: Uint8Array, headerSize: number);
|
|
46
|
+
/** @readonly */
|
|
47
|
+
readonly bytes: Uint8Array;
|
|
48
|
+
byteOffset: number;
|
|
49
|
+
/**
|
|
50
|
+
* @readonly
|
|
51
|
+
* @type {CID[]}
|
|
52
|
+
*/
|
|
53
|
+
readonly roots: CID[];
|
|
54
|
+
headerSize: number;
|
|
55
|
+
/**
|
|
56
|
+
* Add a root to this writer, to be used to create a header when the CAR is
|
|
57
|
+
* finalized with {@link CarBufferWriter.close `close()`}
|
|
58
|
+
*
|
|
59
|
+
* @param {CID} root
|
|
60
|
+
* @param {{resize?:boolean}} [options]
|
|
61
|
+
* @returns {CarBufferWriter}
|
|
62
|
+
*/
|
|
63
|
+
addRoot(root: CID, options?: {
|
|
64
|
+
resize?: boolean | undefined;
|
|
65
|
+
} | undefined): CarBufferWriter;
|
|
66
|
+
/**
|
|
67
|
+
* Write a `Block` (a `{ cid:CID, bytes:Uint8Array }` pair) to the archive.
|
|
68
|
+
* Throws if there is not enough capacity.
|
|
69
|
+
*
|
|
70
|
+
* @param {Block} block A `{ cid:CID, bytes:Uint8Array }` pair.
|
|
71
|
+
* @returns {CarBufferWriter}
|
|
72
|
+
*/
|
|
73
|
+
write(block: Block): CarBufferWriter;
|
|
74
|
+
/**
|
|
75
|
+
* Finalize the CAR and return it as a `Uint8Array`.
|
|
76
|
+
*
|
|
77
|
+
* @param {object} [options]
|
|
78
|
+
* @param {boolean} [options.resize]
|
|
79
|
+
* @returns {Uint8Array}
|
|
80
|
+
*/
|
|
81
|
+
close(options?: {
|
|
82
|
+
resize?: boolean | undefined;
|
|
83
|
+
} | undefined): Uint8Array;
|
|
84
|
+
}
|
|
85
|
+
export {};
|
|
86
|
+
//# sourceMappingURL=buffer-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffer-writer.d.ts","sourceRoot":"","sources":["../../lib/buffer-writer.js"],"names":[],"mappings":"AAgFO,gCAJI,eAAe,QACf,GAAG;;qBA0Bb;AAUM,4CAHI,KAAK,GACH,MAAM,CAKlB;AAMM,iCAHI,eAAe,kBACf,KAAK,QAYf;AAOM,8BAJI,eAAe;;2BA2BzB;AAMM,qCAHI,eAAe,cACf,MAAM,QAShB;AAqCM,mDAHI,MAAM,EAAE,GACN,MAAM,CAWlB;AAUM;IAHmB,KAAK,EAApB,GAAG,EAAE;IACH,MAAM,CAG4C;AAYxD,gDAJI,MAAM,wCAEJ,MAAM,CAG+C;AAuB3D,qCARI,WAAW;;;;;gBAMT,eAAe,CAmB3B;kBAvRY,OAAO,QAAQ,EAAE,GAAG;oBACpB,OAAO,QAAQ,EAAE,KAAK;qBACtB,OAAO,QAAQ,EAAE,eAAe;sBAChC,OAAO,QAAQ,EAAE,sBAAsB;yBACvC,OAAO,UAAU,EAAE,UAAU;AAL1C;;;;;;GAMG;AAEH;;;;;;GAMG;AACH;IACE;;;OAGG;IACH,mBAHW,UAAU,cACV,MAAM,EAahB;IAVC,gBAAgB;IAChB,2BAAkB;IAClB,mBAA4B;IAE5B;;;OAGG;IACH,gBAFU,GAAG,EAAE,CAEA;IACf,mBAA4B;IAG9B;;;;;;;OAOG;IACH,cAJW,GAAG;;oBAED,eAAe,CAK3B;IAED;;;;;;OAMG;IACH,aAHW,KAAK,GACH,eAAe,CAK3B;IAED;;;;;;OAMG;IACH;;oBAFa,UAAU,CAItB;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-buffer-writer.d.ts","sourceRoot":"","sources":["../../test/test-buffer-writer.js"],"names":[],"mappings":""}
|