Skip to content

Commit de97015

Browse files
committed
feat
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 3ec62c7 commit de97015

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

src/_helpers/mime.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020

2121
import {extname} from "path";
2222

23-
export type MimeType = string
23+
import type {MimeType} from "../types/mimeType";
2424

2525
const MAP_TEXT_EXTENSION_MIMETYPE: Readonly<Record<string, MimeType>> = {
2626
'': 'text/plain', // our scope is text!
27+
'.csv': 'text/csv',
28+
'.htm': 'text/html',
29+
'.html': 'text/html',
2730
'.licence': 'text/plain',
2831
'.license': 'text/plain',
2932
'.md': 'text/markdown',
@@ -32,6 +35,10 @@ const MAP_TEXT_EXTENSION_MIMETYPE: Readonly<Record<string, MimeType>> = {
3235
'.xml': 'text/xml' // not `application/xml` -- our scope is text!
3336
} as const
3437

38+
/**
39+
* Returns the guessed mime type of file, based on file name extension.
40+
* Returns undefined if tile extension is unknown.
41+
*/
3542
export function getMimeTypeForTextFile (filename: string): MimeType | undefined {
3643
return MAP_TEXT_EXTENSION_MIMETYPE[extname(filename).toLowerCase()]
3744
}

src/builders/fromPath.node.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2424
import {readdirSync} from "fs";
2525
import {join, relative, resolve} from "path";
2626

27-
import type {AttachmentFactory} from "../factories/fromPath.node";
27+
import type * as Factories from "../factories/index.node";
2828
import {NamedLicense} from "../models/license";
2929

3030

@@ -34,21 +34,23 @@ import {NamedLicense} from "../models/license";
3434
*/
3535
export class LicenseEvidenceBuilder {
3636

37-
readonly #afac: AttachmentFactory
37+
readonly #attachmentFactory: Factories.FromPath.AttachmentFactory
3838

39-
constructor(afac: AttachmentFactory) {
40-
this.#afac = afac
39+
constructor(attachmentFactory: LicenseEvidenceBuilder['attachmentFactory']) {
40+
this.#attachmentFactory = attachmentFactory
4141
}
4242

43-
readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
43+
get attachmentFactory (): Factories.FromPath.AttachmentFactory {
44+
return this.#attachmentFactory
45+
}
4446

4547
/**
46-
* Return a license on success, returns undefined if it appears to bes no known text file.
48+
* Return a license on success.
49+
* Returns undefined if it appears to bes no known text file.
4750
* Throws error, if license attachment content could not be fetched.
4851
*
4952
* @param file - path to file
50-
* @param relativeFrom - path the file shall be relative to
51-
* @returns {@link NamedLicense} on success
53+
* @param relativeFrom - path the file shall be relative from
5254
*/
5355
public fromFile(file: string, relativeFrom: string | undefined = undefined): NamedLicense | undefined {
5456
let name
@@ -59,19 +61,23 @@ export class LicenseEvidenceBuilder {
5961
file = resolve(relativeFrom, file)
6062
name = `file: ${relative(relativeFrom, file)}`
6163
}
62-
const text = this.#afac.fromTextFile(file)
64+
const text = this.#attachmentFactory.fromTextFile(file)
6365
if (text === undefined) {
6466
return undefined
6567
}
6668
return new NamedLicense(name, {text})
6769
}
6870

71+
readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
72+
6973
/**
70-
* Returns a generator for license evidences.
71-
* Throws error, if dir content could not be inspected.
74+
* Returns a generator for license evidences in a directory.
75+
* Throws error, if directory content could not be inspected.
76+
*
77+
* Unreadable files will be omitted.
7278
*
7379
* @param dir - path to inspect
74-
* @param relativeFrom - path the dir and files shall be relative to
80+
* @param relativeFrom - path the dir and files shall be relative from
7581
*/
7682
public * fromDir(dir: string, relativeFrom: string | undefined = undefined): Generator<NamedLicense> {
7783
if ( relativeFrom !== undefined) {

src/factories/fromPath.node.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2323

2424
import {readFileSync} from "fs";
2525

26-
import {getMimeTypeForTextFile, type MimeType} from "../_helpers/mime";
26+
import {getMimeTypeForTextFile} from "../_helpers/mime";
2727
import {AttachmentEncoding} from "../enums/attachmentEncoding";
2828
import {Attachment} from "../models/attachment";
29+
import type {MimeType} from "../types/mimeType";
2930

3031

3132

@@ -35,26 +36,26 @@ import {Attachment} from "../models/attachment";
3536
export class AttachmentFactory {
3637

3738
/**
38-
* Return an attachment on success.
39-
* Throws error, if content could not be fetched.
39+
* Throws error, if file content could not be read.
4040
*
41-
* @returns {@link NamedLicense} on success
41+
* Content will be base64 encoded.
4242
*/
43-
public fromFile(file: string, contentType: MimeType): Attachment {
43+
public fromFile(file: string, contentType: MimeType | undefined = undefined): Attachment {
4444
return new Attachment(
4545
// may throw if `readFileSync()` fails
46-
readFileSync(file).toString('base64'),
46+
readFileSync(file, {encoding: 'base64'}),
4747
{
4848
contentType,
4949
encoding: AttachmentEncoding.Base64
5050
})
5151
}
5252

5353
/**
54-
* Return an attachment on success, returns undefined if it appears to bes no known text file.
54+
* Return an attachment on success.
55+
* Returns undefined if it appears to be no known text file.
5556
* Throws error, if content could not be fetched.
5657
*
57-
* @returns {@link Attachment} on success
58+
* Tries to guess the file's mime-type.
5859
*/
5960
public fromTextFile(file: string): Attachment | undefined {
6061
const contentType = getMimeTypeForTextFile(file)

0 commit comments

Comments
 (0)