-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecoder.js
347 lines (316 loc) · 9.7 KB
/
decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { decode as decodeDagCbor } from '@ipld/dag-cbor'
import { CID } from 'multiformats/cid'
import * as Digest from 'multiformats/hashes/digest'
import { CIDV0_BYTES, decodeV2Header, decodeVarint, getMultihashLength, V2_HEADER_LENGTH } from './decoder-common.js'
import { CarV1HeaderOrV2Pragma } from './header-validator.js'
/**
* @typedef {import('./api').Block} Block
* @typedef {import('./api').BlockHeader} BlockHeader
* @typedef {import('./api').BlockIndex} BlockIndex
* @typedef {import('./coding').BytesReader} BytesReader
* @typedef {import('./coding').CarHeader} CarHeader
* @typedef {import('./coding').CarV2Header} CarV2Header
* @typedef {import('./coding').CarV2FixedHeader} CarV2FixedHeader
* @typedef {import('./coding').CarDecoder} CarDecoder
*/
/**
* Reads header data from a `BytesReader`. The header may either be in the form
* of a `CarHeader` or `CarV2Header` depending on the CAR being read.
*
* @name async decoder.readHeader(reader)
* @param {BytesReader} reader
* @param {number} [strictVersion]
* @returns {Promise<CarHeader|CarV2Header>}
*/
export async function readHeader (reader, strictVersion) {
const length = decodeVarint(await reader.upTo(8), reader)
if (length === 0) {
throw new Error('Invalid CAR header (zero length)')
}
const header = await reader.exactly(length, true)
const block = decodeDagCbor(header)
if (CarV1HeaderOrV2Pragma.toTyped(block) === undefined) {
throw new Error('Invalid CAR header format')
}
if ((block.version !== 1 && block.version !== 2) || (strictVersion !== undefined && block.version !== strictVersion)) {
throw new Error(`Invalid CAR version: ${block.version}${strictVersion !== undefined ? ` (expected ${strictVersion})` : ''}`)
}
if (block.version === 1) {
// CarV1HeaderOrV2Pragma makes roots optional, let's make it mandatory
if (!Array.isArray(block.roots)) {
throw new Error('Invalid CAR header format')
}
return block
}
// version 2
if (block.roots !== undefined) {
throw new Error('Invalid CAR header format')
}
const v2Header = decodeV2Header(await reader.exactly(V2_HEADER_LENGTH, true))
reader.seek(v2Header.dataOffset - reader.pos)
const v1Header = await readHeader(reader, 1)
return Object.assign(v1Header, v2Header)
}
/**
* @param {BytesReader} reader
* @returns {Promise<CID>}
*/
async function readCid (reader) {
const first = await reader.exactly(2, false)
if (first[0] === CIDV0_BYTES.SHA2_256 && first[1] === CIDV0_BYTES.LENGTH) {
// cidv0 32-byte sha2-256
const bytes = await reader.exactly(34, true)
const multihash = Digest.decode(bytes)
return CID.create(0, CIDV0_BYTES.DAG_PB, multihash)
}
const version = decodeVarint(await reader.upTo(8), reader)
if (version !== 1) {
throw new Error(`Unexpected CID version (${version})`)
}
const codec = decodeVarint(await reader.upTo(8), reader)
const bytes = await reader.exactly(getMultihashLength(await reader.upTo(8)), true)
const multihash = Digest.decode(bytes)
return CID.create(version, codec, multihash)
}
/**
* Reads the leading data of an individual block from CAR data from a
* `BytesReader`. Returns a `BlockHeader` object which contains
* `{ cid, length, blockLength }` which can be used to either index the block
* or read the block binary data.
*
* @name async decoder.readBlockHead(reader)
* @param {BytesReader} reader
* @returns {Promise<BlockHeader>}
*/
export async function readBlockHead (reader) {
// length includes a CID + Binary, where CID has a variable length
// we have to deal with
const start = reader.pos
let length = decodeVarint(await reader.upTo(8), reader)
if (length === 0) {
throw new Error('Invalid CAR section (zero length)')
}
length += (reader.pos - start)
const cid = await readCid(reader)
const blockLength = length - Number(reader.pos - start) // subtract CID length
return { cid, length, blockLength }
}
/**
* @param {BytesReader} reader
* @returns {Promise<Block>}
*/
async function readBlock (reader) {
const { cid, blockLength } = await readBlockHead(reader)
const bytes = await reader.exactly(blockLength, true)
return { bytes, cid }
}
/**
* @param {BytesReader} reader
* @returns {Promise<BlockIndex>}
*/
async function readBlockIndex (reader) {
const offset = reader.pos
const { cid, length, blockLength } = await readBlockHead(reader)
const index = { cid, length, blockLength, offset, blockOffset: reader.pos }
reader.seek(index.blockLength)
return index
}
/**
* Creates a `CarDecoder` from a `BytesReader`. The `CarDecoder` is as async
* interface that will consume the bytes from the `BytesReader` to yield a
* `header()` and either `blocks()` or `blocksIndex()` data.
*
* @name decoder.createDecoder(reader)
* @param {BytesReader} reader
* @returns {CarDecoder}
*/
export function createDecoder (reader) {
const headerPromise = (async () => {
const header = await readHeader(reader)
if (header.version === 2) {
const v1length = reader.pos - header.dataOffset
reader = limitReader(reader, header.dataSize - v1length)
}
return header
})()
return {
header: () => headerPromise,
async * blocks () {
await headerPromise
while ((await reader.upTo(8)).length > 0) {
yield await readBlock(reader)
}
},
async * blocksIndex () {
await headerPromise
while ((await reader.upTo(8)).length > 0) {
yield await readBlockIndex(reader)
}
}
}
}
/**
* Creates a `BytesReader` from a `Uint8Array`.
*
* @name decoder.bytesReader(bytes)
* @param {Uint8Array} bytes
* @returns {BytesReader}
*/
export function bytesReader (bytes) {
let pos = 0
/** @type {BytesReader} */
return {
async upTo (length) {
const out = bytes.subarray(pos, pos + Math.min(length, bytes.length - pos))
return out
},
async exactly (length, seek = false) {
if (length > bytes.length - pos) {
throw new Error('Unexpected end of data')
}
const out = bytes.subarray(pos, pos + length)
if (seek) {
pos += length
}
return out
},
seek (length) {
pos += length
},
get pos () {
return pos
}
}
}
/**
* @ignore
* reusable reader for streams and files, we just need a way to read an
* additional chunk (of some undetermined size) and a way to close the
* reader when finished
* @param {() => Promise<Uint8Array|null>} readChunk
* @returns {BytesReader}
*/
export function chunkReader (readChunk /*, closer */) {
let pos = 0
let have = 0
let offset = 0
let currentChunk = new Uint8Array(0)
const read = async (/** @type {number} */ length) => {
have = currentChunk.length - offset
const bufa = [currentChunk.subarray(offset)]
while (have < length) {
const chunk = await readChunk()
if (chunk == null) {
break
}
/* c8 ignore next 8 */
// undo this ignore ^ when we have a fd implementation that can seek()
if (have < 0) { // because of a seek()
/* c8 ignore next 4 */
// toohard to test the else
if (chunk.length > have) {
bufa.push(chunk.subarray(-have))
} // else discard
} else {
bufa.push(chunk)
}
have += chunk.length
}
currentChunk = new Uint8Array(bufa.reduce((p, c) => p + c.length, 0))
let off = 0
for (const b of bufa) {
currentChunk.set(b, off)
off += b.length
}
offset = 0
}
/** @type {BytesReader} */
return {
async upTo (length) {
if (currentChunk.length - offset < length) {
await read(length)
}
return currentChunk.subarray(offset, offset + Math.min(currentChunk.length - offset, length))
},
async exactly (length, seek = false) {
if (currentChunk.length - offset < length) {
await read(length)
}
if (currentChunk.length - offset < length) {
throw new Error('Unexpected end of data')
}
const out = currentChunk.subarray(offset, offset + length)
if (seek) {
pos += length
offset += length
}
return out
},
seek (length) {
pos += length
offset += length
},
get pos () {
return pos
}
}
}
/**
* Creates a `BytesReader` from an `AsyncIterable<Uint8Array>`, which allows for
* consumption of CAR data from a streaming source.
*
* @name decoder.asyncIterableReader(asyncIterable)
* @param {AsyncIterable<Uint8Array>} asyncIterable
* @returns {BytesReader}
*/
export function asyncIterableReader (asyncIterable) {
const iterator = asyncIterable[Symbol.asyncIterator]()
async function readChunk () {
const next = await iterator.next()
if (next.done) {
return null
}
return next.value
}
return chunkReader(readChunk)
}
/**
* Wraps a `BytesReader` in a limiting `BytesReader` which limits maximum read
* to `byteLimit` bytes. It _does not_ update `pos` of the original
* `BytesReader`.
*
* @name decoder.limitReader(reader, byteLimit)
* @param {BytesReader} reader
* @param {number} byteLimit
* @returns {BytesReader}
*/
export function limitReader (reader, byteLimit) {
let bytesRead = 0
/** @type {BytesReader} */
return {
async upTo (length) {
let bytes = await reader.upTo(length)
if (bytes.length + bytesRead > byteLimit) {
bytes = bytes.subarray(0, byteLimit - bytesRead)
}
return bytes
},
async exactly (length, seek = false) {
const bytes = await reader.exactly(length, seek)
if (bytes.length + bytesRead > byteLimit) {
throw new Error('Unexpected end of data')
}
if (seek) {
bytesRead += length
}
return bytes
},
seek (length) {
bytesRead += length
reader.seek(length)
},
get pos () {
return reader.pos
}
}
}