Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@changesets/cli": "^2.29.4",
"@edge-runtime/vm": "^5.0.0",
"@effect/build-utils": "^0.8.3",
"@effect/docgen": "https://pkg.pr.new/Effect-TS/docgen/@effect/docgen@fd06738",
"@effect/eslint-plugin": "^0.3.2",
"@effect/language-service": "^0.23.3",
"@effect/vitest": "workspace:^",
Expand Down
6 changes: 6 additions & 0 deletions packages/platform/src/HttpServerRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,9 @@ export const fromWeb: (request: Request) => HttpServerRequest = internal.fromWeb
* @category conversions
*/
export const toURL: (self: HttpServerRequest) => Option<URL> = internal.toURL

/**
* @since 1.0.0
* @category conversions
*/
export const toWeb: (self: HttpServerRequest) => Request = internal.toWeb
35 changes: 35 additions & 0 deletions packages/platform/src/internal/httpServerRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,38 @@ export const toURL = (self: ServerRequest.HttpServerRequest): Option.Option<URL>
return Option.none()
}
}

/** @internal */
export const toWeb = (self: ServerRequest.HttpServerRequest): globalThis.Request => {
// Check if the request is a ServerRequestImpl wrapping a global Request
if (
typeof globalThis.Request !== "undefined" &&
self.source != null &&
self.source instanceof globalThis.Request
) {
return self.source
}

// Reconstruct a new Request for non-wrapping implementations
// Determine the URL to use
let url: string
const urlOption = toURL(self)
if (Option.isSome(urlOption)) {
url = urlOption.value.href
} else {
url = self.originalUrl || self.url || "/"
}

// Prepare RequestInit options
const init: RequestInit = {
method: self.method,
// Headers type in platform is ReadonlyRecord<string, string | undefined>
// which is compatible with HeadersInit
headers: self.headers as HeadersInit
// Body is intentionally omitted for non-wrapping implementations
// to keep this synchronous and simple
}

// Create and return the Request
return new globalThis.Request(url, init)
}
145 changes: 145 additions & 0 deletions packages/platform/test/HttpServerRequest.toWeb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import * as Headers from "@effect/platform/Headers"
import * as IncomingMessage from "@effect/platform/HttpIncomingMessage"
import * as HttpServerRequest from "@effect/platform/HttpServerRequest"
import { describe, it } from "@effect/vitest"
import { strictEqual } from "@effect/vitest/utils"

describe("HttpServerRequest.toWeb", () => {
it("round-trip identity for wrapped requests", () => {
// Create a global Request with url/method/body/headers
const originalRequest = new Request("https://example.com/api/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer token123"
},
body: JSON.stringify({ name: "test" })
})

// Call fromWeb to obtain HttpServerRequest
const httpRequest = HttpServerRequest.fromWeb(originalRequest)

// Call toWeb and assert the returned object is strictly equal to the original Request
const resultRequest = HttpServerRequest.toWeb(httpRequest)

strictEqual(resultRequest, originalRequest, "toWeb should return the exact same Request object")
})

it("reconstructs URL/method/headers for non-wrapped implementation", () => {
// Create a minimal stub object implementing HttpServerRequest
// that does not wrap a source Request
const stubRequest = {
[HttpServerRequest.TypeId]: HttpServerRequest.TypeId,
[IncomingMessage.TypeId]: IncomingMessage.TypeId,
source: { custom: "source" }, // Not a Request instance
url: "/api/users",
originalUrl: "/api/users",
method: "GET",
headers: Headers.fromInput({
"host": "example.com",
"content-type": "application/json",
"x-custom-header": "custom-value"
}),
cookies: {},
remoteAddress: undefined as any,
multipart: undefined as any,
multipartStream: undefined as any,
upgrade: undefined as any,
modify: undefined as any,
stream: undefined as any,
text: undefined as any,
json: undefined as any,
urlParamsBody: undefined as any,
arrayBuffer: undefined as any
} as HttpServerRequest.HttpServerRequest

// Call toWeb(stub) and assert the returned Request has expected properties
const resultRequest = HttpServerRequest.toWeb(stubRequest)

// Check that we got a Request object
strictEqual(resultRequest instanceof Request, true, "toWeb should return a Request instance")

// Check method
strictEqual(resultRequest.method, "GET", "Request method should match")

// Check URL - should be reconstructed from stub data
const resultUrl = new URL(resultRequest.url)
strictEqual(resultUrl.protocol, "http:", "Should default to http protocol")
strictEqual(resultUrl.hostname, "example.com", "Should use host from headers")
strictEqual(resultUrl.pathname, "/api/users", "Should use pathname from stub.url")

// Check headers
strictEqual(
resultRequest.headers.get("content-type"),
"application/json",
"Should preserve content-type header"
)
strictEqual(
resultRequest.headers.get("x-custom-header"),
"custom-value",
"Should preserve custom headers"
)
})

it("reconstructs with https protocol when x-forwarded-proto header is present", () => {
const stubRequest = {
[HttpServerRequest.TypeId]: HttpServerRequest.TypeId,
[IncomingMessage.TypeId]: IncomingMessage.TypeId,
source: { custom: "source" },
url: "/secure/path",
originalUrl: "/secure/path",
method: "POST",
headers: Headers.fromInput({
"host": "secure.example.com",
"x-forwarded-proto": "https",
"content-type": "text/plain"
}),
cookies: {},
remoteAddress: undefined as any,
multipart: undefined as any,
multipartStream: undefined as any,
upgrade: undefined as any,
modify: undefined as any,
stream: undefined as any,
text: undefined as any,
json: undefined as any,
urlParamsBody: undefined as any,
arrayBuffer: undefined as any
} as HttpServerRequest.HttpServerRequest

const resultRequest = HttpServerRequest.toWeb(stubRequest)
const resultUrl = new URL(resultRequest.url)

strictEqual(resultUrl.protocol, "https:", "Should use https when x-forwarded-proto is https")
strictEqual(resultUrl.hostname, "secure.example.com", "Should use host from headers")
strictEqual(resultUrl.pathname, "/secure/path", "Should preserve the path")
})

it("handles missing host header gracefully", () => {
const stubRequest = {
[HttpServerRequest.TypeId]: HttpServerRequest.TypeId,
[IncomingMessage.TypeId]: IncomingMessage.TypeId,
source: {},
url: "/path",
originalUrl: "/path",
method: "GET",
headers: Headers.empty,
cookies: {},
remoteAddress: undefined as any,
multipart: undefined as any,
multipartStream: undefined as any,
upgrade: undefined as any,
modify: undefined as any,
stream: undefined as any,
text: undefined as any,
json: undefined as any,
urlParamsBody: undefined as any,
arrayBuffer: undefined as any
} as HttpServerRequest.HttpServerRequest

const resultRequest = HttpServerRequest.toWeb(stubRequest)
const resultUrl = new URL(resultRequest.url)

strictEqual(resultUrl.hostname, "localhost", "Should default to localhost when host is missing")
})
})
Loading