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

new version #13

Merged
merged 2 commits into from
Oct 16, 2023
Merged

new version #13

merged 2 commits into from
Oct 16, 2023

Conversation

cainus
Copy link
Owner

@cainus cainus commented Aug 1, 2022

  • separates getters / setters for stronger typing. eg. port() is used for setting the port, but getPort() is used for getting it. UrlGrey is more for url-manipulation than parsing, so setPort() seemed too verbose and backward incompatible.
  • uses typescript & jest
  • drops explicit browser support
  • uses native WHATWG classes and drops node.js imports and 3rd-party deps . These should be faster.

…ers for stronger typing. uses typescript & jest. uses WHATWG classes and drops node.js imports and 3rd-party deps.
@cainus cainus requested a review from KelWill August 1, 2022 14:08
@KelWill
Copy link
Collaborator

KelWill commented Aug 1, 2022

I'm not seeing the new TS files. Did you skip adding them to the commit?

@cainus
Copy link
Owner Author

cainus commented Aug 1, 2022

Bah yeah sorry... they're on the branch now.


export class UrlGrey {
#url: string
_parsed: Parsed | null
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this intended to be public? it has an underscore but no visibility modifier

const defaults = getDefaults()

this._parsed.protocol = this._parsed.protocol || defaults.protocol
return this._parsed
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should mark this as readonly in typescript, or copy it on the way out. readonly is probably better. if we don't do one of these then calling code can change the internal state by mutating the return value.

throw new Error("file urls don't have ports")
}
const clone = this.clone()
clone.parsed().port = parseInt(`${num}`, 10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ah I see you're relying on manipulating the internal state here. hmm. Maybe we should instead have a method called extend which takes a Partial<Parsed> merges it with the current instance's Parsed and produces a new UrlGrey from that.

pathname: string;
port: number | null; // some protocols have no port, like file://
path: string;
query: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

any reason not to use URLSearchParams here for the query property?

const p = urlParse(`http://localhost/${url}`)
clone.parsed().hash = p.hash
clone.parsed().query = p.query
return clone.path(p.pathname)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there any rhyme or reason to which fields require you to go through this.parsed().xyz vs having a dedicated method like this.xyz()?

Copy link
Collaborator

@KelWill KelWill left a comment

Choose a reason for hiding this comment

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

URL feels like it already supports many of the features that urlgrey is adding on.

  • would it make sense to have _parsed be a URL object that you can mutate and stringify?
  • does the library as a whole still make sense? URL has good documentation and is pretty easy to work with! If there are functions that URL is missing, would it make sense to set up helpers that take a URL object and transform that object?

},
"maintainers": [
"Gregg Caines <gregg@caines.ca> (http://caines.ca)"
],
"dependencies": {
"fast-url-parser": "^1.1.3"
Copy link
Collaborator

@KelWill KelWill Aug 2, 2022

Choose a reason for hiding this comment

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

I'd recommend benchmarking this removal. There's a huge difference between it and the Node.JS url library and I'd be curious to see how things compare with URL. Using fast-url-parser significantly reduced how much event-loop-blocking we were seeing within API when handling 1,000-10,000s of urls.

@cainus cainus merged commit a752840 into master Oct 16, 2023
@cainus cainus deleted the v2 branch October 16, 2023 17:31
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