Skip to content

Improves relative path handling for urls #126

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

Closed
wants to merge 8 commits into from

Conversation

rbuckton
Copy link

Fixes #84.

Adds two utility functions to better manage path handling: isURI and toPath.

isURI - Determines whether a file path is a URI using the following rules:

  • A path is a URI if it starts with an alpha character followed by one or more of either an alpha character, digit, +, -, or -, followed by a colon. For example:

    http://tempuri.org/path - Web URI
    file://host/path        - File URI for UNC path
    file:///c:/path         - File URI for DOS path
    urn:custom              - Other URI
    
  • A path is NOT a URI if it starts with a single alpha character and a colon is, instead it is treated as a DOS path. For example:

    c:/path                 - DOS path
    c:\path                 - DOS path
    
  • Any other sequence of characters is not considered a URI.

toPath - Tries to convert a URI to a local path. If the URI is a file URI (file://), it is converted into a local path format that NodeJS understands using the following rules:

  • A file URI with a host name is treated as a UNC path:

    file://server/share         -> //host/path
    
  • A file URI without a host name, whose first path segment is a single alpha character followed by either a colon (:) or pipe (|) is treated as a rooted DOS path:

    file:///c:/path         -> c:\path
    file:///c|/path         -> c:\path
    
  • A file URI without a hostname that is not treated as a DOS path is treated as a rooted POSIX path:

    file:///etc/path        -> /etc/path
    
  • Querystring (?) and fragments (#) are removed.

URI handling is designed to conform to RFC 3986, with exceptions for DOS path handling.

Also fixes the tests so that they also run on Windows.

@@ -3,7 +3,7 @@
"file": "script.js",
"sourceRoot": "..",
"sources": [
"browser-test/script.coffee"
"browser-test\\script.coffee"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this work on unix?

Copy link
Author

Choose a reason for hiding this comment

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

I didn't notice that. script.map is a build output, but I can revert the incidental change

Copy link
Author

Choose a reason for hiding this comment

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

Actually, it looks like script.map is not excluded via the .gitignore as is the case for the other tests.

@ncalexan
Copy link

Is there more work to be done here? This looks like a more comprehensive approach to #133; can we move this forward and close that attempt?

@ncalexan
Copy link

I need a single small commit to get better printing locally: ncalexan@bbfcde4

ncalexan added a commit to ncalexan/tofino that referenced this pull request Apr 28, 2016
…ces.

The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
ncalexan added a commit to ncalexan/tofino that referenced this pull request Apr 28, 2016
…ces.

The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
ncalexan added a commit to ncalexan/tofino that referenced this pull request Apr 28, 2016
…ces.

The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
ncalexan added a commit to ncalexan/tofino that referenced this pull request Apr 29, 2016
…ces.

The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
ncalexan added a commit to ncalexan/tofino that referenced this pull request Apr 29, 2016
The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
ncalexan added a commit to ncalexan/tofino that referenced this pull request Apr 29, 2016
The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
ncalexan added a commit to mozilla/tofino that referenced this pull request Apr 29, 2016
…es. r=Mossop,rnewman. (#289)

The source-map-support package doesn't appreciate absolute file://
URLs.  I tried to address this in
evanw/node-source-map-support#133 and made
some progress before discovering
evanw/node-source-map-support#126.  I pushed
that work, and a small follow-up, to my local version used here.

With this, I get stack traces that referring to app/.
@felixfbecker
Copy link

felixfbecker commented Jun 2, 2016

I don't know why isUri is needed. Source map paths are always URIs. They can be relative, but they cannot be paths (for example no backslashes). This should be enough:

        let path = url.parse(uri).pathname;
        // strip the trailing slash from Windows paths (indicated by a drive letter with a colon)
        if (/^\/[a-zA-Z]:\//.test(path)) {
            path = path.substr(1);
        }

@rbuckton
Copy link
Author

rbuckton commented Jun 7, 2016

@felixfbecker path.resolve and uri.resolve work very differently. Being able to more accurately gauge which branch to take helps to make this library better able to handle a larger number of scenarios. Just because source map paths should always be a URI doesn't mean that tools and developers will always make sure it is a valid URI. This makes the library more resilient to various inputs and hopefully reduces breaking changes.

@felixfbecker
Copy link

Can't wait to get this merged...

@felixfbecker
Copy link

What's the progress on this?

@LinusU
Copy link
Collaborator

LinusU commented Jul 15, 2016

@rbuckton Do you feel that this is ready to merge?

@ncalexan I saw that you are using a fork, would merging this be enough for you to go back, or is there something more that we should add?

@felixfbecker
Copy link

What I noticed is that when using an absolute file uri as sourceRoot, the file path in the stack trace is also reported as a URL, while it should be converted to a path.

@felixfbecker
Copy link

felixfbecker commented Jul 15, 2016

Example:

Error: Cannot find module './routes/investment/fundings'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (file:///c:/Users/felix/git/Projekte/asdasdsad/src/api/app.ts:19:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (file:///c:/Users/felix/git/Projekte/afsdfdsaf/src/test/api/routes/index.ts:4:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)

but this could be solved in a different PR, my team is currently depending on this from your fork and it works fine so far. Would really like to see this get merged.

@ncalexan
Copy link

@rbuckton Do you feel that this is ready to merge?

@ncalexan I saw that you are using a fork, would merging this be enough for you to go back, or is there something more that we should add?

Hi folks, sorry I've been out of this loop. The project consuming this (https://github.com/mozilla/tofino) may not really need the fork anymore; I'd need to evaluate. My guess is we'd revert to mainline immediately after merge, and re-open if necessary. We're not interested in maintaining forks long term.

@felixfbecker
Copy link

So can we merge this?

@rbuckton
Copy link
Author

I have no further changes at this time, so I feel it is ready to merge.

file = path.resolve(file);
file = file.replace(/\\/g, '/');
file = file[0] !== '/' ? '/' + file : file;
return encodeURI('file://' + file);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you update this (and the other new functions) to follow the 2 space indentation that the rest of the project uses?

@@ -110,7 +147,7 @@ function compareStdout(done, sourceMap, source, expected) {
compareLines(
(stdout + stderr)
.trim()
.split('\n')
.split(/\r\n|\r|\n/g)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Standalone \r is only used on Mac OS classic which no one uses today. How about /\r?\n/g?

@felixfbecker
Copy link

ping @rbuckton
can you adress the comments? Otherwise I can do it and open a new PR.

@rbuckton rbuckton closed this Aug 22, 2018
@felixfbecker
Copy link

Why close this?

@rbuckton
Copy link
Author

It's been over two years since I opened it and I haven't had the time to update the PR or determine if it is still needed.

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.

Incorrectly resolves absolute source mapping url as relative to file
4 participants