Skip to content

Commit c360d39

Browse files
authored
refactor: implement safer get chunk data (#416)
1 parent 595be20 commit c360d39

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

backend/src/useCases/objects/files.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const retrieveAndReassembleFile = async (
143143
if (metadata.totalChunks === 1) {
144144
const chunkData = await NodesUseCases.getChunkData(metadata.chunks[0].cid)
145145
if (!chunkData) {
146-
throw new Error('Chunk not found')
146+
throw new Error(`Chunk not found: cid=${metadata.chunks[0].cid}`)
147147
}
148148

149149
return Readable.from(chunkData)
@@ -167,7 +167,7 @@ const retrieveAndReassembleFile = async (
167167
)
168168

169169
if (chunkedData.some((e) => e === undefined)) {
170-
this.destroy(new Error('Chunk not found'))
170+
this.destroy(new Error(`Chunk not found: cid=${chunks[0].cid}`))
171171
return
172172
}
173173

backend/src/useCases/objects/nodes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ const getChunkData = async (cid: string | CID): Promise<Buffer | undefined> => {
6969

7070
let ipldNodeBytes: Buffer | undefined = await nodesRepository
7171
.getNode(cidString)
72-
.then((e) => (e ? Buffer.from(e.encoded_node, 'base64') : undefined))
72+
.then((e) => {
73+
if (!e || !e.encoded_node) {
74+
return undefined
75+
}
76+
77+
return Buffer.from(e.encoded_node, 'base64')
78+
})
7379

7480
if (!ipldNodeBytes) {
7581
ipldNodeBytes = await BlockstoreUseCases.getNode(cidString)

0 commit comments

Comments
 (0)