@cyclonedx/cyclonedx-library
Version:
Core functionality of CycloneDX for JavaScript (Node.js or WebBrowser).
59 lines (55 loc) • 2.35 kB
JavaScript
;
/*!
This file is part of CycloneDX JavaScript Library.
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.LicenseEvidenceGatherer = void 0;
const attachmentEncoding_1 = require("../../enums/attachmentEncoding");
const attachment_1 = require("../../models/attachment");
const mime_node_1 = require("./_helpers/mime.node");
const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i;
class LicenseEvidenceGatherer {
#fs;
#path;
constructor(options = {}) {
this.#fs = options.fs ?? require('node:fs');
this.#path = options.path ?? require('node:path');
}
*getFileAttachments(prefixPath, onError = noop) {
const files = this.#fs.readdirSync(prefixPath);
for (const file of files) {
if (!LICENSE_FILENAME_PATTERN.test(file)) {
continue;
}
const filePath = this.#path.join(prefixPath, file);
if (!this.#fs.statSync(filePath).isFile()) {
continue;
}
const contentType = (0, mime_node_1.guessMimeTypeForLicenseFile)(file);
if (contentType === undefined) {
continue;
}
try {
yield { filePath, file, text: new attachment_1.Attachment(this.#fs.readFileSync(filePath)
.toString('base64'), { contentType, encoding: attachmentEncoding_1.AttachmentEncoding.Base64 }) };
}
catch (cause) {
onError(new Error(`skipped license file ${filePath}`, { cause }));
}
}
}
}
exports.LicenseEvidenceGatherer = LicenseEvidenceGatherer;
function noop() { }
//# sourceMappingURL=utils.node.js.map