Skip to content

Commit

Permalink
Fix URLPath parsing other origins
Browse files Browse the repository at this point in the history
  • Loading branch information
samhh committed Apr 6, 2024
1 parent 6401f7f commit 5df8fc2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to semantic versioning.
## 0.19.0 (_Unreleased_)

- Add `clone` to `URLPath`.
- Fix `URLPath` incorrectly parsing alternative origins.
- Fix typings in presence of `isolatedModules`.
- Bump minimum supported Node LTS to v20.

Expand Down
7 changes: 6 additions & 1 deletion src/URLPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ export const fromString =
// It should only throw some sort of `TypeError`:
// https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
E.tryCatch(
() => new globalThis.URL(x, phonyBase),
() => {
const y = new globalThis.URL(x, phonyBase)
if (y.origin !== phonyBase)

Check failure on line 184 in src/URLPath.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected if, use a conditional expression (ternary operator) instead
throw new TypeError("Failed to retain phony base URL")

Check failure on line 185 in src/URLPath.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected throw, throwing exceptions is not functional
return y
},
e => f(e as TypeError),
),
E.map(pack<URLPath>),
Expand Down
26 changes: 26 additions & 0 deletions test/URLPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@ describe("URLPath", () => {

expect(e.name).toBe("TypeError")
})

it("accepts all valid paths", () => {
fc.assert(
fc.property(fc.webPath(), x =>
expect(f(x)).toEqual(E.right(new URL(x, phonyBase))),
),
)
})

it("does not parse origins", () => {
const g = fromString(identity)

const e1 = unsafeUnwrapLeft(g("//x"))
expect(e1.name).toBe("TypeError")
expect(e1.message).toMatch(/phony/)

const e2 = unsafeUnwrapLeft(g("https://samhh.com/foo"))
expect(e2.name).toBe("TypeError")
expect(e2.message).toMatch(/phony/)

fc.assert(
fc.property(fc.string().map(f).filter(E.isRight), ({ right: x }) =>
expect((x as unknown as URL).origin).toBe(phonyBase),
),
)
})
})

describe("fromStringO", () => {
Expand Down

0 comments on commit 5df8fc2

Please sign in to comment.