| 1213 | * @since 4.0.0 |
| 1214 | */ |
| 1215 | export 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`. |