From 6186d73da96c1e8651ef5939e5a89026b546bcc2 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 01:26:15 +0100 Subject: [PATCH 01/11] feat: `builders.FromPath.LicenseEvidenceBuilder` Signed-off-by: Jan Kowalleck --- src/_helpers/mime.ts | 37 ++++++++++++ src/builders/fromPath.node.ts | 110 ++++++++++++++++++++++++++++++++++ src/builders/index.node.ts | 1 + 3 files changed, 148 insertions(+) create mode 100644 src/_helpers/mime.ts create mode 100644 src/builders/fromPath.node.ts diff --git a/src/_helpers/mime.ts b/src/_helpers/mime.ts new file mode 100644 index 000000000..9730e9a89 --- /dev/null +++ b/src/_helpers/mime.ts @@ -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> = { + '': '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()] +} diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts new file mode 100644 index 000000000..4a83e7810 --- /dev/null +++ b/src/builders/fromPath.node.ts @@ -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 { + 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 + } + if (le !== undefined) { + yield le + } + } + } + +} diff --git a/src/builders/index.node.ts b/src/builders/index.node.ts index 12bdce998..2f3f9ef8f 100644 --- a/src/builders/index.node.ts +++ b/src/builders/index.node.ts @@ -18,3 +18,4 @@ Copyright (c) OWASP Foundation. All Rights Reserved. */ export * as FromNodePackageJson from './fromNodePackageJson.node' +export * as FromPath from './fromPath.node' From 98973e5045cb46a65600678c87f648333e13b1fb Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 04:01:33 +0100 Subject: [PATCH 02/11] feat Signed-off-by: Jan Kowalleck --- src/builders/fromPath.node.ts | 35 +++++++++------------- src/factories/fromPath.node.ts | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 src/factories/fromPath.node.ts diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts index 4a83e7810..bb28866e6 100644 --- a/src/builders/fromPath.node.ts +++ b/src/builders/fromPath.node.ts @@ -21,12 +21,10 @@ Copyright (c) OWASP Foundation. All Rights Reserved. * Node-specifics. */ -import {readdirSync, readFileSync} from "fs"; +import {readdirSync} from "fs"; import {join, relative, resolve} from "path"; -import {getMimeForTextFile} from "../_helpers/mime"; -import {AttachmentEncoding} from "../enums/attachmentEncoding"; -import {Attachment} from "../models/attachment"; +import type {AttachmentFactory} from "../factories/fromPath.node"; import {NamedLicense} from "../models/license"; @@ -36,6 +34,12 @@ import {NamedLicense} from "../models/license"; */ export class LicenseEvidenceBuilder { + readonly #afac: AttachmentFactory + + constructor(afac: AttachmentFactory) { + this.#afac = afac + } + readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i /** @@ -47,10 +51,6 @@ export class LicenseEvidenceBuilder { * @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}` @@ -59,18 +59,11 @@ export class LicenseEvidenceBuilder { 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 - } - ) - }) + const text = this.#afac.fromTextFile(file) + if (text === undefined) { + return undefined + } + return new NamedLicense(lname, {text}) } /** @@ -78,7 +71,7 @@ export class LicenseEvidenceBuilder { * Throws errors, if dir cannot be inspected. * * @param dir - path to inspect - * @param relativeFrom - path the dir shall be relative to + * @param relativeFrom - path the dir and files shall be relative to */ public * fromDir(dir: string, relativeFrom: string | undefined = undefined): Generator { if ( relativeFrom !== undefined) { diff --git a/src/factories/fromPath.node.ts b/src/factories/fromPath.node.ts new file mode 100644 index 000000000..b15555c12 --- /dev/null +++ b/src/factories/fromPath.node.ts @@ -0,0 +1,55 @@ +/*! +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 {getMimeForTextFile} from "../_helpers/mime"; +import {AttachmentEncoding} from "../enums/attachmentEncoding"; +import {Attachment} from "../models/attachment"; + + + +/** + * Node-specific AttachmentFactory. + */ +export class AttachmentFactory { + + public fromFile(file: string, contentType: string): Attachment { + return new Attachment( + // may throw if `readFileSync()` fails + readFileSync(file).toString('base64'), + { + contentType, + encoding: AttachmentEncoding.Base64 + }) + } + + public fromTextFile(file: string): Attachment | undefined { + const contentType = getMimeForTextFile(file) + if (contentType === undefined) { + return undefined + } + return this.fromFile(file, contentType) + } + +} From 30f19156e1815f64ec512bc410f7979e73070744 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 04:04:31 +0100 Subject: [PATCH 03/11] feat Signed-off-by: Jan Kowalleck --- src/factories/index.node.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/factories/index.node.ts b/src/factories/index.node.ts index 872de4f2f..0cfe916a3 100644 --- a/src/factories/index.node.ts +++ b/src/factories/index.node.ts @@ -22,5 +22,6 @@ export * from './index.common' // region node-specifics export * as FromNodePackageJson from './fromNodePackageJson.node' +export * as FromPath from './fromPath.node' // endregion node-specifics From 09cb566da1e8209928971a5a2039a3ca76512502 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 04:06:59 +0100 Subject: [PATCH 04/11] feat Signed-off-by: Jan Kowalleck --- src/factories/fromPath.node.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/factories/fromPath.node.ts b/src/factories/fromPath.node.ts index b15555c12..713c98f92 100644 --- a/src/factories/fromPath.node.ts +++ b/src/factories/fromPath.node.ts @@ -23,7 +23,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import {readFileSync} from "fs"; -import {getMimeForTextFile} from "../_helpers/mime"; +import {getMimeForTextFile, MimeType} from "../_helpers/mime"; import {AttachmentEncoding} from "../enums/attachmentEncoding"; import {Attachment} from "../models/attachment"; @@ -34,7 +34,7 @@ import {Attachment} from "../models/attachment"; */ export class AttachmentFactory { - public fromFile(file: string, contentType: string): Attachment { + public fromFile(file: string, contentType: MimeType): Attachment { return new Attachment( // may throw if `readFileSync()` fails readFileSync(file).toString('base64'), From e68b2bc57c217cc3ae69092b84eb8669664fe02e Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 04:08:50 +0100 Subject: [PATCH 05/11] feat Signed-off-by: Jan Kowalleck --- src/factories/fromPath.node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/factories/fromPath.node.ts b/src/factories/fromPath.node.ts index 713c98f92..7d347fafd 100644 --- a/src/factories/fromPath.node.ts +++ b/src/factories/fromPath.node.ts @@ -23,7 +23,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import {readFileSync} from "fs"; -import {getMimeForTextFile, MimeType} from "../_helpers/mime"; +import {getMimeForTextFile, type MimeType} from "../_helpers/mime"; import {AttachmentEncoding} from "../enums/attachmentEncoding"; import {Attachment} from "../models/attachment"; From 9a94456bd0fb075937eca97f7cbc2e22b45055fd Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 04:12:06 +0100 Subject: [PATCH 06/11] feat Signed-off-by: Jan Kowalleck --- src/_helpers/mime.ts | 8 ++++---- src/factories/fromPath.node.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/_helpers/mime.ts b/src/_helpers/mime.ts index 9730e9a89..4f099c8bf 100644 --- a/src/_helpers/mime.ts +++ b/src/_helpers/mime.ts @@ -22,8 +22,8 @@ import {extname} from "path"; export type MimeType = string -const MAP_TEXT_EXTENSION_MIME: Readonly> = { - '': 'text/plain', +const MAP_TEXT_EXTENSION_MIMETYPE: Readonly> = { + '': 'text/plain', // our scope is text! '.licence': 'text/plain', '.license': 'text/plain', '.md': 'text/markdown', @@ -32,6 +32,6 @@ const MAP_TEXT_EXTENSION_MIME: Readonly> = { '.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()] +export function getMimeTypeForTextFile (filename: string): MimeType | undefined { + return MAP_TEXT_EXTENSION_MIMETYPE[extname(filename).toLowerCase()] } diff --git a/src/factories/fromPath.node.ts b/src/factories/fromPath.node.ts index 7d347fafd..3d01d0fc0 100644 --- a/src/factories/fromPath.node.ts +++ b/src/factories/fromPath.node.ts @@ -23,7 +23,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import {readFileSync} from "fs"; -import {getMimeForTextFile, type MimeType} from "../_helpers/mime"; +import {getMimeTypeForTextFile, type MimeType} from "../_helpers/mime"; import {AttachmentEncoding} from "../enums/attachmentEncoding"; import {Attachment} from "../models/attachment"; @@ -45,7 +45,7 @@ export class AttachmentFactory { } public fromTextFile(file: string): Attachment | undefined { - const contentType = getMimeForTextFile(file) + const contentType = getMimeTypeForTextFile(file) if (contentType === undefined) { return undefined } From 3ec62c77ffeb2a2ceae36cd36478e9001050c03c Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 04:19:16 +0100 Subject: [PATCH 07/11] feat Signed-off-by: Jan Kowalleck --- src/builders/fromPath.node.ts | 12 ++++++------ src/factories/fromPath.node.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts index bb28866e6..33a0c3f23 100644 --- a/src/builders/fromPath.node.ts +++ b/src/builders/fromPath.node.ts @@ -44,31 +44,31 @@ export class LicenseEvidenceBuilder { /** * Return a license on success, returns undefined if it appears to bes no known text file. - * Throws errors, if license attachment failed to create. + * Throws error, if license attachment content could not be fetched. * * @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 { - let lname + let name if ( relativeFrom === undefined) { - lname = `file: ${file}` + name = `file: ${file}` } else { // `file` could be absolute or relative path - lets resolve it anyway file = resolve(relativeFrom, file) - lname = `file: ${relative(relativeFrom, file)}` + name = `file: ${relative(relativeFrom, file)}` } const text = this.#afac.fromTextFile(file) if (text === undefined) { return undefined } - return new NamedLicense(lname, {text}) + return new NamedLicense(name, {text}) } /** * Returns a generator for license evidences. - * Throws errors, if dir cannot be inspected. + * Throws error, if dir content could not be inspected. * * @param dir - path to inspect * @param relativeFrom - path the dir and files shall be relative to diff --git a/src/factories/fromPath.node.ts b/src/factories/fromPath.node.ts index 3d01d0fc0..eb7feecf6 100644 --- a/src/factories/fromPath.node.ts +++ b/src/factories/fromPath.node.ts @@ -34,6 +34,12 @@ import {Attachment} from "../models/attachment"; */ export class AttachmentFactory { + /** + * Return an attachment on success. + * Throws error, if content could not be fetched. + * + * @returns {@link NamedLicense} on success + */ public fromFile(file: string, contentType: MimeType): Attachment { return new Attachment( // may throw if `readFileSync()` fails @@ -44,6 +50,12 @@ export class AttachmentFactory { }) } + /** + * Return an attachment on success, returns undefined if it appears to bes no known text file. + * Throws error, if content could not be fetched. + * + * @returns {@link Attachment} on success + */ public fromTextFile(file: string): Attachment | undefined { const contentType = getMimeTypeForTextFile(file) if (contentType === undefined) { From de97015f5af23012560b67b5cc0dd5251aba1708 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 10:41:18 +0100 Subject: [PATCH 08/11] feat Signed-off-by: Jan Kowalleck --- src/_helpers/mime.ts | 9 ++++++++- src/builders/fromPath.node.ts | 30 ++++++++++++++++++------------ src/factories/fromPath.node.ts | 17 +++++++++-------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/_helpers/mime.ts b/src/_helpers/mime.ts index 4f099c8bf..5dbdc26b2 100644 --- a/src/_helpers/mime.ts +++ b/src/_helpers/mime.ts @@ -20,10 +20,13 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import {extname} from "path"; -export type MimeType = string +import type {MimeType} from "../types/mimeType"; const MAP_TEXT_EXTENSION_MIMETYPE: Readonly> = { '': 'text/plain', // our scope is text! + '.csv': 'text/csv', + '.htm': 'text/html', + '.html': 'text/html', '.licence': 'text/plain', '.license': 'text/plain', '.md': 'text/markdown', @@ -32,6 +35,10 @@ const MAP_TEXT_EXTENSION_MIMETYPE: Readonly> = { '.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()] } diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts index 33a0c3f23..401e90dce 100644 --- a/src/builders/fromPath.node.ts +++ b/src/builders/fromPath.node.ts @@ -24,7 +24,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import {readdirSync} from "fs"; import {join, relative, resolve} from "path"; -import type {AttachmentFactory} from "../factories/fromPath.node"; +import type * as Factories from "../factories/index.node"; import {NamedLicense} from "../models/license"; @@ -34,21 +34,23 @@ import {NamedLicense} from "../models/license"; */ export class LicenseEvidenceBuilder { - readonly #afac: AttachmentFactory + readonly #attachmentFactory: Factories.FromPath.AttachmentFactory - constructor(afac: AttachmentFactory) { - this.#afac = afac + constructor(attachmentFactory: LicenseEvidenceBuilder['attachmentFactory']) { + this.#attachmentFactory = attachmentFactory } - readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i + get attachmentFactory (): Factories.FromPath.AttachmentFactory { + return this.#attachmentFactory + } /** - * Return a license on success, returns undefined if it appears to bes no known text file. + * 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. * * @param file - path to file - * @param relativeFrom - path the file shall be relative to - * @returns {@link NamedLicense} on success + * @param relativeFrom - path the file shall be relative from */ public fromFile(file: string, relativeFrom: string | undefined = undefined): NamedLicense | undefined { let name @@ -59,19 +61,23 @@ export class LicenseEvidenceBuilder { file = resolve(relativeFrom, file) name = `file: ${relative(relativeFrom, file)}` } - const text = this.#afac.fromTextFile(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. - * Throws error, if dir content could not be inspected. + * 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 - path the dir and files shall be relative to + * @param relativeFrom - path the dir and files shall be relative from */ public * fromDir(dir: string, relativeFrom: string | undefined = undefined): Generator { if ( relativeFrom !== undefined) { diff --git a/src/factories/fromPath.node.ts b/src/factories/fromPath.node.ts index eb7feecf6..5a35cfd63 100644 --- a/src/factories/fromPath.node.ts +++ b/src/factories/fromPath.node.ts @@ -23,9 +23,10 @@ Copyright (c) OWASP Foundation. All Rights Reserved. import {readFileSync} from "fs"; -import {getMimeTypeForTextFile, type MimeType} from "../_helpers/mime"; +import {getMimeTypeForTextFile} from "../_helpers/mime"; import {AttachmentEncoding} from "../enums/attachmentEncoding"; import {Attachment} from "../models/attachment"; +import type {MimeType} from "../types/mimeType"; @@ -35,15 +36,14 @@ import {Attachment} from "../models/attachment"; export class AttachmentFactory { /** - * Return an attachment on success. - * Throws error, if content could not be fetched. + * Throws error, if file content could not be read. * - * @returns {@link NamedLicense} on success + * Content will be base64 encoded. */ - public fromFile(file: string, contentType: MimeType): Attachment { + public fromFile(file: string, contentType: MimeType | undefined = undefined): Attachment { return new Attachment( // may throw if `readFileSync()` fails - readFileSync(file).toString('base64'), + readFileSync(file, {encoding: 'base64'}), { contentType, encoding: AttachmentEncoding.Base64 @@ -51,10 +51,11 @@ export class AttachmentFactory { } /** - * Return an attachment on success, returns undefined if it appears to bes no known text file. + * Return an attachment on success. + * Returns undefined if it appears to be no known text file. * Throws error, if content could not be fetched. * - * @returns {@link Attachment} on success + * Tries to guess the file's mime-type. */ public fromTextFile(file: string): Attachment | undefined { const contentType = getMimeTypeForTextFile(file) From a9e5615f85d8e4dbb0aa60e2137aba99093b2a2b Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 10:49:35 +0100 Subject: [PATCH 09/11] feat Signed-off-by: Jan Kowalleck --- src/builders/fromPath.node.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts index 401e90dce..a47a864a5 100644 --- a/src/builders/fromPath.node.ts +++ b/src/builders/fromPath.node.ts @@ -50,7 +50,7 @@ export class LicenseEvidenceBuilder { * Throws error, if license attachment content could not be fetched. * * @param file - path to file - * @param relativeFrom - path the file shall be relative from + * @param relativeFrom - relative path reference for file display */ public fromFile(file: string, relativeFrom: string | undefined = undefined): NamedLicense | undefined { let name @@ -58,7 +58,6 @@ export class LicenseEvidenceBuilder { name = `file: ${file}` } else { // `file` could be absolute or relative path - lets resolve it anyway - file = resolve(relativeFrom, file) name = `file: ${relative(relativeFrom, file)}` } const text = this.#attachmentFactory.fromTextFile(file) @@ -77,13 +76,9 @@ export class LicenseEvidenceBuilder { * Unreadable files will be omitted. * * @param dir - path to inspect - * @param relativeFrom - path the dir and files shall be relative from + * @param relativeFrom - relative path reference for file display */ public * fromDir(dir: string, relativeFrom: string | undefined = undefined): Generator { - 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) { From d80368ce4520096b08f5c86b081bd8f4bf844dca Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 10:55:09 +0100 Subject: [PATCH 10/11] feat Signed-off-by: Jan Kowalleck --- src/builders/fromPath.node.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts index a47a864a5..c42d74f52 100644 --- a/src/builders/fromPath.node.ts +++ b/src/builders/fromPath.node.ts @@ -22,7 +22,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved. */ import {readdirSync} from "fs"; -import {join, relative, resolve} from "path"; +import {join, relative} from "path"; import type * as Factories from "../factories/index.node"; import {NamedLicense} from "../models/license"; @@ -49,6 +49,9 @@ export class LicenseEvidenceBuilder { * 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 full file name is displayed. + * * @param file - path to file * @param relativeFrom - relative path reference for file display */ @@ -77,6 +80,10 @@ export class LicenseEvidenceBuilder { * * @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 { // may throw if `readdirSync()` fails From 4d9a1002b10baebad65efcd2cddf6bf1d334c4ee Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 30 Oct 2024 10:56:48 +0100 Subject: [PATCH 11/11] feat Signed-off-by: Jan Kowalleck --- src/builders/fromPath.node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builders/fromPath.node.ts b/src/builders/fromPath.node.ts index c42d74f52..2dea23a14 100644 --- a/src/builders/fromPath.node.ts +++ b/src/builders/fromPath.node.ts @@ -50,7 +50,7 @@ export class LicenseEvidenceBuilder { * Throws error, if license attachment content could not be fetched. * * If `relativeFrom` is given, the file is displayed relative from there, - * else the full file name is displayed. + * else the file is displayed unmodified. * * @param file - path to file * @param relativeFrom - relative path reference for file display