diff --git a/lib/internal/policy/manifest.js b/lib/internal/policy/manifest.js index 55e6fe3423beda..dda94c76bbe5b7 100644 --- a/lib/internal/policy/manifest.js +++ b/lib/internal/policy/manifest.js @@ -91,6 +91,10 @@ class DependencyMapperInstance { * @type {DependencyMap | undefined} */ #dependencies; + /** + * @type {PatternDependencyMap | undefined} + */ + #patternDependencies; /** * @type {DependencyMapperInstance | null | undefined} */ @@ -107,6 +111,13 @@ class DependencyMapperInstance { * @type {boolean} */ allowSameHREFScope; + /** + * + * @param {string} parentHREF + * @param {DependencyMap | undefined} dependencies + * @param {boolean} cascade + * @param {boolean} allowSameHREFScope + */ constructor( parentHREF, dependencies, @@ -114,7 +125,30 @@ class DependencyMapperInstance { allowSameHREFScope = false ) { this.href = parentHREF; - this.#dependencies = dependencies; + if (dependencies === kFallThrough || dependencies === undefined) { + this.#dependencies = dependencies; + this.#patternDependencies = undefined; + } else { + let patterns = []; + let direct = ObjectCreate(null); + for (const [key, value] of Object.entries(dependencies)) { + if (key.endsWith('*')) { + let target = /^([^*]*)\*([^*]*)$/.exec(value); + if (!target) { + throw new Error('pattern needs to have a single "*" in target'); + } + let prefix = target[1]; + let suffix = target[2]; + patterns.push([ + target.slice(0, -1), + [prefix, suffix] + ]); + } + } + patterns.sort((a, b) => a[0] < b[0] ? -1 : 1); + this.#dependencies = dependencies; + this.#patternDependencies = patterns; + } this.cascade = cascade; this.allowSameHREFScope = allowSameHREFScope; ObjectFreeze(this); @@ -303,6 +337,7 @@ function findScopeHREF(href, scopeStore, allowSame) { /** * @typedef {Record | typeof kFallThrough} DependencyMap + * @typedef {Array<[string, [string, string]]>} PatternDependencyMap * @typedef {Record | null | true} JSONDependencyMap */ /**