Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
improve serve api (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Dec 7, 2023
1 parent b08a685 commit 6b90c81
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .changeset/two-items-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@effect/platform-node": patch
"@effect/platform-bun": patch
---

improve serve api
12 changes: 6 additions & 6 deletions docs/platform-node/Http/Server.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Also includes exports from [`@effect/platform/Http/Server`](https://effect-ts.gi

```ts
export declare const make: (
evaluate: LazyArg<Http.Server>,
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
options: Net.ListenOptions
) => Effect.Effect<Scope.Scope, never, Server.Server>
) => Effect.Effect<Scope.Scope, ServeError, Server.Server>
```
Added in v1.0.0
Expand Down Expand Up @@ -88,9 +88,9 @@ Added in v1.0.0

```ts
export declare const layer: (
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions
) => Layer.Layer<never, never, Server.Server | Platform.Platform>
) => Layer.Layer<never, ServeError, Server.Server | Platform.Platform>
```
Added in v1.0.0
Expand All @@ -101,9 +101,9 @@ Added in v1.0.0
```ts
export declare const layerConfig: (
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
evaluate: LazyArg<Http.Server>,
options: Config.Config.Wrap<Net.ListenOptions>
) => Layer.Layer<never, ConfigError.ConfigError, Server.Server | Platform.Platform>
) => Layer.Layer<never, ServeError | ConfigError.ConfigError, Server.Server | Platform.Platform>
```
Added in v1.0.0
6 changes: 5 additions & 1 deletion docs/platform/Terminal.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ export interface Terminal {
/**
* The number of columns available on the platform's terminal interface.
*/
readonly columns: number
readonly columns: Effect<never, never, number>
/**
* Reads a single input event from the default standard input.
*/
readonly readInput: Effect<never, QuitException, UserInput>
/**
* Reads a single line from the default standard input.
*/
readonly readLine: Effect<never, QuitException, string>
/**
* Displays text to the the default standard output.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/platform-bun/src/internal/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const make = (
const app = (middleware
? middleware(App.withDefaultMiddleware(respond(httpApp)))
: App.withDefaultMiddleware(respond(httpApp))) as App.Default<never, unknown>

return pipe(
Effect.all([Effect.runtime<never>(), Effect.fiberId]),
Effect.zipLeft(
Expand Down
17 changes: 11 additions & 6 deletions packages/platform-node/src/Http/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type * as App from "@effect/platform/Http/App"
import type * as Middleware from "@effect/platform/Http/Middleware"
import type * as Platform from "@effect/platform/Http/Platform"
import type * as Server from "@effect/platform/Http/Server"
import type { ServeError } from "@effect/platform/Http/ServerError"
import type * as ServerRequest from "@effect/platform/Http/ServerRequest"
import type * as Config from "effect/Config"
import type * as ConfigError from "effect/ConfigError"
Expand All @@ -28,9 +29,13 @@ export * from "@effect/platform/Http/Server"
* @category constructors
*/
export const make: (
evaluate: LazyArg<Http.Server>,
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
options: Net.ListenOptions
) => Effect.Effect<Scope.Scope, never, Server.Server> = internal.make
) => Effect.Effect<
Scope.Scope,
ServeError,
Server.Server
> = internal.make

/**
* @since 1.0.0
Expand Down Expand Up @@ -59,15 +64,15 @@ export const makeHandler: {
* @category layers
*/
export const layer: (
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions
) => Layer.Layer<never, never, Server.Server | Platform.Platform> = internal.layer
) => Layer.Layer<never, ServeError, Server.Server | Platform.Platform> = internal.layer

/**
* @since 1.0.0
* @category layers
*/
export const layerConfig: (
evaluate: LazyArg<Http.Server<typeof Http.IncomingMessage, typeof Http.ServerResponse>>,
evaluate: LazyArg<Http.Server>,
options: Config.Config.Wrap<Net.ListenOptions>
) => Layer.Layer<never, ConfigError.ConfigError, Server.Server | Platform.Platform> = internal.layerConfig
) => Layer.Layer<never, ServeError | ConfigError.ConfigError, Server.Server | Platform.Platform> = internal.layerConfig
43 changes: 18 additions & 25 deletions packages/platform-node/src/internal/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type * as ServerResponse from "@effect/platform/Http/ServerResponse"
import type * as Path from "@effect/platform/Path"
import * as Config from "effect/Config"
import * as Effect from "effect/Effect"
import * as Fiber from "effect/Fiber"
import type { LazyArg } from "effect/Function"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
Expand All @@ -32,12 +31,11 @@ import * as internalPlatform from "./platform.js"
export const make = (
evaluate: LazyArg<Http.Server>,
options: Net.ListenOptions
): Effect.Effect<Scope.Scope, never, Server.Server> =>
): Effect.Effect<Scope.Scope, Error.ServeError, Server.Server> =>
Effect.gen(function*(_) {
const server = evaluate()

const serverFiber = yield* _(
Effect.addFinalizer(() =>
const server = yield* _(Effect.acquireRelease(
Effect.sync(evaluate),
(server) =>
Effect.async<never, never, void>((resume) => {
server.close((error) => {
if (error) {
Expand All @@ -47,19 +45,12 @@ export const make = (
}
})
})
),
Effect.zipRight(
Effect.async<never, Error.ServeError, never>((resume) => {
server.on("error", (error) => {
resume(Effect.fail(Error.ServeError({ error })))
})
})
),
Effect.scoped,
Effect.forkScoped
)
))

yield* _(Effect.async<never, never, void>((resume) => {
yield* _(Effect.async<never, Error.ServeError, void>((resume) => {
server.on("error", (error) => {
resume(Effect.fail(Error.ServeError({ error })))
})
server.listen(options, () => {
resume(Effect.unit)
})
Expand All @@ -80,13 +71,15 @@ export const make = (
},
serve: (httpApp, middleware) =>
Effect.flatMap(makeHandler(httpApp, middleware!), (handler) =>
Effect.all([
Effect.acquireRelease(
Effect.sync(() => server.on("request", handler)),
() => Effect.sync(() => server.off("request", handler))
),
Fiber.join(serverFiber)
], { discard: true, concurrency: "unbounded" }) as Effect.Effect<never, Error.ServeError, never>)
Effect.zipRight(
Effect.addFinalizer(() => Effect.sync(() => server.off("request", handler))),
Effect.async<never, Error.ServeError, never>((resume) => {
server.on("request", handler)
server.on("error", (error) => {
resume(Effect.fail(Error.ServeError({ error })))
})
})
))
})
}).pipe(
Effect.locally(
Expand Down

0 comments on commit 6b90c81

Please sign in to comment.