Skip to content

Commit

Permalink
chore: remove error test
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Feb 21, 2024
1 parent aedd315 commit 216ca40
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 49 deletions.
4 changes: 1 addition & 3 deletions packages/car/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
"interface-blockstore": "^5.2.9",
"it-drain": "^3.0.5",
"it-map": "^3.0.5",
"it-merge": "^3.0.3",
"multiformats": "^13.0.1",
"p-defer": "^4.0.0",
"p-queue": "^8.0.1",
Expand All @@ -157,7 +156,6 @@
"aegir": "^42.2.2",
"blockstore-core": "^4.4.0",
"ipfs-unixfs-importer": "^15.2.4",
"it-to-buffer": "^4.0.5",
"sinon": "^17.0.1"
"it-to-buffer": "^4.0.5"
}
}
32 changes: 5 additions & 27 deletions packages/car/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import { CarWriter } from '@ipld/car'
import drain from 'it-drain'
import map from 'it-map'
import merge from 'it-merge'
import defer from 'p-defer'
import PQueue from 'p-queue'
import type { DAGWalker } from '@helia/interface'
Expand Down Expand Up @@ -193,49 +192,28 @@ class DefaultCar implements Car {
await writer.put({ cid, bytes })
}, options)
})
.catch(() => {})
}

let exportError: Error | undefined

// wait for the writer to end
try {
await deferred.promise
} finally {
await writer.close()
}

if (exportError != null) {
throw exportError
}
}

async * stream (root: CID | CID[], options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>): AsyncGenerator<Uint8Array, void, undefined> {
const { writer, out } = CarWriter.create(root)
const deferred = defer<Error | undefined>()

// has to be done async so we write to `writer` and read from `out` at the
// same time
this.export(root, writer, options)
.then(() => {
deferred.resolve()
})
.catch((err) => {
deferred.reject(err)
})
.catch(() => {})

yield * merge(
(async function * () {
// out is AsyncIterable<Uint8Array> not AsyncIterator<Uint8Array> so we
// can't just `yield * out`
for await (const buf of out) {
yield buf
}
})(),
// eslint-disable-next-line require-yield
(async function * () {
await deferred.promise
})()
)
for await (const buf of out) {
yield buf
}
}

/**
Expand Down
20 changes: 1 addition & 19 deletions packages/car/test/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { type UnixFS, unixfs } from '@helia/unixfs'
import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import toBuffer from 'it-to-buffer'
import Sinon from 'sinon'
import { car, type Car } from '../src/index.js'
import { dagWalkers } from './fixtures/dag-walkers.js'
import { largeFile, smallFile } from './fixtures/files.js'
import { smallFile } from './fixtures/files.js'
import { memoryCarWriter } from './fixtures/memory-car.js'
import type { Blockstore } from 'interface-blockstore'

Expand Down Expand Up @@ -35,21 +34,4 @@ describe('stream car file', () => {

expect(bytes).to.equalBytes(streamed)
})

it('errors when writing during streaming car file', async () => {
const exportSpy = Sinon.spy(c, 'export')
const cid = await u.addBytes(largeFile)
const iter = c.stream(cid)

// start stream moving so we can get at the CAR writer
await iter.next()

expect(exportSpy.called).to.be.true()

// make the next write error
const writer = exportSpy.getCall(0).args[1]
writer.put = async () => {
throw new Error('Urk!')
}
})
})

0 comments on commit 216ca40

Please sign in to comment.