-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add license file gatherer utility #1249
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
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
2188ac3
feat: license file gathering
jkowalleck a2da270
feat: license file gathering
jkowalleck 028dc6c
feat: license file gathering
jkowalleck 51da56a
feat: license file gathering
jkowalleck e096713
feat: license file gathering
jkowalleck b8d2fd0
feat: license file gathering
jkowalleck 626fdb1
feat: license file gathering
jkowalleck 49a08c6
feat: license file gathering
jkowalleck 54e22ed
feat: license file gathering
jkowalleck a1a7b81
feat: license file gathering
jkowalleck d754f77
feat: license file gathering
jkowalleck 110fb06
feat: license file gathering
jkowalleck dffd67c
feat: license file gathering
jkowalleck 8bbbdfe
wip
jkowalleck 7d7c112
wip
jkowalleck 9681a3a
wip
jkowalleck 3dabf6d
wip
jkowalleck cf80a9d
Merge remote-tracking branch 'origin/main' into feat/license-file-gat…
jkowalleck 7c516b4
wip
jkowalleck 1dcad1a
wip
jkowalleck c17b4bc
wip
jkowalleck 2205fee
wip
jkowalleck 61bf083
wip
jkowalleck 706918d
wip
jkowalleck 4813b25
wip
jkowalleck f42a690
wip
jkowalleck fe64d90
wip
jkowalleck f1d02ec
tests
jkowalleck 2296edf
tests
jkowalleck 0fedd41
tests
jkowalleck a707089
tests
jkowalleck 5aa4057
tests
jkowalleck 2b86c5e
tests
jkowalleck d97ae31
tests
jkowalleck 3f2bf42
tests
jkowalleck 174232e
tests
jkowalleck 7e0bbc3
tests
jkowalleck 74db0a4
tests
jkowalleck fd30cce
tests
jkowalleck 7d793ef
tests
jkowalleck fdf0ad7
docs
jkowalleck 37857b9
docs
jkowalleck 8fde217
docs
jkowalleck 7ef897d
Merge remote-tracking branch 'origin/main' into feat/license-file-gat…
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
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,57 @@ | ||
| /*! | ||
| 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 { parse as parsePath } from 'node:path' | ||
|
|
||
| type MimeType = string | ||
|
|
||
| const MIMETYPE_TEXT_PLAIN: MimeType = 'text/plain' | ||
|
|
||
| const MAP_TEXT_EXTENSION_MIMETYPE: Readonly<Record<string, MimeType>> = { | ||
| '': MIMETYPE_TEXT_PLAIN, | ||
| // https://www.iana.org/assignments/media-types/media-types.xhtml | ||
| '.csv': 'text/csv', | ||
| '.htm': 'text/html', | ||
| '.html': 'text/html', | ||
| '.md': 'text/markdown', | ||
| '.txt': MIMETYPE_TEXT_PLAIN, | ||
| '.rst': 'text/prs.fallenstein.rst', | ||
| '.rtf': 'application/rtf', // our scope is text, yes, but RTF is binary - so we should base64 encode it ... | ||
| '.xml': 'text/xml', // not `application/xml` -- our scope is text! | ||
| // add more mime types above this line. pull-requests welcome! | ||
| // license-specific files | ||
| '.license': MIMETYPE_TEXT_PLAIN, | ||
| '.licence': MIMETYPE_TEXT_PLAIN, | ||
| } as const | ||
|
|
||
| const LICENSE_FILENAME_BASE: Readonly<Set<string>> = new Set(['licence', 'license']) | ||
| const LICENSE_FILENAME_EXT: Readonly<Set<string>> = new Set([ | ||
| '.apache', | ||
| '.bsd', | ||
| '.gpl', | ||
| '.mit', | ||
| // to be continued ... pullrequests welcome | ||
| ]) | ||
|
|
||
| export function guessMimeTypeForLicenseFile (filename: string): MimeType | undefined { | ||
| const {name, ext} = parsePath(filename.toLowerCase()) | ||
| return LICENSE_FILENAME_BASE.has(name) && LICENSE_FILENAME_EXT.has(ext) | ||
| ? MIMETYPE_TEXT_PLAIN | ||
| : MAP_TEXT_EXTENSION_MIMETYPE[ext] | ||
| } |
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,104 @@ | ||
| /*! | ||
| 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. | ||
| */ | ||
|
|
||
| /** | ||
| * This module tries to be as compatible as possible, it only uses basic methods that are known to be working in all FileSystem abstraction-layers. | ||
| * In addition, we use type parameters for all `PathLike`s, so downstream users can utilize their implementations accordingly. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| import type { Stats } from 'node:fs' | ||
|
|
||
| import { guessMimeTypeForLicenseFile } from '../_helpers/mime.node' | ||
| import { AttachmentEncoding } from '../enums/attachmentEncoding' | ||
| import { Attachment } from '../models/attachment' | ||
|
|
||
| export interface FsUtils<P extends string> { | ||
| readdirSync: (path: P ) => P[] | ||
| readFileSync: (path: P) => Buffer | ||
| statSync: (path: P) => Stats | ||
| } | ||
|
|
||
| export interface PathUtils<P extends string> { | ||
| join: (...paths: P[]) => P | ||
| } | ||
|
|
||
| export interface FileAttachment<P extends string> { | ||
| filePath: P | ||
| file: P | ||
| text: Attachment | ||
| } | ||
|
|
||
| const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i | ||
|
|
||
| export type ErrorReporter = (e:Error) => any | ||
|
|
||
| export class LicenseEvidenceGatherer<P extends string = string> { | ||
| readonly #fs: FsUtils<P> | ||
| readonly #path: PathUtils<P> | ||
|
|
||
| /* eslint-disable tsdoc/syntax -- we want to use the dot-syntax - https://github.com/microsoft/tsdoc/issues/19 */ | ||
| /** | ||
| * `fs` and `path` can be supplied, if any compatibility-layer or drop-in replacement is used. | ||
| * | ||
| * @param options.fs - If omitted, the native `node:fs` is used. | ||
| * @param options.path - If omitted, the native `node:path` is used. | ||
| */ | ||
| constructor (options: { fs?: FsUtils<P>, path?: PathUtils<P> } = {}) { | ||
| /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports -- needed */ | ||
| this.#fs = options.fs ?? require('node:fs') | ||
| this.#path = options.path ?? require('node:path') | ||
| /* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports */ | ||
| } | ||
| /* eslint-enable tsdoc/syntax */ | ||
|
|
||
| * getFileAttachments (prefixPath: P, onError: ErrorReporter = noop): Generator<FileAttachment<P>> { | ||
| const files = this.#fs.readdirSync(prefixPath) // may throw | ||
| for (const file of files) { | ||
| if (!LICENSE_FILENAME_PATTERN.test(file)) { | ||
| continue | ||
| } | ||
| const filePath = this.#path.join(prefixPath, file) | ||
| if (!this.#fs.statSync(filePath).isFile()) { | ||
| // Ignore all directories - they are not files :-) | ||
| // Don't follow symlinks for security reasons! | ||
| continue | ||
| } | ||
| const contentType = guessMimeTypeForLicenseFile(file) | ||
| if (contentType === undefined) { | ||
| continue | ||
| } | ||
| try { | ||
| yield { filePath, file, text: new Attachment( | ||
| // since we cannot be sure weather the file content is text-only, or maybe binary, | ||
| // we tend to base64 everything, regardless of the detected encoding. | ||
| this.#fs.readFileSync(filePath) // may throw | ||
| .toString('base64'), | ||
| { contentType, encoding: AttachmentEncoding.Base64 } | ||
| ) } | ||
| } catch (cause) { | ||
| onError(new Error(`skipped license file ${filePath}`, {cause})) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* eslint-disable-next-line @typescript-eslint/no-empty-function -- ack */ | ||
| function noop ():void {} | ||
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.