@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
|
@@ -7,7 +7,7 @@ import { HttpClient, HttpClientRequest } from "effect/http";
|
|
|
7
7
|
import { RateLimiter } from "effect/persistence";
|
|
8
8
|
import { TestClock } from "effect/testing";
|
|
9
9
|
|
|
10
|
-
import type {
|
|
10
|
+
import type { demoMembers } from "../../src/identity/members.js";
|
|
11
11
|
import { issueEvents } from "../../src/issues/events.js";
|
|
12
12
|
import { issues } from "../../src/issues/handlers.js";
|
|
13
13
|
import { IssueChanged } from "../../src/issues/Issues.js";
|
|
@@ -22,9 +22,8 @@ const inProcess = Layer.mergeAll(
|
|
|
22
22
|
Event.layerMemory
|
|
23
23
|
);
|
|
24
24
|
|
|
25
|
-
// The changes a member's watch receives, as data, while `act` changes issues.
|
|
26
25
|
const watched = <E, R>(
|
|
27
|
-
member: keyof typeof
|
|
26
|
+
member: keyof typeof demoMembers,
|
|
28
27
|
input: typeof IssueChanged.input.Type,
|
|
29
28
|
count: number,
|
|
30
29
|
act: (
|
|
@@ -43,8 +42,6 @@ const watched = <E, R>(
|
|
|
43
42
|
Effect.forkChild
|
|
44
43
|
);
|
|
45
44
|
|
|
46
|
-
// Lets the forked watch run up to its subscription first: started after `act`, it would wait
|
|
47
|
-
// for changes already made.
|
|
48
45
|
yield* TestClock.adjust("0 millis");
|
|
49
46
|
yield* act(client);
|
|
50
47
|
|
|
@@ -107,7 +104,6 @@ describe("issue.changed", () => {
|
|
|
107
104
|
.commentOnIssue({ id: filed.id, text: "Seen it." })
|
|
108
105
|
.pipe(as("ada"));
|
|
109
106
|
yield* client.closeIssue({ id: filed.id }).pipe(as("ada"));
|
|
110
|
-
// Closing it again changes nothing, and nothing is sent.
|
|
111
107
|
yield* client.closeIssue({ id: filed.id }).pipe(as("ada"));
|
|
112
108
|
yield* client
|
|
113
109
|
.commentOnIssue({ id: filed.id, text: "Fixed." })
|
|
@@ -152,15 +148,12 @@ describe("issue.changed", () => {
|
|
|
152
148
|
);
|
|
153
149
|
});
|
|
154
150
|
|
|
155
|
-
// The subscriber's secret: `whsec_` and 24 to 64 random bytes in base64.
|
|
156
151
|
const secret = `whsec_${Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString("base64")}`;
|
|
157
152
|
|
|
158
153
|
const Delivered = Schema.fromJsonString(
|
|
159
154
|
Schema.Record(Schema.String, Schema.Json)
|
|
160
155
|
);
|
|
161
156
|
|
|
162
|
-
// linus's server: it echoes the verification's challenge, which proves it asked for these
|
|
163
|
-
// deliveries, and acknowledges each delivery with a 2xx.
|
|
164
157
|
const subscriber =
|
|
165
158
|
(received: Queue.Enqueue<typeof Delivered.Type>) =>
|
|
166
159
|
async (request: Request) => {
|
|
@@ -13,8 +13,6 @@ import { publicRoutes, rpc } from "../../src/routes.js";
|
|
|
13
13
|
import { Rpc } from "../../src/Rpc.js";
|
|
14
14
|
import { services } from "../support/services.js";
|
|
15
15
|
|
|
16
|
-
// The public port's routes and the web app's rpcs under one `Testing.layer`, as server.ts
|
|
17
|
-
// serves them under one router.
|
|
18
16
|
const served = RpcClient.layerProtocolHttp({ url: "/rpc" }).pipe(
|
|
19
17
|
Layer.provide(RpcSerialization.layerJson),
|
|
20
18
|
Layer.provideMerge(
|
|
@@ -69,7 +67,6 @@ describe("exportIssues", () => {
|
|
|
69
67
|
)
|
|
70
68
|
);
|
|
71
69
|
|
|
72
|
-
// The web app's call carries its session cookie, as a browser attaches it.
|
|
73
70
|
const overRpc = yield* Stream.runCollect(
|
|
74
71
|
web.exportIssues({ status: "open" })
|
|
75
72
|
).pipe(
|
|
@@ -3,14 +3,13 @@ import * as Action from "@gjermundgaraba/effect-actions/Action";
|
|
|
3
3
|
import { Array, Effect, Layer, References } from "effect";
|
|
4
4
|
import { RateLimiter } from "effect/persistence";
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { demoMembers } from "../../src/identity/members.js";
|
|
7
7
|
import { issues } from "../../src/issues/handlers.js";
|
|
8
8
|
import { IssueNotFound, Issues, RateLimited } from "../../src/issues/Issues.js";
|
|
9
9
|
import { IssueStore } from "../../src/issues/store.js";
|
|
10
10
|
import { status } from "../../src/workspaces/handlers.js";
|
|
11
11
|
import { as } from "../support/as.js";
|
|
12
12
|
|
|
13
|
-
// The two startup services the issues' builder yields, and nothing else of the application.
|
|
14
13
|
const services = Layer.mergeAll(
|
|
15
14
|
IssueStore.layerMemory,
|
|
16
15
|
RateLimiter.layer.pipe(Layer.provide(RateLimiter.layerStoreMemory))
|
|
@@ -45,9 +44,11 @@ describe("issues, in process", () => {
|
|
|
45
44
|
const client = yield* Action.client(issues);
|
|
46
45
|
const store = yield* IssueStore;
|
|
47
46
|
|
|
48
|
-
// Written to the store, not through `createIssue`: twenty-one are over its write limit.
|
|
49
47
|
yield* Effect.forEach(Array.range(1, 21), (n) =>
|
|
50
|
-
store.create(
|
|
48
|
+
store.create(demoMembers.ada.workspace, {
|
|
49
|
+
title: `Issue ${n}`,
|
|
50
|
+
body: "",
|
|
51
|
+
})
|
|
51
52
|
);
|
|
52
53
|
yield* client.closeIssue({ id: "ACME-2" }).pipe(as("ada"));
|
|
53
54
|
|
|
@@ -86,12 +87,8 @@ describe("issues, in process", () => {
|
|
|
86
87
|
it.effect("keeps both of two comments made at once", () =>
|
|
87
88
|
Effect.gen(function* () {
|
|
88
89
|
const store = yield* IssueStore;
|
|
89
|
-
const { workspace } =
|
|
90
|
+
const { workspace } = demoMembers.ada;
|
|
90
91
|
|
|
91
|
-
// Asked of the store, not through `commentOnIssue`, whose write limit this would spend.
|
|
92
|
-
// A fiber is set aside after every `steps` steps, and two that run at once then take
|
|
93
|
-
// turns. Each count sets the first comment aside at another point of its work; under
|
|
94
|
-
// three, the two do not finish.
|
|
95
92
|
const kept = yield* Effect.forEach(Array.range(3, 60), (steps) =>
|
|
96
93
|
Effect.gen(function* () {
|
|
97
94
|
const { id } = yield* store.create(workspace, {
|
|
@@ -126,7 +123,6 @@ describe("issues, in process", () => {
|
|
|
126
123
|
.pipe(as("ada"))
|
|
127
124
|
);
|
|
128
125
|
|
|
129
|
-
// Ten writes are spent, by two actions: the eleventh, of a third, is refused.
|
|
130
126
|
expect(
|
|
131
127
|
yield* Effect.flip(
|
|
132
128
|
client.closeIssue({ id: "ACME-1" }).pipe(as("ada"))
|
|
@@ -149,8 +145,6 @@ describe("issues, in process", () => {
|
|
|
149
145
|
"builds only the implementations whose actions a client lists",
|
|
150
146
|
() =>
|
|
151
147
|
Effect.gen(function* () {
|
|
152
|
-
// Two implementations are passed, and `actions` selects the issues': `status` is left
|
|
153
|
-
// out, unbuilt, so `services` need not hold the registry its builder yields.
|
|
154
148
|
const client = yield* Action.client([status, issues], {
|
|
155
149
|
actions: Issues,
|
|
156
150
|
});
|
|
@@ -15,9 +15,6 @@ import { memory } from "../src/memory.js";
|
|
|
15
15
|
import { adminRoutes, publicRoutes } from "../src/routes.js";
|
|
16
16
|
import { services } from "./support/services.js";
|
|
17
17
|
|
|
18
|
-
// Each port as a web handler, `Request` in and `Response` out: what a platform without
|
|
19
|
-
// Effect's server runs, and what a test sends raw requests to. A handler builds its routes
|
|
20
|
-
// once; the test owns it, and disposes it when done.
|
|
21
18
|
const web = HttpRouter.toWebHandler(
|
|
22
19
|
publicRoutes.pipe(
|
|
23
20
|
Layer.provide(HttpServer.layerServices),
|
|
@@ -163,8 +160,6 @@ describe("the request policy", () => {
|
|
|
163
160
|
expect([here.status, foreignHost.status]).toStrictEqual([200, 403]);
|
|
164
161
|
});
|
|
165
162
|
|
|
166
|
-
// Behind a proxy ending TLS, Node's server sees `http:`: the public URL's origin is allowed by
|
|
167
|
-
// name, not as the request's own, so a page of it is answered whatever the proxy forwards.
|
|
168
163
|
it.effect("answers the public URL's page behind a proxy ending TLS", () =>
|
|
169
164
|
Effect.gen(function* () {
|
|
170
165
|
const client = yield* HttpClient.HttpClient;
|
|
@@ -206,7 +201,6 @@ describe("the request policy", () => {
|
|
|
206
201
|
)
|
|
207
202
|
);
|
|
208
203
|
|
|
209
|
-
// `Testing.layer(handler)` gives the typed clients the same handler the raw requests hit.
|
|
210
204
|
it.effect(
|
|
211
205
|
"leaves the typed client, served from the same web handler, alone",
|
|
212
206
|
() =>
|
|
@@ -11,9 +11,6 @@ import { AdminHttp, Http } from "../src/Api.js";
|
|
|
11
11
|
import { adminRoutes, builders, publicRoutes } from "../src/routes.js";
|
|
12
12
|
import { services } from "./support/services.js";
|
|
13
13
|
|
|
14
|
-
// The host's surfaces, as routes.ts builds them, answering in memory: no port is opened. Each
|
|
15
|
-
// port is a `Testing.layer` of its own, and what they share is provided above both, as
|
|
16
|
-
// server.ts does.
|
|
17
14
|
const shared = builders.pipe(Layer.provideMerge(services()));
|
|
18
15
|
|
|
19
16
|
const onPublicPort = Effect.provide(Testing.layer(publicRoutes));
|
|
@@ -98,7 +95,6 @@ describe("the public port", () => {
|
|
|
98
95
|
"refuses, at the MCP endpoint, a page of this host that is not on its list",
|
|
99
96
|
() =>
|
|
100
97
|
Effect.gen(function* () {
|
|
101
|
-
// The request policy lets both through: one is the request's own origin, one is listed.
|
|
102
98
|
const listed = yield* from("/mcp", "http://localhost:6274");
|
|
103
99
|
|
|
104
100
|
const sameHost = yield* from(
|
|
@@ -133,8 +129,6 @@ describe("the two ports", () => {
|
|
|
133
129
|
(client) => client.getStatus()
|
|
134
130
|
).pipe(onAdminPort);
|
|
135
131
|
|
|
136
|
-
// `status`'s builder names the start it ran in. With `builders` above both ports it ran
|
|
137
|
-
// once. Provide `memory` alone instead, and each port runs it: two instances.
|
|
138
132
|
expect(onAdmin.instance).toBe(onPublic.instance);
|
|
139
133
|
}).pipe(Effect.provide(shared))
|
|
140
134
|
);
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
|
|
3
3
|
import { CurrentMember } from "../../src/identity/Identity.js";
|
|
4
|
-
import {
|
|
4
|
+
import { demoMembers } from "../../src/identity/members.js";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const as = (member: keyof typeof members) =>
|
|
9
|
-
Effect.provideService(CurrentMember, members[member]);
|
|
6
|
+
export const as = (member: keyof typeof demoMembers) =>
|
|
7
|
+
Effect.provideService(CurrentMember, demoMembers[member]);
|
|
@@ -4,9 +4,6 @@ import type { Response } from "effect/ai";
|
|
|
4
4
|
|
|
5
5
|
type Reply = readonly Response.PartEncoded[];
|
|
6
6
|
|
|
7
|
-
// A model for a test: it answers its first request with the first reply it was given, its
|
|
8
|
-
// second with the second, whatever the prompt says. A reply is the parts a provider sends, or
|
|
9
|
-
// an Effect that makes them: `down` to fail as a provider that is down does, or one that waits.
|
|
10
7
|
export const scripted = (
|
|
11
8
|
...replies: ReadonlyArray<Reply | Effect.Effect<Reply, AiError.AiError>>
|
|
12
9
|
) =>
|
|
@@ -6,9 +6,6 @@ import { memory } from "../../src/memory.js";
|
|
|
6
6
|
|
|
7
7
|
const unreached = async () => new Response(null, { status: 503 });
|
|
8
8
|
|
|
9
|
-
// What the public routes need, in memory: the application's services, and for webhook delivery
|
|
10
|
-
// a client and a resolver. `subscribers` answers each POST in place of a member's server, and
|
|
11
|
-
// hooks.example.com resolves to a public address: delivery refuses a private or loopback one.
|
|
12
9
|
export const services = (subscribers = Testing.layer(unreached)) =>
|
|
13
10
|
Layer.mergeAll(
|
|
14
11
|
memory,
|
|
@@ -3,10 +3,6 @@ import * as ActionRpc from "@gjermundgaraba/effect-actions/ActionRpc";
|
|
|
3
3
|
import { ListRooms, MessagePosted, Messages } from "./chat/Chat.js";
|
|
4
4
|
import { Login } from "./identity/Identity.js";
|
|
5
5
|
|
|
6
|
-
// The RPC binding: one native rpc per action, named after it, and a stream rpc for
|
|
7
|
-
// `MessagePosted`, the protected ones requiring `Login`. It is plain data and imports no server
|
|
8
|
-
// code, so the server and a browser page both import this one value. `Rpc.group` is the native
|
|
9
|
-
// `RpcGroup`, for Effect's own `RpcClient.make`.
|
|
10
6
|
export const Rpc = ActionRpc.make([ListRooms, ...Messages, MessagePosted], {
|
|
11
7
|
authentication: Login,
|
|
12
8
|
});
|
|
@@ -7,37 +7,23 @@ import { Socket } from "effect/socket";
|
|
|
7
7
|
import { MessagePosted, roomHeader } from "./chat/Chat.js";
|
|
8
8
|
import { Rpc } from "./Rpc.js";
|
|
9
9
|
|
|
10
|
-
// Client module: what a web page imports. It holds the binding and nothing of the server, so a
|
|
11
|
-
// bundler gets the contracts and Effect's RPC client, no handler and nothing of Node.
|
|
12
|
-
|
|
13
|
-
// Effect's own client protocol: one WebSocket, speaking the server's JSON. This layer owns
|
|
14
|
-
// the socket, so a page provides it once, around its whole program, and every call shares
|
|
15
|
-
// that one socket. Which WebSocket is left to the host: a page provides
|
|
16
|
-
// `Socket.layerWebSocketConstructorGlobal`, the browser's own, and client.ts provides Node's.
|
|
17
10
|
export const connection = (url: string) =>
|
|
18
11
|
RpcClient.layerProtocolSocket().pipe(
|
|
19
12
|
Layer.provide(Socket.layerWebSocket(url)),
|
|
20
13
|
Layer.provide(RpcSerialization.layerJson)
|
|
21
14
|
);
|
|
22
15
|
|
|
23
|
-
// The chat as a page uses it, on the connection provided around it.
|
|
24
16
|
export const openChat = Effect.gen(function* () {
|
|
25
|
-
// One typed method per action of the binding, all on the one connection.
|
|
26
17
|
const client = yield* ActionRpc.client(Rpc);
|
|
27
18
|
|
|
28
19
|
return {
|
|
29
|
-
// The public rpc: no token.
|
|
30
20
|
rooms: Effect.map(client.listRooms(), ({ rooms }) => rooms),
|
|
31
21
|
|
|
32
22
|
room: (name: string, token: string) => {
|
|
33
|
-
// A browser WebSocket cannot set an `Authorization` header on its upgrade, so the token
|
|
34
|
-
// travels on each message, and the room beside it. A refreshed token needs no new socket.
|
|
35
23
|
const headers = { authorization: `Bearer ${token}`, [roomHeader]: name };
|
|
36
24
|
const here = RpcClient.withHeaders(headers);
|
|
37
25
|
|
|
38
26
|
return {
|
|
39
|
-
// What the room holds after the last id the page has. A token may expire while the
|
|
40
|
-
// page stays open, so each refusal becomes what the page shows.
|
|
41
27
|
read: (after: number) =>
|
|
42
28
|
client.readMessages({ after }).pipe(
|
|
43
29
|
here,
|
|
@@ -58,14 +44,6 @@ export const openChat = Effect.gen(function* () {
|
|
|
58
44
|
|
|
59
45
|
post: (text: string) => client.postMessage({ text }).pipe(here),
|
|
60
46
|
|
|
61
|
-
// Each message posted to the room after the last id the page holds: a page opening a
|
|
62
|
-
// room reads it, then watches from the last id it read, so a post between the two is
|
|
63
|
-
// not lost; 0 sends the room's history first. `watch` connects again after a drop and
|
|
64
|
-
// resumes from the last message it delivered, so the page never subscribes by hand; a
|
|
65
|
-
// `Gap` says some were missed, or are another run's, so the page reads the room again
|
|
66
|
-
// with `read(0)`. The headers go with the
|
|
67
|
-
// stream, as `here` puts them on a call, and so with every request it sends again. A
|
|
68
|
-
// refusal ends it, as when it connects again with a token that has expired.
|
|
69
47
|
messages: (after: number) =>
|
|
70
48
|
client
|
|
71
49
|
.watch(MessagePosted, {}, { cursor: String(after) })
|
|
@@ -76,8 +54,6 @@ export const openChat = Effect.gen(function* () {
|
|
|
76
54
|
)
|
|
77
55
|
),
|
|
78
56
|
|
|
79
|
-
// A declared error arrives as the contract's class. A message someone else already
|
|
80
|
-
// removed is gone, which is what the page asked for.
|
|
81
57
|
remove: (id: number) =>
|
|
82
58
|
client.deleteMessage({ id }).pipe(
|
|
83
59
|
here,
|
|
@@ -4,8 +4,6 @@ import { Schema } from "effect";
|
|
|
4
4
|
|
|
5
5
|
import { CurrentMember } from "../identity/Identity.js";
|
|
6
6
|
|
|
7
|
-
// A message's `id` counts up within its room, so a client asks for what came after the last
|
|
8
|
-
// one it holds.
|
|
9
7
|
export const Message = Schema.Struct({
|
|
10
8
|
id: Schema.Int,
|
|
11
9
|
author: Schema.String,
|
|
@@ -14,35 +12,20 @@ export const Message = Schema.Struct({
|
|
|
14
12
|
|
|
15
13
|
export type Message = typeof Message.Type;
|
|
16
14
|
|
|
17
|
-
// A declared error is part of the contract: every client decodes it as this class. Its status
|
|
18
|
-
// is for a JSON binding, should these actions be served as one too: an rpc carries no status,
|
|
19
|
-
// and an error without one answers 422 over HTTP.
|
|
20
15
|
export class MessageNotFound extends Schema.TaggedError<
|
|
21
16
|
MessageNotFound,
|
|
22
17
|
{ readonly brand: unique symbol }
|
|
23
18
|
>()("MessageNotFound", { id: Schema.Int }, { httpApiStatus: 404 }) {}
|
|
24
19
|
|
|
25
|
-
// A refusal carries what the caller sent and no more: who wrote the message is for the actions
|
|
26
|
-
// that read it, under their own rule.
|
|
27
20
|
export class NotAuthor extends Schema.TaggedError<
|
|
28
21
|
NotAuthor,
|
|
29
22
|
{ readonly brand: unique symbol }
|
|
30
23
|
>()("NotAuthor", { id: Schema.Int }, { httpApiStatus: 403 }) {}
|
|
31
24
|
|
|
32
|
-
// Which room a call is about is not an input of any action: it travels beside the call, in
|
|
33
|
-
// this header, as the token does, because `authorize` sees the action and not its input, and
|
|
34
|
-
// the membership rule needs the room. With the room in the input, that check belongs in the
|
|
35
|
-
// store, as `MessageStore.remove` takes the author and refuses with `NotAuthor`, never in a
|
|
36
|
-
// helper each handler must remember to call. The client sets the header; on the server,
|
|
37
|
-
// room.ts reads it. A call without it is about the default room, so `listRooms`, which is
|
|
38
|
-
// about none, sends none; yours may refuse such a call instead.
|
|
39
25
|
export const roomHeader = "x-room";
|
|
40
26
|
|
|
41
27
|
export const defaultRoom = "lobby";
|
|
42
28
|
|
|
43
|
-
// Public: `caller: Action.Anyone`, so a page shows the rooms before anyone has signed in. It
|
|
44
|
-
// tells a signed-out caller every room's name and how many messages it holds, a room appearing
|
|
45
|
-
// once someone posts: decide whether yours may.
|
|
46
29
|
export const ListRooms = Action.make("listRooms", {
|
|
47
30
|
description:
|
|
48
31
|
"List the rooms someone has posted in, and how many messages each holds.",
|
|
@@ -57,8 +40,6 @@ export const ListRooms = Action.make("listRooms", {
|
|
|
57
40
|
|
|
58
41
|
const signedIn = { caller: CurrentMember } as const;
|
|
59
42
|
|
|
60
|
-
// Reads once: a page opening a room reads what is there, then watches `MessagePosted` below from
|
|
61
|
-
// the last id it read.
|
|
62
43
|
export const ReadMessages = Action.make("readMessages", {
|
|
63
44
|
...signedIn,
|
|
64
45
|
description: "Read the room's messages after the given id; 0 reads them all.",
|
|
@@ -87,9 +68,6 @@ export const DeleteMessage = Action.make("deleteMessage", {
|
|
|
87
68
|
|
|
88
69
|
export const Messages = [ReadMessages, PostMessage, DeleteMessage] as const;
|
|
89
70
|
|
|
90
|
-
// Each message posted to the room, as it is posted: a page watches it on its open socket,
|
|
91
|
-
// about the room its header names, as the actions are. Signed-in, so each watch authenticates
|
|
92
|
-
// and runs `authorize` as it starts, refused for a member outside the room.
|
|
93
71
|
export const MessagePosted = Event.make("message.posted", {
|
|
94
72
|
description: "A message posted to the room.",
|
|
95
73
|
payload: Message,
|
|
@@ -4,9 +4,6 @@ import { Effect } from "effect";
|
|
|
4
4
|
import { CurrentMember } from "../identity/Identity.js";
|
|
5
5
|
import { CurrentRoom } from "./room.js";
|
|
6
6
|
|
|
7
|
-
// The one rule: a member reads and writes only the rooms they belong to. It runs before every
|
|
8
|
-
// protected handler, on every surface, and reads two request-time services: the caller, from
|
|
9
|
-
// authentication, and the room, from the middleware.
|
|
10
7
|
export const authorize = Effect.fn("authorize")(function* () {
|
|
11
8
|
const member = yield* CurrentMember;
|
|
12
9
|
const room = yield* CurrentRoom;
|
|
@@ -10,9 +10,6 @@ const idOf = Schema.decodeUnknownOption(
|
|
|
10
10
|
Schema.NumberFromString.pipe(Schema.check(Schema.isInt()))
|
|
11
11
|
);
|
|
12
12
|
|
|
13
|
-
// A cursor this source gives is `<epoch>:<id>`, the store's epoch and the id of the last message
|
|
14
|
-
// a page holds, so one from before a restart of the memory store is told apart. A page starting
|
|
15
|
-
// a watch gives the id alone, as `readMessages`' `after` is, which is the store's now.
|
|
16
13
|
const positionOf = (epoch: string, cursor: string | null) => {
|
|
17
14
|
const parts =
|
|
18
15
|
cursor?.includes(":") === true ? cursor.split(":") : [epoch, cursor];
|
|
@@ -22,9 +19,6 @@ const positionOf = (epoch: string, cursor: string | null) => {
|
|
|
22
19
|
: Option.none();
|
|
23
20
|
};
|
|
24
21
|
|
|
25
|
-
// The room's messages as an event, read from the store that keeps them: a check source.
|
|
26
|
-
// `postMessage` notifies after each post, so a page receives it at once, not at its next read
|
|
27
|
-
// every 5 seconds.
|
|
28
22
|
export const messageEvents = Event.implement(
|
|
29
23
|
MessagePosted,
|
|
30
24
|
Effect.gen(function* () {
|
|
@@ -41,9 +35,6 @@ export const messageEvents = Event.implement(
|
|
|
41
35
|
(id) => id >= 0 && id <= last
|
|
42
36
|
);
|
|
43
37
|
|
|
44
|
-
// No cursor starts after the room's last message. One this room never gave, such as
|
|
45
|
-
// one from before a restart, is answered `truncated` from there: the page learns its
|
|
46
|
-
// messages may be another run's, and reads the room again with `readMessages`.
|
|
47
38
|
if (Option.isNone(after)) {
|
|
48
39
|
return {
|
|
49
40
|
cursor: `${store.epoch}:${last}`,
|
|
@@ -56,17 +47,12 @@ export const messageEvents = Event.implement(
|
|
|
56
47
|
|
|
57
48
|
return {
|
|
58
49
|
cursor: `${store.epoch}:${messages.at(-1)?.id ?? after.value}`,
|
|
59
|
-
// A message's id counts within its room and the store's epoch, so the event's id
|
|
60
|
-
// names all three: a watch drops an id it has seen, and after a restart of the memory
|
|
61
|
-
// store `lobby:1` is another message.
|
|
62
50
|
events: messages.map((data) => ({
|
|
63
51
|
eventId: `${store.epoch}:${room}:${data.id}`,
|
|
64
52
|
data,
|
|
65
53
|
})),
|
|
66
54
|
};
|
|
67
55
|
}),
|
|
68
|
-
// A notify for one room wakes that room's pages alone: the key is the room the watch's
|
|
69
|
-
// header names, read as `check` reads it.
|
|
70
56
|
key: () => CurrentRoom,
|
|
71
57
|
};
|
|
72
58
|
}),
|
|
@@ -8,7 +8,6 @@ import { ListRooms, MessagePosted, Messages } from "./Chat.js";
|
|
|
8
8
|
import { CurrentRoom } from "./room.js";
|
|
9
9
|
import { MessageStore } from "./store.js";
|
|
10
10
|
|
|
11
|
-
// A public action takes no `authorize`: nothing would run it.
|
|
12
11
|
export const rooms = Action.implement(
|
|
13
12
|
ListRooms,
|
|
14
13
|
Effect.gen(function* () {
|
|
@@ -18,8 +17,6 @@ export const rooms = Action.implement(
|
|
|
18
17
|
})
|
|
19
18
|
);
|
|
20
19
|
|
|
21
|
-
// The builder runs once, at startup: it is where a handler gets its startup services. The
|
|
22
|
-
// caller and the room are read per call, inside each handler, because every message has its own.
|
|
23
20
|
export const messages = Action.implement(
|
|
24
21
|
Messages,
|
|
25
22
|
Effect.gen(function* () {
|
|
@@ -33,8 +30,6 @@ export const messages = Action.implement(
|
|
|
33
30
|
|
|
34
31
|
return { messages: yield* store.read(room, after) };
|
|
35
32
|
}),
|
|
36
|
-
// The message is in the store, which `MessagePosted`'s check reads: notifying wakes the
|
|
37
|
-
// pages watching this room to read it now.
|
|
38
33
|
postMessage: ({ text }) =>
|
|
39
34
|
Effect.gen(function* () {
|
|
40
35
|
const room = yield* CurrentRoom;
|
|
@@ -11,17 +11,11 @@ export class CurrentRoom extends Context.Service<CurrentRoom, string>()(
|
|
|
11
11
|
"live-rpc/CurrentRoom"
|
|
12
12
|
) {}
|
|
13
13
|
|
|
14
|
-
// Native Effect RPC middleware, declared as providing `CurrentRoom`. Listed in
|
|
15
|
-
// `ActionRpc.layer`'s `middleware`, it runs once per message.
|
|
16
14
|
export class ResolveRoom extends RpcMiddleware.Service<
|
|
17
15
|
ResolveRoom,
|
|
18
16
|
{ provides: CurrentRoom }
|
|
19
17
|
>()("live-rpc/ResolveRoom") {}
|
|
20
18
|
|
|
21
|
-
// The middleware's implementation. It gets the message's headers, which over a WebSocket are
|
|
22
|
-
// the upgrade request's merged with the message's own, and runs the rest of the call with the
|
|
23
|
-
// room they name, or the default room when they name none. It refuses nothing: whether the
|
|
24
|
-
// caller may be in that room is `authorize`'s rule.
|
|
25
19
|
export const resolveRoom = Layer.succeed(ResolveRoom, (call, { headers }) =>
|
|
26
20
|
Effect.provideService(call, CurrentRoom, headers[roomHeader] ?? defaultRoom)
|
|
27
21
|
);
|
|
@@ -3,14 +3,9 @@ import { Context, Effect, Layer } from "effect";
|
|
|
3
3
|
import { MessageNotFound, NotAuthor } from "./Chat.js";
|
|
4
4
|
import type { Message } from "./Chat.js";
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* Where messages live, by room. That only its author deletes a message is checked here, in the
|
|
8
|
-
* data access, so no surface and no handler can skip it.
|
|
9
|
-
*/
|
|
10
6
|
export class MessageStore extends Context.Service<
|
|
11
7
|
MessageStore,
|
|
12
8
|
{
|
|
13
|
-
/** The store's epoch: a message's id is unique within it, and a restarted memory store starts another. */
|
|
14
9
|
readonly epoch: string;
|
|
15
10
|
readonly rooms: Effect.Effect<
|
|
16
11
|
ReadonlyArray<{ name: string; messages: number }>
|
|
@@ -19,7 +14,6 @@ export class MessageStore extends Context.Service<
|
|
|
19
14
|
room: string,
|
|
20
15
|
after: number
|
|
21
16
|
) => Effect.Effect<readonly Message[]>;
|
|
22
|
-
/** The id of the room's last message posted, kept or deleted: 0 for an empty room. */
|
|
23
17
|
readonly last: (room: string) => Effect.Effect<number>;
|
|
24
18
|
readonly post: (
|
|
25
19
|
room: string,
|
|
@@ -33,9 +27,6 @@ export class MessageStore extends Context.Service<
|
|
|
33
27
|
) => Effect.Effect<void, MessageNotFound | NotAuthor>;
|
|
34
28
|
}
|
|
35
29
|
>()("live-rpc/MessageStore") {
|
|
36
|
-
// DEMO ONLY: memory, lost when the process stops, its ids then counting from 1 again, so each
|
|
37
|
-
// start is another epoch. A real store is another Layer of this service, chosen by the
|
|
38
|
-
// entrypoint, whose ids never repeat.
|
|
39
30
|
static readonly layerMemory = Layer.sync(MessageStore, () => {
|
|
40
31
|
const epoch = crypto.randomUUID();
|
|
41
32
|
|
|
@@ -3,12 +3,9 @@ import { Config, Console, Effect, Layer, Stream } from "effect";
|
|
|
3
3
|
|
|
4
4
|
import { connection, openChat } from "./browser.js";
|
|
5
5
|
|
|
6
|
-
// Entrypoint: `node --import tsx examples/live-rpc/src/client.ts`, with the server running.
|
|
7
|
-
// The page's client, run from Node: the same `openChat`, given Node's WebSocket.
|
|
8
6
|
const visit = Effect.gen(function* () {
|
|
9
7
|
const chat = yield* openChat;
|
|
10
8
|
|
|
11
|
-
// Two members on the one socket.
|
|
12
9
|
const ada = chat.room("lobby", "ada");
|
|
13
10
|
const grace = chat.room("lobby", "grace");
|
|
14
11
|
|
|
@@ -18,8 +15,6 @@ const visit = Effect.gen(function* () {
|
|
|
18
15
|
|
|
19
16
|
yield* Console.log("ada posts:", posted);
|
|
20
17
|
yield* Console.log("grace reads:", yield* grace.read(posted.id - 1));
|
|
21
|
-
// A watch from the last id grace held before the post: it sends the post, then would send
|
|
22
|
-
// each one after it, until taken.
|
|
23
18
|
yield* Console.log(
|
|
24
19
|
"grace watches:",
|
|
25
20
|
yield* grace.messages(posted.id - 1).pipe(Stream.take(1), Stream.runCollect)
|
|
@@ -41,7 +36,6 @@ const main = Effect.gen(function* () {
|
|
|
41
36
|
Config.withDefault("ws://127.0.0.1:3000/rpc")
|
|
42
37
|
);
|
|
43
38
|
|
|
44
|
-
// One socket for the whole visit, so both members' calls share it.
|
|
45
39
|
yield* visit.pipe(
|
|
46
40
|
Effect.scoped,
|
|
47
41
|
Effect.provide(
|
|
@@ -6,14 +6,8 @@ export interface Member {
|
|
|
6
6
|
readonly rooms: readonly string[];
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
/**
|
|
10
|
-
* Who sent a message. A protected action declares it, `caller: CurrentMember`; authentication
|
|
11
|
-
* provides it for each message, and a test calling in process provides it itself.
|
|
12
|
-
*/
|
|
13
9
|
export class CurrentMember extends Context.Service<CurrentMember, Member>()(
|
|
14
10
|
"live-rpc/CurrentMember"
|
|
15
11
|
) {}
|
|
16
12
|
|
|
17
|
-
// How a remote caller proves it is CurrentMember: a bearer token, the default scheme. Clients
|
|
18
|
-
// import this descriptor with the binding; its verifier, in verifier.ts, stays on the server.
|
|
19
13
|
export const Login = Authentication.make("live-rpc.Login", CurrentMember);
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { Member } from "./Identity.js";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
// has no such table: its verifier reads the member from a signed token.
|
|
5
|
-
export const members = {
|
|
3
|
+
export const demoMembers = {
|
|
6
4
|
ada: { id: "ada", rooms: ["lobby", "ops"] },
|
|
7
5
|
grace: { id: "grace", rooms: ["lobby"] },
|
|
8
6
|
} as const satisfies Readonly<Record<string, Member>>;
|
|
@@ -3,25 +3,19 @@ import * as Authentication from "@gjermundgaraba/effect-actions/Authentication";
|
|
|
3
3
|
import { Effect, Redacted } from "effect";
|
|
4
4
|
|
|
5
5
|
import { Login } from "./Identity.js";
|
|
6
|
-
import {
|
|
6
|
+
import { demoMembers } from "./members.js";
|
|
7
7
|
|
|
8
|
-
const isToken = (token: string): token is keyof typeof
|
|
9
|
-
Object.hasOwn(
|
|
8
|
+
const isToken = (token: string): token is keyof typeof demoMembers =>
|
|
9
|
+
Object.hasOwn(demoMembers, token);
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
// authorization server's library, its audience included. An unknown token is the built-in
|
|
13
|
-
// `Unauthenticated`, which every client decodes.
|
|
14
|
-
const verify = (token: Redacted.Redacted) => {
|
|
11
|
+
const demoVerify = (token: Redacted.Redacted) => {
|
|
15
12
|
const name = Redacted.value(token);
|
|
16
13
|
|
|
17
14
|
return isToken(name)
|
|
18
|
-
? Effect.succeed(
|
|
15
|
+
? Effect.succeed(demoMembers[name])
|
|
19
16
|
: Effect.fail(
|
|
20
17
|
new Action.Unauthenticated({ message: "Unknown demo token." })
|
|
21
18
|
);
|
|
22
19
|
};
|
|
23
20
|
|
|
24
|
-
|
|
25
|
-
// once for the connection, so a socket has no identity of its own: each call is whoever its
|
|
26
|
-
// token says. It publishes no OAuth discovery, so it needs no router and no configuration.
|
|
27
|
-
export const authenticate = Authentication.layer(Login, verify);
|
|
21
|
+
export const authenticate = Authentication.layer(Login, demoVerify);
|
|
@@ -6,10 +6,6 @@ import { Socket } from "effect/socket";
|
|
|
6
6
|
import { connection, openChat } from "./browser.js";
|
|
7
7
|
import type { MessagePosted } from "./chat/Chat.js";
|
|
8
8
|
|
|
9
|
-
// The chat for a page that is not one Effect program: the same client, each call a promise.
|
|
10
|
-
// A `ManagedRuntime` keeps the protocol's layer, and so the socket, between calls. It builds
|
|
11
|
-
// the layer on the first call, which is when the socket opens, and `close` releases it. A
|
|
12
|
-
// failure `openChat` does not handle, such as `InvalidInput`, rejects the promise.
|
|
13
9
|
class Chat extends Context.Service<Chat, Effect.Success<typeof openChat>>()(
|
|
14
10
|
"live-rpc/Chat"
|
|
15
11
|
) {}
|
|
@@ -33,10 +29,6 @@ export const chatOver = <E>(protocol: Layer.Layer<RpcClient.Protocol, E>) => {
|
|
|
33
29
|
await call((chat) => chat.room(name, token).post(text)),
|
|
34
30
|
remove: async (id: number) =>
|
|
35
31
|
await call((chat) => chat.room(name, token).remove(id)),
|
|
36
|
-
// Calls `receive` with each message posted after `after`, the id of the last message the
|
|
37
|
-
// page holds, and each gap, as `messages` streams them, until the page calls the function
|
|
38
|
-
// it returns. `ended` is told why it stopped: a refusal, such as a token that expired by
|
|
39
|
-
// the time the watch connected again, or anything else, the page's own call included.
|
|
40
32
|
watch: (
|
|
41
33
|
after: number,
|
|
42
34
|
receive: (received: Event.Received<typeof MessagePosted>) => void,
|
|
@@ -72,8 +64,6 @@ export const chatOver = <E>(protocol: Layer.Layer<RpcClient.Protocol, E>) => {
|
|
|
72
64
|
};
|
|
73
65
|
};
|
|
74
66
|
|
|
75
|
-
// What a page calls: `const chat = connect("wss://chat.example/rpc")`, on the browser's own
|
|
76
|
-
// WebSocket, then `await chat.room("lobby", token).post("Hello")`.
|
|
77
67
|
export const connect = (url: string) =>
|
|
78
68
|
chatOver(
|
|
79
69
|
connection(url).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal))
|
|
@@ -7,19 +7,6 @@ import { ResolveRoom, resolveRoom } from "./chat/room.js";
|
|
|
7
7
|
import { authenticate } from "./identity/verifier.js";
|
|
8
8
|
import { Rpc } from "./Rpc.js";
|
|
9
9
|
|
|
10
|
-
// The binding's rpcs on Effect's own RpcServer, the protected ones behind `Login`'s verifier;
|
|
11
|
-
// `listRooms` needs no token.
|
|
12
|
-
//
|
|
13
|
-
// `ResolveRoom`, listed in `middleware`, provides `CurrentRoom`, which the handlers, the event's
|
|
14
|
-
// source and `authorize` read; on a watch it runs as the watch starts, and each time it connects
|
|
15
|
-
// again. Only something that runs per message can, since each message names its own room:
|
|
16
|
-
// `Layer.succeed(CurrentRoom, "lobby")` provided here would leave the layer still requiring it,
|
|
17
|
-
// in its type.
|
|
18
|
-
//
|
|
19
|
-
// It still requires four things, each chosen by whoever serves it. The protocol and its
|
|
20
|
-
// serialization: server.ts gives it a WebSocket, and a test Effect's HTTP protocol, which
|
|
21
|
-
// answers in memory. `MessageStore`, and `Event.Events`, which `postMessage` notifies and the
|
|
22
|
-
// watches wait on: one of each, provided around the whole server.
|
|
23
10
|
export const rpc = ActionRpc.layer(Rpc, [rooms, messages, messageEvents], {
|
|
24
11
|
middleware: [ResolveRoom],
|
|
25
12
|
}).pipe(Layer.provide([resolveRoom, authenticate]));
|