From 6c2c95851ae759cb149bc0868145f0b6611b61ee Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 12 Apr 2018 23:34:43 +0300 Subject: [PATCH] fix(service-worker): let `*` match 0 characters in globs (#23339) In [glob patterns][1], the `*` wildcard is supposed to match 0 or more characters. For reference: - This is also how `*` works in other implementations, such as `.gitignore` files or Firebase hosting config. - Some popular JS implementations (e.g. [minimatch][2], [micromatch][3]) work differently, matching 1 or more character (but not 0). This commit "fixes" the minimal glob support in `@angular/service-worker` to allow `*` to also match 0 characters. [1]: https://en.wikipedia.org/wiki/Glob_%28programming%29 [2]: https://www.npmjs.com/package/minimatch [3]: https://www.npmjs.com/package/micromatch PR Close #23339 --- packages/service-worker/config/src/glob.ts | 2 +- packages/service-worker/config/test/generator_spec.ts | 9 +++++---- packages/service-worker/worker/test/happy_spec.ts | 6 ++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/service-worker/config/src/glob.ts b/packages/service-worker/config/src/glob.ts index 802189d09e..e886296cd2 100644 --- a/packages/service-worker/config/src/glob.ts +++ b/packages/service-worker/config/src/glob.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -const WILD_SINGLE = '[^\\/]+'; +const WILD_SINGLE = '[^\\/]*'; const WILD_OPEN = '(?:.+\\/)?'; const TO_ESCAPE = [ diff --git a/packages/service-worker/config/test/generator_spec.ts b/packages/service-worker/config/test/generator_spec.ts index 75047119bf..d7252af471 100644 --- a/packages/service-worker/config/test/generator_spec.ts +++ b/packages/service-worker/config/test/generator_spec.ts @@ -81,7 +81,7 @@ import {MockFilesystem} from '../testing/mock'; patterns: [ '\\/absolute\\/.*', '\\/some\\/url\\?with\\+escaped\\+chars', - '\\/test\\/relative\\/[^\\/]+\\.txt', + '\\/test\\/relative\\/[^\\/]*\\.txt', ] }], dataGroups: [{ @@ -97,7 +97,7 @@ import {MockFilesystem} from '../testing/mock'; {positive: true, regex: '^\\/included\\/absolute\\/.*$'}, {positive: false, regex: '^\\/excluded\\/absolute\\/.*$'}, {positive: true, regex: '^\\/included\\/some\\/url\\?with\\+escaped\\+chars$'}, - {positive: false, regex: '^\\/test\\/excluded\\/relative\\/[^\\/]+\\.txt$'}, + {positive: false, regex: '^\\/test\\/excluded\\/relative\\/[^\\/]*\\.txt$'}, {positive: true, regex: '^http:\\/\\/example\\.com\\/included$'}, {positive: false, regex: '^http:\\/\\/example\\.com\\/excluded$'}, ], @@ -129,8 +129,9 @@ import {MockFilesystem} from '../testing/mock'; dataGroups: [], navigationUrls: [ {positive: true, regex: '^\\/.*$'}, - {positive: false, regex: '^\\/(?:.+\\/)?[^\\/]+\\.[^\\/]+$'}, - {positive: false, regex: '^\\/(?:.+\\/)?[^\\/]+__[^\\/]+\\/.*$'}, + {positive: false, regex: '^\\/(?:.+\\/)?[^\\/]*\\.[^\\/]*$'}, + {positive: false, regex: '^\\/(?:.+\\/)?[^\\/]*__[^\\/]*$'}, + {positive: false, regex: '^\\/(?:.+\\/)?[^\\/]*__[^\\/]*\\/.*$'}, ], hashTable: {} }); diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index 423751be78..87a57c67e7 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -688,6 +688,12 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate)); expect(await navRequest('/baz/x__x/qux')).toBeNull(); server.assertSawRequestFor('/baz/x__x/qux'); + + expect(await navRequest('/baz/__')).toBeNull(); + server.assertSawRequestFor('/baz/__'); + + expect(await navRequest('/baz/__/qux')).toBeNull(); + server.assertSawRequestFor('/baz/__/qux'); }); describe('(with custom `navigationUrls`)', () => {