Skip to content

Commit

Permalink
feat(core): add external package name fallback function to options in…
Browse files Browse the repository at this point in the history
… generatePolicy, use it in webpack
  • Loading branch information
naugtur committed Mar 20, 2024
1 parent 84a9acc commit 8a3a0a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/core/src/generatePolicy.js
Expand Up @@ -348,7 +348,11 @@ function createModuleInspector(opts) {
/**
* @type {GeneratePolicyFn}
*/
function generatePolicy({ policyOverride, includeDebugInfo = false }) {
function generatePolicy({
policyOverride,
includeDebugInfo = false,
moduleToPackageFallback,
}) {
/** @type {import('./schema').Resources} */
const resources = {}
/**
Expand All @@ -375,6 +379,7 @@ function createModuleInspector(opts) {
const packageDeps = aggregateDeps({
packageModules,
moduleIdToModuleRecord,
moduleToPackageFallback,
})
if (packageDeps.length) {
packages = Object.fromEntries(
Expand Down Expand Up @@ -453,10 +458,15 @@ function createModuleInspector(opts) {
* string,
* import('./moduleRecord').LavamoatModuleRecord
* >
* moduleToPackageFallback?: (specifier: string) => string | undefined
* }} opts
* @returns
*/
function aggregateDeps({ packageModules, moduleIdToModuleRecord }) {
function aggregateDeps({
packageModules,
moduleIdToModuleRecord,
moduleToPackageFallback = guessPackageName,
}) {
const deps = new Set()
// get all dep package from the "packageModules" collection of modules
Object.values(packageModules).forEach((moduleRecord) => {
Expand All @@ -477,7 +487,8 @@ function aggregateDeps({ packageModules, moduleIdToModuleRecord }) {
return
}
// moduleRecord missing, guess package name

This comment has been minimized.

Copy link
@Mistahood

Mistahood Apr 13, 2024

Designer

const packageName = guessPackageName(requestedName)
const packageName =
moduleToPackageFallback(requestedName) || `<unknown:${requestedName}>`
deps.add(packageName)
}
)
Expand All @@ -494,13 +505,13 @@ function aggregateDeps({ packageModules, moduleIdToModuleRecord }) {
* because resolution was skipped for that module
*
* @param {string} requestedName
* @returns {string}
* @returns {string | undefined}
*/
function guessPackageName(requestedName) {
const isNotPackageName =
requestedName.startsWith('/') || requestedName.startsWith('.')
if (isNotPackageName) {
return `<unknown:${requestedName}>`
return
}
// resolving is skipped so guess package name
const pathParts = requestedName.split('/')
Expand Down Expand Up @@ -530,7 +541,9 @@ function getDefaultPaths(policyName) {
* @callback GeneratePolicyFn
* @param {Partial<ModuleInspectorOptions> & {
* policyOverride?: import('./schema').LavaMoatPolicyOverrides
* moduleToPackageFallback?: (value: string) => string | undefined
* }} opts
*
* @returns {import('./schema').LavaMoatPolicy
* | import('./schema').LavaMoatPolicyDebug}
*/
Expand All @@ -545,6 +558,7 @@ function getDefaultPaths(policyName) {
* @typedef ModuleInspectorOptions
* @property {(value: string) => boolean} isBuiltin
* @property {boolean} [includeDebugInfo]
* @property {(specifier: string) => string | undefined} [moduleToPackageFallback]
*/

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/webpack/src/buildtime/policyGenerator.js
Expand Up @@ -73,6 +73,10 @@ module.exports = {
const moduleInspector = createModuleInspector({
isBuiltin: () => false,
includeDebugInfo: false,
// If the specifier is requested as a dependency in importMap but was never passed to inspectModule, its package name will be looked up here.
// This is a workaround to inconsistencies in how webpack represents connections. We're not aware of any security implications of this, since the package is already resolved clearly and this is only a part of policy generation, not runtime.
moduleToPackageFallback: (specifier) =>
getPackageNameForModulePath(canonicalNameMap, specifier),
})

return {
Expand Down

0 comments on commit 8a3a0a6

Please sign in to comment.