Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues #58 & #63 #62

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/package-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ class PackageURL {

let type
({ 0: type, 1: remainder } = remainder.split('/', 2));
if (!type || !remainder) {
if (!type) {
throw new Error('purl is missing the required "type" component.');
}
type = decodeURIComponent(type);

const url = new URL(purl);
// this strip '/, // and /// as possible in :// or :///
// from https://gist.github.com/refo/47632c8a547f2d9b6517#file-remove-leading-slash
const url = new URL(purl.trim().replace(/^pkg:\/+/g, 'pkg:'));

const { searchParams } = url;
let qualifiers = undefined;
Expand All @@ -192,9 +194,7 @@ class PackageURL {
throw new Error('Invalid purl: cannot contain a "user:pass@host:port"');
}

// this strip '/, // and /// as possible in :// or :///
// from https://gist.github.com/refo/47632c8a547f2d9b6517#file-remove-leading-slash
const path = url.pathname.trim().replace(/^\/+/g, '');
const path = url.pathname.trim();

// version is optional - check for existence
let version = undefined;
Expand Down Expand Up @@ -225,7 +225,7 @@ class PackageURL {
const nameIndex = remaining.length - 1;
const namespaceComponents = remaining.slice(0, nameIndex);
name = decodeURIComponent(remaining[nameIndex]);
namespace = decodeURIComponent(namespaceComponents.join('/'));
namespace = decodeURIComponent(namespaceComponents.filter(item => item.trim()).join('/'));
} else if (remaining.length === 1) {
name = decodeURIComponent(remaining[0]);
}
Expand Down
12 changes: 12 additions & 0 deletions test/data/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,17 @@
"qualifiers": null,
"subpath": null,
"is_invalid": false
},
{
"description": "In namespace, leading and trailing slashes '/' are not significant and should be stripped in the canonical form",
"purl": "pkg:golang//github.com/ll/[email protected]",
"canonical_purl": "pkg:golang/github.com/ll/[email protected]",
"type": "golang",
"namespace": "github.com/ll",
"name": "xlog",
"version": "2.0",
"qualifiers": null,
"subpath": null,
"is_invalid": false
}
]
Loading