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 UrlParams.makeUrl when globalThis.location is set to undefined #2514

Conversation

rocwang
Copy link
Contributor

@rocwang rocwang commented Apr 14, 2024

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Deno has globalThis.location defined as undefined by default. This breaks the HTTP Client in @effect/platform. As the current implemention of baseUrl() as per v0.48.27 only checks "location" in globalThis, but not globalThis.location !== undefined. This PR fixes it by adding the latter.

Here a test script to demostrate the bug:

// filename: test.ts
import { NodeRuntime } from "@effect/platform-node"
import { HttpClient } from "@effect/platform"
import { Console, Effect, pipe } from "effect"

pipe(
  Console.log("location in globalThis:", "location" in globalThis),
  Effect.andThen(Console.log("globalThis.location:", globalThis.location)),
  Effect.as(
    HttpClient.request.get("https://jsonplaceholder.typicode.com/posts/1"),
  ),
  Effect.andThen(HttpClient.client.fetch()),
  Effect.andThen((response) => Console.log("Status:", response.status)),
  Effect.scoped,
  NodeRuntime.runMain,
)

Before the fix:

$ deno run -A ./test.ts
location in globalThis: true
globalThis.location: undefined
timestamp=2024-04-14T11:55:59.777Z level=ERROR fiber=#0 cause="RequestError: InvalidUrl error (GET https://jsonplaceholder.typicode.com/posts/1): Cannot read properties of undefined (reading 'origin')
    at baseUrl (file:///Users/roc/Library/Caches/deno/npm/registry.npmjs.org/@effect/platform/0.48.27/dist/esm/Http/UrlParams.js:98:21)
    at try (file:///Users/roc/Library/Caches/deno/npm/registry.npmjs.org/@effect/platform/0.48.27/dist/esm/Http/UrlParams.js:86:38)
    at file:///Users/roc/Library/Caches/deno/npm/registry.npmjs.org/effect/2.4.18/dist/esm/internal/core-effect.js:46:14"

$ deno run -A --location https://example.com/path ./test.ts
location in globalThis: true
globalThis.location: Location {
  hash: "",
  host: "example.com",
  hostname: "example.com",
  href: "https://example.com/path",
  origin: "https://example.com",
  pathname: "/path",
  port: "",
  protocol: "https:",
  search: ""
}
Status: 200

After the fix:

$ deno run -A ./test.ts                                    
location in globalThis: true
globalThis.location: undefined
Status: 200

$ deno run -A --location https://example.com/path ./test.ts
location in globalThis: true
globalThis.location: Location {
  hash: "",
  host: "example.com",
  hostname: "example.com",
  href: "https://example.com/path",
  origin: "https://example.com",
  pathname: "/path",
  port: "",
  protocol: "https:",
  search: ""
}
Status: 200

A unit test is added for this in the PR.

See also Deno's Location API.

Related

@rocwang rocwang requested a review from tim-smart as a code owner April 14, 2024 12:04
Copy link

changeset-bot bot commented Apr 14, 2024

🦋 Changeset detected

Latest commit: 98ff8b5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@effect/platform Patch
@effect/cli Patch
@effect/experimental Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/rpc-http Patch
@effect/rpc Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rocwang rocwang changed the title Fix HTTP Client's compatibility with Deno due to globalThis.location. Fix HTTP Client's compatibility with Deno due to globalThis.location Apr 14, 2024
@tim-smart tim-smart force-pushed the fix/http-client-location-compatibility-with-deno branch from 4dbd796 to 98ff8b5 Compare April 16, 2024 02:50
Copy link
Member

@tim-smart tim-smart left a comment

Choose a reason for hiding this comment

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

Thanks!

@tim-smart tim-smart changed the title Fix HTTP Client's compatibility with Deno due to globalThis.location Fix UrlParams.makeUrl when globalThis.location is set to undefined Apr 16, 2024
@tim-smart tim-smart merged commit 25d74f8 into Effect-TS:main Apr 16, 2024
12 checks passed
@github-actions github-actions bot mentioned this pull request Apr 16, 2024
@rocwang rocwang deleted the fix/http-client-location-compatibility-with-deno branch April 16, 2024 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants