Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: destroy stream to prevent leaking file descriptors #1812

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading