MCPcopy Create free account
hub / github.com/Effect-TS/effect / toWebResult

Function toWebResult

packages/effect/src/http/HttpServerRequest.ts:1215–1251  ·  view source on GitHub ↗
(self: HttpServerRequest, options?: {
  readonly signal?: AbortSignal | undefined
  readonly context?: Context.Context<never> | undefined
})

Source from the content-addressed store, hash-verified

1213 * @since 4.0.0
1214 */
1215export const toWebResult = (self: HttpServerRequest, options?: {
1216 readonly signal?: AbortSignal | undefined
1217 readonly context?: Context.Context<never> | undefined
1218}): Result.Result<Request, RequestError> => {
1219 if (self.source instanceof Request) {
1220 return Result.succeed(self.source)
1221 }
1222 const url = toURL(self)
1223 if (Option.isNone(url)) {
1224 return Result.fail(
1225 new RequestParseError({
1226 request: self,
1227 description: "Invalid URL"
1228 })
1229 )
1230 }
1231 const requestInit: RequestInit = {
1232 method: self.method,
1233 headers: self.headers
1234 }
1235 if (options?.signal) {
1236 requestInit.signal = options.signal
1237 }
1238 if (hasBody(self.method)) {
1239 // Opening on the first pull with a zero high-water mark reads the body only
1240 // when the Web body is read, so converting a request leaves it readable.
1241 requestInit.body = Stream.toReadableStreamWith(
1242 lazyStream.openOnFirstPull(self.stream),
1243 options?.context ?? Context.empty(),
1244 {
1245 strategy: { highWaterMark: 0 }
1246 }
1247 )
1248 ;(requestInit as any).duplex = "half"
1249 }
1250 return Result.succeed(new Request(url.value, requestInit))
1251}
1252
1253/**
1254 * Converts an `HttpServerRequest` to a Web `Request` in `Effect`.

Callers 1

toWebFunction · 0.70

Calls 4

hasBodyFunction · 0.90
toURLFunction · 0.85
failMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected