@halonicjs/testing 0.1.0 → 0.1.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.
Files changed (153) hide show
  1. package/NOTICE +4 -0
  2. package/README.md +1 -1
  3. package/dist/adapters/http-kit/src/address-checking.d.ts +19 -0
  4. package/dist/adapters/http-kit/src/address-checking.js +1 -0
  5. package/dist/adapters/http-kit/src/index.d.ts +1 -0
  6. package/dist/adapters/integration-kit/src/auth-placement.d.ts +40 -0
  7. package/dist/adapters/integration-kit/src/auth-placement.js +102 -0
  8. package/dist/adapters/integration-kit/src/current-secret-forms.d.ts +28 -0
  9. package/dist/adapters/integration-kit/src/current-secret-forms.js +60 -0
  10. package/dist/adapters/integration-kit/src/declaration.d.ts +2 -1
  11. package/dist/adapters/integration-kit/src/declaration.js +4 -3
  12. package/dist/adapters/integration-kit/src/host.d.ts +7 -1
  13. package/dist/adapters/integration-kit/src/host.js +1 -0
  14. package/dist/adapters/integration-kit/src/index.d.ts +3 -1
  15. package/dist/adapters/integration-kit/src/index.js +1 -0
  16. package/dist/adapters/integration-kit/src/leak-attempt.d.ts +4 -2
  17. package/dist/adapters/integration-kit/src/leak-attempt.js +5 -2
  18. package/dist/adapters/integration-kit/src/limited-http.d.ts +7 -1
  19. package/dist/adapters/integration-kit/src/limited-http.js +53 -10
  20. package/dist/adapters/integration-kit/src/loader.d.ts +29 -2
  21. package/dist/adapters/integration-kit/src/loader.js +44 -12
  22. package/dist/adapters/integration-kit/src/own-request.d.ts +2 -2
  23. package/dist/adapters/integration-kit/src/own-request.js +1 -1
  24. package/dist/adapters/integration-kit/src/plugin-load.d.ts +3 -3
  25. package/dist/adapters/integration-kit/src/plugin-load.js +5 -5
  26. package/dist/adapters/integration-kit/src/policy.d.ts +28 -0
  27. package/dist/adapters/integration-kit/src/response-body.d.ts +6 -2
  28. package/dist/adapters/integration-kit/src/secret-filter.d.ts +3 -3
  29. package/dist/adapters/integration-kit/src/secret-filter.js +19 -4
  30. package/dist/adapters/integration-kit/src/secret-in-request.d.ts +3 -0
  31. package/dist/adapters/integration-kit/src/secret-in-request.js +8 -2
  32. package/dist/adapters/integration-kit/src/secret-placement.d.ts +35 -25
  33. package/dist/adapters/integration-kit/src/secret-placement.js +65 -95
  34. package/dist/adapters/integration-kit/src/sign-in-placement.d.ts +29 -0
  35. package/dist/adapters/integration-kit/src/sign-in-placement.js +92 -0
  36. package/dist/core/events/src/blocked-sites.d.ts +57 -0
  37. package/dist/core/events/src/blocked-sites.js +89 -0
  38. package/dist/core/events/src/index.d.ts +5 -1
  39. package/dist/core/events/src/index.js +3 -0
  40. package/dist/core/events/src/run-basics.d.ts +30 -7
  41. package/dist/core/events/src/run-event.d.ts +49 -5
  42. package/dist/core/events/src/run-reads.d.ts +25 -0
  43. package/dist/core/events/src/run-reads.js +44 -0
  44. package/dist/core/events/src/run-sends.d.ts +47 -0
  45. package/dist/core/events/src/run-sends.js +68 -0
  46. package/dist/core/events/src/summarize.js +5 -1
  47. package/dist/core/events/src/timeline-entry.js +1 -0
  48. package/dist/core/format/src/index.d.ts +7 -5
  49. package/dist/core/format/src/index.js +5 -3
  50. package/dist/core/format/src/integration/index.d.ts +3 -2
  51. package/dist/core/format/src/integration/index.js +2 -1
  52. package/dist/core/format/src/kinds/agent/sandbox-fields.d.ts +15 -5
  53. package/dist/core/format/src/kinds/agent/sandbox-fields.js +29 -10
  54. package/dist/core/format/src/kinds/agent/schema.js +2 -2
  55. package/dist/core/format/src/kinds/connector/is-local.js +1 -1
  56. package/dist/core/format/src/kinds/connector/kind.js +99 -0
  57. package/dist/core/format/src/kinds/connector/schema.d.ts +24 -0
  58. package/dist/core/format/src/kinds/connector/schema.js +53 -2
  59. package/dist/core/format/src/kinds/deployments/kind.js +25 -1
  60. package/dist/core/format/src/kinds/deployments/schema.d.ts +40 -0
  61. package/dist/core/format/src/kinds/deployments/schema.js +52 -0
  62. package/dist/core/format/src/kinds/integration/approved-sign-ins.d.ts +62 -0
  63. package/dist/core/format/src/kinds/integration/approved-sign-ins.js +125 -0
  64. package/dist/core/format/src/kinds/integration/host-key.d.ts +7 -0
  65. package/dist/core/format/src/kinds/integration/host-key.js +8 -0
  66. package/dist/core/format/src/kinds/integration/kind.d.ts +4 -2
  67. package/dist/core/format/src/kinds/integration/kind.js +49 -12
  68. package/dist/core/format/src/kinds/integration/policy.d.ts +47 -2
  69. package/dist/core/format/src/kinds/integration/policy.js +61 -2
  70. package/dist/core/format/src/kinds/integration/schema.d.ts +180 -3
  71. package/dist/core/format/src/kinds/integration/schema.js +132 -6
  72. package/dist/core/format/src/kinds/integration/sign-in-checks.d.ts +13 -0
  73. package/dist/core/format/src/kinds/integration/sign-in-checks.js +120 -0
  74. package/dist/core/format/src/kinds/integration/sign-in-urls.d.ts +19 -0
  75. package/dist/core/format/src/kinds/integration/sign-in-urls.js +47 -0
  76. package/dist/core/format/src/kinds/plugin/checks.js +7 -1
  77. package/dist/core/format/src/kinds/plugin/schema.d.ts +130 -0
  78. package/dist/core/format/src/kinds/plugin/schema.js +2 -2
  79. package/dist/core/format/src/kinds/plugin/ui-checks.d.ts +1 -1
  80. package/dist/core/format/src/kinds/plugin/ui-checks.js +1 -10
  81. package/dist/core/format/src/kinds/routine/kind.js +38 -1
  82. package/dist/core/format/src/kinds/routine/schema.d.ts +10 -0
  83. package/dist/core/format/src/kinds/routine/schema.js +23 -1
  84. package/dist/core/format/src/kinds/routine/webhook-headers.d.ts +9 -0
  85. package/dist/core/format/src/kinds/routine/webhook-headers.js +26 -0
  86. package/dist/core/format/src/kinds/workflow/schema.js +1 -1
  87. package/dist/core/format/src/kinds/workspace/kind.js +1 -1
  88. package/dist/core/format/src/kinds/workspace/schema.d.ts +2 -2
  89. package/dist/core/format/src/kinds/workspace/schema.js +1 -1
  90. package/dist/core/format/src/shared/fields.d.ts +3 -6
  91. package/dist/core/format/src/shared/fields.js +33 -13
  92. package/dist/core/ports/src/agent-runs-not-available.d.ts +21 -0
  93. package/dist/core/ports/src/agent-runs-not-available.js +25 -0
  94. package/dist/core/ports/src/blob-store.d.ts +12 -0
  95. package/dist/core/ports/src/blob-store.js +22 -0
  96. package/dist/core/ports/src/connector-client.d.ts +48 -4
  97. package/dist/core/ports/src/connector-client.js +17 -0
  98. package/dist/core/ports/src/definition-store.d.ts +23 -2
  99. package/dist/core/ports/src/harness-bridge-server.d.ts +12 -3
  100. package/dist/core/ports/src/harness-sign-in-command.d.ts +27 -0
  101. package/dist/core/ports/src/harness-sign-in-command.js +31 -0
  102. package/dist/core/ports/src/harness.d.ts +46 -13
  103. package/dist/core/ports/src/harness.js +1 -1
  104. package/dist/core/ports/src/host-settings-store.d.ts +116 -0
  105. package/dist/core/ports/src/host-settings-store.js +106 -0
  106. package/dist/core/ports/src/index.d.ts +18 -10
  107. package/dist/core/ports/src/index.js +15 -7
  108. package/dist/core/ports/src/instruction-trust-store.d.ts +81 -0
  109. package/dist/core/ports/src/instruction-trust-store.js +43 -0
  110. package/dist/core/ports/src/integration.d.ts +71 -7
  111. package/dist/core/ports/src/integration.js +22 -4
  112. package/dist/core/ports/src/llm-provider.d.ts +3 -1
  113. package/dist/core/ports/src/llm-provider.js +3 -0
  114. package/dist/core/ports/src/password-hasher.d.ts +36 -20
  115. package/dist/core/ports/src/port-versions.d.ts +7 -4
  116. package/dist/core/ports/src/port-versions.js +6 -0
  117. package/dist/core/ports/src/program-probe.d.ts +25 -2
  118. package/dist/core/ports/src/program-probe.js +1 -1
  119. package/dist/core/ports/src/provider-default-base-url.d.ts +10 -0
  120. package/dist/core/ports/src/provider-default-base-url.js +18 -0
  121. package/dist/core/ports/src/record-store.d.ts +17 -0
  122. package/dist/core/ports/src/record-store.js +70 -0
  123. package/dist/core/ports/src/run-executor.d.ts +5 -4
  124. package/dist/core/ports/src/run-target-not-found.d.ts +34 -0
  125. package/dist/core/ports/src/run-target-not-found.js +41 -0
  126. package/dist/core/ports/src/sandbox-setting-store.d.ts +68 -0
  127. package/dist/core/ports/src/sandbox-setting-store.js +33 -0
  128. package/dist/core/ports/src/sandbox.d.ts +130 -13
  129. package/dist/core/ports/src/sandbox.js +38 -2
  130. package/dist/core/ports/src/secrets.d.ts +73 -0
  131. package/dist/core/ports/src/secrets.js +56 -0
  132. package/dist/core/ports/src/tool-approval-store.d.ts +50 -8
  133. package/dist/core/ports/src/tool-approval-store.js +59 -0
  134. package/dist/core/ports/src/vcs-status.d.ts +8 -0
  135. package/dist/core/ports/src/vcs-status.js +90 -0
  136. package/dist/core/std/src/amount.d.ts +13 -0
  137. package/dist/core/std/src/amount.js +22 -0
  138. package/dist/core/std/src/hidden-characters.d.ts +19 -0
  139. package/dist/core/std/src/hidden-characters.js +54 -0
  140. package/dist/core/std/src/index.d.ts +5 -0
  141. package/dist/core/std/src/index.js +5 -0
  142. package/dist/core/std/src/public-suffixes.generated.d.ts +9 -0
  143. package/dist/core/std/src/public-suffixes.generated.js +10429 -0
  144. package/dist/core/std/src/registrable-domain.d.ts +10 -0
  145. package/dist/core/std/src/registrable-domain.js +62 -0
  146. package/dist/core/std/src/secret-variable-name.d.ts +12 -0
  147. package/dist/core/std/src/secret-variable-name.js +34 -0
  148. package/dist/core/std/src/site-name.d.ts +35 -0
  149. package/dist/core/std/src/site-name.js +111 -0
  150. package/dist/core/std/src/url-host.d.ts +35 -0
  151. package/dist/core/std/src/url-host.js +170 -0
  152. package/dist/testing/integration/src/load.js +3 -1
  153. package/package.json +2 -2
@@ -1,5 +1,53 @@
1
1
  import { s } from "../../../../schema-kit/src/index.js";
2
2
  import { deploymentId, version } from "../../shared/fields.js";
3
+ /**
4
+ * The most characters a Worker name may have: it becomes the first label of
5
+ * `<worker>.<account>.workers.dev`, and a DNS label holds at most 63.
6
+ */
7
+ const WORKER_NAME_MAX_LENGTH = 63;
8
+ /** The most characters a host name may have in DNS. */
9
+ const HOST_NAME_MAX_LENGTH = 253;
10
+ /**
11
+ * A host name: lowercase labels of letters, digits and inner `-`, joined by
12
+ * dots, with a top-level label that isn't all digits. The length cap is a
13
+ * separate `maxLength`, not a lookahead, so the JSON Schema's pattern also
14
+ * works in Go and Rust regex libraries, which have no lookaround.
15
+ */
16
+ const HOST_NAME_PATTERN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
17
+ /**
18
+ * Non-secret settings for a deployment with `host: cloudflare` (ADR HAL-35,
19
+ * 5.7). They carry no account id, token or key, so the file stays safe to
20
+ * commit and the same file deploys from any account.
21
+ */
22
+ export const CloudflareSettings = s
23
+ .object({
24
+ /** The Worker's name; it serves at `<worker>.<account>.workers.dev`. */
25
+ worker: s
26
+ .string()
27
+ .min(1)
28
+ .max(WORKER_NAME_MAX_LENGTH, `use at most ${WORKER_NAME_MAX_LENGTH} characters, so the name fits in a workers.dev address`)
29
+ .regex(/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/, "use lowercase letters, digits or '-', not at the start or end"),
30
+ /** A custom domain the Worker also serves at, such as `halonic.example.com`. */
31
+ domain: s
32
+ .string()
33
+ .max(HOST_NAME_MAX_LENGTH, `use at most ${HOST_NAME_MAX_LENGTH} characters`)
34
+ .regex(HOST_NAME_PATTERN, "use a lowercase host name such as halonic.example.com")
35
+ .optional(),
36
+ /**
37
+ * Where the deployment keeps its data. `eu` keeps D1, R2 and Durable
38
+ * Objects in the EU jurisdiction. Cloudflare fixes it when the data is
39
+ * first created, so changing it later needs an export and an import.
40
+ */
41
+ location: s.enum(["eu"]).optional(),
42
+ })
43
+ .strict();
44
+ /** Ceilings the deployment's handler enforces, because Cloudflare has no hard spending cap. */
45
+ export const DeploymentLimits = s
46
+ .object({
47
+ /** The most requests the deployment answers per UTC day; beyond it, it refuses them. */
48
+ dailyRequests: s.number().int().positive().optional(),
49
+ })
50
+ .strict();
3
51
  /** One place the workspace runs, with its own data (runs, Inbox, records, simulations). */
4
52
  export const Deployment = s
5
53
  .object({
@@ -10,6 +58,10 @@ export const Deployment = s
10
58
  stage: s.enum(["development", "production"]).default("development"),
11
59
  /** Every write a connector would make is only recorded, never done. */
12
60
  shadow: s.boolean().default(false),
61
+ /** Settings used when `host` is `cloudflare`. */
62
+ cloudflare: CloudflareSettings.optional(),
63
+ /** Ceilings on what the deployment answers, such as requests per day. */
64
+ limits: DeploymentLimits.optional(),
13
65
  })
14
66
  .strict();
15
67
  /** `deployments.yaml`: the places the workspace runs. */
@@ -0,0 +1,62 @@
1
+ import type { Path } from "../../../../schema-kit/src/index.js";
2
+ import { type Result } from "../../../../std/src/index.js";
3
+ import { type PolicyProblem } from "./policy.js";
4
+ import { type IntegrationSheet, type IntegrationSignIn } from "./schema.js";
5
+ /**
6
+ * A sign-in with every setting filled in, as the trust check shows it and as
7
+ * Halonic saves it with the grant. It has the shape of `ApprovedSignIn` in
8
+ * `core/oauth-sign-in`, which this package can't import; the host passes it
9
+ * on as it is. It holds no secret: `clientSecretName` is a name.
10
+ */
11
+ export interface ResolvedSignIn {
12
+ readonly flow: "oauth-code";
13
+ readonly authorizeUrl: string;
14
+ readonly tokenUrl: string;
15
+ readonly revokeUrl?: string;
16
+ /** Only ever `true`: a sign-in that doesn't say so is left without the key. */
17
+ readonly revokeEndsClientGrants?: true;
18
+ readonly issuer?: string;
19
+ readonly clientId: string;
20
+ readonly clientAuth: "post" | "basic";
21
+ readonly clientSecretName?: string;
22
+ readonly scopes: readonly string[];
23
+ readonly accessHosts: readonly string[];
24
+ readonly as: "bearer" | "header";
25
+ readonly header?: string;
26
+ readonly prefix?: string;
27
+ }
28
+ /** Why a sign-in's client ID can't be resolved. */
29
+ export type ClientIdProblem = PolicyProblem & {
30
+ readonly code: "client-id-missing" | "client-id-invalid";
31
+ };
32
+ /** What {@link resolveSignIn} needs from its caller, who already checked the hosts. */
33
+ export interface SignInResolution {
34
+ readonly sheetId: string;
35
+ readonly settings: Readonly<Record<string, unknown>>;
36
+ /** The host a `{ fromSetting }` URL reads, `host[:port]`; undefined when the setting is a problem, which it reports. */
37
+ readonly hostOfSetting: (setting: string, path: Path) => string | undefined;
38
+ /** The hosts the access token may go to, already resolved. */
39
+ readonly accessHosts: readonly string[];
40
+ readonly path: Path;
41
+ readonly report: (problem: ClientIdProblem) => void;
42
+ }
43
+ /**
44
+ * Fills in one sign-in: its URLs (a `{ fromSetting, path }` URL becomes
45
+ * `https://host/path`), its client ID, and its scopes as plain words.
46
+ *
47
+ * @returns the resolved sign-in, or undefined when a setting it reads is
48
+ * missing or invalid; the reason is then passed to `report`
49
+ *
50
+ * @example
51
+ * resolveSignIn(signIn, resolution); // { tokenUrl: "https://git.example.com/oauth/token", … }
52
+ */
53
+ export declare function resolveSignIn(signIn: IntegrationSignIn, resolution: SignInResolution): ResolvedSignIn | undefined;
54
+ /**
55
+ * Each sign-in of the sheet with its settings filled in, by name: what the
56
+ * user approves. Apart from `integrationPolicy`, which the kit can use
57
+ * before the client ID setting is set.
58
+ *
59
+ * @example
60
+ * approvedSignIns(sheet, { clientId: "abc123" }); // ok: { "example-account": { … } }
61
+ */
62
+ export declare function approvedSignIns(sheet: IntegrationSheet, settings: Readonly<Record<string, unknown>>): Result<Record<string, ResolvedSignIn>, PolicyProblem[]>;
@@ -0,0 +1,125 @@
1
+ import { err, ok } from "../../../../std/src/index.js";
2
+ import { resolveHost, resolveSignIns, settingClasses } from "./policy.js";
3
+ import { CLIENT_ID_PATTERN, } from "./schema.js";
4
+ /**
5
+ * Fills in one sign-in: its URLs (a `{ fromSetting, path }` URL becomes
6
+ * `https://host/path`), its client ID, and its scopes as plain words.
7
+ *
8
+ * @returns the resolved sign-in, or undefined when a setting it reads is
9
+ * missing or invalid; the reason is then passed to `report`
10
+ *
11
+ * @example
12
+ * resolveSignIn(signIn, resolution); // { tokenUrl: "https://git.example.com/oauth/token", … }
13
+ */
14
+ export function resolveSignIn(signIn, resolution) {
15
+ const urlOf = (url, field) => {
16
+ if (typeof url === "string") {
17
+ return url;
18
+ }
19
+ const host = resolution.hostOfSetting(url.fromSetting, [...resolution.path, field]);
20
+ return host === undefined ? undefined : `https://${host}${url.path ?? ""}`;
21
+ };
22
+ const authorizeUrl = urlOf(signIn.authorizeUrl, "authorizeUrl");
23
+ const tokenUrl = urlOf(signIn.tokenUrl, "tokenUrl");
24
+ const revokeUrl = signIn.revokeUrl === undefined ? undefined : urlOf(signIn.revokeUrl, "revokeUrl");
25
+ const issuer = signIn.issuer === undefined ? undefined : urlOf(signIn.issuer, "issuer");
26
+ const clientId = clientIdOf(signIn, resolution);
27
+ const complete = authorizeUrl !== undefined &&
28
+ tokenUrl !== undefined &&
29
+ clientId !== undefined &&
30
+ (signIn.revokeUrl === undefined || revokeUrl !== undefined) &&
31
+ (signIn.issuer === undefined || issuer !== undefined);
32
+ if (!complete) {
33
+ return undefined;
34
+ }
35
+ return {
36
+ flow: signIn.flow,
37
+ authorizeUrl,
38
+ tokenUrl,
39
+ ...(revokeUrl === undefined ? {} : { revokeUrl }),
40
+ ...(signIn.revokeEndsClientGrants === true ? { revokeEndsClientGrants: true } : {}),
41
+ ...(issuer === undefined ? {} : { issuer }),
42
+ clientId,
43
+ clientAuth: signIn.clientAuth,
44
+ ...(signIn.clientSecret === undefined ? {} : { clientSecretName: signIn.clientSecret }),
45
+ scopes: signIn.scopes.map((entry) => entry.scope),
46
+ accessHosts: resolution.accessHosts,
47
+ as: signIn.access.as,
48
+ ...(signIn.access.as === "header"
49
+ ? {
50
+ header: signIn.access.header,
51
+ ...(signIn.access.prefix === undefined ? {} : { prefix: signIn.access.prefix }),
52
+ }
53
+ : {}),
54
+ };
55
+ }
56
+ function clientIdOf(signIn, resolution) {
57
+ if (typeof signIn.clientId === "string") {
58
+ return signIn.clientId;
59
+ }
60
+ const { fromSetting } = signIn.clientId;
61
+ const path = [...resolution.path, "clientId"];
62
+ const value = Object.hasOwn(resolution.settings, fromSetting)
63
+ ? resolution.settings[fromSetting]
64
+ : undefined;
65
+ if (typeof value !== "string" || value === "") {
66
+ resolution.report({
67
+ code: "client-id-missing",
68
+ path,
69
+ message: `${resolution.sheetId}: the setting "${fromSetting}" must hold the sign-in's client ID`,
70
+ });
71
+ return undefined;
72
+ }
73
+ if (!CLIENT_ID_PATTERN.test(value)) {
74
+ resolution.report({
75
+ code: "client-id-invalid",
76
+ path,
77
+ message: `${resolution.sheetId}: the setting "${fromSetting}" must hold a client ID without spaces or line breaks`,
78
+ });
79
+ return undefined;
80
+ }
81
+ return value;
82
+ }
83
+ /**
84
+ * Fills in each sign-in of a sheet from the integration's settings.
85
+ *
86
+ * @param declared - what `integrationPolicy` resolved, for the access hosts
87
+ * @param report - called with each client ID problem
88
+ * @param hostOfSetting - the host a `{ fromSetting }` URL reads, or undefined
89
+ * when the setting is a problem, which it has already reported
90
+ */
91
+ function approveEach(sheet, settings, declared, report, hostOfSetting) {
92
+ const approved = {};
93
+ for (const [name, signIn] of Object.entries(sheet.signIns)) {
94
+ const resolved = resolveSignIn(signIn, {
95
+ sheetId: sheet.id,
96
+ settings,
97
+ path: ["signIns", name],
98
+ accessHosts: declared[name]?.hosts ?? [],
99
+ hostOfSetting,
100
+ report,
101
+ });
102
+ if (resolved !== undefined) {
103
+ approved[name] = resolved;
104
+ }
105
+ }
106
+ return approved;
107
+ }
108
+ /**
109
+ * Each sign-in of the sheet with its settings filled in, by name: what the
110
+ * user approves. Apart from `integrationPolicy`, which the kit can use
111
+ * before the client ID setting is set.
112
+ *
113
+ * @example
114
+ * approvedSignIns(sheet, { clientId: "abc123" }); // ok: { "example-account": { … } }
115
+ */
116
+ export function approvedSignIns(sheet, settings) {
117
+ const problems = [];
118
+ const classes = settingClasses(sheet);
119
+ const declared = resolveSignIns(sheet, classes, settings, problems);
120
+ const approved = approveEach(sheet, settings, declared, (problem) => problems.push(problem),
121
+ // The sheet's checks keep a sign-in's address setting in class `public`,
122
+ // and `resolveHost` refuses plain http there, so the host is always https.
123
+ (setting, path) => resolveHost(sheet, { fromSetting: setting }, classes, settings, path, problems)?.host);
124
+ return problems.length > 0 ? err(problems) : ok(approved);
125
+ }
@@ -0,0 +1,7 @@
1
+ import type { IntegrationHost, IntegrationSecretHost } from "./schema.js";
2
+ /**
3
+ * A host as one text, to compare: `api.example.com`, or `fromSetting url`.
4
+ * A secret or a sign-in names a `{ fromSetting }` host by its setting alone,
5
+ * so the class isn't part of it.
6
+ */
7
+ export declare function hostKey(host: IntegrationHost | IntegrationSecretHost): string;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A host as one text, to compare: `api.example.com`, or `fromSetting url`.
3
+ * A secret or a sign-in names a `{ fromSetting }` host by its setting alone,
4
+ * so the class isn't part of it.
5
+ */
6
+ export function hostKey(host) {
7
+ return typeof host === "string" ? host : `fromSetting ${host.fromSetting}`;
8
+ }
@@ -7,8 +7,10 @@ import { IntegrationSheet } from "./schema.js";
7
7
  export declare const integrationKind: FileKind<IntegrationSheet>;
8
8
  /**
9
9
  * The rules over several fields: no list names a thing twice, a secret goes
10
- * only to hosts the sheet declares, a send tool has a noun, and the Google
11
- * fields fit the sheet.
10
+ * only to hosts the sheet declares, the sign-ins fit the sheet and its `kit`
11
+ * (`checkSignIns`), a send tool has a noun, a verb comes with a noun, a send
12
+ * tool never says "drafted", and
13
+ * the Google fields fit the sheet.
12
14
  *
13
15
  * @example
14
16
  * checkIntegrationSheet(sheet); // [] for the weather example in the guide
@@ -1,4 +1,6 @@
1
- import { GOOGLE_CHECK_URL_PATTERN, GOOGLE_SECRET, IntegrationSheet, } from "./schema.js";
1
+ import { hostKey } from "./host-key.js";
2
+ import { GOOGLE_CHECK_URL_PATTERN, GOOGLE_SECRET, IntegrationSheet, isPlacedSecret, } from "./schema.js";
3
+ import { checkSignIns } from "./sign-in-checks.js";
2
4
  /**
3
5
  * `integration.yaml`, next to an integration's code. It isn't a workspace
4
6
  * file, so it has no place; the loader for integrations reads it.
@@ -14,8 +16,10 @@ export const integrationKind = {
14
16
  };
15
17
  /**
16
18
  * The rules over several fields: no list names a thing twice, a secret goes
17
- * only to hosts the sheet declares, a send tool has a noun, and the Google
18
- * fields fit the sheet.
19
+ * only to hosts the sheet declares, the sign-ins fit the sheet and its `kit`
20
+ * (`checkSignIns`), a send tool has a noun, a verb comes with a noun, a send
21
+ * tool never says "drafted", and
22
+ * the Google fields fit the sheet.
19
23
  *
20
24
  * @example
21
25
  * checkIntegrationSheet(sheet); // [] for the weather example in the guide
@@ -49,6 +53,9 @@ export function checkIntegrationSheet(sheet) {
49
53
  duplicates(recording?.allowContentTypes ?? [], ["recording", "allowContentTypes"], "type");
50
54
  const declared = new Set(sheet.hosts.map(hostKey));
51
55
  for (const [name, secret] of Object.entries(sheet.secrets)) {
56
+ if (!isPlacedSecret(secret)) {
57
+ continue; // a client secret has no hosts of its own; `checkSignIns` checks it
58
+ }
52
59
  duplicates(secret.hosts.map(hostKey), ["secrets", name, "hosts"], "host");
53
60
  secret.hosts.forEach((host, index) => {
54
61
  if (!declared.has(hostKey(host))) {
@@ -61,7 +68,10 @@ export function checkIntegrationSheet(sheet) {
61
68
  }
62
69
  });
63
70
  }
71
+ problems.push(...checkSignIns(sheet));
64
72
  problems.push(...checkSendNouns(sheet));
73
+ problems.push(...checkToolVerbs(sheet));
74
+ problems.push(...checkDraftedVerbs(sheet));
65
75
  problems.push(...checkGoogleFields(sheet));
66
76
  return problems;
67
77
  }
@@ -81,6 +91,39 @@ function checkSendNouns(sheet) {
81
91
  ]
82
92
  : []);
83
93
  }
94
+ /**
95
+ * A `verb` says what a call did to its noun ("Saved 1 Drive file"), so on its
96
+ * own it says nothing: a tool with a verb needs a noun too.
97
+ */
98
+ function checkToolVerbs(sheet) {
99
+ return sheet.tools.flatMap((tool, index) => tool.verb !== undefined && tool.noun === undefined
100
+ ? [
101
+ {
102
+ severity: "error",
103
+ code: "verb-without-noun",
104
+ path: ["tools", index, "verb"],
105
+ message: `Integration "${sheet.id}": tool "${tool.name}" has verb ${tool.verb} but no noun, so a run couldn't say what was ${tool.verb}; add one, such as noun: { one: file, many: files }`,
106
+ },
107
+ ]
108
+ : []);
109
+ }
110
+ /**
111
+ * `drafted` says nothing was sent, so only a `write` tool may use it. The list
112
+ * is closed so a send can't be worded as something milder, and "drafted" is
113
+ * milder than "sent".
114
+ */
115
+ function checkDraftedVerbs(sheet) {
116
+ return sheet.tools.flatMap((tool, index) => tool.verb === "drafted" && tool.risk !== "write"
117
+ ? [
118
+ {
119
+ severity: "error",
120
+ code: "drafted-on-send-tool",
121
+ path: ["tools", index, "verb"],
122
+ message: `Integration "${sheet.id}": tool "${tool.name}" has risk ${tool.risk}, so it may not say drafted: only a write tool prepares something without sending it`,
123
+ },
124
+ ]
125
+ : []);
126
+ }
84
127
  /** The sheet fields that only an integration using the Google sign-in may have. */
85
128
  const googleFields = ["googleScopes", "googleApis", "googleCheck"];
86
129
  /**
@@ -90,9 +133,11 @@ const googleFields = ["googleScopes", "googleApis", "googleCheck"];
90
133
  * The same rules as the `google-sheet` check in halonic-com/integrations.
91
134
  */
92
135
  function checkGoogleFields(sheet) {
93
- const secret = Object.hasOwn(sheet.secrets, GOOGLE_SECRET)
136
+ const entry = Object.hasOwn(sheet.secrets, GOOGLE_SECRET)
94
137
  ? sheet.secrets[GOOGLE_SECRET]
95
138
  : undefined;
139
+ // The Google secret is one the integration places; a client secret of that name has no token to send.
140
+ const secret = entry !== undefined && isPlacedSecret(entry) ? entry : undefined;
96
141
  if (secret === undefined) {
97
142
  return googleFields
98
143
  .filter((field) => sheet[field] !== undefined)
@@ -128,11 +173,3 @@ function checkGoogleFields(sheet) {
128
173
  }
129
174
  return problems;
130
175
  }
131
- /**
132
- * A host as one text, to compare: `api.example.com`, or `fromSetting url`.
133
- * A secret names a `{ fromSetting }` host by its setting alone, so the class
134
- * isn't part of it.
135
- */
136
- function hostKey(host) {
137
- return typeof host === "string" ? host : `fromSetting ${host.fromSetting}`;
138
- }
@@ -1,6 +1,6 @@
1
1
  import type { Path } from "../../../../schema-kit/src/index.js";
2
2
  import { type Result } from "../../../../std/src/index.js";
3
- import type { IntegrationSheet } from "./schema.js";
3
+ import { type AddressClass, type IntegrationHost, type IntegrationSecretHost, type IntegrationSheet } from "./schema.js";
4
4
  /**
5
5
  * What one integration may do, in the shape of the kit's `IntegrationPolicy`
6
6
  * (`adapters/integration-kit`). The core can't import an adapter, so the
@@ -14,6 +14,8 @@ export interface IntegrationPolicy {
14
14
  readonly localHosts?: readonly string[];
15
15
  /** Each secret, the hosts it may go to, whether a request may go without it, and where it goes. */
16
16
  readonly secrets: Readonly<Record<string, DeclaredSecret>>;
17
+ /** Each sign-in, and where its access token may go and how it is put in. */
18
+ readonly signIns: Readonly<Record<string, DeclaredSignIn>>;
17
19
  readonly tokens: readonly string[];
18
20
  }
19
21
  /** One secret as the kit takes it. */
@@ -34,6 +36,32 @@ export type DeclaredPlacement = {
34
36
  readonly as: "form";
35
37
  readonly field: string;
36
38
  };
39
+ /**
40
+ * One sign-in as the kit takes it: the hosts its access token may go to, and
41
+ * where the sheet puts it (`as`, and `header` and `prefix` for a header). The
42
+ * kit refuses any other placement.
43
+ */
44
+ export type DeclaredSignIn = DeclaredSignInPlacement & {
45
+ readonly hosts: readonly string[];
46
+ /** The client secret, when the sign-in has one. Only Halonic's sign-in code uses it. */
47
+ readonly clientSecret?: DeclaredClientSecret;
48
+ };
49
+ /** Where the sheet puts a sign-in's access token. There is no `form`. */
50
+ export type DeclaredSignInPlacement = {
51
+ readonly as: "bearer";
52
+ } | {
53
+ readonly as: "header";
54
+ readonly header: string;
55
+ readonly prefix?: string;
56
+ };
57
+ /**
58
+ * A sign-in's client secret. Its hosts are the sign-in's token and revoke
59
+ * hosts and nothing else, and the integration can't place it.
60
+ */
61
+ export interface DeclaredClientSecret {
62
+ readonly name: string;
63
+ readonly hosts: readonly string[];
64
+ }
37
65
  /** The kit's request limits, as the sheet gives them. */
38
66
  export type IntegrationLimits = NonNullable<IntegrationSheet["limits"]>;
39
67
  /** What the kit needs to build an integration's host. */
@@ -44,7 +72,7 @@ export interface ResolvedIntegration {
44
72
  }
45
73
  /** Why a `fromSetting` host can't be resolved. */
46
74
  export interface PolicyProblem {
47
- readonly code: "setting-missing" | "setting-not-a-url" | "setting-outside-class" | "plain-http-not-local" | "secret-over-plain-http";
75
+ readonly code: "setting-missing" | "setting-not-a-url" | "setting-outside-class" | "plain-http-not-local" | "secret-over-plain-http" | "client-id-missing" | "client-id-invalid";
48
76
  /** Where the host sits in the sheet. */
49
77
  readonly path: Path;
50
78
  readonly message: string;
@@ -65,3 +93,20 @@ export interface PolicyProblem {
65
93
  * if (resolved.ok) createIntegrationHost({ policy: resolved.value.policy, ...ports });
66
94
  */
67
95
  export declare function integrationPolicy(sheet: IntegrationSheet, settings: Readonly<Record<string, unknown>>): Result<ResolvedIntegration, PolicyProblem[]>;
96
+ /**
97
+ * Each sign-in's access hosts and placement, and its client secret with the
98
+ * hosts of the sign-in's token and revoke URLs. The other sign-in URLs read
99
+ * settings that `hosts` already resolves, so they need no check here.
100
+ */
101
+ export declare function resolveSignIns(sheet: IntegrationSheet, classes: ReadonlyMap<string, AddressClass>, settings: Readonly<Record<string, unknown>>, problems: PolicyProblem[]): Record<string, DeclaredSignIn>;
102
+ interface FoundHost {
103
+ /** As the kit matches it: `URL.host`, without the scheme's default port. */
104
+ readonly host: string;
105
+ readonly hostname: string;
106
+ /** Plain `http`, so only to a local address. */
107
+ readonly plain: boolean;
108
+ }
109
+ /** The class the sheet declares for each setting a `{ fromSetting }` host reads. */
110
+ export declare function settingClasses(sheet: IntegrationSheet): ReadonlyMap<string, AddressClass>;
111
+ export declare function resolveHost(sheet: IntegrationSheet, host: IntegrationHost | IntegrationSecretHost, classes: ReadonlyMap<string, AddressClass>, settings: Readonly<Record<string, unknown>>, path: Path, problems: PolicyProblem[]): FoundHost | undefined;
112
+ export {};
@@ -1,4 +1,6 @@
1
1
  import { err, ok } from "../../../../std/src/index.js";
2
+ import { isPlacedSecret, } from "./schema.js";
3
+ import { fixedSignInUrl } from "./sign-in-urls.js";
2
4
  /**
3
5
  * Turns a parsed sheet and the integration's settings into the policy the
4
6
  * kit enforces. A `{ fromSetting }` host becomes the host of the URL in that
@@ -27,6 +29,9 @@ export function integrationPolicy(sheet, settings) {
27
29
  });
28
30
  const secrets = {};
29
31
  for (const [name, secret] of Object.entries(sheet.secrets)) {
32
+ if (!isPlacedSecret(secret)) {
33
+ continue; // a client secret: `resolveSignIns` gives it its hosts
34
+ }
30
35
  const secretHosts = [];
31
36
  secret.hosts.forEach((host, index) => {
32
37
  const path = ["secrets", name, "hosts", index];
@@ -50,6 +55,7 @@ export function integrationPolicy(sheet, settings) {
50
55
  ? { hosts: resolved, optional: true, placement }
51
56
  : { hosts: resolved, placement };
52
57
  }
58
+ const signIns = resolveSignIns(sheet, classes, settings, problems);
53
59
  if (problems.length > 0) {
54
60
  return err(problems);
55
61
  }
@@ -58,10 +64,63 @@ export function integrationPolicy(sheet, settings) {
58
64
  hosts: unique(hosts),
59
65
  ...(localHosts.length > 0 ? { localHosts: unique(localHosts) } : {}),
60
66
  secrets,
67
+ signIns,
61
68
  tokens: sheet.tokens.map((token) => token.name),
62
69
  };
63
70
  return ok(sheet.limits === undefined ? { policy } : { policy, limits: sheet.limits });
64
71
  }
72
+ /**
73
+ * Each sign-in's access hosts and placement, and its client secret with the
74
+ * hosts of the sign-in's token and revoke URLs. The other sign-in URLs read
75
+ * settings that `hosts` already resolves, so they need no check here.
76
+ */
77
+ export function resolveSignIns(sheet, classes, settings, problems) {
78
+ const signIns = {};
79
+ const hostsOf = (hosts, path) => unique(hosts.flatMap((host, index) => {
80
+ const found = resolveHost(sheet, host, classes, settings, [...path, index], problems);
81
+ return found === undefined ? [] : [found.host];
82
+ }));
83
+ for (const [name, signIn] of Object.entries(sheet.signIns)) {
84
+ const path = ["signIns", name];
85
+ const base = {
86
+ hosts: hostsOf(signIn.access.hosts, [...path, "access", "hosts"]),
87
+ ...signInPlacement(signIn.access),
88
+ };
89
+ if (signIn.clientSecret === undefined) {
90
+ signIns[name] = base;
91
+ continue;
92
+ }
93
+ const endpoints = [];
94
+ for (const field of ["tokenUrl", "revokeUrl"]) {
95
+ const url = signIn[field];
96
+ const fixed = url === undefined ? undefined : fixedSignInUrl(url);
97
+ if (typeof url === "object") {
98
+ endpoints.push({ fromSetting: url.fromSetting });
99
+ }
100
+ else if (fixed !== undefined) {
101
+ endpoints.push(withoutDefaultPort(fixed.host));
102
+ }
103
+ }
104
+ signIns[name] = {
105
+ ...base,
106
+ clientSecret: { name: signIn.clientSecret, hosts: hostsOf(endpoints, [...path, "tokenUrl"]) },
107
+ };
108
+ }
109
+ return signIns;
110
+ }
111
+ /** `host:443` as `URL.host` gives it, which drops the scheme's default port. */
112
+ function withoutDefaultPort(host) {
113
+ return host.endsWith(":443") ? host.slice(0, -4) : host;
114
+ }
115
+ /** The placement part of a sign-in's `access`, without its hosts. */
116
+ function signInPlacement(access) {
117
+ if (access.as === "bearer") {
118
+ return { as: "bearer" };
119
+ }
120
+ return access.prefix === undefined
121
+ ? { as: "header", header: access.header }
122
+ : { as: "header", header: access.header, prefix: access.prefix };
123
+ }
65
124
  /** The placement part of a sheet's secret, without its purpose and hosts. */
66
125
  function placementOf(secret) {
67
126
  switch (secret.as) {
@@ -76,7 +135,7 @@ function placementOf(secret) {
76
135
  }
77
136
  }
78
137
  /** The class the sheet declares for each setting a `{ fromSetting }` host reads. */
79
- function settingClasses(sheet) {
138
+ export function settingClasses(sheet) {
80
139
  const classes = new Map();
81
140
  for (const host of sheet.hosts) {
82
141
  if (typeof host !== "string" && !classes.has(host.fromSetting)) {
@@ -85,7 +144,7 @@ function settingClasses(sheet) {
85
144
  }
86
145
  return classes;
87
146
  }
88
- function resolveHost(sheet, host, classes, settings, path, problems) {
147
+ export function resolveHost(sheet, host, classes, settings, path, problems) {
89
148
  if (typeof host === "string") {
90
149
  return { host, hostname: host, plain: false };
91
150
  }