Skip to content

Commit

Permalink
feat: add version method to CarWriter (#162)
Browse files Browse the repository at this point in the history
The `version()` method retrieves the version number from the
underlying encoder.

Fixes #161
  • Loading branch information
achingbrain authored Feb 22, 2024
1 parent d53ca08 commit d1ded7f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface BlockBufferReader {
export interface BlockWriter {
put(block: Block): Promise<void>
close(): Promise<void>
version(): number
}

export interface CarBufferWriter {
Expand Down
2 changes: 2 additions & 0 deletions src/coding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface CarEncoder {
writeBlock(block: Block): Promise<void>

close(): Promise<void>

version(): number
}

export interface IteratorChannel_Writer<T> {
Expand Down
11 changes: 10 additions & 1 deletion src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import varint from 'varint'
* @typedef {import('./coding').IteratorChannel_Writer<Uint8Array>} IteratorChannel_Writer
*/

const CAR_V1_VERSION = 1

/**
* Create a header from an array of roots.
*
* @param {CID[]} roots
* @returns {Uint8Array}
*/
export function createHeader (roots) {
const headerBytes = dagCborEncode({ version: 1, roots })
const headerBytes = dagCborEncode({ version: CAR_V1_VERSION, roots })
const varintBytes = varint.encode(headerBytes.length)
const header = new Uint8Array(varintBytes.length + headerBytes.length)
header.set(varintBytes, 0)
Expand Down Expand Up @@ -60,6 +62,13 @@ function createEncoder (writer) {
*/
async close () {
await writer.end()
},

/**
* @returns {number}
*/
version () {
return CAR_V1_VERSION
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/writer-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ export class CarWriter {
return this._encoder.close()
}

/**
* Returns the version number of the CAR file being written
*
* @returns {number}
*/
version () {
return this._encoder.version()
}

/**
* Create a new CAR writer "channel" which consists of a
* `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }` pair.
Expand Down
7 changes: 7 additions & 0 deletions test/test-writer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ describe('CarWriter', () => {
assert.strictEqual(toHex(bytes).substring(0, expectedStart.length), expectedStart)
})

it('version', async () => {
const { writer } = CarWriter.create(roots)

// v1 only
assert.equal(writer.version(), 1)
})

it('no roots', async () => {
const { writer, out } = CarWriter.create()
const collection = collector(out)
Expand Down

0 comments on commit d1ded7f

Please sign in to comment.