@cyclonedx/cyclonedx-library
Version:
Core functionality of CycloneDX for JavaScript (Node.js or WebBrowser).
68 lines (64 loc) • 2.58 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.PackageUrlFactory = void 0;
const packageurl_js_1 = require("packageurl-js");
const externalReferenceType_1 = require("../enums/externalReferenceType");
class PackageUrlFactory {
#type;
constructor(type) {
this.#type = type;
}
get type() {
return this.#type;
}
makeFromComponent(component, sort = false) {
const qualifiers = {};
qualifiers.__proto__ = null;
let subpath = undefined;
const extRefs = sort
? component.externalReferences.sorted()
: component.externalReferences;
for (const extRef of extRefs) {
const url = extRef.url.toString();
if (url.length <= 0) {
continue;
}
switch (extRef.type) {
case externalReferenceType_1.ExternalReferenceType.VCS:
[qualifiers[packageurl_js_1.PurlQualifierNames.VcsUrl], subpath] = url.split('#', 2);
break;
case externalReferenceType_1.ExternalReferenceType.Distribution:
qualifiers[packageurl_js_1.PurlQualifierNames.DownloadUrl] = url;
break;
}
}
const hashes = component.hashes;
if (hashes.size > 0) {
qualifiers[packageurl_js_1.PurlQualifierNames.Checksum] = Array.from(sort
? hashes.sorted()
: hashes, ([hashAlgo, hashCont]) => `${hashAlgo.toLowerCase()}:${hashCont.toLowerCase()}`).join(',');
}
try {
return new packageurl_js_1.PackageURL(this.#type, component.group, component.name, component.version, qualifiers, subpath);
}
catch {
return undefined;
}
}
}
exports.PackageUrlFactory = PackageUrlFactory;
//# sourceMappingURL=packageUrl.js.map
;