better code
Massimo Melina committed
Dec 21, 2021 at 20:43 UTC
8b0b151ec32db38b4b0d7ab799e134ca149ad83b
1 file changed
+34
-38
src/vfs.ts
+34
-38
@@ -125,45 +125,41 @@ export function directPermOnNode(node:VfsNode, username:string) {
125
}
126
127
128
-export async function* walkNode(root:VfsNode, who:string, depth:number=0): AsyncIterableIterator<VfsNode> {
129
- yield* recur(root, '', depth)
130
-
131
- async function* recur(parent:VfsNode, prefixPath:string, depth:number): AsyncGenerator<VfsNode> {
132
- const { children, source } = parent
133
- if (children)
134
- for (const c of children) {
135
- if (c.hidden || !directPermOnNode(c,who))
136
- continue
137
- yield prefixPath ? { ...c, name: prefixPath+c.name } : c
138
- if (depth > 0 && c)
139
- yield* recur(c, prefixPath+c.name+'/', depth - 1)
140
- }
141
- if (!source)
142
- return
143
- const base = enforceFinal('/', complySlashes(source)) // fast-glob lib wants forward-slashes
144
- const baseForGlob = glob.escapePath(base)
145
- const ignore = [parent.hide, parent.remove].flat().filter(Boolean).map(x => baseForGlob+x)
146
- const depthPath = depth === Infinity ? '**/' : _.repeat('*/',depth)
147
- try {
148
- const dirStream = glob.stream(baseForGlob + depthPath + '*', {
149
- dot: true,
150
- onlyFiles: false,
151
- ignore,
152
- })
153
- for await (let path of dirStream) {
154
- if (path instanceof Buffer)
155
- path = path.toString('utf8')
156
- const name = path.slice(base.length)
157
- yield {
158
- type: VfsNodeType.temp,
159
- source: path,
160
- name: parent!.rename?.[name] || name
161
- }
162
- }
128
+export async function* walkNode(parent:VfsNode, who:string, depth:number=0, prefixPath:string=''): AsyncIterableIterator<VfsNode> {
129
+ const { children, source } = parent
130
+ if (children)
131
+ for (const c of children) {
132
+ if (c.hidden || !directPermOnNode(c,who))
133
+ continue
134
+ yield prefixPath ? { ...c, name: prefixPath+c.name } : c
135
+ if (depth > 0 && c)
136
+ yield* walkNode(c, prefixPath+c.name+'/', depth - 1)
137
}
164
- catch(e) {
165
- if ((e as any).code !== 'ENOTDIR')
166
- throw e
138
+ if (!source)
139
+ return
140
+ const base = enforceFinal('/', complySlashes(source)) // fast-glob lib wants forward-slashes
141
+ const baseForGlob = glob.escapePath(base)
142
+ const ignore = [parent.hide, parent.remove].flat().filter(Boolean).map(x => baseForGlob+x)
143
+ const depthPath = depth === Infinity ? '**/' : _.repeat('*/',depth)
144
+ try {
145
+ const dirStream = glob.stream(baseForGlob + depthPath + '*', {
146
+ dot: true,
147
+ onlyFiles: false,
148
+ ignore,
149
+ })
150
+ for await (let path of dirStream) {
151
+ if (path instanceof Buffer)
152
+ path = path.toString('utf8')
153
+ const name = path.slice(base.length)
154
+ yield {
155
+ type: VfsNodeType.temp,
156
+ source: path,
157
+ name: parent!.rename?.[name] || name
158
+ }
159
}
160
}
161
+ catch(e) {
162
+ if ((e as any).code !== 'ENOTDIR')
163
+ throw e
164
+ }
165
}