@effect/platform-bun 4.0.2 → 4.0.3

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/src/BunDns.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Bun implementation of Effect's `Dns` service.
3
+ *
4
+ * Address lookups use `Bun.dns.lookup` with the `system` backend, which calls
5
+ * the operating system resolver (`getaddrinfo`) and therefore also reads the
6
+ * hosts file. Bun's `node:dns` lookup uses c-ares instead, which bypasses the
7
+ * system's name service configuration. Record queries and reverse lookups use
8
+ * `NodeDns.resolver` without the UTF-8 correction of `NodeDns.make`,
9
+ * because Bun already decodes TXT and CAA character strings as UTF-8.
10
+ *
11
+ * **Gotchas**
12
+ *
13
+ * Bun returns each character string of a TXT record as a separate record, so
14
+ * a record made of several strings arrives as several TXT records whose chunks
15
+ * cannot be reassembled (https://github.com/oven-sh/bun/issues/44692).
16
+ *
17
+ * Bun's resolver cannot query TLSA records, so TLSA queries fail with
18
+ * `Unsupported`.
19
+ *
20
+ * @stability experimental
21
+ * @since 4.0.0
22
+ */
23
+ import * as NodeDns from "@effect/platform-node-shared/NodeDns"
24
+ import * as Config from "effect/Config"
25
+ import * as Effect from "effect/Effect"
26
+ import * as Layer from "effect/Layer"
27
+ import * as Dns from "effect/net/Dns"
28
+ import type * as NetAddress from "effect/net/NetAddress"
29
+
30
+ export type {
31
+ /**
32
+ * Options for the Bun `Dns` service: name servers, timeout, and tries for
33
+ * record queries and reverse lookups.
34
+ *
35
+ * @stability experimental
36
+ * @category re-exports
37
+ * @since 4.0.0
38
+ */
39
+ Options
40
+ } from "@effect/platform-node-shared/NodeDns"
41
+
42
+ /**
43
+ * Creates a Bun `Dns` service whose resolver lives as long as the scope.
44
+ *
45
+ * @stability experimental
46
+ * @category constructors
47
+ * @since 4.0.0
48
+ */
49
+ export const make = Effect.fnUntraced(function*(options?: NodeDns.Options) {
50
+ const resolve = yield* NodeDns.resolver(options)
51
+ return Dns.make({
52
+ lookup: (host, family) =>
53
+ Effect.tryPromise({
54
+ try: () => Bun.dns.lookup(host, { family: NodeDns.toFamily(family), backend: "system" }),
55
+ catch: (cause) => NodeDns.dnsErrorFromCause(cause, "lookup", host)
56
+ }).pipe(Effect.map(NodeDns.addressesFromLookup)),
57
+ resolve
58
+ })
59
+ })
60
+
61
+ /**
62
+ * Layer that provides the Bun `Dns` service using the system resolver
63
+ * configuration.
64
+ *
65
+ * @stability experimental
66
+ * @category layers
67
+ * @since 4.0.0
68
+ */
69
+ export const layer: Layer.Layer<Dns.Dns> = Layer.effect(Dns.Dns, Effect.orDie(make()))
70
+
71
+ /**
72
+ * Creates a layer that provides the Bun `Dns` service with options read
73
+ * from configuration.
74
+ *
75
+ * @stability experimental
76
+ * @category layers
77
+ * @since 4.0.0
78
+ */
79
+ export const layerConfig = (
80
+ options: Config.Wrap<NodeDns.Options>
81
+ ): Layer.Layer<Dns.Dns, Config.ConfigError | NetAddress.NetAddressError> =>
82
+ Layer.effect(Dns.Dns, Effect.flatMap(Config.unwrap(options), make))
package/src/BunSocket.ts CHANGED
@@ -22,7 +22,8 @@ export * from "@effect/platform-node-shared/NodeSocket"
22
22
 
23
23
  /**
24
24
  * Provides a `Socket.WebSocketConstructor` backed by Bun's global
25
- * `WebSocket` implementation.
25
+ * `WebSocket` implementation, supporting handshake headers and subprotocols
26
+ * together in client options objects.
26
27
  *
27
28
  * @stability unstable
28
29
  * @category layers
@@ -31,15 +32,32 @@ export * from "@effect/platform-node-shared/NodeSocket"
31
32
  export const layerWebSocketConstructor: Layer.Layer<
32
33
  Socket.WebSocketConstructor
33
34
  > = Layer.succeed(Socket.WebSocketConstructor)(
34
- (url, options) =>
35
- // Bun accepts `WebSocketOptions`, but `bun-types` selects the DOM overload
36
- // when `lib.dom` is loaded and hides those constructor options.
37
- new globalThis.WebSocket(url, options as string | Array<string> | undefined)
35
+ (url, options) => {
36
+ if (options === undefined || typeof options === "string" || Array.isArray(options)) {
37
+ return new globalThis.WebSocket(url, options)
38
+ }
39
+ // Bun accepts `WebSocketOptions`, but `bun-types` selects the DOM
40
+ // overload when `lib.dom` is loaded and hides those constructor options.
41
+ const WebSocket = globalThis.WebSocket as unknown as {
42
+ new(
43
+ url: string,
44
+ options: {
45
+ protocols?: Array<string> | undefined
46
+ headers?: Readonly<Record<string, string>> | undefined
47
+ }
48
+ ): globalThis.WebSocket
49
+ }
50
+ return new WebSocket(url, {
51
+ // Bun requires a sequence for `protocols` in its options object.
52
+ protocols: typeof options.protocols === "string" ? [options.protocols] : options.protocols,
53
+ headers: options.headers
54
+ })
55
+ }
38
56
  )
39
57
 
40
58
  /**
41
59
  * Creates a `Socket.Socket` layer for a WebSocket URL using Bun's global
42
- * `WebSocket` constructor, honoring protocol, open-timeout, and
60
+ * `WebSocket` constructor, honoring protocol, handshake-header, open-timeout, and
43
61
  * high-water-mark options.
44
62
  *
45
63
  * @stability unstable
@@ -51,6 +69,7 @@ export const layerWebSocket: (
51
69
  options?: {
52
70
  readonly openTimeout?: Duration.Input | undefined
53
71
  readonly protocols?: string | Array<string> | undefined
72
+ readonly headers?: Readonly<Record<string, string>> | undefined
54
73
  readonly highWaterMark?: number | undefined
55
74
  } | undefined
56
75
  ) => Layer.Layer<Socket.Socket, never, never> = flow(
@@ -15,8 +15,9 @@ import type { Scope } from "effect/Scope"
15
15
  import type { Terminal, UserInput } from "effect/Terminal"
16
16
 
17
17
  /**
18
- * Creates a scoped `Terminal` service backed by process stdin/stdout, using the
19
- * optional predicate to decide when key input should end the input stream.
18
+ * Creates a scoped `Terminal` service backed by process stdin/stdout.
19
+ * By default, Esc, Ctrl+C, and Ctrl+D end key input. Supply `shouldQuit` to
20
+ * override these quit keys.
20
21
  *
21
22
  * @stability unstable
22
23
  * @category constructors
@@ -26,7 +27,7 @@ export const make: (shouldQuit?: (input: UserInput) => boolean) => Effect<Termin
26
27
 
27
28
  /**
28
29
  * Provides the default process-backed `Terminal` service, ending key input on
29
- * the default quit keys.
30
+ * bare Esc, Ctrl+C, or Ctrl+D.
30
31
  *
31
32
  * @stability unstable
32
33
  * @category layers
@@ -64,7 +64,6 @@ export const layer: Layer.Layer<WorkerRunner.WorkerRunnerPlatform> = Layer.succe
64
64
  trackFiber(fiber)
65
65
  }
66
66
  } else {
67
- port.close()
68
67
  Deferred.doneUnsafe(closeLatch, Exit.void)
69
68
  }
70
69
  }
package/src/index.ts CHANGED
@@ -5,6 +5,12 @@
5
5
 
6
6
  // @barrel: Auto-generated exports. Do not edit manually.
7
7
 
8
+ /**
9
+ * @stability experimental
10
+ * @since 4.0.0
11
+ */
12
+ export * as BunAddressResolver from "./BunAddressResolver.ts"
13
+
8
14
  /**
9
15
  * @stability unstable
10
16
  * @since 4.0.0
@@ -29,6 +35,18 @@ export * as BunClusterSocket from "./BunClusterSocket.ts"
29
35
  */
30
36
  export * as BunCrypto from "./BunCrypto.ts"
31
37
 
38
+ /**
39
+ * @stability experimental
40
+ * @since 4.0.0
41
+ */
42
+ export * as BunDatagramSocket from "./BunDatagramSocket.ts"
43
+
44
+ /**
45
+ * @stability experimental
46
+ * @since 4.0.0
47
+ */
48
+ export * as BunDns from "./BunDns.ts"
49
+
32
50
  /**
33
51
  * @stability unstable
34
52
  * @since 4.0.0