@superforge/infra 0.4.0 → 0.6.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 CHANGED
@@ -7,14 +7,14 @@ for. It's the one way an app reaches Infra: a typed client, the stand-in
7
7
  an app's tests and a fresh dev server use in its place, and the choice
8
8
  between them. Infra's own API reference, each call with its fields and
9
9
  errors, isn't public: an agent reads it with the Superforge MCP's
10
- `read_infra_docs`. Everything an app calls is below, and every request and
11
- answer is a zod schema in `@superforge/infra/contract`.
10
+ `read_infra_docs`. Everything an app calls is below.
12
11
 
13
12
  ```bash
14
- pnpm add @superforge/infra zod
13
+ pnpm add @superforge/infra
15
14
  ```
16
15
 
17
- It needs Node 22 or later, and zod 4, which an app has already.
16
+ It needs Node 22 or later. It brings its own zod 4, so an app can be on any
17
+ zod, and move to the next one without waiting for the package.
18
18
 
19
19
  ## Using it
20
20
 
@@ -86,6 +86,7 @@ Every method answers a result; every refusal but `unavailable` and
86
86
  | ------------------------------------------------------ | --------------------------------------------------------------------------------------- | ------------------------------------------------- |
87
87
  | `project()` | the key's project, its mode and its domains | `unavailable` |
88
88
  | `startLogin({ channel, address, ip, link_to? })` | the address, `with_link`, `expires_at`, and `test` (the code and link, with a test key) | `rate-limited`, `not-sent`, `unavailable` |
89
+ | `makeLoginLink({ channel, address, ip, link_to })` | the address, the `link` for the app's own message, and `expires_at` (24 hours on) | `rate-limited`, `unavailable` |
89
90
  | `verifyLogin({ channel, address, code } \| { token })` | whose address it was | `wrong-code`, `no-code`, `no-link`, `unavailable` |
90
91
  | `fileComment(comment)` | the comment's `id` and `created_at` | `invalid`, `too-large`, `unavailable` |
91
92
  | `passOAuth(request, call, origin)` | the `Response` for one of Claude's OAuth calls | (a `Response`: OAuth's own errors) |
@@ -119,23 +120,26 @@ that passes on what it was sent can't reach another of Infra's addresses.
119
120
  - **`APP_PATHS`** (`{ oauth: "/oauth", allow: "/connect" }`) is where the
120
121
  app answers Claude's login; an app that keeps it elsewhere gives
121
122
  `chooseInfra` its own `paths`.
122
- - **`oauthMetadata`** is the server's metadata, which `passOAuth`
123
- answers `metadata` with.
123
+ - **`AGENT_SCOPES`** (`["read", "act"]`) is what someone lets Claude do.
124
+ An app that checks scopes with zod builds its own list from it,
125
+ `z.enum(AGENT_SCOPES)`, with its own zod.
124
126
 
125
127
  ### `infraClient`
126
128
 
127
129
  `infraClient({ key, url?, send?, paths? })` is the real Infra, which
128
130
  `chooseInfra` makes; an app calls it itself only to point a test at a fake
129
- of Infra (`send` is `fetch`, or the test's own; `url` is `INFRA_URL`,
130
- Infra's address, unless set).
131
+ of Infra (`send` is `fetch`, or the test's own; `url` is Infra's own
132
+ address unless set).
131
133
 
132
134
  ### The stand-in, for tests
133
135
 
134
- `standInInfra({ waiting?, logs?, domains? })` is Infra's test mode in
136
+ `standInInfra({ domains? })` is Infra's test mode in
135
137
  memory: login sends nothing and answers the code and the link, which
136
138
  verify as Infra's do (once each, for 15 minutes, a code for five tries),
137
139
  and links, as Infra's do, only to this computer and the project's
138
- `domains`, over https; any other host gets `with_link: false`. A comment
140
+ `domains`, over https; any other host gets `with_link: false`. A link
141
+ made for the app's own message (`makeLoginLink`) verifies the same way,
142
+ for 24 hours, and one to any other host is refused `unavailable`. A comment
139
143
  is checked as Infra checks it and kept nowhere; Claude logs in to nothing,
140
144
  and `checkAgentToken` takes only the tokens `standInAgentToken()` mints:
141
145
  `standInAgentToken({ user_id, resource, scopes?, origin?, expires_in? })`,
@@ -144,11 +148,23 @@ app's tests use it, so they never reach Infra.
144
148
 
145
149
  ### The contract
146
150
 
147
- `@superforge/infra/contract`, also exported from the package itself, is
148
- Infra's contract with nothing of Node's, so a page can import it: every
149
- call's request and answer as zod schemas (`login_start`, `started_login`,
150
- `agent_request`…), the types made from them, and every reason a call is
151
- refused, Infra's (`INFRA_ERRORS`) and the package's own (`CLIENT_ERRORS`).
151
+ `@superforge/infra/contract` is Infra's contract with nothing of Node's, so
152
+ a page can import it: the constants and types every call is made of, every
153
+ reason a call is refused, Infra's (`INFRA_ERRORS`) and the package's own
154
+ (`CLIENT_ERRORS`), and the zod schemas of what Infra is sent (`login_start`,
155
+ `login_link`, `login_verify`, `filed_comment`, `agent_allow`,
156
+ `connections_query`), which Infra checks with, with the three answers
157
+ Infra's own server ties itself to (`started_login`, `made_login_link`,
158
+ `verified_login`). The package itself exports the same
159
+ constants and types, and no schema.
160
+
161
+ A schema here is made with the package's own zod: parse with it as it is,
162
+ and never put it inside a schema of the app's own, which may be another
163
+ zod's and then fails. Build the app's own from the constants instead.
164
+
165
+ Every name either entry exports is a promise: from 1.0.0 on, taking one
166
+ away is a major. So they export only what an app or Infra's server uses;
167
+ the rest is the package's own.
152
168
 
153
169
  ## When something goes wrong
154
170
 
@@ -181,11 +197,14 @@ should only learn that login isn't working.
181
197
 
182
198
  ## What every call tells Infra
183
199
 
184
- Every call, Claude's OAuth calls passed through included, carries
185
- `Infra-Client: @superforge/infra/<version>`, so Infra knows which version
186
- each app is on, and can say when one's out of date (`PACKAGE_VERSION` and
187
- `CLIENT_HEADER` are exported). The version is written in
188
- `source/version.ts`, which a test checks against `package.json`.
200
+ Every call, Claude's OAuth calls passed through and the fetch of Infra's
201
+ public keys included, carries `Infra-Client: @superforge/infra/<version>`
202
+ (`CLIENT_HEADER`, in the contract), so Infra knows which version each app
203
+ is on. That release is the version Infra answers the call as: once Infra
204
+ changes an answer, a release from before the change still gets the shape
205
+ it reads. A call that names no release, like one from a terminal, is
206
+ answered as the oldest release Infra still answers. The version is written
207
+ in `source/version.ts`, which a test checks against `package.json`.
189
208
 
190
209
  ## Releases
191
210
 
@@ -208,6 +227,13 @@ renames or drops one an app's release calls, so a release keeps working
208
227
  until the app takes the next. Until 1.0.0 it keeps no older shapes: an app
209
228
  takes the latest release.
210
229
 
230
+ A release reads what Infra adds without breaking: a field it doesn't know
231
+ is dropped, a 429's `limit` is read as any name, and a request's or a
232
+ connection's `scopes` keep only the scopes the release knows. A refusal
233
+ whose `reason` it doesn't know is `unavailable`, with Infra's `message`.
234
+ `mode` and a login's `channel` stay closed lists: an app only meets a new
235
+ one once it asks for it.
236
+
211
237
  ## Working on it
212
238
 
213
239
  ```bash
@@ -220,13 +246,14 @@ checkout at this folder (`pnpm add @superforge/infra@link:<path to this
220
246
  folder>`), and back to the released version before its PR: a `link:` is
221
247
  this computer's alone, and CI can't install it.
222
248
 
223
- - `source/index.ts`: everything the package exports.
224
- - `source/contract/`: Infra's contract, exported on its own at
225
- `@superforge/infra/contract`, with nothing of Node's: every call's
226
- request and answer as zod schemas, which Infra checks what it's sent
227
- with, and every reason it refuses one (`errors.ts`), a file per area:
228
- `login.ts`, `comments.ts`, `agents.ts`, `project.ts` and `people.ts`
229
- (`user_id`, the app's ID for one of its people).
249
+ - `source/index.ts`: everything the package exports, by name.
250
+ - `source/contract/`: Infra's contract, with nothing of Node's: every
251
+ call's request and answer as zod schemas, which Infra checks what it's
252
+ sent with, and every reason it refuses one (`errors.ts`), a file per
253
+ area: `login.ts`, `comments.ts`, `agents.ts`, `project.ts` and
254
+ `people.ts` (`user_id`, the app's ID for one of its people).
255
+ `index.ts` is what `@superforge/infra/contract` exports, by name;
256
+ `all.ts` is all of it, which only the package itself imports.
230
257
  - `source/infra.ts`: `Infra`, what an app asks of it, and its results.
231
258
  - `source/client.ts`: Infra over HTTP with a project's key, its answers
232
259
  parsed with the contract and every one logged. No call is retried: a
@@ -1,5 +1,5 @@
1
1
  import { type JWTVerifyGetKey } from "jose";
2
- import { type AppPaths, type KeyMode, type OAuthCall } from "./contract/index.js";
2
+ import { type AppPaths, type KeyMode, type OAuthCall } from "./contract/all.js";
3
3
  import type { AgentToken, AgentTokenRefused, InfraUnavailable } from "./infra.js";
4
4
  import type { AsyncOperationResult } from "./results.js";
5
5
  /**
@@ -70,7 +70,7 @@ export declare function passOAuth({ url, key, send, paths, request, call, origin
70
70
  export declare function unavailableOAuth(message: string): Response;
71
71
  /**
72
72
  * Where a token's signature is checked: Infra's published public keys,
73
- * fetched with `send`
73
+ * fetched with `send`, saying which release asks, as every call does
74
74
  */
75
75
  export declare function infraKeys(url: string, send?: typeof fetch): JWTVerifyGetKey;
76
76
  /**
package/output/agents.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRemoteJWKSet, customFetch, errors, jwtVerify } from "jose";
2
- import { AGENT_SCOPES, APP_ORIGIN_HEADER, agent_authorized, agent_claims, authorize_refused, CLIENT_HEADER, infra_failure, isLoopbackHost } from "./contract/index.js";
2
+ import { AGENT_SCOPES, APP_ORIGIN_HEADER, agent_authorized, agent_claims, authorize_refused, CLIENT_HEADER, infra_failure, isLoopbackHost } from "./contract/all.js";
3
3
  import { CALL_TIMEOUT, SECOND } from "./primitives.js";
4
4
  import { CLIENT } from "./version.js";
5
5
  // Claude logging in to an app's own MCP, through Infra: Infra is the OAuth
@@ -211,10 +211,11 @@ export function unavailableOAuth(message) {
211
211
  }
212
212
  /**
213
213
  * Where a token's signature is checked: Infra's published public keys,
214
- * fetched with `send`
214
+ * fetched with `send`, saying which release asks, as every call does
215
215
  */
216
216
  export function infraKeys(url, send = fetch) {
217
217
  return createRemoteJWKSet(new URL("/.well-known/jwks.json", new URL(url).origin), {
218
+ headers: { [CLIENT_HEADER]: CLIENT },
218
219
  timeoutDuration: 10 * SECOND,
219
220
  cooldownDuration: 30 * SECOND,
220
221
  [customFetch]: send
@@ -1,4 +1,4 @@
1
- import { type AppPaths } from "./contract/index.js";
1
+ import { type AppPaths } from "./contract/all.js";
2
2
  import type { Infra } from "./infra.js";
3
3
  /** Which Infra a server calls, as `chooseInfra` chose it. */
4
4
  export type InfraChoice = {
package/output/choose.js CHANGED
@@ -4,8 +4,8 @@ import { parseEnv } from "node:util";
4
4
  import { z } from "zod";
5
5
  import { unavailableOAuth } from "./agents.js";
6
6
  import { INFRA_URL, infraClient } from "./client.js";
7
- import { APP_PATHS, KEY_BODY, KEY_PREFIXES } from "./contract/index.js";
8
- import { standInInfra } from "./stand-in.js";
7
+ import { APP_PATHS, KEY_BODY, KEY_PREFIXES } from "./contract/all.js";
8
+ import { standInWith } from "./stand-in.js";
9
9
  // Which Infra the server calls, by what it runs as and the keys it has:
10
10
  //
11
11
  // | Runs as | Has | Calls |
@@ -101,7 +101,7 @@ export function chooseInfra({ development, environment, checkout, domains = [],
101
101
  console.info("Infra: no test key, so login uses the stand-in, which emails nothing");
102
102
  }
103
103
  return {
104
- infra: standInInfra({
104
+ infra: standInWith({
105
105
  waiting: devServerLogins(),
106
106
  logs: true,
107
107
  domains
@@ -210,6 +210,9 @@ function unavailableInfra(message) {
210
210
  async startLogin() {
211
211
  return unavailable;
212
212
  },
213
+ async makeLoginLink() {
214
+ return unavailable;
215
+ },
213
216
  async verifyLogin() {
214
217
  return unavailable;
215
218
  },
@@ -1,4 +1,4 @@
1
- import { type AppPaths } from "./contract/index.js";
1
+ import { type AppPaths } from "./contract/all.js";
2
2
  import type { Infra } from "./infra.js";
3
3
  /** Infra's API, which a project's keys call */
4
4
  export declare const INFRA_URL = "https://infra.superforge.studio/v1";
package/output/client.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { checkToken, infraKeys, passOAuth } from "./agents.js";
2
- import { AGENT_ID, APP_PATHS, agent_connections, agent_ended, agent_redirect, agent_refused, agent_request, CLIENT_HEADER, comment_refused, infra_failure, infra_project, kept_comment, start_refused, started_login, verified_login, verify_refused } from "./contract/index.js";
2
+ import { AGENT_ID, APP_PATHS, agent_connections, agent_ended, agent_redirect, agent_refused, agent_request, CLIENT_HEADER, comment_refused, infra_failure, infra_project, kept_comment, link_refused, made_login_link, start_refused, started_login, verified_login, verify_refused } from "./contract/all.js";
3
3
  import { CALL_TIMEOUT } from "./primitives.js";
4
4
  import { CLIENT } from "./version.js";
5
5
  /** Infra's API, which a project's keys call */
@@ -107,6 +107,10 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
107
107
  const call = "/login/start";
108
108
  return answered(ask("POST", call, request), call, { schema: started_login }, start_refused);
109
109
  },
110
+ makeLoginLink(request) {
111
+ const call = "/login/links";
112
+ return answered(ask("POST", call, request), call, { schema: made_login_link }, link_refused);
113
+ },
110
114
  verifyLogin(proof) {
111
115
  const call = "/login/verify";
112
116
  return answered(ask("POST", call, proof), call, { schema: verified_login }, verify_refused);
@@ -103,10 +103,7 @@ export declare const agent_request: z.ZodObject<{
103
103
  request: z.ZodObject<{
104
104
  id: z.ZodString;
105
105
  client_name: z.ZodString;
106
- scopes: z.ZodArray<z.ZodEnum<{
107
- read: "read";
108
- act: "act";
109
- }>>;
106
+ scopes: z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("read" | "act")[], string[]>>;
110
107
  resource: z.ZodString;
111
108
  expires_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
112
109
  }, z.core.$strip>;
@@ -134,10 +131,7 @@ export declare const agent_connection: z.ZodObject<{
134
131
  id: z.ZodString;
135
132
  user_id: z.ZodString;
136
133
  client_name: z.ZodString;
137
- scopes: z.ZodArray<z.ZodEnum<{
138
- read: "read";
139
- act: "act";
140
- }>>;
134
+ scopes: z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("read" | "act")[], string[]>>;
141
135
  resource: z.ZodString;
142
136
  last_used_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
143
137
  created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
@@ -149,10 +143,7 @@ export declare const agent_connections: z.ZodObject<{
149
143
  id: z.ZodString;
150
144
  user_id: z.ZodString;
151
145
  client_name: z.ZodString;
152
- scopes: z.ZodArray<z.ZodEnum<{
153
- read: "read";
154
- act: "act";
155
- }>>;
146
+ scopes: z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("read" | "act")[], string[]>>;
156
147
  resource: z.ZodString;
157
148
  last_used_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
158
149
  created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
@@ -12,6 +12,14 @@ import { key_mode } from "./project.js";
12
12
  /** What someone on the app's team lets Claude do through its MCP */
13
13
  export const AGENT_SCOPES = ["read", "act"];
14
14
  export const agent_scope = z.enum(AGENT_SCOPES);
15
+ /**
16
+ * Scopes as an answer carries them: whatever Infra says, keeping the ones
17
+ * this release knows, as the token check does. Infra may add a scope, and
18
+ * a release that can't read it would find the whole answer unreadable.
19
+ */
20
+ const known_scopes = z
21
+ .array(z.string())
22
+ .transform((scopes) => AGENT_SCOPES.filter((scope) => scopes.includes(scope)));
15
23
  /**
16
24
  * The header the app's server sends Claude's OAuth calls on with: where
17
25
  * Claude reached the app, `https://example.com`, its tokens' issuer.
@@ -47,7 +55,7 @@ export const agent_request = z.object({
47
55
  request: z.object({
48
56
  id: z.string(),
49
57
  client_name: z.string(),
50
- scopes: z.array(agent_scope),
58
+ scopes: known_scopes,
51
59
  // The address Claude asked to use, which its tokens are good for:
52
60
  // the app's MCP
53
61
  resource: z.string(),
@@ -72,7 +80,7 @@ export const agent_connection = z.object({
72
80
  id: z.string(),
73
81
  user_id: z.string(),
74
82
  client_name: z.string(),
75
- scopes: z.array(agent_scope),
83
+ scopes: known_scopes,
76
84
  resource: z.string(),
77
85
  // When Claude last refreshed it, to within about ten minutes
78
86
  last_used_at: z.iso
@@ -0,0 +1,6 @@
1
+ export * from "./agents.js";
2
+ export * from "./comments.js";
3
+ export * from "./errors.js";
4
+ export * from "./login.js";
5
+ export * from "./people.js";
6
+ export * from "./project.js";
@@ -0,0 +1,9 @@
1
+ // Every schema and constant of Infra's contract, public or not, which the
2
+ // package itself reads Infra's answers with. Only the package imports this
3
+ // file: what an app or Infra's server may import is index.ts.
4
+ export * from "./agents.js";
5
+ export * from "./comments.js";
6
+ export * from "./errors.js";
7
+ export * from "./login.js";
8
+ export * from "./people.js";
9
+ export * from "./project.js";
@@ -1,6 +1,5 @@
1
- export * from "./agents.js";
2
- export * from "./comments.js";
3
- export * from "./errors.js";
4
- export * from "./login.js";
5
- export * from "./people.js";
6
- export * from "./project.js";
1
+ export { AGENT_ID, AGENT_SCOPES, type AgentAllow, type AgentConnection, type AgentRefused, type AgentRequest, type AgentScope, APP_ORIGIN_HEADER, APP_PATHS, type AppPaths, agent_allow, connections_query, OAUTH_CALLS, type OAuthCall } from "./agents.js";
2
+ export { COMMENT_BODY_LIMIT, type CommentRefused, type FiledComment, filed_comment, type KeptComment, LONGEST_COMMENT } from "./comments.js";
3
+ export { CLIENT_ERRORS, type ClientErrorReason, INFRA_ERRORS, type InfraErrorReason, type InfraIssue } from "./errors.js";
4
+ export { type LinkRefused, LOGIN_CHANNELS, type LoginChannel, type LoginLink, type LoginProof, type LoginStart, login_link, login_start, login_verify, type MadeLoginLink, made_login_link, type StartedLogin, type StartRefused, started_login, type VerifiedLogin, type VerifyRefused, verified_login } from "./login.js";
5
+ export { CLIENT_HEADER, type InfraProject, isLoopbackHost, KEY_BODY, KEY_MODES, KEY_PREFIXES, type KeyMode, LOOPBACK_HOSTNAMES, PROJECT_ID_PREFIX } from "./project.js";
@@ -1,11 +1,16 @@
1
- // Infra's contract: every call's request and answer, as zod schemas, and
2
- // every reason it refuses one, which Infra itself checks what it's sent
3
- // with and answers by, and the client reads its answers with. Nothing here
4
- // runs in Node alone, so a page can import it too, at
5
- // `@superforge/infra/contract`. Infra's README has the calls.
6
- export * from "./agents.js";
7
- export * from "./comments.js";
8
- export * from "./errors.js";
9
- export * from "./login.js";
10
- export * from "./people.js";
11
- export * from "./project.js";
1
+ // Infra's contract, as `@superforge/infra/contract` exports it: the
2
+ // constants and types every call is made of, the schemas of what Infra is
3
+ // sent, which Infra checks with, and the three answers Infra's own server
4
+ // ties itself to. Nothing here runs in Node alone, so a page can import it
5
+ // too. Infra's README has the calls.
6
+ //
7
+ // Every name here is a promise: from 1.0.0 on, taking one away is a major.
8
+ // So a name is here only when an app or Infra's server needs it; the rest
9
+ // of contract/ is the package's own (all.ts). A schema here is made with
10
+ // the package's own zod: parse with it as it is, and never put it inside a
11
+ // schema of an app's, which may be another zod's.
12
+ export { AGENT_ID, AGENT_SCOPES, APP_ORIGIN_HEADER, APP_PATHS, agent_allow, connections_query, OAUTH_CALLS } from "./agents.js";
13
+ export { COMMENT_BODY_LIMIT, filed_comment, LONGEST_COMMENT } from "./comments.js";
14
+ export { CLIENT_ERRORS, INFRA_ERRORS } from "./errors.js";
15
+ export { LOGIN_CHANNELS, login_link, login_start, login_verify, made_login_link, started_login, verified_login } from "./login.js";
16
+ export { CLIENT_HEADER, isLoopbackHost, KEY_BODY, KEY_MODES, KEY_PREFIXES, LOOPBACK_HOSTNAMES, PROJECT_ID_PREFIX } from "./project.js";
@@ -51,23 +51,19 @@ export declare const start_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
51
51
  error: z.ZodObject<{
52
52
  reason: z.ZodLiteral<"rate-limited">;
53
53
  message: z.ZodString;
54
- limit: z.ZodEnum<{
55
- project: "project";
56
- address: "address";
57
- network: "network";
58
- }>;
54
+ limit: z.ZodString;
59
55
  retry_after: z.ZodNumber;
60
56
  }, z.core.$strip>;
61
57
  }, z.core.$strip>, z.ZodTransform<{
62
58
  reason: "rate-limited";
63
59
  message: string;
64
- limit: "project" | "address" | "network";
60
+ limit: string;
65
61
  retry_after: number;
66
62
  }, {
67
63
  error: {
68
64
  reason: "rate-limited";
69
65
  message: string;
70
- limit: "project" | "address" | "network";
66
+ limit: string;
71
67
  retry_after: number;
72
68
  };
73
69
  }>>, z.ZodPipe<z.ZodObject<{
@@ -85,6 +81,50 @@ export declare const start_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
85
81
  };
86
82
  }>>]>;
87
83
  export type StartRefused = z.output<typeof start_refused>;
84
+ /**
85
+ * `POST /v1/login/links`: a login whose link the app sends in a message of
86
+ * its own, like a welcome email, where `login/start` has Infra send one.
87
+ * `link_to` must be on one of the project's domains, or this computer.
88
+ */
89
+ export declare const login_link: z.ZodObject<{
90
+ channel: z.ZodLiteral<"email">;
91
+ address: z.ZodPipe<z.ZodString, z.ZodEmail>;
92
+ ip: z.ZodUnion<readonly [z.ZodIPv4, z.ZodIPv6]>;
93
+ link_to: z.ZodURL;
94
+ }, z.core.$strip>;
95
+ export type LoginLink = z.input<typeof login_link>;
96
+ /** What `login/links` answers: the link, for the app's own message */
97
+ export declare const made_login_link: z.ZodObject<{
98
+ channel: z.ZodEnum<{
99
+ email: "email";
100
+ }>;
101
+ address: z.ZodString;
102
+ link: z.ZodString;
103
+ expires_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
104
+ }, z.core.$strip>;
105
+ export type MadeLoginLink = z.output<typeof made_login_link>;
106
+ /** Why `login/links` made nothing: over one of Infra's limits, as a start can be */
107
+ export declare const link_refused: z.ZodPipe<z.ZodObject<{
108
+ error: z.ZodObject<{
109
+ reason: z.ZodLiteral<"rate-limited">;
110
+ message: z.ZodString;
111
+ limit: z.ZodString;
112
+ retry_after: z.ZodNumber;
113
+ }, z.core.$strip>;
114
+ }, z.core.$strip>, z.ZodTransform<{
115
+ reason: "rate-limited";
116
+ message: string;
117
+ limit: string;
118
+ retry_after: number;
119
+ }, {
120
+ error: {
121
+ reason: "rate-limited";
122
+ message: string;
123
+ limit: string;
124
+ retry_after: number;
125
+ };
126
+ }>>;
127
+ export type LinkRefused = z.output<typeof link_refused>;
88
128
  /**
89
129
  * `POST /v1/login/verify`: the code, with the address it went to, or the
90
130
  * link's token alone
@@ -63,17 +63,43 @@ export const started_login = z.object({
63
63
  // held, for a dev server to show on the page. Null with a secret key.
64
64
  test: z.object({ code: z.string(), link: z.string().nullable() }).nullable()
65
65
  });
66
+ // Over one of Infra's limits: today this address (one a minute, five an
67
+ // hour), the person's network, or the project's. Read as any name, since
68
+ // Infra may add a limit, and a release that can't read its name would
69
+ // answer "login isn't working" in place of "try again soon".
70
+ const rate_limited = refusal("rate-limited", {
71
+ limit: z.string(),
72
+ retry_after: z.number()
73
+ });
66
74
  /** Why `login/start` sent nothing */
67
75
  export const start_refused = z.union([
68
- // Over one of Infra's limits: this address (one a minute, five an
69
- // hour), the person's network, or the project's
70
- refusal("rate-limited", {
71
- limit: z.enum(["address", "network", "project"]),
72
- retry_after: z.number()
73
- }),
76
+ rate_limited,
74
77
  // The message didn't go out, and nothing was counted
75
78
  refusal("not-sent", {})
76
79
  ]);
80
+ /**
81
+ * `POST /v1/login/links`: a login whose link the app sends in a message of
82
+ * its own, like a welcome email, where `login/start` has Infra send one.
83
+ * `link_to` must be on one of the project's domains, or this computer.
84
+ */
85
+ export const login_link = login_address.extend({
86
+ ip: visitor_ip,
87
+ link_to
88
+ });
89
+ /** What `login/links` answers: the link, for the app's own message */
90
+ export const made_login_link = z.object({
91
+ channel: login_channel,
92
+ // The address as Infra keeps it
93
+ address: z.string(),
94
+ // `link_to#<token>`, which logs in once, until `expires_at`. A newer
95
+ // login to the same address replaces it.
96
+ link: z.string(),
97
+ expires_at: z.iso
98
+ .datetime({ offset: true })
99
+ .transform((value) => new Date(value))
100
+ });
101
+ /** Why `login/links` made nothing: over one of Infra's limits, as a start can be */
102
+ export const link_refused = rate_limited;
77
103
  /**
78
104
  * `POST /v1/login/verify`: the code, with the address it went to, or the
79
105
  * link's token alone
package/output/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- export { oauthMetadata, requestOrigin } from "./agents.js";
1
+ export { requestOrigin } from "./agents.js";
2
2
  export { chooseInfra, type InfraChoice } from "./choose.js";
3
- export { INFRA_URL, infraClient } from "./client.js";
4
- export * from "./contract/index.js";
3
+ export { infraClient } from "./client.js";
4
+ export { AGENT_SCOPES, type AgentAllow, type AgentConnection, type AgentRefused, type AgentRequest, type AgentScope, APP_PATHS, type AppPaths, type ClientErrorReason, type CommentRefused, type FiledComment, type InfraErrorReason, type InfraIssue, type InfraProject, type KeptComment, type KeyMode, type LinkRefused, type LoginChannel, type LoginLink, type LoginProof, type LoginStart, type MadeLoginLink, OAUTH_CALLS, type OAuthCall, type StartedLogin, type StartRefused, type VerifiedLogin, type VerifyRefused } from "./contract/index.js";
5
5
  export type { AgentToken, AgentTokenRefused, Infra, InfraUnavailable } from "./infra.js";
6
6
  export type { AsyncOperationResult, OperationResult } from "./results.js";
7
- export { standInAgentToken, standInInfra, type WaitingLogin } from "./stand-in.js";
8
- export { PACKAGE_VERSION } from "./version.js";
7
+ export { standInAgentToken, standInInfra } from "./stand-in.js";
package/output/index.js CHANGED
@@ -1,19 +1,18 @@
1
1
  // @superforge/infra: Superforge Infra, the backend services Foundation and
2
2
  // Skylevel apps run on, as an app's server calls it. README.md has how an
3
- // app uses it. Infra's contract, every call's request and answer as zod
4
- // schemas, is here too, and on its own at `@superforge/infra/contract`,
5
- // which needs nothing of Node's, for a page.
6
- // Claude's login to an app's MCP: where a request reached the app, which
7
- // every token is bound to, and the OAuth server's metadata
8
- export { oauthMetadata, requestOrigin } from "./agents.js";
9
- // Calling Infra: the real one over HTTP with a project's key, the
10
- // stand-in, Infra's test mode in memory, and choosing between them by how
11
- // the server runs and the keys it has
3
+ // app uses it.
4
+ //
5
+ // Every name here is a promise: from 1.0.0 on, taking one away is a major.
6
+ // So the package exports what an app calls, its constants and its types,
7
+ // and no zod schema: those are at `@superforge/infra/contract` alone
8
+ // (contract/index.ts), made with the package's own zod.
9
+ // Where a request reached the app, which every token Claude holds is bound to
10
+ export { requestOrigin } from "./agents.js";
11
+ // Calling Infra: the real one over HTTP with a project's key, chosen by
12
+ // how the server runs and the keys it has, or the stand-in, Infra's test
13
+ // mode in memory, which an app's tests use
12
14
  export { chooseInfra } from "./choose.js";
13
- export { INFRA_URL, infraClient } from "./client.js";
14
- // Infra's contract (contract/): every call's request and answer, every
15
- // reason it refuses one, and the constants they're made of
16
- export * from "./contract/index.js";
15
+ export { infraClient } from "./client.js";
16
+ // The constants and types of Infra's contract an app calls with
17
+ export { AGENT_SCOPES, APP_PATHS, OAUTH_CALLS } from "./contract/index.js";
17
18
  export { standInAgentToken, standInInfra } from "./stand-in.js";
18
- // Which release this is, which every call tells Infra in `Infra-Client`
19
- export { PACKAGE_VERSION } from "./version.js";
package/output/infra.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AgentAllow, AgentConnection, AgentRefused, AgentRequest, CommentRefused, FiledComment, InfraProject, KeptComment, LoginProof, LoginStart, OAuthCall, StartedLogin, StartRefused, VerifiedLogin, VerifyRefused } from "./contract/index.js";
1
+ import type { AgentAllow, AgentConnection, AgentRefused, AgentRequest, CommentRefused, FiledComment, InfraProject, KeptComment, LinkRefused, LoginLink, LoginProof, LoginStart, MadeLoginLink, OAuthCall, StartedLogin, StartRefused, VerifiedLogin, VerifyRefused } from "./contract/all.js";
2
2
  import type { AsyncOperationResult } from "./results.js";
3
3
  /** What an app asks of Infra, whichever answers: the real one or the stand-in */
4
4
  export type Infra = {
@@ -13,6 +13,14 @@ export type Infra = {
13
13
  * `ip` is theirs, which Infra's limits count.
14
14
  */
15
15
  startLogin(request: LoginStart): AsyncOperationResult<StartedLogin, StartRefused | InfraUnavailable>;
16
+ /**
17
+ * Makes a login whose link the app sends in a message of its own, like
18
+ * a welcome email, rather than Infra sending one: a link to `link_to`,
19
+ * which must be on one of the project's domains or localhost, that
20
+ * logs in once, for 24 hours. It's counted and limited as a login
21
+ * Infra sends is. `ip` is the person's, which Infra's limits count.
22
+ */
23
+ makeLoginLink(request: LoginLink): AsyncOperationResult<MadeLoginLink, LinkRefused | InfraUnavailable>;
16
24
  /**
17
25
  * Whose address `proof` is: the code typed with the address it went to,
18
26
  * or the link's token alone. Either works once, and spends the other.
@@ -1,4 +1,4 @@
1
- import { type AgentScope } from "./contract/index.js";
1
+ import { type AgentScope } from "./contract/all.js";
2
2
  import type { Infra } from "./infra.js";
3
3
  /** A login the stand-in sent, waiting to be verified, by its address */
4
4
  export type WaitingLogin = {
@@ -13,7 +13,8 @@ export type WaitingLogin = {
13
13
  * with no key and no network. Starting a login sends nothing and answers
14
14
  * the code and the link, and verifying checks them as Infra does: each
15
15
  * works once, for 15 minutes, a code for five tries, and using either
16
- * spends both. No limits apply, as none do to a test key. A request is
16
+ * spends both. A link made for the app to send itself works once, for 24
17
+ * hours. No limits apply, as none do to a test key. A request is
17
18
  * checked with Infra's own contract, as Infra checks it, and a comment is
18
19
  * answered an ID, though nothing keeps it. Its project has the `domains`
19
20
  * it's given, an app's website's. An app's tests always use it, so they
@@ -22,13 +23,19 @@ export type WaitingLogin = {
22
23
  * the page shows the code. Its refusals have Infra's reasons, though not
23
24
  * always its words, which aren't contract. Like Infra, it links only to
24
25
  * this computer and the project's `domains`, over https, and answers
25
- * `with_link: false` for any other host.
26
- *
27
- * `waiting` is where it keeps what it sent, by address: its own, or one
28
- * that outlives a dev server's reloads. With `logs`, as a dev server has
29
- * it, each comment is logged, since nothing keeps it.
26
+ * `with_link: false` for any other host, or makes no link the app sends
27
+ * itself.
30
28
  */
31
- export declare function standInInfra({ waiting, logs, domains }?: {
29
+ export declare function standInInfra({ domains }?: {
30
+ domains?: string[];
31
+ }): Infra;
32
+ /**
33
+ * The stand-in as the package itself makes it, a dev server's in
34
+ * choose.ts: `waiting` is where it keeps what it sent, by address, its own
35
+ * or one that outlives a dev server's reloads. With `logs`, as a dev
36
+ * server has it, each comment is logged, since nothing keeps it.
37
+ */
38
+ export declare function standInWith({ waiting, logs, domains }?: {
32
39
  waiting?: Map<string, WaitingLogin>;
33
40
  logs?: boolean;
34
41
  domains?: string[];
@@ -1,18 +1,21 @@
1
1
  import { createLocalJWKSet, exportJWK, generateKeyPair, SignJWT } from "jose";
2
2
  import { z } from "zod";
3
3
  import { checkToken, unavailableOAuth } from "./agents.js";
4
- import { COMMENT_BODY_LIMIT, filed_comment, isLoopbackHost, login_start, login_verify } from "./contract/index.js";
4
+ import { COMMENT_BODY_LIMIT, filed_comment, isLoopbackHost, login_link, login_start, login_verify } from "./contract/all.js";
5
5
  import { MINUTE, randomToken } from "./primitives.js";
6
6
  // How Infra treats a code: it works for 15 minutes and five tries, and a
7
- // new one for the same address replaces it, link and all
7
+ // new one for the same address replaces it, link and all. A link the app
8
+ // sends itself works for 24 hours, since its message may be read later.
8
9
  const LIFETIME = 15 * MINUTE;
10
+ const LINK_LIFETIME = 24 * 60 * MINUTE;
9
11
  const ATTEMPTS = 5;
10
12
  /**
11
13
  * Infra's test mode, in memory: what a test key gets from the real one,
12
14
  * with no key and no network. Starting a login sends nothing and answers
13
15
  * the code and the link, and verifying checks them as Infra does: each
14
16
  * works once, for 15 minutes, a code for five tries, and using either
15
- * spends both. No limits apply, as none do to a test key. A request is
17
+ * spends both. A link made for the app to send itself works once, for 24
18
+ * hours. No limits apply, as none do to a test key. A request is
16
19
  * checked with Infra's own contract, as Infra checks it, and a comment is
17
20
  * answered an ID, though nothing keeps it. Its project has the `domains`
18
21
  * it's given, an app's website's. An app's tests always use it, so they
@@ -21,13 +24,19 @@ const ATTEMPTS = 5;
21
24
  * the page shows the code. Its refusals have Infra's reasons, though not
22
25
  * always its words, which aren't contract. Like Infra, it links only to
23
26
  * this computer and the project's `domains`, over https, and answers
24
- * `with_link: false` for any other host.
25
- *
26
- * `waiting` is where it keeps what it sent, by address: its own, or one
27
- * that outlives a dev server's reloads. With `logs`, as a dev server has
28
- * it, each comment is logged, since nothing keeps it.
27
+ * `with_link: false` for any other host, or makes no link the app sends
28
+ * itself.
29
29
  */
30
- export function standInInfra({ waiting = new Map(), logs = false, domains = [] } = {}) {
30
+ export function standInInfra({ domains = [] } = {}) {
31
+ return standInWith({ domains });
32
+ }
33
+ /**
34
+ * The stand-in as the package itself makes it, a dev server's in
35
+ * choose.ts: `waiting` is where it keeps what it sent, by address, its own
36
+ * or one that outlives a dev server's reloads. With `logs`, as a dev
37
+ * server has it, each comment is logged, since nothing keeps it.
38
+ */
39
+ export function standInWith({ waiting = new Map(), logs = false, domains = [] } = {}) {
31
40
  return {
32
41
  async project() {
33
42
  return {
@@ -71,6 +80,42 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
71
80
  }
72
81
  };
73
82
  },
83
+ async makeLoginLink(request) {
84
+ const parsed = login_link.safeParse(request);
85
+ if (!parsed.success) {
86
+ return misfit(parsed.error);
87
+ }
88
+ const { channel, address, link_to } = parsed.data;
89
+ const page = linkPage(link_to, domains);
90
+ if (page === null) {
91
+ // Infra answers it 400 `invalid`, which the client reads
92
+ // as unavailable: the app named a page that isn't its own
93
+ return {
94
+ ok: false,
95
+ error: {
96
+ reason: "unavailable",
97
+ message: `link_to isn't on one of the project's domains (${domains.join(", ") || "none"}) or this computer, so Infra makes no link to it.`
98
+ }
99
+ };
100
+ }
101
+ const made = {
102
+ address,
103
+ code: randomCode(),
104
+ token: randomToken(),
105
+ attempts: 0,
106
+ expires_at: Date.now() + LINK_LIFETIME
107
+ };
108
+ waiting.set(address, made);
109
+ return {
110
+ ok: true,
111
+ data: {
112
+ channel,
113
+ address,
114
+ link: `${page}#${made.token}`,
115
+ expires_at: new Date(made.expires_at)
116
+ }
117
+ };
118
+ },
74
119
  async verifyLogin(given) {
75
120
  const parsed = login_verify.safeParse(given);
76
121
  if (!parsed.success) {
@@ -231,7 +276,7 @@ const NO_CODE = {
231
276
  };
232
277
  const NO_LINK = {
233
278
  reason: "no-link",
234
- message: `That link has been used, ran out after ${LIFETIME / MINUTE} minutes, or isn't this project's. Start a new login.`
279
+ message: "That link has been used, ran out (after 15 minutes, or 24 hours for one the app sent itself), or isn't this project's. Start a new login."
235
280
  };
236
281
  const NO_REQUEST = {
237
282
  reason: "not-found",
@@ -1,4 +1,4 @@
1
1
  /** The package's version, as package.json says it */
2
- export declare const PACKAGE_VERSION = "0.4.0";
2
+ export declare const PACKAGE_VERSION = "0.6.0";
3
3
  /** What it says: the package and its version, `@superforge/infra/<version>` */
4
- export declare const CLIENT = "@superforge/infra/0.4.0";
4
+ export declare const CLIENT = "@superforge/infra/0.6.0";
package/output/version.js CHANGED
@@ -5,6 +5,6 @@
5
5
  // package away from its package.json; a test checks the two agree, so a
6
6
  // release that bumps one and not the other fails `pnpm check`.
7
7
  /** The package's version, as package.json says it */
8
- export const PACKAGE_VERSION = "0.4.0";
8
+ export const PACKAGE_VERSION = "0.6.0";
9
9
  /** What it says: the package and its version, `@superforge/infra/<version>` */
10
10
  export const CLIENT = `@superforge/infra/${PACKAGE_VERSION}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superforge/infra",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Superforge Infra, the backend services Foundation and Skylevel apps run on, as an app's server calls it",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -37,17 +37,14 @@
37
37
  "prepublishOnly": "pnpm check"
38
38
  },
39
39
  "dependencies": {
40
- "jose": "^6.2.15"
41
- },
42
- "peerDependencies": {
43
- "zod": "^4.6.7"
40
+ "jose": "^6.2.15",
41
+ "zod": "^4.0.0"
44
42
  },
45
43
  "devDependencies": {
46
44
  "@biomejs/biome": "^2.6.2",
47
45
  "@types/node": "^22.10.2",
48
46
  "hono": "^4.13.13",
49
47
  "typescript": "^6.0.2",
50
- "vitest": "^5.0.5",
51
- "zod": "^4.6.7"
48
+ "vitest": "^5.0.5"
52
49
  }
53
50
  }