better code

Massimo Melina committed Feb 7, 2022 at 00:04 UTC 98f4926ffb1f4e8c53302a99d9c4344f87104500
1 file changed +9 -13
src/vfs.ts
+9 -13
@@ -1,7 +1,7 @@
1 import fs from 'fs/promises'
2 import { basename } from 'path'
3 import { isMatch } from 'micromatch'
4 -import { enforceFinal, isDirectory, isWindows, onlyTruthy, prefix } from './misc'
4 +import { enforceFinal, isDirectory, isWindows, onlyTruthy } from './misc'
5 import Koa from 'koa'
6 import glob from 'fast-glob'
7 import _ from 'lodash'
@@ -51,20 +51,16 @@ export class Vfs {
51 const rest = decoded.split('/').filter(Boolean)
52 if (ctx && !hasPermission(run, ctx)) return
53 while (rest.length) {
54 - let piece = rest.shift() as string
55 - const child = findChildByName(piece, run)
56 - if (child) {
57 - run = child
54 + const child = findChildByName(rest[0], run) // does the tree node have a child that goes by this name?
55 + if (child) { // yes
56 + rest.shift() // consume
57 + run = child // move cursor
58 if (ctx && !hasPermission(run, ctx)) return
59 - continue
60 - }
61 - if (!run.source)
62 - return
63 - const relativeSource = piece + prefix('/', rest.join('/'))
64 - if (relativeSource.includes('..')) {
65 - ctx?.throw(418)
66 - return
59 + continue // go on
60 }
61 + // not in the tree, we can see consider continuing on the disk
62 + if (!run.source) return // but then we need the current node to be linked to the disk, otherwise, we give up
63 + const relativeSource = rest.join('/')
64 const baseSource = run.source+ '/'
65 const source = baseSource + relativeSource
66 if (run.remove && isMatch(source, run.remove.split('|').map(x => baseSource + x)))