UNPKG

@cyclonedx/webpack-plugin

Version:

Creates CycloneDX Software Bill of Materials (SBoM) from webpack projects

87 lines (82 loc) 3.08 kB
"use strict"; /*! This file is part of CycloneDX Webpack plugin. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache-2.0 Copyright (c) OWASP Foundation. All Rights Reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.structuredClonePolyfill = void 0; exports.isNonNullable = isNonNullable; exports.getPackageDescription = getPackageDescription; exports.isValidPackageJSON = isValidPackageJSON; exports.loadJsonFile = loadJsonFile; exports.iterableSome = iterableSome; const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); function isNonNullable(value) { return value !== null && value !== undefined; } exports.structuredClonePolyfill = typeof structuredClone === 'function' ? structuredClone : function (value) { return JSON.parse(JSON.stringify(value)); }; const PACKAGE_MANIFEST_FILENAME = 'package.json'; function getPackageDescription(path) { const isSubDirOfNodeModules = isSubDirectoryOfNodeModulesFolder(path); while ((0, node_path_1.isAbsolute)(path)) { const pathToPackageJson = (0, node_path_1.join)(path, PACKAGE_MANIFEST_FILENAME); if ((0, node_fs_1.existsSync)(pathToPackageJson)) { try { const contentOfPackageJson = loadJsonFile(pathToPackageJson) ?? {}; if (!isSubDirOfNodeModules || isValidPackageJSON(contentOfPackageJson)) { return { path: pathToPackageJson, packageJson: contentOfPackageJson }; } } catch { return undefined; } } const nextPath = (0, node_path_1.dirname)(path); if (nextPath === path || isNodeModulesFolder(nextPath)) { return undefined; } path = nextPath; } return undefined; } const NODE_MODULES_FOLDERNAME = 'node_modules'; function isNodeModulesFolder(path) { return path.endsWith(`${node_path_1.sep}${NODE_MODULES_FOLDERNAME}`); } function isSubDirectoryOfNodeModulesFolder(path) { return path.includes(`${node_path_1.sep}${NODE_MODULES_FOLDERNAME}${node_path_1.sep}`); } function isValidPackageJSON(pkg) { return isNonNullable(pkg) && typeof pkg.name === 'string' && typeof pkg.version === 'string'; } function loadJsonFile(path) { return JSON.parse((0, node_fs_1.readFileSync)(path, 'utf8')); } function iterableSome(i, t) { for (const v of i) { if (t(v)) { return true; } } return false; }