-
-
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 1 commit
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,37 @@ | ||
| /*! | ||
| 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"; | ||
|
|
||
| export type MimeType = string | ||
|
|
||
| const MAP_TEXT_EXTENSION_MIME: Readonly<Record<string, MimeType>> = { | ||
| '': 'text/plain', | ||
| '.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 | ||
|
|
||
| export function getMimeForTextFile (filename: string): MimeType | undefined { | ||
| return MAP_TEXT_EXTENSION_MIME[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,110 @@ | ||
| /*! | ||
| 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, readFileSync} from "fs"; | ||
| import {join, relative, resolve} from "path"; | ||
|
|
||
| import {getMimeForTextFile} from "../_helpers/mime"; | ||
| import {AttachmentEncoding} from "../enums/attachmentEncoding"; | ||
| import {Attachment} from "../models/attachment"; | ||
| import {NamedLicense} from "../models/license"; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Node-specific LicenseEvidenceBuilder. | ||
| */ | ||
| export class LicenseEvidenceBuilder { | ||
|
|
||
| readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i | ||
|
|
||
| /** | ||
| * Return a license on success, returns undefined if it appears to bes no known text file. | ||
| * Throws errors, if license attachment failed to create. | ||
| * | ||
| * @param file - path to file | ||
| * @param relativeFrom - path the file shall be relative to | ||
| * @returns {@link NamedLicense} on success | ||
| */ | ||
| public fromFile(file: string, relativeFrom: string | undefined = undefined): NamedLicense | undefined { | ||
| const contentType = getMimeForTextFile(file) | ||
| if (contentType === undefined) { | ||
| return undefined | ||
| } | ||
| let lname | ||
| if ( relativeFrom === undefined) { | ||
| lname = `file: ${file}` | ||
| } else { | ||
| // `file` could be absolute or relative path - lets resolve it anyway | ||
| file = resolve(relativeFrom, file) | ||
| lname = `file: ${relative(relativeFrom, file)}` | ||
| } | ||
| return new NamedLicense( | ||
| `file: ${lname}`, | ||
| { | ||
| text: new Attachment( | ||
| // may throw if `readFileSync()` fails | ||
| readFileSync(file).toString('base64'), | ||
| { | ||
| contentType, | ||
| encoding: AttachmentEncoding.Base64 | ||
| } | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Returns a generator for license evidences. | ||
| * Throws errors, if dir cannot be inspected. | ||
| * | ||
| * @param dir - path to inspect | ||
| * @param relativeFrom - path the dir shall be relative to | ||
| */ | ||
| public * fromDir(dir: string, relativeFrom: string | undefined = undefined): Generator<NamedLicense> { | ||
| if ( relativeFrom !== undefined) { | ||
| // `dir` could be absolute or relative path - lets resolve it anyway | ||
| dir = resolve(relativeFrom, dir) | ||
|
||
| } | ||
| // 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 | ||
jkowalleck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsure if this is a good idea ...