@gjermundgaraba/effect-actions 0.31.0 → 0.32.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 +4 -3
- package/dist/Action.d.mts +6 -6
- package/dist/Action.mjs +3 -4
- package/dist/ActionEntity.d.mts +237 -0
- package/dist/ActionEntity.mjs +211 -0
- package/dist/ActionHttp.d.mts +4 -5
- package/dist/ActionHttp.mjs +12 -12
- package/dist/ActionMcp.d.mts +2 -3
- package/dist/ActionMcp.mjs +1 -1
- package/dist/ActionRpc.d.mts +25 -58
- package/dist/ActionRpc.mjs +34 -249
- package/dist/Authentication.d.mts +8 -9
- package/dist/Authentication.mjs +6 -6
- package/dist/Event.d.mts +26 -6
- package/dist/Event.mjs +2 -2
- package/dist/Stored.d.mts +49 -0
- package/dist/Stored.mjs +78 -0
- package/dist/Webhook.d.mts +5 -4
- package/dist/Webhook.mjs +11 -4
- package/dist/authentication/provider.d.mts +2 -2
- package/dist/contract/call.mjs +6 -2
- package/dist/contract/caller.d.mts +2 -6
- package/dist/contract/caller.mjs +3 -3
- package/dist/contract/event-tables.mjs +90 -0
- package/dist/contract/feed.d.mts +15 -3
- package/dist/contract/feed.mjs +20 -11
- package/dist/contract/hub.d.mts +4 -57
- package/dist/contract/hub.mjs +12 -15
- package/dist/contract/idempotency.d.mts +6 -18
- package/dist/contract/idempotency.mjs +6 -14
- package/dist/contract/implementation.mjs +3 -4
- package/dist/contract/log.d.mts +0 -26
- package/dist/contract/log.mjs +16 -8
- package/dist/contract/pages.d.mts +30 -2
- package/dist/contract/pages.mjs +27 -3
- package/dist/contract/wake.mjs +9 -0
- package/dist/contract/watch.mjs +20 -15
- package/dist/contract/webhooks.d.mts +8 -4
- package/dist/contract/webhooks.mjs +42 -31
- package/dist/entity/forwarded.d.mts +11 -0
- package/dist/entity/forwarded.mjs +32 -0
- package/dist/entity/wakes.mjs +39 -0
- package/dist/http/client.mjs +2 -2
- package/dist/http/events.d.mts +1 -12
- package/dist/http/events.mjs +2 -24
- package/dist/http/redacted.mjs +9 -0
- package/dist/mcp/bridge.mjs +5 -6
- package/dist/mcp/client.mjs +2 -2
- package/dist/rpc/erased.d.mts +62 -0
- package/dist/rpc/erased.mjs +356 -0
- package/dist/rpc/watch.mjs +10 -9
- package/dist/webhook/stored.mjs +6 -4
- package/dist/webhook/subscriptions.mjs +58 -40
- package/docs/Action-streams.md +2 -2
- package/docs/Action.md +3 -3
- package/docs/ActionEntity-failures.md +10 -0
- package/docs/ActionEntity.md +165 -0
- package/docs/ActionHttp.md +0 -4
- package/docs/ActionMcp.md +2 -2
- package/docs/ActionRpc-events.md +2 -2
- package/docs/ActionRpc.md +1 -1
- package/docs/Authentication-failures.md +1 -0
- package/docs/Authentication.md +4 -2
- package/docs/Event.md +5 -5
- package/docs/README.md +5 -2
- package/docs/Webhook.md +1 -1
- package/docs/setup-failures.md +1 -1
- package/docs/setup.md +6 -5
- package/examples/README.md +1 -1
- package/examples/kitchen-sink/src/admin/Admin.ts +1 -0
- package/examples/kitchen-sink/src/identity/Identity.ts +5 -1
- package/examples/live-rpc/src/identity/Identity.ts +3 -1
- package/examples/notes-service/src/identity/Identity.ts +3 -1
- package/examples/shopping-carts/README.md +46 -0
- package/examples/shopping-carts/src/Api.ts +9 -0
- package/examples/shopping-carts/src/Rpc.ts +6 -0
- package/examples/shopping-carts/src/cart/Cart.ts +47 -0
- package/examples/shopping-carts/src/cart/handlers.ts +42 -0
- package/examples/shopping-carts/src/cart/held.ts +5 -0
- package/examples/shopping-carts/src/cart/store.ts +10 -0
- package/examples/shopping-carts/src/entities.ts +7 -0
- package/examples/shopping-carts/src/identity/Identity.ts +16 -0
- package/examples/shopping-carts/src/identity/shoppers.ts +6 -0
- package/examples/shopping-carts/src/identity/verifier.ts +21 -0
- package/examples/shopping-carts/src/platform/cluster.ts +7 -0
- package/examples/shopping-carts/src/platform/sql-carts.ts +30 -0
- package/examples/shopping-carts/src/routes.ts +23 -0
- package/examples/shopping-carts/src/server.ts +23 -0
- package/examples/shopping-carts/test/routes.test.ts +69 -0
- package/package.json +9 -6
- package/dist/contract/input-check.d.mts +0 -15
- package/dist/contract/input-check.mjs +0 -15
- package/dist/http/placed.d.mts +0 -34
- package/dist/http/placed.mjs +0 -71
package/dist/contract/watch.mjs
CHANGED
|
@@ -12,8 +12,21 @@ const maxReconnectDelay = Duration.seconds(30);
|
|
|
12
12
|
const afterFailures = (transient) => Schedule.min([Schedule.exponential(reconnectDelay), Schedule.spaced(maxReconnectDelay)]).pipe(Schedule.setInputType(), Schedule.while(({ input }) => transient(input)), Schedule.tap(({ input, duration }) => Effect.logWarning(`A watch reconnects in ${Duration.format(duration)} after a failure`, input)));
|
|
13
13
|
const isSubscriptionError = Schema.is(SubscriptionError);
|
|
14
14
|
const pushRefused = Predicate.compose(isSubscriptionError, ({ reason }) => Predicate.isTagged(reason, "Unsupported") && reason.feature === "deliveryMode" && reason.value === "push");
|
|
15
|
+
const reconnecting = (transient, open, cursor, polled) => {
|
|
16
|
+
let at = cursor;
|
|
17
|
+
const once = Stream.suspend(() => open(at)).pipe(Stream.tap((fed) => Effect.sync(() => {
|
|
18
|
+
at = fed.cursor ?? at;
|
|
19
|
+
})));
|
|
20
|
+
const reopened = transient === void 0 ? once : once.pipe(Stream.repeat(Schedule.spaced(reconnectDelay)), Stream.retry(afterFailures(transient)));
|
|
21
|
+
return polled === void 0 ? reopened : reopened.pipe(Stream.catchIf(pushRefused, () => Stream.suspend(() => polled(at))));
|
|
22
|
+
};
|
|
23
|
+
const closeTimeout = Duration.seconds(5);
|
|
24
|
+
const closing = (closed) => closed.pipe(Effect.timeout(closeTimeout), Effect.ignore({ log: "Debug" }));
|
|
25
|
+
const polling = ({ poll }, transient) => (cursor) => Stream.suspend(() => {
|
|
26
|
+
const watch = globalThis.crypto.randomUUID();
|
|
27
|
+
return reconnecting(transient, (at) => paged((from) => poll(from, watch), at), cursor);
|
|
28
|
+
});
|
|
15
29
|
const capacity = 16;
|
|
16
|
-
const unsubscribeTimeout = Duration.seconds(5);
|
|
17
30
|
const delivered = (webhook, request, received, cursor) => {
|
|
18
31
|
let at = cursor;
|
|
19
32
|
const subscribed = Effect.suspend(() => webhook.subscribe({
|
|
@@ -30,16 +43,8 @@ const delivered = (webhook, request, received, cursor) => {
|
|
|
30
43
|
}));
|
|
31
44
|
};
|
|
32
45
|
const watching = (transport, { cursor: given, mode, resume } = {}) => {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
let at = cursor;
|
|
36
|
-
const once = Stream.suspend(() => open(at)).pipe(Stream.tap((fed) => Effect.sync(() => {
|
|
37
|
-
at = fed.cursor ?? at;
|
|
38
|
-
})));
|
|
39
|
-
const reopened = transient === void 0 ? once : once.pipe(Stream.repeat(Schedule.spaced(reconnectDelay)), Stream.retry(afterFailures(transient)));
|
|
40
|
-
return polled === void 0 ? reopened : reopened.pipe(Stream.catchIf(pushRefused, () => Stream.suspend(() => polled(at))));
|
|
41
|
-
};
|
|
42
|
-
const polled = (cursor) => reconnecting((at) => paged(transport.poll, at), cursor);
|
|
46
|
+
const { transient } = transport;
|
|
47
|
+
const polled = polling(transport, transient);
|
|
43
48
|
const webhooked = ({ baseUrl, ttl }, cursor) => Stream.unwrap(Effect.gen(function* () {
|
|
44
49
|
const { webhook } = transport;
|
|
45
50
|
if (webhook === void 0) return yield* deliveryUnsupported(transport.name, "webhook");
|
|
@@ -56,8 +61,8 @@ const watching = (transport, { cursor: given, mode, resume } = {}) => {
|
|
|
56
61
|
},
|
|
57
62
|
ttlMs: ttl === void 0 ? void 0 : Duration.toMillis(Duration.fromInputUnsafe(ttl))
|
|
58
63
|
};
|
|
59
|
-
yield* Effect.addFinalizer(() => webhook.unsubscribe({ delivery: { url } })
|
|
60
|
-
return reconnecting((at) => delivered(webhook, request, received, at), cursor);
|
|
64
|
+
yield* Effect.addFinalizer(() => closing(webhook.unsubscribe({ delivery: { url } })));
|
|
65
|
+
return reconnecting(transient, (at) => delivered(webhook, request, received, at), cursor);
|
|
61
66
|
}));
|
|
62
67
|
return Stream.suspend(() => {
|
|
63
68
|
const seen = makeRecent();
|
|
@@ -83,8 +88,8 @@ const watching = (transport, { cursor: given, mode, resume } = {}) => {
|
|
|
83
88
|
return Stream.unwrap(Effect.map(start, (cursor) => {
|
|
84
89
|
if (Predicate.isTagged(mode, "WebhookDelivery")) return webhooked(mode, cursor);
|
|
85
90
|
if (mode === "poll") return polled(cursor);
|
|
86
|
-
return reconnecting(transport.stream, cursor, mode === "stream" ? void 0 : polled);
|
|
91
|
+
return reconnecting(transient, transport.stream, cursor, mode === "stream" ? void 0 : polled);
|
|
87
92
|
})).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);
|
|
88
93
|
});
|
|
89
94
|
};
|
|
90
|
-
export { watching };
|
|
95
|
+
export { polling, watching };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Anyone,
|
|
1
|
+
import { Anyone, Keyed } from "./caller.mjs";
|
|
2
2
|
import { Any } from "../Event.mjs";
|
|
3
3
|
import { Feed } from "./feed.mjs";
|
|
4
4
|
import { AnyWebhookCancellation, AnyWebhookSubscription, Subscribed } from "./subscribe.mjs";
|
|
@@ -23,20 +23,24 @@ export interface Subscription extends AnyWebhookCancellation {
|
|
|
23
23
|
export interface Subscribing extends AnyWebhookSubscription {
|
|
24
24
|
readonly feed: WebhookFeed;
|
|
25
25
|
readonly principal: string;
|
|
26
|
-
readonly keyed:
|
|
26
|
+
readonly keyed: Keyed;
|
|
27
27
|
}
|
|
28
28
|
declare const Webhooks_base: Context.ServiceClass<Webhooks, "effect-actions/Webhooks", {
|
|
29
|
-
readonly serve: (feeds: readonly WebhookFeed[], keyed:
|
|
29
|
+
readonly serve: (feeds: readonly WebhookFeed[], keyed: Keyed) => Effect.Effect<void, never, unknown>;
|
|
30
30
|
readonly subscribe: (subscribing: Subscribing) => Effect.Effect<Subscribed, unknown, unknown>;
|
|
31
31
|
readonly unsubscribe: (subscription: Subscription) => Effect.Effect<void, unknown, unknown>;
|
|
32
32
|
}>;
|
|
33
33
|
/**
|
|
34
34
|
* Where a surface's webhook subscriptions are held and delivered from: `Webhook.layer` provides
|
|
35
|
-
* it, and a surface offers webhook delivery only where it is
|
|
35
|
+
* it, and a surface offers webhook delivery of its own events only where it is, and of a
|
|
36
|
+
* protected event `ActionEntity.reaching` serves, held by the entity reached, which refuses
|
|
37
|
+
* a subscription as `Unsupported` where it holds no `Webhook.layer`. A surface `serve`s the feeds it
|
|
36
38
|
* offers by webhook as it builds, with its authentication, which restores their kept
|
|
37
39
|
* subscriptions, and dies where they cannot be kept. `subscribe` runs as the subscriber, whose
|
|
38
40
|
* context each delivery runs in; it fails as the feed's admission does, or with
|
|
39
41
|
* `Event.SubscriptionError`.
|
|
40
42
|
*/
|
|
41
43
|
export declare class Webhooks extends Webhooks_base {}
|
|
44
|
+
/** Subscribes and unsubscribes a feed's webhooks where they are held. */
|
|
45
|
+
export type WebhookHolder = Pick<Webhooks["Service"], "subscribe" | "unsubscribe">;
|
|
42
46
|
export {};
|
|
@@ -1,53 +1,64 @@
|
|
|
1
|
-
import { Anyone
|
|
1
|
+
import { Anyone } from "./caller.mjs";
|
|
2
2
|
import { acquireFeeds } from "./feed.mjs";
|
|
3
3
|
import { deliveryUnsupported } from "./subscribe.mjs";
|
|
4
4
|
import { Context, Effect, Option } from "effect";
|
|
5
5
|
// src/contract/webhooks.ts
|
|
6
6
|
/**
|
|
7
7
|
* Where a surface's webhook subscriptions are held and delivered from: `Webhook.layer` provides
|
|
8
|
-
* it, and a surface offers webhook delivery only where it is
|
|
8
|
+
* it, and a surface offers webhook delivery of its own events only where it is, and of a
|
|
9
|
+
* protected event `ActionEntity.reaching` serves, held by the entity reached, which refuses
|
|
10
|
+
* a subscription as `Unsupported` where it holds no `Webhook.layer`. A surface `serve`s the feeds it
|
|
9
11
|
* offers by webhook as it builds, with its authentication, which restores their kept
|
|
10
12
|
* subscriptions, and dies where they cannot be kept. `subscribe` runs as the subscriber, whose
|
|
11
13
|
* context each delivery runs in; it fails as the feed's admission does, or with
|
|
12
14
|
* `Event.SubscriptionError`.
|
|
13
15
|
*/
|
|
14
16
|
var Webhooks = class extends Context.Service()("effect-actions/Webhooks") {};
|
|
15
|
-
|
|
16
|
-
const offersWebhook = (event
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
/** Whether a surface offers `event` by webhook: a protected one, its caller an identity. */
|
|
18
|
+
const offersWebhook = (event) => event.caller !== Anyone;
|
|
19
|
+
const offeredByWebhook = (feed) => offersWebhook(feed.event);
|
|
20
|
+
const deliversByWebhook = (feed) => feed.webhook === void 0 && offeredByWebhook(feed);
|
|
21
|
+
/**
|
|
22
|
+
* Who subscribes to `feed`'s webhooks, and where they are held: the holder the feed names, a
|
|
23
|
+
* reached entity, or else the surface's own `Webhooks`; none where the event is not offered by
|
|
24
|
+
* webhook. Its principal is read from the caller each time it runs.
|
|
25
|
+
*/
|
|
26
|
+
const webhookSubscriber = (feed, keyed, webhooks) => keyed !== void 0 && offeredByWebhook(feed) ? Option.map(feed.webhook === void 0 ? webhooks : Option.some(feed.webhook), (holder) => Effect.map(Effect.service(feed.event.caller), (identity) => ({
|
|
27
|
+
webhooks: holder,
|
|
28
|
+
feed,
|
|
29
|
+
keyed,
|
|
30
|
+
principal: keyed.principal(identity)
|
|
31
|
+
}))) : Option.none();
|
|
32
|
+
/** As `webhookSubscriber`, failing as `Unsupported` where the event is not offered by webhook. */
|
|
33
|
+
const subscriberOf = (feed, keyed, webhooks) => Option.getOrElse(webhookSubscriber(feed, keyed, webhooks), () => Effect.fail(deliveryUnsupported(feed.event.name, "webhook")));
|
|
34
|
+
/** A surface's answers to a webhook subscribe and unsubscribe of `feed`, run as its subscriber. */
|
|
35
|
+
const webhookAnswers = (feed, keyed, webhooks) => {
|
|
36
|
+
const served = subscriberOf(feed, keyed, webhooks);
|
|
21
37
|
return {
|
|
22
|
-
webhooks,
|
|
23
|
-
|
|
38
|
+
subscribe: (payload) => Effect.flatMap(served, ({ webhooks: holder, ...subscriber }) => holder.subscribe({
|
|
39
|
+
...payload,
|
|
40
|
+
...subscriber
|
|
41
|
+
})),
|
|
42
|
+
unsubscribe: (payload) => Effect.flatMap(served, ({ webhooks: holder, feed: subscribed, principal }) => holder.unsubscribe({
|
|
43
|
+
...payload,
|
|
44
|
+
feed: subscribed,
|
|
45
|
+
principal
|
|
46
|
+
}))
|
|
24
47
|
};
|
|
25
|
-
});
|
|
26
|
-
const webhookSubscriber = (feed, keyed, webhooks) => {
|
|
27
|
-
const { event } = feed;
|
|
28
|
-
if (Option.isNone(webhooks) || !isNamed(keyed) || !isProtected(event)) return Effect.fail(deliveryUnsupported(event.name, "webhook"));
|
|
29
|
-
const service = webhooks.value;
|
|
30
|
-
return Effect.map(Effect.service(event.caller), (identity) => ({
|
|
31
|
-
webhooks: service,
|
|
32
|
-
feed: {
|
|
33
|
-
...feed,
|
|
34
|
-
event
|
|
35
|
-
},
|
|
36
|
-
keyed,
|
|
37
|
-
principal: keyed.principal(identity)
|
|
38
|
-
}));
|
|
39
48
|
};
|
|
40
49
|
/**
|
|
41
|
-
* The feeds of `events` a surface's layer serves, and the
|
|
42
|
-
* layer builds,
|
|
50
|
+
* The feeds of `events` a surface's layer serves, and the `Webhooks` it offers them by: run as
|
|
51
|
+
* the layer builds, which serves the webhooks it delivers itself, those of the feeds `holds`
|
|
52
|
+
* admits, and restores their kept subscriptions.
|
|
43
53
|
*/
|
|
44
|
-
const servingFeeds = Effect.fnUntraced(function* (
|
|
54
|
+
const servingFeeds = Effect.fnUntraced(function* (events, keyed, holds = () => true) {
|
|
45
55
|
const feeds = yield* acquireFeeds(events);
|
|
46
|
-
const
|
|
47
|
-
|
|
56
|
+
const webhooks = yield* Effect.serviceOption(Webhooks);
|
|
57
|
+
const delivered = feeds.filter(deliversByWebhook).filter(holds);
|
|
58
|
+
if (Option.isSome(webhooks) && keyed !== void 0) yield* webhooks.value.serve(delivered, keyed);
|
|
48
59
|
return {
|
|
49
60
|
feeds,
|
|
50
|
-
|
|
61
|
+
webhooks
|
|
51
62
|
};
|
|
52
63
|
});
|
|
53
|
-
export { Webhooks, offersWebhook,
|
|
64
|
+
export { Webhooks, offersWebhook, servingFeeds, subscriberOf, webhookAnswers, webhookSubscriber };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { Headers } from "effect/http";
|
|
3
|
+
/**
|
|
4
|
+
* The headers of a request that a call into an entity carries, their names lowercased: every
|
|
5
|
+
* one but its connection's (`host`, `connection`, `keep-alive`, `te`, `trailer`,
|
|
6
|
+
* `transfer-encoding`, `upgrade`), its body's (`content-*`), what it accepts (`accept*`), a
|
|
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 the Cloudflare
|
|
9
|
+
* package's socket host keeps these of a page's upgrade.
|
|
10
|
+
*/
|
|
11
|
+
export declare const forwardedHeaders: (headers: Headers.Input) => Headers.Headers;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Effect, Option } from "effect";
|
|
2
|
+
import { Headers, HttpServerRequest } from "effect/http";
|
|
3
|
+
// src/entity/forwarded.ts
|
|
4
|
+
const ownedByTheRequest = (name) => [
|
|
5
|
+
"host",
|
|
6
|
+
"connection",
|
|
7
|
+
"keep-alive",
|
|
8
|
+
"te",
|
|
9
|
+
"trailer",
|
|
10
|
+
"transfer-encoding",
|
|
11
|
+
"upgrade"
|
|
12
|
+
].includes(name) || [
|
|
13
|
+
"content-",
|
|
14
|
+
"accept",
|
|
15
|
+
"proxy-",
|
|
16
|
+
"sec-websocket-",
|
|
17
|
+
"mcp-"
|
|
18
|
+
].some((family) => name.startsWith(family));
|
|
19
|
+
/**
|
|
20
|
+
* The headers of a request that a call into an entity carries, their names lowercased: every
|
|
21
|
+
* one but its connection's (`host`, `connection`, `keep-alive`, `te`, `trailer`,
|
|
22
|
+
* `transfer-encoding`, `upgrade`), its body's (`content-*`), what it accepts (`accept*`), a
|
|
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 the Cloudflare
|
|
25
|
+
* package's socket host keeps these of a page's upgrade.
|
|
26
|
+
*/
|
|
27
|
+
const forwardedHeaders = (headers) => Headers.fromInput(Object.entries(Headers.fromInput(headers)).filter(([name]) => !ownedByTheRequest(name)));
|
|
28
|
+
const forwarded = Effect.map(Effect.serviceOption(HttpServerRequest.HttpServerRequest), Option.match({
|
|
29
|
+
onNone: () => Headers.empty,
|
|
30
|
+
onSome: ({ headers }) => forwardedHeaders(headers)
|
|
31
|
+
}));
|
|
32
|
+
export { forwarded, forwardedHeaders };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { reported } from "../contract/reported.mjs";
|
|
2
|
+
import { Wake } from "../contract/wake.mjs";
|
|
3
|
+
import { Clock, DateTime, Duration, Effect, Exit, Latch, Schema } from "effect";
|
|
4
|
+
import { ClusterSchema, DeliverAt } from "effect/cluster";
|
|
5
|
+
import { Rpc } from "effect/rpc";
|
|
6
|
+
// src/entity/wakes.ts
|
|
7
|
+
const wakeTag = "effect-actions/wake";
|
|
8
|
+
var WakeAt = class extends Schema.Class("effect-actions/ActionEntity/WakeAt")({ at: Schema.Int }) {
|
|
9
|
+
[DeliverAt.symbol]() {
|
|
10
|
+
return DateTime.makeUnsafe(this.at);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const WakeRpc = Rpc.make(wakeTag, { payload: WakeAt }).annotate(ClusterSchema.Persisted, true);
|
|
14
|
+
const makeWake = (send) => Effect.map(Clock.Clock, (clock) => {
|
|
15
|
+
const waiting = /* @__PURE__ */ new Set();
|
|
16
|
+
const sent = /* @__PURE__ */ new Set();
|
|
17
|
+
const slept = (at) => clock.sleep(Duration.millis(Math.max(at - clock.currentTimeMillisUnsafe(), 0)));
|
|
18
|
+
const until = (at) => Effect.suspend(() => {
|
|
19
|
+
const waiter = {
|
|
20
|
+
at,
|
|
21
|
+
woken: Latch.makeUnsafe()
|
|
22
|
+
};
|
|
23
|
+
waiting.add(waiter);
|
|
24
|
+
return Effect.raceFirst(waiter.woken.await, sent.has(at) ? Effect.never : Effect.uninterruptible(Effect.exit(Effect.tap(send(new WakeAt({ at })), () => Effect.sync(() => sent.add(at))))).pipe(Effect.flatMap(Exit.match({
|
|
25
|
+
onSuccess: () => Effect.never,
|
|
26
|
+
onFailure: (cause) => Effect.andThen(reported(cause), slept(at))
|
|
27
|
+
})))).pipe(Effect.ensuring(Effect.sync(() => {
|
|
28
|
+
waiting.delete(waiter);
|
|
29
|
+
})));
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
wake: Wake.of({ until }),
|
|
33
|
+
woke: ({ payload }) => Rpc.fork(Effect.sync(() => {
|
|
34
|
+
for (const each of sent) if (each <= payload.at) sent.delete(each);
|
|
35
|
+
for (const waiter of waiting) if (waiter.at <= payload.at) waiter.woken.openUnsafe();
|
|
36
|
+
}))
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
export { WakeRpc, makeWake, wakeTag };
|
package/dist/http/client.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { checked, checkedStream, sendingValue,
|
|
1
|
+
import { checked, checkedStream, sendingValue, sentInput } from "../contract/call.mjs";
|
|
2
2
|
import { Answering, answeringOf } from "../contract/stream.mjs";
|
|
3
3
|
import { watching } from "../contract/watch.mjs";
|
|
4
4
|
import { readElements, readWithProgress } from "./streamed.mjs";
|
|
@@ -48,7 +48,7 @@ const sendersWith = (http, httpClient, baseUrl) => Effect.gen(function* () {
|
|
|
48
48
|
const methodsWith = (http, httpClient, baseUrl) => Effect.map(sendersWith(http, httpClient, baseUrl), ({ method, transport }) => {
|
|
49
49
|
const actions = Object.fromEntries(http.actions.map((action) => [action.name, method(action)]));
|
|
50
50
|
if (http.events.length === 0) return actions;
|
|
51
|
-
const watch = (event, input, options) => Stream.unwrap(Effect.map(
|
|
51
|
+
const watch = (event, input, options) => Stream.unwrap(Effect.map(sentInput(event, input), ({ value }) => watching(transport(event, value), options)));
|
|
52
52
|
return {
|
|
53
53
|
...actions,
|
|
54
54
|
watch
|
package/dist/http/events.d.mts
CHANGED
|
@@ -7,19 +7,8 @@ import "../Action.mjs";
|
|
|
7
7
|
import { Any } from "../Event.mjs";
|
|
8
8
|
import "../contract/feed.mjs";
|
|
9
9
|
import "../contract/push.mjs";
|
|
10
|
-
import "
|
|
11
|
-
import "../contract/webhooks.mjs";
|
|
12
|
-
import { Effect, Option, Schema, Stream } from "effect";
|
|
10
|
+
import { Effect, Schema, Stream } from "effect";
|
|
13
11
|
import { HttpApiEndpoint, HttpApiSchema } from "effect/http-api";
|
|
14
|
-
export declare const pageOf: <E extends Any>(event: E) => Schema.encodeKeys<Schema.Struct<{
|
|
15
|
-
readonly events: Schema.$Array<OccurrenceSchema<E>>;
|
|
16
|
-
readonly cursor: Schema.NullOr<Schema.String>;
|
|
17
|
-
readonly truncated: Schema.Boolean;
|
|
18
|
-
readonly hasMore: Schema.Boolean;
|
|
19
|
-
readonly nextPoll: Schema.DurationFromMillis;
|
|
20
|
-
}>, {
|
|
21
|
-
readonly nextPoll: "nextPollMs";
|
|
22
|
-
}>;
|
|
23
12
|
export declare const sentOf: <E extends Any>(event: E) => HttpApiSchema.StreamSse<HttpApiSchema.SseEventFromData<Schema.Union<readonly [OccurrenceSchema<E>, Schema.TaggedStruct<"Gap", {
|
|
24
13
|
readonly cursor: Schema.NullOr<Schema.String>;
|
|
25
14
|
}>, Schema.TaggedStruct<"Checked", {
|
package/dist/http/events.mjs
CHANGED
|
@@ -2,23 +2,15 @@ import { principalOf } from "../contract/caller.mjs";
|
|
|
2
2
|
import { assertDistinctTags } from "../contract/declared.mjs";
|
|
3
3
|
import { Refusal } from "../contract/errors.mjs";
|
|
4
4
|
import { SubscriptionError } from "../Event.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { pageOf } from "../contract/pages.mjs";
|
|
6
6
|
import { PushedOf, SubscriptionOf, queuedPush } from "../contract/push.mjs";
|
|
7
7
|
import { declared } from "../contract/status.mjs";
|
|
8
8
|
import { Subscribed, WebhookCancellationOf, WebhookSubscriptionOf } from "../contract/subscribe.mjs";
|
|
9
|
-
import { webhookSubscriber } from "../contract/webhooks.mjs";
|
|
10
9
|
import { acceptsEventStream } from "./streamed.mjs";
|
|
11
10
|
import { Effect, Function, Schema, Stream } from "effect";
|
|
12
11
|
import { HttpServerRequest } from "effect/http";
|
|
13
12
|
import { HttpApiEndpoint, HttpApiSchema, OpenApi } from "effect/http-api";
|
|
14
13
|
// src/http/events.ts
|
|
15
|
-
const pageOf = (event) => Schema.Struct({
|
|
16
|
-
events: Schema.Array(OccurrenceOf(event)),
|
|
17
|
-
cursor: Schema.NullOr(Schema.String),
|
|
18
|
-
truncated: Schema.Boolean,
|
|
19
|
-
hasMore: Schema.Boolean,
|
|
20
|
-
nextPoll: Schema.DurationFromMillis
|
|
21
|
-
}).pipe(Schema.encodeKeys({ nextPoll: "nextPollMs" }));
|
|
22
14
|
const sentOf = (event) => HttpApiSchema.StreamSse({
|
|
23
15
|
data: PushedOf(event),
|
|
24
16
|
error: Refusal
|
|
@@ -46,20 +38,6 @@ const webhookEndpointsOf = (event, path, errors) => {
|
|
|
46
38
|
error
|
|
47
39
|
}).annotate(OpenApi.Description, `Stop delivering ${event.name} to a webhook.`)];
|
|
48
40
|
};
|
|
49
|
-
const webhookAnswers = (feed, authentication, webhooks) => {
|
|
50
|
-
const served = webhookSubscriber(feed, authentication, webhooks);
|
|
51
|
-
return {
|
|
52
|
-
subscribe: ({ payload }) => Effect.flatMap(served, ({ webhooks: service, ...subscriber }) => service.subscribe({
|
|
53
|
-
...payload,
|
|
54
|
-
...subscriber
|
|
55
|
-
})),
|
|
56
|
-
unsubscribe: ({ payload }) => Effect.flatMap(served, ({ webhooks: service, feed: subscribed, principal }) => service.unsubscribe({
|
|
57
|
-
...payload,
|
|
58
|
-
feed: subscribed,
|
|
59
|
-
principal
|
|
60
|
-
}))
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
41
|
const answering = (feed, authentication) => {
|
|
64
42
|
const encode = Schema.encodeEffect(Schema.toCodecJson(PushedOf(feed.event)));
|
|
65
43
|
return ({ payload: { input, cursor } }) => Effect.gen(function* () {
|
|
@@ -75,4 +53,4 @@ const answering = (feed, authentication) => {
|
|
|
75
53
|
return yield* feed.poll(input, cursor, { subscriber });
|
|
76
54
|
});
|
|
77
55
|
};
|
|
78
|
-
export { answering, eventEndpointOf,
|
|
56
|
+
export { answering, eventEndpointOf, sentOf, servedSent, webhookEndpointsOf };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Function, Match } from "effect";
|
|
2
|
+
// src/http/redacted.ts
|
|
3
|
+
/**
|
|
4
|
+
* Where an API key scheme sends its credential, for `HttpApi.RedactedNames`: a span records a
|
|
5
|
+
* request's query, and a header that the host's `TracerHeaderFilter` admits, as sent unless it
|
|
6
|
+
* is named there or by Effect's defaults, which already cover `Authorization` and `Cookie`.
|
|
7
|
+
*/
|
|
8
|
+
const credentialNames = (authentication) => authentication === void 0 ? {} : Match.value(authentication.security).pipe(Match.tag("ApiKey", ({ in: placement, key }) => Match.value(placement).pipe(Match.when("query", () => ({ query: [key] })), Match.when("header", () => ({ headers: [key] })), Match.when("cookie", Function.constant({})), Match.exhaustive)), Match.tag("Http", "Basic", Function.constant({})), Match.exhaustive);
|
|
9
|
+
export { credentialNames };
|
package/dist/mcp/bridge.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { encodeUntaggedOf } from "../contract/occurrence.mjs";
|
|
|
8
8
|
import { pushed, refusalIn } from "../contract/push.mjs";
|
|
9
9
|
import { reported } from "../contract/reported.mjs";
|
|
10
10
|
import { deliveryUnsupported } from "../contract/subscribe.mjs";
|
|
11
|
-
import { Webhooks,
|
|
11
|
+
import { Webhooks, subscriberOf, webhookSubscriber } from "../contract/webhooks.mjs";
|
|
12
12
|
import { Active, EventsError, Failed, Heartbeat, Notifications, Occurred, Requests, Terminated, codes, extension, subscriptionIdKey } from "./events.mjs";
|
|
13
13
|
import { isObjectSchema, toolJsonSchema } from "./protocol.mjs";
|
|
14
14
|
import { Cause, Context, Duration, Effect, Exit, Match, Option, Predicate, Result, Schema } from "effect";
|
|
@@ -136,18 +136,17 @@ const withEvents = (base, serving) => {
|
|
|
136
136
|
const send = target.context.sendNotification;
|
|
137
137
|
const meta = Effect.map(Effect.orDie(encodeServerInfo(target.context.serverInfo)), (serverInfo) => ({ "io.modelcontextprotocol/serverInfo": serverInfo }));
|
|
138
138
|
const webhooks = Context.getOption(built, Webhooks);
|
|
139
|
-
const
|
|
140
|
-
const deliveryOf = (event) => [
|
|
139
|
+
const deliveryOf = (feed) => [
|
|
141
140
|
"poll",
|
|
142
141
|
...send === void 0 ? [] : ["push"],
|
|
143
|
-
...
|
|
142
|
+
...Option.isSome(webhookSubscriber(feed, authentication, webhooks)) ? ["webhook"] : []
|
|
144
143
|
];
|
|
145
|
-
const served = new Map(feeds.map((feed) => [feed.event.name, servedOf(feed, deliveryOf(feed
|
|
144
|
+
const served = new Map(feeds.map((feed) => [feed.event.name, servedOf(feed, deliveryOf(feed))]));
|
|
146
145
|
const servedNamed = (name) => {
|
|
147
146
|
const found = served.get(name);
|
|
148
147
|
return found === void 0 ? Effect.fail(new EventsError(sentError(SubscriptionError.of(name, new NotServed())))) : Effect.succeed(found);
|
|
149
148
|
};
|
|
150
|
-
const asWebhook = (feed, run) => Effect.catchCause(asCaller(authentication, feed.event, Effect.flatMap(
|
|
149
|
+
const asWebhook = (feed, run) => Effect.catchCause(asCaller(authentication, feed.event, Effect.flatMap(subscriberOf(feed, authentication, webhooks), run)), (cause) => Effect.flatMap(wireErrorOf(cause), Effect.fail));
|
|
151
150
|
const fromBuilt = (handled) => Effect.updateContext(handled, (request) => Context.merge(built, request));
|
|
152
151
|
const handlers = Requests.of({
|
|
153
152
|
"events/list": () => Effect.map(meta, (_meta) => ({
|
package/dist/mcp/client.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { checked, checkedStream, sendingJson,
|
|
1
|
+
import { checked, checkedStream, sendingJson, sentInput } from "../contract/call.mjs";
|
|
2
2
|
import { Anyone } from "../contract/caller.mjs";
|
|
3
3
|
import { projectedErrors } from "../contract/declared.mjs";
|
|
4
4
|
import { Refusal } from "../contract/errors.mjs";
|
|
@@ -57,7 +57,7 @@ function client(contracts, { url = defaultPath, transformClient, authentication
|
|
|
57
57
|
const refusedWith = (contract) => [...Refusal.members, ...contract.caller === Anyone ? [] : authentication?.error ?? []];
|
|
58
58
|
return Effect.map(HttpClient.HttpClient, (native) => {
|
|
59
59
|
const http = transformClient === void 0 ? native : transformClient(native);
|
|
60
|
-
const watch = (event, input, options) => Stream.unwrap(Effect.map(
|
|
60
|
+
const watch = (event, input, options) => Stream.unwrap(Effect.map(sentInput(event, input), ({ json }) => watching(transportOf({
|
|
61
61
|
http,
|
|
62
62
|
url,
|
|
63
63
|
event,
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import "../authentication/provider.mjs";
|
|
2
|
+
import { Forbidden } from "../contract/errors.mjs";
|
|
3
|
+
import { Any } from "../Action.mjs";
|
|
4
|
+
import "../contract/call.mjs";
|
|
5
|
+
import { Codec } from "../contract/codec.mjs";
|
|
6
|
+
import "../contract/declared.mjs";
|
|
7
|
+
import { Any as Any$1 } from "../Event.mjs";
|
|
8
|
+
import "../contract/feed.mjs";
|
|
9
|
+
import { ErasedValue } from "../contract/implementation.mjs";
|
|
10
|
+
import "../contract/progress.mjs";
|
|
11
|
+
import "../contract/watch.mjs";
|
|
12
|
+
import "../contract/webhooks.mjs";
|
|
13
|
+
import { Cause, Duration, Effect, ErrorReporter, Queue, Schema, Stream } from "effect";
|
|
14
|
+
import { Rpc, RpcGroup, RpcMiddleware } from "effect/rpc";
|
|
15
|
+
export declare class DeclaredFailure {
|
|
16
|
+
readonly [ErrorReporter.ignore] = true;
|
|
17
|
+
readonly json: Schema.Json;
|
|
18
|
+
constructor(json: Schema.Json);
|
|
19
|
+
toString(): string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* What serves a binding's erased rpcs: `ActionRpc.layer`, which streams each event type beside
|
|
23
|
+
* polling it, through its `middleware`, or an entity, which only polls them, holds their webhooks
|
|
24
|
+
* and runs `check` on each input.
|
|
25
|
+
*/
|
|
26
|
+
export type Server = {
|
|
27
|
+
readonly _tag: "ActionRpc.layer";
|
|
28
|
+
readonly middleware: readonly RpcMiddleware.AnyService[];
|
|
29
|
+
} | {
|
|
30
|
+
readonly _tag: "ActionEntity.layer";
|
|
31
|
+
readonly check: InputCheck;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* The tag of the unary rpc polling the event type `name`, `<name>/poll`, taking `Poll` beside
|
|
35
|
+
* its `input` and answering a `Page`, which `ActionRpc.layer` and an `ActionEntity` serve and
|
|
36
|
+
* `ActionRpc.client`'s polling watch calls.
|
|
37
|
+
*/
|
|
38
|
+
export declare const pollTag: (name: string) => string;
|
|
39
|
+
/** The rpcs `server` serves for `actions` and `events`, and its clients call. */
|
|
40
|
+
export declare const erasedOf: (actions: readonly Any[], events: readonly Any$1[], server: Server["_tag"]) => RpcGroup.RpcGroup<Rpc.Rpc<string, Schema.Codec<Schema.Json, Schema.Json, never, never>, Schema.Codec<Schema.Json, Schema.Json, never, never>, Schema.instanceOf<DeclaredFailure, DeclaredFailure>, never, never> | Rpc.Rpc<string, Schema.Codec<Schema.Json, Schema.Json, never, never>, import("effect/rpc/RpcSchema").Stream<Schema.Codec<Schema.Json, Schema.Json, never, never>, Schema.instanceOf<DeclaredFailure, DeclaredFailure>>, Schema.Never, never, never>>;
|
|
41
|
+
/**
|
|
42
|
+
* The payload of an event type's poll rpc, `<event>/poll`, past its `input`, as a client sends
|
|
43
|
+
* it: the `cursor` to read after, `null` reading from now, at most `maxEvents` occurrences, and
|
|
44
|
+
* the `watch`, a UUID one watch sends with each of its polls, which tells a page's watches apart
|
|
45
|
+
* on a socket and holds nothing on the server: a poll's subscription hook is its caller's.
|
|
46
|
+
*/
|
|
47
|
+
export declare const Poll: Schema.Struct<{
|
|
48
|
+
readonly cursor: Schema.NullOr<Schema.String>;
|
|
49
|
+
readonly maxEvents: Schema.optionalKey<Schema.Finite>;
|
|
50
|
+
readonly watch: Schema.optionalKey<Schema.String>;
|
|
51
|
+
}>;
|
|
52
|
+
export declare const ProgressedOf: <P extends Codec, S extends Codec>(progress: P, success: S) => Schema.Union<readonly [Schema.TaggedStruct<"Progress", {
|
|
53
|
+
readonly value: P;
|
|
54
|
+
}>, Schema.TaggedStruct<"Final", {
|
|
55
|
+
readonly value: S;
|
|
56
|
+
}>]>;
|
|
57
|
+
/**
|
|
58
|
+
* What a server checks of each call's and subscription's decoded input, once it has verified its
|
|
59
|
+
* caller and before the implementation's authorizer: an entity refuses an input whose key names
|
|
60
|
+
* another entity.
|
|
61
|
+
*/
|
|
62
|
+
export type InputCheck = (contract: Any | Any$1, input: ErasedValue) => Effect.Effect<void, Forbidden>;
|