-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: error on decode with meaningful message
- Loading branch information
Showing
7 changed files
with
127 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
'use strict' | ||
|
||
const { decodeURIComponent: decodeURIComponent_ } = globalThis | ||
const { PurlError } = require('./error') | ||
|
||
function decodeURIComponent(encodedURIComponent) { | ||
const { decodeURIComponent } = globalThis | ||
|
||
function decodePurlComponent(comp, encodedURIComponent) { | ||
try { | ||
return decodeURIComponent_(encodedURIComponent) | ||
return decodeURIComponent(encodedURIComponent) | ||
} catch {} | ||
return encodedURIComponent | ||
throw new PurlError(`unable to decode "${comp}" component`) | ||
} | ||
|
||
module.exports = { | ||
decodeURIComponent | ||
decodePurlComponent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
function formatPurlErrorMessage(message = '') { | ||
const { length } = message | ||
let formatted = '' | ||
if (length) { | ||
// Lower case start of message. | ||
const code0 = message.charCodeAt(0) | ||
formatted = | ||
code0 >= 65 /*'A'*/ || code0 <= 90 /*'Z'*/ | ||
? `${message[0].toLowerCase()}${message.slice(1)}` | ||
: message | ||
// Remove period from end of message. | ||
if ( | ||
length > 1 && | ||
message.charCodeAt(length - 1) === 46 /*'.'*/ && | ||
message.charCodeAt(length - 2) !== 46 | ||
) { | ||
formatted = formatted.slice(0, -1) | ||
} | ||
} | ||
return `Invalid purl: ${formatted}` | ||
} | ||
|
||
class PurlError extends Error { | ||
constructor(message) { | ||
super(formatPurlErrorMessage(message)) | ||
} | ||
} | ||
|
||
module.exports = { | ||
formatPurlErrorMessage, | ||
PurlError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,18 +119,6 @@ | |
"subpath": null, | ||
"is_invalid": true | ||
}, | ||
{ | ||
"description": "improperly encoded version string", | ||
"purl": "pkg:maven/org.apache.commons/[email protected]$@", | ||
"canonical_purl": "pkg:maven/org.apache.commons/[email protected]$@", | ||
"type": null, | ||
"namespace": null, | ||
"name": "io", | ||
"version": "1.4.0-$@", | ||
"qualifiers": null, | ||
"subpath": null, | ||
"is_invalid": true | ||
}, | ||
{ | ||
"description": "leading and trailing slashes '/' are not significant and should be stripped in the canonical form", | ||
"purl": "pkg:golang//github.com///ll////[email protected]", | ||
|
@@ -142,65 +130,5 @@ | |
"qualifiers": null, | ||
"subpath": null, | ||
"is_invalid": false | ||
}, | ||
{ | ||
"description": "percent encoded namespace", | ||
"purl": "pkg:type/100%/name", | ||
"canonical_purl": "pkg:type/100%25/name", | ||
"type": "type", | ||
"namespace": "100%", | ||
"name": "name", | ||
"version": null, | ||
"qualifiers": null, | ||
"subpath": null, | ||
"is_invalid": false | ||
}, | ||
{ | ||
"description": "percent encoded name", | ||
"purl": "pkg:type/namespace/100%", | ||
"canonical_purl": "pkg:type/namespace/100%25", | ||
"type": "type", | ||
"namespace": "namespace", | ||
"name": "100%", | ||
"version": null, | ||
"qualifiers": null, | ||
"subpath": null, | ||
"is_invalid": false | ||
}, | ||
{ | ||
"description": "percent encoded version", | ||
"purl": "pkg:type/namespace/name@100%", | ||
"canonical_purl": "pkg:type/namespace/name@100%25", | ||
"type": "type", | ||
"namespace": "namespace", | ||
"name": "name", | ||
"version": "100%", | ||
"qualifiers": null, | ||
"subpath": null, | ||
"is_invalid": false | ||
}, | ||
{ | ||
"description": "percent encoded qualifiers", | ||
"purl": "pkg:type/namespace/[email protected]?a=100%", | ||
"canonical_purl": "pkg:type/namespace/[email protected]?a=100%25", | ||
"type": "type", | ||
"namespace": "namespace", | ||
"name": "name", | ||
"version": "1.0", | ||
"qualifiers": { "a": "100%" }, | ||
"subpath": null, | ||
"is_invalid": false | ||
}, | ||
{ | ||
"description": "percent encoded subpath", | ||
"purl": "pkg:type/namespace/[email protected]#100%", | ||
"canonical_purl": "pkg:type/namespace/[email protected]#100%25", | ||
"type": "type", | ||
"namespace": "namespace", | ||
"name": "name", | ||
"version": "1.0", | ||
"qualifiers": null, | ||
"subpath": "100%", | ||
"is_invalid": false | ||
} | ||
] |
Oops, something went wrong.