-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly parse non-url encoded file specs
Properly creates file package args that contain characters that need to be url encoded. There is also a refactor/cleanup in here - Removed the magic windows global for testing, fixing the tests to mock process.platform instead. - Moved inline regexes up to where the others are defined - Renamed a few variables to be more correct (i.e. isFilename to isFileType) - Refactored Result to be a proper Class instead of a function w/ prototypes Closes: #193
- Loading branch information
Showing
4 changed files
with
229 additions
and
123 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 |
---|---|---|
|
@@ -8,22 +8,22 @@ Parses package name and specifier passed to commands like `npm install` or | |
## EXAMPLES | ||
|
||
```javascript | ||
var assert = require("assert") | ||
var npa = require("npm-package-arg") | ||
const assert = require("assert") | ||
const npa = require("npm-package-arg") | ||
|
||
// Pass in the descriptor, and it'll return an object | ||
try { | ||
var parsed = npa("@bar/[email protected]") | ||
const parsed = npa("@bar/[email protected]") | ||
} catch (ex) { | ||
… | ||
} | ||
``` | ||
|
||
## USING | ||
|
||
`var npa = require('npm-package-arg')` | ||
`const npa = require('npm-package-arg')` | ||
|
||
### var result = npa(*arg*[, *where*]) | ||
### const result = npa(*arg*[, *where*]) | ||
|
||
* *arg* - a string that you might pass to `npm install`, like: | ||
`[email protected]`, `@bar/[email protected]`, `foo@user/foo`, `http://x.com/foo.tgz`, | ||
|
@@ -34,7 +34,7 @@ part, eg `foo` then the specifier will default to `latest`. | |
|
||
**Throws** if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported. | ||
|
||
### var result = npa.resolve(*name*, *spec*[, *where*]) | ||
### const result = npa.resolve(*name*, *spec*[, *where*]) | ||
|
||
* *name* - The name of the module you want to install. For example: `foo` or `@bar/foo`. | ||
* *spec* - The specifier indicating where and how you can get this module. Something like: | ||
|
@@ -45,7 +45,7 @@ included then the default is `latest`. | |
|
||
**Throws** if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported. | ||
|
||
### var purl = npa.toPurl(*arg*, *reg*) | ||
### const purl = npa.toPurl(*arg*, *reg*) | ||
|
||
Returns the [purl (package URL)](https://github.com/package-url/purl-spec) form of the given package name/spec. | ||
|
||
|
@@ -79,9 +79,9 @@ keys: | |
specification. Mostly used when making requests against a registry. When | ||
`name` is `null`, `escapedName` will also be `null`. | ||
* `rawSpec` - The specifier part that was parsed out in calls to `npa(arg)`, | ||
or the value of `spec` in calls to `npa.resolve(name, spec). | ||
or the value of `spec` in calls to `npa.resolve(name, spec)`. | ||
* `saveSpec` - The normalized specifier, for saving to package.json files. | ||
`null` for registry dependencies. | ||
`null` for registry dependencies. See note below about how this is (not) encoded. | ||
* `fetchSpec` - The version of the specifier to be used to fetch this | ||
resource. `null` for shortcuts to hosted git dependencies as there isn't | ||
just one URL to try with them. | ||
|
@@ -94,3 +94,11 @@ keys: | |
`npa.resolve(name, spec)` then this will be `name + '@' + spec`. | ||
* `subSpec` - If `type === 'alias'`, this is a Result Object for parsing the | ||
target specifier for the alias. | ||
|
||
## SAVE SPECS | ||
|
||
TLDR: `file:` urls are NOT uri encoded. | ||
|
||
Historically, npm would uri decode file package args, but did not do any uri encoding for the `saveSpec`. This meant that it generated incorrect saveSpecs for directories with characters that *looked* like encoded uri characters, and also that it could not parse directories with some unencoded uri characters (such as `%`). | ||
|
||
In order to fix this, and to not break all existing versions of npm, this module now parses all file package args as not being uri encoded. And in order to not break all of the package.json files npm has made in the past, it also does not uri encode the saveSpec. This includes package args that start with `file:`. This does mean that npm `file:` package args are not RFC compliant, and making them so constitutes quite a breaking change. |
Oops, something went wrong.