Skip to content

Commit 14cb8a1

Browse files
authored
fix: properly parse non-url encoded file specs (#200)
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
1 parent d45dabc commit 14cb8a1

File tree

4 files changed

+240
-123
lines changed

4 files changed

+240
-123
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ Parses package name and specifier passed to commands like `npm install` or
88
## EXAMPLES
99

1010
```javascript
11-
var assert = require("assert")
12-
var npa = require("npm-package-arg")
11+
const assert = require("assert")
12+
const npa = require("npm-package-arg")
1313

1414
// Pass in the descriptor, and it'll return an object
1515
try {
16-
var parsed = npa("@bar/[email protected]")
16+
const parsed = npa("@bar/[email protected]")
1717
} catch (ex) {
1818
1919
}
2020
```
2121

2222
## USING
2323

24-
`var npa = require('npm-package-arg')`
24+
`const npa = require('npm-package-arg')`
2525

26-
### var result = npa(*arg*[, *where*])
26+
### const result = npa(*arg*[, *where*])
2727

2828
* *arg* - a string that you might pass to `npm install`, like:
2929
`[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`.
3434

3535
**Throws** if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported.
3636

37-
### var result = npa.resolve(*name*, *spec*[, *where*])
37+
### const result = npa.resolve(*name*, *spec*[, *where*])
3838

3939
* *name* - The name of the module you want to install. For example: `foo` or `@bar/foo`.
4040
* *spec* - The specifier indicating where and how you can get this module. Something like:
@@ -45,7 +45,7 @@ included then the default is `latest`.
4545

4646
**Throws** if the package name is invalid, a dist-tag is invalid or a URL's protocol is not supported.
4747

48-
### var purl = npa.toPurl(*arg*, *reg*)
48+
### const purl = npa.toPurl(*arg*, *reg*)
4949

5050
Returns the [purl (package URL)](https://github.com/package-url/purl-spec) form of the given package name/spec.
5151

@@ -79,9 +79,9 @@ keys:
7979
specification. Mostly used when making requests against a registry. When
8080
`name` is `null`, `escapedName` will also be `null`.
8181
* `rawSpec` - The specifier part that was parsed out in calls to `npa(arg)`,
82-
or the value of `spec` in calls to `npa.resolve(name, spec).
82+
or the value of `spec` in calls to `npa.resolve(name, spec)`.
8383
* `saveSpec` - The normalized specifier, for saving to package.json files.
84-
`null` for registry dependencies.
84+
`null` for registry dependencies. See note below about how this is (not) encoded.
8585
* `fetchSpec` - The version of the specifier to be used to fetch this
8686
resource. `null` for shortcuts to hosted git dependencies as there isn't
8787
just one URL to try with them.
@@ -94,3 +94,11 @@ keys:
9494
`npa.resolve(name, spec)` then this will be `name + '@' + spec`.
9595
* `subSpec` - If `type === 'alias'`, this is a Result Object for parsing the
9696
target specifier for the alias.
97+
98+
## SAVE SPECS
99+
100+
TLDR: `file:` urls are NOT uri encoded.
101+
102+
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 `%`).
103+
104+
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.

0 commit comments

Comments
 (0)