@gjermundgaraba/effect-actions 0.35.0 → 0.36.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/dist/ActionEntity.d.mts +162 -75
- package/dist/ActionEntity.mjs +73 -55
- package/dist/ActionHttp.d.mts +5 -2
- package/dist/ActionRpc.d.mts +65 -13
- package/dist/ActionRpc.mjs +22 -7
- package/dist/Event.d.mts +60 -6
- package/dist/Event.mjs +49 -2
- package/dist/RequestPolicy.d.mts +3 -9
- package/dist/RequestPolicy.mjs +18 -6
- package/dist/contract/feed.mjs +41 -21
- package/dist/contract/serving.mjs +15 -9
- package/dist/contract/wake.d.mts +14 -0
- package/dist/contract/wake.mjs +4 -3
- package/dist/entity/wakes.mjs +32 -22
- package/dist/rpc/erased.mjs +6 -2
- package/dist/rpc/watch.mjs +4 -2
- package/docs/ActionEntity-failures.md +0 -1
- package/docs/ActionEntity.md +17 -9
- package/docs/ActionHttp.md +1 -1
- package/docs/ActionRpc.md +21 -1
- package/docs/Authentication-failures.md +1 -1
- package/docs/Event.md +3 -1
- package/docs/setup.md +1 -1
- package/examples/kitchen-sink/src/browser.ts +9 -21
- package/examples/shopping-carts/README.md +1 -1
- package/examples/shopping-carts/src/entities.ts +4 -2
- package/package.json +1 -1
- package/examples/shopping-carts/src/Rpc.ts +0 -6
package/dist/ActionEntity.d.mts
CHANGED
|
@@ -1,35 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Any as Any$1,
|
|
1
|
+
import { Anyone } from "./contract/caller.mjs";
|
|
2
|
+
import { Any as Any$1, Matching, RemoteRequest, Required, ServedProvider, ServerBuildServices, VerifierError } from "./authentication/provider.mjs";
|
|
3
|
+
import { Any as Any$2, Errors } from "./Action.mjs";
|
|
3
4
|
import { Call } from "./contract/call.mjs";
|
|
4
5
|
import { BuiltIns, Unavailable } from "./contract/errors.mjs";
|
|
5
|
-
import { Any as Any$
|
|
6
|
+
import { Any as Any$3, Received, SubscriptionError } from "./Event.mjs";
|
|
6
7
|
import { EventBuildServices, EventImplementation, EventServedRequest, HoldingEvents, ServingEvents } from "./contract/feed.mjs";
|
|
7
8
|
import { Holding, Implementation, Member, Served } from "./contract/implementation.mjs";
|
|
9
|
+
import { Only } from "./contract/listed.mjs";
|
|
8
10
|
import { Answer } from "./contract/stream.mjs";
|
|
11
|
+
import { Wake } from "./contract/wake.mjs";
|
|
9
12
|
import { WatchArgs } from "./contract/watch.mjs";
|
|
10
|
-
import { Any as Any$
|
|
13
|
+
import { Any as Any$4, Binding } from "./ActionRpc.mjs";
|
|
11
14
|
import { erasedOf } from "./rpc/erased.mjs";
|
|
12
15
|
import { forwardedHeaders } from "./entity/forwarded.mjs";
|
|
13
|
-
import { Duration, Effect, Layer, Scope, Stream } from "effect";
|
|
16
|
+
import { Context, DateTime, Duration, Effect, Layer, Scope, Stream } from "effect";
|
|
14
17
|
import { ClusterError, Entity as Entity$1, Sharding } from "effect/cluster";
|
|
15
18
|
import { HttpServerRequest } from "effect/http";
|
|
16
19
|
import { Rpc, RpcClient, RpcGroup } from "effect/rpc";
|
|
17
|
-
type Contract<B extends Any$
|
|
20
|
+
type Contract<B extends Any$4> = B["actions"][number] | B["events"][number];
|
|
18
21
|
/**
|
|
19
22
|
* The key of the binding `B`'s actions and event types, the id of the entity that serves a call
|
|
20
23
|
* or a subscription, read from its input: one function of every input, `({ room }) => room`.
|
|
21
24
|
*/
|
|
22
|
-
export type Key<B extends Any$
|
|
25
|
+
export type Key<B extends Any$4> = (input: Contract<B>["input"]["Type"]) => string;
|
|
23
26
|
type ErasedRpc = ReturnType<typeof erasedOf> extends RpcGroup.RpcGroup<infer R> ? R : never;
|
|
24
27
|
/**
|
|
25
|
-
* A cluster `Entity` serving
|
|
26
|
-
* type one unary rpc answering a page past a cursor, plus the entity's own
|
|
27
|
-
*
|
|
28
|
-
* and Effect's own `Entity` functions take it, `Entity.client`
|
|
28
|
+
* A cluster `Entity` serving actions and event types, `make`'s contracts: one rpc of JSON per
|
|
29
|
+
* action, and per event type one unary rpc answering a page past a cursor, plus the entity's own
|
|
30
|
+
* native rpcs `N`, the binding `make` built of its contracts and the key of each. `layer` serves
|
|
31
|
+
* it, `client` calls its actions, and Effect's own `Entity` functions take it, `Entity.client`
|
|
32
|
+
* calling its native rpcs.
|
|
29
33
|
*/
|
|
30
|
-
export interface Entity<Type extends string, B extends Any$
|
|
34
|
+
export interface Entity<Type extends string, B extends Any$4, N extends Rpc.Any = never> extends Entity$1.Entity<Type, ErasedRpc | N> {
|
|
35
|
+
/**
|
|
36
|
+
* The RPC binding of the entity's contracts and its `authentication`, as
|
|
37
|
+
* `ActionRpc.make` builds it: what `ActionRpc.client` and `socketClient` call where a socket
|
|
38
|
+
* forwards every contract into the entity.
|
|
39
|
+
*/
|
|
31
40
|
readonly binding: B;
|
|
32
41
|
readonly key: Key<B>;
|
|
42
|
+
/**
|
|
43
|
+
* The handlers of the entity's native rpcs, as `layer`'s `rpcs` takes them, typed from `make`'s
|
|
44
|
+
* `rpcs`: Effect's `Entity.of`, for handlers written apart from `layer`.
|
|
45
|
+
*/
|
|
46
|
+
readonly of: <Handlers extends Entity$1.HandlersFrom<N>>(handlers: Handlers) => Handlers;
|
|
47
|
+
/** Effect's `Entity.annotate`, keeping the entity's binding and key. */
|
|
48
|
+
readonly annotate: <I, S>(key: Context.Key<I, S>, value: S) => Entity<Type, B, N>;
|
|
49
|
+
/** Effect's `Entity.annotateMerge`, keeping the entity's binding and key. */
|
|
50
|
+
readonly annotateMerge: <S>(annotations: Context.Context<S>) => Entity<Type, B, N>;
|
|
51
|
+
/**
|
|
52
|
+
* Effect's `Entity.annotateRpcs`, annotating the entity's native rpcs alone and keeping its
|
|
53
|
+
* binding and key.
|
|
54
|
+
*
|
|
55
|
+
* **Gotchas**
|
|
56
|
+
*
|
|
57
|
+
* - The rpcs of the binding's actions, event types and webhook subscriptions keep their own annotations, volatile: `ClusterSchema.Persisted` here persists the natives, never a caller's credential-bearing envelope.
|
|
58
|
+
*/
|
|
59
|
+
readonly annotateRpcs: <I, S>(key: Context.Key<I, S>, value: S) => Entity<Type, B, N>;
|
|
60
|
+
/**
|
|
61
|
+
* Effect's `Entity.annotateRpcsMerge`, annotating the entity's native rpcs alone, as
|
|
62
|
+
* `annotateRpcs` does, and keeping its binding and key.
|
|
63
|
+
*/
|
|
64
|
+
readonly annotateRpcsMerge: <S>(annotations: Context.Context<S>) => Entity<Type, B, N>;
|
|
33
65
|
}
|
|
34
66
|
/**
|
|
35
67
|
* Any entity `make` returns, as a function accepting every entity reads it: its type, its
|
|
@@ -37,60 +69,70 @@ export interface Entity<Type extends string, B extends Any$3, N extends Rpc.Any
|
|
|
37
69
|
*/
|
|
38
70
|
export interface Any {
|
|
39
71
|
readonly type: string;
|
|
40
|
-
readonly binding: Any$
|
|
72
|
+
readonly binding: Any$4;
|
|
41
73
|
readonly client: Effect.Effect<(entityId: string) => object, never, Sharding.Sharding>;
|
|
42
74
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
export
|
|
75
|
+
/**
|
|
76
|
+
* The binding `make` builds from an entity's contracts and its `authentication`, as
|
|
77
|
+
* `ActionRpc.make` builds one: the entity's `binding`.
|
|
78
|
+
*/
|
|
79
|
+
export type BindingOf<Contracts extends ReadonlyArray<Any$2 | Any$3>, D extends Any$1 | undefined = undefined> = Binding<Only<Contracts, Any$2>, [], D, Only<Contracts, Any$3>>;
|
|
80
|
+
/** `make`'s options: `authentication` required where a contract is protected, and of its identity. */
|
|
81
|
+
export type MakeOptions<Contracts extends ReadonlyArray<Any$2 | Any$3>, D extends Any$1 | undefined = undefined, N extends Rpc.Any = never> = {
|
|
48
82
|
/**
|
|
49
|
-
* The entity id an input names: one function every
|
|
50
|
-
*
|
|
83
|
+
* The entity id an input names: one function every input of the contracts reads,
|
|
84
|
+
* `({ room }) => room`.
|
|
51
85
|
*
|
|
52
86
|
* **Gotchas**
|
|
53
87
|
*
|
|
54
|
-
* - It takes the union of the
|
|
88
|
+
* - It takes the union of the contracts' inputs, so it reads only a field every one declares: a field one input lacks is a type error.
|
|
55
89
|
*/
|
|
56
|
-
readonly key:
|
|
90
|
+
readonly key: (input: Contracts[number]["input"]["Type"]) => string;
|
|
91
|
+
/**
|
|
92
|
+
* `Authentication.make(...)` of the protected contracts' identity, which authenticates each call
|
|
93
|
+
* from the headers its client sent: required where a contract is protected, and of its identity.
|
|
94
|
+
*/
|
|
95
|
+
readonly authentication?: D;
|
|
57
96
|
/**
|
|
58
|
-
* The entity's own native rpcs, beside its
|
|
59
|
-
* `RpcGroup.make(Expire)`, such as a
|
|
60
|
-
* `
|
|
97
|
+
* The entity's own native rpcs, beside its contracts, with their own schemas:
|
|
98
|
+
* `RpcGroup.make(Expire)`, such as a tell another entity sends it; an entity waking itself later
|
|
99
|
+
* calls `wakeAt` instead, and a call another part of the application makes is an action the
|
|
100
|
+
* entity lists and no surface's binding does. `layer` takes their handlers in its own `rpcs`.
|
|
61
101
|
*
|
|
62
102
|
* **Gotchas**
|
|
63
103
|
*
|
|
64
|
-
* - A native rpc authenticates nothing, whatever
|
|
104
|
+
* - A native rpc authenticates nothing, whatever `authentication` says: it is for tells the entity and other entities send with `Entity.client`, never for a surface's callers.
|
|
65
105
|
* - A native rpc named after an action, an event type, an event's `<event>/poll`, a protected event's `<event>/subscribe` or `<event>/unsubscribe`, or the entity's wake, `effect-actions/wake`, throws as the entity is made.
|
|
66
106
|
*/
|
|
67
107
|
readonly rpcs?: RpcGroup.RpcGroup<N> | undefined;
|
|
68
|
-
}
|
|
108
|
+
} & Required<Contracts[number]> & Matching<Contracts[number], D>;
|
|
69
109
|
/**
|
|
70
|
-
* The cluster entity of type `type` serving
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* `{ input, cursor, maxEvents, watch }` and answering a page,
|
|
110
|
+
* The cluster entity of type `type` serving `contracts`, actions and event types, each call and
|
|
111
|
+
* subscription in the entity its contract's `key` names for its input. Each action is an rpc named
|
|
112
|
+
* after it, as `ActionRpc.make` binds it, and each event type a unary rpc named `<event>/poll`,
|
|
113
|
+
* `ActionRpc.pollTag`, taking `{ input, cursor, maxEvents, watch }` and answering a page,
|
|
74
114
|
* `{ events, cursor, truncated, hasMore, nextPollMs, notified }`, an HTTP poll's page and
|
|
75
115
|
* `notified`, `ActionRpc.Page`, as `ActionRpc.layer` serves it too, its subscription hook held
|
|
76
116
|
* for the caller's principal. A protected event type has two more, `<event>/subscribe` and
|
|
77
117
|
* `<event>/unsubscribe`, taking a webhook subscription and its cancellation, and the entity has
|
|
78
|
-
* its wake, `effect-actions/wake`,
|
|
79
|
-
* retry wait. Guide: `docs/ActionEntity.md` in this package.
|
|
118
|
+
* its wake, `effect-actions/wake`, the persisted tell `wakeAt` sends it, which a webhook
|
|
119
|
+
* delivery's retry wait shares. Guide: `docs/ActionEntity.md` in this package.
|
|
80
120
|
*
|
|
81
121
|
* **Gotchas**
|
|
82
122
|
*
|
|
83
123
|
* - The rpcs of its actions, event types and webhook subscriptions are volatile, as `ClusterSchema.Persisted` defaults, and its wake alone is persisted: annotated persisted, each envelope, its caller's headers and credential included, would be stored and replayed with them, on Effect's SQL runners until a `MessageRetention` layer deletes it, and on the Cloudflare runtime, which never prunes `cluster_messages`, for good, its credential headers redacted. `rpcs`' natives are persisted as their own annotations say.
|
|
84
|
-
* - A binding with native rpcs throws: give the entity's own rpcs to `make`'s `rpcs`.
|
|
85
124
|
* - A streaming action, one declaring `progress` or answering `Action.Stream`, is a stream rpc, its frames those `ActionRpc.make` states, `client`'s method handing each progress value to `onProgress` before it answers, and returning a `Stream` of the elements of an `Action.Stream` answer; it holds a slot of the entity's `concurrency`, by default its only one, until its stream ends, but on the Cloudflare runtime, which frees the slot while a frame waits for its client's acknowledgement.
|
|
86
|
-
* - `annotate` and its kin return a plain `Entity`, without `binding` and `key`, which `layer` and `client` do not take.
|
|
87
125
|
*/
|
|
88
|
-
export declare
|
|
126
|
+
export declare function make<const Type extends string, const Contracts extends ReadonlyArray<(Any$2 | Any$3) & {
|
|
127
|
+
readonly caller: typeof Anyone;
|
|
128
|
+
}>, N extends Rpc.Any = never>(type: Type, contracts: Contracts, options: MakeOptions<Contracts, undefined, N>): Entity<Type, BindingOf<Contracts>, N>;
|
|
129
|
+
export declare function make<const Type extends string, const Contracts extends ReadonlyArray<Any$2 | Any$3>, D extends Any$1, N extends Rpc.Any = never>(type: Type, contracts: Contracts, options: MakeOptions<Contracts, D, N>): Entity<Type, BindingOf<Contracts, D>, N>;
|
|
89
130
|
/**
|
|
90
131
|
* The rpcs of `entity`'s binding, as its `protocol` holds them without its native rpcs: one per
|
|
91
|
-
* action, and per event type its poll.
|
|
92
|
-
* cluster
|
|
93
|
-
* `ActionRpc.client`'s watch polls, finding
|
|
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 the Cloudflare
|
|
134
|
+
* package's socket host forwards its `actions`, which `ActionRpc.client`'s watch polls, finding
|
|
135
|
+
* no stream rpc.
|
|
94
136
|
*/
|
|
95
137
|
export declare const bindingRpcs: (entity: Any) => RpcGroup.RpcGroup<ErasedRpc>;
|
|
96
138
|
/**
|
|
@@ -98,7 +140,7 @@ export declare const bindingRpcs: (entity: Any) => RpcGroup.RpcGroup<ErasedRpc>;
|
|
|
98
140
|
* `disableFatalDefects`, which is always on, and `defectRetryPolicy`, which the Cloudflare
|
|
99
141
|
* runtime would follow to run a call that died again, and `services` and `rpcs`.
|
|
100
142
|
*/
|
|
101
|
-
export interface LayerOptions<ROut = never, RIn = never, Handlers = never, RX = never> extends Omit<NonNullable<Parameters<Entity$1.Any["toLayer"]>[1]>, "disableFatalDefects" | "defectRetryPolicy"> {
|
|
143
|
+
export interface LayerOptions<ROut = never, RIn = never, Handlers = never, RX = never, RW = never> extends Omit<NonNullable<Parameters<Entity$1.Any["toLayer"]>[1]>, "disableFatalDefects" | "defectRetryPolicy"> {
|
|
102
144
|
/**
|
|
103
145
|
* What each entity id builds for itself as it activates, before its implementations'
|
|
104
146
|
* builders and its native rpcs' handlers, which read it, as its handlers do per call: a store
|
|
@@ -113,13 +155,14 @@ export interface LayerOptions<ROut = never, RIn = never, Handlers = never, RX =
|
|
|
113
155
|
* - A `Webhook.layer` here holds the entity's webhook subscriptions and delivers them from its activation: give it `persist`, so a later activation restores them; each retry wait is a wake the entity sends itself, so a stopped entity is built again to retry, on Effect's SQL runners up to `entityMessagePollInterval` after it is due, each wake a row of the runtime's messages, kept once processed.
|
|
114
156
|
* - A webhook subscription to a check source not `notified` is refused with `Event.SubscriptionError`, reason `Unsupported`, and a warning saying why, its polls and unsubscribes still served: its reads on a timer would be cut by Effect's reaper and keep a Durable Object awake.
|
|
115
157
|
* - A webhook subscription past its expiry ends at its next delivery, and its kept record at the next activation, on no timer: until then it holds its subscription hook.
|
|
116
|
-
* - A `Webhook.layer` here with `persist` needs the
|
|
158
|
+
* - A `Webhook.layer` here with `persist` needs the entity's authentication to name a `schema` where it serves a protected event whose source is no check source left un-`notified`: without one each activation dies with the library's message naming `schema`, so on Effect's runners each call to the entity waits while the runner builds it again, and on the Cloudflare runtime each fails at once.
|
|
117
159
|
*/
|
|
118
160
|
readonly services?: Layer.Layer<ROut, unknown, RIn> | undefined;
|
|
119
161
|
/**
|
|
120
162
|
* The handlers of `make`'s `rpcs`, as `Entity.toLayer` takes them: an `Entity.HandlersFrom` of
|
|
121
163
|
* those rpcs, or an Effect building one as each entity id activates, after `services`, whose
|
|
122
|
-
* output it and the handlers' answers read
|
|
164
|
+
* output it and the handlers' answers read, either typed from those rpcs where written here, or
|
|
165
|
+
* by the entity's `of` where written apart. Each handler receives Effect's own `Entity.Request`,
|
|
123
166
|
* its `payload`, `headers` and `requestId`, and may answer through `Rpc.fork` or
|
|
124
167
|
* `Rpc.uninterruptible`.
|
|
125
168
|
*
|
|
@@ -128,23 +171,74 @@ export interface LayerOptions<ROut = never, RIn = never, Handlers = never, RX =
|
|
|
128
171
|
* - Required exactly where `make` was given `rpcs`: left out there, or given for an entity without them, it is a type error.
|
|
129
172
|
*/
|
|
130
173
|
readonly rpcs?: ([Handlers] extends [never] ? never : Handlers | Effect.Effect<Handlers, never, RX>) | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* What the entity does when its wake fires, given the time it was due: `wakeAt` schedules it,
|
|
176
|
+
* and it runs in whichever activation is current then, one built again for it where the entity
|
|
177
|
+
* had stopped, reading `services`' output as a handler does.
|
|
178
|
+
*
|
|
179
|
+
* **Gotchas**
|
|
180
|
+
*
|
|
181
|
+
* - It is a check, not one reminder per `wakeAt`: it runs at the earliest time asked for, early for the later ones, which it asks for again, and may run again for a wake already handled, as a replay after a restart: do what is due by then and `wakeAt` the next time.
|
|
182
|
+
* - The entity's webhook retry waits share its wake, so with a `Webhook.layer` in `services` it also runs when a delivery's retry is due.
|
|
183
|
+
* - It runs beside the entity's calls, outside its `concurrency`, as a webhook delivery holding the entity's only slot waits on the wake, and two deliveries, such as a replay and a fresh wake, run it at once: a read, change and write of state it shares with a handler or with itself needs its own lock or transaction.
|
|
184
|
+
* - Its failure or defect is reported to the `ErrorReporter`s the activation provides and logged, and that wake is done; interrupted as the entity stops, it runs again in the next activation.
|
|
185
|
+
* - On the Cloudflare runtime, a call's defect that rebuilds the activation in place while it runs closes the services it reads beneath it, and that wake counts as handled, as `@effect/platform-cloudflare` 4.1.4 finalizes an activation without waiting for its tells: keep it a check the next `wakeAt` repeats, not the only record of work.
|
|
186
|
+
*/
|
|
187
|
+
readonly onWake?: ((at: DateTime.Utc) => Effect.Effect<void, unknown, RW>) | undefined;
|
|
131
188
|
}
|
|
132
|
-
type NativeLayerOptions<N extends Rpc.Any, ROut, RIn, Handlers, RX> = [N] extends [never] ? [options?: LayerOptions<ROut, RIn>] : [options: LayerOptions<ROut, RIn, Handlers, RX> & {
|
|
189
|
+
type NativeLayerOptions<N extends Rpc.Any, ROut, RIn, Handlers, RX, RW> = [N] extends [never] ? [options?: LayerOptions<ROut, RIn, never, never, RW>] : [options: LayerOptions<ROut, RIn, Handlers, RX, RW> & {
|
|
133
190
|
readonly rpcs: Handlers | Effect.Effect<Handlers, never, RX>;
|
|
134
191
|
}];
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
192
|
+
/**
|
|
193
|
+
* What an entity's activation provides to `layer`'s `services`, its implementations' builders and
|
|
194
|
+
* the Effect building its native rpcs' handlers, without requiring it of the layer: the entity's
|
|
195
|
+
* `Entity.CurrentAddress`, its runner's `Entity.CurrentRunnerAddress`, the activation's
|
|
196
|
+
* `Scope`, closed as the entity stops, and the entity's wake, which `wakeAt` reads, as its
|
|
197
|
+
* handlers and `onWake` do too. A layer an application builds for `services` apart from `layer`
|
|
198
|
+
* reads it as `Layer.Layer<ROut, E, ActionEntity.Activation | …>`.
|
|
199
|
+
*
|
|
200
|
+
* **Gotchas**
|
|
201
|
+
*
|
|
202
|
+
* - A handler's `Scope`, a native one's or an action's, is its call's, closed as the call ends: acquire what lives as long as the activation in `services` or a builder.
|
|
203
|
+
*/
|
|
204
|
+
export type Activation = Entity$1.CurrentAddress | Entity$1.CurrentRunnerAddress | Scope.Scope | Wake;
|
|
205
|
+
/**
|
|
206
|
+
* Wake the entity at `at`: a persisted tell it sends itself, delivered then to `layer`'s
|
|
207
|
+
* `onWake`, through eviction, a restart and a deploy, in whichever activation is current, built
|
|
208
|
+
* again for it where the entity had stopped. It succeeds once the runtime stored the tell, and
|
|
209
|
+
* fails with `Action.Unavailable` where it could not, so a caller knows the wake is scheduled
|
|
210
|
+
* before it starts what the wake guards. Read it in the entity: `services`, a builder or a handler.
|
|
211
|
+
*
|
|
212
|
+
* **Gotchas**
|
|
213
|
+
*
|
|
214
|
+
* - The wake keeps the earliest time: a time no earlier than one this activation scheduled and has not seen fire sends nothing, as that wake's `onWake` asks for it again; a tell cannot be taken back, so a time moved later or dropped still fires.
|
|
215
|
+
* - Each wake is a row of the runtime's messages, kept once processed, on Effect's SQL runners until a `MessageRetention` layer deletes it, and on the Cloudflare runtime for good.
|
|
216
|
+
* - It holds nothing awake: work already running keeps its activation with `Entity.keepAlive` and its Durable Object event with `waitUntil`, beside the wake that resumes it later.
|
|
217
|
+
* - Asked for inside a transaction of the entity's or the runner's `SqlClient`, it dies, as the wake would be stored in that transaction and dropped by its rollback while the activation counted it pending: call it after the transaction commits.
|
|
218
|
+
* - A time between milliseconds is rounded up to the next one, as the runtime stores whole milliseconds.
|
|
219
|
+
*/
|
|
220
|
+
export declare const wakeAt: (at: DateTime.DateTime) => Effect.Effect<void, Unavailable, Wake>;
|
|
221
|
+
type PerCall<B extends Any$4, Apps extends Served, ROut, A extends Any$2 = B["actions"][number], E extends Any$3 = B["events"][number]> = Exclude<RemoteRequest<Member<Apps>, A> | EventServedRequest<Member<Apps>, E>, Activation | HttpServerRequest.HttpServerRequest | HttpServerRequest.ParsedSearchParams | ROut>;
|
|
222
|
+
type NoPerCall<B extends Any$4, Apps extends Served, ROut> = [PerCall<B, Apps, ROut>] extends [never] ? unknown : {
|
|
138
223
|
readonly "An entity provides no service per call but services' output: yield it in the implementation's builder, where it is a startup service, or build it in layer's services": PerCall<B, Apps, ROut>;
|
|
139
224
|
};
|
|
140
|
-
type EntityLayer<B extends Any$
|
|
225
|
+
type EntityLayer<B extends Any$4, Apps extends Served, N extends Rpc.Any, ROut, RIn, Handlers, RX, RW, A extends Any$2 = B["actions"][number], E extends Any$3 = B["events"][number]> = Layer.Layer<never, never, Exclude<ServerBuildServices<Holding<Member<Apps>, A>, A> | EventBuildServices<HoldingEvents<Member<Apps>, E>, E> | ServedProvider<Member<Apps>, A, B extends {
|
|
141
226
|
readonly authentication: infer D;
|
|
142
|
-
} ? D : never, ServingEvents<Member<Apps>, E>> | RX | RpcGroup.HandlersServices<N, Handlers>, Activation | ROut> | Exclude<RIn, Activation> | Rpc.ServicesClient<N> | Rpc.ServicesServer<N> | Rpc.Middleware<N> | Sharding.Sharding>;
|
|
227
|
+
} ? D : never, ServingEvents<Member<Apps>, E>> | RX | RW | RpcGroup.HandlersServices<N, Handlers>, Activation | ROut> | Exclude<RIn, Activation> | Rpc.ServicesClient<N> | Rpc.ServicesServer<N> | Rpc.Middleware<N> | Sharding.Sharding>;
|
|
143
228
|
/**
|
|
144
|
-
*
|
|
229
|
+
* Whether a failure is one of those Effect's entity client fails with when the entity could not
|
|
230
|
+
* be reached, `MailboxFull`, `PersistenceError`, `AlreadyProcessingMessage` and
|
|
231
|
+
* `EntityNotAssignedToRunner`, which `client`, `nativeClient` and `reaching` answer as
|
|
232
|
+
* `Action.Unavailable`: a server forwarding calls into an entity, as the Cloudflare package's
|
|
233
|
+
* socket host does, makes them a defect, and a Worker calling Effect's `Entity.client` itself
|
|
234
|
+
* answers them, as the Cloudflare package's `ActionCluster.failureResponse` does, with a 503.
|
|
235
|
+
*/
|
|
236
|
+
export declare const isClusterFailure: <I>(input: I) => input is I & (ClusterError.AlreadyProcessingMessage | ClusterError.EntityNotAssignedToRunner | ClusterError.MailboxFull | ClusterError.PersistenceError);
|
|
237
|
+
/**
|
|
238
|
+
* Register `entity` with the cluster's `Sharding`, serving its actions and event types
|
|
145
239
|
* from `implementations`, which implement every one. Each entity id builds the implementations
|
|
146
240
|
* itself as it activates, so a builder may read `Entity.CurrentAddress`; each call of a
|
|
147
|
-
* protected one authenticates from the headers its client sent with the
|
|
241
|
+
* protected one authenticates from the headers its client sent with the entity's descriptor, as
|
|
148
242
|
* `ActionRpc.layer`'s does; then the input decodes, failing `InvalidInput` where it does not, an
|
|
149
243
|
* input whose key names another entity or none is refused with `Forbidden`, and the
|
|
150
244
|
* implementation's `authorize` and the handler run; a declared error is the call's typed failure.
|
|
@@ -157,32 +251,23 @@ type EntityLayer<B extends Any$3, Apps extends Served, N extends Rpc.Any, ROut,
|
|
|
157
251
|
* - `Action.layer` above it shares no build with it: each entity id runs its implementations' builders again, beside the run `Action.layer` made for the other surfaces.
|
|
158
252
|
* - A builder that fails or dies is a defect of the entity's activation, never a failure of the layer: Effect's own runners log it and build again, 500 ms later and growing to 10 seconds apart, while its calls wait, and the Cloudflare runtime fails the calls waiting on that build, the next call building again.
|
|
159
253
|
* - A handler's defect or undeclared failure ends that call with an `Error` whose message is `Internal server error`, as on `ActionRpc.layer`, and the call is not run again; Effect's own runners keep the entity's handlers for its next call, and the Cloudflare runtime builds them again, so what its builders and `services` held in memory is gone.
|
|
160
|
-
* - An implementation missing for one of the
|
|
161
|
-
*/
|
|
162
|
-
export declare function layer<const Type extends string, const B extends Any$3, N extends Rpc.Any, const Apps extends Served, ROut = never, RIn = never, Handlers extends Entity$1.HandlersFrom<N> = never, RX = never>(entity: Entity<Type, B, N>, implementations: Apps & NoPerCall<B, Apps, ROut>, ...options: NativeLayerOptions<N, ROut, RIn, Handlers, RX>): EntityLayer<B, Apps, N, ROut, RIn, Handlers, RX>;
|
|
163
|
-
/**
|
|
164
|
-
* Whether a failure is one of those Effect's entity client fails with when the entity could not
|
|
165
|
-
* be reached, `MailboxFull`, `PersistenceError`, `AlreadyProcessingMessage` and
|
|
166
|
-
* `EntityNotAssignedToRunner`, which `client`, `nativeClient` and `reaching` answer as
|
|
167
|
-
* `Action.Unavailable`: a server forwarding calls into an entity, as the Cloudflare package's
|
|
168
|
-
* socket host does, makes them a defect, and a Worker calling Effect's `Entity.client` itself
|
|
169
|
-
* answers them, as the Cloudflare package's `ActionCluster.failureResponse` does, with a 503.
|
|
254
|
+
* - An implementation missing for one of the entity's actions or event types throws as the layer is made: an entity serves every contract it was made of.
|
|
170
255
|
*/
|
|
171
|
-
export declare const
|
|
256
|
+
export declare function layer<const Type extends string, const B extends Any$4, N extends Rpc.Any, const Apps extends Served, Handlers extends Entity$1.HandlersFrom<N>, ROut = never, RIn = never, RX = never, RW = never>(entity: Entity<Type, B, N>, implementations: Apps & NoPerCall<B, Apps, ROut>, ...options: NativeLayerOptions<N, ROut, RIn, Handlers, RX, RW>): EntityLayer<B, Apps, N, ROut, RIn, Handlers, RX, RW>;
|
|
172
257
|
/**
|
|
173
|
-
* What one call of an action `A` fails with, beside
|
|
258
|
+
* What one call of an action `A` fails with, beside its verifier's errors `E`: its declared and
|
|
174
259
|
* built-in errors, `Action.Unavailable` among them where the entity could not be reached or did
|
|
175
260
|
* not answer in time. An answer that does not decode is a defect.
|
|
176
261
|
*/
|
|
177
|
-
export type MethodError<A extends Any$
|
|
178
|
-
type Method<A extends Any$
|
|
262
|
+
export type MethodError<A extends Any$2, E extends Errors[number] = never> = A["error"][number]["Type"] | E["Type"] | BuiltIns["Type"];
|
|
263
|
+
type Method<A extends Any$2, E extends Errors[number]> = Call<A, Answer<A["success"], MethodError<A, E>, never>>;
|
|
179
264
|
/**
|
|
180
|
-
* What a watch of `W` over the binding `B` may fail with: a refusal, `InvalidInput`,
|
|
181
|
-
*
|
|
265
|
+
* What a watch of `W` over the binding `B` may fail with: a refusal, `InvalidInput`, what its
|
|
266
|
+
* descriptor's verifier declares, or an `Event.SubscriptionError` (`NotServed` for an event the entity does not
|
|
182
267
|
* serve).
|
|
183
268
|
*/
|
|
184
|
-
export type WatchError<W extends Any$
|
|
185
|
-
type Watching<B extends Any$
|
|
269
|
+
export type WatchError<W extends Any$3, B extends Any$4> = SubscriptionError | BuiltIns["Type"] | VerifierError<W, B["authentication"]>["Type"];
|
|
270
|
+
type Watching<B extends Any$4> = [B["events"][number]] extends [never] ? unknown : {
|
|
186
271
|
readonly watch: <W extends B["events"][number], SE = never>(event: W, ...args: WatchArgs<W, SE, {
|
|
187
272
|
readonly mode?: "poll" | undefined;
|
|
188
273
|
}>) => Stream.Stream<Received<W>, WatchError<W, B> | SE>;
|
|
@@ -192,7 +277,7 @@ type Watching<B extends Any$3> = [B["events"][number]] extends [never] ? unknown
|
|
|
192
277
|
* with what its descriptor's verifier declares. Where `B` lists event types, `watch(event, input,
|
|
193
278
|
* options)` follows one by polling.
|
|
194
279
|
*/
|
|
195
|
-
export type Client<B extends Any$
|
|
280
|
+
export type Client<B extends Any$4> = { readonly [A in B["actions"][number] as A["name"]]: Method<A, VerifierError<A, B["authentication"]>>; } & Watching<B>;
|
|
196
281
|
/** `client`'s, `nativeClient`'s and `reaching`'s options. */
|
|
197
282
|
export interface EntityCallOptions {
|
|
198
283
|
/**
|
|
@@ -226,7 +311,7 @@ export interface EntityCallOptions {
|
|
|
226
311
|
* - A watch polls, its only `mode` `"poll"`, at the page's `nextPollMs`, each poll bounded by `timeout`, its polls holding the subscription hook of the caller's principal in the entity until its source's `lease` lapses after the last, whichever watch sent it.
|
|
227
312
|
* - A watch asks the entity again after `Action.Unavailable`, from where it reached, a second later and doubling to 30 seconds; another declared error, a refusal included, ends it.
|
|
228
313
|
*/
|
|
229
|
-
export declare const client: <const Type extends string, const B extends Any$
|
|
314
|
+
export declare const client: <const Type extends string, const B extends Any$4, N extends Rpc.Any = never>(entity: Entity<Type, B, N>, options?: EntityCallOptions) => Effect.Effect<Client<B>, never, Sharding.Sharding>;
|
|
230
315
|
/**
|
|
231
316
|
* Effect's own client of an entity's native rpcs `N`, `make`'s `rpcs`, its methods keyed by rpc
|
|
232
317
|
* tag, each failing with its rpc's errors and `Action.Unavailable` in place of what Effect's
|
|
@@ -246,11 +331,12 @@ export type NativeClient<N extends Rpc.Any> = RpcClient.RpcClient.From<N, Unavai
|
|
|
246
331
|
* - A stream rpc's call has no deadline, and a cluster failure ends its stream with `Unavailable`; a call with `asQueue: true` makes its queue from that stream, buffering `streamBufferSize` elements, 16 by default, so a take fails with `Unavailable` too.
|
|
247
332
|
* - Effect's own `entity.client` still fails with the cluster's errors, and waits without end on an entity no runner is assigned: call it only where that is wanted.
|
|
248
333
|
*/
|
|
249
|
-
export declare function nativeClient<const Type extends string, const B extends Any$
|
|
250
|
-
type Nothing<C extends Any$
|
|
334
|
+
export declare function nativeClient<const Type extends string, const B extends Any$4, N extends Rpc.Any>(entity: Entity<Type, B, N>, options?: EntityCallOptions): Effect.Effect<(entityId: string) => NativeClient<N>, never, Sharding.Sharding>;
|
|
335
|
+
type Nothing<C extends Any$2 | Any$3> = { readonly [K in C["name"]]: never; };
|
|
251
336
|
/**
|
|
252
337
|
* The implementations of `entity`'s actions and event types that any surface serves beside its
|
|
253
|
-
* own, `ActionHttp.layer`, `ActionRpc.layer` or `ActionMcp.layerHttp
|
|
338
|
+
* own, `ActionHttp.layer`, `ActionRpc.layer` or `ActionMcp.layerHttp`, one for every action the
|
|
339
|
+
* entity lists, those no surface's binding lists included: each call sent through
|
|
254
340
|
* `Entity.client` to the entity its contract's key names for its input, with the caller's
|
|
255
341
|
* headers, and its answer, declared errors and progress passed back; each event followed through
|
|
256
342
|
* the entity's poll rpc. A call the entity could not be reached for, or that did not answer
|
|
@@ -259,6 +345,7 @@ type Nothing<C extends Any$1 | Any$2> = { readonly [K in C["name"]]: never; };
|
|
|
259
345
|
*
|
|
260
346
|
* **Gotchas**
|
|
261
347
|
*
|
|
348
|
+
* - A surface that serves every implementation it is given serves the entity's internal actions to its callers too: `ActionMcp.layerHttp` without `actions`, and `ActionRpc.layer` or `ActionHttp.layer` on `entity.binding`, so give MCP its `actions` and the others a binding of the public contracts.
|
|
262
349
|
* - The caller's credential is verified by the surface serving these and again by the entity reached, so both are given the verifier: a call the surface refuses reaches no entity.
|
|
263
350
|
* - A refusal by the entity, a credential expired since the surface verified it included, ends a stream after it answered 200, where the surface's own refusal would have answered its 401 or 403.
|
|
264
351
|
* - The caller's request's headers are sent on as `forwardedHeaders` keeps them: its connection's, its body's, what it accepts and an MCP request's routing headers are not.
|
|
@@ -271,7 +358,7 @@ type Nothing<C extends Any$1 | Any$2> = { readonly [K in C["name"]]: never; };
|
|
|
271
358
|
* - HTTP's `text/event-stream` whose polling ended on a failure other than a refusal sends `Failed` and opens it again 5 seconds later, from where it reached; MCP's `events/stream` does what `ActionMcp.layerHttp` says of an upstream's failure, a `-32603` before `active`.
|
|
272
359
|
* - A stream's polls, and a poll, MCP's `events/poll` included, hold the subscriber's subscription hook in the entity until its source's `lease` lapses after the last, as on any surface.
|
|
273
360
|
* - A protected event's webhook subscription is held by the entity its input's key names, the subscribe and unsubscribe sent there with the caller's headers, and delivered by the `Webhook.layer` in that entity's `services`, never the serving surface's: an entity without one fails the subscribe with `Event.SubscriptionError`, reason `Unsupported`, a 409 over HTTP, before any callback is sent.
|
|
274
|
-
* - An error the entity declares beyond the action's own and the built-in ones, its
|
|
361
|
+
* - An error the entity declares beyond the action's own and the built-in ones, its verifier's, is undeclared where the serving surface's binding does not declare it too.
|
|
275
362
|
*/
|
|
276
|
-
export declare const reaching: <const Type extends string, const B extends Any$
|
|
363
|
+
export declare const reaching: <const Type extends string, const B extends Any$4, N extends Rpc.Any = never>(entity: Entity<Type, B, N>, options?: EntityCallOptions) => ReadonlyArray<Implementation<B["actions"][number], Nothing<B["actions"][number]>, never, Sharding.Sharding> | EventImplementation<B["events"][number], Nothing<B["events"][number]>, never, Sharding.Sharding>>;
|
|
277
364
|
export { forwardedHeaders };
|