Skip to content

Fix: Allow absolute URLs as paths, e.g. for load() #2056

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tchebb
Copy link

@tchebb tchebb commented Apr 4, 2025

When using protobuf.js in a browser, it's useful to be able to load proto files from other origins. But currently, path.normalize() mangles URLs by removing the second slash after the scheme (e.g. https://example.com/file.js becomes https:/example.com/file.js).

Fix that issue, and better integrate the existing UNC support, by introducing a new path.absolutePrefix() function that checks for all the various prefixes that indicate a path is absolute and returns the one it finds (if any). By including both slashes in the return value for URLs, we exclude them from the normalization logic.

Note that this almost certainly still isn't perfect, as URLs, UNC paths, and even Windows drive letters are all quite complex. But I don't have the time or project context to make a major change here.

Copy link
Author

@tchebb tchebb left a comment

Choose a reason for hiding this comment

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

Note that I'm not sure what your workflow is for updates inside lib/. I didn't bump the version in lib/path/package.json, but I assume that'll be needed for protobuf.js to see these changes. Let me know if I'm missing something.

* @returns {string|null} the prefix, or null if path is relative
*/
export function absolutePrefix(path: string): (string|null);

Copy link
Author

Choose a reason for hiding this comment

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

Is there a script to update these index.d.ts files? I saw that npm run build:types updates the one at the root, but I didn't see anything similar for these, so I just made the change manually.

@tchebb tchebb force-pushed the support-absolute-urls branch from 862eb9f to 578768c Compare April 4, 2025 05:44
When using protobuf.js in a browser, it's useful to be able to load
proto files from other origins. But currently, path.normalize() mangles
URLs by removing the second slash after the scheme (e.g.
"https://example.com/" becomes "https:/example.com").

Fix that issue, and better integrate the existing UNC support, by
introducing a new path.absolutePrefix() function that checks for all the
various prefixes that indicate a path is absolute and returns the one it
finds (if any). By including both slashes in the return value for URLs,
we exclude them from the normalization logic.

Note that this almost certainly still isn't perfect, as URLs, UNC paths,
and even Windows drive letters are all quite complex. But I don't have
the time or project context to make a major change here.
@tchebb tchebb force-pushed the support-absolute-urls branch from 578768c to f21d293 Compare April 4, 2025 06:15
@tchebb
Copy link
Author

tchebb commented Apr 12, 2025

Note that this fixes the same problem that #1820 does. I didn't notice that PR before making this one, although this one has the added benefit of supporting additional URL schemes and simplifying the UNC code too.

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.

1 participant