@effect/platform-deno 4.0.0-beta.104
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/DenoChildProcessSpawner.d.ts +39 -0
- package/dist/DenoChildProcessSpawner.d.ts.map +1 -0
- package/dist/DenoChildProcessSpawner.js +311 -0
- package/dist/DenoChildProcessSpawner.js.map +1 -0
- package/dist/DenoClusterHttp.d.ts +60 -0
- package/dist/DenoClusterHttp.d.ts.map +1 -0
- package/dist/DenoClusterHttp.js +78 -0
- package/dist/DenoClusterHttp.js.map +1 -0
- package/dist/DenoClusterSocket.d.ts +65 -0
- package/dist/DenoClusterSocket.d.ts.map +1 -0
- package/dist/DenoClusterSocket.js +101 -0
- package/dist/DenoClusterSocket.js.map +1 -0
- package/dist/DenoCrypto.d.ts +25 -0
- package/dist/DenoCrypto.d.ts.map +1 -0
- package/dist/DenoCrypto.js +52 -0
- package/dist/DenoCrypto.js.map +1 -0
- package/dist/DenoFileSystem.d.ts +10 -0
- package/dist/DenoFileSystem.d.ts.map +1 -0
- package/dist/DenoFileSystem.js +331 -0
- package/dist/DenoFileSystem.js.map +1 -0
- package/dist/DenoHttpClient.d.ts +9 -0
- package/dist/DenoHttpClient.d.ts.map +1 -0
- package/dist/DenoHttpClient.js +9 -0
- package/dist/DenoHttpClient.js.map +1 -0
- package/dist/DenoHttpPlatform.d.ts +34 -0
- package/dist/DenoHttpPlatform.d.ts.map +1 -0
- package/dist/DenoHttpPlatform.js +88 -0
- package/dist/DenoHttpPlatform.js.map +1 -0
- package/dist/DenoHttpServer.d.ts +99 -0
- package/dist/DenoHttpServer.d.ts.map +1 -0
- package/dist/DenoHttpServer.js +353 -0
- package/dist/DenoHttpServer.js.map +1 -0
- package/dist/DenoHttpServerRequest.d.ts +18 -0
- package/dist/DenoHttpServerRequest.d.ts.map +1 -0
- package/dist/DenoHttpServerRequest.js +8 -0
- package/dist/DenoHttpServerRequest.js.map +1 -0
- package/dist/DenoKeyValueStore.d.ts +22 -0
- package/dist/DenoKeyValueStore.d.ts.map +1 -0
- package/dist/DenoKeyValueStore.js +21 -0
- package/dist/DenoKeyValueStore.js.map +1 -0
- package/dist/DenoMultipart.d.ts +32 -0
- package/dist/DenoMultipart.d.ts.map +1 -0
- package/dist/DenoMultipart.js +25 -0
- package/dist/DenoMultipart.js.map +1 -0
- package/dist/DenoPath.d.ts +43 -0
- package/dist/DenoPath.d.ts.map +1 -0
- package/dist/DenoPath.js +81 -0
- package/dist/DenoPath.js.map +1 -0
- package/dist/DenoRedis.d.ts +60 -0
- package/dist/DenoRedis.d.ts.map +1 -0
- package/dist/DenoRedis.js +76 -0
- package/dist/DenoRedis.js.map +1 -0
- package/dist/DenoRuntime.d.ts +36 -0
- package/dist/DenoRuntime.d.ts.map +1 -0
- package/dist/DenoRuntime.js +35 -0
- package/dist/DenoRuntime.js.map +1 -0
- package/dist/DenoServices.d.ts +34 -0
- package/dist/DenoServices.d.ts.map +1 -0
- package/dist/DenoServices.js +16 -0
- package/dist/DenoServices.js.map +1 -0
- package/dist/DenoSocket.d.ts +99 -0
- package/dist/DenoSocket.d.ts.map +1 -0
- package/dist/DenoSocket.js +192 -0
- package/dist/DenoSocket.js.map +1 -0
- package/dist/DenoSocketServer.d.ts +70 -0
- package/dist/DenoSocketServer.d.ts.map +1 -0
- package/dist/DenoSocketServer.js +68 -0
- package/dist/DenoSocketServer.js.map +1 -0
- package/dist/DenoStdio.d.ts +11 -0
- package/dist/DenoStdio.d.ts.map +1 -0
- package/dist/DenoStdio.js +39 -0
- package/dist/DenoStdio.js.map +1 -0
- package/dist/DenoTerminal.d.ts +21 -0
- package/dist/DenoTerminal.d.ts.map +1 -0
- package/dist/DenoTerminal.js +27 -0
- package/dist/DenoTerminal.js.map +1 -0
- package/dist/DenoWorker.d.ts +17 -0
- package/dist/DenoWorker.d.ts.map +1 -0
- package/dist/DenoWorker.js +54 -0
- package/dist/DenoWorker.js.map +1 -0
- package/dist/DenoWorkerRunner.d.ts +24 -0
- package/dist/DenoWorkerRunner.d.ts.map +1 -0
- package/dist/DenoWorkerRunner.js +150 -0
- package/dist/DenoWorkerRunner.js.map +1 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/denoSocketServer.d.ts +7 -0
- package/dist/internal/denoSocketServer.d.ts.map +1 -0
- package/dist/internal/denoSocketServer.js +55 -0
- package/dist/internal/denoSocketServer.js.map +1 -0
- package/dist/internal/error.d.ts +4 -0
- package/dist/internal/error.d.ts.map +1 -0
- package/dist/internal/error.js +46 -0
- package/dist/internal/error.js.map +1 -0
- package/package.json +72 -0
- package/src/DenoChildProcessSpawner.ts +439 -0
- package/src/DenoClusterHttp.ts +156 -0
- package/src/DenoClusterSocket.ts +197 -0
- package/src/DenoCrypto.ts +63 -0
- package/src/DenoFileSystem.ts +497 -0
- package/src/DenoHttpClient.ts +9 -0
- package/src/DenoHttpPlatform.ts +93 -0
- package/src/DenoHttpServer.ts +535 -0
- package/src/DenoHttpServerRequest.ts +18 -0
- package/src/DenoKeyValueStore.ts +28 -0
- package/src/DenoMultipart.ts +53 -0
- package/src/DenoPath.ts +102 -0
- package/src/DenoRedis.ts +113 -0
- package/src/DenoRuntime.ts +73 -0
- package/src/DenoServices.ts +49 -0
- package/src/DenoSocket.ts +318 -0
- package/src/DenoSocketServer.ts +120 -0
- package/src/DenoStdio.ts +51 -0
- package/src/DenoTerminal.ts +32 -0
- package/src/DenoWorker.ts +75 -0
- package/src/DenoWorkerRunner.ts +175 -0
- package/src/index.ts +110 -0
- package/src/internal/denoSocketServer.ts +89 -0
- package/src/internal/error.ts +63 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Deno implementation of the Effect `HttpServer`.
|
|
3
|
+
*
|
|
4
|
+
* @since 4.0.0
|
|
5
|
+
*/
|
|
6
|
+
import * as Config from "effect/Config"
|
|
7
|
+
import type { ConfigError } from "effect/Config"
|
|
8
|
+
import * as Context from "effect/Context"
|
|
9
|
+
import * as Duration from "effect/Duration"
|
|
10
|
+
import * as Effect from "effect/Effect"
|
|
11
|
+
import * as Fiber from "effect/Fiber"
|
|
12
|
+
import type * as FileSystem from "effect/FileSystem"
|
|
13
|
+
import { flow } from "effect/Function"
|
|
14
|
+
import * as Inspectable from "effect/Inspectable"
|
|
15
|
+
import * as Layer from "effect/Layer"
|
|
16
|
+
import * as Option from "effect/Option"
|
|
17
|
+
import type * as Path from "effect/Path"
|
|
18
|
+
import type * as Record from "effect/Record"
|
|
19
|
+
import type * as Schema from "effect/Schema"
|
|
20
|
+
import * as Scope from "effect/Scope"
|
|
21
|
+
import * as Stream from "effect/Stream"
|
|
22
|
+
import * as Cookies from "effect/unstable/http/Cookies"
|
|
23
|
+
import * as Etag from "effect/unstable/http/Etag"
|
|
24
|
+
import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient"
|
|
25
|
+
import * as Headers from "effect/unstable/http/Headers"
|
|
26
|
+
import type * as HttpBody from "effect/unstable/http/HttpBody"
|
|
27
|
+
import type { HttpClient } from "effect/unstable/http/HttpClient"
|
|
28
|
+
import * as HttpEffect from "effect/unstable/http/HttpEffect"
|
|
29
|
+
import * as IncomingMessage from "effect/unstable/http/HttpIncomingMessage"
|
|
30
|
+
import type { HttpMethod } from "effect/unstable/http/HttpMethod"
|
|
31
|
+
import type { HttpPlatform } from "effect/unstable/http/HttpPlatform"
|
|
32
|
+
import * as Server from "effect/unstable/http/HttpServer"
|
|
33
|
+
import * as Error from "effect/unstable/http/HttpServerError"
|
|
34
|
+
import * as ServerRequest from "effect/unstable/http/HttpServerRequest"
|
|
35
|
+
import type * as ServerResponse from "effect/unstable/http/HttpServerResponse"
|
|
36
|
+
import type * as Multipart from "effect/unstable/http/Multipart"
|
|
37
|
+
import * as UrlParams from "effect/unstable/http/UrlParams"
|
|
38
|
+
import * as Socket from "effect/unstable/socket/Socket"
|
|
39
|
+
import * as Platform from "./DenoHttpPlatform.ts"
|
|
40
|
+
import * as DenoMultipart from "./DenoMultipart.ts"
|
|
41
|
+
import * as DenoServices from "./DenoServices.ts"
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Native Deno TCP, TLS, or Unix serve options managed by the scoped server.
|
|
45
|
+
*
|
|
46
|
+
* **Details**
|
|
47
|
+
*
|
|
48
|
+
* `signal` and `onError` are omitted because the scope and Effect HTTP handler
|
|
49
|
+
* own server and failure lifecycles. Server constructors additionally accept
|
|
50
|
+
* WebSocket settings that apply to every upgrade; `protocol` therefore cannot
|
|
51
|
+
* vary per request.
|
|
52
|
+
*
|
|
53
|
+
* @category options
|
|
54
|
+
* @since 4.0.0
|
|
55
|
+
*/
|
|
56
|
+
export type ServeOptions =
|
|
57
|
+
| (Omit<Deno.ServeTcpOptions, "signal" | "onError"> & Partial<Deno.TlsCertifiedKeyPem>)
|
|
58
|
+
| Omit<Deno.ServeUnixOptions, "signal" | "onError">
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a scoped native Deno HTTP server.
|
|
62
|
+
*
|
|
63
|
+
* @category constructors
|
|
64
|
+
* @since 4.0.0
|
|
65
|
+
*/
|
|
66
|
+
export const make = Effect.fnUntraced(function*(
|
|
67
|
+
options: ServeOptions & {
|
|
68
|
+
readonly disablePreemptiveShutdown?: boolean | undefined
|
|
69
|
+
readonly gracefulShutdownTimeout?: Duration.Input | undefined
|
|
70
|
+
readonly websocket?: Deno.UpgradeWebSocketOptions | undefined
|
|
71
|
+
}
|
|
72
|
+
) {
|
|
73
|
+
const scope = yield* Effect.scope
|
|
74
|
+
type Handler = (request: Request, info: Deno.ServeHandlerInfo<Deno.NetAddr | Deno.UnixAddr>) => Promise<Response>
|
|
75
|
+
const notFound: Handler = (_request, _info) => Promise.resolve(new Response("not found", { status: 404 }))
|
|
76
|
+
const handlerStack: Array<Handler> = [notFound]
|
|
77
|
+
let currentHandler = notFound
|
|
78
|
+
const {
|
|
79
|
+
disablePreemptiveShutdown,
|
|
80
|
+
gracefulShutdownTimeout,
|
|
81
|
+
websocket,
|
|
82
|
+
...serveOptions
|
|
83
|
+
} = options
|
|
84
|
+
const server = Deno.serve(
|
|
85
|
+
serveOptions as Deno.ServeTcpOptions,
|
|
86
|
+
(request, info) => currentHandler(request, info)
|
|
87
|
+
) as Deno.HttpServer<Deno.NetAddr | Deno.UnixAddr>
|
|
88
|
+
|
|
89
|
+
const shutdown = yield* Effect.promise(() => server.shutdown()).pipe(Effect.cached)
|
|
90
|
+
const preemptiveShutdown = disablePreemptiveShutdown ? Effect.void : Effect.timeoutOrElse(shutdown, {
|
|
91
|
+
duration: gracefulShutdownTimeout ?? Duration.seconds(20),
|
|
92
|
+
orElse: () => Effect.void
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
yield* Scope.addFinalizer(scope, shutdown)
|
|
96
|
+
|
|
97
|
+
const serverAddress = server.addr
|
|
98
|
+
const address: Server.Address = serverAddress.transport === "unix"
|
|
99
|
+
? { _tag: "UnixAddress", path: serverAddress.path }
|
|
100
|
+
: {
|
|
101
|
+
_tag: "TcpAddress",
|
|
102
|
+
port: (serverAddress as Deno.NetAddr).port,
|
|
103
|
+
hostname: (serverAddress as Deno.NetAddr).hostname
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return Server.make({
|
|
107
|
+
address,
|
|
108
|
+
serve: Effect.fnUntraced(function*(httpApp, middleware) {
|
|
109
|
+
const parent = yield* Effect.fiber
|
|
110
|
+
const services = parent.context
|
|
111
|
+
const serveScope = Context.getUnsafe(services, Scope.Scope)
|
|
112
|
+
const scope = Scope.forkUnsafe(serveScope, "parallel")
|
|
113
|
+
|
|
114
|
+
const httpEffect = HttpEffect.toHandled(httpApp, (request, response) => {
|
|
115
|
+
const denoRequest = request as DenoServerRequest
|
|
116
|
+
if (denoRequest.upgraded) return cancelResponseBody(response.body)
|
|
117
|
+
return Effect.flatMap(
|
|
118
|
+
makeResponse(request, response, services, scope),
|
|
119
|
+
(response) => Effect.sync(() => denoRequest.resolve(response))
|
|
120
|
+
)
|
|
121
|
+
}, middleware)
|
|
122
|
+
|
|
123
|
+
function handler(
|
|
124
|
+
request: Request,
|
|
125
|
+
info: Deno.ServeHandlerInfo<Deno.NetAddr | Deno.UnixAddr>
|
|
126
|
+
): Promise<Response> {
|
|
127
|
+
return new Promise<Response>((resolve) => {
|
|
128
|
+
const context = Context.add(
|
|
129
|
+
services,
|
|
130
|
+
ServerRequest.HttpServerRequest,
|
|
131
|
+
new DenoServerRequest(request, info.remoteAddr, resolve, removeHost(request.url), websocket)
|
|
132
|
+
)
|
|
133
|
+
const fiber = Fiber.runIn(Effect.runForkWith(context)(httpEffect), scope)
|
|
134
|
+
request.signal.addEventListener("abort", () => {
|
|
135
|
+
fiber.interruptUnsafe(parent.id, Error.ClientAbort.annotation)
|
|
136
|
+
}, { once: true })
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
yield* Scope.addFinalizerExit(serveScope, () => {
|
|
141
|
+
const index = handlerStack.lastIndexOf(handler)
|
|
142
|
+
if (index !== -1) handlerStack.splice(index, 1)
|
|
143
|
+
currentHandler = handlerStack[handlerStack.length - 1] ?? notFound
|
|
144
|
+
return preemptiveShutdown
|
|
145
|
+
})
|
|
146
|
+
handlerStack.push(handler)
|
|
147
|
+
currentHandler = handler
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
const makeResponse = Effect.fnUntraced(function*(
|
|
153
|
+
request: ServerRequest.HttpServerRequest,
|
|
154
|
+
response: ServerResponse.HttpServerResponse,
|
|
155
|
+
context: Context.Context<never>,
|
|
156
|
+
scope: Scope.Scope
|
|
157
|
+
) {
|
|
158
|
+
const fields: {
|
|
159
|
+
headers: globalThis.Headers
|
|
160
|
+
status?: number
|
|
161
|
+
statusText?: string
|
|
162
|
+
} = {
|
|
163
|
+
headers: new globalThis.Headers(response.headers),
|
|
164
|
+
status: response.status
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!Cookies.isEmpty(response.cookies)) {
|
|
168
|
+
for (const header of Cookies.toSetCookieHeaders(response.cookies)) {
|
|
169
|
+
fields.headers.append("set-cookie", header)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (response.statusText !== undefined) fields.statusText = response.statusText
|
|
173
|
+
|
|
174
|
+
if (request.method === "HEAD") {
|
|
175
|
+
yield* cancelResponseBody(response.body)
|
|
176
|
+
return new Response(undefined, fields)
|
|
177
|
+
}
|
|
178
|
+
response = HttpEffect.scopeTransferToStream(response)
|
|
179
|
+
const body = response.body
|
|
180
|
+
switch (body._tag) {
|
|
181
|
+
case "Empty":
|
|
182
|
+
return new Response(undefined, fields)
|
|
183
|
+
case "Uint8Array":
|
|
184
|
+
case "Raw": {
|
|
185
|
+
if (body.body instanceof Response) {
|
|
186
|
+
for (const [key, value] of fields.headers.entries()) body.body.headers.set(key, value)
|
|
187
|
+
return body.body
|
|
188
|
+
}
|
|
189
|
+
return new Response(body.body as any, fields)
|
|
190
|
+
}
|
|
191
|
+
case "FormData":
|
|
192
|
+
return new Response(body.formData as any, fields)
|
|
193
|
+
case "Stream":
|
|
194
|
+
return new Response(
|
|
195
|
+
Stream.toReadableStreamWith(
|
|
196
|
+
Stream.unwrap(Effect.withFiber((fiber) => {
|
|
197
|
+
Fiber.runIn(fiber, scope)
|
|
198
|
+
return Effect.succeed(body.stream)
|
|
199
|
+
})),
|
|
200
|
+
context
|
|
201
|
+
),
|
|
202
|
+
fields
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Provides only the native Deno HTTP server.
|
|
209
|
+
*
|
|
210
|
+
* @category layers
|
|
211
|
+
* @since 4.0.0
|
|
212
|
+
*/
|
|
213
|
+
export const layerServer: (
|
|
214
|
+
options: ServeOptions & {
|
|
215
|
+
readonly disablePreemptiveShutdown?: boolean | undefined
|
|
216
|
+
readonly gracefulShutdownTimeout?: Duration.Input | undefined
|
|
217
|
+
readonly websocket?: Deno.UpgradeWebSocketOptions | undefined
|
|
218
|
+
}
|
|
219
|
+
) => Layer.Layer<Server.HttpServer> = flow(
|
|
220
|
+
make,
|
|
221
|
+
Layer.effect(Server.HttpServer)
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Provides Deno HTTP platform services and the standard Deno service set.
|
|
226
|
+
*
|
|
227
|
+
* @category layers
|
|
228
|
+
* @since 4.0.0
|
|
229
|
+
*/
|
|
230
|
+
export const layerHttpServices: Layer.Layer<HttpPlatform | Etag.Generator | DenoServices.DenoServices> = Layer.mergeAll(
|
|
231
|
+
Platform.layer,
|
|
232
|
+
Etag.layerWeak,
|
|
233
|
+
DenoServices.layer
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Provides a native Deno HTTP server together with Deno HTTP services.
|
|
238
|
+
*
|
|
239
|
+
* @category layers
|
|
240
|
+
* @since 4.0.0
|
|
241
|
+
*/
|
|
242
|
+
export const layer = (
|
|
243
|
+
options: ServeOptions & {
|
|
244
|
+
readonly disablePreemptiveShutdown?: boolean | undefined
|
|
245
|
+
readonly gracefulShutdownTimeout?: Duration.Input | undefined
|
|
246
|
+
readonly websocket?: Deno.UpgradeWebSocketOptions | undefined
|
|
247
|
+
}
|
|
248
|
+
): Layer.Layer<Server.HttpServer | HttpPlatform | Etag.Generator | DenoServices.DenoServices> =>
|
|
249
|
+
Layer.mergeAll(layerServer(options), layerHttpServices)
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Starts a Deno HTTP server on an ephemeral loopback port for tests.
|
|
253
|
+
*
|
|
254
|
+
* @category testing
|
|
255
|
+
* @since 4.0.0
|
|
256
|
+
*/
|
|
257
|
+
export const layerTest: Layer.Layer<
|
|
258
|
+
Server.HttpServer | HttpPlatform | FileSystem.FileSystem | Etag.Generator | Path.Path | HttpClient
|
|
259
|
+
> = Server.layerTestClient.pipe(
|
|
260
|
+
Layer.provide(FetchHttpClient.layer.pipe(
|
|
261
|
+
Layer.provide(Layer.succeed(FetchHttpClient.RequestInit)({ keepalive: false }))
|
|
262
|
+
)),
|
|
263
|
+
Layer.provideMerge(layer({ hostname: "127.0.0.1", port: 0, onListen: () => {} }))
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Creates the Deno HTTP server and support-services layer from configurable options.
|
|
268
|
+
*
|
|
269
|
+
* @category layers
|
|
270
|
+
* @since 4.0.0
|
|
271
|
+
*/
|
|
272
|
+
export const layerConfig = (
|
|
273
|
+
options: Config.Wrap<
|
|
274
|
+
ServeOptions & {
|
|
275
|
+
readonly disablePreemptiveShutdown?: boolean | undefined
|
|
276
|
+
readonly gracefulShutdownTimeout?: Duration.Input | undefined
|
|
277
|
+
readonly websocket?: Deno.UpgradeWebSocketOptions | undefined
|
|
278
|
+
}
|
|
279
|
+
>
|
|
280
|
+
): Layer.Layer<
|
|
281
|
+
Server.HttpServer | HttpPlatform | FileSystem.FileSystem | Etag.Generator | Path.Path,
|
|
282
|
+
ConfigError
|
|
283
|
+
> =>
|
|
284
|
+
Layer.mergeAll(
|
|
285
|
+
Layer.effect(Server.HttpServer)(Effect.flatMap(Config.unwrap(options), make)),
|
|
286
|
+
layerHttpServices
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpServerRequest {
|
|
290
|
+
readonly [ServerRequest.TypeId]: typeof ServerRequest.TypeId
|
|
291
|
+
readonly [IncomingMessage.TypeId]: typeof IncomingMessage.TypeId
|
|
292
|
+
readonly source: Request
|
|
293
|
+
readonly remoteAddr: Deno.NetAddr | Deno.UnixAddr
|
|
294
|
+
readonly url: string
|
|
295
|
+
readonly websocketOptions: Deno.UpgradeWebSocketOptions | undefined
|
|
296
|
+
public resolve: (response: Response) => void
|
|
297
|
+
public upgraded = false
|
|
298
|
+
public headersOverride?: Headers.Headers | undefined
|
|
299
|
+
private remoteAddressOverride?: Option.Option<string> | undefined
|
|
300
|
+
|
|
301
|
+
constructor(
|
|
302
|
+
source: Request,
|
|
303
|
+
remoteAddr: Deno.NetAddr | Deno.UnixAddr,
|
|
304
|
+
resolve: (response: Response) => void,
|
|
305
|
+
url: string,
|
|
306
|
+
websocketOptions: Deno.UpgradeWebSocketOptions | undefined,
|
|
307
|
+
headersOverride?: Headers.Headers,
|
|
308
|
+
remoteAddressOverride?: Option.Option<string>
|
|
309
|
+
) {
|
|
310
|
+
super()
|
|
311
|
+
this[ServerRequest.TypeId] = ServerRequest.TypeId
|
|
312
|
+
this[IncomingMessage.TypeId] = IncomingMessage.TypeId
|
|
313
|
+
this.source = source
|
|
314
|
+
this.remoteAddr = remoteAddr
|
|
315
|
+
this.resolve = resolve
|
|
316
|
+
this.url = url
|
|
317
|
+
this.websocketOptions = websocketOptions
|
|
318
|
+
this.headersOverride = headersOverride
|
|
319
|
+
this.remoteAddressOverride = remoteAddressOverride
|
|
320
|
+
}
|
|
321
|
+
toJSON(): unknown {
|
|
322
|
+
return IncomingMessage.inspect(this, {
|
|
323
|
+
_id: "HttpServerRequest",
|
|
324
|
+
method: this.method,
|
|
325
|
+
url: this.originalUrl
|
|
326
|
+
})
|
|
327
|
+
}
|
|
328
|
+
modify(options: {
|
|
329
|
+
readonly url?: string | undefined
|
|
330
|
+
readonly headers?: Headers.Headers | undefined
|
|
331
|
+
readonly remoteAddress?: Option.Option<string> | undefined
|
|
332
|
+
}) {
|
|
333
|
+
return new DenoServerRequest(
|
|
334
|
+
this.source,
|
|
335
|
+
this.remoteAddr,
|
|
336
|
+
this.resolve,
|
|
337
|
+
options.url ?? this.url,
|
|
338
|
+
this.websocketOptions,
|
|
339
|
+
options.headers ?? this.headersOverride,
|
|
340
|
+
"remoteAddress" in options ? options.remoteAddress : this.remoteAddressOverride
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
get method(): HttpMethod {
|
|
344
|
+
return this.source.method.toUpperCase() as HttpMethod
|
|
345
|
+
}
|
|
346
|
+
get originalUrl() {
|
|
347
|
+
return this.source.url
|
|
348
|
+
}
|
|
349
|
+
get remoteAddress(): Option.Option<string> {
|
|
350
|
+
return this.remoteAddressOverride ?? (this.remoteAddr.transport === "tcp"
|
|
351
|
+
? Option.some(this.remoteAddr.hostname)
|
|
352
|
+
: Option.none())
|
|
353
|
+
}
|
|
354
|
+
get headers(): Headers.Headers {
|
|
355
|
+
this.headersOverride ??= Headers.fromInput(this.source.headers)
|
|
356
|
+
return this.headersOverride
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
private cachedCookies: Record.ReadonlyRecord<string, string> | undefined
|
|
360
|
+
get cookies() {
|
|
361
|
+
return this.cachedCookies ??= Cookies.parseHeader(this.headers.cookie ?? "")
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
get stream(): Stream.Stream<Uint8Array, Error.HttpServerError> {
|
|
365
|
+
return this.source.body
|
|
366
|
+
? Stream.fromReadableStream({
|
|
367
|
+
evaluate: () => this.source.body ?? emptyReadableStream,
|
|
368
|
+
onError: (cause) =>
|
|
369
|
+
new Error.HttpServerError({
|
|
370
|
+
reason: new Error.RequestParseError({ request: this, cause })
|
|
371
|
+
})
|
|
372
|
+
})
|
|
373
|
+
: Stream.fail(
|
|
374
|
+
new Error.HttpServerError({
|
|
375
|
+
reason: new Error.RequestParseError({
|
|
376
|
+
request: this,
|
|
377
|
+
description: "can not create stream from empty body"
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
private textEffect: Effect.Effect<string, Error.HttpServerError> | undefined
|
|
384
|
+
get text(): Effect.Effect<string, Error.HttpServerError> {
|
|
385
|
+
return this.textEffect ??= Effect.runSync(Effect.cached(
|
|
386
|
+
Effect.tryPromise({
|
|
387
|
+
try: () => this.source.text(),
|
|
388
|
+
catch: (cause) =>
|
|
389
|
+
new Error.HttpServerError({
|
|
390
|
+
reason: new Error.RequestParseError({ request: this, cause })
|
|
391
|
+
})
|
|
392
|
+
})
|
|
393
|
+
))
|
|
394
|
+
}
|
|
395
|
+
get json(): Effect.Effect<Schema.Json, Error.HttpServerError> {
|
|
396
|
+
return Effect.flatMap(this.text, (_) =>
|
|
397
|
+
Effect.try({
|
|
398
|
+
try: () => JSON.parse(_) as Schema.Json,
|
|
399
|
+
catch: (cause) =>
|
|
400
|
+
new Error.HttpServerError({
|
|
401
|
+
reason: new Error.RequestParseError({ request: this, cause })
|
|
402
|
+
})
|
|
403
|
+
}))
|
|
404
|
+
}
|
|
405
|
+
get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, Error.HttpServerError> {
|
|
406
|
+
return Effect.flatMap(this.text, (_) =>
|
|
407
|
+
Effect.try({
|
|
408
|
+
try: () => UrlParams.fromInput(new URLSearchParams(_)),
|
|
409
|
+
catch: (cause) =>
|
|
410
|
+
new Error.HttpServerError({
|
|
411
|
+
reason: new Error.RequestParseError({ request: this, cause })
|
|
412
|
+
})
|
|
413
|
+
}))
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
private multipartEffect:
|
|
417
|
+
| Effect.Effect<Multipart.Persisted, Multipart.MultipartError, Scope.Scope | FileSystem.FileSystem | Path.Path>
|
|
418
|
+
| undefined
|
|
419
|
+
get multipart(): Effect.Effect<
|
|
420
|
+
Multipart.Persisted,
|
|
421
|
+
Multipart.MultipartError,
|
|
422
|
+
Scope.Scope | FileSystem.FileSystem | Path.Path
|
|
423
|
+
> {
|
|
424
|
+
return this.multipartEffect ??= Effect.runSync(Effect.cached(DenoMultipart.persisted(this.source)))
|
|
425
|
+
}
|
|
426
|
+
get multipartStream(): Stream.Stream<Multipart.Part, Multipart.MultipartError> {
|
|
427
|
+
return DenoMultipart.stream(this.source)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private arrayBufferEffect: Effect.Effect<ArrayBuffer, Error.HttpServerError> | undefined
|
|
431
|
+
get arrayBuffer(): Effect.Effect<ArrayBuffer, Error.HttpServerError> {
|
|
432
|
+
if (this.arrayBufferEffect) return this.arrayBufferEffect
|
|
433
|
+
this.arrayBufferEffect = Effect.runSync(Effect.cached(
|
|
434
|
+
Effect.tryPromise({
|
|
435
|
+
try: () => this.source.arrayBuffer(),
|
|
436
|
+
catch: (cause) =>
|
|
437
|
+
new Error.HttpServerError({
|
|
438
|
+
reason: new Error.RequestParseError({ request: this, cause })
|
|
439
|
+
})
|
|
440
|
+
})
|
|
441
|
+
))
|
|
442
|
+
this.textEffect = Effect.map(this.arrayBufferEffect, (_) => new TextDecoder().decode(_))
|
|
443
|
+
return this.arrayBufferEffect
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
get upgrade(): Effect.Effect<Socket.Socket, Error.HttpServerError> {
|
|
447
|
+
return Effect.flatMap(
|
|
448
|
+
Effect.try({
|
|
449
|
+
try: () => Deno.upgradeWebSocket(this.source, this.websocketOptions),
|
|
450
|
+
catch: (cause) =>
|
|
451
|
+
new Error.HttpServerError({
|
|
452
|
+
reason: new Error.RequestParseError({
|
|
453
|
+
request: this,
|
|
454
|
+
cause,
|
|
455
|
+
description: "Not an upgradeable ServerRequest"
|
|
456
|
+
})
|
|
457
|
+
})
|
|
458
|
+
}),
|
|
459
|
+
(upgrade) => {
|
|
460
|
+
const buffered: Array<MessageEvent> = []
|
|
461
|
+
const buffer = (event: MessageEvent) => buffered.push(event)
|
|
462
|
+
upgrade.socket.addEventListener("message", buffer)
|
|
463
|
+
this.upgraded = true
|
|
464
|
+
this.resolve(upgrade.response)
|
|
465
|
+
|
|
466
|
+
return Effect.callback<Socket.Socket, Error.HttpServerError>((resume) => {
|
|
467
|
+
const cleanup = () => {
|
|
468
|
+
upgrade.socket.removeEventListener("open", onOpen)
|
|
469
|
+
upgrade.socket.removeEventListener("error", onFailure)
|
|
470
|
+
upgrade.socket.removeEventListener("close", onFailure)
|
|
471
|
+
}
|
|
472
|
+
const onFailure = (cause: Event) => {
|
|
473
|
+
cleanup()
|
|
474
|
+
upgrade.socket.removeEventListener("message", buffer)
|
|
475
|
+
buffered.length = 0
|
|
476
|
+
resume(Effect.fail(
|
|
477
|
+
new Error.HttpServerError({
|
|
478
|
+
reason: new Error.RequestParseError({
|
|
479
|
+
request: this,
|
|
480
|
+
cause,
|
|
481
|
+
description: "WebSocket upgrade failed before open"
|
|
482
|
+
})
|
|
483
|
+
})
|
|
484
|
+
))
|
|
485
|
+
}
|
|
486
|
+
const onOpen = () => {
|
|
487
|
+
cleanup()
|
|
488
|
+
resume(Socket.fromWebSocket(
|
|
489
|
+
Effect.acquireRelease(
|
|
490
|
+
Effect.succeed(upgrade.socket),
|
|
491
|
+
(socket) => Effect.sync(() => socket.close(1000))
|
|
492
|
+
),
|
|
493
|
+
{
|
|
494
|
+
onInitialRun: (socket) => {
|
|
495
|
+
socket.removeEventListener("message", buffer)
|
|
496
|
+
return buffered.splice(0)
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
))
|
|
500
|
+
}
|
|
501
|
+
upgrade.socket.addEventListener("open", onOpen, { once: true })
|
|
502
|
+
upgrade.socket.addEventListener("error", onFailure, { once: true })
|
|
503
|
+
upgrade.socket.addEventListener("close", onFailure, { once: true })
|
|
504
|
+
return Effect.sync(() => {
|
|
505
|
+
cleanup()
|
|
506
|
+
upgrade.socket.removeEventListener("message", buffer)
|
|
507
|
+
buffered.length = 0
|
|
508
|
+
upgrade.socket.close()
|
|
509
|
+
})
|
|
510
|
+
})
|
|
511
|
+
}
|
|
512
|
+
)
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const cancelResponseBody = (body: HttpBody.HttpBody): Effect.Effect<void> => {
|
|
517
|
+
const stream = (body as any).body
|
|
518
|
+
if ((body._tag === "Raw" || body._tag === "Uint8Array") && stream instanceof ReadableStream) {
|
|
519
|
+
return Effect.ignoreCause(Effect.promise(() => stream.cancel()))
|
|
520
|
+
}
|
|
521
|
+
return Effect.void
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const emptyReadableStream = new ReadableStream<Uint8Array>({
|
|
525
|
+
start(controller) {
|
|
526
|
+
controller.enqueue(new Uint8Array())
|
|
527
|
+
controller.close()
|
|
528
|
+
}
|
|
529
|
+
})
|
|
530
|
+
|
|
531
|
+
const removeHost = (url: string) => {
|
|
532
|
+
if (url[0] === "/") return url
|
|
533
|
+
const index = url.indexOf("/", url.indexOf("//") + 2)
|
|
534
|
+
return index === -1 ? "/" : url.slice(index)
|
|
535
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accessor for the web-standard request behind a Deno HTTP server request.
|
|
3
|
+
*
|
|
4
|
+
* Unlike Bun's accessor, this function has no route-pattern generic because
|
|
5
|
+
* `Deno.serve` always receives a standard `Request`. Connection information is
|
|
6
|
+
* intentionally kept internal to `DenoHttpServer`.
|
|
7
|
+
*
|
|
8
|
+
* @since 4.0.0
|
|
9
|
+
*/
|
|
10
|
+
import type { HttpServerRequest } from "effect/unstable/http/HttpServerRequest"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns the underlying web-standard `Request` from an Effect `HttpServerRequest`.
|
|
14
|
+
*
|
|
15
|
+
* @category accessors
|
|
16
|
+
* @since 4.0.0
|
|
17
|
+
*/
|
|
18
|
+
export const toDenoServerRequest = (self: HttpServerRequest): Request => (self as any).source
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deno-backed `KeyValueStore` layers using Web Storage APIs.
|
|
3
|
+
*
|
|
4
|
+
* @since 4.0.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type * as Layer from "effect/Layer"
|
|
8
|
+
import * as KeyValueStore from "effect/unstable/persistence/KeyValueStore"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a `KeyValueStore` layer backed by `localStorage`, with values stored between sessions.
|
|
12
|
+
*
|
|
13
|
+
* @category layers
|
|
14
|
+
* @since 4.0.0
|
|
15
|
+
*/
|
|
16
|
+
export const layerLocalStorage: Layer.Layer<KeyValueStore.KeyValueStore> = KeyValueStore.layerStorage(() =>
|
|
17
|
+
localStorage
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a `KeyValueStore` layer backed by `sessionStorage`, with values stored only for the current session.
|
|
22
|
+
*
|
|
23
|
+
* @category layers
|
|
24
|
+
* @since 4.0.0
|
|
25
|
+
*/
|
|
26
|
+
export const layerSessionStorage: Layer.Layer<KeyValueStore.KeyValueStore> = KeyValueStore.layerStorage(() =>
|
|
27
|
+
sessionStorage
|
|
28
|
+
)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-standard helpers for parsing HTTP `multipart/form-data` request bodies.
|
|
3
|
+
*
|
|
4
|
+
* This module mirrors `BunMultipart`, adapting a web `Request` body and headers
|
|
5
|
+
* into the shared `Multipart` model. `stream` returns multipart parts as a
|
|
6
|
+
* `Stream`, while `persisted` collects the form and writes file parts to scoped
|
|
7
|
+
* temporary files through the current `FileSystem`, `Path`, and `Scope`
|
|
8
|
+
* services.
|
|
9
|
+
*
|
|
10
|
+
* @since 4.0.0
|
|
11
|
+
*/
|
|
12
|
+
import type * as Effect from "effect/Effect"
|
|
13
|
+
import type { FileSystem } from "effect/FileSystem"
|
|
14
|
+
import type { Path } from "effect/Path"
|
|
15
|
+
import type * as Scope from "effect/Scope"
|
|
16
|
+
import * as Stream from "effect/Stream"
|
|
17
|
+
import * as Multipart from "effect/unstable/http/Multipart"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parses a web `Request` body as multipart data and returns a stream of multipart parts.
|
|
21
|
+
*
|
|
22
|
+
* @category constructors
|
|
23
|
+
* @since 4.0.0
|
|
24
|
+
*/
|
|
25
|
+
export const stream = (source: Request): Stream.Stream<Multipart.Part, Multipart.MultipartError> =>
|
|
26
|
+
Stream.fromReadableStream({
|
|
27
|
+
evaluate: () =>
|
|
28
|
+
source.body ?? new ReadableStream({
|
|
29
|
+
start(controller) {
|
|
30
|
+
controller.enqueue(new Uint8Array())
|
|
31
|
+
controller.close()
|
|
32
|
+
}
|
|
33
|
+
}),
|
|
34
|
+
onError: (cause) => Multipart.MultipartError.fromReason("InternalError", cause)
|
|
35
|
+
}).pipe(
|
|
36
|
+
Stream.pipeThroughChannel(Multipart.makeChannel(Object.fromEntries(source.headers)))
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Parses and persists multipart data from a web `Request`, requiring file-system, path, and scope services.
|
|
41
|
+
*
|
|
42
|
+
* @category constructors
|
|
43
|
+
* @since 4.0.0
|
|
44
|
+
*/
|
|
45
|
+
export const persisted = (
|
|
46
|
+
source: Request
|
|
47
|
+
): Effect.Effect<
|
|
48
|
+
Multipart.Persisted,
|
|
49
|
+
Multipart.MultipartError,
|
|
50
|
+
| FileSystem
|
|
51
|
+
| Path
|
|
52
|
+
| Scope.Scope
|
|
53
|
+
> => Multipart.toPersisted(stream(source))
|