File tree Expand file tree Collapse file tree 5 files changed +29
-1
lines changed Expand file tree Collapse file tree 5 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ export interface BlockBufferReader {
55
55
export interface BlockWriter {
56
56
put ( block : Block ) : Promise < void >
57
57
close ( ) : Promise < void >
58
+ version ( ) : number
58
59
}
59
60
60
61
export interface CarBufferWriter {
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ export interface CarEncoder {
7
7
writeBlock ( block : Block ) : Promise < void >
8
8
9
9
close ( ) : Promise < void >
10
+
11
+ version ( ) : number
10
12
}
11
13
12
14
export interface IteratorChannel_Writer < T > {
Original file line number Diff line number Diff line change @@ -8,14 +8,16 @@ import varint from 'varint'
8
8
* @typedef {import('./coding').IteratorChannel_Writer<Uint8Array> } IteratorChannel_Writer
9
9
*/
10
10
11
+ const CAR_V1_VERSION = 1
12
+
11
13
/**
12
14
* Create a header from an array of roots.
13
15
*
14
16
* @param {CID[] } roots
15
17
* @returns {Uint8Array }
16
18
*/
17
19
export function createHeader ( roots ) {
18
- const headerBytes = dagCborEncode ( { version : 1 , roots } )
20
+ const headerBytes = dagCborEncode ( { version : CAR_V1_VERSION , roots } )
19
21
const varintBytes = varint . encode ( headerBytes . length )
20
22
const header = new Uint8Array ( varintBytes . length + headerBytes . length )
21
23
header . set ( varintBytes , 0 )
@@ -60,6 +62,13 @@ function createEncoder (writer) {
60
62
*/
61
63
async close ( ) {
62
64
await writer . end ( )
65
+ } ,
66
+
67
+ /**
68
+ * @returns {number }
69
+ */
70
+ version ( ) {
71
+ return CAR_V1_VERSION
63
72
}
64
73
}
65
74
}
Original file line number Diff line number Diff line change @@ -104,6 +104,15 @@ export class CarWriter {
104
104
return this . _encoder . close ( )
105
105
}
106
106
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
+
107
116
/**
108
117
* Create a new CAR writer "channel" which consists of a
109
118
* `{ writer:CarWriter, out:AsyncIterable<Uint8Array> }` pair.
Original file line number Diff line number Diff line change @@ -224,6 +224,13 @@ describe('CarWriter', () => {
224
224
assert . strictEqual ( toHex ( bytes ) . substring ( 0 , expectedStart . length ) , expectedStart )
225
225
} )
226
226
227
+ it ( 'version' , async ( ) => {
228
+ const { writer } = CarWriter . create ( roots )
229
+
230
+ // v1 only
231
+ assert . equal ( writer . version ( ) , 1 )
232
+ } )
233
+
227
234
it ( 'no roots' , async ( ) => {
228
235
const { writer, out } = CarWriter . create ( )
229
236
const collection = collector ( out )
You can’t perform that action at this time.
0 commit comments