@effect/platform-bun 4.0.2 → 4.0.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 (44) hide show
  1. package/AGENTS.md +24 -0
  2. package/CLAUDE.md +24 -0
  3. package/ai-docs/package.json +2 -2
  4. package/ai-docs/src/52_graphql/10_basics.ts +111 -0
  5. package/ai-docs/src/52_graphql/20_testing.ts +45 -0
  6. package/ai-docs/src/52_graphql/30_batching.ts +116 -0
  7. package/ai-docs/src/52_graphql/40_aliased-batching.ts +122 -0
  8. package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
  9. package/ai-docs/src/52_graphql/index.md +6 -0
  10. package/dist/BunAddressResolver.d.ts +15 -0
  11. package/dist/BunAddressResolver.d.ts.map +1 -0
  12. package/dist/BunAddressResolver.js +15 -0
  13. package/dist/BunAddressResolver.js.map +1 -0
  14. package/dist/BunDatagramSocket.d.ts +227 -0
  15. package/dist/BunDatagramSocket.d.ts.map +1 -0
  16. package/dist/BunDatagramSocket.js +502 -0
  17. package/dist/BunDatagramSocket.js.map +1 -0
  18. package/dist/BunDns.d.ts +72 -0
  19. package/dist/BunDns.d.ts.map +1 -0
  20. package/dist/BunDns.js +66 -0
  21. package/dist/BunDns.js.map +1 -0
  22. package/dist/BunSocket.d.ts +4 -2
  23. package/dist/BunSocket.d.ts.map +1 -1
  24. package/dist/BunSocket.js +16 -6
  25. package/dist/BunSocket.js.map +1 -1
  26. package/dist/BunTerminal.d.ts +4 -3
  27. package/dist/BunTerminal.d.ts.map +1 -1
  28. package/dist/BunTerminal.js +4 -3
  29. package/dist/BunTerminal.js.map +1 -1
  30. package/dist/BunWorkerRunner.d.ts.map +1 -1
  31. package/dist/BunWorkerRunner.js +0 -1
  32. package/dist/BunWorkerRunner.js.map +1 -1
  33. package/dist/index.d.ts +15 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +15 -0
  36. package/dist/index.js.map +1 -1
  37. package/package.json +7 -5
  38. package/src/BunAddressResolver.ts +15 -0
  39. package/src/BunDatagramSocket.ts +666 -0
  40. package/src/BunDns.ts +82 -0
  41. package/src/BunSocket.ts +25 -6
  42. package/src/BunTerminal.ts +4 -3
  43. package/src/BunWorkerRunner.ts +0 -1
  44. package/src/index.ts +18 -0
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Bun UDP adapter for `effect/socket/DatagramSocket`, built on the native
3
+ * `Bun.udpSocket`. Requires Bun 1.4 or later: earlier versions throw `EAGAIN`
4
+ * from `send` instead of returning `false`, and never fire `drain`.
5
+ *
6
+ * `make` opens a new Bun socket for each reader acquisition, `fromUdpSocket`
7
+ * adopts a socket the caller creates, and `layer` provides the
8
+ * `DatagramSocket` service.
9
+ *
10
+ * The reader owns the native socket, so a send-only client must still acquire
11
+ * a reader, or its writes wait forever. Bun sends synchronously: a send error
12
+ * fails the write that caused it, and when the kernel buffer is full the write
13
+ * waits for `drain` and sends what is left.
14
+ *
15
+ * `writeAll` sends the batch with one `sendMany` call and fails with its first
16
+ * error. Which datagrams went out is unspecified, nothing is resent, and the
17
+ * error has no `address`, because Bun reports one error for the whole call.
18
+ *
19
+ * ICMP reports, such as a refused port, arrive after the write has completed,
20
+ * so they go to the `onError` option as a `DatagramSocketReadError` with no
21
+ * address.
22
+ *
23
+ * On macOS and Windows a poll error closes the socket without any event. The
24
+ * adapter notices on the next write, which fails with
25
+ * `DatagramSocketClosedError`, as do the reader and any write waiting for
26
+ * `drain`. Until then those waiting writes and `pull` keep waiting. The same
27
+ * applies when the caller closes a socket adopted with `fromUdpSocket`.
28
+ *
29
+ * **Example** (Echo server)
30
+ *
31
+ * ```ts
32
+ * import { BunDatagramSocket } from "@effect/platform-bun"
33
+ * import { Effect } from "effect"
34
+ *
35
+ * const echo = Effect.gen(function*() {
36
+ * const socket = yield* BunDatagramSocket.make({ bind: { port: 9000 } })
37
+ * const reader = yield* socket.reader
38
+ * const writer = yield* socket.writer
39
+ * while (true) {
40
+ * for (const received of yield* reader.pull) {
41
+ * yield* writer.write({ payload: received.payload, address: received })
42
+ * }
43
+ * }
44
+ * }).pipe(Effect.scoped)
45
+ * ```
46
+ *
47
+ * **Example** (Send-only client with a connected socket)
48
+ *
49
+ * ```ts
50
+ * import { BunDatagramSocket } from "@effect/platform-bun"
51
+ * import { Effect } from "effect"
52
+ *
53
+ * const send = (payload: string) =>
54
+ * Effect.gen(function*() {
55
+ * const socket = yield* BunDatagramSocket.make({
56
+ * connect: { address: "localhost", port: 9000 }
57
+ * })
58
+ * // Required even for send-only clients.
59
+ * yield* socket.reader
60
+ * const writer = yield* socket.writer
61
+ * yield* writer.write({ payload })
62
+ * }).pipe(Effect.scoped)
63
+ * ```
64
+ *
65
+ * **Example** (Adopting a socket)
66
+ *
67
+ * ```ts
68
+ * import { BunDatagramSocket } from "@effect/platform-bun"
69
+ * import { Effect } from "effect"
70
+ *
71
+ * const receive = Effect.gen(function*() {
72
+ * // `fromUdpSocket` replaces the socket's handlers with its own
73
+ * const socket = yield* BunDatagramSocket.fromUdpSocket(
74
+ * Effect.promise(() => Bun.udpSocket({ hostname: "127.0.0.1", port: 9000 }))
75
+ * )
76
+ * const reader = yield* socket.reader
77
+ * return yield* reader.pull
78
+ * }).pipe(Effect.scoped)
79
+ * ```
80
+ *
81
+ * @stability experimental
82
+ * @since 4.0.0
83
+ */
84
+ import * as Effect from "effect/Effect";
85
+ import * as Layer from "effect/Layer";
86
+ import * as NetAddress from "effect/net/NetAddress";
87
+ import type * as Scope from "effect/Scope";
88
+ import * as DatagramSocket from "effect/socket/DatagramSocket";
89
+ /**
90
+ * An open-time endpoint. Hostnames resolve once per reader acquisition via
91
+ * `node:dns.lookup`, avoiding Bun's synchronous `getaddrinfo`.
92
+ * An `InetAddress` can be passed directly.
93
+ *
94
+ * @stability experimental
95
+ * @category models
96
+ * @since 4.0.0
97
+ */
98
+ export interface Endpoint {
99
+ readonly address: string | NetAddress.IpAddress;
100
+ readonly port: number;
101
+ }
102
+ /**
103
+ * The local address and port to bind. Defaults to `0.0.0.0` (or `::` for
104
+ * `family: "ipv6"`) and an ephemeral port.
105
+ *
106
+ * @stability experimental
107
+ * @category models
108
+ * @since 4.0.0
109
+ */
110
+ export interface BindOptions {
111
+ readonly address?: string | NetAddress.IpAddress | undefined;
112
+ readonly port?: number | undefined;
113
+ }
114
+ /**
115
+ * Options for `fromUdpSocket`. The caller configures the adopted socket, so
116
+ * only the options that live in JavaScript apply.
117
+ *
118
+ * @stability experimental
119
+ * @category models
120
+ * @since 4.0.0
121
+ */
122
+ export interface AdoptOptions {
123
+ readonly peer?: Endpoint | undefined;
124
+ readonly receiveBuffer?: DatagramSocket.ReceiveBufferOptions | undefined;
125
+ readonly onError?: ((error: DatagramSocket.DatagramSocketError) => void) | undefined;
126
+ }
127
+ /**
128
+ * Options for `make` and `layer`.
129
+ *
130
+ * **Details**
131
+ *
132
+ * `peer` is a default destination and doesn't filter senders. `connect`
133
+ * connects the socket when Bun creates it, so the kernel filters senders. The
134
+ * two can't be combined.
135
+ *
136
+ * Family selection, in order: explicit `family`, a `bind` IP literal, the
137
+ * `peer` or `connect` address, a `bind` hostname, then `"ipv4"`. Hostname
138
+ * lookups use the family selected so far, or prefer IPv4 if none is set.
139
+ *
140
+ * `reuseAddress` means `SO_REUSEADDR` on Linux and `SO_REUSEPORT` on BSD and
141
+ * macOS.
142
+ *
143
+ * `multicast.interface` is the egress interface for datagrams sent to a group,
144
+ * which is a different socket option from the ingress `interface` of
145
+ * `Reader.joinMulticast`.
146
+ *
147
+ * @stability experimental
148
+ * @category models
149
+ * @since 4.0.0
150
+ */
151
+ export type Options = {
152
+ readonly bind?: BindOptions | undefined;
153
+ readonly family?: "ipv4" | "ipv6" | undefined;
154
+ readonly reuseAddress?: boolean | undefined;
155
+ readonly reusePort?: boolean | undefined;
156
+ readonly ipv6Only?: boolean | undefined;
157
+ readonly broadcast?: boolean | undefined;
158
+ readonly ttl?: number | undefined;
159
+ readonly multicast?: {
160
+ readonly interface?: NetAddress.MulticastInterface | undefined;
161
+ readonly loopback?: boolean | undefined;
162
+ readonly ttl?: number | undefined;
163
+ } | undefined;
164
+ readonly receiveBuffer?: DatagramSocket.ReceiveBufferOptions | undefined;
165
+ readonly onError?: ((error: DatagramSocket.DatagramSocketError) => void) | undefined;
166
+ } & ({
167
+ readonly peer?: Endpoint | undefined;
168
+ readonly connect?: undefined;
169
+ } | {
170
+ readonly connect: Endpoint;
171
+ readonly peer?: undefined;
172
+ });
173
+ /**
174
+ * A native Bun UDP socket, connected or not.
175
+ *
176
+ * @stability experimental
177
+ * @category models
178
+ * @since 4.0.0
179
+ */
180
+ export type UdpSocket = Bun.udp.Socket<"buffer"> | Bun.udp.ConnectedSocket<"buffer">;
181
+ /**
182
+ * Creates a `DatagramSocket` that opens and binds a new Bun UDP socket for
183
+ * each reader acquisition.
184
+ *
185
+ * **Details**
186
+ *
187
+ * Creating the socket never fails. Binding, applying options and resolving
188
+ * names happen on reader acquisition and fail it with a `DatagramSocketError`.
189
+ * Hostnames in `bind`, `peer` and `connect` are resolved once per acquisition;
190
+ * sends never wait for DNS.
191
+ *
192
+ * Invalid `receiveBuffer.capacity` values cause a defect; use a positive
193
+ * safe integer.
194
+ *
195
+ * @stability experimental
196
+ * @category constructors
197
+ * @since 4.0.0
198
+ */
199
+ export declare const make: (options?: Options) => Effect.Effect<DatagramSocket.DatagramSocket>;
200
+ /**
201
+ * Adopts a Bun UDP socket.
202
+ *
203
+ * **Details**
204
+ *
205
+ * `acquire` runs once per reader acquisition, inside the reader's scope, and
206
+ * the adapter closes the socket when that scope ends. Whether the socket is
207
+ * connected is checked once at open.
208
+ *
209
+ * The adapter installs its own `data`, `drain` and `error` handlers with
210
+ * `socket.reload`, which replaces the ones the caller passed to
211
+ * `Bun.udpSocket`. Packets that arrive before `acquire` completes still go to
212
+ * the caller's handlers.
213
+ *
214
+ * @stability experimental
215
+ * @category constructors
216
+ * @since 4.0.0
217
+ */
218
+ export declare const fromUdpSocket: <R>(acquire: Effect.Effect<UdpSocket, DatagramSocket.DatagramSocketError, R>, options?: AdoptOptions) => Effect.Effect<DatagramSocket.DatagramSocket, never, Exclude<R, Scope.Scope>>;
219
+ /**
220
+ * Provides a `DatagramSocket` built with `make`.
221
+ *
222
+ * @stability experimental
223
+ * @category layers
224
+ * @since 4.0.0
225
+ */
226
+ export declare const layer: (options?: Options) => Layer.Layer<DatagramSocket.DatagramSocket>;
227
+ //# sourceMappingURL=BunDatagramSocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BunDatagramSocket.d.ts","sourceRoot":"","sources":["../src/BunDatagramSocket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkFG;AACH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAA;AAEnD,OAAO,KAAK,KAAK,KAAK,MAAM,cAAc,CAAA;AAC1C,OAAO,KAAK,cAAc,MAAM,8BAA8B,CAAA;AAK9D;;;;;;;;GAQG;AACH,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC,SAAS,CAAA;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CACtB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC,SAAS,GAAG,SAAS,CAAA;IAC5D,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACnC;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,aAAa,CAAC,EAAE,cAA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