-
Notifications
You must be signed in to change notification settings - Fork 74
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
fix(compartment-mapper): remove the file: protocol from __filename an… #1155
Conversation
export const dropFileProtocol = url => { | ||
if (url.substring(0, 7) === 'file://') { | ||
url = url.substring(7); | ||
} | ||
return url; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn’t quite sufficient, accounting for Windows. The readPowers
threaded into all of the Compartment Mapper API’s capture url
(url.filePathToURL
, url.pathToFileURL
) so it can convert between URL space and path space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took me a moment to understand why it won't work. The examples here were helpful: https://nodejs.org/api/url.html#urlfileurltopathurl (how dreadful)
When importLocation is used, read powers don't enter compartment-mapper - at least the type signature is defined as just the read function. And I've been using it that way in most places too.
Even if I knew how to drill the reference to fileURLToPath
(which is what I need) down to parse-cjs.js, I can't reliably assume I'm going to get it in the first place. That means I need a fallback implementation.
Whichever implementation I use as a fallback, without adding a dependency on node's url
, I'll allow for the readPowers being just a function use pattern to continue working fine everywhere that's not Windows.
While I'm personally happy to do so, I think that was not the point here.
I'd need to pass readPowers to makeImportHookMaker and then the set of tools necessary to implement url->path and stuff like require.resolve would need to be the new last argument to parse
since parsers don't have any initialization/instantiation and even if they did, readPowers are not available at the time they'd be initialized anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a reasonable fallback position is to provide __dirname === undefined
if the compartment mapper doesn’t receive the canonicalize
property on ReadPowers
. You may have to change the signature to ReadFn | ReadPowers
to thread it, but there are examples where I’ve already done this. There’s a utility function for normalizing that type laying around somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readpowers is { read, canonical }
.
canonical
is not helpful for providing a file path on windows. I will expose existing path2url and url2path functions.
6512003
to
62d70bc
Compare
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
Outdated
Show resolved
Hide resolved
62d70bc
to
37a06c4
Compare
@kriskowal this should now be ready. One controversy I can see is removing the trailing slash with regex - couldn't find a function that'd do that for me. |
37a06c4
to
c8d08c9
Compare
This calls for an obscure reference. Barclay says: const truncateIfFinalSlash = path => {
if (path.lastIndexOf('/', url.length - 1) === -1) return path;
return path.slice(0, -1);
}; |
I prefer substring with negative values, which I could use too, but the thing is, you insisted on supporting both |
@@ -8,6 +8,47 @@ const { freeze, keys, create, hasOwnProperty } = Object; | |||
* @returns {boolean} | |||
*/ | |||
const has = (object, key) => apply(hasOwnProperty, object, [key]); | |||
const removeProtocol = url => { | |||
if (url.substring(0, 7) === 'file://') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indexOf('file://') !== -1
has the virtue of not allocating, but I don’t feel strongly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
appreciated
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
Outdated
Show resolved
Hide resolved
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
Outdated
Show resolved
Hide resolved
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
Outdated
Show resolved
Hide resolved
c8d08c9
to
bb85911
Compare
…d __dirname for cjs compatibility
This makes browserify work.