-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
httpServer.ts
493 lines (463 loc) · 15.3 KB
/
httpServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import * as Etag from "@effect/platform-node-shared/NodeEtag"
import * as MultipartNode from "@effect/platform-node-shared/NodeMultipart"
import * as Cookies from "@effect/platform/Cookies"
import * as FileSystem from "@effect/platform/FileSystem"
import type * as Headers from "@effect/platform/Headers"
import * as App from "@effect/platform/HttpApp"
import * as IncomingMessage from "@effect/platform/HttpIncomingMessage"
import type { HttpMethod } from "@effect/platform/HttpMethod"
import type * as Middleware from "@effect/platform/HttpMiddleware"
import * as Server from "@effect/platform/HttpServer"
import * as Error from "@effect/platform/HttpServerError"
import * as ServerRequest from "@effect/platform/HttpServerRequest"
import type * as ServerResponse from "@effect/platform/HttpServerResponse"
import type * as Multipart from "@effect/platform/Multipart"
import type * as Path from "@effect/platform/Path"
import * as Socket from "@effect/platform/Socket"
import type * as Cause from "effect/Cause"
import * as Config from "effect/Config"
import * as Effect from "effect/Effect"
import * as FiberSet from "effect/FiberSet"
import { type LazyArg } from "effect/Function"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
import type { ReadonlyRecord } from "effect/Record"
import * as Scope from "effect/Scope"
import * as Stream from "effect/Stream"
import * as Http from "node:http"
import type * as Net from "node:net"
import type { Duplex } from "node:stream"
import { Readable } from "node:stream"
import { pipeline } from "node:stream/promises"
import * as WS from "ws"
import * as NodeContext from "../NodeContext.js"
import * as NodeHttpClient from "../NodeHttpClient.js"
import * as NodeSink from "../NodeSink.js"
import { HttpIncomingMessageImpl } from "./httpIncomingMessage.js"
import * as internalPlatform from "./httpPlatform.js"
/** @internal */
export const make = (
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions
): Effect.Effect<Server.HttpServer, Error.ServeError, Scope.Scope> =>
Effect.gen(function*(_) {
const scope = yield* Effect.scope
const server = yield* Effect.acquireRelease(
Effect.sync(evaluate),
(server) =>
Effect.async<void>((resume) => {
if (!server.listening) {
return resume(Effect.void)
}
server.close((error) => {
if (error) {
resume(Effect.die(error))
} else {
resume(Effect.void)
}
})
})
)
yield* Effect.async<void, Error.ServeError>((resume) => {
function onError(cause: Error) {
resume(Effect.fail(new Error.ServeError({ cause })))
}
server.on("error", onError)
server.listen(options, () => {
server.off("error", onError)
resume(Effect.void)
})
})
const address = server.address()!
const wss = yield* _(
Effect.acquireRelease(
Effect.sync(() => new WS.WebSocketServer({ noServer: true })),
(wss) =>
Effect.async<void>((resume) => {
wss.close(() => resume(Effect.void))
})
),
Scope.extend(scope),
Effect.cached
)
return Server.make({
address: typeof address === "string" ?
{
_tag: "UnixAddress",
path: address
} :
{
_tag: "TcpAddress",
hostname: address.address === "::" ? "0.0.0.0" : address.address,
port: address.port
},
serve: (httpApp, middleware) =>
Effect.gen(function*(_) {
const handler = yield* _(makeHandler(httpApp, middleware!))
const upgradeHandler = yield* _(makeUpgradeHandler(wss, httpApp, middleware!))
yield* _(Effect.addFinalizer(() =>
Effect.sync(() => {
server.off("request", handler)
server.off("upgrade", upgradeHandler)
})
))
server.on("request", handler)
server.on("upgrade", upgradeHandler)
})
})
}).pipe(
Effect.locally(
IncomingMessage.maxBodySize,
Option.some(FileSystem.Size(1024 * 1024 * 10))
)
)
/** @internal */
export const makeHandler: {
<R, E>(httpApp: App.Default<E, R>): Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void,
never,
Exclude<R, ServerRequest.HttpServerRequest | Scope.Scope>
>
<R, E, App extends App.Default<any, any>>(
httpApp: App.Default<E, R>,
middleware: Middleware.HttpMiddleware.Applied<App, E, R>
): Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void,
never,
Exclude<Effect.Effect.Context<App>, ServerRequest.HttpServerRequest | Scope.Scope>
>
} = <E, R>(httpApp: App.Default<E, R>, middleware?: Middleware.HttpMiddleware) => {
const handledApp = App.toHandled(httpApp, handleResponse, middleware)
return Effect.map(FiberSet.makeRuntime<R>(), (runFork) =>
function handler(
nodeRequest: Http.IncomingMessage,
nodeResponse: Http.ServerResponse
) {
const fiber = runFork(
Effect.provideService(
handledApp,
ServerRequest.HttpServerRequest,
new ServerRequestImpl(nodeRequest, nodeResponse)
)
)
nodeResponse.on("close", () => {
if (!nodeResponse.writableEnded) {
fiber.unsafeInterruptAsFork(Error.clientAbortFiberId)
}
})
})
}
/** @internal */
export const makeUpgradeHandler = <R, E>(
lazyWss: Effect.Effect<WS.WebSocketServer>,
httpApp: App.Default<E, R>,
middleware?: Middleware.HttpMiddleware
) => {
const handledApp = App.toHandled(httpApp, handleResponse, middleware)
return Effect.map(FiberSet.makeRuntime<R>(), (runFork) =>
function handler(
nodeRequest: Http.IncomingMessage,
socket: Duplex,
head: Buffer
) {
let nodeResponse_: Http.ServerResponse | undefined = undefined
const nodeResponse = () => {
if (nodeResponse_ === undefined) {
nodeResponse_ = new Http.ServerResponse(nodeRequest)
nodeResponse_.assignSocket(socket as any)
}
return nodeResponse_
}
const upgradeEffect = Socket.fromWebSocket(Effect.flatMap(
lazyWss,
(wss) =>
Effect.acquireRelease(
Effect.async<globalThis.WebSocket>((resume) =>
wss.handleUpgrade(nodeRequest, socket, head, (ws) => {
resume(Effect.succeed(ws as any))
})
),
(ws) => Effect.sync(() => ws.close())
)
))
const fiber = runFork(
Effect.provideService(
handledApp,
ServerRequest.HttpServerRequest,
new ServerRequestImpl(nodeRequest, nodeResponse, upgradeEffect)
)
)
socket.on("close", () => {
if (!socket.writableEnded) {
fiber.unsafeInterruptAsFork(Error.clientAbortFiberId)
}
})
})
}
class ServerRequestImpl extends HttpIncomingMessageImpl<Error.RequestError> implements ServerRequest.HttpServerRequest {
readonly [ServerRequest.TypeId]: ServerRequest.TypeId
constructor(
readonly source: Http.IncomingMessage,
readonly response: Http.ServerResponse | LazyArg<Http.ServerResponse>,
private upgradeEffect?: Effect.Effect<Socket.Socket, Error.RequestError>,
readonly url = source.url!,
private headersOverride?: Headers.Headers,
remoteAddressOverride?: string
) {
super(source, (cause) =>
new Error.RequestError({
request: this,
reason: "Decode",
cause
}), remoteAddressOverride)
this[ServerRequest.TypeId] = ServerRequest.TypeId
}
private cachedCookies: ReadonlyRecord<string, string> | undefined
get cookies() {
if (this.cachedCookies) {
return this.cachedCookies
}
return this.cachedCookies = Cookies.parseHeader(this.headers.cookie ?? "")
}
get resolvedResponse(): Http.ServerResponse {
return typeof this.response === "function" ? this.response() : this.response
}
modify(
options: {
readonly url?: string | undefined
readonly headers?: Headers.Headers | undefined
readonly remoteAddress?: string | undefined
}
) {
return new ServerRequestImpl(
this.source,
this.response,
this.upgradeEffect,
options.url ?? this.url,
options.headers ?? this.headersOverride,
options.remoteAddress ?? this.remoteAddressOverride
)
}
get originalUrl(): string {
return this.source.url!
}
get method(): HttpMethod {
return this.source.method!.toUpperCase() as HttpMethod
}
get headers(): Headers.Headers {
this.headersOverride ??= this.source.headers as Headers.Headers
return this.headersOverride
}
private multipartEffect:
| Effect.Effect<
Multipart.Persisted,
Multipart.MultipartError,
Scope.Scope | FileSystem.FileSystem | Path.Path
>
| undefined
get multipart(): Effect.Effect<
Multipart.Persisted,
Multipart.MultipartError,
Scope.Scope | FileSystem.FileSystem | Path.Path
> {
if (this.multipartEffect) {
return this.multipartEffect
}
this.multipartEffect = Effect.runSync(Effect.cached(
MultipartNode.persisted(this.source, this.source.headers)
))
return this.multipartEffect
}
get multipartStream(): Stream.Stream<Multipart.Part, Multipart.MultipartError> {
return MultipartNode.stream(this.source, this.source.headers)
}
get upgrade(): Effect.Effect<Socket.Socket, Error.RequestError> {
return this.upgradeEffect ?? Effect.fail(
new Error.RequestError({
request: this,
reason: "Decode",
description: "not an upgradeable ServerRequest"
})
)
}
toString(): string {
return `ServerRequest(${this.method} ${this.url})`
}
toJSON(): unknown {
return IncomingMessage.inspect(this, {
_id: "@effect/platform/HttpServerRequest",
method: this.method,
url: this.originalUrl
})
}
}
/** @internal */
export const layerServer = (
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions
) => Layer.scoped(Server.HttpServer, make(evaluate, options))
/** @internal */
export const layer = (
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions
) =>
Layer.mergeAll(
Layer.scoped(Server.HttpServer, make(evaluate, options)),
internalPlatform.layer,
Etag.layerWeak,
NodeContext.layer
)
/** @internal */
export const layerTest = Server.layerTestClient.pipe(
Layer.provide(NodeHttpClient.layerWithoutAgent),
Layer.provide(NodeHttpClient.makeAgentLayer({ keepAlive: false })),
Layer.provideMerge(layer(Http.createServer, { port: 0 }))
)
/** @internal */
export const layerConfig = (
evaluate: LazyArg<Http.Server>,
options: Config.Config.Wrap<Net.ListenOptions>
) =>
Layer.mergeAll(
Layer.scoped(
Server.HttpServer,
Effect.flatMap(Config.unwrap(options), (options) => make(evaluate, options))
),
internalPlatform.layer,
Etag.layerWeak,
NodeContext.layer
)
const handleResponse = (request: ServerRequest.HttpServerRequest, response: ServerResponse.HttpServerResponse) =>
Effect.suspend((): Effect.Effect<void, Error.ResponseError> => {
const nodeResponse = (request as ServerRequestImpl).resolvedResponse
if (nodeResponse.writableEnded) {
return Effect.void
}
let headers: Record<string, string | Array<string>> = response.headers
if (!Cookies.isEmpty(response.cookies)) {
headers = { ...headers }
const toSet = Cookies.toSetCookieHeaders(response.cookies)
if (headers["set-cookie"] !== undefined) {
toSet.push(headers["set-cookie"] as string)
}
headers["set-cookie"] = toSet
}
if (request.method === "HEAD") {
nodeResponse.writeHead(response.status, headers)
return Effect.async<void>((resume) => {
nodeResponse.end(() => resume(Effect.void))
})
}
const body = response.body
switch (body._tag) {
case "Empty": {
nodeResponse.writeHead(response.status, headers)
nodeResponse.end()
return Effect.void
}
case "Raw": {
nodeResponse.writeHead(response.status, headers)
if (
typeof body.body === "object" && body.body !== null && "pipe" in body.body &&
typeof body.body.pipe === "function"
) {
return Effect.tryPromise({
try: (signal) => pipeline(body.body as any, nodeResponse, { signal, end: true }),
catch: (cause) =>
new Error.ResponseError({
request,
response,
reason: "Decode",
cause
})
}).pipe(
Effect.interruptible,
Effect.tapErrorCause(handleCause(nodeResponse))
)
}
return Effect.async<void>((resume) => {
nodeResponse.end(body.body, () => resume(Effect.void))
})
}
case "Uint8Array": {
nodeResponse.writeHead(response.status, headers)
return Effect.async<void>((resume) => {
nodeResponse.end(body.body, () => resume(Effect.void))
})
}
case "FormData": {
return Effect.suspend(() => {
const r = new Response(body.formData)
nodeResponse.writeHead(response.status, {
...headers,
...Object.fromEntries(r.headers)
})
return Effect.async<void, Error.ResponseError>((resume, signal) => {
Readable.fromWeb(r.body as any, { signal })
.pipe(nodeResponse)
.on("error", (cause) => {
resume(Effect.fail(
new Error.ResponseError({
request,
response,
reason: "Decode",
cause
})
))
})
.once("finish", () => {
resume(Effect.void)
})
}).pipe(
Effect.interruptible,
Effect.tapErrorCause(handleCause(nodeResponse))
)
})
}
case "Stream": {
nodeResponse.writeHead(response.status, headers)
return Stream.run(
Stream.mapError(
body.stream,
(cause) =>
new Error.ResponseError({
request,
response,
reason: "Decode",
cause
})
),
NodeSink.fromWritable(() => nodeResponse, (cause) =>
new Error.ResponseError({
request,
response,
reason: "Decode",
cause
}))
).pipe(
Effect.interruptible,
Effect.tapErrorCause(handleCause(nodeResponse))
)
}
}
})
const handleCause = (nodeResponse: Http.ServerResponse) => <E>(cause: Cause.Cause<E>) =>
Error.causeResponse(cause).pipe(
Effect.flatMap(([response, cause]) => {
if (!nodeResponse.headersSent) {
nodeResponse.writeHead(response.status)
}
if (!nodeResponse.writableEnded) {
nodeResponse.end()
}
return Effect.failCause(cause)
})
)
/** @internal */
export const toIncomingMessage = (self: ServerRequest.HttpServerRequest): Http.IncomingMessage =>
(self as ServerRequestImpl).source
/** @internal */
export const toServerResponse = (self: ServerRequest.HttpServerRequest): Http.ServerResponse => {
const res = (self as ServerRequestImpl).response
return typeof res === "function" ? res() : res
}