@gjermundgaraba/effect-actions 0.22.0 → 0.24.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 +9 -3
- package/dist/Action.d.mts +2 -2
- package/dist/Action.mjs +1 -0
- package/dist/ActionCli.d.mts +1 -1
- package/dist/ActionCli.mjs +1 -2
- package/dist/ActionHttp.d.mts +2 -2
- package/dist/ActionMcp.d.mts +20 -20
- package/dist/ActionMcp.mjs +9 -11
- 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 +1 -26
- 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-failures.md +1 -1
- package/docs/ActionHttp.md +0 -28
- package/docs/ActionMcp-failures.md +2 -1
- 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-failures.md +1 -1
- package/docs/Testing.md +3 -13
- package/docs/Webhook.md +0 -46
- package/docs/setup-failures.md +1 -1
- package/docs/setup.md +5 -10
- 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 +6 -6
|
@@ -4,11 +4,6 @@ import { Effect } from "effect";
|
|
|
4
4
|
import { PrintFarm } from "./farm.js";
|
|
5
5
|
import { Printers } from "./Printers.js";
|
|
6
6
|
|
|
7
|
-
// The builder runs once, when the server starts, whichever transport serves the tools: it is
|
|
8
|
-
// where the handlers get the farm, already signed in. Each handler is a step from a tool's
|
|
9
|
-
// input to the service and back to the tool's success. None handles a failure: the service
|
|
10
|
-
// fails only with `PrintFarmError`, which every tool declares, so it reaches the model as is.
|
|
11
|
-
// Public actions take no `authorize`.
|
|
12
7
|
export const printers = Action.implement(
|
|
13
8
|
Printers,
|
|
14
9
|
Effect.gen(function* () {
|
|
@@ -17,7 +12,6 @@ export const printers = Action.implement(
|
|
|
17
12
|
return {
|
|
18
13
|
list_printers: () =>
|
|
19
14
|
Effect.map(farm.printers, (all) => ({ printers: all })),
|
|
20
|
-
// Returned as `{ data, mimeType }` like any other field: the MCP surface makes the block.
|
|
21
15
|
get_camera_snapshot: ({ printer_id }) =>
|
|
22
16
|
Effect.all(
|
|
23
17
|
{
|
|
@@ -5,23 +5,13 @@ import { HttpRouter, HttpServerResponse } from "effect/http";
|
|
|
5
5
|
import { serverInfo } from "./instructions.js";
|
|
6
6
|
import { printers } from "./printers/handlers.js";
|
|
7
7
|
|
|
8
|
-
// The tools at `/mcp` and the host's own route beside them. It is a Layer built from `Config`
|
|
9
|
-
// because the origins are known only where the server is deployed.
|
|
10
8
|
export const routes = Layer.unwrap(
|
|
11
9
|
Effect.gen(function* () {
|
|
12
|
-
// A browser sends its page's `Origin`, and the endpoint refuses every origin not listed
|
|
13
|
-
// here, so a web page a user happens to visit cannot call the tools. Other MCP clients
|
|
14
|
-
// send none and pass. `ALLOWED_ORIGINS=https://a.example,https://b.example`; none by default.
|
|
15
10
|
const allowedOrigins = yield* Config.Array(
|
|
16
11
|
Schema.String,
|
|
17
12
|
"ALLOWED_ORIGINS"
|
|
18
13
|
).pipe(Config.withDefault([]));
|
|
19
14
|
|
|
20
|
-
// The Origin check only refuses. A browser also needs CORS: an answer to its preflight, and
|
|
21
|
-
// headers on the endpoint's answers. `HttpRouter.cors` is global to the router and, given
|
|
22
|
-
// no origin, allows every one, so it is added only where an origin is configured. The
|
|
23
|
-
// methods are the endpoint's, the headers those an MCP client sends and must be able to
|
|
24
|
-
// read; an endpoint with authentication adds `Authorization` and exposes `WWW-Authenticate`.
|
|
25
15
|
const cors =
|
|
26
16
|
allowedOrigins.length === 0
|
|
27
17
|
? Layer.empty
|
|
@@ -39,7 +29,6 @@ export const routes = Layer.unwrap(
|
|
|
39
29
|
});
|
|
40
30
|
|
|
41
31
|
return Layer.mergeAll(
|
|
42
|
-
// No `authentication`: every tool is public, so the endpoint authenticates nothing.
|
|
43
32
|
ActionMcp.layerHttp(printers, { ...serverInfo, allowedOrigins }),
|
|
44
33
|
HttpRouter.add("GET", "/healthz", HttpServerResponse.text("ok"))
|
|
45
34
|
).pipe(Layer.provide(cors));
|
|
@@ -8,11 +8,6 @@ import { fakeFarm } from "./platform/fake-farm.js";
|
|
|
8
8
|
import { printFarm } from "./platform/print-farm.js";
|
|
9
9
|
import { routes } from "./routes.js";
|
|
10
10
|
|
|
11
|
-
// Entrypoint: `node --import tsx examples/mcp-tools/src/server.ts`, with FARM_URL, FARM_USER
|
|
12
|
-
// and FARM_PASSWORD set. MCP over HTTP, for clients that connect to a URL.
|
|
13
|
-
// These tools are public: no route asks for a token, so where the server listens is its access
|
|
14
|
-
// control, the loopback interface by default or a network only trusted clients are on. Tools
|
|
15
|
-
// that need a caller are protected as docs/ActionMcp.md's "Protect tools" shows.
|
|
16
11
|
const server = Layer.unwrap(
|
|
17
12
|
Effect.gen(function* () {
|
|
18
13
|
const host = yield* Config.String("HOST").pipe(
|
|
@@ -23,13 +18,9 @@ const server = Layer.unwrap(
|
|
|
23
18
|
|
|
24
19
|
return HttpRouter.serve(routes).pipe(
|
|
25
20
|
Layer.provide(NodeHttpServer.layer(createServer, { host, port })),
|
|
26
|
-
// Request bodies have no size limit unless the host sets one. A tool call is a few
|
|
27
|
-
// short fields, so anything larger is not one.
|
|
28
21
|
Layer.provide(
|
|
29
22
|
Layer.succeed(HttpServerRequest.MaxBodySize, ByteSize.kibibytes(64))
|
|
30
23
|
),
|
|
31
|
-
// DEMO ONLY: `fakeFarm` answers the farm's API in memory. A real server provides
|
|
32
|
-
// `FetchHttpClient.layer` in its place, and `printFarm` calls the farm at FARM_URL.
|
|
33
24
|
Layer.provide(printFarm.pipe(Layer.provide(fakeFarm)))
|
|
34
25
|
);
|
|
35
26
|
})
|
|
@@ -8,20 +8,8 @@ import { fakeFarm } from "./platform/fake-farm.js";
|
|
|
8
8
|
import { printFarm } from "./platform/print-farm.js";
|
|
9
9
|
import { printers } from "./printers/handlers.js";
|
|
10
10
|
|
|
11
|
-
// Entrypoint: an MCP server a local agent starts as a subprocess, with FARM_URL, FARM_USER and
|
|
12
|
-
// FARM_PASSWORD in its environment. The README has the line that adds it to Claude Code and
|
|
13
|
-
// its entry in Claude Desktop's configuration.
|
|
14
|
-
// The same implementation and the same server information as the HTTP server, and no port:
|
|
15
|
-
// the agent's host starts the process, writes requests to its stdin, and closes it to stop it.
|
|
16
11
|
ActionMcp.runStdio(printers, serverInfo).pipe(
|
|
17
|
-
Effect.provide([
|
|
18
|
-
NodeStdio.layer,
|
|
19
|
-
// DEMO ONLY: `fakeFarm` answers the farm's API in memory. A real server provides
|
|
20
|
-
// `FetchHttpClient.layer` in its place.
|
|
21
|
-
printFarm.pipe(Layer.provide(fakeFarm)),
|
|
22
|
-
]),
|
|
23
|
-
// stdout carries the protocol alone. Applied last, this sends every log to stderr, the
|
|
24
|
-
// sign-in line `printFarm` logs included, and a startup failure too.
|
|
12
|
+
Effect.provide([NodeStdio.layer, printFarm.pipe(Layer.provide(fakeFarm))]),
|
|
25
13
|
ActionCli.logToStderr,
|
|
26
14
|
NodeRuntime.runMain()
|
|
27
15
|
);
|
|
@@ -10,11 +10,6 @@ import { printFarm } from "../../src/platform/print-farm.js";
|
|
|
10
10
|
import { printers } from "../../src/printers/handlers.js";
|
|
11
11
|
import { PrintFarmError } from "../../src/printers/Printers.js";
|
|
12
12
|
|
|
13
|
-
// What the tools do, with no transport: `Action.client` runs the same decoding and the same
|
|
14
|
-
// handlers a server runs. A public action has no caller, so a call needs none provided.
|
|
15
|
-
|
|
16
|
-
// The real `PrintFarm` over an upstream of the test's: its requests, its sign-in and its error
|
|
17
|
-
// mapping all run, and only the network is replaced. Its settings come from a Layer too.
|
|
18
13
|
const farmOver = (upstream: Layer.Layer<HttpClient.HttpClient>) =>
|
|
19
14
|
printFarm.pipe(
|
|
20
15
|
Layer.provide(upstream),
|
|
@@ -29,8 +24,6 @@ const farmOver = (upstream: Layer.Layer<HttpClient.HttpClient>) =>
|
|
|
29
24
|
)
|
|
30
25
|
);
|
|
31
26
|
|
|
32
|
-
// The fake farm, noting each request the service sends it. A request is noted as the service
|
|
33
|
-
// wrote it, before its client prepends FARM_URL, so the paths are relative.
|
|
34
27
|
const noting = (sent: string[]) =>
|
|
35
28
|
Layer.effect(
|
|
36
29
|
HttpClient.HttpClient,
|
|
@@ -42,7 +35,6 @@ const noting = (sent: string[]) =>
|
|
|
42
35
|
)
|
|
43
36
|
).pipe(Layer.provide(fakeFarm));
|
|
44
37
|
|
|
45
|
-
// A farm that lets the service sign in, then answers every other request with `answer`.
|
|
46
38
|
const farmThat = (
|
|
47
39
|
answer: (
|
|
48
40
|
request: HttpClientRequest.HttpClientRequest
|
|
@@ -142,7 +134,6 @@ describe("printers, in process", () => {
|
|
|
142
134
|
}
|
|
143
135
|
);
|
|
144
136
|
|
|
145
|
-
// The message is all a model gets of a failure, so each test asserts it.
|
|
146
137
|
it.effect(
|
|
147
138
|
"fails with the declared error, carrying the farm's own reason",
|
|
148
139
|
() =>
|
|
@@ -214,7 +205,6 @@ describe("printers, in process", () => {
|
|
|
214
205
|
)
|
|
215
206
|
);
|
|
216
207
|
|
|
217
|
-
// `it.effect` runs on a test clock, so the thirty seconds pass when the test says so.
|
|
218
208
|
it.effect("gives up on a farm that does not answer", () =>
|
|
219
209
|
Effect.gen(function* () {
|
|
220
210
|
const client = yield* Action.client(printers);
|
|
@@ -9,17 +9,12 @@ import { printFarm } from "../src/platform/print-farm.js";
|
|
|
9
9
|
import { PrintFarmError, Printers } from "../src/printers/Printers.js";
|
|
10
10
|
import { routes } from "../src/routes.js";
|
|
11
11
|
|
|
12
|
-
// What the MCP surface adds: the tools as listed, their results as sent, the origin check.
|
|
13
|
-
// The real routes answer in memory, and no port is opened.
|
|
14
|
-
|
|
15
|
-
// What the entrypoint reads from the environment, given to the test as a Layer.
|
|
16
12
|
const farm = {
|
|
17
13
|
FARM_URL: "https://farm.example.test",
|
|
18
14
|
FARM_USER: "agent",
|
|
19
15
|
FARM_PASSWORD: "secret",
|
|
20
16
|
};
|
|
21
17
|
|
|
22
|
-
// The fake farm goes to `printFarm` alone, so the test's own requests still reach the routes.
|
|
23
18
|
const servedWith = (environment: Record<string, string>) =>
|
|
24
19
|
Testing.layer(routes).pipe(
|
|
25
20
|
Layer.provide(printFarm.pipe(Layer.provide(fakeFarm))),
|
|
@@ -33,7 +28,6 @@ const served = servedWith({
|
|
|
33
28
|
ALLOWED_ORIGINS: "https://console.example.test,https://wall.example.test",
|
|
34
29
|
});
|
|
35
30
|
|
|
36
|
-
// A browser's preflight: what it asks before a cross-origin `POST` with an MCP client's headers.
|
|
37
31
|
const preflight = (origin: string) =>
|
|
38
32
|
HttpClient.execute(
|
|
39
33
|
HttpClientRequest.make("OPTIONS")("/mcp", {
|
|
@@ -45,7 +39,6 @@ const preflight = (origin: string) =>
|
|
|
45
39
|
})
|
|
46
40
|
);
|
|
47
41
|
|
|
48
|
-
// One tool of a `tools/list` answer, as far as this test reads it.
|
|
49
42
|
const Listed = Schema.Struct({
|
|
50
43
|
result: Schema.Struct({
|
|
51
44
|
tools: Schema.Array(
|
|
@@ -80,7 +73,6 @@ const from = (origin: string) =>
|
|
|
80
73
|
describe("the tools, over MCP", () => {
|
|
81
74
|
it.effect("lists every tool with its title and hints as sent", () =>
|
|
82
75
|
Effect.gen(function* () {
|
|
83
|
-
// A raw request, for what no typed client shows: here, the listing itself.
|
|
84
76
|
const response = yield* HttpClient.execute(
|
|
85
77
|
ActionMcp.request("tools/list")
|
|
86
78
|
);
|
|
@@ -121,8 +113,6 @@ describe("the tools, over MCP", () => {
|
|
|
121
113
|
|
|
122
114
|
it.effect("answers a camera's picture as an image beside the printer", () =>
|
|
123
115
|
Effect.gen(function* () {
|
|
124
|
-
// A client method per tool: it sends `tools/call` and decodes the result, the image
|
|
125
|
-
// from its image block, as the handler returned it.
|
|
126
116
|
const mcp = yield* ActionMcp.client(Printers);
|
|
127
117
|
|
|
128
118
|
expect(
|
|
@@ -200,7 +190,6 @@ describe("the tools, over MCP", () => {
|
|
|
200
190
|
"Mcp-Session-Id"
|
|
201
191
|
);
|
|
202
192
|
|
|
203
|
-
// Two origins are configured, so each request's own is compared with the list.
|
|
204
193
|
expect(
|
|
205
194
|
(yield* preflight("https://elsewhere.example.test")).headers
|
|
206
195
|
).not.toHaveProperty("access-control-allow-origin");
|
|
@@ -211,9 +200,6 @@ describe("the tools, over MCP", () => {
|
|
|
211
200
|
"names its one configured origin to every preflight, and still refuses any other",
|
|
212
201
|
() =>
|
|
213
202
|
Effect.gen(function* () {
|
|
214
|
-
// With exactly one origin, Effect's CORS compares nothing: the header is that origin
|
|
215
|
-
// whoever asks. A page elsewhere is still refused, twice: its browser reads an origin
|
|
216
|
-
// that is not its own, and the endpoint answers its request 403.
|
|
217
203
|
const asked = yield* preflight("https://elsewhere.example.test");
|
|
218
204
|
|
|
219
205
|
expect(asked.status).toBe(204);
|
|
@@ -4,10 +4,6 @@ import { Login, WhoAmI } from "./identity/Identity.js";
|
|
|
4
4
|
import { Notes } from "./notes/Notes.js";
|
|
5
5
|
import { GetStatus } from "./status/Status.js";
|
|
6
6
|
|
|
7
|
-
// The HTTP binding: every action at `POST /api/<name>`, the protected ones requiring `Login`.
|
|
8
|
-
// It is plain data and imports no server code, so the server, the CLI and a browser page all
|
|
9
|
-
// import this one value, and each gets its routes or its typed methods from it. An action
|
|
10
|
-
// added here is also a `notes` command: commands.ts derives one per action of this binding.
|
|
11
7
|
export const Http = ActionHttp.make([GetStatus, ...Notes, WhoAmI], {
|
|
12
8
|
prefix: "/api",
|
|
13
9
|
authentication: Login,
|
|
@@ -5,13 +5,7 @@ import { HttpClient, HttpClientRequest } from "effect/http";
|
|
|
5
5
|
import { Http } from "./Api.js";
|
|
6
6
|
import type { Note } from "./notes/Notes.js";
|
|
7
7
|
|
|
8
|
-
// Client module: what a web page imports. It holds the binding and nothing of the server, so a
|
|
9
|
-
// bundler gets the contracts and Effect's fetch client, and no handler.
|
|
10
|
-
//
|
|
11
|
-
// Without `baseUrl` the routes are called on the page's own origin. A page served elsewhere
|
|
12
|
-
// passes the API's URL, and the server must then allow its origin (CORS).
|
|
13
8
|
export const connect = (token: string, baseUrl?: string) => {
|
|
14
|
-
// Built once, outside any Effect. Every method is typed from its action.
|
|
15
9
|
const api = ActionHttp.fetchClient(Http, {
|
|
16
10
|
transformClient: HttpClient.mapRequest(
|
|
17
11
|
HttpClientRequest.bearerToken(token)
|
|
@@ -19,21 +13,15 @@ export const connect = (token: string, baseUrl?: string) => {
|
|
|
19
13
|
...(baseUrl === undefined ? {} : { baseUrl }),
|
|
20
14
|
});
|
|
21
15
|
|
|
22
|
-
// Each method returns a promise. A failure rejects it with the decoded error, so in a `catch`
|
|
23
|
-
// `error instanceof Action.Unauthenticated` tells an expired sign-in from anything else.
|
|
24
16
|
return {
|
|
25
17
|
notes: async () => await Effect.runPromise(api.listNotes()),
|
|
26
18
|
|
|
27
|
-
// A declared error can also be handled by its tag before the promise: a missing note is
|
|
28
|
-
// `undefined` here, and every other failure still rejects.
|
|
29
19
|
note: async (id: string): Promise<Note | undefined> =>
|
|
30
20
|
await api.getNote({ id }).pipe(
|
|
31
21
|
Effect.catchTag("NoteNotFound", () => Effect.undefined),
|
|
32
22
|
Effect.runPromise
|
|
33
23
|
),
|
|
34
24
|
|
|
35
|
-
// A stale revision is refused, never overwritten: the error, `NoteChanged`, carries the
|
|
36
|
-
// current one.
|
|
37
25
|
save: async (note: Note, body: string) =>
|
|
38
26
|
await Effect.runPromise(
|
|
39
27
|
api.updateNote({ id: note.id, body, revision: note.revision })
|
|
@@ -9,8 +9,6 @@ import { Command } from "effect/cli";
|
|
|
9
9
|
|
|
10
10
|
import { commands } from "./commands.js";
|
|
11
11
|
|
|
12
|
-
// Entrypoint: `node --import tsx examples/notes-service/src/cli.ts list-notes`.
|
|
13
|
-
// The commands of commands.ts, which read where the server is and the token when one runs.
|
|
14
12
|
Command.run(commands, { version: "1.0.0" }).pipe(
|
|
15
13
|
Effect.provide([NodeHttpClient.layerUndici, NodeServices.layer]),
|
|
16
14
|
ActionCli.logToStderr,
|
|
@@ -6,8 +6,6 @@ import { HttpClient, HttpClientRequest } from "effect/http";
|
|
|
6
6
|
|
|
7
7
|
import { Http } from "./Api.js";
|
|
8
8
|
|
|
9
|
-
// What a command fails with when no token is configured: a tagged error of the CLI's own, printed
|
|
10
|
-
// on stderr as `MissingToken: <message>` under an `ERROR` heading, and exit 1.
|
|
11
9
|
class MissingToken extends Schema.TaggedError<
|
|
12
10
|
MissingToken,
|
|
13
11
|
{ readonly brand: unique symbol }
|
|
@@ -15,15 +13,11 @@ class MissingToken extends Schema.TaggedError<
|
|
|
15
13
|
message: Schema.String,
|
|
16
14
|
}) {}
|
|
17
15
|
|
|
18
|
-
// The binding's typed client, connected to where the server is and as who is calling, both read
|
|
19
|
-
// from config. Every command here, the derived ones and your own, calls through this one.
|
|
20
16
|
const api = Effect.gen(function* () {
|
|
21
17
|
const baseUrl = yield* Config.String("NOTES_URL").pipe(
|
|
22
18
|
Config.withDefault("http://127.0.0.1:3000")
|
|
23
19
|
);
|
|
24
20
|
|
|
25
|
-
// Required, since every command but `get-status` is refused without one, and mapped so that a
|
|
26
|
-
// missing one prints what to do.
|
|
27
21
|
const token = yield* Config.Redacted("NOTES_TOKEN").pipe(
|
|
28
22
|
Effect.mapError(
|
|
29
23
|
() =>
|
|
@@ -42,18 +36,10 @@ const api = Effect.gen(function* () {
|
|
|
42
36
|
});
|
|
43
37
|
});
|
|
44
38
|
|
|
45
|
-
// The file's format: each note a `# ` title line, a blank line and its body, the notes apart by
|
|
46
|
-
// a blank line. A body line that starts as a title would is written with a backslash before it,
|
|
47
|
-
// as Markdown escapes one, and `import` takes it off, so every note reads back as it was.
|
|
48
39
|
const escape = (body: string) => body.replaceAll(/^(?=\\*# )/gmu, "\\");
|
|
49
40
|
|
|
50
41
|
const unescape = (body: string) => body.replaceAll(/^\\(?=\\*# )/gmu, "");
|
|
51
42
|
|
|
52
|
-
// A command of your own, for what no single action does: this one calls two and writes a file.
|
|
53
|
-
// It is a native Effect CLI command, so its syntax and its output are what you write: `notes
|
|
54
|
-
// export notes.md` takes its argument from `Argument`, as a flag would come from `Flag`. It
|
|
55
|
-
// calls the server through `api`, the client the derived commands call, and writes with the
|
|
56
|
-
// `FileSystem` every Effect CLI command has.
|
|
57
43
|
const exportNotes = Command.make(
|
|
58
44
|
"export",
|
|
59
45
|
{
|
|
@@ -80,24 +66,13 @@ const exportNotes = Command.make(
|
|
|
80
66
|
|
|
81
67
|
yield* Console.log(`Wrote ${whole.length} notes to ${file}`);
|
|
82
68
|
}).pipe(
|
|
83
|
-
// Context of your own for a failure, on stderr before it. The line prints for every
|
|
84
|
-
// failure of the command, a file that cannot be written included, so it says only what is
|
|
85
|
-
// true of them all.
|
|
86
69
|
Effect.tapError(() =>
|
|
87
70
|
Console.error(`The export to ${file} did not finish.`)
|
|
88
71
|
),
|
|
89
|
-
// So the export fails as a derived command does.
|
|
90
72
|
ActionCli.toUserError(Http)
|
|
91
73
|
)
|
|
92
74
|
).pipe(Command.withDescription("Write every note to a file, as Markdown."));
|
|
93
75
|
|
|
94
|
-
// The other half of `export`: it reads such a file and creates its notes with one call of the
|
|
95
|
-
// bulk action, so the server checks every note before it writes any, and a file of more than
|
|
96
|
-
// the action's 100 is refused whole. The command reads the file and nothing more.
|
|
97
|
-
// `Argument.FileText` reads it, and a missing file is a usage error naming it, where
|
|
98
|
-
// `Argument.String` and `FileSystem` would print the platform's error. Text before the first
|
|
99
|
-
// title is refused, not dropped: the file is not one `export` wrote. The derived `import-notes`
|
|
100
|
-
// stays beside it, taking the notes as a JSON array in `--notes`.
|
|
101
76
|
const importNotes = Command.make(
|
|
102
77
|
"import",
|
|
103
78
|
{
|
|
@@ -137,20 +112,15 @@ const importNotes = Command.make(
|
|
|
137
112
|
)
|
|
138
113
|
);
|
|
139
114
|
|
|
140
|
-
// A command per action of the binding, a flag per input field, each calling the server over
|
|
141
|
-
// HTTP. It imports the binding alone, never a handler. It is apart from cli.ts so a test runs
|
|
142
|
-
// it: test/commands.test.ts.
|
|
143
115
|
export const commands = ActionCli.remote(Http, {
|
|
144
116
|
name: "notes",
|
|
145
117
|
client: api,
|
|
146
118
|
commands: {
|
|
147
|
-
// `notes create-note --title Draft < draft.md`: the body is piped in, not a flag.
|
|
148
119
|
createNote: { stdin: "body" },
|
|
149
120
|
getNote: {
|
|
150
121
|
positional: ["id"],
|
|
151
122
|
render: (note) => `# ${note.title}\n\n${note.body}`,
|
|
152
123
|
},
|
|
153
|
-
// `notes update-note <id> --revision 1 < draft.md`: an argument, a flag and stdin together.
|
|
154
124
|
updateNote: { positional: ["id"], stdin: "body" },
|
|
155
125
|
},
|
|
156
126
|
subcommands: [exportNotes, importNotes],
|
|
@@ -19,8 +19,6 @@ export class CurrentUser extends Context.Service<CurrentUser, User>()(
|
|
|
19
19
|
"notes-service/CurrentUser"
|
|
20
20
|
) {}
|
|
21
21
|
|
|
22
|
-
// How a remote caller proves it is CurrentUser: a bearer token, the default scheme. Clients
|
|
23
|
-
// import this descriptor with the binding; its verifier, in verifier.ts, stays on the server.
|
|
24
22
|
export const Login = Authentication.make("notes-service.Login", CurrentUser);
|
|
25
23
|
|
|
26
24
|
export const WhoAmI = Action.make("whoAmI", {
|
|
@@ -3,10 +3,6 @@ import { Effect } from "effect";
|
|
|
3
3
|
|
|
4
4
|
import { CurrentUser, scopes } from "./Identity.js";
|
|
5
5
|
|
|
6
|
-
// The one rule, read from each contract: a read needs the read scope, anything else the write
|
|
7
|
-
// scope. Naming the missing scope lets an OAuth client ask for it and retry (`Forbidden`):
|
|
8
|
-
// `scopes` below goes with `protectedResource` in verifier.ts, copy both or neither.
|
|
9
|
-
// This named span measures authorization within the action's span, including a refusal.
|
|
10
6
|
export const authorize = Effect.fn("authorize")(function* (action: Action.Any) {
|
|
11
7
|
const scope = action.readOnly ? scopes.read : scopes.write;
|
|
12
8
|
const user = yield* CurrentUser;
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { scopes } from "./Identity.js";
|
|
2
2
|
import type { User } from "./Identity.js";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
// has no such table: its verifier reads the user from a signed token.
|
|
6
|
-
export const users = {
|
|
4
|
+
export const demoUsers = {
|
|
7
5
|
ada: { id: "ada", scopes: [scopes.read, scopes.write] },
|
|
8
6
|
grace: { id: "grace", scopes: [scopes.read, scopes.write] },
|
|
9
7
|
reader: { id: "reader", scopes: [scopes.read] },
|
|
@@ -3,27 +3,21 @@ import * as Authentication from "@gjermundgaraba/effect-actions/Authentication";
|
|
|
3
3
|
import { Config, Effect, Redacted } from "effect";
|
|
4
4
|
|
|
5
5
|
import { Login, scopes } from "./Identity.js";
|
|
6
|
-
import {
|
|
6
|
+
import { demoUsers } from "./users.js";
|
|
7
7
|
|
|
8
|
-
const isToken = (token: string): token is keyof typeof
|
|
9
|
-
Object.hasOwn(
|
|
8
|
+
const isToken = (token: string): token is keyof typeof demoUsers =>
|
|
9
|
+
Object.hasOwn(demoUsers, token);
|
|
10
10
|
|
|
11
|
-
// DEMO ONLY: a token is a user's name. A real verifier checks a signed token with your
|
|
12
|
-
// authorization server's library, its audience included. An unknown token is the built-in
|
|
13
|
-
// `Unauthenticated`: a 401 every client decodes.
|
|
14
11
|
const verify = (token: Redacted.Redacted) => {
|
|
15
12
|
const name = Redacted.value(token);
|
|
16
13
|
|
|
17
14
|
return isToken(name)
|
|
18
|
-
? Effect.succeed(
|
|
15
|
+
? Effect.succeed(demoUsers[name])
|
|
19
16
|
: Effect.fail(
|
|
20
17
|
new Action.Unauthenticated({ message: "Unknown demo token." })
|
|
21
18
|
);
|
|
22
19
|
};
|
|
23
20
|
|
|
24
|
-
// Server only: `Login`'s provider. As an OAuth protected resource it also publishes discovery,
|
|
25
|
-
// so an MCP client that gets a 401 finds where to sign in, asking for read first. The resource
|
|
26
|
-
// is the server's public URL, known only at startup, so it is read from config.
|
|
27
21
|
export const authenticate = Authentication.layer(Login, verify, {
|
|
28
22
|
protectedResource: Effect.map(
|
|
29
23
|
Config.String("PUBLIC_URL").pipe(
|
|
@@ -32,7 +26,6 @@ export const authenticate = Authentication.layer(Login, verify, {
|
|
|
32
26
|
(url) =>
|
|
33
27
|
({
|
|
34
28
|
resource: `${url}/mcp`,
|
|
35
|
-
// DEMO ONLY: no server is there. Yours is your authorization server's issuer.
|
|
36
29
|
authorizationServers: ["https://auth.example.com"],
|
|
37
30
|
scopesSupported: [scopes.read, scopes.write],
|
|
38
31
|
scopesRequired: [scopes.read],
|
|
@@ -12,8 +12,6 @@ export const Note = Schema.Struct({
|
|
|
12
12
|
|
|
13
13
|
export type Note = typeof Note.Type;
|
|
14
14
|
|
|
15
|
-
// A declared error is part of the contract: every client decodes it as this class, and its
|
|
16
|
-
// status is what HTTP answers with.
|
|
17
15
|
export class NoteNotFound extends Schema.TaggedError<
|
|
18
16
|
NoteNotFound,
|
|
19
17
|
{ readonly brand: unique symbol }
|
|
@@ -33,15 +31,10 @@ export class RequestConflict extends Schema.TaggedError<
|
|
|
33
31
|
{ readonly brand: unique symbol }
|
|
34
32
|
>()("RequestConflict", { requestId: Schema.String }, { httpApiStatus: 409 }) {}
|
|
35
33
|
|
|
36
|
-
// A description on an input field is that field's help on every surface: beside its flag or
|
|
37
|
-
// argument in a command's `--help`, and in the input schema a model reads from the MCP tool
|
|
38
|
-
// listing and the OpenAPI document. A field without one has a flag with no help.
|
|
39
34
|
const Id = Schema.String.annotate({
|
|
40
35
|
description: "The note's id, as the list of your notes shows it.",
|
|
41
36
|
});
|
|
42
37
|
|
|
43
|
-
// A check's `message` is what a caller reads when the value fails it, in place of the
|
|
44
|
-
// generic "Expected a value with a length of at least 1". A model and a CLI user read it too.
|
|
45
38
|
const Title = Schema.String.check(
|
|
46
39
|
Schema.isMinLength(1, { message: "A note needs a title." }),
|
|
47
40
|
Schema.isPattern(/^[^\r\n\u2028\u2029]*$/u, {
|
|
@@ -89,7 +82,6 @@ export const CreateNote = Action.make("createNote", {
|
|
|
89
82
|
error: RequestConflict,
|
|
90
83
|
readOnly: false,
|
|
91
84
|
caller: CurrentUser,
|
|
92
|
-
// No `idempotentHint: true`: a call without a `requestId` creates a note each time.
|
|
93
85
|
mcp: { destructiveHint: false },
|
|
94
86
|
});
|
|
95
87
|
|
|
@@ -110,8 +102,6 @@ export const UpdateNote = Action.make("updateNote", {
|
|
|
110
102
|
error: [NoteNotFound, NoteChanged],
|
|
111
103
|
readOnly: false,
|
|
112
104
|
caller: CurrentUser,
|
|
113
|
-
// No `destructiveHint`: an update replaces the body, so it is destructive, MCP's default for
|
|
114
|
-
// a write. `createNote` and `importNotes`, which only add, say `false`.
|
|
115
105
|
});
|
|
116
106
|
|
|
117
107
|
export const DeleteNote = Action.make("deleteNote", {
|
|
@@ -122,8 +112,6 @@ export const DeleteNote = Action.make("deleteNote", {
|
|
|
122
112
|
caller: CurrentUser,
|
|
123
113
|
});
|
|
124
114
|
|
|
125
|
-
// Imports share note content with `createNote`, but exclude request keys because bulk creation
|
|
126
|
-
// does not deduplicate.
|
|
127
115
|
export const ImportNotes = Action.make("importNotes", {
|
|
128
116
|
description:
|
|
129
117
|
"Create several notes at once, such as those `notes export` wrote. Every note is checked " +
|
|
@@ -6,10 +6,6 @@ import { CurrentUser } from "../identity/Identity.js";
|
|
|
6
6
|
import { Notes } from "./Notes.js";
|
|
7
7
|
import { NoteStore } from "./store.js";
|
|
8
8
|
|
|
9
|
-
// The builder runs once, at startup, however many surfaces serve these actions: it is where a
|
|
10
|
-
// handler gets the services that last the process, never the caller. `CurrentUser` is read per
|
|
11
|
-
// call, inside each handler. Each handler is a thin step from the contract to the store, and
|
|
12
|
-
// none checks a scope: `authorize` has run before it.
|
|
13
9
|
export const notes = Action.implement(
|
|
14
10
|
Notes,
|
|
15
11
|
Effect.gen(function* () {
|
|
@@ -3,10 +3,6 @@ import { Context, Effect, Layer } from "effect";
|
|
|
3
3
|
import { NoteChanged, NoteNotFound, RequestConflict } from "./Notes.js";
|
|
4
4
|
import type { Note } from "./Notes.js";
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* Where notes live, each owned by one user. Ownership is checked here, in the data access, so no
|
|
8
|
-
* surface and no handler can skip it.
|
|
9
|
-
*/
|
|
10
6
|
export class NoteStore extends Context.Service<
|
|
11
7
|
NoteStore,
|
|
12
8
|
{
|
|
@@ -21,7 +17,6 @@ export class NoteStore extends Context.Service<
|
|
|
21
17
|
body: string,
|
|
22
18
|
requestId?: string
|
|
23
19
|
) => Effect.Effect<Note, RequestConflict>;
|
|
24
|
-
/** Every note or none: a database does this in one transaction. */
|
|
25
20
|
readonly createAll: (
|
|
26
21
|
owner: string,
|
|
27
22
|
notes: ReadonlyArray<{ readonly title: string; readonly body: string }>
|
|
@@ -39,16 +34,9 @@ export class NoteStore extends Context.Service<
|
|
|
39
34
|
readonly count: Effect.Effect<number>;
|
|
40
35
|
}
|
|
41
36
|
>()("notes-service/NoteStore") {
|
|
42
|
-
// DEMO ONLY: notes and replay records live in this process's memory, without eviction, so
|
|
43
|
-
// they neither survive a restart nor coordinate separate server instances. A deployed store
|
|
44
|
-
// commits the mutation and its replay record in one transaction, enforces uniqueness of
|
|
45
|
-
// caller, operation and key, and defines a retention window: an expired key no longer
|
|
46
|
-
// protects a retry. A separate network call is not made atomic by a local record.
|
|
47
37
|
static readonly layerMemory = Layer.sync(NoteStore, () => {
|
|
48
|
-
// One map per owner: a note is only ever looked up among its owner's, whatever id is asked.
|
|
49
38
|
const notes = new Map<string, Map<string, Note>>();
|
|
50
39
|
|
|
51
|
-
// Keep the original result even after the note is updated or deleted.
|
|
52
40
|
const creations = new Map<string, Map<string, Note>>();
|
|
53
41
|
|
|
54
42
|
const ownedBy = (owner: string) =>
|
|
@@ -60,6 +48,57 @@ export class NoteStore extends Context.Service<
|
|
|
60
48
|
return note;
|
|
61
49
|
};
|
|
62
50
|
|
|
51
|
+
const createdAtomically = (
|
|
52
|
+
owner: string,
|
|
53
|
+
title: string,
|
|
54
|
+
body: string,
|
|
55
|
+
requestId: string | undefined
|
|
56
|
+
): Note | RequestConflict => {
|
|
57
|
+
if (requestId !== undefined) {
|
|
58
|
+
const recorded = creations.get(owner)?.get(requestId);
|
|
59
|
+
|
|
60
|
+
if (recorded !== undefined) {
|
|
61
|
+
return recorded.title === title && recorded.body === body
|
|
62
|
+
? recorded
|
|
63
|
+
: new RequestConflict({ requestId });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const note = save(owner, {
|
|
68
|
+
id: crypto.randomUUID(),
|
|
69
|
+
title,
|
|
70
|
+
body,
|
|
71
|
+
revision: 1,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (requestId !== undefined) {
|
|
75
|
+
const requests = creations.get(owner) ?? new Map<string, Note>();
|
|
76
|
+
requests.set(requestId, note);
|
|
77
|
+
creations.set(owner, requests);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return note;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const updatedAtomically = (
|
|
84
|
+
owner: string,
|
|
85
|
+
id: string,
|
|
86
|
+
body: string,
|
|
87
|
+
revision: number
|
|
88
|
+
): Note | NoteNotFound | NoteChanged => {
|
|
89
|
+
const current = ownedBy(owner).get(id);
|
|
90
|
+
|
|
91
|
+
if (current === undefined) {
|
|
92
|
+
return new NoteNotFound({ id });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (current.revision !== revision) {
|
|
96
|
+
return new NoteChanged({ id, revision: current.revision });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return save(owner, { ...current, body, revision: revision + 1 });
|
|
100
|
+
};
|
|
101
|
+
|
|
63
102
|
const get = Effect.fn("NoteStore.get")(function* (
|
|
64
103
|
owner: string,
|
|
65
104
|
id: string
|
|
@@ -78,34 +117,13 @@ export class NoteStore extends Context.Service<
|
|
|
78
117
|
get,
|
|
79
118
|
create: Effect.fn("NoteStore.create")(
|
|
80
119
|
function* (owner, title, body, requestId) {
|
|
81
|
-
|
|
82
|
-
const recorded = creations.get(owner)?.get(requestId);
|
|
83
|
-
|
|
84
|
-
if (recorded !== undefined) {
|
|
85
|
-
if (recorded.title !== title || recorded.body !== body) {
|
|
86
|
-
return yield* new RequestConflict({ requestId });
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return recorded;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
120
|
+
const created = createdAtomically(owner, title, body, requestId);
|
|
92
121
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const note = save(owner, {
|
|
96
|
-
id: crypto.randomUUID(),
|
|
97
|
-
title,
|
|
98
|
-
body,
|
|
99
|
-
revision: 1,
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
if (requestId !== undefined) {
|
|
103
|
-
const requests = creations.get(owner) ?? new Map<string, Note>();
|
|
104
|
-
requests.set(requestId, note);
|
|
105
|
-
creations.set(owner, requests);
|
|
122
|
+
if (created instanceof RequestConflict) {
|
|
123
|
+
return yield* created;
|
|
106
124
|
}
|
|
107
125
|
|
|
108
|
-
return
|
|
126
|
+
return created;
|
|
109
127
|
}
|
|
110
128
|
),
|
|
111
129
|
createAll: (owner, all) =>
|
|
@@ -114,22 +132,18 @@ export class NoteStore extends Context.Service<
|
|
|
114
132
|
save(owner, { id: crypto.randomUUID(), title, body, revision: 1 })
|
|
115
133
|
)
|
|
116
134
|
),
|
|
117
|
-
// No `yield*` between the read and the write: a fiber may be set aside at one, and two
|
|
118
|
-
// updates that each read revision 1 before either wrote would both succeed. A database
|
|
119
|
-
// does this with a conditional update or a transaction.
|
|
120
135
|
update: Effect.fn("NoteStore.update")(
|
|
121
136
|
function* (owner, id, body, revision) {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
if (current === undefined) {
|
|
125
|
-
return yield* new NoteNotFound({ id });
|
|
126
|
-
}
|
|
137
|
+
const updated = updatedAtomically(owner, id, body, revision);
|
|
127
138
|
|
|
128
|
-
if (
|
|
129
|
-
|
|
139
|
+
if (
|
|
140
|
+
updated instanceof NoteNotFound ||
|
|
141
|
+
updated instanceof NoteChanged
|
|
142
|
+
) {
|
|
143
|
+
return yield* updated;
|
|
130
144
|
}
|
|
131
145
|
|
|
132
|
-
return
|
|
146
|
+
return updated;
|
|
133
147
|
}
|
|
134
148
|
),
|
|
135
149
|
remove: Effect.fn("NoteStore.remove")(function* (owner, id) {
|