From b95a6e2b0d8559d63e3c63c11694e67636c65e5d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Fri, 18 Jun 2021 16:06:06 +1000 Subject: [PATCH] chore: allow access to full header in CarReader --- examples/car-to-fixture.js | 5 +---- lib/reader-browser.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/examples/car-to-fixture.js b/examples/car-to-fixture.js index 740b859..0556679 100755 --- a/examples/car-to-fixture.js +++ b/examples/car-to-fixture.js @@ -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 diff --git a/lib/reader-browser.js b/lib/reader-browser.js index d520922..471f551 100644 --- a/lib/reader-browser.js +++ b/lib/reader-browser.js @@ -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 */ /** @@ -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()) } @@ -48,7 +48,7 @@ export class CarReader { * @instance */ get version () { - return this._version + return this._header.version } /** @@ -62,7 +62,7 @@ export class CarReader { * @returns {Promise} */ async getRoots () { - return this._roots + return this._header.roots /* c8 ignore next 2 */ // Node.js 12 c8 bug } @@ -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 }