Skip to content
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

Merged
merged 4 commits into from
May 17, 2022

Conversation

naugtur
Copy link
Member

@naugtur naugtur commented Apr 13, 2022

…d __dirname for cjs compatibility

This makes browserify work.

@naugtur naugtur requested a review from kriskowal April 13, 2022 20:48
Comment on lines 12 to 60
export const dropFileProtocol = url => {
if (url.substring(0, 7) === 'file://') {
url = url.substring(7);
}
return url;
};
Copy link
Member

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.

Copy link
Member Author

@naugtur naugtur Apr 19, 2022

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.

Copy link
Member

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.

Copy link
Member Author

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.

@naugtur naugtur force-pushed the naugtur-missed-the-unnecessary-protocol branch 2 times, most recently from 6512003 to 62d70bc Compare May 2, 2022 10:05
@naugtur naugtur force-pushed the naugtur-missed-the-unnecessary-protocol branch from 62d70bc to 37a06c4 Compare May 16, 2022 11:17
@naugtur
Copy link
Member Author

naugtur commented May 16, 2022

@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.

@naugtur naugtur requested a review from kriskowal May 16, 2022 11:26
@naugtur naugtur force-pushed the naugtur-missed-the-unnecessary-protocol branch from 37a06c4 to c8d08c9 Compare May 16, 2022 11:52
@kriskowal
Copy link
Member

kriskowal commented May 16, 2022

@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.

This calls for an obscure reference. Barclay says:

image

const truncateIfFinalSlash = path => {
  if (path.lastIndexOf('/', url.length - 1) === -1) return path;
  return path.slice(0, -1);
};

@naugtur
Copy link
Member Author

naugtur commented May 16, 2022

@kriskowal

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 / and \ which seemed most readable with a tiny regex.
Want me to switch to string manipulation?

@@ -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://') {
Copy link
Member

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.

Copy link
Member Author

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-pre-cjs.js Outdated Show resolved Hide resolved
@naugtur naugtur force-pushed the naugtur-missed-the-unnecessary-protocol branch from c8d08c9 to bb85911 Compare May 17, 2022 10:26
@naugtur naugtur merged commit 43fdf69 into master May 17, 2022
@naugtur naugtur deleted the naugtur-missed-the-unnecessary-protocol branch May 17, 2022 12:46
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.

2 participants