Skip to content

Commit 4a3faab

Browse files
authored
refactor: normalizePackageManifest type assert (#1403)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 4a4f841 commit 4a3faab

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

src/_helpers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
import { existsSync, readFileSync } from 'node:fs'
2121
import { dirname, isAbsolute, join, sep } from 'node:path'
2222

23+
import normalizePackageData from 'normalize-package-data'
24+
25+
2326
export function isNonNullable<T>(value: T): value is NonNullable<T> {
2427
// NonNullable: not null and not undefined
2528
return value !== null && value !== undefined
@@ -116,3 +119,22 @@ export function iterableSome<T>(i: Iterable<T>, t: (v: T) => boolean): boolean {
116119
}
117120

118121
// endregion polyfills
122+
123+
124+
export function isString (v: any): v is string {
125+
return typeof v === 'string'
126+
}
127+
128+
export function normalizePackageManifest (data: any, warn?: normalizePackageData.WarnFn): asserts data is normalizePackageData.Package {
129+
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access -- ack*/
130+
const oVersion = data.version
131+
132+
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ack */
133+
normalizePackageData(data as normalizePackageData.Input, warn)
134+
135+
if (isString(oVersion)) {
136+
// normalizer might have stripped version or sanitized it to SemVer -- we want the original
137+
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- ack */
138+
data.version = oVersion.trim()
139+
}
140+
}

src/extractor.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
import { dirname } from 'node:path'
2121

2222
import * as CDX from '@cyclonedx/cyclonedx-library'
23-
import normalizePackageJson from 'normalize-package-data'
2423
import type { Compilation, Module } from 'webpack'
2524

2625
import {
2726
getPackageDescription,
2827
isNonNullable,
28+
normalizePackageManifest,
2929
type PackageDescription,
30-
structuredClonePolyfill
31-
} from './_helpers'
30+
structuredClonePolyfill} from './_helpers'
3231

3332
type WebpackLogger = Compilation['logger']
3433

@@ -93,29 +92,19 @@ export class Extractor {
9392
*/
9493
makeComponent (pkg: PackageDescription, collectEvidence: boolean, logger?: WebpackLogger): CDX.Models.Component {
9594
try {
96-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- expected */
95+
// work with a deep copy, because `normalizePackageManifest()` might modify the data
96+
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- ach */
9797
const _packageJson = structuredClonePolyfill(pkg.packageJson)
98-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hont */
99-
normalizePackageJson(_packageJson as normalizePackageJson.Input /* add debug for warnings? */)
100-
// region fix normalizations
101-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- expected */
102-
if (typeof pkg.packageJson.version === 'string') {
103-
// allow non-SemVer strings
104-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
105-
, @typescript-eslint/no-unsafe-type-assertion
106-
-- hint hint */
107-
_packageJson.version = (pkg.packageJson.version as string).trim()
108-
}
109-
// endregion fix normalizations
110-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hint */
111-
pkg.packageJson = _packageJson as normalizePackageJson.Package
98+
normalizePackageManifest(_packageJson)
99+
pkg.packageJson = _packageJson
112100
} catch (e) {
113101
logger?.warn('normalizePackageJson from PkgPath', pkg.path, 'failed:', e)
114102
}
115103

116104
const component = this.#componentBuilder.makeComponent(
117-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hint */
118-
pkg.packageJson as normalizePackageJson.Package)
105+
/* @ts-expect-error TS2559 */
106+
pkg.packageJson as PackageDescription) /* eslint-disable-line @typescript-eslint/no-unsafe-type-assertion -- ack */
107+
119108
if (component === undefined) {
120109
throw new Error(`failed building Component from PkgPath ${pkg.path}`)
121110
}

src/plugin.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import { existsSync } from 'node:fs'
2121
import { join as joinPath, resolve } from 'node:path'
2222

2323
import * as CDX from '@cyclonedx/cyclonedx-library'
24-
import normalizePackageJson from 'normalize-package-data'
2524
import { Compilation, type Compiler, sources, version as WEBPACK_VERSION } from 'webpack'
2625

27-
import { getPackageDescription, iterableSome, loadJsonFile, type PackageDescription } from './_helpers'
26+
import {
27+
getPackageDescription,
28+
iterableSome,
29+
loadJsonFile,
30+
normalizePackageManifest,
31+
type PackageDescription
32+
} from './_helpers'
2833
import { Extractor } from './extractor'
2934

3035
type WebpackLogger = Compilation['logger']
@@ -381,13 +386,13 @@ export class CycloneDxWebpackPlugin {
381386
? getPackageDescription(path)?.packageJson
382387
: { name: this.rootComponentName, version: this.rootComponentVersion }
383388
if (thisPackageJson === undefined) { return undefined }
384-
normalizePackageJson(
385-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hint */
386-
thisPackageJson as normalizePackageJson.Input,
389+
normalizePackageManifest(
390+
391+
thisPackageJson,
387392
w => { logger.debug('normalizePackageJson from PkgPath', path, 'caused:', w) }
388393
)
389-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hint */
390-
return builder.makeComponent(thisPackageJson as normalizePackageJson.Package)
394+
395+
return builder.makeComponent(thisPackageJson)
391396
}
392397

393398
#finalizeBom (
@@ -447,15 +452,11 @@ export class CycloneDxWebpackPlugin {
447452
logger.log('try to build new Tool from PkgPath', packageJsonPath)
448453
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- expected */
449454
const packageJson: PackageDescription['packageJson'] = loadJsonFile(packageJsonPath) ?? {}
450-
normalizePackageJson(
451-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hint */
452-
packageJson as normalizePackageJson.Input,
455+
normalizePackageManifest(
456+
packageJson,
453457
w => { logger.debug('normalizePackageJson from PkgPath', packageJsonPath, 'caused:', w) }
454458
)
455-
const tool = builder.makeComponent(
456-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- hint hint */
457-
packageJson as normalizePackageJson.Package,
458-
cType)
459+
const tool = builder.makeComponent(packageJson, cType)
459460
if (tool !== undefined) {
460461
yield tool
461462
}

0 commit comments

Comments
 (0)