-
-
Notifications
You must be signed in to change notification settings - Fork 14
[WIP] feat: Builders.FromPath.LicenseEvidenceBuilder
#1158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6186d73
feat: `builders.FromPath.LicenseEvidenceBuilder`
jkowalleck 98973e5
feat
jkowalleck 30f1915
feat
jkowalleck 09cb566
feat
jkowalleck e68b2bc
feat
jkowalleck 9a94456
feat
jkowalleck 3ec62c7
feat
jkowalleck de97015
feat
jkowalleck a9e5615
feat
jkowalleck d80368c
feat
jkowalleck 4d9a100
feat
jkowalleck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /*! | ||
| 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. | ||
| */ | ||
|
|
||
|
|
||
| import {extname} from "path"; | ||
|
|
||
| import type {MimeType} from "../types/mimeType"; | ||
|
|
||
| const MAP_TEXT_EXTENSION_MIMETYPE: Readonly<Record<string, MimeType>> = { | ||
| '': 'text/plain', // our scope is text! | ||
| '.csv': 'text/csv', | ||
| '.htm': 'text/html', | ||
| '.html': 'text/html', | ||
| '.licence': 'text/plain', | ||
| '.license': 'text/plain', | ||
| '.md': 'text/markdown', | ||
| '.rst': 'text/prs.fallenstein.rst', | ||
| '.txt': 'text/plain', | ||
| '.xml': 'text/xml' // not `application/xml` -- our scope is text! | ||
| } as const | ||
|
|
||
| /** | ||
| * Returns the guessed mime type of file, based on file name extension. | ||
| * Returns undefined if tile extension is unknown. | ||
| */ | ||
| export function getMimeTypeForTextFile (filename: string): MimeType | undefined { | ||
| return MAP_TEXT_EXTENSION_MIMETYPE[extname(filename).toLowerCase()] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /*! | ||
| 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Node-specifics. | ||
| */ | ||
|
|
||
| import {readdirSync} from "fs"; | ||
| import {join, relative} from "path"; | ||
|
|
||
| import type * as Factories from "../factories/index.node"; | ||
| import {NamedLicense} from "../models/license"; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Node-specific LicenseEvidenceBuilder. | ||
| */ | ||
| export class LicenseEvidenceBuilder { | ||
|
|
||
| readonly #attachmentFactory: Factories.FromPath.AttachmentFactory | ||
|
|
||
| constructor(attachmentFactory: LicenseEvidenceBuilder['attachmentFactory']) { | ||
| this.#attachmentFactory = attachmentFactory | ||
| } | ||
|
|
||
| get attachmentFactory (): Factories.FromPath.AttachmentFactory { | ||
| return this.#attachmentFactory | ||
| } | ||
|
|
||
| /** | ||
| * Return a license on success. | ||
| * Returns undefined if it appears to bes no known text file. | ||
| * Throws error, if license attachment content could not be fetched. | ||
| * | ||
| * If `relativeFrom` is given, the file is displayed relative from there, | ||
| * else the file is displayed unmodified. | ||
| * | ||
| * @param file - path to file | ||
| * @param relativeFrom - relative path reference for file display | ||
| */ | ||
| public fromFile(file: string, relativeFrom: string | undefined = undefined): NamedLicense | undefined { | ||
| let name | ||
| if ( relativeFrom === undefined) { | ||
| name = `file: ${file}` | ||
| } else { | ||
| // `file` could be absolute or relative path - lets resolve it anyway | ||
| name = `file: ${relative(relativeFrom, file)}` | ||
| } | ||
| const text = this.#attachmentFactory.fromTextFile(file) | ||
| if (text === undefined) { | ||
| return undefined | ||
| } | ||
| return new NamedLicense(name, {text}) | ||
| } | ||
|
|
||
| readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i | ||
|
|
||
| /** | ||
| * Returns a generator for license evidences in a directory. | ||
| * Throws error, if directory content could not be inspected. | ||
| * | ||
| * Unreadable files will be omitted. | ||
| * | ||
| * @param dir - path to inspect | ||
| * @param relativeFrom - relative path reference for file display | ||
| * | ||
| * @remarks | ||
| * | ||
| * Utilizes {@link fromFile}. | ||
| */ | ||
| public * fromDir(dir: string, relativeFrom: string | undefined = undefined): Generator<NamedLicense> { | ||
| // may throw if `readdirSync()` fails | ||
| const dcis = readdirSync(dir, { withFileTypes: true }) | ||
| for (const dci of dcis) { | ||
| if ( | ||
| !dci.isFile() || | ||
| !this.#LICENSE_FILENAME_PATTERN.test(dci.name.toLowerCase()) | ||
| ) { | ||
| continue | ||
| } | ||
|
|
||
| let le | ||
| try { | ||
| le = this.fromFile( join(dir, dci.name), relativeFrom) | ||
| } catch (e) { | ||
| continue | ||
| } | ||
| if (le !== undefined) { | ||
| yield le | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /*! | ||
| 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Node-specifics. | ||
| */ | ||
|
|
||
| import {readFileSync} from "fs"; | ||
|
|
||
| import {getMimeTypeForTextFile} from "../_helpers/mime"; | ||
| import {AttachmentEncoding} from "../enums/attachmentEncoding"; | ||
| import {Attachment} from "../models/attachment"; | ||
| import type {MimeType} from "../types/mimeType"; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Node-specific AttachmentFactory. | ||
| */ | ||
| export class AttachmentFactory { | ||
|
|
||
| /** | ||
| * Throws error, if file content could not be read. | ||
| * | ||
| * Content will be base64 encoded. | ||
| */ | ||
| public fromFile(file: string, contentType: MimeType | undefined = undefined): Attachment { | ||
| return new Attachment( | ||
| // may throw if `readFileSync()` fails | ||
| readFileSync(file, {encoding: 'base64'}), | ||
| { | ||
| contentType, | ||
| encoding: AttachmentEncoding.Base64 | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Return an attachment on success. | ||
| * Returns undefined if it appears to be no known text file. | ||
| * Throws error, if content could not be fetched. | ||
| * | ||
| * Tries to guess the file's mime-type. | ||
| */ | ||
| public fromTextFile(file: string): Attachment | undefined { | ||
| const contentType = getMimeTypeForTextFile(file) | ||
| if (contentType === undefined) { | ||
| return undefined | ||
jkowalleck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return this.fromFile(file, contentType) | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.