Skip to content

Commit

Permalink
chore: appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Sep 6, 2023
1 parent bc6f7f5 commit b76919a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
4 changes: 3 additions & 1 deletion packages/upload-client/src/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export function headerEncodingLength(root) {

/** @param {Block} block */
export function blockEncodingLength(block) {
const varintLength = varint.encodingLength(block.cid.bytes.length + block.bytes.length)
const varintLength = varint.encodingLength(
block.cid.bytes.length + block.bytes.length
)
const cidLength = block.cid.bytes.length
return varintLength + cidLength + block.bytes.length
}
Expand Down
8 changes: 6 additions & 2 deletions packages/upload-client/src/sharding.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export class ShardingStream extends TransformStream {
throw new Error(`block exceeds shard size: ${block.cid}`)
}

if (shard.length && headerEncodingLength() + shardBlockLength + blockLength > shardSize) {
if (
shard.length &&
headerEncodingLength() + shardBlockLength + blockLength > shardSize
) {
readyShard = shard
shard = []
shardBlockLength = 0
Expand All @@ -64,7 +67,8 @@ export class ShardingStream extends TransformStream {
let lastShardBlockLength = 0
while (lastShardBlockLength < overage) {
// need at least 1 block in original shard
if (shard.length < 2) throw new Error(`block exceeds shard size: ${shard.at(-1)?.cid}`)
if (shard.length < 2)
throw new Error(`block exceeds shard size: ${shard.at(-1)?.cid}`)
const block = shard[shard.length - 1]
shard.pop()
lastShard.unshift(block)
Expand Down
14 changes: 10 additions & 4 deletions packages/upload-client/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { randomBlock, randomBytes } from './helpers/random.js'
import { toCAR } from './helpers/car.js'
import { File } from './helpers/shims.js'
import { mockService } from './helpers/mocks.js'
import { blockEncodingLength, encode, headerEncodingLength } from '../src/car.js'
import {
blockEncodingLength,
encode,
headerEncodingLength,
} from '../src/car.js'

describe('uploadFile', () => {
it('uploads a file to the service', async () => {
Expand Down Expand Up @@ -372,9 +376,11 @@ describe('uploadCAR', () => {
const car = await encode(blocks, blocks.at(-1)?.cid)
// Wanted: 2 shards
// 2 * CAR header (34) + 2 * blocks (256), 2 * block encoding prefix (78)
const shardSize = (headerEncodingLength() * 2) + blocks
.slice(0, -1)
.reduce((size, block) => size + blockEncodingLength(block), 0)
const shardSize =
headerEncodingLength() * 2 +
blocks
.slice(0, -1)
.reduce((size, block) => size + blockEncodingLength(block), 0)

/** @type {import('../src/types').CARLink[]} */
const carCIDs = []
Expand Down
78 changes: 50 additions & 28 deletions packages/upload-client/test/sharding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,31 @@ describe('ShardingStream', () => {

it('fails to shard block that exceeds shard size when encoded', async () => {
const file = new Blob([await randomBytes(128)])
await assert.rejects(() => createFileEncoderStream(file)
.pipeThrough(new ShardingStream({ shardSize: 64 }))
.pipeTo(new WritableStream()), /block exceeds shard size/)
await assert.rejects(
() =>
createFileEncoderStream(file)
.pipeThrough(new ShardingStream({ shardSize: 64 }))
.pipeTo(new WritableStream()),
/block exceeds shard size/
)
})

it('reduces final shard to accomodate CAR header with root CID', async () => {
const blocks = [
await randomBlock(128), // encoded block length = 166
await randomBlock(64), // encoded block length = 102
await randomBlock(32) // encoded block length = 70
await randomBlock(64), // encoded block length = 102
await randomBlock(32), // encoded block length = 70
]

/** @type {import('../src/types').CARFile[]} */
const shards = []
await new ReadableStream({
pull (controller) {
const block = blocks.shift()
if (!block) return controller.close()
controller.enqueue(block)
}
})
pull(controller) {
const block = blocks.shift()
if (!block) return controller.close()
controller.enqueue(block)
},
})
// shard with no roots = encoded block (166) + CAR header (17) = 183
// shard with no roots = encoded block (102) + CAR header (17) = 119
// shard with 1 root = encoded block (70) + CAR header (17) = 87
Expand All @@ -93,38 +97,56 @@ describe('ShardingStream', () => {
// will actually exceed the shard size. It must then be refactored into
// 2 shards.
.pipeThrough(new ShardingStream({ shardSize: 206 }))
.pipeTo(new WritableStream({ write: s => { shards.push(s) } }))
.pipeTo(
new WritableStream({
write: (s) => {
shards.push(s)
},
})
)

assert.equal(shards.length, 3)
})

it('fails to shard block that exceeds shard size when encoded with root CID', async () => {
const blocks = [
await randomBlock(128) // encoded block length = 166
await randomBlock(128), // encoded block length = 166
]

await assert.rejects(() => {
return new ReadableStream({
pull (controller) {
const block = blocks.shift()
if (!block) return controller.close()
controller.enqueue(block)
}
})
// shard with no roots = encoded block (166) + CAR header (17) = 183
// shard with 1 root = encoded block (166) + CAR header (59) = 225
// i.e. shard size of 183 should allow us 1 shard with no roots and then
// we'll fail to create a shard with 1 root.
.pipeThrough(new ShardingStream({ shardSize: 183 }))
.pipeTo(new WritableStream())
return (
new ReadableStream({
pull(controller) {
const block = blocks.shift()
if (!block) return controller.close()
controller.enqueue(block)
},
})
// shard with no roots = encoded block (166) + CAR header (17) = 183
// shard with 1 root = encoded block (166) + CAR header (59) = 225
// i.e. shard size of 183 should allow us 1 shard with no roots and then
// we'll fail to create a shard with 1 root.
.pipeThrough(new ShardingStream({ shardSize: 183 }))
.pipeTo(new WritableStream())
)
}, /block exceeds shard size/)
})

it('no blocks no shards', async () => {
let shards = 0
await new ReadableStream({ pull: controller => { controller.close() } })
await new ReadableStream({
pull: (controller) => {
controller.close()
},
})
.pipeThrough(new ShardingStream({ shardSize: 206 }))
.pipeTo(new WritableStream({ write: () => { shards++ } }))
.pipeTo(
new WritableStream({
write: () => {
shards++
},
})
)
assert.equal(shards, 0)
})
})
Expand Down

0 comments on commit b76919a

Please sign in to comment.