fix: mask "!" operator wasn't working

Massimo Melina committed Apr 10, 2023 at 13:04 UTC cf17a5de3ff0cdfb86d3cf45647fa0512ac17749
3 files changed +10 -10
src/misc.ts
+2 -2
@@ -180,12 +180,12 @@ export function makeNetMatcher(mask: string, emptyMaskReturns=false) {
180 }
181
182 export function makeMatcher(mask: string, emptyMaskReturns=false) {
183 - return mask ? matcher('(' + mask + ')') // adding () will allow us to use the pipe at root level
183 + return mask ? matcher(mask.replace(/^(!)?/, '$1(') + ')') // adding () will allow us to use the pipe at root level
184 : () => emptyMaskReturns
185 }
186
187 export function matches(s: string, mask: string, emptyMaskReturns=false) {
188 - return makeMatcher('(' + mask + ')', emptyMaskReturns)(s) // adding () will allow us to use the pipe at root level
188 + return makeMatcher(mask, emptyMaskReturns)(s) // adding () will allow us to use the pipe at root level
189 }
190
191 export function same(a: any, b: any) {
src/vfs.ts
+7 -7
@@ -251,14 +251,14 @@ function inheritMasks(item: VfsNode, parent: VfsNode, virtualBasename:string) {
251 const { masks } = parent
252 if (!masks) return
253 const o: Masks = {}
254 - for (const [k,v] of Object.entries(masks))
255 - if (k.startsWith('**')) {
254 + for (const [k,v] of Object.entries(masks)) {
255 + const neg = k[0] === '!' && k[1] !== '(' ? '!' : ''
256 + const withoutNeg = neg ? k.slice(1) : k
257 + if (withoutNeg.startsWith('**'))
258 o[k] = v
257 - if (k[3] === '/' )
258 - o[k.slice(3)] = v
259 - }
260 - else if (k.startsWith(virtualBasename+'/'))
261 - o[k.slice(virtualBasename.length+1)] = v
259 + else if (withoutNeg.startsWith(virtualBasename + '/'))
260 + o[neg + withoutNeg.slice(virtualBasename.length + 1)] = v
261 + }
262 if (Object.keys(o).length)
263 item.masks = _.defaults(item.masks, o)
264 }
tests/config.yaml
+1 -1
@@ -3,7 +3,7 @@ vfs:
3 mime:
4 "*": auto
5 masks:
6 - tests/page/*.html:
6 + "!tests/page/*.png":
7 mime: text/plain
8 protectFromAbove/child/*.txt:
9 can_read: false