fix: bad name if vfs node was just source '.'

Massimo Melina committed Jan 19, 2023 at 11:15 UTC f65a2938093019eae6291c0e1214b6e1fcf2b0a8
1 file changed +12 -8
src/vfs.ts
+12 -8
@@ -1,7 +1,7 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import fs from 'fs/promises'
4 -import { basename, join } from 'path'
4 +import { basename, join, resolve } from 'path'
5 import { isMatch } from 'micromatch'
6 import { dirStream, dirTraversal, enforceFinal, getOrSet, isDirectory, typedKeys } from './misc'
7 import Koa from 'koa'
@@ -129,13 +129,17 @@ export function saveVfs() {
129 }
130
131 export function getNodeName(node: VfsNode) {
132 - return node.name
133 - || node.source && (
134 - /^[a-zA-Z]:\\?$/.test(node.source) && node.source.slice(0, 2)
135 - || basename(node.source)
136 - || node.source
137 - )
138 - || '' // should happen only for root
132 + const { name, source: s } = node
133 + if (name)
134 + return name
135 + if (!s)
136 + return '' // should happen only for root
137 + if (/^[a-zA-Z]:\\?$/.test(s))
138 + return s.slice(0, 2) // exclude trailing slash
139 + const base = basename(s)
140 + if (/^[./\\]*$/.test(base)) // if empty or special-chars-only
141 + return basename(resolve(s)) // resolve to try to get more
142 + return base
143 }
144
145 export async function nodeIsDirectory(node: VfsNode) {