From e8278ffba97ca832782f22310e53227361eaa63e Mon Sep 17 00:00:00 2001 From: Matias Pentreath Date: Tue, 1 Oct 2024 14:54:41 -0300 Subject: [PATCH 1/2] fix: destroy stream to prevent leaking file descriptors --- .vscode/settings.json | 2 +- content/src/controller/utils.ts | 35 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8b86f1400..8d843355d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,7 +17,7 @@ 120 ], "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "eslint.validate": [ "typescript" diff --git a/content/src/controller/utils.ts b/content/src/controller/utils.ts index 363a2e36d..6756cf588 100644 --- a/content/src/controller/utils.ts +++ b/content/src/controller/utils.ts @@ -41,21 +41,26 @@ export function asEnumValue( export async function createContentFileHeaders(content: ContentItem, hashId: string): Promise> { const stream: Readable = await content.asRawStream() - const mime = await fromStream(stream) - const mimeType = mime?.mime || 'application/octet-stream' - - const headers: Record = { - '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() + const headers: Record = { + '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() + } + } catch (error) { + stream.destroy() + throw error + } return headers } From f815f1291f6f425c9221ba3ad53925c637d87bfe Mon Sep 17 00:00:00 2001 From: Matias Pentreath Date: Tue, 1 Oct 2024 14:57:56 -0300 Subject: [PATCH 2/2] fix: destroy stream to prevent leaking file descriptors --- content/src/controller/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/src/controller/utils.ts b/content/src/controller/utils.ts index 6756cf588..367b9bec6 100644 --- a/content/src/controller/utils.ts +++ b/content/src/controller/utils.ts @@ -58,9 +58,9 @@ export async function createContentFileHeaders(content: ContentItem, hashId: str if (content.size) { headers['Content-Length'] = content.size.toString() } + return headers } catch (error) { stream.destroy() throw error } - return headers }