Skip to content

Commit

Permalink
fix: destroy stream to prevent leaking file descriptors (#1812)
Browse files Browse the repository at this point in the history
* fix: destroy stream to prevent leaking file descriptors

* fix: destroy stream to prevent leaking file descriptors
  • Loading branch information
pentreathm authored Oct 1, 2024
1 parent 5d99c61 commit fe67a84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
120
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": [
"typescript"
Expand Down
37 changes: 21 additions & 16 deletions content/src/controller/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,26 @@ export function asEnumValue<T extends { [key: number]: string }>(

export async function createContentFileHeaders(content: ContentItem, hashId: string): Promise<Record<string, string>> {
const stream: Readable = await content.asRawStream()
const mime = await fromStream(stream)
const mimeType = mime?.mime || 'application/octet-stream'

const headers: Record<string, string> = {
'Content-Type': mimeType,
ETag: JSON.stringify(hashId), // by spec, the ETag must be a double-quoted string
'Access-Control-Expose-Headers': 'ETag',
'Cache-Control': 'public,max-age=31536000,s-maxage=31536000,immutable'
}
if (content.encoding) {
headers['Content-Encoding'] = content.encoding
}
if (content.size) {
headers['Content-Length'] = content.size.toString()
}
try {
const mime = await fromStream(stream)
const mimeType = mime?.mime || 'application/octet-stream'
stream.destroy()

return headers
const headers: Record<string, string> = {
'Content-Type': mimeType,
ETag: JSON.stringify(hashId), // by spec, the ETag must be a double-quoted string
'Access-Control-Expose-Headers': 'ETag',
'Cache-Control': 'public,max-age=31536000,s-maxage=31536000,immutable'
}
if (content.encoding) {
headers['Content-Encoding'] = content.encoding
}
if (content.size) {
headers['Content-Length'] = content.size.toString()
}
return headers
} catch (error) {
stream.destroy()
throw error
}
}

0 comments on commit fe67a84

Please sign in to comment.