@effect/platform-deno 4.0.6 → 4.1.0

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.
@@ -327,6 +327,8 @@ export const layerConfig = (
327
327
  layerHttpServices
328
328
  )
329
329
 
330
+ const formDecoder = new TextDecoder("utf-8", { ignoreBOM: true })
331
+
330
332
  class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpServerRequest {
331
333
  readonly [ServerRequest.TypeId]: typeof ServerRequest.TypeId
332
334
  readonly [IncomingMessage.TypeId]: typeof IncomingMessage.TypeId
@@ -441,15 +443,7 @@ class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpS
441
443
 
442
444
  private textEffect: Effect.Effect<string, Error.HttpServerError> | undefined
443
445
  get text(): Effect.Effect<string, Error.HttpServerError> {
444
- return this.textEffect ??= Effect.runSync(Effect.cached(
445
- Effect.tryPromise({
446
- try: () => this.source.text(),
447
- catch: (cause) =>
448
- new Error.HttpServerError({
449
- reason: new Error.RequestParseError({ request: this, cause })
450
- })
451
- })
452
- ))
446
+ return this.textEffect ??= Effect.map(this.arrayBuffer, (buffer) => new TextDecoder().decode(buffer))
453
447
  }
454
448
  get json(): Effect.Effect<Schema.Json, Error.HttpServerError> {
455
449
  return Effect.flatMap(this.text, (_) =>
@@ -462,14 +456,15 @@ class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpS
462
456
  }))
463
457
  }
464
458
  get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, Error.HttpServerError> {
465
- return Effect.flatMap(this.text, (_) =>
459
+ return Effect.flatMap(this.arrayBuffer, (bytes) =>
466
460
  Effect.try({
467
- try: () => UrlParams.fromInput(new URLSearchParams(_)),
461
+ // A leading "?" is form data; URLSearchParams would drop it as a query prefix.
462
+ try: () => UrlParams.fromInput(new URLSearchParams(formDecoder.decode(bytes).replace(/^\?/, "%3F"))),
468
463
  catch: (cause) =>
469
464
  new Error.HttpServerError({
470
465
  reason: new Error.RequestParseError({ request: this, cause })
471
466
  })
472
- }))
467
+ }));
473
468
  }
474
469
 
475
470
  private multipartEffect:
@@ -498,10 +493,23 @@ class DenoServerRequest extends Inspectable.Class implements ServerRequest.HttpS
498
493
  })
499
494
  })
500
495
  ))
501
- this.textEffect = Effect.map(this.arrayBufferEffect, (_) => new TextDecoder().decode(_))
502
496
  return this.arrayBufferEffect
503
497
  }
504
498
 
499
+ upgradeWith(
500
+ options: { readonly headers?: Headers.Input | undefined }
501
+ ): Effect.Effect<Socket.Socket, Error.HttpServerError> {
502
+ if (Object.keys(Headers.fromInput(options.headers)).length === 0) return this.upgrade
503
+ return Effect.fail(
504
+ new Error.HttpServerError({
505
+ reason: new Error.InternalError({
506
+ request: this,
507
+ description: "WebSocket upgrades do not support custom response headers"
508
+ })
509
+ })
510
+ )
511
+ }
512
+
505
513
  get upgrade(): Effect.Effect<Socket.Socket, Error.HttpServerError> {
506
514
  return Effect.flatMap(
507
515
  Effect.try({
package/src/DenoRedis.ts CHANGED
@@ -5,7 +5,8 @@
5
5
  * both the portable `Redis` service and the Deno-specific {@link DenoRedis}
6
6
  * service for direct access to the raw client. Unlike Bun's built-in client,
7
7
  * `@db/redis` connects eagerly using RESP2, so layer construction can fail
8
- * with a `RedisError`.
8
+ * with a `RedisError`. The layers also accept `Redis.PoolOptions` for the
9
+ * connections behind `Redis.reserve`.
9
10
  *
10
11
  * @stability unstable
11
12
  * @since 4.0.0
@@ -46,8 +47,10 @@ export class DenoRedis extends Context.Service<DenoRedis, {
46
47
  readonly use: <A>(f: (client: RedisClient) => Promise<A>) => Effect.Effect<A, Redis.RedisError>
47
48
  }>()("@effect/platform-deno/DenoRedis") {}
48
49
 
49
- const make = Effect.fnUntraced(function*(options: RedisOptions = {}) {
50
- const connectClient = () => {
50
+ const make = Effect.fnUntraced(function*(
51
+ { acquireTimeout, idleTimeout, maxConnections, minConnections, ...options }: RedisOptions & Redis.PoolOptions = {}
52
+ ) {
53
+ const connectClient = (overrides?: Partial<RedisConnectOptions>) => {
51
54
  const { url, ...connectOptions } = options
52
55
  const parsedUrl = url === undefined ? undefined : new URL(url)
53
56
  const { name, ...parsed } = url === undefined ? { hostname: "localhost" } : parseURL(url)
@@ -56,13 +59,14 @@ const make = Effect.fnUntraced(function*(options: RedisOptions = {}) {
56
59
  ...(name === undefined ? {} : { username: name }),
57
60
  ...(parsedUrl?.username ? { username: decodeURIComponent(parsedUrl.username) } : {}),
58
61
  ...(parsedUrl?.password ? { password: decodeURIComponent(parsedUrl.password) } : {}),
59
- ...Record.filter(connectOptions, Predicate.isNotUndefined)
62
+ ...Record.filter(connectOptions, Predicate.isNotUndefined),
63
+ ...overrides
60
64
  })
61
65
  }
62
66
 
63
67
  const client = yield* Effect.acquireRelease(
64
68
  Effect.tryPromise({
65
- try: connectClient,
69
+ try: () => connectClient(),
66
70
  catch: (cause) => new Redis.RedisError({ cause })
67
71
  }),
68
72
  (client) => Effect.sync(() => client.close())
@@ -74,12 +78,28 @@ const make = Effect.fnUntraced(function*(options: RedisOptions = {}) {
74
78
  catch: (cause) => new Redis.RedisError({ cause })
75
79
  })
76
80
 
77
- const redis = yield* Redis.make({
78
- send: <A = unknown>(command: string, ...args: ReadonlyArray<string>) =>
81
+ const sendWith =
82
+ (client: RedisClient) =>
83
+ <A = unknown>(command: string, ...args: ReadonlyArray<string>): Effect.Effect<A, Redis.RedisError> =>
79
84
  Effect.tryPromise({
80
85
  try: () => client.sendCommand(command, args as Array<string>) as Promise<A>,
81
86
  catch: (cause) => new Redis.RedisError({ cause })
82
- }),
87
+ })
88
+
89
+ const redis = yield* Redis.make({
90
+ send: sendWith(client),
91
+ connect: Effect.tryPromise({
92
+ // @db/redis re-executes commands after a reconnect, which could repeat
93
+ // a destructive blocking command whose reply was lost.
94
+ try: () => connectClient({ maxRetryCount: 0 }),
95
+ catch: (cause) => new Redis.RedisError({ cause })
96
+ }).pipe(
97
+ Effect.map((connection) => ({
98
+ send: sendWith(connection),
99
+ close: Effect.sync(() => connection.close())
100
+ }))
101
+ ),
102
+ pool: { acquireTimeout, idleTimeout, maxConnections, minConnections },
83
103
  subscribe: (channel, onMessage) =>
84
104
  Effect.acquireRelease(
85
105
  Effect.tryPromise({
@@ -134,7 +154,7 @@ const make = Effect.fnUntraced(function*(options: RedisOptions = {}) {
134
154
  * @since 4.0.0
135
155
  */
136
156
  export const layer = (
137
- options?: RedisOptions | undefined
157
+ options?: (RedisOptions & Redis.PoolOptions) | undefined
138
158
  ): Layer.Layer<Redis.Redis | DenoRedis, Redis.RedisError> => Layer.effectContext(make(options))
139
159
 
140
160
  /**
@@ -146,7 +166,7 @@ export const layer = (
146
166
  * @since 4.0.0
147
167
  */
148
168
  export const layerConfig = (
149
- options: Config.Wrap<RedisOptions>
169
+ options: Config.Wrap<RedisOptions & Redis.PoolOptions>
150
170
  ): Layer.Layer<Redis.Redis | DenoRedis, Redis.RedisError | Config.ConfigError> =>
151
171
  Layer.effectContext(
152
172
  Config.unwrap(options).pipe(
@@ -2,14 +2,13 @@
2
2
  * Aggregate Deno platform services layer.
3
3
  *
4
4
  * This module defines the `DenoServices` union and a single `layer` that
5
- * provides Deno-backed child process spawning, crypto, filesystem, path, stdio,
5
+ * provides Deno-backed child process spawning, filesystem, path, stdio,
6
6
  * and terminal services. Use the layer when a Deno program wants the standard
7
7
  * platform services from one place.
8
8
  *
9
9
  * @stability unstable
10
10
  * @since 4.0.0
11
11
  */
12
- import type { Crypto } from "effect/Crypto"
13
12
  import type { FileSystem } from "effect/FileSystem"
14
13
  import * as Layer from "effect/Layer"
15
14
  import type { Path } from "effect/Path"
@@ -17,7 +16,6 @@ import type { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"
17
16
  import type { Stdio } from "effect/Stdio"
18
17
  import type { Terminal } from "effect/Terminal"
19
18
  import * as DenoChildProcessSpawner from "./DenoChildProcessSpawner.ts"
20
- import * as DenoCrypto from "./DenoCrypto.ts"
21
19
  import * as DenoFileSystem from "./DenoFileSystem.ts"
22
20
  import * as DenoPath from "./DenoPath.ts"
23
21
  import * as DenoStdio from "./DenoStdio.ts"
@@ -25,13 +23,13 @@ import * as DenoTerminal from "./DenoTerminal.ts"
25
23
 
26
24
  /**
27
25
  * The union of core services provided by the Deno platform layer, including
28
- * child process spawning, crypto, filesystem, path, stdio, and terminal services.
26
+ * child process spawning, filesystem, path, stdio, and terminal services.
29
27
  *
30
28
  * @stability unstable
31
29
  * @category models
32
30
  * @since 4.0.0
33
31
  */
34
- export type DenoServices = ChildProcessSpawner | Crypto | FileSystem | Path | Terminal | Stdio
32
+ export type DenoServices = ChildProcessSpawner | FileSystem | Path | Terminal | Stdio
35
33
 
36
34
  /**
37
35
  * Provides the default Deno implementations for child process spawning,
@@ -44,7 +42,6 @@ export type DenoServices = ChildProcessSpawner | Crypto | FileSystem | Path | Te
44
42
  export const layer: Layer.Layer<DenoServices> = DenoChildProcessSpawner.layer.pipe(
45
43
  Layer.provideMerge(Layer.mergeAll(
46
44
  DenoFileSystem.layer,
47
- DenoCrypto.layer,
48
45
  DenoPath.layer,
49
46
  DenoStdio.layer,
50
47
  DenoTerminal.layer