|
| 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 { ComponentType, ExternalReferenceType, HashAlgorithm, Vulnerability } from '../enums' |
| 21 | +import type { HashContent } from '../models' |
| 22 | +import type { Format, Version } from './enums' |
| 23 | + |
| 24 | +/** |
| 25 | + * This interface is not intended to be public API. |
| 26 | + * This interface may be affected by breaking changes without notice. |
| 27 | + * |
| 28 | + * See the public exported constants, like {@link Spec.Spec1dot4}, that provide objects implementing this interface. |
| 29 | + * See also {@link Spec.SpecVersionDict} for implementations. |
| 30 | + */ |
| 31 | +export interface _SpecProtocol { |
| 32 | + version: Version |
| 33 | + supportsFormat: (f: Format | any) => boolean |
| 34 | + supportsComponentType: (ct: ComponentType | any) => boolean |
| 35 | + supportsHashAlgorithm: (ha: HashAlgorithm | any) => boolean |
| 36 | + supportsHashValue: (hv: HashContent | any) => boolean |
| 37 | + supportsExternalReferenceType: (ert: ExternalReferenceType | any) => boolean |
| 38 | + supportsDependencyGraph: boolean |
| 39 | + supportsToolReferences: boolean |
| 40 | + requiresComponentVersion: boolean |
| 41 | + supportsProperties: (model: any) => boolean |
| 42 | + supportsVulnerabilities: boolean |
| 43 | + supportsVulnerabilityRatingMethod: (rm: Vulnerability.RatingMethod | any) => boolean |
| 44 | + supportsComponentEvidence: boolean |
| 45 | + supportsMetadataLifecycles: boolean |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * This class was never intended to be public API, |
| 50 | + * |
| 51 | + * This is a helper to get the exact spec-versions implemented according to {@link _SpecProtocol | Specification}. |
| 52 | + * |
| 53 | + * @internal as this class may be affected by breaking changes without notice |
| 54 | + */ |
| 55 | +export class _Spec implements _SpecProtocol { |
| 56 | + readonly #version: Version |
| 57 | + readonly #formats: ReadonlySet<Format> |
| 58 | + readonly #componentTypes: ReadonlySet<ComponentType> |
| 59 | + readonly #hashAlgorithms: ReadonlySet<HashAlgorithm> |
| 60 | + readonly #hashValuePattern: RegExp |
| 61 | + readonly #externalReferenceTypes: ReadonlySet<ExternalReferenceType> |
| 62 | + readonly #vulnerabilityRatingMethods: ReadonlySet<Vulnerability.RatingMethod> |
| 63 | + readonly #supportsDependencyGraph: boolean |
| 64 | + readonly #supportsToolReferences: boolean |
| 65 | + readonly #requiresComponentVersion: boolean |
| 66 | + readonly #supportsProperties: boolean |
| 67 | + readonly #supportsVulnerabilities: boolean |
| 68 | + readonly #supportsComponentEvidence: boolean |
| 69 | + readonly #supportsMetadataLifecycles: boolean |
| 70 | + |
| 71 | + constructor ( |
| 72 | + version: Version, |
| 73 | + formats: Iterable<Format>, |
| 74 | + componentTypes: Iterable<ComponentType>, |
| 75 | + hashAlgorithms: Iterable<HashAlgorithm>, |
| 76 | + hashValuePattern: RegExp, |
| 77 | + externalReferenceTypes: Iterable<ExternalReferenceType>, |
| 78 | + supportsDependencyGraph: boolean, |
| 79 | + supportsToolReferences: boolean, |
| 80 | + requiresComponentVersion: boolean, |
| 81 | + supportsProperties: boolean, |
| 82 | + supportsVulnerabilities: boolean, |
| 83 | + vulnerabilityRatingMethods: Iterable<Vulnerability.RatingMethod>, |
| 84 | + supportsComponentEvidence: boolean, |
| 85 | + supportsMetadataLifecycles: boolean |
| 86 | + ) { |
| 87 | + this.#version = version |
| 88 | + this.#formats = new Set(formats) |
| 89 | + this.#componentTypes = new Set(componentTypes) |
| 90 | + this.#hashAlgorithms = new Set(hashAlgorithms) |
| 91 | + this.#hashValuePattern = hashValuePattern |
| 92 | + this.#externalReferenceTypes = new Set(externalReferenceTypes) |
| 93 | + this.#supportsDependencyGraph = supportsDependencyGraph |
| 94 | + this.#supportsToolReferences = supportsToolReferences |
| 95 | + this.#requiresComponentVersion = requiresComponentVersion |
| 96 | + this.#supportsProperties = supportsProperties |
| 97 | + this.#supportsVulnerabilities = supportsVulnerabilities |
| 98 | + this.#vulnerabilityRatingMethods = new Set(vulnerabilityRatingMethods) |
| 99 | + this.#supportsComponentEvidence = supportsComponentEvidence |
| 100 | + this.#supportsMetadataLifecycles = supportsMetadataLifecycles |
| 101 | + } |
| 102 | + |
| 103 | + get version (): Version { |
| 104 | + return this.#version |
| 105 | + } |
| 106 | + |
| 107 | + supportsFormat (f: Format | any): boolean { |
| 108 | + return this.#formats.has(f) |
| 109 | + } |
| 110 | + |
| 111 | + supportsComponentType (ct: ComponentType | any): boolean { |
| 112 | + return this.#componentTypes.has(ct) |
| 113 | + } |
| 114 | + |
| 115 | + supportsHashAlgorithm (ha: HashAlgorithm | any): boolean { |
| 116 | + return this.#hashAlgorithms.has(ha) |
| 117 | + } |
| 118 | + |
| 119 | + supportsHashValue (hv: HashContent | any): boolean { |
| 120 | + return typeof hv === 'string' && |
| 121 | + this.#hashValuePattern.test(hv) |
| 122 | + } |
| 123 | + |
| 124 | + supportsExternalReferenceType (ert: ExternalReferenceType | any): boolean { |
| 125 | + return this.#externalReferenceTypes.has(ert) |
| 126 | + } |
| 127 | + |
| 128 | + get supportsDependencyGraph (): boolean { |
| 129 | + return this.#supportsDependencyGraph |
| 130 | + } |
| 131 | + |
| 132 | + get supportsToolReferences (): boolean { |
| 133 | + return this.#supportsToolReferences |
| 134 | + } |
| 135 | + |
| 136 | + get requiresComponentVersion (): boolean { |
| 137 | + return this.#requiresComponentVersion |
| 138 | + } |
| 139 | + |
| 140 | + supportsProperties (): boolean { |
| 141 | + // currently a global allow/deny -- might work based on input, in the future |
| 142 | + return this.#supportsProperties |
| 143 | + } |
| 144 | + |
| 145 | + get supportsVulnerabilities (): boolean { |
| 146 | + return this.#supportsVulnerabilities |
| 147 | + } |
| 148 | + |
| 149 | + supportsVulnerabilityRatingMethod (rm: Vulnerability.RatingMethod | any): boolean { |
| 150 | + return this.#vulnerabilityRatingMethods.has(rm) |
| 151 | + } |
| 152 | + |
| 153 | + get supportsComponentEvidence (): boolean { |
| 154 | + return this.#supportsComponentEvidence |
| 155 | + } |
| 156 | + |
| 157 | + get supportsMetadataLifecycles (): boolean { |
| 158 | + return this.#supportsMetadataLifecycles |
| 159 | + } |
| 160 | +} |
0 commit comments