Skip to content

Commit

Permalink
fix: try to improve error handling logic in unixfs streams
Browse files Browse the repository at this point in the history
right now errors in the `finalize` methods can leave a writer unclosed forever - this should help improve the DX issues described in storacha/console#81 (comment) by at least printing an error to the console with information about the problem
  • Loading branch information
travis committed Jan 17, 2024
1 parent 7f42f39 commit 7f9c925
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/upload-client/src/unixfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ export function createFileEncoderStream(blob) {
const unixfsWriter = UnixFS.createWriter({ writable, settings })
const fileBuilder = new UnixFSFileBuilder('', blob)
void (async () => {
await fileBuilder.finalize(unixfsWriter)
await unixfsWriter.close()
try {
await fileBuilder.finalize(unixfsWriter)
} catch (e) {
console.log("Error finalizing file builder: ", e)
} finally {
await unixfsWriter.close()
}
})()
return readable
}
Expand Down Expand Up @@ -147,11 +152,16 @@ export function createDirectoryEncoderStream(files, options) {
const { readable, writable } = new TransformStream({}, queuingStrategy)
const unixfsWriter = UnixFS.createWriter({ writable, settings })
void (async () => {
const link = await rootDir.finalize(unixfsWriter)
if (options?.onDirectoryEntryLink) {
options.onDirectoryEntryLink({ name: '', ...link })
try {
const link = await rootDir.finalize(unixfsWriter)
if (options?.onDirectoryEntryLink) {
options.onDirectoryEntryLink({ name: '', ...link })
}
} catch (e) {
console.log("Error finalizing directory builder:", e)
} finally {
await unixfsWriter.close()
}
await unixfsWriter.close()
})()

return readable
Expand Down

0 comments on commit 7f9c925

Please sign in to comment.