UNPKG

@cyclonedx/cyclonedx-npm

Version:

Create CycloneDX Software Bill of Materials (SBOM) from NPM projects.

110 lines (105 loc) 3.28 kB
"use strict"; /*! This file is part of CycloneDX generator for NPM projects. 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.loadJsonFile = loadJsonFile; exports.writeAllSync = writeAllSync; exports.isString = isString; exports.tryRemoveSecretsFromUrl = tryRemoveSecretsFromUrl; exports.getMimeForTextFile = getMimeForTextFile; exports.getMimeForLicenseFile = getMimeForLicenseFile; exports.versionCompare = versionCompare; const fs_1 = require("fs"); const path_1 = require("path"); exports.structuredClonePolyfill = typeof structuredClone === 'function' ? structuredClone : function (value) { return JSON.parse(JSON.stringify(value)); }; function loadJsonFile(path) { return JSON.parse((0, fs_1.readFileSync)(path, 'utf8')); } async function writeAllSync(fd, data) { const b = Buffer.from(data); const l = b.byteLength; let w = 0; while (w < l) { try { w += (0, fs_1.writeSync)(fd, b, w); } catch (error) { if (error.code !== 'EAGAIN') { throw error; } await new Promise((resolve) => setTimeout(resolve, 100)); } } return w; } function isString(v) { return typeof v === 'string'; } function tryRemoveSecretsFromUrl(url) { try { const u = new URL(url); u.password = ''; return u.toString(); } catch { return url; } } const MIME_TEXT_PLAIN = 'text/plain'; const MAP_TEXT_EXTENSION_MIME = { '': MIME_TEXT_PLAIN, '.csv': 'text/csv', '.htm': 'text/html', '.html': 'text/html', '.md': 'text/markdown', '.txt': MIME_TEXT_PLAIN, '.rst': 'text/prs.fallenstein.rst', '.xml': 'text/xml', '.license': MIME_TEXT_PLAIN, '.licence': MIME_TEXT_PLAIN }; function getMimeForTextFile(filename) { return MAP_TEXT_EXTENSION_MIME[(0, path_1.extname)(filename).toLowerCase()]; } const LICENSE_FILENAME_BASE = new Set(['licence', 'license']); const LICENSE_FILENAME_EXT = new Set([ '.apache', '.bsd', '.gpl', '.mit' ]); function getMimeForLicenseFile(filename) { const { name, ext } = (0, path_1.parse)(filename.toLowerCase()); return LICENSE_FILENAME_BASE.has(name) && LICENSE_FILENAME_EXT.has(ext) ? MIME_TEXT_PLAIN : MAP_TEXT_EXTENSION_MIME[ext]; } function versionCompare(a, b) { let ai, bi; for (let i = 0, l = Math.max(a.length, b.length); i < l; ++i) { ai = a[i] || 0; bi = b[i] || 0; if (ai < bi) { return -1; } if (ai > bi) { return +1; } } return 0; }