@effect/platform-deno 4.0.0-rc.111 → 4.0.0-rc.113

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 (57) hide show
  1. package/AGENTS.md +24 -9
  2. package/CLAUDE.md +24 -9
  3. package/ai-docs/package.json +2 -2
  4. package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +18 -5
  5. package/ai-docs/src/01_effect/01_basics/index.md +5 -3
  6. package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +1 -1
  7. package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +2 -2
  8. package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +2 -2
  9. package/ai-docs/src/03_stream/30_encoding.ts +5 -7
  10. package/ai-docs/src/08_observability/10_logging.ts +1 -1
  11. package/ai-docs/src/70_cli/10_basics.ts +7 -7
  12. package/ai-docs/src/71_ai/10_language-model.ts +2 -2
  13. package/ai-docs/src/71_ai/20_tools.ts +1 -1
  14. package/ai-docs/src/71_ai/30_chat.ts +1 -1
  15. package/dist/DenoClusterHttp.d.ts +1 -1
  16. package/dist/DenoClusterHttp.d.ts.map +1 -1
  17. package/dist/DenoClusterHttp.js +1 -1
  18. package/dist/DenoClusterHttp.js.map +1 -1
  19. package/dist/DenoClusterSocket.d.ts +2 -2
  20. package/dist/DenoClusterSocket.d.ts.map +1 -1
  21. package/dist/DenoClusterSocket.js +16 -11
  22. package/dist/DenoClusterSocket.js.map +1 -1
  23. package/dist/DenoFileSystem.d.ts.map +1 -1
  24. package/dist/DenoFileSystem.js +41 -29
  25. package/dist/DenoFileSystem.js.map +1 -1
  26. package/dist/DenoHttpPlatform.d.ts +6 -6
  27. package/dist/DenoHttpPlatform.js +4 -4
  28. package/dist/DenoHttpPlatform.js.map +1 -1
  29. package/dist/DenoHttpServer.d.ts +7 -5
  30. package/dist/DenoHttpServer.d.ts.map +1 -1
  31. package/dist/DenoHttpServer.js +85 -48
  32. package/dist/DenoHttpServer.js.map +1 -1
  33. package/dist/DenoRedis.d.ts.map +1 -1
  34. package/dist/DenoRedis.js +3 -0
  35. package/dist/DenoRedis.js.map +1 -1
  36. package/dist/DenoSocket.d.ts +16 -3
  37. package/dist/DenoSocket.d.ts.map +1 -1
  38. package/dist/DenoSocket.js +128 -39
  39. package/dist/DenoSocket.js.map +1 -1
  40. package/dist/DenoSocketServer.d.ts +1 -63
  41. package/dist/DenoSocketServer.d.ts.map +1 -1
  42. package/dist/DenoSocketServer.js +1 -61
  43. package/dist/DenoSocketServer.js.map +1 -1
  44. package/package.json +6 -6
  45. package/src/DenoClusterHttp.ts +2 -2
  46. package/src/DenoClusterSocket.ts +20 -15
  47. package/src/DenoFileSystem.ts +43 -31
  48. package/src/DenoHttpPlatform.ts +4 -4
  49. package/src/DenoHttpServer.ts +98 -60
  50. package/src/DenoRedis.ts +3 -0
  51. package/src/DenoSocket.ts +162 -82
  52. package/src/DenoSocketServer.ts +1 -113
  53. package/dist/internal/denoSocketServer.d.ts +0 -7
  54. package/dist/internal/denoSocketServer.d.ts.map +0 -1
  55. package/dist/internal/denoSocketServer.js +0 -55
  56. package/dist/internal/denoSocketServer.js.map +0 -1
  57. package/src/internal/denoSocketServer.ts +0 -89
package/src/DenoSocket.ts CHANGED
@@ -9,14 +9,13 @@
9
9
  *
10
10
  * @since 4.0.0
11
11
  */
12
- import type { Array } from "effect"
12
+ import * as Arr from "effect/Array"
13
13
  import * as Channel from "effect/Channel"
14
14
  import * as Context from "effect/Context"
15
- import * as Deferred from "effect/Deferred"
16
15
  import type * as Duration from "effect/Duration"
17
16
  import * as Effect from "effect/Effect"
18
- import * as FiberSet from "effect/FiberSet"
19
17
  import * as Function from "effect/Function"
18
+ import { constVoid } from "effect/Function"
20
19
  import * as Latch from "effect/Latch"
21
20
  import * as Layer from "effect/Layer"
22
21
  import * as Scope from "effect/Scope"
@@ -56,6 +55,17 @@ export class Conn extends Context.Service<Conn, Deno.Conn>()(
56
55
  /**
57
56
  * Adapts a Deno connection into an Effect socket.
58
57
  *
58
+ * **Details**
59
+ *
60
+ * Readers over TCP connections can upgrade in place with `Deno.startTls`.
61
+ * Deno exposes only the client side of this transition; Unix connections fail
62
+ * the upgrade with a `SocketUpgradeError`.
63
+ *
64
+ * `Deno.startTls` does not support client certificates, so `key`, `cert`,
65
+ * `passphrase`, and `requestCert` do not affect Deno upgrades. Setting
66
+ * `rejectUnauthorized` to `false` disables hostname verification only;
67
+ * certificate-chain validation still applies.
68
+ *
59
69
  * @category constructors
60
70
  * @since 4.0.0
61
71
  */
@@ -66,89 +76,140 @@ export const fromConn = <RO>(
66
76
  let current: {
67
77
  readonly conn: Deno.Conn
68
78
  readonly writer: WritableStreamDefaultWriter<Uint8Array>
79
+ readonly fail: (error: Socket.SocketError) => void
69
80
  } | undefined
70
81
  let tearingDown = false
71
82
  let writeClosed = false
72
83
  const latch = Latch.makeUnsafe(false)
73
84
  const openServices = fiber.context as Context.Context<RO>
74
85
 
75
- const run = <R, E, _>(handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, options?: {
76
- readonly onOpen?: Effect.Effect<void> | undefined
77
- }) =>
78
- Effect.scopedWith(Effect.fnUntraced(function*(scope) {
79
- const fiberSet = yield* FiberSet.make<any, E | Socket.SocketError>().pipe(
80
- Scope.provide(scope)
81
- )
82
- let conn: Deno.Conn | undefined
83
- yield* Scope.addFinalizer(
84
- scope,
85
- Effect.suspend(() => {
86
- tearingDown = true
87
- return conn === undefined ? Effect.void : close(conn)
88
- })
89
- )
90
- conn = yield* Scope.provide(open, scope)
91
- const reader = conn.readable.getReader()
92
- const writer = conn.writable.getWriter()
93
- const runFork = yield* Effect.provideService(FiberSet.runtime(fiberSet)<R>(), Conn, conn)
86
+ const reader: Socket.Socket["reader"] = Effect.gen(function*() {
87
+ const scope = yield* Effect.scope
88
+ let conn: Deno.Conn | undefined
89
+ let upgradeAvailable = true
90
+ yield* Scope.addFinalizer(
91
+ scope,
92
+ Effect.suspend(() => {
93
+ tearingDown = true
94
+ latch.closeUnsafe()
95
+ current = undefined
96
+ upgradeAvailable = false
97
+ return conn === undefined ? Effect.void : close(conn)
98
+ })
99
+ )
100
+ conn = yield* Scope.provide(open, scope)
101
+ let readerHandle = conn.readable.getReader()
102
+ let writerHandle = conn.writable.getWriter()
94
103
 
95
- current = { conn, writer }
96
- tearingDown = false
97
- if (writeClosed) {
98
- writeClosed = false
99
- writer.releaseLock()
100
- yield* closeWrite(conn)
101
- }
102
- latch.openUnsafe()
104
+ let error: Socket.SocketError | undefined
105
+ const fail = (err: Socket.SocketError) => {
106
+ if (error === undefined) error = err
107
+ tearingDown = true
108
+ readerHandle.cancel().catch(constVoid)
109
+ }
110
+ current = {
111
+ conn,
112
+ writer: writerHandle,
113
+ fail
114
+ }
115
+ tearingDown = false
116
+ if (writeClosed) {
117
+ writeClosed = false
118
+ writerHandle.releaseLock()
119
+ yield* closeWrite(conn)
120
+ }
121
+ latch.openUnsafe()
103
122
 
104
- const read = Effect.tryPromise(
105
- () => reader.read()
106
- ).pipe(
107
- Effect.catchIf(
108
- (error) => tearingDown && isTeardownError(error.cause),
109
- () => Effect.succeed({ done: true, value: undefined } as ReadableStreamReadDoneResult<Uint8Array>)
110
- ),
111
- Effect.mapError((error) =>
112
- new Socket.SocketError({
113
- reason: new Socket.SocketReadError({ cause: error.cause })
123
+ const read = Effect.tryPromise({
124
+ try: () => readerHandle.read(),
125
+ catch: (cause) =>
126
+ tearingDown && isTeardownError(cause)
127
+ ? error ?? new Socket.SocketError({
128
+ reason: new Socket.SocketCloseError({ code: 1000 })
129
+ })
130
+ : new Socket.SocketError({
131
+ reason: new Socket.SocketReadError({ cause })
114
132
  })
133
+ })
134
+ const upgrade: Socket.Reader["upgrade"] = (options = {}) =>
135
+ Effect.suspend(() => {
136
+ if (!upgradeAvailable) {
137
+ return Effect.fail(
138
+ new Socket.SocketError({
139
+ reason: new Socket.SocketUpgradeError({
140
+ cause: new Error("socket is already upgraded or closed")
141
+ })
142
+ })
143
+ )
144
+ }
145
+ if (conn!.remoteAddr.transport !== "tcp") {
146
+ return Effect.fail(
147
+ new Socket.SocketError({
148
+ reason: new Socket.SocketUpgradeError({
149
+ cause: new Error("TLS upgrade requires a TCP connection")
150
+ })
151
+ })
152
+ )
153
+ }
154
+ upgradeAvailable = false
155
+ const raw = conn as Deno.TcpConn
156
+ readerHandle.releaseLock()
157
+ writerHandle.releaseLock()
158
+ return Effect.tryPromise({
159
+ try: async () => {
160
+ const tls = await Deno.startTls(raw, {
161
+ hostname: raw.remoteAddr.hostname,
162
+ caCerts: options.ca === undefined ? [] : toStrings(options.ca),
163
+ unsafelyDisableHostnameVerification: options.rejectUnauthorized === false,
164
+ ...(options.alpnProtocols === undefined ? {} : { alpnProtocols: [...options.alpnProtocols] })
165
+ })
166
+ await tls.handshake()
167
+ return tls
168
+ },
169
+ catch: (cause) =>
170
+ new Socket.SocketError({
171
+ reason: new Socket.SocketUpgradeError({ cause })
172
+ })
173
+ }).pipe(
174
+ Effect.tap((tls) =>
175
+ Effect.sync(() => {
176
+ conn = tls
177
+ readerHandle = tls.readable.getReader()
178
+ writerHandle = tls.writable.getWriter()
179
+ current = { conn: tls, writer: writerHandle, fail }
180
+ })
181
+ ),
182
+ Effect.tapError((upgradeError) => Effect.sync(() => fail(upgradeError))),
183
+ Effect.asVoid
115
184
  )
116
- )
117
- const readLoop: Effect.Effect<void, Socket.SocketError> = Effect.suspend(() =>
118
- Effect.flatMap(read, ({ done, value }) => {
119
- if (done) {
120
- Deferred.doneUnsafe(fiberSet.deferred, Effect.void)
121
- return Effect.void
122
- }
123
- const result = handler(value)
124
- if (Effect.isEffect(result)) {
125
- runFork(result)
126
- }
127
- return readLoop
128
- })
129
- )
130
- yield* FiberSet.run(fiberSet, readLoop)
131
-
132
- if (options?.onOpen) {
133
- yield* options.onOpen
134
- }
135
- return yield* FiberSet.join(fiberSet)
136
- })).pipe(
137
- Effect.updateContext((input: Context.Context<R>) => Context.merge(openServices, input)),
138
- Effect.onExit(() =>
139
- Effect.sync(() => {
140
- tearingDown = true
141
- latch.closeUnsafe()
142
- current = undefined
143
- })
144
- )
145
- )
185
+ })
186
+ return {
187
+ pull: Effect.suspend(() => {
188
+ if (error !== undefined) return Effect.fail(error)
189
+ return Effect.flatMap(read, ({ done, value }) =>
190
+ done
191
+ ? Effect.fail(
192
+ error ?? new Socket.SocketError({
193
+ reason: new Socket.SocketCloseError({ code: 1000 })
194
+ })
195
+ )
196
+ : Effect.succeed([value] as const))
197
+ }),
198
+ upgrade
199
+ }
200
+ }).pipe(
201
+ Effect.updateContext((input: Context.Context<Scope.Scope>) => Context.merge(openServices, input))
202
+ ) as Socket.Socket["reader"]
146
203
 
147
204
  const write = (chunk: Uint8Array | string | Socket.CloseEvent) =>
148
205
  latch.whenOpen(Effect.suspend(() => {
149
- const { conn, writer } = current!
206
+ const { conn, fail, writer } = current!
150
207
  if (Socket.isCloseEvent(chunk)) {
151
- tearingDown = true
208
+ fail(
209
+ new Socket.SocketError({
210
+ reason: new Socket.SocketCloseError({ code: chunk.code, closeReason: chunk.reason })
211
+ })
212
+ )
152
213
  return close(conn)
153
214
  }
154
215
  const bytes = typeof chunk === "string" ? encoder.encode(chunk) : chunk
@@ -161,10 +222,27 @@ export const fromConn = <RO>(
161
222
  })
162
223
  }))
163
224
 
164
- const writer = Effect.acquireRelease(
225
+ const writeAll = (chunks: Arr.NonEmptyReadonlyArray<Uint8Array | string>) =>
226
+ latch.whenOpen(Effect.tryPromise({
227
+ try: async () => {
228
+ const { writer } = current!
229
+ for (let i = 0; i < chunks.length; i++) {
230
+ const chunk = chunks[i]
231
+ const bytes = typeof chunk === "string" ? encoder.encode(chunk) : chunk
232
+ await writer.ready
233
+ await writer.write(bytes)
234
+ }
235
+ },
236
+ catch: (cause) =>
237
+ new Socket.SocketError({
238
+ reason: new Socket.SocketWriteError({ cause })
239
+ })
240
+ }))
241
+
242
+ const writer: Socket.Socket["writer"] = Effect.acquireRelease(
165
243
  Effect.sync(() => {
166
244
  writeClosed = false
167
- return write
245
+ return { write, writeAll }
168
246
  }),
169
247
  () =>
170
248
  Effect.suspend(() => {
@@ -178,11 +256,7 @@ export const fromConn = <RO>(
178
256
  })
179
257
  )
180
258
 
181
- return Effect.succeed(Socket.make({
182
- run,
183
- runRaw: run,
184
- writer
185
- }))
259
+ return Effect.succeed(Socket.make({ reader, writer }))
186
260
  })
187
261
 
188
262
  /**
@@ -261,10 +335,10 @@ export const makeTcp = (options: TcpOptions): Effect.Effect<Socket.Socket> => {
261
335
  export const makeTcpChannel = <IE = never>(
262
336
  options: ConnectOptions
263
337
  ): Channel.Channel<
264
- Array.NonEmptyReadonlyArray<Uint8Array>,
338
+ Arr.NonEmptyReadonlyArray<Uint8Array>,
265
339
  Socket.SocketError | IE,
266
340
  void,
267
- Array.NonEmptyReadonlyArray<Uint8Array | string | Socket.CloseEvent>,
341
+ Arr.NonEmptyReadonlyArray<Uint8Array | string | Socket.CloseEvent>,
268
342
  IE
269
343
  > => Channel.unwrap(Effect.map(makeTcp(options), Socket.toChannelWith<IE>()))
270
344
 
@@ -286,7 +360,9 @@ export const layerTcp: (options: ConnectOptions) => Layer.Layer<
286
360
  * @since 4.0.0
287
361
  */
288
362
  export const layerWebSocket = (url: string, options?: {
289
- readonly closeCodeIsError?: (code: number) => boolean
363
+ readonly openTimeout?: Duration.Input | undefined
364
+ readonly protocols?: string | globalThis.Array<string> | undefined
365
+ readonly highWaterMark?: number | undefined
290
366
  }): Layer.Layer<Socket.Socket> =>
291
367
  Layer.effect(Socket.Socket, Socket.makeWebSocket(url, options)).pipe(
292
368
  Layer.provide(layerWebSocketConstructor)
@@ -302,6 +378,10 @@ export const layerWebSocketConstructor: Layer.Layer<Socket.WebSocketConstructor>
302
378
  Socket.layerWebSocketConstructorGlobal
303
379
 
304
380
  const encoder = new TextEncoder()
381
+ const decoder = new TextDecoder()
382
+
383
+ const toStrings = (values: string | Uint8Array | ReadonlyArray<string | Uint8Array>): Array<string> =>
384
+ Arr.map(Arr.ensure(values), (value) => typeof value === "string" ? value : decoder.decode(value))
305
385
 
306
386
  const isBadResource = (cause: unknown): cause is Deno.errors.BadResource => cause instanceof Deno.errors.BadResource
307
387
 
@@ -1,120 +1,8 @@
1
1
  /**
2
- * Native Deno TCP and Unix socket servers for Effect's unstable socket server
3
- * API.
4
- *
5
- * The plain `make` and `layer` names intentionally differ from
6
- * `DenoSocket.makeTcp` and `DenoSocket.layerTcp` because this module has only
7
- * one server kind. Deno has no standalone WebSocket server, so WebSocket
8
- * constructors are omitted, and there is no `IncomingMessage` analogue.
9
- * Connections made before `run` starts remain in the kernel backlog instead of
10
- * being accepted and buffered as they are by the Node implementation. A second
11
- * concurrent `run` waits for the first because listener access is guarded by a
12
- * semaphore.
13
- *
14
- * Deno-native TLS server constructors are included even though the Node socket
15
- * server has no corresponding dedicated constructors. TLS is deliberately not
16
- * tested here because the repository has no certificate fixture.
17
- *
18
2
  * @since 4.0.0
19
3
  */
20
- import * as Effect from "effect/Effect"
21
- import * as Function from "effect/Function"
22
- import * as Layer from "effect/Layer"
23
- import type * as Scope from "effect/Scope"
24
- import * as SocketServer from "effect/unstable/socket/SocketServer"
25
- import { closeListener, fromListener } from "./internal/denoSocketServer.ts"
26
4
 
27
5
  /**
28
- * Native Deno options for listening on a TCP or Unix socket.
29
- *
30
- * @category models
31
6
  * @since 4.0.0
32
7
  */
33
- export type ListenOptions =
34
- | (Deno.TcpListenOptions & { transport?: "tcp" })
35
- | (Deno.UnixListenOptions & { transport: "unix" })
36
-
37
- /**
38
- * Native Deno options and certified key material for listening with TLS.
39
- *
40
- * @category models
41
- * @since 4.0.0
42
- */
43
- export type TlsListenOptions = Deno.ListenTlsOptions & Deno.TlsCertifiedKeyPem
44
-
45
- /**
46
- * Creates a scoped socket server using a native Deno TCP or Unix listener.
47
- *
48
- * @category constructors
49
- * @since 4.0.0
50
- */
51
- export const make: (
52
- options: ListenOptions
53
- ) => Effect.Effect<
54
- SocketServer.SocketServer["Service"],
55
- SocketServer.SocketServerError,
56
- Scope.Scope
57
- > = Effect.fnUntraced(function*(options) {
58
- const listener = yield* Effect.acquireRelease(
59
- Effect.try({
60
- try: () => options.transport === "unix" ? Deno.listen(options) : Deno.listen(options),
61
- catch: openError
62
- }),
63
- closeListener
64
- )
65
- return fromListener(listener)
66
- })
67
-
68
- /**
69
- * Provides a socket server using a scoped native Deno TCP or Unix listener.
70
- *
71
- * @category layers
72
- * @since 4.0.0
73
- */
74
- export const layer: (
75
- options: ListenOptions
76
- ) => Layer.Layer<SocketServer.SocketServer, SocketServer.SocketServerError> = Function.flow(
77
- make,
78
- Layer.effect(SocketServer.SocketServer)
79
- )
80
-
81
- /**
82
- * Creates a scoped TLS socket server using a native Deno TLS listener.
83
- *
84
- * @category constructors
85
- * @since 4.0.0
86
- */
87
- export const makeTls: (
88
- options: TlsListenOptions
89
- ) => Effect.Effect<
90
- SocketServer.SocketServer["Service"],
91
- SocketServer.SocketServerError,
92
- Scope.Scope
93
- > = Effect.fnUntraced(function*(options) {
94
- const listener = yield* Effect.acquireRelease(
95
- Effect.try({
96
- try: () => Deno.listenTls(options),
97
- catch: openError
98
- }),
99
- closeListener
100
- )
101
- return fromListener(listener)
102
- })
103
-
104
- /**
105
- * Provides a TLS socket server using a scoped native Deno TLS listener.
106
- *
107
- * @category layers
108
- * @since 4.0.0
109
- */
110
- export const layerTls: (
111
- options: TlsListenOptions
112
- ) => Layer.Layer<SocketServer.SocketServer, SocketServer.SocketServerError> = Function.flow(
113
- makeTls,
114
- Layer.effect(SocketServer.SocketServer)
115
- )
116
-
117
- const openError = (cause: unknown) =>
118
- new SocketServer.SocketServerError({
119
- reason: new SocketServer.SocketServerOpenError({ cause })
120
- })
8
+ export * from "@effect/platform-node-shared/NodeSocketServer"
@@ -1,7 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
- import * as SocketServer from "effect/unstable/socket/SocketServer";
3
- type Listener = Deno.Listener<Deno.Conn, Deno.NetAddr | Deno.UnixAddr>;
4
- export declare const fromListener: (listener: Listener) => SocketServer.SocketServer["Service"];
5
- export declare const closeListener: (listener: Listener) => Effect.Effect<void>;
6
- export {};
7
- //# sourceMappingURL=denoSocketServer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"denoSocketServer.d.ts","sourceRoot":"","sources":["../../src/internal/denoSocketServer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAQvC,OAAO,KAAK,YAAY,MAAM,qCAAqC,CAAA;AAMnE,KAAK,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAA;AAEtE,eAAO,MAAM,YAAY,aAAc,QAAQ,KAAG,YAAY,CAAC,YAAY,CAAC,SAAS,CAgDpF,CAAA;AAED,eAAO,MAAM,aAAa,aAAc,QAAQ,KAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAGlE,CAAA"}
@@ -1,55 +0,0 @@
1
- import * as Cause from "effect/Cause";
2
- import * as Context from "effect/Context";
3
- import * as Effect from "effect/Effect";
4
- import * as Exit from "effect/Exit";
5
- import * as Fiber from "effect/Fiber";
6
- import { pipe } from "effect/Function";
7
- import * as References from "effect/References";
8
- import * as Scope from "effect/Scope";
9
- import * as Semaphore from "effect/Semaphore";
10
- import * as SocketServer from "effect/unstable/socket/SocketServer";
11
- import * as DenoSocket from "../DenoSocket.js";
12
- const initialBackoffMillis = 10;
13
- const maximumBackoffMillis = 1_000;
14
- export const fromListener = (listener) => {
15
- const semaphore = Semaphore.makeUnsafe(1);
16
- const run = (handler) => semaphore.withPermit(Effect.gen(function* () {
17
- const scope = yield* Scope.make();
18
- const services = Context.omit(Scope.Scope)(yield* Effect.context());
19
- const trackFiber = Fiber.runIn(scope);
20
- let failures = 0;
21
- const loop = Effect.suspend(() => Effect.tryPromise(() => listener.accept()).pipe(Effect.matchEffect({
22
- onFailure: (error) => {
23
- const cause = error.cause;
24
- if (isTeardownError(cause))
25
- return Effect.never;
26
- const backoff = Math.min(initialBackoffMillis * 2 ** failures++, maximumBackoffMillis);
27
- return reportUnhandledError(Cause.fail(cause)).pipe(Effect.andThen(Effect.sleep(backoff)), Effect.andThen(loop));
28
- },
29
- onSuccess: (conn) => {
30
- failures = 0;
31
- pipe(DenoSocket.fromConn(Effect.acquireRelease(Effect.succeed(conn), closeConn)), Effect.flatMap(handler), Effect.ensuring(closeConn(conn)), Effect.catchCause(reportUnhandledError), Effect.runForkWith(Context.add(services, DenoSocket.Conn, conn)), trackFiber);
32
- return loop;
33
- }
34
- })));
35
- return yield* loop.pipe(Effect.ensuring(Scope.close(scope, Exit.void)));
36
- }));
37
- const address = listener.addr;
38
- return SocketServer.SocketServer.of({
39
- address: "path" in address
40
- ? { _tag: "UnixAddress", path: address.path }
41
- : { _tag: "TcpAddress", hostname: address.hostname, port: address.port },
42
- run
43
- });
44
- };
45
- export const closeListener = (listener) => Effect.try(() => listener.close()).pipe(Effect.catch(({ cause }) => isTeardownError(cause) ? Effect.void : Effect.die(cause)));
46
- const closeConn = (conn) => Effect.try(() => conn.close()).pipe(Effect.catch(({ cause }) => isTeardownError(cause) ? Effect.void : Effect.die(cause)));
47
- const isTeardownError = (cause) => cause instanceof Deno.errors.BadResource || cause instanceof Deno.errors.Interrupted;
48
- const reportUnhandledError = (cause) => Effect.withFiber((fiber) => {
49
- const unhandledLogLevel = fiber.getRef(References.UnhandledLogLevel);
50
- if (unhandledLogLevel) {
51
- return Effect.logWithLevel(unhandledLogLevel)(cause, "Unhandled error in SocketServer");
52
- }
53
- return Effect.void;
54
- });
55
- //# sourceMappingURL=denoSocketServer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"denoSocketServer.js","sourceRoot":"","sources":["../../src/internal/denoSocketServer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAA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oB,GAAG,CAAI,KAAqB,EAAE,EAAE,CACxD,MAAM,CAAC,SAAS,CAAO,CAAC,KAAK,EAAE,EAAE;IAC/B,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,iBAAiB,CAAC,CAAA;IACpE,IAAI,iBAAiB,EAAE,CAAC;QACtB,OAAO,MAAM,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC,KAAK,EAAE,iCAAiC,CAAC,CAAA;IACzF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAA;AACpB,CAAC,CAAC,CAAA"}
@@ -1,89 +0,0 @@
1
- import * as Cause from "effect/Cause"
2
- import * as Context from "effect/Context"
3
- import * as Effect from "effect/Effect"
4
- import * as Exit from "effect/Exit"
5
- import * as Fiber from "effect/Fiber"
6
- import { pipe } from "effect/Function"
7
- import * as References from "effect/References"
8
- import * as Scope from "effect/Scope"
9
- import * as Semaphore from "effect/Semaphore"
10
- import type * as Socket from "effect/unstable/socket/Socket"
11
- import * as SocketServer from "effect/unstable/socket/SocketServer"
12
- import * as DenoSocket from "../DenoSocket.ts"
13
-
14
- const initialBackoffMillis = 10
15
- const maximumBackoffMillis = 1_000
16
-
17
- type Listener = Deno.Listener<Deno.Conn, Deno.NetAddr | Deno.UnixAddr>
18
-
19
- export const fromListener = (listener: Listener): SocketServer.SocketServer["Service"] => {
20
- const semaphore = Semaphore.makeUnsafe(1)
21
-
22
- const run = <R, E, _>(handler: (socket: Socket.Socket) => Effect.Effect<_, E, R>) =>
23
- semaphore.withPermit(Effect.gen(function*() {
24
- const scope = yield* Scope.make()
25
- const services = Context.omit(Scope.Scope)(yield* Effect.context<R>()) as Context.Context<R>
26
- const trackFiber = Fiber.runIn(scope)
27
- let failures = 0
28
-
29
- const loop: Effect.Effect<never> = Effect.suspend(() =>
30
- Effect.tryPromise(() => listener.accept()).pipe(
31
- Effect.matchEffect({
32
- onFailure: (error) => {
33
- const cause = error.cause
34
- if (isTeardownError(cause)) return Effect.never
35
- const backoff = Math.min(initialBackoffMillis * 2 ** failures++, maximumBackoffMillis)
36
- return reportUnhandledError(Cause.fail(cause)).pipe(
37
- Effect.andThen(Effect.sleep(backoff)),
38
- Effect.andThen(loop)
39
- )
40
- },
41
- onSuccess: (conn) => {
42
- failures = 0
43
- pipe(
44
- DenoSocket.fromConn(Effect.acquireRelease(Effect.succeed(conn), closeConn)),
45
- Effect.flatMap(handler),
46
- Effect.ensuring(closeConn(conn)),
47
- Effect.catchCause(reportUnhandledError),
48
- Effect.runForkWith(Context.add(services, DenoSocket.Conn, conn)),
49
- trackFiber
50
- )
51
- return loop
52
- }
53
- })
54
- )
55
- )
56
-
57
- return yield* loop.pipe(Effect.ensuring(Scope.close(scope, Exit.void)))
58
- }))
59
-
60
- const address = listener.addr
61
- return SocketServer.SocketServer.of({
62
- address: "path" in address
63
- ? { _tag: "UnixAddress", path: address.path }
64
- : { _tag: "TcpAddress", hostname: address.hostname, port: address.port },
65
- run
66
- })
67
- }
68
-
69
- export const closeListener = (listener: Listener): Effect.Effect<void> =>
70
- Effect.try(() => listener.close()).pipe(
71
- Effect.catch(({ cause }) => isTeardownError(cause) ? Effect.void : Effect.die(cause))
72
- )
73
-
74
- const closeConn = (conn: Deno.Conn): Effect.Effect<void> =>
75
- Effect.try(() => conn.close()).pipe(
76
- Effect.catch(({ cause }) => isTeardownError(cause) ? Effect.void : Effect.die(cause))
77
- )
78
-
79
- const isTeardownError = (cause: unknown): boolean =>
80
- cause instanceof Deno.errors.BadResource || cause instanceof Deno.errors.Interrupted
81
-
82
- const reportUnhandledError = <E>(cause: Cause.Cause<E>) =>
83
- Effect.withFiber<void>((fiber) => {
84
- const unhandledLogLevel = fiber.getRef(References.UnhandledLogLevel)
85
- if (unhandledLogLevel) {
86
- return Effect.logWithLevel(unhandledLogLevel)(cause, "Unhandled error in SocketServer")
87
- }
88
- return Effect.void
89
- })