@composable-os/product 0.1.0-beta.11 → 0.1.0-beta.13

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/dist/index.d.ts CHANGED
@@ -31,6 +31,23 @@ export interface ProductDefinition {
31
31
  name: string;
32
32
  };
33
33
  }
34
+ /**
35
+ * The location hints both D1 (`primary_location_hint`) and R2 (`locationHint`) accept when a
36
+ * database or a bucket is created, as Cloudflare's API reference listed them on 2026-11-23 (ADR
37
+ * 0580). A hint is a preference Cloudflare follows best effort, only at creation: never a
38
+ * guarantee of where data is kept. A jurisdiction (eu, us, fedramp) is a different setting and is
39
+ * not offered.
40
+ */
41
+ export declare const LOCATION_HINTS: {
42
+ readonly wnam: "Western North America";
43
+ readonly enam: "Eastern North America";
44
+ readonly weur: "Western Europe";
45
+ readonly eeur: "Eastern Europe";
46
+ readonly apac: "Asia-Pacific";
47
+ readonly oc: "Oceania";
48
+ };
49
+ export type LocationHint = keyof typeof LOCATION_HINTS;
50
+ export declare function isLocationHint(value: unknown): value is LocationHint;
34
51
  /** Checks a definition and returns it. Throws on the first name a Cloudflare account would refuse. */
35
52
  export declare function defineProduct(definition: ProductDefinition): ProductDefinition;
36
53
  /**
@@ -46,6 +63,18 @@ export declare function defineProduct(definition: ProductDefinition): ProductDef
46
63
  * every surface that shows the product's name shows `label` (COS_PRODUCT_LABEL). A deployment that
47
64
  * exists keeps its names: nothing deployed is renamed.
48
65
  */
66
+ /** A pack role the mobile app's staff sign in with, and the grants its client also types. */
67
+ export interface MobileRole {
68
+ /** `<pack>.<role>`, as an account holds it. */
69
+ role: string;
70
+ /** Grantable pack permissions (`<pack>:<name>`) whose tools the role's client also holds. */
71
+ grants?: string[];
72
+ }
73
+ /** A `mobile.roles` entry as its role and grants (a bare string has none). */
74
+ export declare function mobileRole(entry: string | MobileRole): {
75
+ role: string;
76
+ grants: string[];
77
+ };
49
78
  export interface ClientProductDefinition {
50
79
  /** The company's product, lowercase: names its Workers (`<id>-web`) and the repo. */
51
80
  id: string;
@@ -90,13 +119,32 @@ export interface ClientProductDefinition {
90
119
  * roles (ADR 0436) its staff may sign in with: `cos client generate` writes a client for each
91
120
  * (`roles/<pack>.<role>.ts`) with only that role's operations, never the whole staff client
92
121
  * (ADR 0451). The web app has none: its binding never serves staff (ADR 0421).
122
+ *
123
+ * A role may name grantable pack permissions its staff are given one by one (ADR 0600), as
124
+ * `{ role: "bookings.coach", grants: ["rostering:roster"] }`: its client then also holds the
125
+ * tools those grants open (stage 45 step 7: a coach who is rostered). The host still decides
126
+ * every call: the client only types what such an account may ask.
93
127
  */
94
128
  mobile?: {
95
129
  dir: string;
96
130
  clientDir?: string;
97
- roles?: string[];
98
- /** The app's URL scheme (app.json `expo.scheme`), default the product's id. */
131
+ roles?: (string | MobileRole)[];
132
+ /**
133
+ * The owner's client (stage 60, ADR 0872): `cos client generate` also writes `roles/owner.ts`,
134
+ * holding only the operations the owner's Today needs (Today, the approvals, the notice inbox,
135
+ * who is signed in), from the release's `roles.json`. Not a pack role: any staff account may
136
+ * sign in with it, and the host decides every call, so a coach sees only what their rights
137
+ * allow.
138
+ */
139
+ owner?: boolean;
140
+ /** The app's URL scheme (`expo.scheme` in its app config), default the product's id. */
99
141
  scheme?: string;
142
+ /**
143
+ * The app's identity in the phone app stores (stage 63, ADR 0900): its name, version, store
144
+ * ids, icon, splash, colours and listing. The app's `app.config.ts` and `brand.ts` are made
145
+ * from it, so none of it is written by hand in the app. Absent: the app is not yet published.
146
+ */
147
+ app?: MobileApp;
100
148
  };
101
149
  /**
102
150
  * The host's names, when they are not the ones derived from `id` (`clientNames`). With
@@ -112,6 +160,13 @@ export interface ClientProductDefinition {
112
160
  environments?: Record<string, ClientEnvironment>;
113
161
  /** Gatekeepers every environment installs, and their settings (ADR 0452). */
114
162
  gatekeepers?: GatekeeperSettings;
163
+ /**
164
+ * A location hint for the host's D1 database and R2 buckets (ADR 0580), e.g. `oc` (Oceania):
165
+ * `cos deploy` sends it when the host is first installed and they are created. A preference
166
+ * Cloudflare follows where it can, not a guarantee; KV, queues and Vectorize take none. A host
167
+ * that exists is never moved by changing it. Absent: no hint.
168
+ */
169
+ location?: LocationHint;
115
170
  }
116
171
  /**
117
172
  * One environment of a client product (ADR 0450). Its names derive from the product's id and the
@@ -142,10 +197,82 @@ export interface ClientEnvironment {
142
197
  */
143
198
  mobile?: {
144
199
  scheme?: string;
200
+ /**
201
+ * What this environment's app changes of `mobile.app` (stage 63): a staging app installs beside
202
+ * the live one with its own name and store ids, and may have its own icon, splash and listing.
203
+ */
204
+ app?: MobileAppOverride;
145
205
  };
146
206
  /** The host's names here, when they are not the derived ones. */
147
207
  names?: Partial<ProductDefinition["names"]>;
208
+ /** The location hint here, over the product's (ADR 0580); `"none"` for no hint. */
209
+ location?: LocationHint | "none";
148
210
  }
211
+ /**
212
+ * A mobile app's identity in the phone app stores (stage 63, ADR 0900). Paths are from the root of
213
+ * the client repo. The store ids never change once the app is published: `cos app check` and
214
+ * `cos app record` refuse one that differs from a build already recorded.
215
+ */
216
+ export interface MobileApp {
217
+ /** The name under the icon, at most 30 characters. */
218
+ name: string;
219
+ /** The version people see (x.y.z), bumped by a pull request. */
220
+ version: string;
221
+ /** The iPhone app's bundle identifier and the Android app's package name, reverse-domain. */
222
+ ids: {
223
+ ios: string;
224
+ android: string;
225
+ };
226
+ /** A 1024 by 1024 PNG with no transparency. */
227
+ icon: string;
228
+ /** The launch screen: a PNG and the colour around it. */
229
+ splash: {
230
+ image: string;
231
+ background: string;
232
+ };
233
+ /** The app's brand (`#rrggbb`): the text colour must be readable on the background. */
234
+ colours: {
235
+ primary: string;
236
+ background: string;
237
+ text: string;
238
+ };
239
+ /** The store listing's folder: one folder per locale (`en-AU/`), see `cos app check`. */
240
+ listing: string;
241
+ /** What the app sells: nothing (decision 8, ADR 0490). The only value accepted. */
242
+ purchases: "none";
243
+ }
244
+ /** What one environment's app changes of the product's `mobile.app`. */
245
+ export type MobileAppOverride = Partial<Pick<MobileApp, "name" | "ids" | "icon" | "splash" | "listing">>;
246
+ /** One build of the mobile app, for one environment: what its app config and its checks read. */
247
+ export interface AppBuild {
248
+ /** The environment it talks to; `live` for a product that declares none. */
249
+ environment: string;
250
+ name: string;
251
+ /** The app framework's project slug: the product's id, `<id>-<env>` outside live. */
252
+ slug: string;
253
+ scheme: string;
254
+ version: string;
255
+ /** `https://<the environment's hostname>`, or null when product.ts gives it none. */
256
+ hostUrl: string | null;
257
+ /** The web app's account page (`https://<web hostname>/account`), or null. */
258
+ websiteUrl: string | null;
259
+ /** The store identity, from `mobile.app` with the environment's changes; null without it. */
260
+ store: Omit<MobileApp, "name" | "version"> | null;
261
+ }
262
+ /** The stores' limit on the name under the icon. */
263
+ export declare const APP_NAME_MAX = 30;
264
+ /** The lowest contrast the text colour may have on the background (WCAG AA for body text). */
265
+ export declare const MIN_TEXT_CONTRAST = 4.5;
266
+ /** WCAG's contrast ratio of two `#rrggbb` colours, from 1 to 21. */
267
+ export declare function contrastRatio(a: string, b: string): number;
268
+ /**
269
+ * The mobile app's build for an environment (stage 63): its name, ids, scheme and host from
270
+ * product.ts. `environment` is required when product.ts declares environments, and refused when
271
+ * it declares none. Throws when the product has no mobile app.
272
+ */
273
+ export declare function appBuild(product: ClientProductDefinition, environment?: string): AppBuild;
274
+ /** The location hint a first install of an environment (or the product) is created with. */
275
+ export declare function clientLocation(product: ClientProductDefinition, environment?: string): LocationHint | undefined;
149
276
  /**
150
277
  * The gatekeepers a client host installs and their settings (ADR 0452), by the gatekeeper's
151
278
  * short name. Each takes only the settings that gatekeeper declares in its release (`cos deploy`
@@ -173,7 +300,19 @@ export interface GatekeeperSettings {
173
300
  /** The fake provider instead of Stripe: only in an environment marked `live: false`. */
174
301
  PAYMENTS_FAKE_PROVIDER?: boolean;
175
302
  };
303
+ /**
304
+ * Text messages (packages/gatekeeper-text, ADR 0672), through the business's own text provider
305
+ * account. Its webhook address, BASE_URL, is derived; the provider's keys are Worker secrets.
306
+ */
307
+ text?: {
308
+ /** The provider module texts go through; `fake` only in an environment marked `live: false`. */
309
+ TEXT_PROVIDER?: string;
310
+ /** The business's own sender number, in international form (`+61…`). */
311
+ TEXT_SENDER_NUMBER?: string;
312
+ };
176
313
  }
314
+ /** Whether `settings` turn on a fake provider (payments' or texts'), which a live host may not. */
315
+ export declare function turnsOnFakeProvider(settings: GatekeeperSettings): string | null;
177
316
  /** Whether an environment of `product` (or the product without environments) is live. */
178
317
  export declare function isLiveEnvironment(product: ClientProductDefinition, environment?: string): boolean;
179
318
  /** The gatekeepers and settings a deploy of `environment` applies: the product's, then its own. */
package/dist/index.js CHANGED
@@ -4,6 +4,24 @@
4
4
  // definition (products/<id>/product.ts).
5
5
  const NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
6
6
  const PREFIX = /^[a-z0-9]+(-[a-z0-9]+)*-$/;
7
+ /**
8
+ * The location hints both D1 (`primary_location_hint`) and R2 (`locationHint`) accept when a
9
+ * database or a bucket is created, as Cloudflare's API reference listed them on 2026-11-23 (ADR
10
+ * 0580). A hint is a preference Cloudflare follows best effort, only at creation: never a
11
+ * guarantee of where data is kept. A jurisdiction (eu, us, fedramp) is a different setting and is
12
+ * not offered.
13
+ */
14
+ export const LOCATION_HINTS = {
15
+ wnam: "Western North America",
16
+ enam: "Eastern North America",
17
+ weur: "Western Europe",
18
+ eeur: "Eastern Europe",
19
+ apac: "Asia-Pacific",
20
+ oc: "Oceania",
21
+ };
22
+ export function isLocationHint(value) {
23
+ return typeof value === "string" && Object.hasOwn(LOCATION_HINTS, value);
24
+ }
7
25
  /** Checks a definition and returns it. Throws on the first name a Cloudflare account would refuse. */
8
26
  export function defineProduct(definition) {
9
27
  const fail = (what) => {
@@ -33,6 +51,157 @@ export function defineProduct(definition) {
33
51
  fail("billingUrl must be an https:// URL");
34
52
  return definition;
35
53
  }
54
+ /** A `mobile.roles` entry as its role and grants (a bare string has none). */
55
+ export function mobileRole(entry) {
56
+ return typeof entry === "string"
57
+ ? { role: entry, grants: [] }
58
+ : { role: entry.role, grants: [...(entry.grants ?? [])] };
59
+ }
60
+ /** An iPhone bundle identifier: reverse-domain, letters, digits and hyphens. */
61
+ const IOS_ID = /^[A-Za-z][A-Za-z0-9-]*(\.[A-Za-z][A-Za-z0-9-]*)+$/;
62
+ /** An Android package name: reverse-domain, letters, digits and underscores. */
63
+ const ANDROID_ID = /^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$/;
64
+ const HEX_COLOUR = /^#[0-9a-fA-F]{6}$/;
65
+ const SCHEME = /^[a-z][a-z0-9+.-]*$/;
66
+ const APP_VERSION = /^\d+\.\d+\.\d+$/;
67
+ /** The stores' limit on the name under the icon. */
68
+ export const APP_NAME_MAX = 30;
69
+ /** The lowest contrast the text colour may have on the background (WCAG AA for body text). */
70
+ export const MIN_TEXT_CONTRAST = 4.5;
71
+ /** WCAG's contrast ratio of two `#rrggbb` colours, from 1 to 21. */
72
+ export function contrastRatio(a, b) {
73
+ const luminance = (hex) => {
74
+ const [r, g, b] = [1, 3, 5].map((i) => {
75
+ const c = Number.parseInt(hex.slice(i, i + 2), 16) / 255;
76
+ return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
77
+ });
78
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
79
+ };
80
+ const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
81
+ return (hi + 0.05) / (lo + 0.05);
82
+ }
83
+ /**
84
+ * The mobile app's build for an environment (stage 63): its name, ids, scheme and host from
85
+ * product.ts. `environment` is required when product.ts declares environments, and refused when
86
+ * it declares none. Throws when the product has no mobile app.
87
+ */
88
+ export function appBuild(product, environment) {
89
+ if (!product.mobile)
90
+ throw new Error(`product ${product.id} has no mobile app`);
91
+ const envs = environmentNames(product);
92
+ if (envs.length && (!environment || !envs.includes(environment))) {
93
+ throw new Error(`product ${product.id}: name the app's environment (${envs.join(", ")}), as COS_ENV or --env${environment ? `; it has no ${environment}` : ""}`);
94
+ }
95
+ if (!envs.length && environment && environment !== LIVE_ENVIRONMENT) {
96
+ throw new Error(`product ${product.id} declares no environments, so there is no ${environment}`);
97
+ }
98
+ const env = envs.length ? product.environments?.[environment] : undefined;
99
+ const live = !envs.length || environment === LIVE_ENVIRONMENT;
100
+ const app = product.mobile.app;
101
+ const own = env?.mobile?.app;
102
+ const web = clientWeb(product, envs.length ? environment : undefined);
103
+ return {
104
+ environment: envs.length ? environment : LIVE_ENVIRONMENT,
105
+ name: own?.name ?? app?.name ?? product.label,
106
+ slug: live ? product.id : `${product.id}-${environment}`,
107
+ scheme: env?.mobile?.scheme ?? product.mobile.scheme ?? product.id,
108
+ version: app?.version ?? "1.0.0",
109
+ hostUrl: env?.hostname ? `https://${env.hostname}` : null,
110
+ websiteUrl: web?.hostname ? `https://${web.hostname}/account` : null,
111
+ store: app
112
+ ? {
113
+ ids: own?.ids ?? app.ids,
114
+ icon: own?.icon ?? app.icon,
115
+ splash: own?.splash ?? app.splash,
116
+ colours: app.colours,
117
+ listing: own?.listing ?? app.listing,
118
+ purchases: app.purchases,
119
+ }
120
+ : null,
121
+ };
122
+ }
123
+ /** The first problem with the mobile app's identity, or null (checked by defineClientProduct). */
124
+ function mobileAppProblem(definition) {
125
+ const app = definition.mobile?.app;
126
+ const envs = environmentNames(definition);
127
+ for (const env of envs) {
128
+ if (definition.environments?.[env]?.mobile?.app && !app) {
129
+ return `environments.${env}.mobile.app changes mobile.app, which product.ts does not give`;
130
+ }
131
+ }
132
+ if (!app)
133
+ return null;
134
+ if (!app.name?.trim() || app.name.length > APP_NAME_MAX) {
135
+ return `mobile.app.name must be 1 to ${APP_NAME_MAX} characters`;
136
+ }
137
+ if (!APP_VERSION.test(app.version ?? ""))
138
+ return "mobile.app.version must be x.y.z (digits only)";
139
+ if (app.purchases !== "none") {
140
+ return 'mobile.app.purchases must be "none": nothing is bought in the app (ADR 0490); buying stays on the website';
141
+ }
142
+ for (const key of ["primary", "background", "text"]) {
143
+ if (!HEX_COLOUR.test(app.colours?.[key] ?? "")) {
144
+ return `mobile.app.colours.${key} must be a colour as #rrggbb`;
145
+ }
146
+ }
147
+ const ratio = contrastRatio(app.colours.text, app.colours.background);
148
+ if (ratio < MIN_TEXT_CONTRAST) {
149
+ return `mobile.app.colours.text is not readable on the background: contrast ${ratio.toFixed(2)}, at least ${MIN_TEXT_CONTRAST} is needed`;
150
+ }
151
+ const builds = (envs.length ? envs : [undefined]).map((env) => ({
152
+ where: env ? `environment ${env}` : "the app",
153
+ build: appBuild(definition, env),
154
+ }));
155
+ const seen = { ios: new Map(), android: new Map() };
156
+ const schemes = new Map();
157
+ for (const { where, build } of builds) {
158
+ const store = build.store;
159
+ if (!store.icon?.trim() || !store.splash?.image?.trim() || !store.listing?.trim()) {
160
+ return `${where}: mobile.app needs icon, splash.image and listing (paths in the repo)`;
161
+ }
162
+ if (!HEX_COLOUR.test(store.splash.background ?? "")) {
163
+ return `${where}: mobile.app.splash.background must be a colour as #rrggbb`;
164
+ }
165
+ if (build.name.length > APP_NAME_MAX) {
166
+ return `${where}: the app's name must be at most ${APP_NAME_MAX} characters`;
167
+ }
168
+ if (!IOS_ID.test(store.ids?.ios ?? "")) {
169
+ return `${where}: the iPhone app's id ${JSON.stringify(store.ids?.ios)} is not reverse-domain (com.example.app)`;
170
+ }
171
+ if (!ANDROID_ID.test(store.ids?.android ?? "")) {
172
+ return `${where}: the Android app's id ${JSON.stringify(store.ids?.android)} is not reverse-domain with letters, digits and underscores (com.example.app)`;
173
+ }
174
+ if (!SCHEME.test(build.scheme))
175
+ return `${where}: the app's scheme ${build.scheme} is not a URL scheme`;
176
+ // Each environment's app installs beside the others: its own ids and its own scheme.
177
+ for (const platform of ["ios", "android"]) {
178
+ const other = seen[platform].get(store.ids[platform]);
179
+ if (other)
180
+ return `${other} and ${where} have the same ${platform} id ${store.ids[platform]}`;
181
+ seen[platform].set(store.ids[platform], where);
182
+ }
183
+ const other = schemes.get(build.scheme);
184
+ if (other)
185
+ return `${other} and ${where} have the same scheme ${build.scheme}`;
186
+ schemes.set(build.scheme, where);
187
+ }
188
+ return null;
189
+ }
190
+ /** The location hint a first install of an environment (or the product) is created with. */
191
+ export function clientLocation(product, environment) {
192
+ const own = environment ? product.environments?.[environment]?.location : undefined;
193
+ if (own === "none")
194
+ return undefined;
195
+ return own ?? product.location;
196
+ }
197
+ /** Whether `settings` turn on a fake provider (payments' or texts'), which a live host may not. */
198
+ export function turnsOnFakeProvider(settings) {
199
+ if (settings.payments?.PAYMENTS_FAKE_PROVIDER === true)
200
+ return "the fake payment provider";
201
+ if (settings.text?.TEXT_PROVIDER === "fake")
202
+ return "the fake text provider";
203
+ return null;
204
+ }
36
205
  /** Whether an environment of `product` (or the product without environments) is live. */
37
206
  export function isLiveEnvironment(product, environment) {
38
207
  if (environment === undefined || environment === LIVE_ENVIRONMENT)
@@ -164,7 +333,9 @@ export function defineClientProduct(definition) {
164
333
  fail("a pack is listed twice");
165
334
  if (definition.web && !NAME.test(definition.web.worker))
166
335
  fail("web.worker must be a Worker name");
167
- for (const role of definition.mobile?.roles ?? []) {
336
+ const seenRoles = new Set();
337
+ for (const entry of definition.mobile?.roles ?? []) {
338
+ const { role, grants } = mobileRole(entry);
168
339
  const [pack, local] = role.split(".");
169
340
  if (!pack || !local || role.split(".").length !== 2) {
170
341
  fail(`mobile.roles: ${role} is not <pack>.<role>`);
@@ -172,6 +343,18 @@ export function defineClientProduct(definition) {
172
343
  if (!definition.packs.includes(pack)) {
173
344
  fail(`mobile.roles: ${role} is a role of ${pack}, which packs does not list`);
174
345
  }
346
+ if (seenRoles.has(role))
347
+ fail(`mobile.roles: ${role} is listed twice`);
348
+ seenRoles.add(role);
349
+ for (const grant of grants) {
350
+ const [grantPack, name] = grant.split(":");
351
+ if (!grantPack || !name || grant.split(":").length !== 2) {
352
+ fail(`mobile.roles: ${role}'s grant ${grant} is not <pack>:<permission>`);
353
+ }
354
+ if (!definition.packs.includes(grantPack)) {
355
+ fail(`mobile.roles: ${role}'s grant ${grant} is of ${grantPack}, which packs does not list`);
356
+ }
357
+ }
175
358
  }
176
359
  const names = clientNames(definition);
177
360
  for (const key of ["app", "runner", "database"]) {
@@ -187,14 +370,28 @@ export function defineClientProduct(definition) {
187
370
  if (definition.web && [names.app, names.runner].includes(definition.web.worker)) {
188
371
  fail("web.worker is one of the host's Workers");
189
372
  }
373
+ const hints = Object.keys(LOCATION_HINTS).join(", ");
374
+ if (definition.location !== undefined && !isLocationHint(definition.location)) {
375
+ fail(`location is a location hint (${hints})`);
376
+ }
377
+ for (const [env, settings] of Object.entries(definition.environments ?? {})) {
378
+ if (settings.location !== undefined &&
379
+ settings.location !== "none" &&
380
+ !isLocationHint(settings.location)) {
381
+ fail(`environments.${env}.location is none or a location hint (${hints})`);
382
+ }
383
+ }
384
+ const appProblem = mobileAppProblem(definition);
385
+ if (appProblem)
386
+ fail(appProblem);
190
387
  if (definition.environments?.[LIVE_ENVIRONMENT]?.live === false) {
191
388
  fail("the live environment cannot be marked live: false");
192
389
  }
193
390
  const deployments = environmentNames(definition);
194
391
  for (const env of deployments.length ? deployments : [undefined]) {
195
- if (gatekeeperSettings(definition, env).payments?.PAYMENTS_FAKE_PROVIDER === true &&
196
- isLiveEnvironment(definition, env)) {
197
- fail(`${env ? `environment ${env}` : "the product"} is live and turns on the fake payment provider: only an environment marked live: false may`);
392
+ const fake = turnsOnFakeProvider(gatekeeperSettings(definition, env));
393
+ if (fake && isLiveEnvironment(definition, env)) {
394
+ fail(`${env ? `environment ${env}` : "the product"} is live and turns on ${fake}: only an environment marked live: false may`);
198
395
  }
199
396
  }
200
397
  if (definition.environments) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@composable-os/product",
3
- "version": "0.1.0-beta.11",
3
+ "version": "0.1.0-beta.13",
4
4
  "license": "FSL-1.1-ALv2",
5
5
  "type": "module",
6
6
  "repository": {