@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/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, LoginProof, LoginStart, 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 = {
@@ -60,7 +60,16 @@ export type Infra = {
60
60
  checkAgentToken(token: string, expected: {
61
61
  origin: string;
62
62
  resource: string;
63
- }): AsyncOperationResult<AgentToken, "invalid-token" | InfraUnavailable>;
63
+ }): AsyncOperationResult<AgentToken, AgentTokenRefused | InfraUnavailable>;
64
+ };
65
+ /**
66
+ * A token Claude brought that the app mustn't take, and why in words:
67
+ * it ran out, it's for another address or app, or Infra didn't sign it.
68
+ * The app answers it 401, so Claude logs in again.
69
+ */
70
+ export type AgentTokenRefused = {
71
+ reason: "invalid-token";
72
+ message: string;
64
73
  };
65
74
  /** What the app learns from a token Claude brings, once it's checked */
66
75
  export type AgentToken = {
@@ -1,5 +1,10 @@
1
1
  /** A second, a minute, in milliseconds */
2
2
  export declare const SECOND = 1000;
3
3
  export declare const MINUTE: number;
4
+ /**
5
+ * How long a call waits for Infra's answer. Starting a login waits on
6
+ * Infra sending the email, which takes a second or two.
7
+ */
8
+ export declare const CALL_TIMEOUT: number;
4
9
  /** 32 random bytes in base64url: a login link's token, as Infra makes one */
5
10
  export declare function randomToken(): string;
@@ -3,6 +3,11 @@
3
3
  /** A second, a minute, in milliseconds */
4
4
  export const SECOND = 1000;
5
5
  export const MINUTE = 60 * SECOND;
6
+ /**
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
+ */
10
+ export const CALL_TIMEOUT = 15 * SECOND;
6
11
  /** 32 random bytes in base64url: a login link's token, as Infra makes one */
7
12
  export function randomToken() {
8
13
  const bytes = crypto.getRandomValues(new Uint8Array(32));
@@ -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 = {
@@ -19,13 +19,21 @@ export type WaitingLogin = {
19
19
  * it's given, an app's website's. An app's tests always use it, so they
20
20
  * never reach Infra, and so does a dev server with no test key
21
21
  * (choose.ts). A build never does: anyone could log in as anyone, since
22
- * the page shows the code. Its refusals say what Infra's say.
23
- *
24
- * `waiting` is where it keeps what it sent, by address: its own, or one
25
- * that outlives a dev server's reloads. With `logs`, as a dev server has
26
- * it, each comment is logged, since nothing keeps it.
22
+ * the page shows the code. Its refusals have Infra's reasons, though not
23
+ * always its words, which aren't contract. Like Infra, it links only to
24
+ * this computer and the project's `domains`, over https, and answers
25
+ * `with_link: false` for any other host.
27
26
  */
28
- export declare function standInInfra({ waiting, logs, domains }?: {
27
+ export declare function standInInfra({ domains }?: {
28
+ domains?: string[];
29
+ }): Infra;
30
+ /**
31
+ * The stand-in as the package itself makes it, a dev server's in
32
+ * choose.ts: `waiting` is where it keeps what it sent, by address, its own
33
+ * or one that outlives a dev server's reloads. With `logs`, as a dev
34
+ * server has it, each comment is logged, since nothing keeps it.
35
+ */
36
+ export declare function standInWith({ waiting, logs, domains }?: {
29
37
  waiting?: Map<string, WaitingLogin>;
30
38
  logs?: boolean;
31
39
  domains?: string[];
@@ -1,7 +1,7 @@
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, login_start, login_verify } from "./contract/index.js";
4
+ import { COMMENT_BODY_LIMIT, filed_comment, isLoopbackHost, 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
7
  // new one for the same address replaces it, link and all
@@ -18,13 +18,21 @@ const ATTEMPTS = 5;
18
18
  * it's given, an app's website's. An app's tests always use it, so they
19
19
  * never reach Infra, and so does a dev server with no test key
20
20
  * (choose.ts). A build never does: anyone could log in as anyone, since
21
- * the page shows the code. Its refusals say what Infra's say.
22
- *
23
- * `waiting` is where it keeps what it sent, by address: its own, or one
24
- * that outlives a dev server's reloads. With `logs`, as a dev server has
25
- * it, each comment is logged, since nothing keeps it.
21
+ * the page shows the code. Its refusals have Infra's reasons, though not
22
+ * always its words, which aren't contract. Like Infra, it links only to
23
+ * this computer and the project's `domains`, over https, and answers
24
+ * `with_link: false` for any other host.
26
25
  */
27
- export function standInInfra({ waiting = new Map(), logs = false, domains = [] } = {}) {
26
+ export function standInInfra({ domains = [] } = {}) {
27
+ return standInWith({ domains });
28
+ }
29
+ /**
30
+ * The stand-in as the package itself makes it, a dev server's in
31
+ * choose.ts: `waiting` is where it keeps what it sent, by address, its own
32
+ * or one that outlives a dev server's reloads. With `logs`, as a dev
33
+ * server has it, each comment is logged, since nothing keeps it.
34
+ */
35
+ export function standInWith({ waiting = new Map(), logs = false, domains = [] } = {}) {
28
36
  return {
29
37
  async project() {
30
38
  return {
@@ -45,6 +53,7 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
45
53
  return misfit(parsed.error);
46
54
  }
47
55
  const { channel, address, link_to } = parsed.data;
56
+ const page = link_to === undefined ? null : linkPage(link_to, domains);
48
57
  const sent = {
49
58
  address,
50
59
  code: randomCode(),
@@ -58,13 +67,11 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
58
67
  data: {
59
68
  channel,
60
69
  address,
61
- with_link: link_to !== undefined,
70
+ with_link: page !== null,
62
71
  expires_at: new Date(sent.expires_at),
63
72
  test: {
64
73
  code: sent.code,
65
- link: link_to === undefined
66
- ? null
67
- : `${link_to}#${sent.token}`
74
+ link: page === null ? null : `${page}#${sent.token}`
68
75
  }
69
76
  }
70
77
  };
@@ -196,6 +203,19 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
196
203
  }
197
204
  };
198
205
  }
206
+ // The page a login link opens, by Infra's rule: as it is on this computer,
207
+ // over https on its hostname when that's one of the project's domains,
208
+ // whatever scheme or port the app said, and null on any other host, which
209
+ // gets the code alone
210
+ function linkPage(link_to, domains) {
211
+ const url = new URL(link_to);
212
+ const page = `${url.pathname}${url.search}`;
213
+ if (isLoopbackHost(url.hostname)) {
214
+ return `${url.origin}${page}`;
215
+ }
216
+ const domain = domains.find((each) => each.toLowerCase() === url.hostname.toLowerCase());
217
+ return domain === undefined ? null : `https://${url.hostname}${page}`;
218
+ }
199
219
  // A request that doesn't fit Infra's contract: Infra answers it 400
200
220
  // `invalid`, which the client reads as unavailable, since it's a bug in
201
221
  // the app, with the fields that didn't fit
@@ -1,4 +1,4 @@
1
1
  /** The package's version, as package.json says it */
2
- export declare const PACKAGE_VERSION = "0.3.0";
3
- /** What it says: the package and its version, `@superforge/infra/0.3.0` */
4
- export declare const CLIENT = "@superforge/infra/0.3.0";
2
+ export declare const PACKAGE_VERSION = "0.5.0";
3
+ /** What it says: the package and its version, `@superforge/infra/<version>` */
4
+ export declare const CLIENT = "@superforge/infra/0.5.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.3.0";
9
- /** What it says: the package and its version, `@superforge/infra/0.3.0` */
8
+ export const PACKAGE_VERSION = "0.5.0";
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.3.0",
3
+ "version": "0.5.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",
@@ -12,6 +12,8 @@
12
12
  "engines": {
13
13
  "node": ">=22"
14
14
  },
15
+ "main": "./output/index.js",
16
+ "types": "./output/index.d.ts",
15
17
  "exports": {
16
18
  ".": {
17
19
  "types": "./output/index.d.ts",
@@ -23,8 +25,7 @@
23
25
  }
24
26
  },
25
27
  "files": [
26
- "output",
27
- "!output/tests"
28
+ "output"
28
29
  ],
29
30
  "sideEffects": false,
30
31
  "scripts": {
@@ -36,17 +37,14 @@
36
37
  "prepublishOnly": "pnpm check"
37
38
  },
38
39
  "dependencies": {
39
- "jose": "6.2.15"
40
- },
41
- "peerDependencies": {
42
- "zod": "^4.6.7"
40
+ "jose": "^6.2.15",
41
+ "zod": "^4.0.0"
43
42
  },
44
43
  "devDependencies": {
45
44
  "@biomejs/biome": "^2.6.2",
46
45
  "@types/node": "^22.10.2",
47
46
  "hono": "^4.13.13",
48
47
  "typescript": "^6.0.2",
49
- "vitest": "^5.0.5",
50
- "zod": "^4.6.7"
48
+ "vitest": "^5.0.5"
51
49
  }
52
50
  }