@pithy-sh/cloudflare 0.5.0 → 0.7.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.
Files changed (59) hide show
  1. package/dist/analytics/analyticsManager.d.ts +78 -0
  2. package/dist/analytics/analyticsManager.d.ts.map +1 -0
  3. package/dist/analytics/analyticsManager.js +206 -0
  4. package/dist/analytics/datasets.d.ts +26 -0
  5. package/dist/analytics/datasets.d.ts.map +1 -0
  6. package/dist/analytics/datasets.js +26 -0
  7. package/dist/analytics/window.d.ts +50 -0
  8. package/dist/analytics/window.d.ts.map +1 -0
  9. package/dist/analytics/window.js +67 -0
  10. package/dist/artifacts/artifactsManager.d.ts +52 -0
  11. package/dist/artifacts/artifactsManager.d.ts.map +1 -0
  12. package/dist/artifacts/artifactsManager.js +150 -0
  13. package/dist/client/clients.d.ts +13 -1
  14. package/dist/client/clients.d.ts.map +1 -1
  15. package/dist/client/clients.js +22 -1
  16. package/dist/client/errors.d.ts +4 -3
  17. package/dist/client/errors.d.ts.map +1 -1
  18. package/dist/media/imageManager.d.ts +25 -0
  19. package/dist/media/imageManager.d.ts.map +1 -1
  20. package/dist/media/imageManager.js +58 -2
  21. package/dist/media/streamManager.d.ts +15 -0
  22. package/dist/media/streamManager.d.ts.map +1 -1
  23. package/dist/media/streamManager.js +28 -1
  24. package/dist/oauth/scopes.d.ts +2 -2
  25. package/dist/oauth/scopes.js +2 -2
  26. package/dist/queue/queueProvisioner.d.ts +30 -0
  27. package/dist/queue/queueProvisioner.d.ts.map +1 -0
  28. package/dist/queue/queueProvisioner.js +72 -0
  29. package/dist/r2/r2Manager.d.ts +11 -1
  30. package/dist/r2/r2Manager.d.ts.map +1 -1
  31. package/dist/r2/r2Manager.js +18 -1
  32. package/dist/tokens/accountTokensManager.d.ts +1 -0
  33. package/dist/tokens/accountTokensManager.d.ts.map +1 -1
  34. package/dist/tokens/accountTokensManager.js +2 -1
  35. package/dist/tokens/permissions.d.ts +10 -0
  36. package/dist/tokens/permissions.d.ts.map +1 -1
  37. package/dist/tokens/permissions.js +12 -2
  38. package/dist/tokens/profiles.d.ts +6 -30
  39. package/dist/tokens/profiles.d.ts.map +1 -1
  40. package/dist/tokens/profiles.js +44 -36
  41. package/dist/tokens/runtime.d.ts +16 -0
  42. package/dist/tokens/runtime.d.ts.map +1 -0
  43. package/dist/tokens/runtime.js +18 -0
  44. package/package.json +3 -3
  45. package/src/analytics/analyticsManager.ts +311 -0
  46. package/src/analytics/datasets.ts +30 -0
  47. package/src/analytics/window.ts +101 -0
  48. package/src/artifacts/artifactsManager.ts +199 -0
  49. package/src/client/clients.ts +28 -1
  50. package/src/client/errors.ts +4 -3
  51. package/src/media/imageManager.ts +93 -1
  52. package/src/media/streamManager.ts +34 -0
  53. package/src/oauth/scopes.ts +2 -2
  54. package/src/queue/queueProvisioner.ts +79 -0
  55. package/src/r2/r2Manager.ts +29 -2
  56. package/src/tokens/accountTokensManager.ts +12 -0
  57. package/src/tokens/permissions.ts +27 -0
  58. package/src/tokens/profiles.ts +54 -70
  59. package/src/tokens/runtime.ts +18 -0
@@ -1,7 +1,7 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
- import type { Capability, TokenProfileSeam } from "@pithy-sh/core/src/capability/capability";
4
+ import type { Capability } from "@pithy-sh/core/src/capability/capability";
5
5
  import { CloudflareNotConfiguredError } from "../client/errors";
6
6
  import { accountResource, type TokenPermission, zoneResources } from "./accountTokensManager";
7
7
  import {
@@ -11,12 +11,13 @@ import {
11
11
  type PermissionKey,
12
12
  resolvePermissionKeys,
13
13
  } from "./permissions";
14
+ import { RUNTIME_PROFILE, RUNTIME_TOKEN_SECRET } from "./runtime";
14
15
 
15
16
  /**
16
17
  * Where a minted token's value is written.
17
18
  *
18
- * - `secrets-store` — the CF Secrets Store; a Worker reads it via its binding. The destination for a
19
- * worker-consumer token; resolved from the token's declared secret (its registry backend) unless a
19
+ * - `secrets-store` — the CF Secrets Store; a Worker reads it via its binding. The destination for the
20
+ * `runtime` token; resolved from the token's declared secret (its registry backend) unless a
20
21
  * profile or `--store` names it directly.
21
22
  * - `dev-vars` — `<config>/<project>/tokens.json`, keyed by environment and outside every checkout.
22
23
  * **Read by an operator, never by a command** — nothing resolves a credential from it. The store for
@@ -62,26 +63,12 @@ export interface TokenProfile {
62
63
  resources?: "account" | Record<string, string>;
63
64
  /** The secret-registry name the minted value is stored under; its declared backend is the destination. */
64
65
  secret: string;
65
- /**
66
- * Whether the value behind {@link secret} differs per environment (the default) or is one value every
67
- * environment shares. It decides the environment segment of the **CF Secrets Store entry name** the
68
- * minted value is written to, and nothing else — never the variable key, which is a variable name
69
- * and stays verbatim. A `global` profile writes one entry with the literal `global` in that slot,
70
- * matching what provisioning wrote; an `environment` profile writes one entry per environment.
71
- */
72
- secretScope?: TokenSecretScope;
73
66
  /** A built-in destination override (`dev-vars`/`ephemeral`/`secrets-store`); else the secret's backend. */
74
67
  defaultStore?: TokenStore;
75
68
  /** Why this profile exists / what job consumes it. */
76
69
  description: string;
77
70
  }
78
71
 
79
- /** Whether a profile's secret is one value per environment or one value shared by all of them. */
80
- export type TokenSecretScope = "environment" | "global";
81
-
82
- /** The valid secret scopes, for validating a capability's declaration. */
83
- const TOKEN_SECRET_SCOPES: readonly TokenSecretScope[] = ["environment", "global"];
84
-
85
72
  /** An adopter's override of a profile's defaults (from `pithy.config.ts` or CLI flags). Every field optional. */
86
73
  export interface ProfileOverride {
87
74
  /** Replace the profile's permission keys. */
@@ -112,15 +99,21 @@ function assertPermissionKeys(keys: readonly string[], where: string): Permissio
112
99
  if (!isPermissionKey(key)) {
113
100
  throw new CloudflareNotConfiguredError({
114
101
  message: `${where} declares an unknown permission key: ${key}.`,
115
- action:
116
- "Use a known permission key (d1:read, d1:write, workers:write, secrets:read, secrets:write, email:routing, kv:write).",
102
+ action: `Use a known permission key: ${Object.keys(PERMISSION_GROUPS).join(", ")}.`,
117
103
  });
118
104
  }
119
105
  }
120
106
  return keys as PermissionKey[];
121
107
  }
122
108
 
123
- /** Build the aggregating `ci-system` profile: the base permissions ∪ every capability's `ciPermissions`. */
109
+ /**
110
+ * Build the aggregating `ci-system` profile: the base permissions ∪ every capability's `ciPermissions` ∪
111
+ * `queues:write` where a composed capability binds a queue ∪ `artifacts:write` where one binds Artifacts.
112
+ *
113
+ * **The queue permission is derived from the binding, not declared (#662).** `pithy provision` creates the queue
114
+ * behind every `queue` binding it composes, the adopter's own app's included, and the app has no `ciPermissions`
115
+ * anyone would think to write. A permission read off the binding cannot be forgotten by whoever declares it.
116
+ */
124
117
  function ciSystemProfile(capabilities: readonly Capability[]): TokenProfile {
125
118
  const permissions: PermissionKey[] = [...CI_SYSTEM_BASE];
126
119
  for (const capability of capabilities) {
@@ -128,6 +121,16 @@ function ciSystemProfile(capabilities: readonly Capability[]): TokenProfile {
128
121
  if (!permissions.includes(key)) permissions.push(key);
129
122
  }
130
123
  }
124
+ const bindsQueue = capabilities.some((capability) =>
125
+ capability.requiredBindings.some((binding) => binding.type === "queue"),
126
+ );
127
+ if (bindsQueue && !permissions.includes("queues:write")) permissions.push("queues:write");
128
+ // **And `artifacts:write` where one binds Artifacts (#673)**, for the same reason: `pithy feature destroy` reaps
129
+ // every repo in the feature's namespace under this token, and the app that binds it declares no `ciPermissions`.
130
+ const bindsArtifacts = capabilities.some((capability) =>
131
+ capability.requiredBindings.some((binding) => binding.type === "artifacts"),
132
+ );
133
+ if (bindsArtifacts && !permissions.includes("artifacts:write")) permissions.push("artifacts:write");
131
134
  return {
132
135
  name: CI_SYSTEM_PROFILE,
133
136
  permissions,
@@ -139,66 +142,47 @@ function ciSystemProfile(capabilities: readonly Capability[]): TokenProfile {
139
142
  }
140
143
 
141
144
  /**
142
- * The seam fields this package reads. Core's {@link TokenProfileSeam} is the structural contract every
143
- * capability declares against; `secretScope` is additive on top of it, so it is named here rather than
144
- * in core — a seam value that omits it is still assignable, and one that carries it is read as declared.
145
+ * Build the aggregating `runtime` profile: every composed capability's `runtimePermissions`, each once,
146
+ * in first-declared order — or nothing, when the composition declares none.
145
147
  *
146
- * Exported so a capability can type its `tokenProfiles` entry against it. Declaring `secretScope` as an
147
- * inline object literal directly in `tokenProfiles` still trips TypeScript's excess-property check
148
- * against core's narrower `TokenProfileSeam`; declare the entry as its own `const` (with `as const` or
149
- * `satisfies TokenProfileSeamInput`) and it flows through.
148
+ * **One token per deployed application, the way `ci-system` is one per pipeline.** A capability names
149
+ * what its Workers call Cloudflare with and never a token of its own, so a second credential is not
150
+ * something a capability can ask for. The token is minted per environment into the Secrets Store, so
151
+ * revoking staging's leaves production reading. A key whose group is zone-scoped is refused here: a
152
+ * Worker's token is account-scoped, and its zones would have to be chosen by hand.
150
153
  */
151
- export interface TokenProfileSeamInput extends TokenProfileSeam {
152
- readonly secretScope?: string;
153
- }
154
-
155
- /** Build a concrete, validated worker-consumer {@link TokenProfile} from a capability's structural seam entry. */
156
- function profileFromSeam(name: string, seam: TokenProfileSeamInput, capability: string): TokenProfile {
157
- const store = seam.defaultStore;
158
- if (store !== undefined && !(TOKEN_STORES as readonly string[]).includes(store)) {
159
- throw new CloudflareNotConfiguredError({
160
- message: `Token profile "${name}" (capability "${capability}") declares an unknown store: ${store}.`,
161
- action: `Use one of: ${TOKEN_STORES.join(", ")}.`,
162
- });
163
- }
164
- const scope = seam.secretScope;
165
- if (scope !== undefined && !(TOKEN_SECRET_SCOPES as readonly string[]).includes(scope)) {
166
- throw new CloudflareNotConfiguredError({
167
- message: `Token profile "${name}" (capability "${capability}") declares an unknown secret scope: ${scope}.`,
168
- action: `Use one of: ${TOKEN_SECRET_SCOPES.join(", ")}.`,
169
- });
154
+ function runtimeProfile(capabilities: readonly Capability[]): TokenProfile | undefined {
155
+ const permissions: PermissionKey[] = [];
156
+ for (const capability of capabilities) {
157
+ const where = `Capability "${capability.name}"`;
158
+ const keys = assertPermissionKeys(capability.runtimePermissions ?? [], where);
159
+ assertAccountScoped(keys, `${where}'s runtimePermissions`);
160
+ for (const key of keys) {
161
+ if (!permissions.includes(key)) permissions.push(key);
162
+ }
170
163
  }
164
+ if (permissions.length === 0) return undefined;
171
165
  return {
172
- name,
173
- permissions: assertPermissionKeys(seam.permissions, `Token profile "${name}"`),
174
- resources: seam.resources,
175
- secret: seam.secret ?? tokenSecretName(name),
176
- secretScope: scope as TokenSecretScope | undefined,
177
- defaultStore: store as TokenStore | undefined,
178
- description: seam.description ?? name,
166
+ name: RUNTIME_PROFILE,
167
+ permissions,
168
+ secret: RUNTIME_TOKEN_SECRET,
169
+ defaultStore: "secrets-store",
170
+ description:
171
+ "The one Cloudflare API token a deployed application holds: every composed capability's runtime needs, per environment. Analytics reads are account-wide by Cloudflare's grain.",
179
172
  };
180
173
  }
181
174
 
182
175
  /**
183
- * The project's token-profile registry: the aggregating `ci-system` profile plus every composed
184
- * capability's worker-consumer {@link Capability.tokenProfiles} slice. A capability profile that
185
- * clashes with `ci-system` (or another capability's name) fails loudly at assembly. This is the one
186
- * registry `pithy token` reads.
176
+ * The project's token-profile registry: the aggregating `ci-system` profile and, when any composed
177
+ * capability declares {@link Capability.runtimePermissions}, the aggregating `runtime` profile. Nothing
178
+ * else — a capability cannot add a profile. This is the one registry `pithy token` reads.
187
179
  */
188
180
  export function resolveTokenProfiles(capabilities: readonly Capability[]): Record<string, TokenProfile> {
189
- const profiles: Record<string, TokenProfile> = { [CI_SYSTEM_PROFILE]: ciSystemProfile(capabilities) };
190
- for (const capability of capabilities) {
191
- for (const [name, seam] of Object.entries(capability.tokenProfiles ?? {})) {
192
- if (profiles[name]) {
193
- throw new CloudflareNotConfiguredError({
194
- message: `Token profile "${name}" is declared more than once (capability "${capability.name}").`,
195
- action: "Give each token profile a unique name.",
196
- });
197
- }
198
- profiles[name] = profileFromSeam(name, seam, capability.name);
199
- }
200
- }
201
- return profiles;
181
+ const runtime = runtimeProfile(capabilities);
182
+ return {
183
+ [CI_SYSTEM_PROFILE]: ciSystemProfile(capabilities),
184
+ ...(runtime ? { [RUNTIME_PROFILE]: runtime } : {}),
185
+ };
202
186
  }
203
187
 
204
188
  /**
@@ -0,0 +1,18 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /**
5
+ * **The names of the one Cloudflare API token a deployed application holds (#728)**, in a module of their
6
+ * own. A Worker reads the token by {@link RUNTIME_TOKEN_SECRET}, and importing the profile registry to
7
+ * learn a string would put the token-minting client on every Worker's bundle. Nothing here imports anything.
8
+ */
9
+
10
+ /** The name of the one runtime token — the single Cloudflare API token a deployed application holds (#728). */
11
+ export const RUNTIME_PROFILE = "runtime";
12
+
13
+ /**
14
+ * The secret the runtime token's value is stored under, and the binding every Worker that calls Cloudflare
15
+ * reads it through. Unscoped, because it is a binding name: the Secrets Store entry it resolves to carries
16
+ * the project and environment (`<project>-<env>-runtime-cf-api-token`).
17
+ */
18
+ export const RUNTIME_TOKEN_SECRET = "RUNTIME_CF_API_TOKEN";