Skip to content

Conversation

@nexdrew
Copy link
Contributor

@nexdrew nexdrew commented Jun 14, 2016

escapedName URI-encodes the slash for scoped names such that @foo/bar becomes @foo%2fbar. For unscoped names, escapedName should just match name, either null or otherwise.

The idea is that anywhere you would use name when talking to a registry, you should now use escapedName instead. This should eliminate the need for duplicating lines like this across multiple repos/packages/modules that need to parse a package name when working with a registry, which is something we do quite often among open and closed source code at npm. This little package seems an ideal place to centralize this logic.

When implementing this, I considered a few different options:

  1. Using Object.defineProperty to add escapedName as a getter method that was always derived from name, conditionally escaping based on scope

    This kept things simple in terms of not needing to "follow" all the places in the logic where name and scope are set. Its drawback, however, was a hidden value when relying on Object.prototype.toString(), such as here, which resulted in output like:

    Result {
      escapedName: [Getter],
      raw: '@foo/bar',
      scope: '@foo',
      name: '@foo/bar',
      rawSpec: '',
      spec: 'latest',
      type: 'tag' }
    

    Which is obviously less than ideal.

  2. Using a wrapper or decorator function to populate escapedName whenever res is returned.

    This fixed the toString() problem, but since the npa() function can actually return in a few different places, I ended up "following" the return statements rather than where name and scope are actually set. This seemed less than ideal since the it didn't make the relationship between the derived property and the original properties obvious.

So instead I opted to align the setting of escapedName in close proximity to the setting of scope and name in an explicit, procedural way.

@nexdrew
Copy link
Contributor Author

nexdrew commented Jun 14, 2016

It seems that builds have been failing or erring for Node 0.8 for quite some time, so please don't hold that against this PR. 😃

var assert = require("assert")
var util = require("util")
var semver = require("semver")
var path = require("path")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid confusion this should be in a separate commit (or PR; I think it may be time to standardize this code base).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good point, I will undo this.

@othiym23
Copy link
Contributor

The CLI team is dropping support for 0.8 as of now, so I'll probably pull that from .travis.yml and we'll see what happens. A couple minor nits, but otherwise this looks good, and as a purely additive API, should be good to go once those nits have been addressed.

escapedName uri-encodes the slash for scoped names

otherwise escapedName matches name

the idea is that anywhere you would use name when talking to a registry, you should now use escapedName instead
@nexdrew nexdrew force-pushed the nexdrew/escapedName branch from 5576b36 to b8d31ba Compare June 14, 2016 01:00
@coveralls
Copy link

coveralls commented Jun 14, 2016

Coverage Status

Coverage increased (+0.3%) to 96.33% when pulling b8d31ba on nexdrew/escapedName into b39730c on master.

@nexdrew
Copy link
Contributor Author

nexdrew commented Jun 14, 2016

@othiym23 Attempted to address your concerns. Tests pass for me locally. Let me know if you see anything else.

@coveralls
Copy link

coveralls commented Jun 14, 2016

Coverage Status

Coverage increased (+0.2%) to 96.262% when pulling 445438a on nexdrew/escapedName into b39730c on master.

@coveralls
Copy link

coveralls commented Jun 14, 2016

Coverage Status

Coverage increased (+0.2%) to 96.262% when pulling a7588aa on nexdrew/escapedName into b39730c on master.

@othiym23
Copy link
Contributor

Rebased and landed as e92fcc9. Thanks, Andrew!

@othiym23 othiym23 closed this Jun 15, 2016
@nexdrew nexdrew deleted the nexdrew/escapedName branch June 15, 2016 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants