Skip to content

Commit 18e7034

Browse files
authored
refactor: helpers (#1313)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 77df409 commit 18e7034

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/_helpers.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,35 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
import { existsSync, readdirSync, readFileSync } from 'fs'
2121
import { dirname, extname, isAbsolute, join, sep } from 'path'
2222

23+
export function isNonNullable<T> (value: T): value is NonNullable<T> {
24+
// NonNullable: not null and not undefined
25+
return value !== null && value !== undefined
26+
}
27+
28+
export const structuredClonePolyfill: <T>(value: T) => T = typeof structuredClone === 'function'
29+
? structuredClone
30+
: function (value) { return JSON.parse(JSON.stringify(value)) }
31+
2332
export interface PackageDescription {
2433
path: string
2534
packageJson: any
2635
}
2736

37+
const PACKAGE_MANIFEST_FILENAME = 'package.json'
38+
2839
export function getPackageDescription (path: string): PackageDescription | undefined {
2940
const isSubDirOfNodeModules = isSubDirectoryOfNodeModulesFolder(path)
3041

3142
while (isAbsolute(path)) {
32-
const pathToPackageJson = join(path, 'package.json')
43+
const pathToPackageJson = join(path, PACKAGE_MANIFEST_FILENAME)
3344
if (existsSync(pathToPackageJson)) {
3445
try {
3546
const contentOfPackageJson = loadJsonFile(pathToPackageJson) ?? {}
3647
// only look for valid candidate if we are in a node_modules subdirectory
3748
if (!isSubDirOfNodeModules || isValidPackageJSON(contentOfPackageJson)) {
3849
return {
3950
path: pathToPackageJson,
40-
packageJson: loadJsonFile(pathToPackageJson) ?? {}
51+
packageJson: contentOfPackageJson
4152
}
4253
}
4354
} catch {
@@ -54,12 +65,14 @@ export function getPackageDescription (path: string): PackageDescription | undef
5465
return undefined
5566
}
5667

68+
const NODE_MODULES_FOLDERNAME = 'node_modules'
69+
5770
function isNodeModulesFolder (path: string): boolean {
58-
return path.endsWith(`${sep}node_modules`)
71+
return path.endsWith(`${sep}${NODE_MODULES_FOLDERNAME}`)
5972
}
6073

6174
function isSubDirectoryOfNodeModulesFolder (path: string): boolean {
62-
return path.includes(`${sep}node_modules${sep}`)
75+
return path.includes(`${sep}${NODE_MODULES_FOLDERNAME}${sep}`)
6376
}
6477

6578
export function isValidPackageJSON (pkg: any): boolean {

src/extractor.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import * as CDX from '@cyclonedx/cyclonedx-library'
2121
import { readFileSync } from 'fs'
2222
import * as normalizePackageJson from 'normalize-package-data'
2323
import { basename, dirname } from 'path'
24-
import { type Compilation, type Module } from 'webpack'
24+
import type { Compilation, Module } from 'webpack'
2525

26-
import { getPackageDescription, type PackageDescription, searchEvidenceSources } from './_helpers'
26+
import { getPackageDescription, isNonNullable, type PackageDescription, searchEvidenceSources, structuredClonePolyfill } from './_helpers'
2727

2828
type WebpackLogger = Compilation['logger']
2929

@@ -158,12 +158,3 @@ export class Extractor {
158158
return cdxComponentEvidence
159159
}
160160
}
161-
162-
function isNonNullable<T> (value: T): value is NonNullable<T> {
163-
// NonNullable: not null and not undefined
164-
return value !== null && value !== undefined
165-
}
166-
167-
const structuredClonePolyfill: <T>(value: T) => T = typeof structuredClone === 'function'
168-
? structuredClone
169-
: function (value) { return JSON.parse(JSON.stringify(value)) }

0 commit comments

Comments
 (0)