Skip to content

Commit 9681a3a

Browse files
committed
wip
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 7d7c112 commit 9681a3a

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/utils/licenseUtility.node.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,39 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import type NATIVE_FS from 'node:fs'
21-
import type NATIVE_PATH from "node:path";
2221

2322
import { getMimeTypeForLicenseFile } from '../_helpers/mime.node'
2423
import { AttachmentEncoding } from '../enums/attachmentEncoding'
2524
import { Attachment } from '../models/attachment'
2625

26+
/*
27+
* this module tries to be as compatible as possible - it only uses basic methods that are known to be working in all FS-abstraction-layers.
28+
* In addition, we use type-vars for all PathLikes, so downstream users can utilize their implementations accordingly.
29+
*/
2730

28-
interface FsUtils {
29-
readdirSync: typeof NATIVE_FS.readdirSync
30-
readFileSync: typeof NATIVE_FS.readFileSync
31-
statSync: typeof NATIVE_FS.statSync
31+
export interface FsUtils<P extends string = string> {
32+
readdirSync: (path: P ) => P[]
33+
readFileSync: (path: P) => Buffer
34+
statSync: (path: P) => NATIVE_FS.Stats
3235
}
3336

34-
interface PathUtils {
35-
join: typeof NATIVE_PATH.join
37+
export interface PathUtils<P extends string> {
38+
join: (...paths: P[]) => P
3639
}
3740

38-
export interface FetchedAttachmentResult {
39-
filePath: string
40-
file: string
41+
export interface FetchedAttachmentResult<P extends string> {
42+
filePath: P
43+
file: P
4144
text: Attachment
4245
}
4346

4447
const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
4548

46-
type ErrorReporter = (e:Error) => any
49+
export type ErrorReporter = (e:Error) => any
4750

48-
/* eslint-disable-next-line @typescript-eslint/no-empty-function -- ack */
49-
function noop ():void {}
50-
51-
export class LicenseEvidenceFetcher {
52-
readonly #fs: FsUtils
53-
readonly #path: PathUtils
51+
export class LicenseEvidenceFetcher<P extends string = string> {
52+
readonly #fs: FsUtils<P>
53+
readonly #path: PathUtils<P>
5454

5555
/* eslint-disable tsdoc/syntax -- we want to use the dot-syntax - https://github.com/microsoft/tsdoc/issues/19 */
5656
/**
@@ -60,14 +60,14 @@ export class LicenseEvidenceFetcher {
6060
* @param options.path - If omitted, the native `node:path` is used.
6161
*/
6262
/* eslint-enable tsdoc/syntax */
63-
constructor (options: { fs?: FsUtils, path?: PathUtils } = {}) {
63+
constructor (options: { fs?: FsUtils<P>, path?: PathUtils<P> } = {}) {
6464
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports -- needed */
6565
this.#fs = options.fs ?? require('node:fs')
6666
this.#path = options.path ?? require('node:path')
6767
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports */
6868
}
6969

70-
* fetchAsAttachment (prefixPath: string, onError: ErrorReporter = noop): Generator<FetchedAttachmentResult> {
70+
* fetchAsAttachment (prefixPath: P, onError: ErrorReporter = noop): Generator<FetchedAttachmentResult<P>> {
7171
const files = this.#fs.readdirSync(prefixPath) // may throw
7272
for (const file of files) {
7373
if (!LICENSE_FILENAME_PATTERN.test(file)) {
@@ -94,3 +94,8 @@ export class LicenseEvidenceFetcher {
9494
}
9595
}
9696
}
97+
98+
99+
100+
/* eslint-disable-next-line @typescript-eslint/no-empty-function -- ack */
101+
function noop ():void {}

0 commit comments

Comments
 (0)