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

From Discord: Discussion on Correct Usage of URL Parsing in ServerRequest and Naming Conventions #3639

Closed
effect-bot opened this issue Sep 19, 2024 · 1 comment · Fixed by #3640

Comments

@effect-bot
Copy link

Summary

The user is discussing a potential issue in the Effect-TS codebase, specifically in the httpMiddleware.ts file. They suggest that the code should use ServerRequest.searchParamsFromURL(new URL(request.originalUrl)) instead of the current implementation because ServerRequest.url is stripped.

Additionally, the user questions the naming conventions used in the ServerRequest object, particularly the distinction between ServerRequest.url and ServerRequest.originalUrl. They express concern that deviating from the web standard Request's url property might be confusing and suggest using a different term for the parsed URL, similar to how Next.js uses NextRequest.pathname.

Key Takeaways:

  1. Code Suggestion: The user proposes a change to how URL search parameters are parsed.
  2. Naming Conventions: There is confusion about the use of ServerRequest.url vs. ServerRequest.originalUrl.
  3. Consistency with Web Standards: The user advocates for adhering to web standards or using clear, distinct naming conventions like Next.js.

Discord thread

https://discord.com/channels/795981131316985866/1286042509616746519

@ethanniser
Copy link
Contributor

REPRO:

import {
  HttpMiddleware,
  HttpServer,
  HttpServerRequest,
  HttpServerResponse,
} from "@effect/platform";
import { NodeHttpServer, NodeRuntime } from "@effect/platform-node";
import { Layer, Effect } from "effect";
import { createServer } from "node:http";

const ServerLive = NodeHttpServer.layer(() => createServer(), { port: 3000 });

const HttpLive = HttpServer.serve(
  Effect.gen(function* () {
    const req = yield* HttpServerRequest.HttpServerRequest;
    console.log(req.url);
    console.log(req.originalUrl);
    const searchParams = yield* HttpServerRequest.ParsedSearchParams;
    console.log(searchParams);
    return HttpServerResponse.text("hi");
  }).pipe(HttpMiddleware.searchParamsParser, HttpMiddleware.logger)
).pipe(HttpServer.withLogAddress, Layer.provide(ServerLive));

NodeRuntime.runMain(Layer.launch(HttpLive));
[16:37:43.744] INFO (#0): Listening on http://0.0.0.0:3000
[16:37:47.884] INFO (#19) http.span.1=1ms:
TypeError: "/" cannot be parsed as a URL.
    at URL (native)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform/dist/esm/internal/httpMiddleware.js:111:56)
    at runLoop (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:1100:34)
    at evaluateEffect (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:703:27)
    at start (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:756:14)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/runtime.js:52:18)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/FiberSet.js:214:19)
    at handler (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform-node/dist/esm/internal/httpServer.js:85:19)
    at emit (node:events:180:48)
    at fetch (node:http:482:12)
    at http.server GET
http.status: 500
http.method: GET
http.url: /
[16:38:00.391] INFO (#20) http.span.2=0ms:
TypeError: "/foo/bar?bar=baz" cannot be parsed as a URL.
    at URL (native)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform/dist/esm/internal/httpMiddleware.js:111:56)
    at runLoop (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:1100:34)
    at evaluateEffect (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:703:27)
    at start (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:756:14)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/runtime.js:52:18)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/FiberSet.js:214:19)
    at handler (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform-node/dist/esm/internal/httpServer.js:85:19)
    at emit (node:events:180:48)
    at fetch (node:http:482:12)
    at http.server GET
http.status: 500
http.method: GET
http.url: /foo/bar?bar=baz

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 a pull request may close this issue.

2 participants