@effect/platform-node 0.27.3 → 0.27.4

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.
Files changed (33) hide show
  1. package/Http/Server/dist/effect-platform-node-Http-Server.cjs.dev.js +3 -1
  2. package/Http/Server/dist/effect-platform-node-Http-Server.cjs.prod.js +3 -1
  3. package/Http/Server/dist/effect-platform-node-Http-Server.esm.js +2 -1
  4. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.d.mts +2 -0
  5. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.d.mts.map +1 -0
  6. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.d.ts +2 -0
  7. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.d.ts.map +1 -0
  8. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.dev.js +75 -0
  9. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.js +7 -0
  10. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.mjs +1 -0
  11. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.cjs.prod.js +75 -0
  12. package/Http/ServerRequest/dist/effect-platform-node-Http-ServerRequest.esm.js +65 -0
  13. package/Http/ServerRequest/package.json +4 -0
  14. package/HttpServer/dist/effect-platform-node-HttpServer.cjs.dev.js +6 -4
  15. package/HttpServer/dist/effect-platform-node-HttpServer.cjs.prod.js +6 -4
  16. package/HttpServer/dist/effect-platform-node-HttpServer.esm.js +5 -3
  17. package/dist/Server-1f2aa4ca.cjs.dev.js +83 -0
  18. package/dist/Server-96c17c96.cjs.prod.js +83 -0
  19. package/dist/Server-c315ddf2.esm.js +57 -0
  20. package/dist/declarations/src/Http/Server.d.ts +11 -0
  21. package/dist/declarations/src/Http/Server.d.ts.map +1 -1
  22. package/dist/declarations/src/Http/ServerRequest.d.ts +18 -0
  23. package/dist/declarations/src/Http/ServerRequest.d.ts.map +1 -0
  24. package/dist/declarations/src/HttpServer.d.ts +3 -3
  25. package/dist/declarations/src/HttpServer.d.ts.map +1 -1
  26. package/dist/{Server-9108df65.cjs.prod.js → server-42346e37.cjs.prod.js} +29 -69
  27. package/dist/{Server-c9262046.esm.js → server-442c5e69.esm.js} +29 -70
  28. package/dist/{Server-7a3dbcbe.cjs.dev.js → server-b13861a8.cjs.dev.js} +29 -69
  29. package/package.json +7 -1
  30. package/src/Http/Server.ts +25 -0
  31. package/src/Http/ServerRequest.ts +19 -0
  32. package/src/HttpServer.ts +3 -3
  33. package/src/internal/http/server.ts +49 -24
@@ -3,8 +3,11 @@
3
3
  *
4
4
  * Also includes exports from [`@effect/platform/Http/Server`](https://effect-ts.github.io/platform/platform/Http/Server.ts.html).
5
5
  */
6
+ import type * as App from "@effect/platform/Http/App"
7
+ import type * as Middleware from "@effect/platform/Http/Middleware"
6
8
  import type * as Platform from "@effect/platform/Http/Platform"
7
9
  import type * as Server from "@effect/platform/Http/Server"
10
+ import type * as ServerRequest from "@effect/platform/Http/ServerRequest"
8
11
  import type * as Config from "effect/Config"
9
12
  import type * as ConfigError from "effect/ConfigError"
10
13
  import type * as Effect from "effect/Effect"
@@ -29,6 +32,28 @@ export const make: (
29
32
  options: Net.ListenOptions
30
33
  ) => Effect.Effect<Scope.Scope, never, Server.Server> = internal.make
31
34
 
35
+ /**
36
+ * @since 1.0.0
37
+ * @category constructors
38
+ */
39
+ export const makeHandler: {
40
+ <R, E>(
41
+ httpApp: App.Default<R, E>
42
+ ): Effect.Effect<
43
+ Exclude<R, ServerRequest.ServerRequest>,
44
+ never,
45
+ (nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse<Http.IncomingMessage>) => void
46
+ >
47
+ <R, E, App extends App.Default<any, any>>(
48
+ httpApp: App.Default<R, E>,
49
+ middleware: Middleware.Middleware.Applied<R, E, App>
50
+ ): Effect.Effect<
51
+ Exclude<Effect.Effect.Context<App>, ServerRequest.ServerRequest>,
52
+ never,
53
+ (nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse<Http.IncomingMessage>) => void
54
+ >
55
+ } = internal.makeHandler
56
+
32
57
  /**
33
58
  * @since 1.0.0
34
59
  * @category layers
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @since 1.0.0
3
+ *
4
+ * Also includes exports from [`@effect/platform/Http/ServerRequest`](https://effect-ts.github.io/platform/platform/Http/ServerRequest.ts.html).
5
+ */
6
+ import type * as ServerRequest from "@effect/platform/Http/ServerRequest"
7
+ import type * as Http from "node:http"
8
+ import * as internal from "../internal/http/server"
9
+
10
+ /**
11
+ * @since 1.0.0
12
+ */
13
+ export * from "@effect/platform/Http/ServerRequest"
14
+
15
+ /**
16
+ * @category conversions
17
+ * @since 1.0.0
18
+ */
19
+ export const toIncomingMessage: (self: ServerRequest.ServerRequest) => Http.IncomingMessage = internal.requestSource
package/src/HttpServer.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
+ import * as request from "@effect/platform-node/Http/ServerRequest"
4
5
  import * as app from "@effect/platform/Http/App"
5
6
  import * as body from "@effect/platform/Http/Body"
6
7
  import * as headers from "@effect/platform/Http/Headers"
7
8
  import * as middleware from "@effect/platform/Http/Middleware"
8
9
  import * as router from "@effect/platform/Http/Router"
9
10
  import * as error from "@effect/platform/Http/ServerError"
10
- import * as request from "@effect/platform/Http/ServerRequest"
11
11
  import * as response from "@effect/platform/Http/ServerResponse"
12
12
  import * as urlParams from "@effect/platform/Http/UrlParams"
13
13
  import * as etag from "./Http/Etag"
@@ -67,8 +67,8 @@ export {
67
67
  /**
68
68
  * @since 1.0.0
69
69
  *
70
- * - Docs: [Http/ServerRequest](https://effect-ts.github.io/platform/platform/Http/ServerRequest.ts.html)
71
- * - Module: `@effect/platform/Http/ServerRequest`
70
+ * - Docs: [Http/ServerRequest](https://effect-ts.github.io/platform/platform-node/Http/ServerRequest.ts.html)
71
+ * - Module: `@effect/platform-node/Http/ServerRequest`
72
72
  */
73
73
  request,
74
74
  /**
@@ -78,35 +78,15 @@ export const make = (
78
78
  hostname: address.address,
79
79
  port: address.port
80
80
  },
81
- serve: (httpApp, middleware) => {
82
- const handledApp = middleware
83
- ? middleware(App.withDefaultMiddleware(respond(httpApp)))
84
- : App.withDefaultMiddleware(respond(httpApp))
85
- return Effect.flatMap(Effect.all([Effect.runtime(), Effect.fiberId]), ([runtime, fiberId]) => {
86
- const runFork = Runtime.runFork(runtime)
87
- function handler(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) {
88
- const fiber = runFork(
89
- Effect.provideService(
90
- handledApp,
91
- ServerRequest.ServerRequest,
92
- new ServerRequestImpl(nodeRequest, nodeResponse)
93
- )
94
- )
95
- nodeResponse.on("close", () => {
96
- if (!nodeResponse.writableEnded) {
97
- runFork(fiber.interruptAsFork(fiberId))
98
- }
99
- })
100
- }
101
- return Effect.all([
81
+ serve: (httpApp, middleware) =>
82
+ Effect.flatMap(makeHandler(httpApp, middleware!), (handler) =>
83
+ Effect.all([
102
84
  Effect.acquireRelease(
103
85
  Effect.sync(() => server.on("request", handler)),
104
86
  () => Effect.sync(() => server.off("request", handler))
105
87
  ),
106
88
  Fiber.join(serverFiber)
107
- ], { discard: true, concurrency: "unbounded" }) as Effect.Effect<never, Error.ServeError, never>
108
- })
109
- }
89
+ ], { discard: true, concurrency: "unbounded" }) as Effect.Effect<never, Error.ServeError, never>)
110
90
  })
111
91
  }).pipe(
112
92
  Effect.locally(
@@ -138,6 +118,47 @@ const respond = Middleware.make((httpApp) =>
138
118
  ))
139
119
  )
140
120
 
121
+ /** @internal */
122
+ export const makeHandler: {
123
+ <R, E>(httpApp: App.Default<R, E>): Effect.Effect<
124
+ Exclude<R, ServerRequest.ServerRequest>,
125
+ never,
126
+ (nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void
127
+ >
128
+ <R, E, App extends App.Default<any, any>>(
129
+ httpApp: App.Default<R, E>,
130
+ middleware: Middleware.Middleware.Applied<R, E, App>
131
+ ): Effect.Effect<
132
+ Exclude<Effect.Effect.Context<App>, ServerRequest.ServerRequest>,
133
+ never,
134
+ (nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void
135
+ >
136
+ } = <R, E>(httpApp: App.Default<R, E>, middleware?: Middleware.Middleware) => {
137
+ const handledApp = middleware
138
+ ? middleware(App.withDefaultMiddleware(respond(httpApp)))
139
+ : App.withDefaultMiddleware(respond(httpApp))
140
+ return Effect.map(
141
+ Effect.zip(Effect.runtime<R>(), Effect.fiberId),
142
+ ([runtime, fiberId]) => {
143
+ const runFork = Runtime.runFork(runtime)
144
+ return function handler(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) {
145
+ const fiber = runFork(
146
+ Effect.provideService(
147
+ handledApp,
148
+ ServerRequest.ServerRequest,
149
+ new ServerRequestImpl(nodeRequest, nodeResponse)
150
+ )
151
+ )
152
+ nodeResponse.on("close", () => {
153
+ if (!nodeResponse.writableEnded) {
154
+ runFork(fiber.interruptAsFork(fiberId))
155
+ }
156
+ })
157
+ }
158
+ }
159
+ )
160
+ }
161
+
141
162
  class ServerRequestImpl extends IncomingMessageImpl<Error.RequestError> implements ServerRequest.ServerRequest {
142
163
  readonly [ServerRequest.TypeId]: ServerRequest.TypeId
143
164
 
@@ -339,3 +360,7 @@ const handleResponse = (request: ServerRequest.ServerRequest, response: ServerRe
339
360
  }
340
361
  }
341
362
  })
363
+
364
+ /** @internal */
365
+ export const requestSource = (self: ServerRequest.ServerRequest): Http.IncomingMessage =>
366
+ (self as ServerRequestImpl).source