Skip to content

Commit d1ded7f

Browse files
authored
feat: add version method to CarWriter (#162)
The `version()` method retrieves the version number from the underlying encoder. Fixes #161
1 parent d53ca08 commit d1ded7f

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface BlockBufferReader {
5555
export interface BlockWriter {
5656
put(block: Block): Promise<void>
5757
close(): Promise<void>
58+
version(): number
5859
}
5960

6061
export interface CarBufferWriter {

src/coding.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export interface CarEncoder {
77
writeBlock(block: Block): Promise<void>
88

99
close(): Promise<void>
10+
11+
version(): number
1012
}
1113

1214
export interface IteratorChannel_Writer<T> {

src/encoder.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import varint from 'varint'
88
* @typedef {import('./coding').IteratorChannel_Writer<Uint8Array>} IteratorChannel_Writer
99
*/
1010

11+
const CAR_V1_VERSION = 1
12+
1113
/**
1214
* Create a header from an array of roots.
1315
*
1416
* @param {CID[]} roots
1517
* @returns {Uint8Array}
1618
*/
1719
export function createHeader (roots) {
18-
const headerBytes = dagCborEncode({ version: 1, roots })
20+
const headerBytes = dagCborEncode({ version: CAR_V1_VERSION, roots })
1921
const varintBytes = varint.encode(headerBytes.length)
2022
const header = new Uint8Array(varintBytes.length + headerBytes.length)
2123
header.set(varintBytes, 0)
@@ -60,6 +62,13 @@ function createEncoder (writer) {
6062
*/
6163
async close () {
6264
await writer.end()
65+
},
66+
67+
/**
68+
* @returns {number}
69+
*/
70+
version () {
71+
return CAR_V1_VERSION
6372
}
6473
}
6574
}

src/writer-browser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ export class CarWriter {
104104
return this._encoder.close()
105105
}
106106

107+
/**
108+
* Returns the version number of the CAR file being written
109+
*
110+
* @returns {number}
111+
*/
112+
version () {
113+
return this._encoder.version()
114+
}
115+
107116
/**
108117
* Create a new CAR writer "channel" which consists of a
109118
* `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }` pair.

test/test-writer.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ describe('CarWriter', () => {
224224
assert.strictEqual(toHex(bytes).substring(0, expectedStart.length), expectedStart)
225225
})
226226

227+
it('version', async () => {
228+
const { writer } = CarWriter.create(roots)
229+
230+
// v1 only
231+
assert.equal(writer.version(), 1)
232+
})
233+
227234
it('no roots', async () => {
228235
const { writer, out } = CarWriter.create()
229236
const collection = collector(out)

0 commit comments

Comments
 (0)