Skip to content

Commit

Permalink
chore: allow access to full header in CarReader
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Mar 4, 2022
1 parent 12480b5 commit b95a6e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
5 changes: 1 addition & 4 deletions examples/car-to-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ async function run () {
const indexer = await CarIndexer.fromBytes(bytes)
const reader = await CarReader.fromBytes(bytes)
const fixture = {
header: {
roots: await reader.getRoots(),
version: reader.version
},
header: reader._header, // a little naughty but we need gory details
blocks: []
}
let i = 0
Expand Down
20 changes: 10 additions & 10 deletions lib/reader-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { asyncIterableReader, bytesReader, createDecoder } from './decoder.js'
* @typedef {import('../api').Block} Block
* @typedef {import('../api').CarReader} CarReaderIface
* @typedef {import('./coding').BytesReader} BytesReader
* @typedef {import('./coding').CarHeader} CarHeader
* @typedef {import('./coding').CarV2Header} CarV2Header
*/

/**
Expand All @@ -26,18 +28,16 @@ import { asyncIterableReader, bytesReader, createDecoder } from './decoder.js'
* @class
* @implements {CarReaderIface}
* @property {number} version The version number of the CAR referenced by this
* reader (should be `1`).
* reader (should be `1` or `2`).
*/
export class CarReader {
/**
* @constructs CarReader
* @param {number} version
* @param {CID[]} roots
* @param {CarHeader|CarV2Header} header
* @param {Block[]} blocks
*/
constructor (version, roots, blocks) {
this._version = version
this._roots = roots
constructor (header, blocks) {
this._header = header
this._blocks = blocks
this._keys = blocks.map((b) => b.cid.toString())
}
Expand All @@ -48,7 +48,7 @@ export class CarReader {
* @instance
*/
get version () {
return this._version
return this._header.version
}

/**
Expand All @@ -62,7 +62,7 @@ export class CarReader {
* @returns {Promise<CID[]>}
*/
async getRoots () {
return this._roots
return this._header.roots
/* c8 ignore next 2 */
// Node.js 12 c8 bug
}
Expand Down Expand Up @@ -192,13 +192,13 @@ export class CarReader {
*/
export async function decodeReaderComplete (reader) {
const decoder = createDecoder(reader)
const { version, roots } = await decoder.header()
const header = await decoder.header()
const blocks = []
for await (const block of decoder.blocks()) {
blocks.push(block)
}

return new CarReader(version, roots, blocks)
return new CarReader(header, blocks)
/* c8 ignore next 2 */
// Node.js 12 c8 bug
}
Expand Down

0 comments on commit b95a6e2

Please sign in to comment.