Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version method to CarWriter #162

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading