|
| 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 type { Comparable } from "../_helpers/sortable"; |
| 21 | +import { SortableComparables } from "../_helpers/sortable"; |
| 22 | +import { treeIteratorSymbol } from "../_helpers/tree"; |
| 23 | +import { BomRef, BomRefRepository } from "./bomRef"; |
| 24 | +import { ExternalReferenceRepository } from "./externalReference"; |
| 25 | +import { LicenseRepository } from "./license"; |
| 26 | +import type {OrganizationalEntity } from "./organizationalEntity"; |
| 27 | +import { PropertyRepository } from "./property"; |
| 28 | + |
| 29 | +export interface OptionalServiceProperties { |
| 30 | + bomRef?: BomRef['value'] |
| 31 | + provider?: Service['provider'] |
| 32 | + group?: Service['group'] |
| 33 | + version?: Service['version'] |
| 34 | + description?: Service['description'] |
| 35 | + licenses?: Service['licenses'] |
| 36 | + externalReferences?: Service['externalReferences'] |
| 37 | + services?: Service['services'] |
| 38 | + properties?: Service['properties'] |
| 39 | + |
| 40 | + dependencies?: Service['dependencies'] |
| 41 | +} |
| 42 | + |
| 43 | +export class Service implements Comparable<Service> { |
| 44 | + provider?: OrganizationalEntity |
| 45 | + group?: string |
| 46 | + name: string |
| 47 | + version?: string |
| 48 | + description?: string |
| 49 | + licenses: LicenseRepository |
| 50 | + externalReferences: ExternalReferenceRepository |
| 51 | + services: ServiceRepository |
| 52 | + properties: PropertyRepository |
| 53 | + |
| 54 | + /** @see {@link bomRef} */ |
| 55 | + readonly #bomRef: BomRef |
| 56 | + |
| 57 | + dependencies: BomRefRepository |
| 58 | + |
| 59 | + constructor(name: Service['name'], op: OptionalServiceProperties = {}) { |
| 60 | + this.#bomRef = new BomRef(op.bomRef) |
| 61 | + this.provider = op.provider |
| 62 | + this.group = op.group |
| 63 | + this.name = name |
| 64 | + this.version = op.version |
| 65 | + this.description = op.description |
| 66 | + this.licenses = op.licenses ?? new LicenseRepository() |
| 67 | + this.externalReferences = op.externalReferences ?? new ExternalReferenceRepository() |
| 68 | + this.services = op.services ?? new ServiceRepository() |
| 69 | + this.properties = op.properties ?? new PropertyRepository() |
| 70 | + |
| 71 | + this.dependencies = op.dependencies ?? new BomRefRepository() |
| 72 | + } |
| 73 | + |
| 74 | + get bomRef(): BomRef { |
| 75 | + return this.#bomRef |
| 76 | + } |
| 77 | + |
| 78 | + compare(other: Service): number { |
| 79 | + // The purpose of this method is not to test for equality, but have deterministic comparability. |
| 80 | + const bomRefCompare = this.bomRef.compare(other.bomRef) |
| 81 | + if (bomRefCompare !== 0) { |
| 82 | + return bomRefCompare |
| 83 | + } |
| 84 | + /* eslint-disable @typescript-eslint/strict-boolean-expressions -- run compares in weighted order */ |
| 85 | + return (this.group ?? '').localeCompare(other.group ?? '') || |
| 86 | + this.name.localeCompare(other.name) || |
| 87 | + (this.version ?? '').localeCompare(other.version ?? '') |
| 88 | + /* eslint-enable @typescript-eslint/strict-boolean-expressions */ |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +export class ServiceRepository extends SortableComparables<Service> { |
| 94 | + * [treeIteratorSymbol] (): Generator<Service> { |
| 95 | + for (const service of this) { |
| 96 | + yield service |
| 97 | + yield * service.services[treeIteratorSymbol]() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments