@gjermundgaraba/effect-actions 0.38.0 → 0.39.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.
- package/README.md +2 -2
- package/dist/ActionEntity.d.mts +6 -6
- package/dist/ActionEntity.mjs +6 -6
- package/dist/ActionHttp.d.mts +1 -1
- package/dist/ActionRpc.d.mts +6 -7
- package/dist/Event.d.mts +40 -9
- package/dist/Event.mjs +20 -1
- package/dist/Stored.d.mts +8 -8
- package/dist/Stored.mjs +8 -8
- package/dist/contract/watch.mjs +15 -11
- package/dist/entity/forwarded.d.mts +2 -2
- package/dist/entity/forwarded.mjs +2 -2
- package/dist/rpc/erased.mjs +3 -2
- package/docs/ActionEntity.md +2 -2
- package/docs/ActionHttp.md +1 -1
- package/docs/ActionMcp.md +1 -1
- package/docs/ActionRpc.md +1 -1
- package/docs/Event.md +2 -2
- package/docs/README.md +0 -1
- package/examples/shopping-carts/README.md +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# effect-actions
|
|
2
2
|
|
|
3
|
-
Define an Effect action once. Serve it over HTTP, MCP and Effect RPC, hand it to a model as a native Toolkit, and run it from a CLI. Same schemas, same handler, Effect's own servers and clients underneath. Events go the same way: watched over HTTP, MCP, Effect RPC and the CLI, or delivered by webhook.
|
|
3
|
+
Define an Effect action once. Serve it over HTTP, MCP and Effect RPC, hand it to a model as a native Toolkit, and run it from a CLI. Same schemas, same handler, Effect's own servers and clients underneath. Events go the same way: watched over HTTP, MCP, Effect RPC and the CLI, or delivered by webhook.
|
|
4
4
|
|
|
5
5
|
**If you are a coding agent**, start at [docs/README.md](docs/README.md), not here. It routes every task, protecting an action and limiting request bodies included, to its page and its example.
|
|
6
6
|
|
|
@@ -136,6 +136,6 @@ Set up with [Vite+](https://viteplus.dev/guide/): `vp install`, then `vp run rea
|
|
|
136
136
|
|
|
137
137
|
## Releasing
|
|
138
138
|
|
|
139
|
-
Bump the version in `package.json
|
|
139
|
+
Bump the version in `package.json`, commit, tag `v<version>` and push the tag. `.github/workflows/npm.yml` then publishes that version, unless npm has it already, through npm trusted publishing.
|
|
140
140
|
|
|
141
141
|
`.github/workflows/effect-latest.yml` runs the full check weekly against the newest Effect 4.x, so a minor release that passes can be admitted to the `effect` peer range.
|
package/dist/ActionEntity.d.mts
CHANGED
|
@@ -130,9 +130,9 @@ export declare function make<const Type extends string, const Contracts extends
|
|
|
130
130
|
/**
|
|
131
131
|
* The rpcs of `entity`'s binding, as its `protocol` holds them without its native rpcs: one per
|
|
132
132
|
* action, the entity's internal actions included, and per event type its poll. A surface outside
|
|
133
|
-
* the cluster forwards to the entity those of them its own binding lists alone, as
|
|
134
|
-
*
|
|
135
|
-
*
|
|
133
|
+
* the cluster forwards to the entity those of them its own binding lists alone, as a socket host
|
|
134
|
+
* forwards those its socket serves, which `ActionRpc.client`'s watch polls, finding no stream
|
|
135
|
+
* rpc.
|
|
136
136
|
*/
|
|
137
137
|
export declare const bindingRpcs: (entity: Any) => RpcGroup.RpcGroup<ErasedRpc>;
|
|
138
138
|
/**
|
|
@@ -229,9 +229,9 @@ type EntityLayer<B extends Any$4, Apps extends Served, N extends Rpc.Any, ROut,
|
|
|
229
229
|
* Whether a failure is one of those Effect's entity client fails with when the entity could not
|
|
230
230
|
* be reached, `MailboxFull`, `PersistenceError`, `AlreadyProcessingMessage` and
|
|
231
231
|
* `EntityNotAssignedToRunner`, which `client`, `nativeClient` and `reaching` answer as
|
|
232
|
-
* `Action.Unavailable`: a server forwarding calls into an entity, as
|
|
233
|
-
*
|
|
234
|
-
*
|
|
232
|
+
* `Action.Unavailable`: a server forwarding calls into an entity, such as a socket host, or a
|
|
233
|
+
* Worker calling Effect's `Entity.client` itself, tells them apart with it, to answer them with a
|
|
234
|
+
* 503 or end the call with a defect.
|
|
235
235
|
*/
|
|
236
236
|
export declare const isClusterFailure: <I>(input: I) => input is I & (ClusterError.AlreadyProcessingMessage | ClusterError.EntityNotAssignedToRunner | ClusterError.MailboxFull | ClusterError.PersistenceError);
|
|
237
237
|
/**
|
package/dist/ActionEntity.mjs
CHANGED
|
@@ -48,9 +48,9 @@ function make(type, contracts, options) {
|
|
|
48
48
|
/**
|
|
49
49
|
* The rpcs of `entity`'s binding, as its `protocol` holds them without its native rpcs: one per
|
|
50
50
|
* action, the entity's internal actions included, and per event type its poll. A surface outside
|
|
51
|
-
* the cluster forwards to the entity those of them its own binding lists alone, as
|
|
52
|
-
*
|
|
53
|
-
*
|
|
51
|
+
* the cluster forwards to the entity those of them its own binding lists alone, as a socket host
|
|
52
|
+
* forwards those its socket serves, which `ActionRpc.client`'s watch polls, finding no stream
|
|
53
|
+
* rpc.
|
|
54
54
|
*/
|
|
55
55
|
const bindingRpcs = (entity) => erasedOf(entity.binding.actions, entity.binding.events, "ActionEntity.layer");
|
|
56
56
|
/**
|
|
@@ -86,9 +86,9 @@ const ClusterFailures = Schema.Union([
|
|
|
86
86
|
* Whether a failure is one of those Effect's entity client fails with when the entity could not
|
|
87
87
|
* be reached, `MailboxFull`, `PersistenceError`, `AlreadyProcessingMessage` and
|
|
88
88
|
* `EntityNotAssignedToRunner`, which `client`, `nativeClient` and `reaching` answer as
|
|
89
|
-
* `Action.Unavailable`: a server forwarding calls into an entity, as
|
|
90
|
-
*
|
|
91
|
-
*
|
|
89
|
+
* `Action.Unavailable`: a server forwarding calls into an entity, such as a socket host, or a
|
|
90
|
+
* Worker calling Effect's `Entity.client` itself, tells them apart with it, to answer them with a
|
|
91
|
+
* 503 or end the call with a defect.
|
|
92
92
|
*/
|
|
93
93
|
const isClusterFailure = Schema.is(ClusterFailures);
|
|
94
94
|
function layer(entity, served, { services = Layer.empty, rpcs = {}, onWake, ...options } = {}) {
|
package/dist/ActionHttp.d.mts
CHANGED
|
@@ -176,7 +176,7 @@ export declare function make<const Contracts extends ReadonlyArray<Any$1 | Any$2
|
|
|
176
176
|
* - An `ErrorReporter` the server runs with is given, as `Info`, a handler's declared failure and an input that does not decode, never a refusal or a refused credential: give an error class `override readonly [ErrorReporter.ignore] = true` to keep it out, and drop `Info` for a built-in error, which takes no mark.
|
|
177
177
|
* - An event's route takes `{ input, cursor }`, a `cursor` omitted or `null` reading from now, and answers a page, `{ events, cursor, truncated, hasMore, nextPollMs }`, or, to a request accepting `text/event-stream`, an SSE stream of `Checked`, `Occurrence`, `Gap` and `Failed` data, a `Checked` every 30 seconds; the stream carries no `id:`, so `Last-Event-ID` resumes nothing: resume from a cursor in the body.
|
|
178
178
|
* - A subscription is refused as an action's call is, before its stream starts, with the route's 401 or 403; a refusal later ends the stream with Effect's `effect/http-api/stream/failure` event.
|
|
179
|
-
* - A source's failure no refusal explains is logged and reported once, sent as a bare `Failed`, and read again from where it reached after 5 seconds, before the stream's first `Checked` too; a poll's is a 500, which `client`'s `watch` reconnects after.
|
|
179
|
+
* - A source's failure no refusal explains is logged and reported once, sent as a bare `Failed`, and read again from where it reached after 5 seconds, before the stream's first `Checked` too; a poll's is a 500, or a 503 with `Retry-After` for `Action.Unavailable`, which `client`'s `watch` reconnects after.
|
|
180
180
|
* - A stream is read only as fast as its client reads it: a slow reader of a log source falls behind its log, and receives a `Gap`.
|
|
181
181
|
* - On a host with no lasting process, such as `HttpRouter.toWebHandler` on a serverless platform, serve check sources alone, without a `subscription` hook or `Webhook.layer`: nothing refuses the others, and under `Event.layerMemory` each instance keeps its own log of a log source, whose cursors another reads as a `Gap`, and its own leases and webhook subscriptions, lost as it ends.
|
|
182
182
|
*/
|
package/dist/ActionRpc.d.mts
CHANGED
|
@@ -203,7 +203,7 @@ export interface LayerOptions<M extends Middleware = [], C extends Any$2 | Any$3
|
|
|
203
203
|
* - Over a protocol with no router, such as `RpcServer.layerProtocolSocketServer` or `layerProtocolStdio`, there is no route: a provider publishing a `protectedResource` leaves `HttpRouter` owed; the request a handler or a verifier reads is, on every protocol, one holding the message's headers, at `/`, with no query or body.
|
|
204
204
|
* - An event's stream rpc takes `{ input, cursor }`, a `cursor` omitted or `null` reading from now, and streams `Checked`, `Occurrence`, `Gap` and `Failed`, a `Checked` every 30 seconds; over `RpcServer.layerProtocolHttp` serialize with `RpcSerialization.layerNdjson`: under `layerJson` a response is sent once its streams end, so a watch never answers.
|
|
205
205
|
* - Beside it, an event's `<event>/poll` answers a `Page`, its subscription hook held for the caller's principal, as an `ActionEntity`'s is, so a `client`'s watch in either `mode` watches the layer.
|
|
206
|
-
* - A subscription refused when it starts or later ends its stream rpc with the refusal as the typed failure; a source's failure no refusal explains is logged and reported once, sent as a bare `Failed`, and read again from where it reached after 5 seconds.
|
|
206
|
+
* - A subscription refused when it starts or later ends its stream rpc with the refusal as the typed failure; a source's failure no refusal explains is logged and reported once, sent as a bare `Failed`, and read again from where it reached after 5 seconds; a defect while a poll rpc reads the event, its source, authorizer or subscription hook, is logged and reported once and answered `Action.Unavailable`, which `client`'s `watch` reads on after.
|
|
207
207
|
* - A stream is read only as fast as its client reads it: a slow reader of a log source falls behind its log, and receives a `Gap`.
|
|
208
208
|
* - An action declaring `progress` streams it to every call, a client's call without `onProgress` too, its values handed on one at a time, so a slow reader drops the oldest; under `RpcSerialization.layerJson` over HTTP they arrive with the answer, at the end.
|
|
209
209
|
* - A failure after an action's progress or its first element ends its stream rpc as it ends any call: a declared failure as the typed failure, a defect as `Internal server error`.
|
|
@@ -242,7 +242,7 @@ export type Client<B extends Any> = { readonly [A in B["actions"][number] as A["
|
|
|
242
242
|
export interface ClientOptions extends Omit<NonNullable<Parameters<typeof RpcClient.make>[1]>, "flatten"> {
|
|
243
243
|
/**
|
|
244
244
|
* The `mode` of a watch that gives none: `"poll"` reads its pages from the first, as a server
|
|
245
|
-
* serving events by poll alone, such as
|
|
245
|
+
* serving events by poll alone, such as a socket host that holds polls, needs, without the
|
|
246
246
|
* round trip a watch otherwise spends finding that out. A watch's own `mode` wins.
|
|
247
247
|
*/
|
|
248
248
|
readonly watchMode?: "stream" | "poll" | undefined;
|
|
@@ -265,8 +265,8 @@ export declare const pollResentAfter: Duration.Duration;
|
|
|
265
265
|
* - It drops undeclared input fields as it encodes, and fails with `InvalidInput`, sending nothing, for an input that does not encode.
|
|
266
266
|
* - A browser WebSocket cannot set an `Authorization` header on its upgrade, so under Bearer a protected call without its own token fails with `Unauthenticated`, `A bearer token is required.`: send the token on each call with `RpcClient.withHeaders`.
|
|
267
267
|
* - The socket belongs to the Layer providing the protocol, not to the `Scope` around the client: provided around one call, it opens a socket for that call alone.
|
|
268
|
-
* - `watch(event, input, options)` with no `mode` holds the event's stream rpc open, as `Action.client`'s does, and polls instead, for that watch, where the server answers the stream rpc `Unknown request tag`, as an `ActionEntity` and
|
|
269
|
-
* - `mode: "poll"` calls the event's `<event>/poll` for each page, a poll unanswered for `pollResentAfter` sent again from the same cursor, the first interrupted, so a server that holds a poll until something changes, as
|
|
268
|
+
* - `watch(event, input, options)` with no `mode` holds the event's stream rpc open, as `Action.client`'s does, and polls instead, for that watch, where the server answers the stream rpc `Unknown request tag`, as an `ActionEntity` and a socket host forwarding into one do: one round trip more per watch, which `mode: "poll"` skips.
|
|
269
|
+
* - `mode: "poll"` calls the event's `<event>/poll` for each page, a poll unanswered for `pollResentAfter` sent again from the same cursor, the first interrupted, so a server that holds a poll until something changes, such as a socket host, reads it again at least that often.
|
|
270
270
|
* - A watch of an event the server does not serve fails with `Event.SubscriptionError`, reason `NotServed`, as over HTTP, whatever its `mode`; a webhook `mode` fails with reason `Unsupported`.
|
|
271
271
|
* - A watch reconnects after the protocol's socket or HTTP transport fails, a response ends before its stream does, as a server's restart ends it, a status a server sends while it recovers, or, over Effect's HTTP protocol, 60 seconds without an element, a socket's own pings telling a dead one, a second later and doubling to 30 seconds, each reconnect logged as a warning, and after `Action.Unavailable`; another declared error, a refusal included, or a socket's close with an application's code, 4000 to 4999, ends it.
|
|
272
272
|
* - A native rpc of the binding's `rpcs` is the native client's method: a stream rpc's is a `Stream`, its headers given as its options' `headers`, since `RpcClient.withHeaders` wraps an Effect.
|
|
@@ -315,9 +315,8 @@ export type SocketClientOptions = SocketTarget & ClientOptions & {
|
|
|
315
315
|
*/
|
|
316
316
|
readonly hooks?: Partial<RpcClient.ConnectionHooks["Service"]> | undefined;
|
|
317
317
|
/**
|
|
318
|
-
* How messages are written on the socket: `RpcSerialization.json` by default
|
|
319
|
-
*
|
|
320
|
-
* for an `ActionRpc.layer` served under another.
|
|
318
|
+
* How messages are written on the socket: `RpcSerialization.json` by default; the server's
|
|
319
|
+
* own, such as `RpcSerialization.ndjson`, for an `ActionRpc.layer` served under another.
|
|
321
320
|
*/
|
|
322
321
|
readonly serialization?: RpcSerialization.RpcSerialization["Service"] | undefined;
|
|
323
322
|
};
|
package/dist/Event.d.mts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Options as Options$1 } from "./Action.mjs";
|
|
2
2
|
import { Deferred as Deferred$1 } from "./contract/authorizer.mjs";
|
|
3
3
|
import { Codec, CodecOf, Fields, NoInput } from "./contract/codec.mjs";
|
|
4
|
-
import { Refusal } from "./contract/errors.mjs";
|
|
4
|
+
import { Refusal, Unavailable } from "./contract/errors.mjs";
|
|
5
5
|
import { AnyEventImplementation, EventImplementation } from "./contract/feed.mjs";
|
|
6
6
|
import { Events, KeyOptions, layerMemory } from "./contract/hub.mjs";
|
|
7
7
|
import { Protected } from "./contract/implementation.mjs";
|
|
8
8
|
import { Gap, OccurrenceSchema } from "./contract/occurrence.mjs";
|
|
9
9
|
import { Listed } from "./contract/log.mjs";
|
|
10
|
-
import { DateTime, Duration, Effect, Schema, Scope, Stream } from "effect";
|
|
10
|
+
import { DateTime, Duration, Effect, Schedule, Schema, Scope, Stream } from "effect";
|
|
11
11
|
declare const TypeId = "~@gjermundgaraba/effect-actions/Event";
|
|
12
12
|
/** What `make` needs to define an event type. */
|
|
13
13
|
export interface Options {
|
|
@@ -119,6 +119,7 @@ interface Hooked<E extends Any, R> {
|
|
|
119
119
|
* **Gotchas**
|
|
120
120
|
*
|
|
121
121
|
* - An occurrence without `eventId` takes a fresh id at each read, and a page's occurrences before its last are read again from the cursor asked after a reconnect mid-page: give the upstream's ids, or a subscriber receives them again under ids no `watch` drops.
|
|
122
|
+
* - A read that fails for a while, such as its store being down, fails with `Action.Unavailable`, which a watch reads on after as after any failure no refusal explains, and an HTTP poll is answered 503 with `Retry-After`; a refusal such as `Forbidden` ends the watch.
|
|
122
123
|
*/
|
|
123
124
|
export interface Check<E extends Any, R = never> extends Hooked<E, R> {
|
|
124
125
|
/**
|
|
@@ -126,14 +127,14 @@ export interface Check<E extends Any, R = never> extends Hooked<E, R> {
|
|
|
126
127
|
* A `null` cursor is a subscriber with no position: answer nothing, so it starts after the last
|
|
127
128
|
* occurrence. An event whose occurrence is the whole value, such as a status, is a `Current`.
|
|
128
129
|
*/
|
|
129
|
-
readonly check: (input: E["input"]["Type"], cursor: string | null) => Effect.Effect<Page<E>, Refusal, R>;
|
|
130
|
+
readonly check: (input: E["input"]["Type"], cursor: string | null) => Effect.Effect<Page<E>, Refusal | Unavailable, R>;
|
|
130
131
|
/**
|
|
131
132
|
* Every change `check` reads is announced by an `Events.notify` the server serving it receives,
|
|
132
133
|
* as a store only that server's handlers write is: its streams and webhook deliveries then ask
|
|
133
134
|
* `check` again only when notified, on no timer, so an idle Cloudflare Durable Object holding
|
|
134
135
|
* them hibernates. Defaults to `false`, asking again at each page's `nextPoll` too. A polling
|
|
135
136
|
* client polls at `nextPoll` either way, and an rpc's poll page says `notified`, so a host that
|
|
136
|
-
* holds polls, as
|
|
137
|
+
* holds polls, such as a socket host, holds one of this source alone.
|
|
137
138
|
*
|
|
138
139
|
* **Gotchas**
|
|
139
140
|
*
|
|
@@ -167,9 +168,9 @@ export interface Appended<E extends Any> {
|
|
|
167
168
|
/** What `appended` needs to page a store appending entries under increasing integer ids. */
|
|
168
169
|
export interface AppendedOptions<E extends Any, RN = never, RA = never, RK = never, RS = never> extends Pick<Check<E>, "notified" | "lease"> {
|
|
169
170
|
/** The id of the newest entry, `0` while there is none, run as the subscriber. */
|
|
170
|
-
readonly newest: (input: E["input"]["Type"]) => Effect.Effect<number, Refusal, RN>;
|
|
171
|
+
readonly newest: (input: E["input"]["Type"]) => Effect.Effect<number, Refusal | Unavailable, RN>;
|
|
171
172
|
/** The entries after `id`, oldest first, as many as one read takes, run as the subscriber. */
|
|
172
|
-
readonly after: (input: E["input"]["Type"], id: number) => Effect.Effect<Appended<E>, Refusal, RA>;
|
|
173
|
+
readonly after: (input: E["input"]["Type"], id: number) => Effect.Effect<Appended<E>, Refusal | Unavailable, RA>;
|
|
173
174
|
/** The id of the entry an occurrence's data was read from: its `eventId`, and a cursor. */
|
|
174
175
|
readonly id: (data: E["payload"]["Type"]) => number;
|
|
175
176
|
/** Each page's `nextPoll`; defaults to 5 seconds. */
|
|
@@ -204,10 +205,11 @@ export declare const appended: <E extends Any, RN = never, RA = never, RK = neve
|
|
|
204
205
|
* - A notify reads the value again for each subscriber it reaches, though a value equal to the one its cursor saw, for the same input and key, is not sent: notify after a write that may change it, not on a timer.
|
|
205
206
|
* - The revision is read before `current`, so a value written between them is read again at the next read, never missed: write the value, then notify.
|
|
206
207
|
* - A cursor is its subscription's own: one taken to another input of the same key, at a revision it already holds, is answered with nothing until the next notify.
|
|
208
|
+
* - A read that fails for a while, such as its store being down, fails with `Action.Unavailable`, which a watch reads on after as after any failure no refusal explains, and an HTTP poll is answered 503 with `Retry-After`; a refusal such as `Forbidden` ends the watch.
|
|
207
209
|
*/
|
|
208
210
|
export interface Current<E extends Any, R = never> extends Hooked<E, R> {
|
|
209
211
|
/** The value as it is, as the subscriber's `input` selects it, run as the subscriber. */
|
|
210
|
-
readonly current: (input: E["input"]["Type"]) => Effect.Effect<E["payload"]["Type"], Refusal, R>;
|
|
212
|
+
readonly current: (input: E["input"]["Type"]) => Effect.Effect<E["payload"]["Type"], Refusal | Unavailable, R>;
|
|
211
213
|
/**
|
|
212
214
|
* How long a polling client waits before it asks again, read in the bounds of `Page`'s
|
|
213
215
|
* `nextPoll`; defaults to 5 seconds. Streams and webhook deliveries never wait on it: they read
|
|
@@ -268,7 +270,7 @@ export type Source<E extends Any, R = never> = Check<E, R> | Current<E, R> | Log
|
|
|
268
270
|
* cannot be resumed.
|
|
269
271
|
*/
|
|
270
272
|
export type Occurrence<E extends Any> = OccurrenceSchema<E>["Type"];
|
|
271
|
-
/** What a subscriber receives: occurrences, and gaps where it missed some
|
|
273
|
+
/** What a subscriber receives: occurrences, and gaps where it missed some, told apart by `_tag`, `"Occurrence"` or `"Gap"`. */
|
|
272
274
|
export type Received<E extends Any> = Occurrence<E> | Gap;
|
|
273
275
|
declare const NotServed_base: Schema.Class<NotServed, Schema.TaggedStruct<"NotServed", {}>, import("effect/Cause").YieldableError & {
|
|
274
276
|
readonly brand: unique symbol;
|
|
@@ -331,6 +333,20 @@ export interface CursorStore<E = never> {
|
|
|
331
333
|
readonly get: (key: string) => Effect.Effect<string | undefined, E>;
|
|
332
334
|
readonly set: (key: string, cursor: string) => Effect.Effect<void, E>;
|
|
333
335
|
}
|
|
336
|
+
/** A `CursorStore` in memory, which `memoryCursors` makes. */
|
|
337
|
+
export interface MemoryCursors extends CursorStore {
|
|
338
|
+
readonly _tag: "MemoryCursors";
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* A `CursorStore` in memory, for as long as it is referenced: give a watch one of its own,
|
|
342
|
+
* `resume: { store: Event.memoryCursors() }`, which takes no `key`, so a retry of the watch's
|
|
343
|
+
* stream goes on where the failed run stopped; a restart starts from now.
|
|
344
|
+
*
|
|
345
|
+
* **Gotchas**
|
|
346
|
+
*
|
|
347
|
+
* - One store given to two watches without a `key` keeps one cursor for both, so each resumes from the other's and misses occurrences: make one per watch.
|
|
348
|
+
*/
|
|
349
|
+
export declare const memoryCursors: () => MemoryCursors;
|
|
334
350
|
declare const WebhookDelivery_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Readonly<A> & {
|
|
335
351
|
readonly _tag: "WebhookDelivery";
|
|
336
352
|
} & import("effect/Pipeable").Pipeable;
|
|
@@ -370,12 +386,27 @@ export interface WatchOptions<E = never> {
|
|
|
370
386
|
/**
|
|
371
387
|
* Where to keep the cursor between watches, such as across restarts: it starts from the cursor
|
|
372
388
|
* kept under `key`, when no `cursor` is given, and keeps each new one there. Effect's
|
|
373
|
-
* `KeyValueStore` is one: `{ store: yield* KeyValueStore.KeyValueStore, key: "builds" }
|
|
389
|
+
* `KeyValueStore` is one: `{ store: yield* KeyValueStore.KeyValueStore, key: "builds" }`; a
|
|
390
|
+
* store of the watch's own, `memoryCursors()`, takes no `key`.
|
|
374
391
|
*/
|
|
375
392
|
readonly resume?: {
|
|
376
393
|
readonly store: CursorStore<E>;
|
|
377
394
|
readonly key: string;
|
|
395
|
+
} | {
|
|
396
|
+
readonly store: MemoryCursors;
|
|
397
|
+
readonly key?: string | undefined;
|
|
378
398
|
} | undefined;
|
|
399
|
+
/**
|
|
400
|
+
* Bounds a watch's reconnects after failures it retries, such as a dropped stream or an
|
|
401
|
+
* `Unavailable`: with `Schedule.recurs(5)`, the sixth failure in a row ends the watch with it.
|
|
402
|
+
* Between reconnects the watch waits the longer of the schedule's delay and its own backoff;
|
|
403
|
+
* left out, it reconnects without end.
|
|
404
|
+
*
|
|
405
|
+
* **Gotchas**
|
|
406
|
+
*
|
|
407
|
+
* - The count starts again at anything the watch receives, a stream's 30-second heartbeat included, so it bounds failures in a row, not a watch's lifetime.
|
|
408
|
+
*/
|
|
409
|
+
readonly reconnect?: Schedule.Schedule<unknown> | undefined;
|
|
379
410
|
}
|
|
380
411
|
/**
|
|
381
412
|
* Authorization for protected event types: it refuses, and fails with nothing else. It runs for
|
package/dist/Event.mjs
CHANGED
|
@@ -131,6 +131,25 @@ var SubscriptionError = class SubscriptionError extends Schema.TaggedError()("Su
|
|
|
131
131
|
}
|
|
132
132
|
};
|
|
133
133
|
/**
|
|
134
|
+
* A `CursorStore` in memory, for as long as it is referenced: give a watch one of its own,
|
|
135
|
+
* `resume: { store: Event.memoryCursors() }`, which takes no `key`, so a retry of the watch's
|
|
136
|
+
* stream goes on where the failed run stopped; a restart starts from now.
|
|
137
|
+
*
|
|
138
|
+
* **Gotchas**
|
|
139
|
+
*
|
|
140
|
+
* - One store given to two watches without a `key` keeps one cursor for both, so each resumes from the other's and misses occurrences: make one per watch.
|
|
141
|
+
*/
|
|
142
|
+
const memoryCursors = () => {
|
|
143
|
+
const cursors = /* @__PURE__ */ new Map();
|
|
144
|
+
return {
|
|
145
|
+
_tag: "MemoryCursors",
|
|
146
|
+
get: (key) => Effect.sync(() => cursors.get(key)),
|
|
147
|
+
set: (key, cursor) => Effect.sync(() => {
|
|
148
|
+
cursors.set(key, cursor);
|
|
149
|
+
})
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
134
153
|
* A watch delivered by webhook, which `Webhook.delivery` makes: the server POSTs each occurrence
|
|
135
154
|
* to the route `Webhook.layerReceiver` serves in the watching process, reached under `baseUrl`,
|
|
136
155
|
* for as long as the watch is read, each grant asking for `ttl`.
|
|
@@ -147,4 +166,4 @@ function implement(events, sources, options) {
|
|
|
147
166
|
}, listed.some((event) => event.caller !== Anyone), options?.authorize);
|
|
148
167
|
return new EventImplementation(listed, sourcesOf(listed, !Array.isArray(events), sources), authorizer);
|
|
149
168
|
}
|
|
150
|
-
export { CallbackCategory, CallbackFailed, Events, Gap, NotServed, NotSubscribed, SubscriptionError, Unsupported, WebhookDelivery, allowAll, appended, implement, isEvent, layerMemory, make };
|
|
169
|
+
export { CallbackCategory, CallbackFailed, Events, Gap, NotServed, NotSubscribed, SubscriptionError, Unsupported, WebhookDelivery, allowAll, appended, implement, isEvent, layerMemory, make, memoryCursors };
|
package/dist/Stored.d.mts
CHANGED
|
@@ -5,10 +5,10 @@ import { SqlClient } from "effect/sql";
|
|
|
5
5
|
/**
|
|
6
6
|
* What `layerEvents` calls in the fiber that made each change its change signal counts, each
|
|
7
7
|
* append to a log, published or emitted, and each `notify`, once the change is written: a host
|
|
8
|
-
* serving the database's events from outside `Events`, as
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* serving the database's events from outside `Events`, such as a socket host that holds polls,
|
|
9
|
+
* learns of a change as it is made, by work that outlived the call that started it too, not only
|
|
10
|
+
* when a handler ends. `layerEvents` reads it as it builds; by default it does nothing, and such a
|
|
11
|
+
* host provides one to each entity's build.
|
|
12
12
|
*
|
|
13
13
|
* **Gotchas**
|
|
14
14
|
*
|
|
@@ -30,7 +30,7 @@ export declare const ChangeListener: Context.Reference<() => void>;
|
|
|
30
30
|
*
|
|
31
31
|
* **Gotchas**
|
|
32
32
|
*
|
|
33
|
-
* - Written for SQLite, such as a cluster entity's own on a Durable Object, `CloudflareCluster.DurableObjectSqlClient`, and tested on `@effect/sql-sqlite-node`'s
|
|
33
|
+
* - Written for SQLite, such as a cluster entity's own on a Durable Object, `CloudflareCluster.DurableObjectSqlClient`, and tested on `@effect/sql-sqlite-node`'s: another dialect is unsupported.
|
|
34
34
|
* - Build it once per database, as `Event.layerMemory` is built once per process: two builds over one database each wake only their own subscribers, and their appends to one log collide, dying on the table's key.
|
|
35
35
|
* - Logs are kept by their event's name: two event types of one name attached to one build die, naming it.
|
|
36
36
|
* - A `key` holding a lone surrogate, not well-formed UTF-16, is kept with U+FFFD in its place: its occurrences no longer reach that key's subscribers.
|
|
@@ -43,12 +43,12 @@ export declare const layerEvents: Layer.Layer<Events, never, SqlClient.SqlClient
|
|
|
43
43
|
* column is the sum of `effect_actions_event_revision`'s counts and of
|
|
44
44
|
* `effect_actions_event_position`'s `last` sequence numbers: it moves at each `notify` and each
|
|
45
45
|
* append to a log, and at no read or restart, so a host that runs it as `ChangeListener` is
|
|
46
|
-
* called and after each handler, as
|
|
47
|
-
*
|
|
46
|
+
* called and after each handler, such as a socket host that holds polls, learns whether a parked
|
|
47
|
+
* poll could answer anything new.
|
|
48
48
|
*
|
|
49
49
|
* **Gotchas**
|
|
50
50
|
*
|
|
51
|
-
* - Before `layerEvents` first builds on the database its tables are missing, and the query fails with SQLite's `no such table`:
|
|
51
|
+
* - Before `layerEvents` first builds on the database its tables are missing, and the query fails with SQLite's `no such table`: a host reading that alone as no signal answers polls at once, as for an entity whose events are kept elsewhere, and another failure, as on tables of another shape, is a defect of the poll.
|
|
52
52
|
*/
|
|
53
53
|
export declare const changeSignal: string;
|
|
54
54
|
/**
|
package/dist/Stored.mjs
CHANGED
|
@@ -8,10 +8,10 @@ import { SqlClient } from "effect/sql";
|
|
|
8
8
|
/**
|
|
9
9
|
* What `layerEvents` calls in the fiber that made each change its change signal counts, each
|
|
10
10
|
* append to a log, published or emitted, and each `notify`, once the change is written: a host
|
|
11
|
-
* serving the database's events from outside `Events`, as
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* serving the database's events from outside `Events`, such as a socket host that holds polls,
|
|
12
|
+
* learns of a change as it is made, by work that outlived the call that started it too, not only
|
|
13
|
+
* when a handler ends. `layerEvents` reads it as it builds; by default it does nothing, and such a
|
|
14
|
+
* host provides one to each entity's build.
|
|
15
15
|
*
|
|
16
16
|
* **Gotchas**
|
|
17
17
|
*
|
|
@@ -33,7 +33,7 @@ const ChangeListener = Context.Reference("effect-actions/Stored/ChangeListener",
|
|
|
33
33
|
*
|
|
34
34
|
* **Gotchas**
|
|
35
35
|
*
|
|
36
|
-
* - Written for SQLite, such as a cluster entity's own on a Durable Object, `CloudflareCluster.DurableObjectSqlClient`, and tested on `@effect/sql-sqlite-node`'s
|
|
36
|
+
* - Written for SQLite, such as a cluster entity's own on a Durable Object, `CloudflareCluster.DurableObjectSqlClient`, and tested on `@effect/sql-sqlite-node`'s: another dialect is unsupported.
|
|
37
37
|
* - Build it once per database, as `Event.layerMemory` is built once per process: two builds over one database each wake only their own subscribers, and their appends to one log collide, dying on the table's key.
|
|
38
38
|
* - Logs are kept by their event's name: two event types of one name attached to one build die, naming it.
|
|
39
39
|
* - A `key` holding a lone surrogate, not well-formed UTF-16, is kept with U+FFFD in its place: its occurrences no longer reach that key's subscribers.
|
|
@@ -73,12 +73,12 @@ const layerEvents = Layer.effect(Events)(Effect.gen(function* () {
|
|
|
73
73
|
* column is the sum of `effect_actions_event_revision`'s counts and of
|
|
74
74
|
* `effect_actions_event_position`'s `last` sequence numbers: it moves at each `notify` and each
|
|
75
75
|
* append to a log, and at no read or restart, so a host that runs it as `ChangeListener` is
|
|
76
|
-
* called and after each handler, as
|
|
77
|
-
*
|
|
76
|
+
* called and after each handler, such as a socket host that holds polls, learns whether a parked
|
|
77
|
+
* poll could answer anything new.
|
|
78
78
|
*
|
|
79
79
|
* **Gotchas**
|
|
80
80
|
*
|
|
81
|
-
* - Before `layerEvents` first builds on the database its tables are missing, and the query fails with SQLite's `no such table`:
|
|
81
|
+
* - Before `layerEvents` first builds on the database its tables are missing, and the query fails with SQLite's `no such table`: a host reading that alone as no signal answers polls at once, as for an entity whose events are kept elsewhere, and another failure, as on tables of another shape, is a defect of the poll.
|
|
82
82
|
*/
|
|
83
83
|
const changeSignal = changeSignalQuery;
|
|
84
84
|
const decodeKept = Schema.decodeUnknownOption(Kept);
|
package/dist/contract/watch.mjs
CHANGED
|
@@ -10,22 +10,25 @@ import { Cause, Clock, Duration, Effect, Predicate, Queue, Schedule, Schema, Str
|
|
|
10
10
|
// src/contract/watch.ts
|
|
11
11
|
const reconnectDelay = Duration.seconds(1);
|
|
12
12
|
const maxReconnectDelay = Duration.seconds(30);
|
|
13
|
-
const afterFailures = (transient
|
|
13
|
+
const afterFailures = (transient, reconnect) => {
|
|
14
|
+
const backoff = Schedule.min([Schedule.exponential(reconnectDelay), Schedule.spaced(maxReconnectDelay)]).pipe(Schedule.setInputType(), Schedule.while(({ input }) => transient(input)));
|
|
15
|
+
return (reconnect === void 0 ? backoff : Schedule.max([backoff, reconnect])).pipe(Schedule.tap(({ input, duration }) => Effect.logWarning(`A watch reconnects in ${Duration.format(duration)} after a failure`, input)));
|
|
16
|
+
};
|
|
14
17
|
const isSubscriptionError = Schema.is(SubscriptionError);
|
|
15
18
|
const pushRefused = Predicate.compose(isSubscriptionError, ({ reason }) => Predicate.isTagged(reason, "Unsupported") && reason.feature === "deliveryMode" && reason.value === "push");
|
|
16
|
-
const reconnecting = (transient, open, cursor, polled) => {
|
|
19
|
+
const reconnecting = (transient, reconnect, open, cursor, polled) => {
|
|
17
20
|
let at = cursor;
|
|
18
21
|
const once = Stream.suspend(() => open(at)).pipe(Stream.tap((fed) => Effect.sync(() => {
|
|
19
22
|
at = fed.cursor ?? at;
|
|
20
23
|
})));
|
|
21
|
-
const reopened = transient === void 0 ? once.pipe(Stream.retry(afterFailures(isUnavailable))) : once.pipe(Stream.repeat(Schedule.spaced(reconnectDelay)), Stream.retry(afterFailures(Predicate.or(transient, isUnavailable))));
|
|
24
|
+
const reopened = transient === void 0 ? once.pipe(Stream.retry(afterFailures(isUnavailable, reconnect))) : once.pipe(Stream.repeat(Schedule.spaced(reconnectDelay)), Stream.retry(afterFailures(Predicate.or(transient, isUnavailable), reconnect)));
|
|
22
25
|
return polled === void 0 ? reopened : reopened.pipe(Stream.catchIf(pushRefused, () => Stream.suspend(() => polled(at))));
|
|
23
26
|
};
|
|
24
27
|
const closeTimeout = Duration.seconds(5);
|
|
25
28
|
const closing = (closed) => closed.pipe(Effect.timeout(closeTimeout), Effect.ignore({ log: "Debug" }));
|
|
26
|
-
const polling = ({ poll }, transient) => (cursor) => Stream.suspend(() => {
|
|
29
|
+
const polling = ({ poll }, transient, reconnect) => (cursor) => Stream.suspend(() => {
|
|
27
30
|
const watch = globalThis.crypto.randomUUID();
|
|
28
|
-
return reconnecting(transient, (at) => paged((from) => poll(from, watch), at), cursor);
|
|
31
|
+
return reconnecting(transient, reconnect, (at) => paged((from) => poll(from, watch), at), cursor);
|
|
29
32
|
});
|
|
30
33
|
const capacity = 16;
|
|
31
34
|
const delivered = (webhook, request, received, cursor) => {
|
|
@@ -43,9 +46,9 @@ const delivered = (webhook, request, received, cursor) => {
|
|
|
43
46
|
})));
|
|
44
47
|
}));
|
|
45
48
|
};
|
|
46
|
-
const watching = (transport, { cursor: given, mode, resume } = {}) => {
|
|
49
|
+
const watching = (transport, { cursor: given, mode, resume, reconnect } = {}) => {
|
|
47
50
|
const { transient } = transport;
|
|
48
|
-
const polled = polling(transport, transient);
|
|
51
|
+
const polled = polling(transport, transient, reconnect);
|
|
49
52
|
const webhooked = ({ baseUrl, ttl }, cursor) => Stream.unwrap(Effect.gen(function* () {
|
|
50
53
|
const { webhook } = transport;
|
|
51
54
|
if (webhook === void 0) return yield* deliveryUnsupported(transport.name, "webhook");
|
|
@@ -63,12 +66,13 @@ const watching = (transport, { cursor: given, mode, resume } = {}) => {
|
|
|
63
66
|
ttlMs: ttl === void 0 ? void 0 : Duration.toMillis(Duration.fromInputUnsafe(ttl))
|
|
64
67
|
};
|
|
65
68
|
yield* Effect.addFinalizer(() => closing(webhook.unsubscribe({ delivery: { url } })));
|
|
66
|
-
return reconnecting(transient, (at) => delivered(webhook, request, received, at), cursor);
|
|
69
|
+
return reconnecting(transient, reconnect, (at) => delivered(webhook, request, received, at), cursor);
|
|
67
70
|
}));
|
|
68
71
|
return Stream.suspend(() => {
|
|
69
72
|
const seen = makeRecent();
|
|
70
73
|
let keptOnNextPull = null;
|
|
71
|
-
const
|
|
74
|
+
const key = resume?.key ?? transport.name;
|
|
75
|
+
const keep = (cursor) => cursor === null || resume === void 0 ? Effect.void : resume.store.set(key, cursor);
|
|
72
76
|
const fresh = (eventId) => {
|
|
73
77
|
if (seen.has(eventId)) return false;
|
|
74
78
|
seen.add(eventId);
|
|
@@ -85,11 +89,11 @@ const watching = (transport, { cursor: given, mode, resume } = {}) => {
|
|
|
85
89
|
keptOnNextPull = fed.cursor;
|
|
86
90
|
return fresh(fed.eventId) ? [fed] : [];
|
|
87
91
|
});
|
|
88
|
-
const start = given !== void 0 || resume === void 0 ? Effect.succeed(given ?? null) : Effect.map(resume.store.get(
|
|
92
|
+
const start = given !== void 0 || resume === void 0 ? Effect.succeed(given ?? null) : Effect.map(resume.store.get(key), (kept) => kept ?? null);
|
|
89
93
|
return Stream.unwrap(Effect.map(start, (cursor) => {
|
|
90
94
|
if (Predicate.isTagged(mode, "WebhookDelivery")) return webhooked(mode, cursor);
|
|
91
95
|
if (mode === "poll") return polled(cursor);
|
|
92
|
-
return reconnecting(transient, transport.stream, cursor, mode === "stream" ? void 0 : polled);
|
|
96
|
+
return reconnecting(transient, reconnect, transport.stream, cursor, mode === "stream" ? void 0 : polled);
|
|
93
97
|
})).pipe(Stream.rechunk(1), Stream.mapEffect(handled), Stream.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Stream.failCause(cause) : Stream.concat(Stream.fromEffectDrain(keep(keptOnNextPull)), Stream.failCause(cause))), Stream.flattenIterable);
|
|
94
98
|
});
|
|
95
99
|
};
|
|
@@ -5,7 +5,7 @@ import { Headers } from "effect/http";
|
|
|
5
5
|
* one but its connection's (`host`, `connection`, `keep-alive`, `te`, `trailer`,
|
|
6
6
|
* `transfer-encoding`, `upgrade`), its body's (`content-*`), what it accepts (`accept*`), a
|
|
7
7
|
* proxy's (`proxy-*`), a WebSocket upgrade's (`sec-websocket-*`) and an MCP request's routing
|
|
8
|
-
* headers (`mcp-*`). `reaching` sends these of the caller's request, and
|
|
9
|
-
*
|
|
8
|
+
* headers (`mcp-*`). `reaching` sends these of the caller's request, and a socket host forwarding
|
|
9
|
+
* a page's calls into an entity keeps these of the page's upgrade.
|
|
10
10
|
*/
|
|
11
11
|
export declare const forwardedHeaders: (headers: Headers.Input) => Headers.Headers;
|
|
@@ -21,8 +21,8 @@ const ownedByTheRequest = (name) => [
|
|
|
21
21
|
* one but its connection's (`host`, `connection`, `keep-alive`, `te`, `trailer`,
|
|
22
22
|
* `transfer-encoding`, `upgrade`), its body's (`content-*`), what it accepts (`accept*`), a
|
|
23
23
|
* proxy's (`proxy-*`), a WebSocket upgrade's (`sec-websocket-*`) and an MCP request's routing
|
|
24
|
-
* headers (`mcp-*`). `reaching` sends these of the caller's request, and
|
|
25
|
-
*
|
|
24
|
+
* headers (`mcp-*`). `reaching` sends these of the caller's request, and a socket host forwarding
|
|
25
|
+
* a page's calls into an entity keeps these of the page's upgrade.
|
|
26
26
|
*/
|
|
27
27
|
const forwardedHeaders = (headers) => Headers.fromInput(Object.entries(Headers.fromInput(headers)).filter(([name]) => !ownedByTheRequest(name)));
|
|
28
28
|
const forwarded = Effect.map(Effect.serviceOption(HttpServerRequest.HttpServerRequest), Option.match({
|
package/dist/rpc/erased.mjs
CHANGED
|
@@ -2,13 +2,14 @@ import { providerOf } from "../authentication/provider.mjs";
|
|
|
2
2
|
import { checked, checkedStream, decodeInput, sendingJson, sentInput } from "../contract/call.mjs";
|
|
3
3
|
import { Anyone, principalOf } from "../contract/caller.mjs";
|
|
4
4
|
import { bindingErrors, eventErrors } from "../contract/declared.mjs";
|
|
5
|
+
import { Unavailable } from "../contract/errors.mjs";
|
|
5
6
|
import { SubscriptionError } from "../Event.mjs";
|
|
6
7
|
import { acquireFeeds } from "../contract/feed.mjs";
|
|
7
8
|
import { acquire, answered, elementsOf } from "../contract/implementation.mjs";
|
|
8
9
|
import { pageOf, rpcPageOf } from "../contract/pages.mjs";
|
|
9
10
|
import { answerFrom, elementsFrom } from "../contract/progress.mjs";
|
|
10
11
|
import { PushedOf, SubscriptionOf, queuedPush } from "../contract/push.mjs";
|
|
11
|
-
import { concealed } from "../contract/reported.mjs";
|
|
12
|
+
import { concealed, reported } from "../contract/reported.mjs";
|
|
12
13
|
import { Answering, answeringOf, elementOf } from "../contract/stream.mjs";
|
|
13
14
|
import { Subscribed, WebhookCancellationOf, WebhookSubscriptionOf, deliveryUnsupported } from "../contract/subscribe.mjs";
|
|
14
15
|
import { watching } from "../contract/watch.mjs";
|
|
@@ -288,7 +289,7 @@ const pollHandlerOf = (feed, steps, error, authentication, check) => {
|
|
|
288
289
|
const page = yield* feed.poll(input, cursor, {
|
|
289
290
|
subscriber,
|
|
290
291
|
maxEvents
|
|
291
|
-
});
|
|
292
|
+
}).pipe(Effect.catchCauseIf(Cause.hasDies, (cause) => Effect.andThen(reported(cause), Effect.fail(new Unavailable({ message: `Event "${feed.event.name}" could not be read: try again later.` })))));
|
|
292
293
|
return yield* encode({
|
|
293
294
|
...page,
|
|
294
295
|
notified: !feed.polls
|
package/docs/ActionEntity.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
`ActionEntity` serves actions and event types as an Effect Cluster entity. An entity type is a kind of stateful object, such as a chat room, and an entity id names one of them, such as the room `lobby`. The cluster's `Sharding` runs each entity id in one place at a time, on whatever runtime provides it: a Node or SQL cluster, `TestRunner` in a test, or a host that runs one entity per object. Each call reaches the entity its input names, and that entity builds its own handlers.
|
|
4
4
|
|
|
5
|
-
The example is [shopping-carts](../examples/shopping-carts/), carts on one process over SQLite
|
|
5
|
+
The example is [shopping-carts](../examples/shopping-carts/), carts on one process over SQLite. A message the library throws says its fix; for a compiler diagnostic or Effect's own error, search [ActionEntity-failures.md](ActionEntity-failures.md). For one symbol, `grep -rn <symbol> node_modules/@gjermundgaraba/effect-actions/dist/`.
|
|
6
6
|
|
|
7
7
|
## Define an entity
|
|
8
8
|
|
|
@@ -86,7 +86,7 @@ A call the entity could not be reached for, or that did not answer within `reach
|
|
|
86
86
|
|
|
87
87
|
An event is followed through the entity's poll rpc, so no watcher holds an entity open; a stream polls it from the surface. What crosses the two hops, and what does not, is on `ActionEntity.reaching`.
|
|
88
88
|
|
|
89
|
-
A server outside the cluster that forwards a socket's calls into the entity, as
|
|
89
|
+
A server outside the cluster that forwards a socket's calls into the entity, such as a socket host, forwards those rpcs of `ActionEntity.bindingRpcs(entity)` its own binding lists, never the entity's internal actions or native rpcs, which authenticate nothing; its clients call them with `ActionRpc.client` of that binding, whose watch polls them.
|
|
90
90
|
|
|
91
91
|
## Watch an event
|
|
92
92
|
|
package/docs/ActionHttp.md
CHANGED
|
@@ -159,7 +159,7 @@ Give `Http.api`, a plain `HttpApi`, to `OpenApi.fromApi`, `HttpApiSwagger.layer`
|
|
|
159
159
|
|
|
160
160
|
## Call the API from Effect
|
|
161
161
|
|
|
162
|
-
What a call fails with is on `ActionHttp.client`, and a token or a per-request sign-in goes through `ActionHttp.ClientOptions.transformClient`.
|
|
162
|
+
What a call fails with is on `ActionHttp.client`, its declared and built-in errors typed `ActionHttp.MethodError`, and a token or a per-request sign-in goes through `ActionHttp.ClientOptions.transformClient`.
|
|
163
163
|
|
|
164
164
|
```ts example=quickstart/src/client.ts
|
|
165
165
|
import { NodeRuntime } from "@effect/platform-node";
|
package/docs/ActionMcp.md
CHANGED
|
@@ -26,7 +26,7 @@ export const routes = Layer.mergeAll(
|
|
|
26
26
|
|
|
27
27
|
- Claude Code 2.1 and Codex CLI 0.162 connect: `claude mcp add --transport http <name> <url>/mcp --header "Authorization: Bearer <token>"`, `codex mcp add <name> --url <url>/mcp --bearer-token-env-var <VAR>`; [mcp-tools](../examples/mcp-tools/README.md#connect-a-client) has stdio and Claude Desktop.
|
|
28
28
|
- Where an endpoint without `authentication` listens, and its body limit: [mcp-tools/src/server.ts](../examples/mcp-tools/src/server.ts).
|
|
29
|
-
- **On a serverless host**, such as Cloudflare Workers, pass `requestScoped: true`: [notes-service/src/worker.ts](../examples/notes-service/src/worker.ts).
|
|
29
|
+
- **On a serverless host**, such as Cloudflare Workers, pass `requestScoped: true`: [notes-service/src/worker.ts](../examples/notes-service/src/worker.ts).
|
|
30
30
|
- `McpServer.middleware` and `McpServer.visibility` provided around the endpoint run for its tools and its native features alike. A limit that must hold on every surface goes in the handlers: [kitchen-sink/src/issues/write-limit.ts](../examples/kitchen-sink/src/issues/write-limit.ts).
|
|
31
31
|
|
|
32
32
|
## Serve tools over stdio
|
package/docs/ActionRpc.md
CHANGED
|
@@ -86,7 +86,7 @@ Layer.launch(server).pipe(NodeRuntime.runMain());
|
|
|
86
86
|
|
|
87
87
|
Where the protocol goes, why one protocol takes one layer, and what a protocol with no router lacks are on `ActionRpc.layer`; the order of `middleware`, and which actions a middleware reading the identity may serve, on `ActionRpc.LayerOptions.middleware`. [kitchen-sink/src/identity/quota.ts](../examples/kitchen-sink/src/identity/quota.ts) is a limit in `middleware`, keyed on the caller, and [kitchen-sink/src/Rpc.ts](../examples/kitchen-sink/src/Rpc.ts) binds the protected actions alone for it.
|
|
88
88
|
|
|
89
|
-
- **Actions a cluster entity holds.** `ActionRpc.layer` serves `ActionEntity.reaching(entity)`'s implementations as it serves its own, each call sent to the entity its input names ([ActionEntity.md](ActionEntity.md)).
|
|
89
|
+
- **Actions a cluster entity holds.** `ActionRpc.layer` serves `ActionEntity.reaching(entity)`'s implementations as it serves its own, each call sent to the entity its input names ([ActionEntity.md](ActionEntity.md)).
|
|
90
90
|
- **The same actions as JSON too.** Merge `ActionHttp.layer(Http, implementations)` beside the rpc layer, each given its descriptor's provider ([kitchen-sink/src/routes.ts](../examples/kitchen-sink/src/routes.ts) defines both, and [server.ts](../examples/kitchen-sink/src/server.ts) merges them): one provider given to both is built once. The routes need what the rpcs did not: an `httpApiStatus` on a declared error, by the rule on `Action.Options.error`, and router middleware for a service an `RpcMiddleware` provides ([ActionHttp.md](ActionHttp.md#serve-the-routes)).
|
|
91
91
|
- **An rpc of your own beside the actions.** Give `ActionRpc.make` native rpcs in `rpcs`, `ActionRpc.make(actions, { authentication, rpcs: RpcGroup.make(Upload) })`, each with its own schemas: under the binding's `authentication` each message authenticates as a protected action's does, and the client has their methods beside the actions'. Implement them with the binding's own `Rpc.rpcs.toLayer(handlers)`, whose handlers read the identity and authorize it themselves, and provide that to `ActionRpc.layer`, which otherwise does not compile. The rules are on `ActionRpc.Options.rpcs`.
|
|
92
92
|
- **An action reporting progress or answering a stream is a stream rpc.** Over a WebSocket, or HTTP under `RpcSerialization.layerNdjson`, its values and elements arrive as sent; under `layerJson` over HTTP, all at the end (`ActionRpc.layer`).
|
package/docs/Event.md
CHANGED
|
@@ -161,7 +161,7 @@ export const buildEvents = Event.implement(
|
|
|
161
161
|
|
|
162
162
|
A subscriber holding a stream open is read again every 5 seconds. Where the application writes the store itself, it wakes them at once: after the write, `Event.Events`' `notify`, as [build-events/src/builds/handlers.ts](../examples/build-events/src/builds/handlers.ts) does after each build finishes. A store another process writes is read at the next poll.
|
|
163
163
|
|
|
164
|
-
Where the store appends entries under increasing integer ids that last as long as it does, such as a SQLite table's `INTEGER PRIMARY KEY`, `Event.appended` builds the check from the newest id and a read after an id
|
|
164
|
+
Where the store appends entries under increasing integer ids that last as long as it does, such as a SQLite table's `INTEGER PRIMARY KEY`, `Event.appended` builds the check from the newest id and a read after an id: the cursor is the last id read, and what a stale cursor answers is on `Event.appended`.
|
|
165
165
|
|
|
166
166
|
- **Check what a subscriber may see where the data is read**, as an action's handler does: `authorize` decides whether it may subscribe at all, and receives the event type, not the subscription's input.
|
|
167
167
|
|
|
@@ -243,4 +243,4 @@ When each subscription needs something set up upstream while it lasts, such as j
|
|
|
243
243
|
|
|
244
244
|
## Watch in process
|
|
245
245
|
|
|
246
|
-
`Action.client` given event implementations has `watch(event, input, options)`: a `Stream` of `Event.Received`, the occurrences and a `Gap` wherever some were missed, owing per subscription what an action's call owes. [Testing.md](Testing.md#test-events) watches one in a test.
|
|
246
|
+
`Action.client` given event implementations has `watch(event, input, options)`: a `Stream` of `Event.Received`, the occurrences and a `Gap` wherever some were missed, owing per subscription what an action's call owes. [Testing.md](Testing.md#test-events) watches one in a test. A watch reconnects after a failure it can retry without end; give it a `reconnect` schedule where a reader must learn that it stopped. To have a retry go on where the failed run stopped, give the watch `resume: { store: Event.memoryCursors() }`.
|
package/docs/README.md
CHANGED
|
@@ -35,7 +35,6 @@ Where things are, in a project that installs the package: these pages in `node_m
|
|
|
35
35
|
| [Authentication.md](Authentication.md) | declare how callers sign in, verify credentials, publish OAuth discovery, step up for a scope |
|
|
36
36
|
| [Authentication-advanced.md](Authentication-advanced.md) | use an API key or a cookie, read the request in a verifier, protect a route of your own, run a request policy before authentication |
|
|
37
37
|
| [Testing.md](Testing.md) | test an implementation in process, and routes, tools and rpcs in memory, as a signed-in caller; test events and webhooks; assert on errors |
|
|
38
|
-
| `@gjermundgaraba/effect-actions-cloudflare`'s `docs/README.md` | run actions and events in Cloudflare Durable Objects as cluster entities, served by a Worker over JSON and MCP: install that package; its pages and example are in its own `node_modules` folder |
|
|
39
38
|
|
|
40
39
|
## Examples
|
|
41
40
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# shopping-carts
|
|
2
2
|
|
|
3
|
-
Shopping carts as Effect Cluster entities: each cart is one entity id of the `Cart` entity, which runs in one place at a time and one call at a time. Shoppers add items to their own cart and list it over JSON routes, which reach the cart the input names through `ActionEntity.reaching`. The server is one process, Effect's `SingleRunner`, keeping the cluster's messages and the carts in one SQLite file.
|
|
3
|
+
Shopping carts as Effect Cluster entities: each cart is one entity id of the `Cart` entity, which runs in one place at a time and one call at a time. Shoppers add items to their own cart and list it over JSON routes, which reach the cart the input names through `ActionEntity.reaching`. The server is one process, Effect's `SingleRunner`, keeping the cluster's messages and the carts in one SQLite file.
|
|
4
4
|
|
|
5
5
|
## Run it
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjermundgaraba/effect-actions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.0",
|
|
4
4
|
"description": "Define Effect action contracts once and project them onto HTTP, MCP, Effect RPC, native Toolkits, and CLIs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai-agents",
|
|
@@ -55,13 +55,13 @@
|
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@clanker-stuff/oxlint-config": "0.5.1",
|
|
58
|
-
"@effect/platform-node": "4.1.
|
|
59
|
-
"@effect/sql-sqlite-node": "4.1.
|
|
58
|
+
"@effect/platform-node": "4.1.6",
|
|
59
|
+
"@effect/sql-sqlite-node": "4.1.6",
|
|
60
60
|
"@effect/tsgo": "0.73.2",
|
|
61
|
-
"@effect/vitest": "4.1.
|
|
61
|
+
"@effect/vitest": "4.1.6",
|
|
62
62
|
"@modelcontextprotocol/client": "2.4.0",
|
|
63
63
|
"@types/node": "26.6.6",
|
|
64
|
-
"effect": "4.1.
|
|
64
|
+
"effect": "4.1.6",
|
|
65
65
|
"tsx": "4.23.15",
|
|
66
66
|
"typescript": "7.0.2",
|
|
67
67
|
"ultracite": "8.3.1",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"test": "vp test",
|
|
89
89
|
"check": "vp check",
|
|
90
90
|
"example": "vpx examples/notes-service/src/server.ts",
|
|
91
|
-
"test:package": "vp run
|
|
91
|
+
"test:package": "vp run build && vpx scripts/test-package.ts",
|
|
92
92
|
"docs:sync": "vpx scripts/sync-skill.ts",
|
|
93
93
|
"ready": "vp check && vp test && vp run test:package"
|
|
94
94
|
}
|