@superforge/infra 0.3.0 → 0.5.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/output/client.js CHANGED
@@ -1,12 +1,9 @@
1
1
  import { checkToken, infraKeys, passOAuth } from "./agents.js";
2
- import { 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";
3
- import { SECOND } from "./primitives.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/all.js";
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 */
6
6
  export const INFRA_URL = "https://infra.superforge.studio/v1";
7
- // How long a call waits for Infra's answer. Starting a login waits on
8
- // Infra sending the email, which takes a second or two.
9
- const TIMEOUT = 15 * SECOND;
10
7
  /**
11
8
  * Infra, called over HTTP with a project's key: a secret key in a build, a
12
9
  * test key in a dev server. No call is retried, since none is safe to send
@@ -35,7 +32,7 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
35
32
  method,
36
33
  headers,
37
34
  body: body === undefined ? undefined : JSON.stringify(body),
38
- signal: AbortSignal.timeout(TIMEOUT)
35
+ signal: AbortSignal.timeout(CALL_TIMEOUT)
39
36
  }).catch((error) => {
40
37
  console.error("Infra didn't answer", { call, url, error });
41
38
  return null;
@@ -68,16 +65,16 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
68
65
  }
69
66
  return { ok: true, data: { status: response.status, json } };
70
67
  }
71
- // An answer read with `schema` when it's `status`, else as one of the
72
- // call's `refused`, else as one the client can't act on: unavailable,
73
- // in Infra's own words
68
+ // An answer read with `schema` when it's a success (any 2xx, so Infra
69
+ // can say 200 or 201 as it likes), else as one of the call's `refused`,
70
+ // else as one the client can't act on: unavailable, in Infra's own words
74
71
  async function answered(asked, call, expected, refused) {
75
72
  const answer = await asked;
76
73
  if (!answer.ok) {
77
74
  return answer;
78
75
  }
79
76
  const { status, json } = answer.data;
80
- if (status === expected.status) {
77
+ if (status >= 200 && status < 300) {
81
78
  const parsed = expected.schema.safeParse(json);
82
79
  return parsed.success
83
80
  ? { ok: true, data: parsed.data }
@@ -94,12 +91,11 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
94
91
  // Claude's login (agents.ts): Infra's public keys, fetched once and
95
92
  // again for a key they don't have, and which project and mode the key
96
93
  // is, which every token must be for, asked once
97
- const keys = infraKeys(url);
94
+ const keys = infraKeys(url, send);
98
95
  let project;
99
96
  function projectOf() {
100
97
  const call = "/project";
101
98
  return answered(ask("GET", call), call, {
102
- status: 200,
103
99
  schema: infra_project
104
100
  });
105
101
  }
@@ -109,15 +105,15 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
109
105
  },
110
106
  startLogin(request) {
111
107
  const call = "/login/start";
112
- return answered(ask("POST", call, request), call, { status: 200, schema: started_login }, start_refused);
108
+ return answered(ask("POST", call, request), call, { schema: started_login }, start_refused);
113
109
  },
114
110
  verifyLogin(proof) {
115
111
  const call = "/login/verify";
116
- return answered(ask("POST", call, proof), call, { status: 200, schema: verified_login }, verify_refused);
112
+ return answered(ask("POST", call, proof), call, { schema: verified_login }, verify_refused);
117
113
  },
118
114
  async fileComment(comment) {
119
115
  const call = "/board/comments";
120
- const filed = await answered(ask("POST", call, comment), call, { status: 201, schema: kept_comment }, comment_refused);
116
+ const filed = await answered(ask("POST", call, comment), call, { schema: kept_comment }, comment_refused);
121
117
  if (!filed.ok && filed.error.reason !== "unavailable") {
122
118
  // What the app sent is wrong, which only a change to it
123
119
  // fixes
@@ -132,22 +128,30 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
132
128
  return passOAuth({ url, key, send, paths, request, call, origin });
133
129
  },
134
130
  async agentRequest(request_id) {
135
- const call = `/agents/requests/${request_id}`;
136
- const asked = await answered(ask("GET", call), call, { status: 200, schema: agent_request }, agent_refused);
131
+ if (!AGENT_ID.test(request_id)) {
132
+ return notAnId("request", request_id);
133
+ }
134
+ const call = `/agents/requests/${encodeURIComponent(request_id)}`;
135
+ const asked = await answered(ask("GET", call), call, { schema: agent_request }, agent_refused);
137
136
  return asked.ok ? { ok: true, data: asked.data.request } : asked;
138
137
  },
139
- allowAgent(request_id, allowed) {
140
- const call = `/agents/requests/${request_id}/allow`;
141
- return answered(ask("POST", call, allowed), call, { status: 200, schema: agent_redirect }, agent_refused);
138
+ async allowAgent(request_id, allowed) {
139
+ if (!AGENT_ID.test(request_id)) {
140
+ return notAnId("request", request_id);
141
+ }
142
+ const call = `/agents/requests/${encodeURIComponent(request_id)}/allow`;
143
+ return answered(ask("POST", call, allowed), call, { schema: agent_redirect }, agent_refused);
142
144
  },
143
- denyAgent(request_id) {
144
- const call = `/agents/requests/${request_id}/deny`;
145
- return answered(ask("POST", call, {}), call, { status: 200, schema: agent_redirect }, agent_refused);
145
+ async denyAgent(request_id) {
146
+ if (!AGENT_ID.test(request_id)) {
147
+ return notAnId("request", request_id);
148
+ }
149
+ const call = `/agents/requests/${encodeURIComponent(request_id)}/deny`;
150
+ return answered(ask("POST", call, {}), call, { schema: agent_redirect }, agent_refused);
146
151
  },
147
152
  async agentConnections(user_id) {
148
153
  const call = `/agents/connections?${new URLSearchParams({ user_id })}`;
149
154
  const listed = await answered(ask("GET", call), call, {
150
- status: 200,
151
155
  schema: agent_connections
152
156
  });
153
157
  return listed.ok
@@ -155,14 +159,16 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
155
159
  : listed;
156
160
  },
157
161
  async endAgentConnection(connection_id) {
158
- const call = `/agents/connections/${connection_id}`;
159
- const ended = await answered(ask("DELETE", call), call, { status: 200, schema: agent_ended }, agent_refused);
162
+ if (!AGENT_ID.test(connection_id)) {
163
+ return notAnId("connection", connection_id);
164
+ }
165
+ const call = `/agents/connections/${encodeURIComponent(connection_id)}`;
166
+ const ended = await answered(ask("DELETE", call), call, { schema: agent_ended }, agent_refused);
160
167
  return ended.ok ? { ok: true, data: null } : ended;
161
168
  },
162
169
  async endAgentConnections(user_id) {
163
170
  const call = `/agents/connections?${new URLSearchParams({ user_id })}`;
164
171
  const ended = await answered(ask("DELETE", call), call, {
165
- status: 200,
166
172
  schema: agent_ended
167
173
  });
168
174
  return ended.ok ? { ok: true, data: ended.data.ended } : ended;
@@ -187,6 +193,19 @@ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PA
187
193
  }
188
194
  };
189
195
  }
196
+ // An ID that can't be one of Infra's, refused before any call, so a route
197
+ // that passes on what it was sent can't make the path reach another of
198
+ // Infra's addresses: what Infra would answer, since it has none such
199
+ function notAnId(kind, id) {
200
+ console.warn("Not an ID Infra makes", { kind, length: id.length });
201
+ return {
202
+ ok: false,
203
+ error: {
204
+ reason: "not-found",
205
+ message: `There's no ${kind} with this ID: Infra's IDs are letters, digits, _ and -, at most 128 of them.`
206
+ }
207
+ };
208
+ }
190
209
  // Infra couldn't be asked, and why
191
210
  function unavailable(message) {
192
211
  return { ok: false, error: { reason: "unavailable", message } };
@@ -8,7 +8,7 @@ export declare const agent_scope: z.ZodEnum<{
8
8
  export type AgentScope = z.infer<typeof agent_scope>;
9
9
  /**
10
10
  * The header the app's server sends Claude's OAuth calls on with: where
11
- * Claude reached the app, `https://vmdsystem.com`, its tokens' issuer.
11
+ * Claude reached the app, `https://example.com`, its tokens' issuer.
12
12
  * Only a header can carry it, since the call's body and query are Claude's.
13
13
  */
14
14
  export declare const APP_ORIGIN_HEADER = "App-Origin";
@@ -24,6 +24,12 @@ export type AppPaths = {
24
24
  allow: string;
25
25
  };
26
26
  export declare const APP_PATHS: AppPaths;
27
+ /**
28
+ * An ID Infra makes for a request or a connection, as the app puts it in a
29
+ * path: letters, digits, `_` and `-`, so it can't reach another address
30
+ */
31
+ export declare const AGENT_ID: RegExp;
32
+ export declare const agent_id: z.ZodString;
27
33
  /** The OAuth calls Claude makes, which the app passes on */
28
34
  export declare const OAUTH_CALLS: readonly ["authorize", "token", "register", "revoke"];
29
35
  export type OAuthCall = (typeof OAUTH_CALLS)[number];
@@ -34,7 +40,7 @@ export type OAuthCall = (typeof OAUTH_CALLS)[number];
34
40
  */
35
41
  export declare const agent_authorized: z.ZodUnion<readonly [z.ZodObject<{
36
42
  request: z.ZodObject<{
37
- id: z.ZodUUID;
43
+ id: z.ZodString;
38
44
  }, z.core.$strip>;
39
45
  }, z.core.$strip>, z.ZodObject<{
40
46
  redirect_to: z.ZodURL;
@@ -97,10 +103,7 @@ export declare const agent_request: z.ZodObject<{
97
103
  request: z.ZodObject<{
98
104
  id: z.ZodString;
99
105
  client_name: z.ZodString;
100
- scopes: z.ZodArray<z.ZodEnum<{
101
- read: "read";
102
- act: "act";
103
- }>>;
106
+ scopes: z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("read" | "act")[], string[]>>;
104
107
  resource: z.ZodString;
105
108
  expires_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
106
109
  }, z.core.$strip>;
@@ -128,10 +131,7 @@ export declare const agent_connection: z.ZodObject<{
128
131
  id: z.ZodString;
129
132
  user_id: z.ZodString;
130
133
  client_name: z.ZodString;
131
- scopes: z.ZodArray<z.ZodEnum<{
132
- read: "read";
133
- act: "act";
134
- }>>;
134
+ scopes: z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("read" | "act")[], string[]>>;
135
135
  resource: z.ZodString;
136
136
  last_used_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
137
137
  created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
@@ -143,10 +143,7 @@ export declare const agent_connections: z.ZodObject<{
143
143
  id: z.ZodString;
144
144
  user_id: z.ZodString;
145
145
  client_name: z.ZodString;
146
- scopes: z.ZodArray<z.ZodEnum<{
147
- read: "read";
148
- act: "act";
149
- }>>;
146
+ scopes: z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("read" | "act")[], string[]>>;
150
147
  resource: z.ZodString;
151
148
  last_used_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
152
149
  created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
@@ -12,13 +12,27 @@ 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
- * Claude reached the app, `https://vmdsystem.com`, its tokens' issuer.
25
+ * Claude reached the app, `https://example.com`, its tokens' issuer.
18
26
  * Only a header can carry it, since the call's body and query are Claude's.
19
27
  */
20
28
  export const APP_ORIGIN_HEADER = "App-Origin";
21
29
  export const APP_PATHS = { oauth: "/oauth", allow: "/connect" };
30
+ /**
31
+ * An ID Infra makes for a request or a connection, as the app puts it in a
32
+ * path: letters, digits, `_` and `-`, so it can't reach another address
33
+ */
34
+ export const AGENT_ID = /^[\w-]{1,128}$/;
35
+ export const agent_id = z.string().regex(AGENT_ID);
22
36
  /** The OAuth calls Claude makes, which the app passes on */
23
37
  export const OAUTH_CALLS = ["authorize", "token", "register", "revoke"];
24
38
  /**
@@ -27,8 +41,8 @@ export const OAUTH_CALLS = ["authorize", "token", "register", "revoke"];
27
41
  * goes back to Claude with an error
28
42
  */
29
43
  export const agent_authorized = z.union([
30
- z.object({ request: z.object({ id: z.uuid() }) }),
31
- z.object({ redirect_to: z.url() })
44
+ z.object({ request: z.object({ id: agent_id }) }),
45
+ z.object({ redirect_to: z.url({ protocol: /^https?$/ }) })
32
46
  ]);
33
47
  /** Why authorize sends nothing back to Claude: answered as plain words */
34
48
  export const authorize_refused = z.union([
@@ -41,11 +55,13 @@ export const agent_request = z.object({
41
55
  request: z.object({
42
56
  id: z.string(),
43
57
  client_name: z.string(),
44
- scopes: z.array(agent_scope),
58
+ scopes: known_scopes,
45
59
  // The address Claude asked to use, which its tokens are good for:
46
60
  // the app's MCP
47
61
  resource: z.string(),
48
- expires_at: z.iso.datetime().transform((value) => new Date(value))
62
+ expires_at: z.iso
63
+ .datetime({ offset: true })
64
+ .transform((value) => new Date(value))
49
65
  })
50
66
  });
51
67
  /** `POST /v1/agents/requests/:request_id/allow`: who allowed what */
@@ -54,7 +70,9 @@ export const agent_allow = z.object({
54
70
  scopes: z.array(agent_scope).min(1)
55
71
  });
56
72
  /** What allowing or denying answers: where the browser goes back to Claude */
57
- export const agent_redirect = z.object({ redirect_to: z.url() });
73
+ export const agent_redirect = z.object({
74
+ redirect_to: z.url({ protocol: /^https?$/ })
75
+ });
58
76
  /** `GET` and `DELETE /v1/agents/connections?user_id=…`: whose */
59
77
  export const connections_query = z.object({ user_id });
60
78
  /** One of Claude's connections, as the app's Connected list shows it */
@@ -62,11 +80,15 @@ export const agent_connection = z.object({
62
80
  id: z.string(),
63
81
  user_id: z.string(),
64
82
  client_name: z.string(),
65
- scopes: z.array(agent_scope),
83
+ scopes: known_scopes,
66
84
  resource: z.string(),
67
85
  // When Claude last refreshed it, to within about ten minutes
68
- last_used_at: z.iso.datetime().transform((value) => new Date(value)),
69
- created_at: z.iso.datetime().transform((value) => new Date(value))
86
+ last_used_at: z.iso
87
+ .datetime({ offset: true })
88
+ .transform((value) => new Date(value)),
89
+ created_at: z.iso
90
+ .datetime({ offset: true })
91
+ .transform((value) => new Date(value))
70
92
  });
71
93
  /** `GET /v1/agents/connections?user_id=…`: theirs, oldest first */
72
94
  export const agent_connections = z.object({
@@ -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";
@@ -55,7 +55,9 @@ export const kept_comment = z
55
55
  .object({
56
56
  comment: z.object({
57
57
  id: z.string(),
58
- created_at: z.iso.datetime().transform((value) => new Date(value))
58
+ created_at: z.iso
59
+ .datetime({ offset: true })
60
+ .transform((value) => new Date(value))
59
61
  })
60
62
  })
61
63
  .transform(({ comment }) => comment);
@@ -20,6 +20,16 @@ export declare const INFRA_ERRORS: {
20
20
  };
21
21
  /** A reason Infra refuses a call with */
22
22
  export type InfraErrorReason = keyof typeof INFRA_ERRORS;
23
+ /**
24
+ * The reasons the package itself answers with, never Infra: beside
25
+ * `INFRA_ERRORS`, so the two lists hold every reason an app sees
26
+ */
27
+ export declare const CLIENT_ERRORS: {
28
+ readonly unavailable: "Infra couldn't be asked: no key in a build, a key it refused, no answer, or an answer outside the contract; `message` says which";
29
+ readonly "invalid-token": "A token Claude brought isn't one Infra signed for this app, address, project and mode, or it ran out; `message` says which";
30
+ };
31
+ /** A reason the package answers with */
32
+ export type ClientErrorReason = keyof typeof CLIENT_ERRORS;
23
33
  /** What didn't fit, for `invalid`: a dotted path, and why */
24
34
  export declare const infra_issue: z.ZodObject<{
25
35
  path: z.ZodString;
@@ -31,6 +31,14 @@ export const INFRA_ERRORS = {
31
31
  "unknown-client": "Who's asking to connect isn't Claude, by its return address",
32
32
  "unknown-origin": "The app's origin isn't one its key may log Claude in at"
33
33
  };
34
+ /**
35
+ * The reasons the package itself answers with, never Infra: beside
36
+ * `INFRA_ERRORS`, so the two lists hold every reason an app sees
37
+ */
38
+ export const CLIENT_ERRORS = {
39
+ unavailable: "Infra couldn't be asked: no key in a build, a key it refused, no answer, or an answer outside the contract; `message` says which",
40
+ "invalid-token": "A token Claude brought isn't one Infra signed for this app, address, project and mode, or it ran out; `message` says which"
41
+ };
34
42
  /** What didn't fit, for `invalid`: a dotted path, and why */
35
43
  export const infra_issue = z.object({ path: z.string(), message: z.string() });
36
44
  /** Any refusal of Infra's, read for its reason and its words */
@@ -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 { LOGIN_CHANNELS, type LoginChannel, type LoginProof, type LoginStart, login_start, login_verify, 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 two 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_start, login_verify, 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";
@@ -20,7 +20,7 @@ export declare const login_token: z.ZodString;
20
20
  /** The person's IP address, which Infra's limits count by network */
21
21
  export declare const visitor_ip: z.ZodUnion<readonly [z.ZodIPv4, z.ZodIPv6]>;
22
22
  /**
23
- * The page a login link opens, like `https://vmdsystem.com/login`: Infra
23
+ * The page a login link opens, like `https://example.com/login`: Infra
24
24
  * adds `#<token>`, and links only to the project's domains and localhost
25
25
  */
26
26
  export declare const link_to: z.ZodURL;
@@ -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<{
@@ -29,7 +29,7 @@ export const login_token = z.string().regex(/^[\w-]{43}$/);
29
29
  /** The person's IP address, which Infra's limits count by network */
30
30
  export const visitor_ip = z.union([z.ipv4(), z.ipv6()]);
31
31
  /**
32
- * The page a login link opens, like `https://vmdsystem.com/login`: Infra
32
+ * The page a login link opens, like `https://example.com/login`: Infra
33
33
  * adds `#<token>`, and links only to the project's domains and localhost
34
34
  */
35
35
  export const link_to = z
@@ -56,17 +56,21 @@ export const started_login = z.object({
56
56
  // Whether the message holds a link as well as the code
57
57
  with_link: z.boolean(),
58
58
  // When the code and the link stop working
59
- expires_at: z.iso.datetime().transform((value) => new Date(value)),
59
+ expires_at: z.iso
60
+ .datetime({ offset: true })
61
+ .transform((value) => new Date(value)),
60
62
  // With a test key, which sends nothing: what the message would have
61
63
  // held, for a dev server to show on the page. Null with a secret key.
62
64
  test: z.object({ code: z.string(), link: z.string().nullable() }).nullable()
63
65
  });
64
66
  /** Why `login/start` sent nothing */
65
67
  export const start_refused = z.union([
66
- // Over one of Infra's limits: this address (one a minute, five an
67
- // hour), the person's network, or the project's
68
+ // Over one of Infra's limits: today this address (one a minute, five
69
+ // an hour), the person's network, or the project's. Read as any name,
70
+ // since Infra may add a limit, and a release that can't read its name
71
+ // would answer "login isn't working" in place of "try again soon".
68
72
  refusal("rate-limited", {
69
- limit: z.enum(["address", "network", "project"]),
73
+ limit: z.string(),
70
74
  retry_after: z.number()
71
75
  }),
72
76
  // The message didn't go out, and nothing was counted
@@ -9,11 +9,27 @@ export declare const key_mode: z.ZodEnum<{
9
9
  }>;
10
10
  export type KeyMode = z.infer<typeof key_mode>;
11
11
  /**
12
- * Each mode's key's prefix, then 32 letters and digits. The prefix lets a
12
+ * Each mode's key's prefix, then 32 to 128 letters, digits and
13
+ * underscores (`KEY_BODY`). The prefix lets a
13
14
  * leak scanner spot one, and tells a test key from a secret one at a
14
15
  * glance.
15
16
  */
16
17
  export declare const KEY_PREFIXES: Record<KeyMode, string>;
18
+ /**
19
+ * What follows a key's prefix. Infra makes 32 letters and digits today; a
20
+ * longer key, or one with a checksum after an underscore, still fits, so
21
+ * Infra can change how it makes them without every app refusing the new
22
+ * ones.
23
+ */
24
+ export declare const KEY_BODY = "[0-9A-Za-z_]{32,128}";
25
+ /**
26
+ * The hostnames that are this computer: a dev server's, which a test key
27
+ * logs Claude in at and a login link always goes to, over plain http.
28
+ * Names under `localhost` (`app.localhost`) are too (`isLoopbackHost`).
29
+ */
30
+ export declare const LOOPBACK_HOSTNAMES: readonly ["localhost", "127.0.0.1", "[::1]"];
31
+ /** Whether `hostname`, as a URL's `hostname` has it, is this computer */
32
+ export declare function isLoopbackHost(hostname: string): boolean;
17
33
  /**
18
34
  * `GET /v1/project`: which project the key is for, its mode, and its
19
35
  * domains, the only hosts besides localhost a login link goes to and
@@ -33,6 +49,6 @@ export declare const infra_project: z.ZodObject<{
33
49
  export type InfraProject = z.output<typeof infra_project>;
34
50
  /**
35
51
  * The header every call carries, saying which release of @superforge/infra
36
- * made it (`@superforge/infra/0.3.0`), which Infra logs and keeps per key
52
+ * made it (`@superforge/infra/<version>`), which Infra logs and keeps per key
37
53
  */
38
54
  export declare const CLIENT_HEADER = "Infra-Client";
@@ -8,7 +8,8 @@ export const PROJECT_ID_PREFIX = "sf_project_";
8
8
  export const KEY_MODES = ["live", "test"];
9
9
  export const key_mode = z.enum(KEY_MODES);
10
10
  /**
11
- * Each mode's key's prefix, then 32 letters and digits. The prefix lets a
11
+ * Each mode's key's prefix, then 32 to 128 letters, digits and
12
+ * underscores (`KEY_BODY`). The prefix lets a
12
13
  * leak scanner spot one, and tells a test key from a secret one at a
13
14
  * glance.
14
15
  */
@@ -16,6 +17,25 @@ export const KEY_PREFIXES = {
16
17
  live: "sf_secret_",
17
18
  test: "sf_test_"
18
19
  };
20
+ /**
21
+ * What follows a key's prefix. Infra makes 32 letters and digits today; a
22
+ * longer key, or one with a checksum after an underscore, still fits, so
23
+ * Infra can change how it makes them without every app refusing the new
24
+ * ones.
25
+ */
26
+ export const KEY_BODY = "[0-9A-Za-z_]{32,128}";
27
+ /**
28
+ * The hostnames that are this computer: a dev server's, which a test key
29
+ * logs Claude in at and a login link always goes to, over plain http.
30
+ * Names under `localhost` (`app.localhost`) are too (`isLoopbackHost`).
31
+ */
32
+ export const LOOPBACK_HOSTNAMES = ["localhost", "127.0.0.1", "[::1]"];
33
+ /** Whether `hostname`, as a URL's `hostname` has it, is this computer */
34
+ export function isLoopbackHost(hostname) {
35
+ const name = hostname.toLowerCase();
36
+ return (LOOPBACK_HOSTNAMES.includes(name) ||
37
+ name.endsWith(".localhost"));
38
+ }
19
39
  /**
20
40
  * `GET /v1/project`: which project the key is for, its mode, and its
21
41
  * domains, the only hosts besides localhost a login link goes to and
@@ -31,6 +51,6 @@ export const infra_project = z.object({
31
51
  });
32
52
  /**
33
53
  * The header every call carries, saying which release of @superforge/infra
34
- * made it (`@superforge/infra/0.3.0`), which Infra logs and keeps per key
54
+ * made it (`@superforge/infra/<version>`), which Infra logs and keeps per key
35
55
  */
36
56
  export const CLIENT_HEADER = "Infra-Client";
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";
5
- export type { AgentToken, Infra, InfraUnavailable } from "./infra.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 LoginChannel, type LoginProof, type LoginStart, OAUTH_CALLS, type OAuthCall, type StartedLogin, type StartRefused, type VerifiedLogin, type VerifyRefused } from "./contract/index.js";
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";