@orthograph/host-cloudflare 0.0.0-stage → 0.1.0-next.1
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/LICENSE +202 -0
- package/README.md +2 -2
- package/dist/index.d.ts +21 -0
- package/dist/index.js +12 -0
- package/dist/modules/actors/internal/tables.d.ts +1 -0
- package/dist/modules/actors/internal/tables.js +5 -0
- package/dist/modules/actors/service.d.ts +18 -0
- package/dist/modules/actors/service.js +51 -0
- package/dist/modules/actors/types.d.ts +35 -0
- package/dist/modules/actors/types.js +1 -0
- package/dist/modules/auth/internal/instance.d.ts +4462 -0
- package/dist/modules/auth/internal/instance.js +36 -0
- package/dist/modules/auth/internal/oauth.d.ts +20 -0
- package/dist/modules/auth/internal/oauth.js +70 -0
- package/dist/modules/auth/internal/options.d.ts +4886 -0
- package/dist/modules/auth/internal/options.js +75 -0
- package/dist/modules/auth/internal/schema.d.ts +2 -0
- package/dist/modules/auth/internal/schema.js +41 -0
- package/dist/modules/auth/internal/world-token.d.ts +15 -0
- package/dist/modules/auth/internal/world-token.js +40 -0
- package/dist/modules/auth/service.d.ts +9 -0
- package/dist/modules/auth/service.js +96 -0
- package/dist/modules/auth/types.d.ts +75 -0
- package/dist/modules/auth/types.js +1 -0
- package/dist/modules/clock/internal/date.d.ts +17 -0
- package/dist/modules/clock/internal/date.js +45 -0
- package/dist/modules/clock/internal/door.d.ts +2 -0
- package/dist/modules/clock/internal/door.js +49 -0
- package/dist/modules/clock/internal/state.d.ts +5 -0
- package/dist/modules/clock/internal/state.js +78 -0
- package/dist/modules/clock/service.d.ts +2 -0
- package/dist/modules/clock/service.js +36 -0
- package/dist/modules/clock/types.d.ts +152 -0
- package/dist/modules/clock/types.js +1 -0
- package/dist/modules/database/service.d.ts +2 -0
- package/dist/modules/database/service.js +6 -0
- package/dist/modules/database/types.d.ts +7 -0
- package/dist/modules/database/types.js +1 -0
- package/dist/modules/follower/internal/entries.d.ts +9 -0
- package/dist/modules/follower/internal/entries.js +20 -0
- package/dist/modules/follower/internal/health.d.ts +18 -0
- package/dist/modules/follower/internal/health.js +27 -0
- package/dist/modules/follower/internal/outbox.d.ts +26 -0
- package/dist/modules/follower/internal/outbox.js +35 -0
- package/dist/modules/follower/internal/poll.d.ts +21 -0
- package/dist/modules/follower/internal/poll.js +23 -0
- package/dist/modules/follower/service.d.ts +31 -0
- package/dist/modules/follower/service.js +87 -0
- package/dist/modules/follower/types.d.ts +71 -0
- package/dist/modules/follower/types.js +4 -0
- package/dist/modules/inbound/internal/accept.d.ts +26 -0
- package/dist/modules/inbound/internal/accept.js +10 -0
- package/dist/modules/inbound/internal/read.d.ts +10 -0
- package/dist/modules/inbound/internal/read.js +26 -0
- package/dist/modules/inbound/internal/same.d.ts +1 -0
- package/dist/modules/inbound/internal/same.js +12 -0
- package/dist/modules/inbound/internal/verify.d.ts +6 -0
- package/dist/modules/inbound/internal/verify.js +57 -0
- package/dist/modules/inbound/service.d.ts +2 -0
- package/dist/modules/inbound/service.js +110 -0
- package/dist/modules/inbound/types.d.ts +28 -0
- package/dist/modules/inbound/types.js +1 -0
- package/dist/modules/journal/service.d.ts +2 -0
- package/dist/modules/journal/service.js +23 -0
- package/dist/modules/journal/types.d.ts +8 -0
- package/dist/modules/journal/types.js +1 -0
- package/dist/modules/relayer/internal/adopt.d.ts +19 -0
- package/dist/modules/relayer/internal/adopt.js +34 -0
- package/dist/modules/relayer/internal/check.d.ts +16 -0
- package/dist/modules/relayer/internal/check.js +141 -0
- package/dist/modules/relayer/internal/health.d.ts +5 -0
- package/dist/modules/relayer/internal/health.js +7 -0
- package/dist/modules/relayer/internal/live.d.ts +26 -0
- package/dist/modules/relayer/internal/live.js +25 -0
- package/dist/modules/relayer/internal/relay.d.ts +22 -0
- package/dist/modules/relayer/internal/relay.js +44 -0
- package/dist/modules/relayer/internal/store.d.ts +41 -0
- package/dist/modules/relayer/internal/store.js +44 -0
- package/dist/modules/relayer/service.d.ts +58 -0
- package/dist/modules/relayer/service.js +124 -0
- package/dist/modules/relayer/types.d.ts +75 -0
- package/dist/modules/relayer/types.js +1 -0
- package/dist/modules/store/internal/actors.d.ts +6 -0
- package/dist/modules/store/internal/actors.js +24 -0
- package/dist/modules/store/internal/commit.d.ts +10 -0
- package/dist/modules/store/internal/commit.js +57 -0
- package/dist/modules/store/internal/list.d.ts +10 -0
- package/dist/modules/store/internal/list.js +28 -0
- package/dist/modules/store/internal/rows.d.ts +14 -0
- package/dist/modules/store/internal/rows.js +29 -0
- package/dist/modules/store/internal/unique.d.ts +3 -0
- package/dist/modules/store/internal/unique.js +17 -0
- package/dist/modules/store/service.d.ts +2 -0
- package/dist/modules/store/service.js +71 -0
- package/dist/modules/store/types.d.ts +36 -0
- package/dist/modules/store/types.js +1 -0
- package/dist/modules/telemetry/internal/exporters.d.ts +21 -0
- package/dist/modules/telemetry/internal/exporters.js +66 -0
- package/dist/modules/telemetry/service.d.ts +2 -0
- package/dist/modules/telemetry/service.js +52 -0
- package/dist/modules/telemetry/types.d.ts +36 -0
- package/dist/modules/telemetry/types.js +1 -0
- package/dist/modules/timers/internal/controller.d.ts +13 -0
- package/dist/modules/timers/internal/controller.js +63 -0
- package/dist/modules/timers/internal/count.d.ts +39 -0
- package/dist/modules/timers/internal/count.js +105 -0
- package/dist/modules/timers/internal/limits.d.ts +4 -0
- package/dist/modules/timers/internal/limits.js +8 -0
- package/dist/modules/timers/internal/table.d.ts +52 -0
- package/dist/modules/timers/internal/table.js +181 -0
- package/dist/modules/timers/service.d.ts +12 -0
- package/dist/modules/timers/service.js +136 -0
- package/dist/modules/timers/types.d.ts +82 -0
- package/dist/modules/timers/types.js +1 -0
- package/dist/modules/world/internal/entry.d.ts +34 -0
- package/dist/modules/world/internal/entry.js +69 -0
- package/dist/modules/world/internal/facts.d.ts +5 -0
- package/dist/modules/world/internal/facts.js +31 -0
- package/dist/modules/world/internal/fence.d.ts +9 -0
- package/dist/modules/world/internal/fence.js +90 -0
- package/dist/modules/world/internal/front-door.d.ts +5 -0
- package/dist/modules/world/internal/front-door.js +50 -0
- package/dist/modules/world/internal/leases.d.ts +33 -0
- package/dist/modules/world/internal/leases.js +79 -0
- package/dist/modules/world/internal/reset.d.ts +6 -0
- package/dist/modules/world/internal/reset.js +24 -0
- package/dist/modules/world/internal/work.d.ts +11 -0
- package/dist/modules/world/internal/work.js +73 -0
- package/dist/modules/world/internal/worlds.d.ts +7 -0
- package/dist/modules/world/internal/worlds.js +48 -0
- package/dist/modules/world/service.d.ts +80 -0
- package/dist/modules/world/service.js +209 -0
- package/dist/modules/world/types.d.ts +47 -0
- package/dist/modules/world/types.js +1 -0
- package/package.json +41 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { betterAuth } from "better-auth";
|
|
2
|
+
import { optionsFor } from "./options.js";
|
|
3
|
+
import { SCHEMA } from "./schema.js";
|
|
4
|
+
/** An instance that knows its origin names it, so OAuth's issuer and the MCP resource are that address; else a placeholder. */
|
|
5
|
+
const make = (o, origin) => betterAuth({ ...optionsFor(o.cli ?? "cli", origin), database: o.db, secret: o.secret, baseURL: origin ?? "http://ortho.local" });
|
|
6
|
+
/** The origins one server answers on are a handful; a caller can't make more (the Host header is the platform's). */
|
|
7
|
+
const MAX_ORIGINS = 8;
|
|
8
|
+
export const instance = (o) => {
|
|
9
|
+
let tables;
|
|
10
|
+
const made = new Map();
|
|
11
|
+
const held = {
|
|
12
|
+
kind: "auth-d1",
|
|
13
|
+
db: o.db,
|
|
14
|
+
secret: o.secret,
|
|
15
|
+
worlds: o.worlds === true,
|
|
16
|
+
auth: (origin) => (tables ??= o.db.batch(SCHEMA.map((s) => o.db.prepare(s)))).then(() => {
|
|
17
|
+
const key = origin ?? "";
|
|
18
|
+
const had = made.get(key);
|
|
19
|
+
if (had)
|
|
20
|
+
return had;
|
|
21
|
+
if (made.size >= MAX_ORIGINS)
|
|
22
|
+
made.clear();
|
|
23
|
+
// Better Auth finishes starting up on its first use, and a start that a request leaves unfinished is one the next
|
|
24
|
+
// request waits on forever (found with the MCP route: a first call in a later request hung). So the instance is
|
|
25
|
+
// used once, here, before it is handed out.
|
|
26
|
+
const fresh = make(o, origin);
|
|
27
|
+
const ready = fresh.handler(new Request(`${origin ?? "http://ortho.local"}/api/auth/ok`)).then(() => fresh);
|
|
28
|
+
made.set(key, ready);
|
|
29
|
+
return ready;
|
|
30
|
+
}),
|
|
31
|
+
};
|
|
32
|
+
return held;
|
|
33
|
+
};
|
|
34
|
+
export const held = (a) => a;
|
|
35
|
+
/** Better Auth's refusals carry the HTTP status they would answer with. */
|
|
36
|
+
export const statusOf = (e) => typeof e === "object" && e !== null && "statusCode" in e && typeof e.statusCode === "number" ? e.statusCode : undefined;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type BetterAuth } from "./instance.ts";
|
|
3
|
+
import type { AuthInstance, Bearer } from "../types.ts";
|
|
4
|
+
/** `WWW-Authenticate` for a request with no usable token: where a client finds out how to get one (RFC 9728). */
|
|
5
|
+
export declare const challenge: (request: Request, error?: "invalid_token") => string;
|
|
6
|
+
/**
|
|
7
|
+
* The discovery documents, at the paths the SDK's client asks for. Better Auth natively serves only
|
|
8
|
+
* `/api/auth/.well-known/...`, so the path-aware forms (RFC 8414 section 3.1) are answered here. `undefined`: not one of them.
|
|
9
|
+
*/
|
|
10
|
+
export declare const discovery: (auth: BetterAuth, request: Request) => Effect.Effect<Response | undefined>;
|
|
11
|
+
/** Who an access token is for. A token proves who; the role check says what they may do. */
|
|
12
|
+
export declare const bearer: (a: AuthInstance, auth: BetterAuth, request: Request) => Effect.Effect<Bearer>;
|
|
13
|
+
/** Dynamic registration is open (MCP clients register themselves); how many clients that can make is not. */
|
|
14
|
+
export declare const CLIENT_CAP = 500;
|
|
15
|
+
export declare const isRegistration: (request: Request) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Before Better Auth sees a registration: clear the clients nobody ever authorized and that are a day old, then refuse
|
|
18
|
+
* (429, `temporarily_unavailable`) when `CLIENT_CAP` remain. `undefined`: go ahead.
|
|
19
|
+
*/
|
|
20
|
+
export declare const registrationGuard: (a: AuthInstance) => Effect.Effect<Response | undefined>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// OAuth 2.1 for the remote MCP server: what the server needs of Better Auth's provider that its own routes don't answer.
|
|
2
|
+
// The metadata the SDK's client discovers (the protected resource, and the path-aware authorization server), the access
|
|
3
|
+
// token's check, and the cap on dynamic client registration (D3 #123, #124).
|
|
4
|
+
import { Clock, DateTime, Effect } from "effect";
|
|
5
|
+
import { createLocalJWKSet, jwtVerify } from "jose";
|
|
6
|
+
import { oauthProviderAuthServerMetadata, oauthProviderOpenIdConfigMetadata } from "@better-auth/oauth-provider";
|
|
7
|
+
import { held } from "./instance.js";
|
|
8
|
+
import { SCOPES, mcpResource } from "./options.js";
|
|
9
|
+
const json = (body, status = 200, headers = {}) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json", ...headers } });
|
|
10
|
+
const issuerOf = (origin) => `${origin}/api/auth`;
|
|
11
|
+
const resourceMetadata = (origin) => `${origin}/.well-known/oauth-protected-resource`;
|
|
12
|
+
const originOf = (request) => new URL(request.url).origin;
|
|
13
|
+
/** `WWW-Authenticate` for a request with no usable token: where a client finds out how to get one (RFC 9728). */
|
|
14
|
+
export const challenge = (request, error) => `Bearer resource_metadata="${resourceMetadata(originOf(request))}"${error ? `, error="${error}"` : ""}`;
|
|
15
|
+
/**
|
|
16
|
+
* The discovery documents, at the paths the SDK's client asks for. Better Auth natively serves only
|
|
17
|
+
* `/api/auth/.well-known/...`, so the path-aware forms (RFC 8414 section 3.1) are answered here. `undefined`: not one of them.
|
|
18
|
+
*/
|
|
19
|
+
export const discovery = (auth, request) => {
|
|
20
|
+
const origin = originOf(request);
|
|
21
|
+
const { pathname } = new URL(request.url);
|
|
22
|
+
if (pathname === "/.well-known/oauth-authorization-server/api/auth")
|
|
23
|
+
return Effect.promise(() => oauthProviderAuthServerMetadata(auth)(request));
|
|
24
|
+
if (pathname === "/.well-known/openid-configuration/api/auth")
|
|
25
|
+
return Effect.promise(() => oauthProviderOpenIdConfigMetadata(auth)(request));
|
|
26
|
+
if (pathname === "/.well-known/oauth-protected-resource" || pathname === "/.well-known/oauth-protected-resource/mcp") {
|
|
27
|
+
return Effect.succeed(json({ resource: mcpResource(origin), authorization_servers: [issuerOf(origin)], bearer_methods_supported: ["header"], scopes_supported: SCOPES }));
|
|
28
|
+
}
|
|
29
|
+
return Effect.succeed(undefined);
|
|
30
|
+
};
|
|
31
|
+
/** Who an access token is for. A token proves who; the role check says what they may do. */
|
|
32
|
+
export const bearer = (a, auth, request) => Effect.gen(function* () {
|
|
33
|
+
const origin = originOf(request);
|
|
34
|
+
const header = request.headers.get("authorization") ?? "";
|
|
35
|
+
const token = /^bearer /i.test(header) ? header.slice(7).trim() : "";
|
|
36
|
+
if (token === "")
|
|
37
|
+
return { kind: "none" };
|
|
38
|
+
// Checked here against the same-origin keys: signature, issuer, audience and expiry; no call back to the provider.
|
|
39
|
+
const verified = yield* Effect.tryPromise(() => auth.api.getJwks().then((keys) => { return jwtVerify(token, createLocalJWKSet(keys), { issuer: issuerOf(origin), audience: mcpResource(origin) }); })).pipe(Effect.option);
|
|
40
|
+
const sub = verified._tag === "Some" ? verified.value.payload.sub : undefined;
|
|
41
|
+
if (typeof sub !== "string")
|
|
42
|
+
return { kind: "invalid" };
|
|
43
|
+
const user = yield* Effect.promise(() => held(a).db.prepare(`SELECT "email" FROM "auth_user" WHERE "id" = ?`).bind(sub).first());
|
|
44
|
+
if (!user)
|
|
45
|
+
return { kind: "invalid" };
|
|
46
|
+
// The MCP door is the agent's: whoever signed in over OAuth acts for the person it names.
|
|
47
|
+
const principal = { kind: "agent", person: sub, email: user.email, via: "agent" };
|
|
48
|
+
return { kind: "person", principal };
|
|
49
|
+
});
|
|
50
|
+
/** Dynamic registration is open (MCP clients register themselves); how many clients that can make is not. */
|
|
51
|
+
export const CLIENT_CAP = 500;
|
|
52
|
+
/** A client nobody ever authorized, this old, is cleared to make room. */
|
|
53
|
+
const STALE_MS = 24 * 60 * 60 * 1000;
|
|
54
|
+
export const isRegistration = (request) => request.method === "POST" && new URL(request.url).pathname === "/api/auth/oauth2/register";
|
|
55
|
+
/**
|
|
56
|
+
* Before Better Auth sees a registration: clear the clients nobody ever authorized and that are a day old, then refuse
|
|
57
|
+
* (429, `temporarily_unavailable`) when `CLIENT_CAP` remain. `undefined`: go ahead.
|
|
58
|
+
*/
|
|
59
|
+
export const registrationGuard = (a) => Effect.gen(function* () {
|
|
60
|
+
const { db } = held(a);
|
|
61
|
+
const before = DateTime.formatIso(DateTime.makeUnsafe((yield* Clock.currentTimeMillis) - STALE_MS));
|
|
62
|
+
const [, counted] = yield* Effect.promise(() => db.batch([
|
|
63
|
+
db.prepare(`DELETE FROM "auth_oauth_client" WHERE "createdAt" < ? AND "clientId" NOT IN (SELECT "clientId" FROM "auth_oauth_consent") AND "clientId" NOT IN (SELECT "clientId" FROM "auth_oauth_access_token") AND "clientId" NOT IN (SELECT "clientId" FROM "auth_oauth_refresh_token")`).bind(before),
|
|
64
|
+
db.prepare(`SELECT COUNT(*) AS n FROM "auth_oauth_client"`),
|
|
65
|
+
]));
|
|
66
|
+
const n = counted?.results[0]?.n ?? 0;
|
|
67
|
+
return n >= CLIENT_CAP
|
|
68
|
+
? json({ error: "temporarily_unavailable", error_description: `this server holds ${CLIENT_CAP} registered clients; try again later` }, 429, { "retry-after": "3600" })
|
|
69
|
+
: undefined;
|
|
70
|
+
});
|