Skip to content

Commit 7cf5337

Browse files
authored
fix: package.JSON split name/group with unorthodox names (#600)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent b99f6de commit 7cf5337

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## unreleased
66

7+
* Fix:
8+
* `Builders.FromNodePackageJson.ComponentBuilder` no longer omits name parts after the second slash ([#599] via [#600])
9+
10+
[#599]: https://github.com/CycloneDX/cyclonedx-javascript-library/issues/599
11+
[#600]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/600
12+
713
## 1.13.1 - 2023-03-28
814

915
* Docs

src/_helpers/packageJson.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
/**
2121
* Split name and group from a package's name.
2222
* Returns a tuple: [name, ?group]
23+
*
24+
* Based on [PackageJson spec](https://nodejs.org/api/packages.html#name) there are no restrictions on it.
25+
* Having multiple slashes(`/`) is basically no issue.
2326
*/
2427
export function splitNameGroup (data: string): [string, string?] {
25-
return data[0] === '@'
26-
? data.split('/', 2).reverse() as [string, string?]
27-
: [data]
28+
const delimGroup = data[0] === '@'
29+
? data.indexOf('/', 2)
30+
: 0
31+
return delimGroup > 0
32+
? [data.slice(delimGroup + 1), data.slice(0, delimGroup)]
33+
: [data, undefined]
2834
}
2935

3036
/**

tests/integration/Builders.FromNodePackageJson.ComponentBuilder.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ suite('Builders.FromNodePackageJson.ComponentBuilder', () => {
7979
version: `1.33.7-alpha.23.${salt}`
8080
}
8181
)
82+
],
83+
[
84+
// Even though https://npmjs.org does not allow it,
85+
// there is nothing wrong with a package name that contains more than one slash(/).
86+
// It is completely compliant to NodeJS rules and will be properly resolved.
87+
'name with slashes',
88+
{ name: '@foo/bar/baz' },
89+
new Models.Component(
90+
Enums.ComponentType.Library,
91+
'bar/baz',
92+
{
93+
group: '@foo',
94+
externalReferences: new Models.ExternalReferenceRepository([`FAKE REFERENCES ${salt}`])
95+
}
96+
)
8297
]
8398
].forEach(([purpose, data, expected]) => {
8499
test(`makeComponent: ${purpose}`, () => {

0 commit comments

Comments
 (0)