@gjermundgaraba/effect-actions 0.22.0 → 0.23.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 +7 -1
- package/dist/Action.d.mts +2 -2
- package/dist/Action.mjs +1 -0
- package/dist/ActionCli.mjs +0 -1
- package/dist/ActionHttp.d.mts +1 -1
- package/dist/ActionMcp.d.mts +0 -3
- package/dist/ActionMcp.mjs +0 -5
- package/dist/ActionRpc.d.mts +8 -15
- package/dist/ActionRpc.mjs +29 -54
- package/dist/ActionToolkit.d.mts +0 -2
- package/dist/ActionToolkit.mjs +0 -2
- package/dist/Event.d.mts +0 -23
- package/dist/Testing.mjs +3 -2
- package/dist/Webhook.mjs +2 -2
- package/dist/authentication/refusal.mjs +0 -1
- package/dist/cli/commands.mjs +0 -5
- package/dist/contract/authorizer.mjs +0 -5
- package/dist/contract/call.mjs +0 -23
- package/dist/contract/callback.mjs +0 -1
- package/dist/contract/caller.mjs +0 -11
- package/dist/contract/canonical.mjs +0 -5
- package/dist/contract/codec.d.mts +0 -1
- package/dist/contract/codec.mjs +0 -1
- package/dist/contract/codes.mjs +1 -15
- package/dist/contract/declared.mjs +0 -15
- package/dist/contract/digest.mjs +0 -1
- package/dist/contract/errors.d.mts +0 -4
- package/dist/contract/errors.mjs +0 -4
- package/dist/contract/feed.d.mts +0 -1
- package/dist/contract/feed.mjs +0 -20
- package/dist/contract/hints.mjs +0 -6
- package/dist/contract/hub.mjs +0 -3
- package/dist/contract/idempotency.mjs +0 -4
- package/dist/contract/implementation.d.mts +0 -1
- package/dist/contract/implementation.mjs +0 -27
- package/dist/contract/input-check.mjs +0 -1
- package/dist/contract/lease.mjs +0 -1
- package/dist/contract/listed.mjs +0 -6
- package/dist/contract/log.mjs +0 -8
- package/dist/contract/names.mjs +0 -1
- package/dist/contract/occurrence.mjs +0 -8
- package/dist/contract/pages.mjs +5 -12
- package/dist/contract/progress.mjs +2 -25
- package/dist/contract/push.d.mts +0 -5
- package/dist/contract/push.mjs +1 -24
- package/dist/contract/receiving.mjs +0 -7
- package/dist/contract/reported.mjs +0 -5
- package/dist/contract/serving.mjs +0 -18
- package/dist/contract/status.mjs +0 -3
- package/dist/contract/stream.mjs +0 -2
- package/dist/contract/subscribe.d.mts +0 -2
- package/dist/contract/subscribe.mjs +0 -8
- package/dist/contract/transient.mjs +0 -12
- package/dist/contract/watch.mjs +5 -17
- package/dist/contract/webhooks.mjs +0 -11
- package/dist/http/client.mjs +0 -1
- package/dist/http/events.d.mts +0 -10
- package/dist/http/events.mjs +2 -31
- package/dist/http/placed.d.mts +0 -2
- package/dist/http/streamed.d.mts +0 -1
- package/dist/http/streamed.mjs +4 -32
- package/dist/http/watch.mjs +0 -6
- package/dist/mcp/bridge.mjs +0 -25
- package/dist/mcp/client.d.mts +0 -1
- package/dist/mcp/client.mjs +0 -1
- package/dist/mcp/events.mjs +0 -6
- package/dist/mcp/progress.mjs +0 -7
- package/dist/mcp/protocol.mjs +0 -6
- package/dist/mcp/reply.mjs +0 -11
- package/dist/mcp/request.mjs +0 -1
- package/dist/mcp/watch.mjs +0 -6
- package/dist/rpc/watch.mjs +0 -6
- package/dist/stdio/console.mjs +2 -2
- package/dist/webhook/address.mjs +4 -9
- package/dist/webhook/attempt.mjs +2 -15
- package/dist/webhook/receiver.mjs +2 -8
- package/dist/webhook/signing.mjs +0 -3
- package/dist/webhook/stored.mjs +8 -17
- package/dist/webhook/subscriptions.mjs +19 -23
- package/docs/Action-streams.md +0 -12
- package/docs/Action.md +0 -10
- package/docs/ActionCli.md +2 -26
- package/docs/ActionHttp.md +0 -28
- package/docs/ActionMcp.md +2 -37
- package/docs/ActionRpc-events.md +0 -16
- package/docs/ActionRpc.md +0 -63
- package/docs/ActionToolkit.md +0 -11
- package/docs/Authentication-advanced.md +0 -31
- package/docs/Authentication.md +4 -13
- package/docs/Event.md +0 -37
- package/docs/README.md +1 -1
- package/docs/Testing.md +3 -13
- package/docs/Webhook.md +0 -46
- package/docs/setup.md +0 -5
- package/examples/README.md +7 -1
- package/examples/assistant/src/agent.ts +8 -31
- package/examples/assistant/src/cli.ts +2 -9
- package/examples/assistant/src/commands.ts +0 -8
- package/examples/assistant/src/identity/Identity.ts +0 -10
- package/examples/assistant/src/identity/authorize.ts +0 -4
- package/examples/assistant/src/identity/members.ts +1 -3
- package/examples/assistant/src/identity/permissions.ts +0 -5
- package/examples/assistant/src/platform/tasks-file.ts +1 -6
- package/examples/assistant/src/scripted.ts +0 -4
- package/examples/assistant/src/tasks/Tasks.ts +0 -2
- package/examples/assistant/src/tasks/handlers.ts +0 -3
- package/examples/assistant/src/tasks/store.ts +0 -7
- package/examples/assistant/src/toolkit.ts +0 -11
- package/examples/assistant/test/commands.test.ts +0 -5
- package/examples/assistant/test/identity/identity.test.ts +6 -6
- package/examples/assistant/test/platform/tasks-file.test.ts +2 -6
- package/examples/assistant/test/tasks/tasks.test.ts +13 -14
- package/examples/assistant/test/toolkit.test.ts +7 -18
- package/examples/build-events/README.md +2 -0
- package/examples/build-events/src/Api.ts +0 -3
- package/examples/build-events/src/Rpc.ts +0 -2
- package/examples/build-events/src/builds/Builds.ts +0 -15
- package/examples/build-events/src/builds/events.ts +0 -10
- package/examples/build-events/src/builds/handlers.ts +0 -6
- package/examples/build-events/src/builds/log.ts +0 -6
- package/examples/build-events/src/builds/status.ts +0 -4
- package/examples/build-events/src/builds/store.ts +0 -21
- package/examples/build-events/src/cli.ts +0 -2
- package/examples/build-events/src/commands.ts +0 -9
- package/examples/build-events/src/deploys/approvers.ts +0 -3
- package/examples/build-events/src/deploys/events.ts +0 -8
- package/examples/build-events/src/deploys/handlers.ts +0 -2
- package/examples/build-events/src/identity/Identity.ts +0 -11
- package/examples/build-events/src/identity/members.ts +1 -2
- package/examples/build-events/src/identity/verifier.ts +8 -9
- package/examples/build-events/src/notifier/Notices.ts +0 -3
- package/examples/build-events/src/notifier/routes.ts +0 -10
- package/examples/build-events/src/notifier.ts +0 -8
- package/examples/build-events/src/routes.ts +0 -24
- package/examples/build-events/src/server.ts +0 -14
- package/examples/build-events/test/builds/events.test.ts +0 -6
- package/examples/build-events/test/builds/log.test.ts +0 -1
- package/examples/build-events/test/builds/progress.test.ts +0 -1
- package/examples/build-events/test/commands.test.ts +0 -5
- package/examples/build-events/test/deploys/events.test.ts +0 -1
- package/examples/build-events/test/http.test.ts +0 -4
- package/examples/build-events/test/notifier.test.ts +0 -9
- package/examples/build-events/test/routes.test.ts +0 -9
- package/examples/build-events/test/rpc.test.ts +0 -4
- package/examples/build-events/test/support/as.ts +3 -4
- package/examples/build-events/test/support/memory.ts +0 -5
- package/examples/build-events/test/webhooks.test.ts +0 -12
- package/examples/kitchen-sink/README.md +1 -1
- package/examples/kitchen-sink/src/Actions.ts +0 -10
- package/examples/kitchen-sink/src/Api.ts +0 -8
- package/examples/kitchen-sink/src/Rpc.ts +0 -4
- package/examples/kitchen-sink/src/admin/Admin.ts +0 -9
- package/examples/kitchen-sink/src/admin/authenticate.ts +2 -6
- package/examples/kitchen-sink/src/admin/authorize.ts +0 -5
- package/examples/kitchen-sink/src/admin/grants.ts +0 -5
- package/examples/kitchen-sink/src/admin/handlers.ts +0 -2
- package/examples/kitchen-sink/src/admin/keys.ts +1 -3
- package/examples/kitchen-sink/src/agent.ts +2 -13
- package/examples/kitchen-sink/src/assistant/Assistant.ts +7 -24
- package/examples/kitchen-sink/src/assistant/conversations.ts +3 -21
- package/examples/kitchen-sink/src/assistant/demo-model.ts +0 -5
- package/examples/kitchen-sink/src/assistant/handlers.ts +1 -30
- package/examples/kitchen-sink/src/attachments/Attachments.ts +0 -4
- package/examples/kitchen-sink/src/attachments/download.ts +0 -10
- package/examples/kitchen-sink/src/attachments/handlers.ts +0 -3
- package/examples/kitchen-sink/src/attachments/store.ts +0 -3
- package/examples/kitchen-sink/src/browser.ts +0 -9
- package/examples/kitchen-sink/src/cli.ts +0 -19
- package/examples/kitchen-sink/src/config.ts +3 -11
- package/examples/kitchen-sink/src/identity/Identity.ts +0 -18
- package/examples/kitchen-sink/src/identity/authenticate.ts +3 -14
- package/examples/kitchen-sink/src/identity/authorize.ts +0 -6
- package/examples/kitchen-sink/src/identity/directory.ts +8 -18
- package/examples/kitchen-sink/src/identity/handlers.ts +0 -3
- package/examples/kitchen-sink/src/identity/members.ts +1 -5
- package/examples/kitchen-sink/src/identity/quota.ts +0 -11
- package/examples/kitchen-sink/src/issues/Issues.ts +0 -24
- package/examples/kitchen-sink/src/issues/assistant.ts +0 -10
- package/examples/kitchen-sink/src/issues/events.ts +2 -16
- package/examples/kitchen-sink/src/issues/export.ts +0 -5
- package/examples/kitchen-sink/src/issues/guide.ts +0 -5
- package/examples/kitchen-sink/src/issues/handlers.ts +0 -5
- package/examples/kitchen-sink/src/issues/store.ts +19 -23
- package/examples/kitchen-sink/src/issues/write-limit.ts +0 -12
- package/examples/kitchen-sink/src/memory.ts +0 -9
- package/examples/kitchen-sink/src/ops.ts +0 -6
- package/examples/kitchen-sink/src/request-policy.ts +0 -17
- package/examples/kitchen-sink/src/routes.ts +0 -48
- package/examples/kitchen-sink/src/sdk.ts +0 -12
- package/examples/kitchen-sink/src/seed.ts +0 -10
- package/examples/kitchen-sink/src/server.ts +0 -22
- package/examples/kitchen-sink/src/stdio.ts +2 -12
- package/examples/kitchen-sink/src/workspaces/Workspaces.ts +0 -4
- package/examples/kitchen-sink/src/workspaces/handlers.ts +0 -4
- package/examples/kitchen-sink/src/workspaces/registry.ts +0 -5
- package/examples/kitchen-sink/src/workspaces/tenant.ts +0 -4
- package/examples/kitchen-sink/test/admin/admin.test.ts +0 -1
- package/examples/kitchen-sink/test/assistant/assistant.test.ts +0 -18
- package/examples/kitchen-sink/test/attachments/attachments.test.ts +0 -9
- package/examples/kitchen-sink/test/identity/identity.test.ts +0 -10
- package/examples/kitchen-sink/test/identity/session.test.ts +0 -10
- package/examples/kitchen-sink/test/issues/assistant.test.ts +3 -7
- package/examples/kitchen-sink/test/issues/events.test.ts +2 -9
- package/examples/kitchen-sink/test/issues/export.test.ts +0 -3
- package/examples/kitchen-sink/test/issues/issues.test.ts +6 -12
- package/examples/kitchen-sink/test/request-policy.test.ts +0 -6
- package/examples/kitchen-sink/test/routes.test.ts +0 -6
- package/examples/kitchen-sink/test/support/as.ts +3 -5
- package/examples/kitchen-sink/test/support/scripted.ts +0 -3
- package/examples/kitchen-sink/test/support/services.ts +0 -3
- package/examples/live-rpc/src/Rpc.ts +0 -4
- package/examples/live-rpc/src/browser.ts +0 -24
- package/examples/live-rpc/src/chat/Chat.ts +0 -22
- package/examples/live-rpc/src/chat/authorize.ts +0 -3
- package/examples/live-rpc/src/chat/events.ts +0 -14
- package/examples/live-rpc/src/chat/handlers.ts +0 -5
- package/examples/live-rpc/src/chat/room.ts +0 -6
- package/examples/live-rpc/src/chat/store.ts +0 -9
- package/examples/live-rpc/src/client.ts +0 -6
- package/examples/live-rpc/src/identity/Identity.ts +0 -6
- package/examples/live-rpc/src/identity/members.ts +1 -3
- package/examples/live-rpc/src/identity/verifier.ts +6 -12
- package/examples/live-rpc/src/page.ts +0 -10
- package/examples/live-rpc/src/routes.ts +0 -13
- package/examples/live-rpc/src/server.ts +0 -16
- package/examples/live-rpc/test/browser.test.ts +0 -9
- package/examples/live-rpc/test/chat/chat.test.ts +3 -11
- package/examples/live-rpc/test/routes.test.ts +0 -8
- package/examples/live-rpc/test/server.test.ts +0 -13
- package/examples/mcp-tools/src/instructions.ts +0 -4
- package/examples/mcp-tools/src/platform/fake-farm.ts +0 -6
- package/examples/mcp-tools/src/platform/print-farm.ts +0 -16
- package/examples/mcp-tools/src/printers/Printers.ts +0 -21
- package/examples/mcp-tools/src/printers/farm.ts +0 -5
- package/examples/mcp-tools/src/printers/handlers.ts +0 -6
- package/examples/mcp-tools/src/routes.ts +0 -11
- package/examples/mcp-tools/src/server.ts +0 -9
- package/examples/mcp-tools/src/stdio.ts +1 -13
- package/examples/mcp-tools/test/printers/printers.test.ts +0 -10
- package/examples/mcp-tools/test/routes.test.ts +0 -14
- package/examples/notes-service/src/Api.ts +0 -4
- package/examples/notes-service/src/browser.ts +0 -12
- package/examples/notes-service/src/cli.ts +0 -2
- package/examples/notes-service/src/commands.ts +0 -30
- package/examples/notes-service/src/identity/Identity.ts +0 -2
- package/examples/notes-service/src/identity/authorize.ts +0 -4
- package/examples/notes-service/src/identity/users.ts +1 -3
- package/examples/notes-service/src/identity/verifier.ts +4 -11
- package/examples/notes-service/src/notes/Notes.ts +0 -12
- package/examples/notes-service/src/notes/handlers.ts +0 -4
- package/examples/notes-service/src/notes/store.ts +62 -48
- package/examples/notes-service/src/routes.ts +0 -12
- package/examples/notes-service/src/server.ts +0 -6
- package/examples/notes-service/src/status/Status.ts +0 -1
- package/examples/notes-service/src/status/handlers.ts +0 -1
- package/examples/notes-service/src/stdio.ts +2 -12
- package/examples/notes-service/src/worker.ts +0 -7
- package/examples/notes-service/test/commands.test.ts +0 -20
- package/examples/notes-service/test/notes/as.ts +3 -4
- package/examples/notes-service/test/notes/in-process.test.ts +3 -6
- package/examples/notes-service/test/notes/notes.test.ts +0 -6
- package/examples/notes-service/test/routes.test.ts +0 -5
- package/examples/notes-service/test/worker.test.ts +0 -1
- package/examples/quickstart/src/Api.ts +0 -1
- package/examples/quickstart/src/client.ts +0 -3
- package/examples/quickstart/src/greeting/Greeting.ts +0 -2
- package/examples/quickstart/src/greeting/handlers.ts +0 -1
- package/examples/quickstart/src/routes.ts +0 -1
- package/examples/quickstart/src/server.ts +3 -6
- package/examples/quickstart/test/routes.test.ts +0 -1
- package/package.json +2 -1
package/dist/mcp/reply.mjs
CHANGED
|
@@ -70,10 +70,6 @@ const callError = (method, target, reason) => new McpCallError({
|
|
|
70
70
|
message: `MCP ${method} "${target}" ${sentenceOf(reason)}`
|
|
71
71
|
});
|
|
72
72
|
const isEventStream = (response) => (response.headers["content-type"] ?? "").startsWith("text/event-stream");
|
|
73
|
-
/**
|
|
74
|
-
* The data of each frame of the event stream `response` is, as it arrives: more than the parser
|
|
75
|
-
* holds with no frame ending fails `method` on `name` with `FrameTooLarge`.
|
|
76
|
-
*/
|
|
77
73
|
const framesOf = (method, name, response) => Stream.suspend(() => {
|
|
78
74
|
const data = [];
|
|
79
75
|
const parser = Sse.makeParser((event) => {
|
|
@@ -84,16 +80,9 @@ const framesOf = (method, name, response) => Stream.suspend(() => {
|
|
|
84
80
|
return error === void 0 ? Effect.succeed(data.splice(0)) : Effect.fail(callError(method, name, new FrameTooLarge({ maxEventSize: error.reason.maxEventSize })));
|
|
85
81
|
}), Stream.flattenIterable);
|
|
86
82
|
});
|
|
87
|
-
/**
|
|
88
|
-
* The messages of `response`, read as it arrives: the data of each frame of an event stream, or
|
|
89
|
-
* the body itself as one. Each `notifications/progress` frame hands its value to `sink` and is
|
|
90
|
-
* left out, so a long call holds the frames its reply is read from, not every value it reported.
|
|
91
|
-
* A stateless request's notifications are its own.
|
|
92
|
-
*/
|
|
93
83
|
const bodyOf = (method, name, response, sink) => isEventStream(response) ? Stream.runCollect(Stream.filterEffect(framesOf(method, name, response), (data) => Option.match(progressOf(data), {
|
|
94
84
|
onNone: () => Effect.succeed(true),
|
|
95
85
|
onSome: (value) => sink === void 0 ? Effect.succeed(false) : Effect.as(sink.progress(value), false)
|
|
96
86
|
}))) : Effect.map(response.text, (text) => [text]);
|
|
97
|
-
/** Fails with the error of `errors` that `text` decodes as, or with `otherwise`. */
|
|
98
87
|
const failWith = (errors, text, otherwise) => Schema.decodeEffect(Schema.fromJsonString(Schema.toCodecJson(Schema.Union(errors))))(text).pipe(Effect.mapError(() => otherwise), Effect.flatMap(Effect.fail));
|
|
99
88
|
export { FrameTooLarge, McpCallError, NoReply, NoStructuredContent, ProtocolError, StatusError, UndeclaredError, bodyOf, callError, failWith, framesOf, isEventStream };
|
package/dist/mcp/request.mjs
CHANGED
|
@@ -2,7 +2,6 @@ import { defaultPath, httpProtocol } from "./protocol.mjs";
|
|
|
2
2
|
import { Predicate } from "effect";
|
|
3
3
|
import { HttpClientRequest } from "effect/http";
|
|
4
4
|
// src/mcp/request.ts
|
|
5
|
-
/** The routing headers and JSON-RPC body of the request `request` builds. */
|
|
6
5
|
const statelessRequest = (method, params) => {
|
|
7
6
|
const { protocolVersion } = httpProtocol;
|
|
8
7
|
const name = method === "resources/read" ? params["uri"] : params["name"];
|
package/dist/mcp/watch.mjs
CHANGED
|
@@ -12,7 +12,6 @@ import { McpCallError, NoReply, ProtocolError, StatusError, bodyOf, callError, f
|
|
|
12
12
|
import { request } from "./request.mjs";
|
|
13
13
|
import { Duration, Effect, Match, Option, Predicate, Schema, Stream } from "effect";
|
|
14
14
|
// src/mcp/watch.ts
|
|
15
|
-
/** Reads one message as a request's reply: its result, by `success`, or its error. */
|
|
16
15
|
const replyOf = (success) => Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Union([Schema.Struct({ result: success }), Schema.Struct({ error: WireError })])));
|
|
17
16
|
const pollReplyOf = replyOf(PollEvents.successSchema);
|
|
18
17
|
const Frame = Schema.Union([
|
|
@@ -35,10 +34,6 @@ const encodeTarget = Schema.encodeSync(WebhookTarget);
|
|
|
35
34
|
const transientReason = (reason) => Match.value(reason).pipe(Match.tag("StatusError", ({ status }) => isTransientStatus(status)), Match.tag("ProtocolError", ({ code }) => code === codes.internal), Match.orElse(() => false));
|
|
36
35
|
const isMcpCallError = Schema.is(McpCallError);
|
|
37
36
|
const transient = Predicate.or(isTransient, Predicate.compose(isMcpCallError, ({ reason }) => transientReason(reason)));
|
|
38
|
-
/**
|
|
39
|
-
* The transport of one subscription over an MCP endpoint served by `layerHttp`: `events/poll`
|
|
40
|
-
* for each page, and `events/stream` read as it arrives, each failing as the endpoint answered.
|
|
41
|
-
*/
|
|
42
37
|
const transportOf = ({ http, url, event, refusedWith }, input) => {
|
|
43
38
|
const { name } = event;
|
|
44
39
|
const untagged = untaggedOccurrenceOf(event);
|
|
@@ -55,7 +50,6 @@ const transportOf = ({ http, url, event, refusedWith }, input) => {
|
|
|
55
50
|
status: response.status,
|
|
56
51
|
body
|
|
57
52
|
})))));
|
|
58
|
-
/** The result of one `method` request, or what its error reply fails with. */
|
|
59
53
|
const answered = (method, params, decodeReply) => Effect.gen(function* () {
|
|
60
54
|
const messages = yield* bodyOf(method, name, yield* sent(method, params));
|
|
61
55
|
const reply = messages.flatMap((message) => Option.toArray(decodeReply(message))).at(-1);
|
package/dist/rpc/watch.mjs
CHANGED
|
@@ -12,17 +12,11 @@ const isAnsweredReason = Schema.is(Schema.Union([
|
|
|
12
12
|
Schema.instanceOf(HttpClientError.DecodeError)
|
|
13
13
|
]));
|
|
14
14
|
const cutResponses = ["HTTP response ended before RPC request completed", "Received empty HTTP response"];
|
|
15
|
-
/** Whether a watch's failure over an rpc protocol is one a reconnect may outlast. */
|
|
16
15
|
const isRpcTransient = Predicate.or(Cause.isTimeoutError, (error) => isRpcClientError(error) && Match.value(error.reason).pipe(Match.tag("SocketReadError", "SocketWriteError", "SocketOpenError", () => true), Match.tag("SocketCloseError", ({ code }) => code < 4e3 || code > 4999), Match.tag("HttpError", ({ cause }) => isAnsweredReason(cause) && isTransientReason(cause)), Match.tag("RpcClientDefect", ({ message }) => cutResponses.some((fragment) => message.includes(fragment))), Match.orElse(() => false)));
|
|
17
16
|
const isUnknownTag = (tag) => {
|
|
18
17
|
const message = `Unknown request tag: ${tag}`;
|
|
19
18
|
return (cause) => cause.reasons.some((reason) => Cause.isDieReason(reason) && (Predicate.hasProperty(reason.defect, "message") ? reason.defect.message === message : reason.defect === message));
|
|
20
19
|
};
|
|
21
|
-
/**
|
|
22
|
-
* The transport of one subscription to `event` over its stream rpc: the stream alone, since an
|
|
23
|
-
* rpc serves no poll, failing as `NotServed` where the server serves no such rpc, and once it goes
|
|
24
|
-
* silent where `silenceEnds`.
|
|
25
|
-
*/
|
|
26
20
|
const transportOf = (event, stream, silenceEnds) => {
|
|
27
21
|
const notServed = Stream.fail(SubscriptionError.of(event.name, new NotServed()));
|
|
28
22
|
const unknownTag = isUnknownTag(event.name);
|
package/dist/stdio/console.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Clock, Console, Effect, Function, Predicate } from "effect";
|
|
2
2
|
// src/stdio/console.ts
|
|
3
|
-
const
|
|
3
|
+
const stderrOnlyConsole = (console, clock) => {
|
|
4
4
|
const counts = /* @__PURE__ */ new Map();
|
|
5
5
|
const timers = /* @__PURE__ */ new Map();
|
|
6
6
|
const write = (...args) => {
|
|
@@ -67,5 +67,5 @@ const stderrConsole = (console, clock) => {
|
|
|
67
67
|
warn: write
|
|
68
68
|
};
|
|
69
69
|
};
|
|
70
|
-
const withStderrConsole = (effect) => Effect.provideServiceEffect(effect, Console.Console, Effect.zipWith(Effect.service(Console.Console), Effect.service(Clock.Clock),
|
|
70
|
+
const withStderrConsole = (effect) => Effect.provideServiceEffect(effect, Console.Console, Effect.zipWith(Effect.service(Console.Console), Effect.service(Clock.Clock), stderrOnlyConsole));
|
|
71
71
|
export { withStderrConsole };
|
package/dist/webhook/address.mjs
CHANGED
|
@@ -31,17 +31,12 @@ const notGlobal = [
|
|
|
31
31
|
"fe80::/10",
|
|
32
32
|
"ff00::/8"
|
|
33
33
|
].map(IpNetwork.fromStringUnsafe);
|
|
34
|
-
/** Whether `address` is globally reachable, in no special-purpose range. */
|
|
35
34
|
const isGlobal = (address) => !notGlobal.some((network) => IpNetwork.contains(network, address));
|
|
36
|
-
/**
|
|
37
|
-
* Checks the host of `url`, failing unless every address it names is globally reachable: a
|
|
38
|
-
* literal address is checked as it is, a name through `dns`, or passed where there is none, and a
|
|
39
|
-
* scoped IPv6 literal refused.
|
|
40
|
-
*/
|
|
41
35
|
const checkedHost = (url, dns) => {
|
|
42
36
|
const host = Result.getOrUndefined(Host.hostFromString(url.hostname.replace(/^\[(?<address>.*)\]$/u, "$<address>")));
|
|
43
|
-
if (
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
if (NetAddress.isIpAddress(host)) return isGlobal(host) ? Effect.void : Effect.fail(void 0);
|
|
38
|
+
if (!Host.isDomainName(host)) return Effect.fail(void 0);
|
|
39
|
+
if (dns === void 0) return Effect.void;
|
|
40
|
+
return Effect.flatMap(Effect.mapError(dns.lookup(host), Function.constUndefined), (resolved) => resolved.every(isGlobal) ? Effect.void : Effect.fail(void 0));
|
|
46
41
|
};
|
|
47
42
|
export { checkedHost };
|
package/dist/webhook/attempt.mjs
CHANGED
|
@@ -2,10 +2,6 @@ import { signature } from "./signing.mjs";
|
|
|
2
2
|
import { Clock, Data, Duration, Effect, Option, Schema, Stream } from "effect";
|
|
3
3
|
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
|
|
4
4
|
// src/webhook/attempt.ts
|
|
5
|
-
/**
|
|
6
|
-
* One attempt of a delivery that failed: `category` is SEP-3415's for it, and `final` says a
|
|
7
|
-
* retry would not help, as for a `410 Gone` or a `413 Payload Too Large`.
|
|
8
|
-
*/
|
|
9
5
|
var AttemptFailed = class extends Data.TaggedError("AttemptFailed") {};
|
|
10
6
|
const timeout = Duration.seconds(5);
|
|
11
7
|
const statusCategory = (status) => status >= 500 ? "http_5xx" : "http_4xx";
|
|
@@ -39,14 +35,9 @@ const sent = ({ url, subscriptionId, webhookId, body, keys }, checked, answer) =
|
|
|
39
35
|
}))
|
|
40
36
|
}), Effect.scoped);
|
|
41
37
|
});
|
|
42
|
-
/**
|
|
43
|
-
* POSTs `body` to the callback, signed with a fresh timestamp, once `checked`, its address check,
|
|
44
|
-
* passes, and succeeds at a 2xx without reading its body. The timeout bounds the check and the
|
|
45
|
-
* POST together.
|
|
46
|
-
*/
|
|
47
38
|
const attempt = (posting, checked) => sent(posting, checked, () => Effect.void);
|
|
48
39
|
const Challenged = Schema.fromJsonString(Schema.Struct({ challenge: Schema.String }));
|
|
49
|
-
const
|
|
40
|
+
const challengeAnswerByteLimit = 4096;
|
|
50
41
|
const textOf = (response) => Effect.suspend(() => {
|
|
51
42
|
const decoder = new TextDecoder();
|
|
52
43
|
return response.stream.pipe(Stream.scan(() => ({
|
|
@@ -55,11 +46,7 @@ const textOf = (response) => Effect.suspend(() => {
|
|
|
55
46
|
}), ({ bytes, text }, chunk) => ({
|
|
56
47
|
bytes: bytes + chunk.length,
|
|
57
48
|
text: text + decoder.decode(chunk, { stream: true })
|
|
58
|
-
})), Stream.takeUntil(({ bytes }) => bytes >
|
|
49
|
+
})), Stream.takeUntil(({ bytes }) => bytes > challengeAnswerByteLimit), Stream.runLast, Effect.map((last) => Option.map(Option.filter(last, ({ bytes }) => bytes <= challengeAnswerByteLimit), ({ text }) => text + decoder.decode())), Effect.orElseSucceed(() => Option.none()));
|
|
59
50
|
});
|
|
60
|
-
/**
|
|
61
|
-
* `attempt` of a verification: the challenge its 2xx answer echoes, where its first 4 KiB hold
|
|
62
|
-
* one.
|
|
63
|
-
*/
|
|
64
51
|
const challenged = (posting, checked) => sent(posting, checked, (response) => Effect.map(textOf(response), (text) => Option.getOrUndefined(Option.map(Option.flatMap(text, Schema.decodeUnknownOption(Challenged)), ({ challenge }) => challenge))));
|
|
65
52
|
export { AttemptFailed, attempt, challenged };
|
|
@@ -5,18 +5,12 @@ import { Clock, Effect, Option, Predicate, Schema } from "effect";
|
|
|
5
5
|
import { HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
6
6
|
import { Base64, Base64Url } from "effect/encoding";
|
|
7
7
|
// src/webhook/receiver.ts
|
|
8
|
-
|
|
9
|
-
const tolerance = 300;
|
|
8
|
+
const timestampToleranceSeconds = 300;
|
|
10
9
|
const dedupeWindow = 1e3;
|
|
11
10
|
const Body = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Json));
|
|
12
11
|
const decodeBody = Schema.decodeUnknownOption(Body);
|
|
13
12
|
const gone = HttpServerResponse.empty({ status: 410 });
|
|
14
13
|
const unauthorized = HttpServerResponse.empty({ status: 401 });
|
|
15
|
-
/**
|
|
16
|
-
* The receiver of one `Webhook.layerReceiver` at `path`: `receiving`, where each watch keeps its
|
|
17
|
-
* place under an id and a secret of its own, and `handler`, which verifies a POST against the
|
|
18
|
-
* secret of the watch its URL names and hands its body to that watch.
|
|
19
|
-
*/
|
|
20
14
|
const makeReceiver = (path) => Effect.sync(() => {
|
|
21
15
|
const places = /* @__PURE__ */ new Map();
|
|
22
16
|
const register = (deliver) => Effect.acquireRelease(Effect.sync(() => {
|
|
@@ -44,7 +38,7 @@ const makeReceiver = (path) => Effect.sync(() => {
|
|
|
44
38
|
const timestamp = Number(request.headers["webhook-timestamp"]);
|
|
45
39
|
const signatures = (request.headers["webhook-signature"] ?? "").split(" ");
|
|
46
40
|
const now = Math.floor((yield* Clock.currentTimeMillis) / 1e3);
|
|
47
|
-
if (id === void 0 || !Number.isInteger(timestamp) || Math.abs(now - timestamp) >
|
|
41
|
+
if (id === void 0 || !Number.isInteger(timestamp) || Math.abs(now - timestamp) > timestampToleranceSeconds) return unauthorized;
|
|
48
42
|
const text = yield* Effect.orElseSucceed(request.text, () => "");
|
|
49
43
|
const expected = yield* signature([place.key], id, timestamp, text);
|
|
50
44
|
if (!signatures.some((each) => constantTimeEqual(each, expected))) return unauthorized;
|
package/dist/webhook/signing.mjs
CHANGED
|
@@ -15,11 +15,8 @@ const hmac = (key, content) => Effect.promise(async () => {
|
|
|
15
15
|
* under each key, space apart, so a receiver mid-rotation verifies under either.
|
|
16
16
|
*/
|
|
17
17
|
const signature = (keys, id, timestamp, body) => Effect.map(Effect.forEach(keys, (key) => hmac(key, `${id}.${timestamp}.${body}`)), (signed) => signed.map((bytes) => `v1,${Base64.encode(bytes)}`).join(" "));
|
|
18
|
-
/** `sub_` and the first 16 bytes of the SHA-256 of `key`, in hex: a subscription's id. */
|
|
19
18
|
const subscriptionId = (key) => Effect.map(sha256Hex(key), (hex) => `sub_${hex.slice(0, 32)}`);
|
|
20
|
-
/** 16 random bytes in hex, for a challenge or an envelope's `webhook-id`. */
|
|
21
19
|
const randomToken = Effect.sync(() => Hex.encode(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))));
|
|
22
|
-
/** Whether `a` and `b` are equal, in a time that does not depend on where they differ. */
|
|
23
20
|
const constantTimeEqual = (a, b) => {
|
|
24
21
|
const left = encoder.encode(a);
|
|
25
22
|
const right = encoder.encode(b);
|
package/dist/webhook/stored.mjs
CHANGED
|
@@ -4,12 +4,6 @@ import { DeliveryStatus } from "../contract/subscribe.mjs";
|
|
|
4
4
|
import { Duration, Effect, Option, Queue, Schema, Semaphore } from "effect";
|
|
5
5
|
import { KeyValueStore } from "effect/persistence";
|
|
6
6
|
// src/webhook/stored.ts
|
|
7
|
-
/**
|
|
8
|
-
* A webhook subscription as `persist` keeps it, enough to start delivering again after a
|
|
9
|
-
* restart: its callback, event, encoded input and identity, secret, expiry, watermark and
|
|
10
|
-
* status. Its callback and secret are decoded again as it is read, as a request's are, and its
|
|
11
|
-
* subscriber's principal is derived again from the identity.
|
|
12
|
-
*/
|
|
13
7
|
const Stored = Schema.Struct({
|
|
14
8
|
url: CallbackUrl,
|
|
15
9
|
event: Schema.String,
|
|
@@ -23,13 +17,7 @@ const Stored = Schema.Struct({
|
|
|
23
17
|
const prefix = "effect-actions/webhook";
|
|
24
18
|
const Index = Schema.Array(Schema.String);
|
|
25
19
|
const flushTimeout = Duration.seconds(5);
|
|
26
|
-
|
|
27
|
-
* A storage over `store`. Its writes run on a fiber of their own, never in the request or the
|
|
28
|
-
* delivery that marked them, so a slow or failing store holds up neither: a failure is reported,
|
|
29
|
-
* and written again with the next mark or as the layer closes.
|
|
30
|
-
* A store whose index cannot be read fails the layer as it builds.
|
|
31
|
-
*/
|
|
32
|
-
const makeStorage = (store) => Effect.gen(function* () {
|
|
20
|
+
const makeWriteBehindStorage = (store) => Effect.gen(function* () {
|
|
33
21
|
const records = KeyValueStore.toSchemaStore(KeyValueStore.prefix(store, `${prefix}/`), Stored);
|
|
34
22
|
const index = KeyValueStore.toSchemaStore(store, Index);
|
|
35
23
|
let indexed = new Set(yield* Effect.orDie(Effect.map(index.get(prefix), Option.getOrElse(() => []))));
|
|
@@ -39,10 +27,13 @@ const makeStorage = (store) => Effect.gen(function* () {
|
|
|
39
27
|
const listed = (next) => Effect.andThen(index.set(prefix, [...next]), Effect.sync(() => {
|
|
40
28
|
indexed = next;
|
|
41
29
|
}));
|
|
42
|
-
const
|
|
43
|
-
const
|
|
30
|
+
const removedThenUnlisted = (id) => Effect.andThen(records.remove(id), Effect.suspend(() => indexed.has(id) ? listed(new Set([...indexed].filter((each) => each !== id))) : Effect.void));
|
|
31
|
+
const listedThenKept = (id, record) => Effect.andThen(Effect.suspend(() => indexed.has(id) ? Effect.void : listed(/* @__PURE__ */ new Set([...indexed, id]))), records.set(id, record));
|
|
32
|
+
const written = (id, record) => record === void 0 ? removedThenUnlisted(id) : listedThenKept(id, record);
|
|
33
|
+
const clearedUnlessRemarked = (id, record) => Effect.sync(() => {
|
|
44
34
|
if (pending.get(id) === record) pending.delete(id);
|
|
45
|
-
})
|
|
35
|
+
});
|
|
36
|
+
const flushed = writing.withPermits(1)(Effect.suspend(() => Effect.forEach([...pending], ([id, record]) => written(id, record).pipe(Effect.andThen(clearedUnlessRemarked(id, record)), Effect.catchCause(reported)), { discard: true })));
|
|
46
37
|
yield* Effect.forkScoped(Effect.forever(Effect.andThen(Queue.take(marked), flushed)));
|
|
47
38
|
yield* Effect.addFinalizer(() => flushed.pipe(Effect.timeout(flushTimeout), Effect.ignore));
|
|
48
39
|
return {
|
|
@@ -54,4 +45,4 @@ const makeStorage = (store) => Effect.gen(function* () {
|
|
|
54
45
|
get: (id) => pending.has(id) ? Effect.succeed(Option.fromUndefinedOr(pending.get(id))) : records.get(id)
|
|
55
46
|
};
|
|
56
47
|
});
|
|
57
|
-
export { Stored,
|
|
48
|
+
export { Stored, makeWriteBehindStorage };
|
|
@@ -14,9 +14,9 @@ const minTtl = Duration.minutes(1);
|
|
|
14
14
|
const rotationGrace = Duration.minutes(5);
|
|
15
15
|
const maxBody = 262144;
|
|
16
16
|
const encoder = new TextEncoder();
|
|
17
|
-
const
|
|
17
|
+
const terminatedEnvelopeRetries = Schedule.exponential(Duration.seconds(1)).pipe(Schedule.upTo({ times: 3 }));
|
|
18
18
|
const iso = (millis) => DateTime.formatIso(DateTime.makeUnsafe(millis));
|
|
19
|
-
const
|
|
19
|
+
const resumedWithFreshBudget = (status) => status.active ? status : {
|
|
20
20
|
...Struct.omit(status, ["failedSince"]),
|
|
21
21
|
active: true
|
|
22
22
|
};
|
|
@@ -27,11 +27,6 @@ const grantOf = (ttlMs, policy) => Duration.clamp(ttlMs === null || ttlMs === vo
|
|
|
27
27
|
const trusted = (url, prefixes) => prefixes.some(({ origin, pathname }) => url.origin === origin && (url.pathname === pathname || url.pathname.startsWith(pathname.endsWith("/") ? pathname : `${pathname}/`)));
|
|
28
28
|
const snapshotOf = Context.omit(Scope.Scope, HttpServerRequest.HttpServerRequest, HttpServerRequest.ParsedSearchParams, HttpRouter.RouteContext);
|
|
29
29
|
const unencoded = Effect.die(/* @__PURE__ */ new Error("A webhook subscriber's identity did not encode by its authentication's schema (give Authentication.make a schema that encodes every identity its verifier provides)"));
|
|
30
|
-
/**
|
|
31
|
-
* The webhook subscriptions of one `Webhook.layer`: each a worker reading its feed as the
|
|
32
|
-
* subscriber and POSTing what it reads to the callback, until it expires, is unsubscribed, or
|
|
33
|
-
* is refused.
|
|
34
|
-
*/
|
|
35
30
|
const confirmedAt = (cursor) => Effect.tap(Deferred.make(), (confirmation) => Deferred.succeed(confirmation, {
|
|
36
31
|
cursor,
|
|
37
32
|
truncated: false
|
|
@@ -62,11 +57,12 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
62
57
|
const locks = yield* RcMap.make({ lookup: (_id) => Semaphore.make(1) });
|
|
63
58
|
const retryForMillis = Duration.toMillis(policy.retryFor);
|
|
64
59
|
const failingFor = ({ status }) => Effect.map(Clock.currentTimeMillis, (now) => status.failedSince === void 0 ? 0 : now - Date.parse(status.failedSince));
|
|
60
|
+
const waitingAsLongAsFailing = (subscription) => Schedule.modifyDelay(Schedule.forever, () => Effect.map(failingFor(subscription), (failing) => Duration.clamp(Duration.millis(failing), {
|
|
61
|
+
minimum: Duration.seconds(1),
|
|
62
|
+
maximum: Duration.minutes(5)
|
|
63
|
+
})));
|
|
65
64
|
const retrying = (subscription) => ({
|
|
66
|
-
schedule:
|
|
67
|
-
minimum: Duration.seconds(1),
|
|
68
|
-
maximum: Duration.minutes(5)
|
|
69
|
-
}))),
|
|
65
|
+
schedule: waitingAsLongAsFailing(subscription),
|
|
70
66
|
while: (failure) => failure.final ? Effect.succeed(false) : Effect.map(failingFor(subscription), (failing) => failing < retryForMillis)
|
|
71
67
|
});
|
|
72
68
|
const locked = (id) => (effect) => Effect.scoped(Effect.flatMap(RcMap.get(locks, id), (lock) => lock.withPermits(1)(effect)));
|
|
@@ -97,12 +93,12 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
97
93
|
active: false
|
|
98
94
|
};
|
|
99
95
|
}), kept(subscription)), Effect.never);
|
|
100
|
-
const
|
|
96
|
+
const abandonedResettingBudget = (subscription) => Effect.andThen(Effect.sync(() => {
|
|
101
97
|
subscription.status = Struct.omit(subscription.status, ["failedSince"]);
|
|
102
98
|
}), kept(subscription));
|
|
103
99
|
const delivered = (subscription, webhookId, body) => post(subscription, webhookId, body).pipe(Effect.tapError((failure) => recorded(subscription, failure)), Effect.retry(retrying(subscription)), Effect.matchEffect({
|
|
104
100
|
onSuccess: () => Effect.as(recorded(subscription, void 0), true),
|
|
105
|
-
onFailure: (failure) => failure.final ? Effect.as(
|
|
101
|
+
onFailure: (failure) => failure.final ? Effect.as(abandonedResettingBudget(subscription), false) : suspended(subscription)
|
|
106
102
|
}));
|
|
107
103
|
const gapped = (subscription, cursor) => Effect.flatMap(randomToken, (token) => delivered(subscription, `msg_gap_${token}`, JSON.stringify({
|
|
108
104
|
type: "gap",
|
|
@@ -141,7 +137,7 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
141
137
|
type: "terminated",
|
|
142
138
|
error: sentError(refusal)
|
|
143
139
|
})).pipe(Effect.retry({
|
|
144
|
-
schedule:
|
|
140
|
+
schedule: terminatedEnvelopeRetries,
|
|
145
141
|
while: (failure) => !failure.final
|
|
146
142
|
}), Effect.ignore));
|
|
147
143
|
};
|
|
@@ -179,7 +175,7 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
179
175
|
}, checkedHost(url, policy.dns)), built).pipe(Effect.mapError(({ category }) => failedCallback(feed, category)))) !== challenge) return yield* failedCallback(feed, "challenge_failed");
|
|
180
176
|
});
|
|
181
177
|
};
|
|
182
|
-
const
|
|
178
|
+
const swapHeldWorker = (id, next, worker) => Effect.uninterruptible(Effect.gen(function* () {
|
|
183
179
|
const previous = yield* FiberMap.get(workers, id);
|
|
184
180
|
if (next === void 0) held.delete(id);
|
|
185
181
|
else held.set(id, next);
|
|
@@ -197,10 +193,10 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
197
193
|
key: subscription.secret.key,
|
|
198
194
|
until: now + Duration.toMillis(rotationGrace)
|
|
199
195
|
} },
|
|
200
|
-
status:
|
|
196
|
+
status: resumedWithFreshBudget(status)
|
|
201
197
|
};
|
|
202
198
|
const confirmation = yield* confirmedAt(watermark);
|
|
203
|
-
yield*
|
|
199
|
+
yield* swapHeldWorker(renewed.id, renewed, workerOf(renewed, input, watermark, confirmation));
|
|
204
200
|
yield* remembered(renewed, now);
|
|
205
201
|
yield* kept(renewed);
|
|
206
202
|
return {
|
|
@@ -262,7 +258,7 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
262
258
|
watermark: record.watermark
|
|
263
259
|
});
|
|
264
260
|
const confirmation = yield* confirmedAt(record.watermark);
|
|
265
|
-
yield*
|
|
261
|
+
yield* swapHeldWorker(current, subscription, record.status.active ? workerOf(subscription, input, record.watermark, confirmation) : Effect.andThen(expired(subscription), finished(subscription)));
|
|
266
262
|
if (current !== id) yield* kept(subscription);
|
|
267
263
|
}).pipe(Effect.catchCause(reported));
|
|
268
264
|
const serve = (feeds, keyed) => Effect.gen(function* () {
|
|
@@ -279,10 +275,10 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
279
275
|
if (storage === void 0 || schema === void 0) return;
|
|
280
276
|
for (const id of storage.ids()) yield* locked(id)(Effect.suspend(() => held.has(id) ? Effect.void : restoredFrom(storage, feeds, keyed, schema, id)));
|
|
281
277
|
});
|
|
282
|
-
const
|
|
278
|
+
const terminatedOnRefusal = (id, cause) => Effect.gen(function* () {
|
|
283
279
|
const existing = held.get(id);
|
|
284
280
|
if (existing !== void 0 && refusalIn(cause) !== void 0 && (yield* Clock.currentTimeMillis) < existing.expiresAt) {
|
|
285
|
-
yield*
|
|
281
|
+
yield* swapHeldWorker(id, void 0, terminated(existing, cause));
|
|
286
282
|
yield* dropped(id);
|
|
287
283
|
}
|
|
288
284
|
return yield* Effect.failCause(cause);
|
|
@@ -291,7 +287,7 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
291
287
|
const { feed, principal, input, delivery: { url, secret }, cursor, ttlMs } = subscribing;
|
|
292
288
|
const id = yield* idOf(subscribing);
|
|
293
289
|
return yield* locked(id)(Effect.gen(function* () {
|
|
294
|
-
yield* Effect.catchCause(feed.admit(input), (cause) =>
|
|
290
|
+
yield* Effect.catchCause(feed.admit(input), (cause) => terminatedOnRefusal(id, cause));
|
|
295
291
|
const now = yield* Clock.currentTimeMillis;
|
|
296
292
|
const granted = {
|
|
297
293
|
secret,
|
|
@@ -317,7 +313,7 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
317
313
|
yield* verify(subscription, now);
|
|
318
314
|
yield* remembered(subscription, now);
|
|
319
315
|
const confirmation = yield* Deferred.make();
|
|
320
|
-
yield*
|
|
316
|
+
yield* swapHeldWorker(id, subscription, workerOf(subscription, input, cursor, confirmation));
|
|
321
317
|
const started = yield* Deferred.await(confirmation);
|
|
322
318
|
yield* kept(subscription);
|
|
323
319
|
return {
|
|
@@ -329,7 +325,7 @@ const makeWebhooks = (policy) => Effect.gen(function* () {
|
|
|
329
325
|
}));
|
|
330
326
|
});
|
|
331
327
|
const unsubscribe = (subscription) => Effect.flatMap(idOf(subscription), (id) => locked(id)(Effect.gen(function* () {
|
|
332
|
-
if ((yield* Clock.currentTimeMillis) < (held.get(id)?.expiresAt ?? 0)) return yield* Effect.andThen(
|
|
328
|
+
if ((yield* Clock.currentTimeMillis) < (held.get(id)?.expiresAt ?? 0)) return yield* Effect.andThen(swapHeldWorker(id, void 0, Effect.void), dropped(id));
|
|
333
329
|
if (storage?.ids().includes(id) === true) return yield* dropped(id);
|
|
334
330
|
return yield* SubscriptionError.of(subscription.feed.event.name, new NotSubscribed());
|
|
335
331
|
})));
|
package/docs/Action-streams.md
CHANGED
|
@@ -22,8 +22,6 @@ import {
|
|
|
22
22
|
import type { Build } from "./Builds.js";
|
|
23
23
|
import { BuildStore } from "./store.js";
|
|
24
24
|
|
|
25
|
-
// DEMO ONLY: each step passes at once. A runner's steps take their time, which is why a caller
|
|
26
|
-
// wants to see them.
|
|
27
25
|
const steps = ["install", "build", "test"];
|
|
28
26
|
|
|
29
27
|
export const builds = Action.implement(
|
|
@@ -32,9 +30,6 @@ export const builds = Action.implement(
|
|
|
32
30
|
const store = yield* BuildStore;
|
|
33
31
|
const events = yield* Event.Events;
|
|
34
32
|
|
|
35
|
-
// `build.finished` is a check source over the store: write the build there first, then wake
|
|
36
|
-
// its subscribers, who read it now rather than at their next poll. `pipeline.status` is a
|
|
37
|
-
// current source: the notify moves its pipeline's revision, so its subscribers alone read it.
|
|
38
33
|
const finished = (build: Effect.Effect<Build>) =>
|
|
39
34
|
Effect.tap(build, ({ pipeline }) =>
|
|
40
35
|
Effect.andThen(
|
|
@@ -46,7 +41,6 @@ export const builds = Action.implement(
|
|
|
46
41
|
return {
|
|
47
42
|
finishBuild: ({ pipeline, status }) =>
|
|
48
43
|
finished(store.finish(pipeline, status, [status])),
|
|
49
|
-
// `progress` never waits for the caller: the build runs at its own pace.
|
|
50
44
|
startBuild: ({ pipeline }, { progress }) =>
|
|
51
45
|
Effect.andThen(
|
|
52
46
|
Effect.forEach(
|
|
@@ -90,22 +84,16 @@ export const logs = Action.implement(
|
|
|
90
84
|
Effect.gen(function* () {
|
|
91
85
|
const store = yield* BuildStore;
|
|
92
86
|
|
|
93
|
-
// The handler returns a `Stream`, run in a scope of its own that closes when the caller
|
|
94
|
-
// stops reading, so what it acquires is released then.
|
|
95
87
|
return ({ buildId }) =>
|
|
96
88
|
Stream.unwrap(
|
|
97
89
|
Effect.gen(function* () {
|
|
98
90
|
const member = yield* CurrentMember;
|
|
99
|
-
// A failure before the first line, `BuildNotFound` here, fails the call as an action
|
|
100
|
-
// answering one value fails, a 404 over HTTP; one after a line ends the stream, the
|
|
101
|
-
// lines before it already read.
|
|
102
91
|
const log = yield* store.logOf(member.pipelines, buildId);
|
|
103
92
|
|
|
104
93
|
return Stream.fromIterable(log.map((line) => ({ line })));
|
|
105
94
|
})
|
|
106
95
|
);
|
|
107
96
|
}),
|
|
108
|
-
// Which builds a member may read is the store's check, by pipeline.
|
|
109
97
|
{ authorize: Action.allowAll }
|
|
110
98
|
);
|
|
111
99
|
```
|
package/docs/Action.md
CHANGED
|
@@ -12,8 +12,6 @@ One `Action.make` per action, in the domain's contract file. Export the domain's
|
|
|
12
12
|
import * as Action from "@gjermundgaraba/effect-actions/Action";
|
|
13
13
|
import { Schema } from "effect";
|
|
14
14
|
|
|
15
|
-
// A contract: what the action takes and answers, whether it changes anything, and who may
|
|
16
|
-
// call it. It imports no server code, so any client can import it, a browser page included.
|
|
17
15
|
export const Greet = Action.make("greet", {
|
|
18
16
|
description: "Greet someone by name.",
|
|
19
17
|
input: { name: Schema.String },
|
|
@@ -48,10 +46,6 @@ import { CurrentUser } from "../identity/Identity.js";
|
|
|
48
46
|
import { Notes } from "./Notes.js";
|
|
49
47
|
import { NoteStore } from "./store.js";
|
|
50
48
|
|
|
51
|
-
// The builder runs once, at startup, however many surfaces serve these actions: it is where a
|
|
52
|
-
// handler gets the services that last the process, never the caller. `CurrentUser` is read per
|
|
53
|
-
// call, inside each handler. Each handler is a thin step from the contract to the store, and
|
|
54
|
-
// none checks a scope: `authorize` has run before it.
|
|
55
49
|
export const notes = Action.implement(
|
|
56
50
|
Notes,
|
|
57
51
|
Effect.gen(function* () {
|
|
@@ -105,10 +99,6 @@ import { Effect } from "effect";
|
|
|
105
99
|
|
|
106
100
|
import { CurrentUser, scopes } from "./Identity.js";
|
|
107
101
|
|
|
108
|
-
// The one rule, read from each contract: a read needs the read scope, anything else the write
|
|
109
|
-
// scope. Naming the missing scope lets an OAuth client ask for it and retry (`Forbidden`):
|
|
110
|
-
// `scopes` below goes with `protectedResource` in verifier.ts, copy both or neither.
|
|
111
|
-
// This named span measures authorization within the action's span, including a refusal.
|
|
112
102
|
export const authorize = Effect.fn("authorize")(function* (action: Action.Any) {
|
|
113
103
|
const scope = action.readOnly ? scopes.read : scopes.write;
|
|
114
104
|
const user = yield* CurrentUser;
|
package/docs/ActionCli.md
CHANGED
|
@@ -20,22 +20,15 @@ import { commands } from "./commands.js";
|
|
|
20
20
|
import { CurrentCaller } from "./identity/Identity.js";
|
|
21
21
|
import type { Operator } from "./identity/Identity.js";
|
|
22
22
|
import { Permissions } from "./identity/permissions.js";
|
|
23
|
-
import {
|
|
23
|
+
import { demoTasksFile } from "./platform/tasks-file.js";
|
|
24
24
|
|
|
25
|
-
// Entrypoint: `node --import tsx examples/assistant/src/cli.ts list-tasks`.
|
|
26
|
-
// The operator's own tool. No credential arrives at a terminal, so this host names the caller
|
|
27
|
-
// itself: whoever runs the command, as the trusted operator. Being local earns nothing more:
|
|
28
|
-
// `authorize` runs here as for a model's tool call, and admits the operator by its kind.
|
|
29
25
|
const operator: Operator = { kind: "operator", id: userInfo().username };
|
|
30
26
|
|
|
31
27
|
const assistant = commands.pipe(
|
|
32
|
-
Command.provide(Layer.mergeAll(
|
|
28
|
+
Command.provide(Layer.mergeAll(demoTasksFile, Permissions.layerMemory)),
|
|
33
29
|
Command.provideSync(CurrentCaller, operator)
|
|
34
30
|
);
|
|
35
31
|
|
|
36
|
-
// A command's defect, such as a tasks file that does not decode, `runMain` reports on stdout.
|
|
37
|
-
// `logToStderr`, applied last, moves that to stderr, so `assistant list-tasks --json | jq` reads
|
|
38
|
-
// results alone.
|
|
39
32
|
Command.run(assistant, { version: "1.0.0" }).pipe(
|
|
40
33
|
Effect.provide(NodeServices.layer),
|
|
41
34
|
ActionCli.logToStderr,
|
|
@@ -66,8 +59,6 @@ import { HttpClient, HttpClientRequest } from "effect/http";
|
|
|
66
59
|
|
|
67
60
|
import { Http } from "./Api.js";
|
|
68
61
|
|
|
69
|
-
// The binding's client, connected to where the server is and as which member, both read from
|
|
70
|
-
// config when a command runs.
|
|
71
62
|
const api = Effect.gen(function* () {
|
|
72
63
|
const baseUrl = yield* Config.String("BUILDS_URL").pipe(
|
|
73
64
|
Config.withDefault("http://127.0.0.1:3000")
|
|
@@ -83,13 +74,6 @@ const api = Effect.gen(function* () {
|
|
|
83
74
|
});
|
|
84
75
|
});
|
|
85
76
|
|
|
86
|
-
// A command per action, and `watch <event>` per event type: `builds watch build.finished
|
|
87
|
-
// --pipeline api` holds the event's stream open and prints each occurrence as a line of JSON,
|
|
88
|
-
// its `cursor` included, until it is stopped. `--cursor <cursor>` resumes after that line;
|
|
89
|
-
// omitted, the watch starts from now. The lines pipe into `jq` or a script as they come.
|
|
90
|
-
// `builds start-build --pipeline api` prints each step as a line of JSON on stderr as it
|
|
91
|
-
// starts, and the build on stdout, which a pipe reads alone. `builds tail-log --build-id
|
|
92
|
-
// build-1` prints each line of its log as a line of JSON as it arrives.
|
|
93
77
|
export const commands = ActionCli.remote(Http, { name: "builds", client: api });
|
|
94
78
|
```
|
|
95
79
|
|
|
@@ -126,15 +110,9 @@ import * as ActionCli from "@gjermundgaraba/effect-actions/ActionCli";
|
|
|
126
110
|
import { identity } from "./identity/handlers.js";
|
|
127
111
|
import { tasks } from "./tasks/handlers.js";
|
|
128
112
|
|
|
129
|
-
// The same implementations as local commands, `assistant <action>`: a command runs the handler
|
|
130
|
-
// in this process, behind the same `authorize`. `add-task` and `who-am-i` are as derived; the
|
|
131
|
-
// others each change their syntax or output.
|
|
132
113
|
export const commands = ActionCli.make([tasks, identity], {
|
|
133
114
|
name: "assistant",
|
|
134
115
|
commands: {
|
|
135
|
-
// `assistant list-tasks -a ada`: a short flag beside `--assignee`. `render` prints a line
|
|
136
|
-
// per task for a person, and adds `--json` for the JSON a script reads. `description` is the
|
|
137
|
-
// command's help, saying what a line holds; the action's stays what a model reads.
|
|
138
116
|
listTasks: {
|
|
139
117
|
description:
|
|
140
118
|
"Print one line per task: [x] when done, then its id, title and assignee.",
|
|
@@ -147,9 +125,7 @@ export const commands = ActionCli.make([tasks, identity], {
|
|
|
147
125
|
)
|
|
148
126
|
.join("\n"),
|
|
149
127
|
},
|
|
150
|
-
// `assistant complete-task 2`: an argument instead of `--id 2`.
|
|
151
128
|
completeTask: { positional: ["id"] },
|
|
152
|
-
// `assistant annotate-task --id 2 < notes.md`: the notes are piped in, not a flag.
|
|
153
129
|
annotateTask: { stdin: "notes" },
|
|
154
130
|
},
|
|
155
131
|
});
|
package/docs/ActionHttp.md
CHANGED
|
@@ -15,10 +15,6 @@ import { Login, WhoAmI } from "./identity/Identity.js";
|
|
|
15
15
|
import { Notes } from "./notes/Notes.js";
|
|
16
16
|
import { GetStatus } from "./status/Status.js";
|
|
17
17
|
|
|
18
|
-
// The HTTP binding: every action at `POST /api/<name>`, the protected ones requiring `Login`.
|
|
19
|
-
// It is plain data and imports no server code, so the server, the CLI and a browser page all
|
|
20
|
-
// import this one value, and each gets its routes or its typed methods from it. An action
|
|
21
|
-
// added here is also a `notes` command: commands.ts derives one per action of this binding.
|
|
22
18
|
export const Http = ActionHttp.make([GetStatus, ...Notes, WhoAmI], {
|
|
23
19
|
prefix: "/api",
|
|
24
20
|
authentication: Login,
|
|
@@ -45,30 +41,18 @@ import { authenticate } from "./identity/verifier.js";
|
|
|
45
41
|
import { notes } from "./notes/handlers.js";
|
|
46
42
|
import { status } from "./status/handlers.js";
|
|
47
43
|
|
|
48
|
-
// Every domain's implementation, once. Each surface below takes the same list.
|
|
49
44
|
const implementations = [status, notes, identity];
|
|
50
45
|
|
|
51
|
-
// JSON routes at `/api/<action>`, the binding's; `getStatus` answers anyone.
|
|
52
46
|
const api = ActionHttp.layer(Http, implementations);
|
|
53
47
|
|
|
54
|
-
// The host's own routes sit beside the actions' on the same router.
|
|
55
48
|
const health = HttpRouter.add("GET", "/healthz", HttpServerResponse.text("ok"));
|
|
56
49
|
|
|
57
|
-
// Served to anyone, which is a choice: it tells a signed-out caller every action's name and
|
|
58
|
-
// shape. To keep them, leave the route out, or provide `Authentication.protect(Login).layer`
|
|
59
|
-
// to it.
|
|
60
50
|
const openApi = HttpRouter.add(
|
|
61
51
|
"GET",
|
|
62
52
|
"/api/openapi.json",
|
|
63
53
|
HttpServerResponse.jsonUnsafe(OpenApi.fromApi(Http.api))
|
|
64
54
|
);
|
|
65
55
|
|
|
66
|
-
// What the host serves: the JSON routes, the same actions as MCP tools at `/mcp`, and the host's
|
|
67
|
-
// own routes. The MCP endpoint serves the public `getStatus` beside the protected tools, so a
|
|
68
|
-
// signed-out 2026-07-28 client may list the tools and call `getStatus`; what else needs a token
|
|
69
|
-
// is on `ActionMcp.LayerHttpOptions.authentication`. It still requires `NoteStore`: the
|
|
70
|
-
// entrypoint chooses which one, and a test chooses its own. The entrypoint also chooses how the
|
|
71
|
-
// MCP endpoint runs: one server for every request, or one per request.
|
|
72
56
|
const serving = (requestScoped: boolean) =>
|
|
73
57
|
Layer.mergeAll(
|
|
74
58
|
api,
|
|
@@ -103,11 +87,8 @@ import { HttpRouter, HttpServerRequest } from "effect/http";
|
|
|
103
87
|
import { NoteStore } from "./notes/store.js";
|
|
104
88
|
import { routes } from "./routes.js";
|
|
105
89
|
|
|
106
|
-
// Entrypoint: `node --import tsx examples/notes-service/src/server.ts`.
|
|
107
|
-
// It chooses the store, reads the configuration and starts Node's HTTP server.
|
|
108
90
|
const server = Layer.unwrap(
|
|
109
91
|
Effect.gen(function* () {
|
|
110
|
-
// The loopback default serves this machine alone; HOST=0.0.0.0 lets others reach it.
|
|
111
92
|
const host = yield* Config.String("HOST").pipe(
|
|
112
93
|
Config.withDefault("127.0.0.1")
|
|
113
94
|
);
|
|
@@ -116,12 +97,9 @@ const server = Layer.unwrap(
|
|
|
116
97
|
|
|
117
98
|
return HttpRouter.serve(routes).pipe(
|
|
118
99
|
Layer.provide(NodeHttpServer.layer(createServer, { host, port })),
|
|
119
|
-
// Request bodies have no size limit unless the host sets one. This covers every route that
|
|
120
|
-
// reads its body whole.
|
|
121
100
|
Layer.provide(
|
|
122
101
|
Layer.succeed(HttpServerRequest.MaxBodySize, ByteSize.mebibytes(1))
|
|
123
102
|
),
|
|
124
|
-
// DEMO ONLY: notes live in this process's memory; a deployment provides a store that lasts.
|
|
125
103
|
Layer.provide(NoteStore.layerMemory)
|
|
126
104
|
);
|
|
127
105
|
})
|
|
@@ -161,9 +139,6 @@ import { FetchHttpClient } from "effect/http";
|
|
|
161
139
|
|
|
162
140
|
import { Http } from "./Api.js";
|
|
163
141
|
|
|
164
|
-
// Entrypoint: `node --import tsx examples/quickstart/src/client.ts`, with the server running.
|
|
165
|
-
// One typed method per action of the binding.
|
|
166
|
-
// DEMO ONLY: a deployed client reads its `baseUrl` from configuration (kitchen-sink/src/seed.ts).
|
|
167
142
|
Effect.gen(function* () {
|
|
168
143
|
const client = yield* ActionHttp.client(Http, {
|
|
169
144
|
baseUrl: "http://127.0.0.1:3000",
|
|
@@ -194,9 +169,6 @@ import {
|
|
|
194
169
|
import { DeployRequested, RequestDeploy } from "./deploys/Deploys.js";
|
|
195
170
|
import { Login } from "./identity/Identity.js";
|
|
196
171
|
|
|
197
|
-
// The HTTP binding: each action at `POST /api/<name>`, and each event at
|
|
198
|
-
// `POST /api/events/<name>`, which answers a page of JSON, or, asked for `text/event-stream`,
|
|
199
|
-
// holds a stream open. It imports no server code, so a client imports it with the contracts.
|
|
200
172
|
export const Http = ActionHttp.make(
|
|
201
173
|
[
|
|
202
174
|
FinishBuild,
|