refactor(deps): replace url-parse with native URL API#122
Open
Kyzgor wants to merge 2 commits into
Open
Conversation
Remove the `url-parse` dependency in favor of the native `URL` class, which has been available since Node.js v10 (the SDK's minimum supported version). This eliminates an unnecessary dependency and reduces bundle size. Fixes permitio#106
0053425 to
aa6d88b
Compare
…elper Extract the OPA base URL construction into a pure buildOpaBaseUrl(pdp) helper and add a hermetic ava unit test (test:unit) asserting the produced URL for the default PDP and edge cases (trailing slash, https-without-port, explicit-port override, path-prefixed PDP). This guards the native URL refactor so a regression in the construction logic fails the build.
Author
|
Heads-up on CI: the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Refactor (removes a runtime dependency) + test coverage.
What is the current behavior? (link:
url-parseris an unnecessary dependency #106)The SDK depends on
url-parse(^1.5.10) and@types/url-parse(^1.4.11) solely to build theOPA client base URL in
src/enforcement/enforcer.ts. Node's built-in WHATWGURLhas beenavailable since Node 10, and
package.jsonalready declaresengines.node: ">=10", so thedependency is redundant. There is also no test guarding the OPA URL construction, so a regression
in that logic would go undetected.
What is the new behavior (if this is a feature change)?
URLAPI;url-parseand@types/url-parseare removed.buildOpaBaseUrl(pdp: string): stringso it is unit-testable without instantiating axios or a live PDP.
yarn test:unit→src/tests/enforcer.spec.ts) asserts the producedURL for the default PDP and edge cases (trailing slash, https-without-port, explicit-port
override, path-prefixed PDP). The test would FAIL if the construction logic regressed (verified by
mutation: changing the appended path makes all 5 assertions fail).
Behaviour is proven equivalent to the old
url-parsepath:url-parse→URL→http://localhost:7766http://localhost:8181/v1/data/permit/http://localhost:7766/http://localhost:8181/v1/data/permit/https://pdp.example.comhttps://pdp.example.com:8181/v1/data/permit/https://pdp.example.com:1234https://pdp.example.com:8181/v1/data/permit/http://localhost:7766/prefixhttp://localhost:8181/prefixv1/data/permit/Other information:
Validated with
yarn lint,yarn build,yarn test:unit(5 tests) andyarn test:module-imports(9 tests) on Node 18 and 20.
What's NOT in this PR: the main REST client
baseURL(a plain string concat, never usedurl-parse) is untouched; noengines.nodebump; nosrc/openapi/changes. The largeyarn.lockdiff is expected lockfile regeneration for the dependency removal. A pre-existing quirk is
intentionally preserved (and now documented in the test): a path-prefixed PDP without a trailing
slash yields
.../prefixv1/data/permit/— identical under both implementations; worth a separatefollow-up if it's a real concern.
Equivalence holds for every PDP value with a scheme (the default
http://localhost:7766and allrealistic configs). A scheme-less PDP (e.g.
localhost:7766) is not a supported input and producesan invalid OPA URL under both the old
url-parseand the newURLimplementations — neitherregresses the other.
The
security/snyk (permit)check is in an ERROR state due to an org quota condition on forkPRs ("You have used your limit of private tests"), not a vulnerability — this PR removes a
dependency and adds none. A maintainer re-run/waiver would clear it.
Fixes
url-parseris an unnecessary dependency #106