fix: double entry if folder loads from disk an entry with same name as another configured as child in the tree

Massimo Melina committed Jan 18, 2023 at 00:33 UTC 8e7a61d287e175ec7efd071dc2eb826d32a87ffd
1 file changed +7 -3
src/vfs.ts
+7 -3
@@ -149,12 +149,15 @@ export function hasPermission(node: VfsNode, perm: keyof VfsPerm, ctx: Koa.Conte
149
150 export async function* walkNode(parent:VfsNode, ctx?: Koa.Context, depth:number=0, prefixPath:string=''): AsyncIterableIterator<VfsNode> {
151 const { children, source } = parent
152 + const took = prefixPath ? undefined : new Set()
153 if (children)
154 for (let idx = 0; idx < children.length; idx++) {
155 const child = children[idx]
156 + const name = prefixPath + getNodeName(child)
157 + took?.add(name)
158 yield* workItem({
159 ...child,
157 - name: prefixPath ? (prefixPath + getNodeName(child)) : child.name
160 + name,
161 }, depth > 0 && await nodeIsDirectory(child).catch(() => false))
162 }
163 if (!source)
@@ -163,9 +166,10 @@ export async function* walkNode(parent:VfsNode, ctx?: Koa.Context, depth:number=
166 for await (const path of dirStream(source, depth)) {
167 if (ctx?.req.aborted)
168 return
166 - const renamed = parent.rename?.[path]
169 + const name = prefixPath + (parent.rename?.[path] || path)
170 + if (took?.has(name)) continue
171 yield* workItem({
168 - name: prefixPath + (renamed || path),
172 + name,
173 source: join(source, path),
174 rename: renameUnderPath(parent.rename, path),
175 })