Skip to content

Commit 9367e53

Browse files
committed
refactor: optional-chain size-cap options instead of a {} default
Drops the options = {} default on the streaming decode/encode functions and guards each cap check with options?.x. Avoids allocating a throwaway empty object per readCid/readBlockHead call when no caps are passed, and the param type now matches the optional [options] annotation. No behavior change.
1 parent 6446250 commit 9367e53

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/decoder.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import { CarV1HeaderOrV2Pragma } from './header-validator.js'
2626
* @param {CarCodecOptions} [options]
2727
* @returns {Promise<CarHeader|CarV2Header>}
2828
*/
29-
export async function readHeader (reader, strictVersion, options = {}) {
29+
export async function readHeader (reader, strictVersion, options) {
3030
const length = decodeVarint(await reader.upTo(8), reader)
3131
if (length === 0) {
3232
throw new Error('Invalid CAR header (zero length)')
3333
}
34-
if (options.maxHeaderLength != null && length > options.maxHeaderLength) {
34+
if (options?.maxHeaderLength != null && length > options.maxHeaderLength) {
3535
throw new RangeError(`CAR header of length ${length} exceeds maxHeaderLength of ${options.maxHeaderLength}`)
3636
}
3737
const header = await reader.exactly(length, true)
@@ -64,7 +64,7 @@ export async function readHeader (reader, strictVersion, options = {}) {
6464
* @param {CarCodecOptions} [options]
6565
* @returns {Promise<CID>}
6666
*/
67-
async function readCid (reader, options = {}) {
67+
async function readCid (reader, options) {
6868
const cidStart = reader.pos
6969
const first = await reader.exactly(2, false)
7070
if (first[0] === CIDV0_BYTES.SHA2_256 && first[1] === CIDV0_BYTES.LENGTH) {
@@ -82,7 +82,7 @@ async function readCid (reader, options = {}) {
8282
const mhLength = getMultihashLength(await reader.upTo(8))
8383
// full CID = version + codec varints (already consumed) + multihash
8484
const cidLength = Number(reader.pos - cidStart) + mhLength
85-
if (options.maxCidLength != null && cidLength > options.maxCidLength) {
85+
if (options?.maxCidLength != null && cidLength > options.maxCidLength) {
8686
throw new RangeError(`CID of length ${cidLength} exceeds maxCidLength of ${options.maxCidLength}`)
8787
}
8888
const bytes = await reader.exactly(mhLength, true)
@@ -101,7 +101,7 @@ async function readCid (reader, options = {}) {
101101
* @param {CarCodecOptions} [options]
102102
* @returns {Promise<BlockHeader>}
103103
*/
104-
export async function readBlockHead (reader, options = {}) {
104+
export async function readBlockHead (reader, options) {
105105
// length includes a CID + Binary, where CID has a variable length
106106
// we have to deal with
107107
const start = reader.pos
@@ -112,7 +112,7 @@ export async function readBlockHead (reader, options = {}) {
112112
length += (reader.pos - start)
113113
const cid = await readCid(reader, options)
114114
const blockLength = length - Number(reader.pos - start) // subtract CID length
115-
if (options.maxBlockLength != null && blockLength > options.maxBlockLength) {
115+
if (options?.maxBlockLength != null && blockLength > options.maxBlockLength) {
116116
throw new RangeError(`CAR block of length ${blockLength} exceeds maxBlockLength of ${options.maxBlockLength}`)
117117
}
118118

@@ -124,7 +124,7 @@ export async function readBlockHead (reader, options = {}) {
124124
* @param {CarCodecOptions} [options]
125125
* @returns {Promise<Block>}
126126
*/
127-
async function readBlock (reader, options = {}) {
127+
async function readBlock (reader, options) {
128128
const { cid, blockLength } = await readBlockHead(reader, options)
129129
const bytes = await reader.exactly(blockLength, true)
130130
return { bytes, cid }
@@ -135,7 +135,7 @@ async function readBlock (reader, options = {}) {
135135
* @param {CarCodecOptions} [options]
136136
* @returns {Promise<BlockIndex>}
137137
*/
138-
async function readBlockIndex (reader, options = {}) {
138+
async function readBlockIndex (reader, options) {
139139
const offset = reader.pos
140140
const { cid, length, blockLength } = await readBlockHead(reader, options)
141141
const index = { cid, length, blockLength, offset, blockOffset: reader.pos }
@@ -153,7 +153,7 @@ async function readBlockIndex (reader, options = {}) {
153153
* @param {CarCodecOptions} [options]
154154
* @returns {CarDecoder}
155155
*/
156-
export function createDecoder (reader, options = {}) {
156+
export function createDecoder (reader, options) {
157157
const headerPromise = (async () => {
158158
const header = await readHeader(reader, undefined, options)
159159
if (header.version === 2) {

src/encoder.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const CAR_V1_VERSION = 1
1818
* @param {CarCodecOptions} [options]
1919
* @returns {Uint8Array}
2020
*/
21-
export function createHeader (roots, options = {}) {
21+
export function createHeader (roots, options) {
2222
const headerBytes = dagCborEncode({ version: CAR_V1_VERSION, roots })
23-
if (options.maxHeaderLength != null && headerBytes.length > options.maxHeaderLength) {
23+
if (options?.maxHeaderLength != null && headerBytes.length > options.maxHeaderLength) {
2424
throw new RangeError(`CAR header of length ${headerBytes.length} exceeds maxHeaderLength of ${options.maxHeaderLength}`)
2525
}
2626
const varintBytes = varint.encode(headerBytes.length)
@@ -35,7 +35,7 @@ export function createHeader (roots, options = {}) {
3535
* @param {CarCodecOptions} [options]
3636
* @returns {CarEncoder}
3737
*/
38-
function createEncoder (writer, options = {}) {
38+
function createEncoder (writer, options) {
3939
// none of this is wrapped in a mutex, that needs to happen above this to
4040
// avoid overwrites
4141

@@ -55,10 +55,10 @@ function createEncoder (writer, options = {}) {
5555
*/
5656
async writeBlock (block) {
5757
const { cid, bytes } = block
58-
if (cid.version === 1 && options.maxCidLength != null && cid.bytes.length > options.maxCidLength) {
58+
if (cid.version === 1 && options?.maxCidLength != null && cid.bytes.length > options.maxCidLength) {
5959
throw new RangeError(`CID of length ${cid.bytes.length} exceeds maxCidLength of ${options.maxCidLength}`)
6060
}
61-
if (options.maxBlockLength != null && bytes.length > options.maxBlockLength) {
61+
if (options?.maxBlockLength != null && bytes.length > options.maxBlockLength) {
6262
throw new RangeError(`CAR block of length ${bytes.length} exceeds maxBlockLength of ${options.maxBlockLength}`)
6363
}
6464
await writer.write(new Uint8Array(varint.encode(cid.bytes.length + bytes.length)))

0 commit comments

Comments
 (0)