@samitouri / QOSami-HFS / commits / d76ba963

folder-size: stop working on aborted requests

Massimo Melina committed Jan 15, 2025 at 00:26 UTC d76ba9634606f59faac6b25038242a82c3c8ef0b
5 files changed +13 -4
src/api.vfs.ts
+1 -1
@@ -201,7 +201,7 @@ const apis: ApiHandlers = {
201 const matching = makeMatcher(fileMask)
202 path = isWindowsDrive(path) ? path + '\\' : resolve(path || '/')
203 for await (const entry of dirStream(path)) {
204 - if (ctx.req.destroyed)
204 + if (ctx.isAborted())
205 return
206 const {path:name} = entry
207 const isDir = entry.isDirectory()
src/index.ts
+9
@@ -72,3 +72,12 @@ defineConfig('proxies', 0).sub(n => {
72 app.proxy = n > 0
73 app.maxIpsCount = n
74 })
75 +
76 +declare module "koa" {
77 + interface BaseContext {
78 + isAborted(): boolean
79 + }
80 +}
81 +app.context.isAborted = function() {
82 + return this.res.destroyed || this.req.aborted // investigate: "aborted" is deprecated, but "destroyed" will cause failure of some tests
83 +}
src/upload.ts
+1 -1
@@ -161,7 +161,7 @@ export function uploadWriter(base: VfsNode, baseUri: string, path: string, ctx:
161 writeStream.once('close', async () => {
162 try {
163 await new Promise(res => fileStream.close(res)) // this only seem to be necessary on Windows
164 - if (ctx.req.aborted) { // doc says .aborted is deprecated, replaced by .destroyed, but in my tests the latter is true even for completed uploads, while .aborted is only for interrupted ones
164 + if (ctx.isAborted()) {
165 if (resumableTempName && !resumableLost && !resuming) // we don't want to be left with 2 temp files
166 return rm(tempName)
167 const sec = deleteUnfinishedUploadsAfter.get()
src/vfs.ts
+1 -1
@@ -307,7 +307,7 @@ export async function* walkNode(parent: VfsNode, {
307 let lastDir = prefixPath.slice(0, -1) || '.'
308 parentsCache.set(lastDir, parent)
309 for await (const entry of dirStream(source, { depth, onlyFolders, hidden: showHiddenFiles.get() })) {
310 - if (ctx?.req.aborted) break // investigate: "aborted" is deprecated, but "destroyed" will cause failure of some tests
310 + if (ctx?.isAborted()) break
311 const {path} = entry
312 const isFolder = entry.isDirectory()
313 const name = prefixPath + (parent.rename?.[path] || path)
src/zip.ts
+1 -1
@@ -24,7 +24,7 @@ export async function zipStreamFromFolder(node: VfsNode, ctx: Koa.Context) {
24 const walker = !list ? walkNode(node, { ctx, requiredPerm: 'can_archive' })
25 : (async function*(): AsyncIterableIterator<VfsNode> {
26 for await (const uri of list) {
27 - if (ctx.req.destroyed) return
27 + if (ctx.isAborted()) return
28 const subNode = await urlToNode(uri, ctx, node)
29 if (!subNode)
30 continue