harden code to avoid possible memory leak
Massimo Melina committed
Feb 24, 2026 at 00:26 UTC
314e8674a834bfc64a4e973437d00f6ae99815c7
3 files changed
+7
-3
src/serveFile.ts
+1
-1
@@ -7,7 +7,7 @@ import { HTTP_BAD_REQUEST, HTTP_FORBIDDEN, HTTP_METHOD_NOT_ALLOWED, HTTP_NO_CONT
7
import { getNodeName, VfsNode } from './vfs'
8
import mimetypes from 'mime-types'
9
import { defineConfig } from './config'
10
-import { CFG, Dict, makeMatcher, matches, throw_, try_, with_, xlate } from './misc'
10
+import { CFG, Dict, makeMatcher, matches, try_, with_ } from './misc'
11
import _ from 'lodash'
12
import { basename } from 'path'
13
import { promisify } from 'util'
src/upload.ts
+5
-1
@@ -229,6 +229,8 @@ export function uploadWriter(base: VfsNode, baseUri: string, filename: string, c
229
Object.assign(ctx.state, { opTotal: fullSize, opOffset: resume / fullSize, opProgress: 0 })
230
const conn = updateConnectionForCtx(ctx)
231
if (!conn) return
232
+ if (writeStream.closed || writeStream.destroyed || writeStream.writableFinished) return
233
+ // tracking
234
const h = setInterval(() => {
235
const now = Date.now()
236
const got = bytesGot()
@@ -237,7 +239,9 @@ export function uploadWriter(base: VfsNode, baseUri: string, filename: string, c
239
lastGotTime = now
240
updateConnection(conn, { inSpeed, got }, { opProgress: (resume + got) / fullSize })
241
}, 1000)
240
- writeStream.once('close', () => clearInterval(h) )
242
+ const stopTracking = () => clearInterval(h)
243
+ writeStream.once('close', stopTracking)
244
+ writeStream.once('error', stopTracking)
245
}
246
247
function bytesGot() {
src/walkDir.ts
+1
-1
@@ -68,7 +68,7 @@ export function walkDir(path: string, { depth = 0, hidden = true, parallelizeRec
68
}
69
else for await (let entry of (pluginIterator || await opendir(base))) {
70
if (stopped) break
71
- if (!hidden && entry.name[0] === '.' && !IS_WINDOWS)
71
+ if (!hidden && !IS_WINDOWS && entry.name[0] === '.')
72
continue
73
const stats = entry.isSymbolicLink?.() && await statWithTimeout(join(base, entry.name)).catch(() => null)
74
if (stats === null) continue