|
| 1 | +/*! |
| 2 | +This file is part of CycloneDX JavaScript Library. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +SPDX-License-Identifier: Apache-2.0 |
| 17 | +Copyright (c) OWASP Foundation. All Rights Reserved. |
| 18 | +*/ |
| 19 | + |
| 20 | +import { PackageURL } from 'packageurl-js' |
| 21 | + |
| 22 | +import * as Enums from '../enums' |
| 23 | +import { ExternalReferenceType } from '../enums' |
| 24 | +import * as Models from '../models' |
| 25 | +import * as Factories from '../factories/index.node' |
| 26 | +import { PackageJson, splitNameGroup } from '../helpers/packageJson' |
| 27 | + |
| 28 | +/** |
| 29 | + * @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json PackageJson spec} |
| 30 | + */ |
| 31 | + |
| 32 | +export class ToolBuilder { |
| 33 | + readonly #extRefFactory: Factories.FromPackageJson.ExternalReferenceFactory |
| 34 | + |
| 35 | + constructor (extRefFactory: Factories.FromPackageJson.ExternalReferenceFactory) { |
| 36 | + this.#extRefFactory = extRefFactory |
| 37 | + } |
| 38 | + |
| 39 | + makeTool (data: PackageJson): Models.Tool | undefined { |
| 40 | + const [name, vendor] = typeof data.name === 'string' |
| 41 | + ? splitNameGroup(data.name) |
| 42 | + : [] |
| 43 | + |
| 44 | + return new Models.Tool({ |
| 45 | + vendor, |
| 46 | + name, |
| 47 | + version: (typeof data.version === 'string') |
| 48 | + ? data.version |
| 49 | + : undefined, |
| 50 | + externalReferences: new Models.ExternalReferenceRepository(this.#extRefFactory.makeExternalReferences(data)) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +export class ComponentBuilder { |
| 56 | + readonly #extRefFactory: Factories.FromPackageJson.ExternalReferenceFactory |
| 57 | + readonly #licenseFactory: Factories.LicenseFactory |
| 58 | + |
| 59 | + constructor (extRefFactory: Factories.FromPackageJson.ExternalReferenceFactory, licenseFactory: Factories.LicenseFactory) { |
| 60 | + this.#extRefFactory = extRefFactory |
| 61 | + this.#licenseFactory = licenseFactory |
| 62 | + } |
| 63 | + |
| 64 | + makeComponent (data: PackageJson, type: Enums.ComponentType = Enums.ComponentType.Library): Models.Component | undefined { |
| 65 | + if (typeof data.name !== 'string') { |
| 66 | + return undefined |
| 67 | + } |
| 68 | + |
| 69 | + const [name, group] = splitNameGroup(data.name) |
| 70 | + if (name.length === 0) { |
| 71 | + return undefined |
| 72 | + } |
| 73 | + |
| 74 | + /** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#author} */ |
| 75 | + const author = typeof data.author === 'string' |
| 76 | + ? data.author |
| 77 | + : (typeof data.author?.name === 'string' |
| 78 | + ? data.author.name |
| 79 | + : undefined) |
| 80 | + /** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#description-1} */ |
| 81 | + const description = typeof data.description === 'string' |
| 82 | + ? data.description |
| 83 | + : undefined |
| 84 | + /** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#version} */ |
| 85 | + const version = typeof data.version === 'string' |
| 86 | + ? data.version |
| 87 | + : undefined |
| 88 | + const externalReferences = this.#extRefFactory.makeExternalReferences(data) |
| 89 | + /** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#license} */ |
| 90 | + const license = typeof data.license === 'string' |
| 91 | + ? this.#licenseFactory.makeFromString(data.license) |
| 92 | + : undefined |
| 93 | + |
| 94 | + return new Models.Component(type, name, { |
| 95 | + author, |
| 96 | + description, |
| 97 | + externalReferences: new Models.ExternalReferenceRepository(externalReferences), |
| 98 | + group, |
| 99 | + licenses: new Models.LicenseRepository( |
| 100 | + license === undefined |
| 101 | + ? [] |
| 102 | + : [license] |
| 103 | + ), |
| 104 | + version, |
| 105 | + purl: this.#makePurl(name, group, version, externalReferences) |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + #makePurl ( |
| 110 | + name: string, |
| 111 | + group: string | undefined, |
| 112 | + version: string | undefined, |
| 113 | + externalReferences: Models.ExternalReference[] |
| 114 | + ): PackageURL { |
| 115 | + const qualifiers: { [key: string]: string } = {} |
| 116 | + const subpath = undefined |
| 117 | + |
| 118 | + const vcsUrl = externalReferences.filter( |
| 119 | + ({ type }) => type === ExternalReferenceType.VCS |
| 120 | + )[0]?.url.toString() |
| 121 | + if (vcsUrl !== undefined) { |
| 122 | + qualifiers.vcs_url = vcsUrl |
| 123 | + } |
| 124 | + |
| 125 | + return new PackageURL( |
| 126 | + 'npm', group, name, version, qualifiers, subpath) |
| 127 | + } |
| 128 | +} |
0 commit comments