@taggor/contract 0.0.0-stage → 0.1.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 (339) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +61 -2
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/artifact-keys.d.ts +169 -0
  5. package/dist/artifact-keys.d.ts.map +1 -0
  6. package/dist/artifact-keys.js +195 -0
  7. package/dist/artifact-keys.js.map +1 -0
  8. package/dist/assets.d.ts +72 -0
  9. package/dist/assets.d.ts.map +1 -0
  10. package/dist/assets.js +94 -0
  11. package/dist/assets.js.map +1 -0
  12. package/dist/bounded-path.d.ts +29 -0
  13. package/dist/bounded-path.d.ts.map +1 -0
  14. package/dist/bounded-path.js +57 -0
  15. package/dist/bounded-path.js.map +1 -0
  16. package/dist/bounded.d.ts +47 -0
  17. package/dist/bounded.d.ts.map +1 -0
  18. package/dist/bounded.js +93 -0
  19. package/dist/bounded.js.map +1 -0
  20. package/dist/build-diagnostic.d.ts +89 -0
  21. package/dist/build-diagnostic.d.ts.map +1 -0
  22. package/dist/build-diagnostic.js +91 -0
  23. package/dist/build-diagnostic.js.map +1 -0
  24. package/dist/component-types.d.ts +61 -0
  25. package/dist/component-types.d.ts.map +1 -0
  26. package/dist/component-types.js +277 -0
  27. package/dist/component-types.js.map +1 -0
  28. package/dist/consent-text.d.ts +31 -0
  29. package/dist/consent-text.d.ts.map +1 -0
  30. package/dist/consent-text.js +41 -0
  31. package/dist/consent-text.js.map +1 -0
  32. package/dist/contract-version.d.ts +150 -0
  33. package/dist/contract-version.d.ts.map +1 -0
  34. package/dist/contract-version.js +198 -0
  35. package/dist/contract-version.js.map +1 -0
  36. package/dist/credential-fields.d.ts +31 -0
  37. package/dist/credential-fields.d.ts.map +1 -0
  38. package/dist/credential-fields.js +36 -0
  39. package/dist/credential-fields.js.map +1 -0
  40. package/dist/css-tokens.d.ts +46 -0
  41. package/dist/css-tokens.d.ts.map +1 -0
  42. package/dist/css-tokens.js +189 -0
  43. package/dist/css-tokens.js.map +1 -0
  44. package/dist/css-value.d.ts +42 -0
  45. package/dist/css-value.d.ts.map +1 -0
  46. package/dist/css-value.js +78 -0
  47. package/dist/css-value.js.map +1 -0
  48. package/dist/data-api.d.ts +127 -0
  49. package/dist/data-api.d.ts.map +1 -0
  50. package/dist/data-api.js +167 -0
  51. package/dist/data-api.js.map +1 -0
  52. package/dist/denylist-keys.d.ts +19 -0
  53. package/dist/denylist-keys.d.ts.map +1 -0
  54. package/dist/denylist-keys.js +48 -0
  55. package/dist/denylist-keys.js.map +1 -0
  56. package/dist/diagnostic-catalog.d.ts +1402 -0
  57. package/dist/diagnostic-catalog.d.ts.map +1 -0
  58. package/dist/diagnostic-catalog.js +1311 -0
  59. package/dist/diagnostic-catalog.js.map +1 -0
  60. package/dist/dom-members.snapshot.d.ts +33 -0
  61. package/dist/dom-members.snapshot.d.ts.map +1 -0
  62. package/dist/dom-members.snapshot.js +760 -0
  63. package/dist/dom-members.snapshot.js.map +1 -0
  64. package/dist/early-loading.d.ts +27 -0
  65. package/dist/early-loading.d.ts.map +1 -0
  66. package/dist/early-loading.js +44 -0
  67. package/dist/early-loading.js.map +1 -0
  68. package/dist/error-catalog.d.ts +749 -0
  69. package/dist/error-catalog.d.ts.map +1 -0
  70. package/dist/error-catalog.js +720 -0
  71. package/dist/error-catalog.js.map +1 -0
  72. package/dist/executable-config.d.ts +35 -0
  73. package/dist/executable-config.d.ts.map +1 -0
  74. package/dist/executable-config.js +44 -0
  75. package/dist/executable-config.js.map +1 -0
  76. package/dist/freeze.d.ts +17 -0
  77. package/dist/freeze.d.ts.map +1 -0
  78. package/dist/freeze.js +29 -0
  79. package/dist/freeze.js.map +1 -0
  80. package/dist/gunzip.d.ts +33 -0
  81. package/dist/gunzip.d.ts.map +1 -0
  82. package/dist/gunzip.js +330 -0
  83. package/dist/gunzip.js.map +1 -0
  84. package/dist/hostname.d.ts +74 -0
  85. package/dist/hostname.d.ts.map +1 -0
  86. package/dist/hostname.js +215 -0
  87. package/dist/hostname.js.map +1 -0
  88. package/dist/human-actions.d.ts +49 -0
  89. package/dist/human-actions.d.ts.map +1 -0
  90. package/dist/human-actions.js +272 -0
  91. package/dist/human-actions.js.map +1 -0
  92. package/dist/index.d.ts +72 -0
  93. package/dist/index.d.ts.map +1 -0
  94. package/dist/index.js +72 -0
  95. package/dist/index.js.map +1 -0
  96. package/dist/json-schema.d.ts +108 -0
  97. package/dist/json-schema.d.ts.map +1 -0
  98. package/dist/json-schema.js +240 -0
  99. package/dist/json-schema.js.map +1 -0
  100. package/dist/key-checksum.d.ts +33 -0
  101. package/dist/key-checksum.d.ts.map +1 -0
  102. package/dist/key-checksum.js +67 -0
  103. package/dist/key-checksum.js.map +1 -0
  104. package/dist/libraries.d.ts +60 -0
  105. package/dist/libraries.d.ts.map +1 -0
  106. package/dist/libraries.js +120 -0
  107. package/dist/libraries.js.map +1 -0
  108. package/dist/limit-catalog.d.ts +371 -0
  109. package/dist/limit-catalog.d.ts.map +1 -0
  110. package/dist/limit-catalog.js +414 -0
  111. package/dist/limit-catalog.js.map +1 -0
  112. package/dist/limits.d.ts +167 -0
  113. package/dist/limits.d.ts.map +1 -0
  114. package/dist/limits.js +175 -0
  115. package/dist/limits.js.map +1 -0
  116. package/dist/locale-tag.d.ts +21 -0
  117. package/dist/locale-tag.d.ts.map +1 -0
  118. package/dist/locale-tag.js +21 -0
  119. package/dist/locale-tag.js.map +1 -0
  120. package/dist/make-diagnostic.d.ts +74 -0
  121. package/dist/make-diagnostic.d.ts.map +1 -0
  122. package/dist/make-diagnostic.js +100 -0
  123. package/dist/make-diagnostic.js.map +1 -0
  124. package/dist/manifest-compat-diagnostics.d.ts +23 -0
  125. package/dist/manifest-compat-diagnostics.d.ts.map +1 -0
  126. package/dist/manifest-compat-diagnostics.js +150 -0
  127. package/dist/manifest-compat-diagnostics.js.map +1 -0
  128. package/dist/manifest-compat.d.ts +58 -0
  129. package/dist/manifest-compat.d.ts.map +1 -0
  130. package/dist/manifest-compat.js +377 -0
  131. package/dist/manifest-compat.js.map +1 -0
  132. package/dist/manifest.d.ts +1769 -0
  133. package/dist/manifest.d.ts.map +1 -0
  134. package/dist/manifest.js +421 -0
  135. package/dist/manifest.js.map +1 -0
  136. package/dist/metering.d.ts +126 -0
  137. package/dist/metering.d.ts.map +1 -0
  138. package/dist/metering.js +147 -0
  139. package/dist/metering.js.map +1 -0
  140. package/dist/migrate.d.ts +70 -0
  141. package/dist/migrate.d.ts.map +1 -0
  142. package/dist/migrate.js +159 -0
  143. package/dist/migrate.js.map +1 -0
  144. package/dist/names.d.ts +37 -0
  145. package/dist/names.d.ts.map +1 -0
  146. package/dist/names.js +55 -0
  147. package/dist/names.js.map +1 -0
  148. package/dist/overage-pricing.d.ts +89 -0
  149. package/dist/overage-pricing.d.ts.map +1 -0
  150. package/dist/overage-pricing.js +170 -0
  151. package/dist/overage-pricing.js.map +1 -0
  152. package/dist/plan-ids.d.ts +10 -0
  153. package/dist/plan-ids.d.ts.map +1 -0
  154. package/dist/plan-ids.js +8 -0
  155. package/dist/plan-ids.js.map +1 -0
  156. package/dist/plan-limits.d.ts +138 -0
  157. package/dist/plan-limits.d.ts.map +1 -0
  158. package/dist/plan-limits.js +112 -0
  159. package/dist/plan-limits.js.map +1 -0
  160. package/dist/plan-pricing.d.ts +145 -0
  161. package/dist/plan-pricing.d.ts.map +1 -0
  162. package/dist/plan-pricing.js +172 -0
  163. package/dist/plan-pricing.js.map +1 -0
  164. package/dist/plan-usage.d.ts +112 -0
  165. package/dist/plan-usage.d.ts.map +1 -0
  166. package/dist/plan-usage.js +134 -0
  167. package/dist/plan-usage.js.map +1 -0
  168. package/dist/preview-token.d.ts +52 -0
  169. package/dist/preview-token.d.ts.map +1 -0
  170. package/dist/preview-token.js +106 -0
  171. package/dist/preview-token.js.map +1 -0
  172. package/dist/prop-names.d.ts +51 -0
  173. package/dist/prop-names.d.ts.map +1 -0
  174. package/dist/prop-names.js +150 -0
  175. package/dist/prop-names.js.map +1 -0
  176. package/dist/prop-type-docs.d.ts +20 -0
  177. package/dist/prop-type-docs.d.ts.map +1 -0
  178. package/dist/prop-type-docs.js +89 -0
  179. package/dist/prop-type-docs.js.map +1 -0
  180. package/dist/prop-types.d.ts +97 -0
  181. package/dist/prop-types.d.ts.map +1 -0
  182. package/dist/prop-types.js +299 -0
  183. package/dist/prop-types.js.map +1 -0
  184. package/dist/props.d.ts +996 -0
  185. package/dist/props.d.ts.map +1 -0
  186. package/dist/props.js +183 -0
  187. package/dist/props.js.map +1 -0
  188. package/dist/proto-guard.d.ts +29 -0
  189. package/dist/proto-guard.d.ts.map +1 -0
  190. package/dist/proto-guard.js +79 -0
  191. package/dist/proto-guard.js.map +1 -0
  192. package/dist/runtime-errors.d.ts +42 -0
  193. package/dist/runtime-errors.d.ts.map +1 -0
  194. package/dist/runtime-errors.js +42 -0
  195. package/dist/runtime-errors.js.map +1 -0
  196. package/dist/runtime-pointers.d.ts +58 -0
  197. package/dist/runtime-pointers.d.ts.map +1 -0
  198. package/dist/runtime-pointers.js +84 -0
  199. package/dist/runtime-pointers.js.map +1 -0
  200. package/dist/secret-scan.d.ts +191 -0
  201. package/dist/secret-scan.d.ts.map +1 -0
  202. package/dist/secret-scan.js +542 -0
  203. package/dist/secret-scan.js.map +1 -0
  204. package/dist/semver.d.ts +89 -0
  205. package/dist/semver.d.ts.map +1 -0
  206. package/dist/semver.js +400 -0
  207. package/dist/semver.js.map +1 -0
  208. package/dist/server-key.d.ts +68 -0
  209. package/dist/server-key.d.ts.map +1 -0
  210. package/dist/server-key.js +150 -0
  211. package/dist/server-key.js.map +1 -0
  212. package/dist/site-manifest-keys.d.ts +107 -0
  213. package/dist/site-manifest-keys.d.ts.map +1 -0
  214. package/dist/site-manifest-keys.js +146 -0
  215. package/dist/site-manifest-keys.js.map +1 -0
  216. package/dist/site-manifest.d.ts +286 -0
  217. package/dist/site-manifest.d.ts.map +1 -0
  218. package/dist/site-manifest.js +100 -0
  219. package/dist/site-manifest.js.map +1 -0
  220. package/dist/source-archive.d.ts +119 -0
  221. package/dist/source-archive.d.ts.map +1 -0
  222. package/dist/source-archive.js +644 -0
  223. package/dist/source-archive.js.map +1 -0
  224. package/dist/source-package-corpus.d.ts +55 -0
  225. package/dist/source-package-corpus.d.ts.map +1 -0
  226. package/dist/source-package-corpus.js +303 -0
  227. package/dist/source-package-corpus.js.map +1 -0
  228. package/dist/source-package.d.ts +227 -0
  229. package/dist/source-package.d.ts.map +1 -0
  230. package/dist/source-package.js +361 -0
  231. package/dist/source-package.js.map +1 -0
  232. package/dist/source-secrets.d.ts +21 -0
  233. package/dist/source-secrets.d.ts.map +1 -0
  234. package/dist/source-secrets.js +43 -0
  235. package/dist/source-secrets.js.map +1 -0
  236. package/dist/ssr-cap.d.ts +61 -0
  237. package/dist/ssr-cap.d.ts.map +1 -0
  238. package/dist/ssr-cap.js +107 -0
  239. package/dist/ssr-cap.js.map +1 -0
  240. package/dist/ssr-kv.d.ts +18 -0
  241. package/dist/ssr-kv.d.ts.map +1 -0
  242. package/dist/ssr-kv.js +18 -0
  243. package/dist/ssr-kv.js.map +1 -0
  244. package/dist/ssr-version-ledger.d.ts +53 -0
  245. package/dist/ssr-version-ledger.d.ts.map +1 -0
  246. package/dist/ssr-version-ledger.js +88 -0
  247. package/dist/ssr-version-ledger.js.map +1 -0
  248. package/dist/style-references.d.ts +58 -0
  249. package/dist/style-references.d.ts.map +1 -0
  250. package/dist/style-references.js +241 -0
  251. package/dist/style-references.js.map +1 -0
  252. package/dist/tag-name.d.ts +87 -0
  253. package/dist/tag-name.d.ts.map +1 -0
  254. package/dist/tag-name.js +222 -0
  255. package/dist/tag-name.js.map +1 -0
  256. package/dist/tar-writer.d.ts +74 -0
  257. package/dist/tar-writer.d.ts.map +1 -0
  258. package/dist/tar-writer.js +148 -0
  259. package/dist/tar-writer.js.map +1 -0
  260. package/dist/testing.d.ts +11 -0
  261. package/dist/testing.d.ts.map +1 -0
  262. package/dist/testing.js +11 -0
  263. package/dist/testing.js.map +1 -0
  264. package/dist/text-codec.d.ts +36 -0
  265. package/dist/text-codec.d.ts.map +1 -0
  266. package/dist/text-codec.js +32 -0
  267. package/dist/text-codec.js.map +1 -0
  268. package/dist/url-value.d.ts +41 -0
  269. package/dist/url-value.d.ts.map +1 -0
  270. package/dist/url-value.js +147 -0
  271. package/dist/url-value.js.map +1 -0
  272. package/dist/usage-guard.d.ts +99 -0
  273. package/dist/usage-guard.d.ts.map +1 -0
  274. package/dist/usage-guard.js +104 -0
  275. package/dist/usage-guard.js.map +1 -0
  276. package/dist/usage-mirror.d.ts +55 -0
  277. package/dist/usage-mirror.d.ts.map +1 -0
  278. package/dist/usage-mirror.js +52 -0
  279. package/dist/usage-mirror.js.map +1 -0
  280. package/dist/value-check.d.ts +88 -0
  281. package/dist/value-check.d.ts.map +1 -0
  282. package/dist/value-check.js +279 -0
  283. package/dist/value-check.js.map +1 -0
  284. package/dist/value-patterns.d.ts +9 -0
  285. package/dist/value-patterns.d.ts.map +1 -0
  286. package/dist/value-patterns.js +17 -0
  287. package/dist/value-patterns.js.map +1 -0
  288. package/dist/value-schema.d.ts +796 -0
  289. package/dist/value-schema.d.ts.map +1 -0
  290. package/dist/value-schema.js +231 -0
  291. package/dist/value-schema.js.map +1 -0
  292. package/dist/zod-free.d.ts +59 -0
  293. package/dist/zod-free.d.ts.map +1 -0
  294. package/dist/zod-free.js +57 -0
  295. package/dist/zod-free.js.map +1 -0
  296. package/package.json +75 -3
  297. package/schema/DEPLOYED +12 -0
  298. package/schema/build-diagnostic.v1.json +63 -0
  299. package/schema/component-manifest.v1.json +1000 -0
  300. package/schema/data-api.v1.json +194 -0
  301. package/schema/library-manifest.v1.json +60 -0
  302. package/schema/published/SHA256SUMS +36 -0
  303. package/schema/published/build-diagnostic/v1.10.json +83 -0
  304. package/schema/published/build-diagnostic/v1.3.json +83 -0
  305. package/schema/published/build-diagnostic/v1.4.json +83 -0
  306. package/schema/published/build-diagnostic/v1.5.json +83 -0
  307. package/schema/published/build-diagnostic/v1.6.json +83 -0
  308. package/schema/published/build-diagnostic/v1.7.json +83 -0
  309. package/schema/published/build-diagnostic/v1.8.json +83 -0
  310. package/schema/published/build-diagnostic/v1.9.json +83 -0
  311. package/schema/published/component-manifest/v1.10.json +1443 -0
  312. package/schema/published/component-manifest/v1.3.json +1411 -0
  313. package/schema/published/component-manifest/v1.4.json +1419 -0
  314. package/schema/published/component-manifest/v1.5.json +1437 -0
  315. package/schema/published/component-manifest/v1.6.json +1437 -0
  316. package/schema/published/component-manifest/v1.7.json +1437 -0
  317. package/schema/published/component-manifest/v1.8.json +1437 -0
  318. package/schema/published/component-manifest/v1.9.json +1437 -0
  319. package/schema/published/data-api/v1.10.json +282 -0
  320. package/schema/published/data-api/v1.3.json +281 -0
  321. package/schema/published/data-api/v1.4.json +281 -0
  322. package/schema/published/data-api/v1.5.json +281 -0
  323. package/schema/published/data-api/v1.6.json +281 -0
  324. package/schema/published/data-api/v1.7.json +281 -0
  325. package/schema/published/data-api/v1.8.json +282 -0
  326. package/schema/published/data-api/v1.9.json +282 -0
  327. package/schema/published/library-manifest/v1.10.json +68 -0
  328. package/schema/published/library-manifest/v1.5.json +68 -0
  329. package/schema/published/library-manifest/v1.6.json +68 -0
  330. package/schema/published/library-manifest/v1.7.json +68 -0
  331. package/schema/published/library-manifest/v1.8.json +68 -0
  332. package/schema/published/library-manifest/v1.9.json +68 -0
  333. package/schema/published/taggor-config/v1.10.json +56 -0
  334. package/schema/published/taggor-config/v1.5.json +56 -0
  335. package/schema/published/taggor-config/v1.6.json +56 -0
  336. package/schema/published/taggor-config/v1.7.json +56 -0
  337. package/schema/published/taggor-config/v1.8.json +56 -0
  338. package/schema/published/taggor-config/v1.9.json +56 -0
  339. package/schema/taggor-config.v1.json +39 -0
@@ -0,0 +1,172 @@
1
+ /**
2
+ * What each plan COSTS, the Agency site extension and the overage rates and rules (TAG-1934 P0, from
3
+ * decisions 68 and 71; TAG-1628, decision 84, replaced the cut rule of decisions 69 and 71). Zod-free, frozen, same style as `plan-usage.ts` and `plan-limits.ts`.
4
+ *
5
+ * Why it lives in the contract: the billing catalog (`apps/api/src/domain/billing/plans.ts`) and the
6
+ * public site both need these numbers, and a number written twice drifts. The billing catalog
7
+ * composes its rows from here; the site reads them through its figures registry, so no plan price
8
+ * is typed by hand in site copy.
9
+ *
10
+ * ALL NUMBERS ARE PROVISIONAL until the owner approves the plans and prices
11
+ * (docs/pricing/planos-e-limites-proposta.md v3). Money is US dollar cents (decision 68 item 1).
12
+ * Nothing here charges or cuts; it is a table.
13
+ *
14
+ * TAG-1628 (decision 84, ADR 0067): the overage rate table below (`OVERAGE_RATES`) is the ONE place
15
+ * the price of a unit above the plan is written; the site, the portal, the API and the Stripe
16
+ * prices read it. Rates are PROVISIONAL until the owner answers pendency 76.
17
+ */
18
+ import { planQuotas } from "./plan-limits.js";
19
+ import { LIMIT_CATALOG, OVERAGE_METRICS } from "./limit-catalog.js";
20
+ const money = (monthly, annual) => Object.freeze({ monthly, annual });
21
+ const table = {
22
+ free: { priceUsdCents: money(0, 0), siteExtension: null },
23
+ pro: { priceUsdCents: money(1400, 14_000), siteExtension: null },
24
+ agency: {
25
+ priceUsdCents: money(4900, 49_000),
26
+ siteExtension: Object.freeze({
27
+ includedSites: planQuotas.agency.maxSites,
28
+ maxSites: 500,
29
+ extraSitePriceUsdCents: money(200, 2000),
30
+ rendersPerExtraSite: 100_000,
31
+ submissionsPerExtraSite: 4000,
32
+ }),
33
+ },
34
+ };
35
+ /** The pricing of every plan, by plan id. Frozen. */
36
+ export const planPricing = Object.freeze({
37
+ free: Object.freeze(table.free),
38
+ pro: Object.freeze(table.pro),
39
+ agency: Object.freeze(table.agency),
40
+ });
41
+ const bothPaid = (value) => Object.freeze({ pro: value, agency: value });
42
+ /**
43
+ * THE SEED of the rate table (TAG-1629, owner 2026-11-14): the tariffs live in the database
44
+ * (`overage_rates`, versions with an effective date, edited by staff with a reason) and this table
45
+ * is only what migration 0081 inserted as version 1 and what every reader falls back to when the
46
+ * database cannot be read. A test compares it with the migration; nothing else reads it for a price.
47
+ *
48
+ * The tariffs of decision 84 (provisional, pendency 76), in micro USD per unit:
49
+ * renders US$ 0,02 per 1.000; SSR renders US$ 0,015 per 1.000; SSR version-day US$ 0,01 (no
50
+ * included amount in v1); build over the weekly limit US$ 0,01; stored submission US$ 0,25 per
51
+ * 1.000; storage US$ 0,35 per GB-month (350 per MB, on the average of the cycle's daily readings); extra site US$ 1 (Pro) and US$ 2 (Agency, the existing
52
+ * add-on price, billed on the plan subscription, so its overage rate is informational).
53
+ */
54
+ export const OVERAGE_RATES = Object.freeze({
55
+ renders: Object.freeze({ metric: "renders", unit: "unit", microUsdPerUnit: bothPaid(20) }),
56
+ ssr_renders: Object.freeze({
57
+ metric: "ssr_renders",
58
+ unit: "unit",
59
+ microUsdPerUnit: bothPaid(15),
60
+ }),
61
+ ssr_version_days: Object.freeze({
62
+ metric: "ssr_version_days",
63
+ unit: "unit",
64
+ microUsdPerUnit: bothPaid(10_000),
65
+ }),
66
+ builds: Object.freeze({ metric: "builds", unit: "unit", microUsdPerUnit: bothPaid(10_000) }),
67
+ submissions: Object.freeze({
68
+ metric: "submissions",
69
+ unit: "unit",
70
+ microUsdPerUnit: bothPaid(250),
71
+ }),
72
+ storage: Object.freeze({
73
+ metric: "storage",
74
+ unit: "mb",
75
+ microUsdPerUnit: bothPaid(350),
76
+ }),
77
+ extra_sites: Object.freeze({
78
+ metric: "extra_sites",
79
+ unit: "unit",
80
+ microUsdPerUnit: Object.freeze({ pro: 1_000_000, agency: 2_000_000 }),
81
+ }),
82
+ });
83
+ /** The metrics in a fixed order (ledger and Stripe meters follow it). */
84
+ export const OVERAGE_METRIC_LIST = OVERAGE_METRICS;
85
+ /**
86
+ * The rules around the money of an overage (decision 84, ADR 0067), numbers only:
87
+ * - `minInvoiceUsdCents`: below it the amount carries to the next month.
88
+ * - `lagHours`: the ledger bills what was counted at least this long ago, never the live count.
89
+ * - NO default spending limit (owner, 2026-11-14, TAG-1629): the customer pays what they use. A
90
+ * spending LIMIT is OPTIONAL, set by the customer or by staff, between `spendCapMinUsdCents` and
91
+ * `spendCapMaxUsdCents` (staff: `staffSpendCapMaxUsdCents`). It is a trigger, not a ceiling on the
92
+ * bill: Taggor TRIES to stop the use when the overage reaches it, WITHOUT guarantee, and what was
93
+ * really used is billed. `capNoticePercents` are the e-mails of a limit that exists.
94
+ * - Spend milestones: the customer warns themselves at amounts of overage accrued in a cycle, one
95
+ * e-mail per milestone per cycle. `spendMilestonePercents` are the named steps of the DEFAULT list
96
+ * (percent of the plan's monthly price: 50%, 100%, 200%, 500%); from there it keeps going, doubling
97
+ * from `spendMilestoneDoublingFrom` (1000%, 2000%, 4000%...) up to `maxSpendMilestones` milestones
98
+ * in all (`defaultSpendMilestonePercents`), so a runaway use keeps warning past 500% (TAG-1629
99
+ * review M4). The customer may set their own list of up to `maxSpendMilestones` amounts, each at
100
+ * most `spendMilestoneMaxUsdCents`.
101
+ * - `dunningDays`: how long an unpaid overage invoice is chased before new overage is suspended.
102
+ * - `freeCutMultiple`: Free is cut at this multiple of its number (1x, no grace).
103
+ */
104
+ export const OVERAGE_RULES = Object.freeze({
105
+ minInvoiceUsdCents: 100,
106
+ lagHours: 48,
107
+ spendCapMinUsdCents: 100,
108
+ spendCapMaxUsdCents: 1_000_000,
109
+ staffSpendCapMaxUsdCents: 10_000_000,
110
+ capNoticePercents: Object.freeze([80, 100]),
111
+ spendMilestonePercents: Object.freeze([50, 100, 200, 500]),
112
+ spendMilestoneDoublingFrom: 1000,
113
+ maxSpendMilestones: 10,
114
+ spendMilestoneMaxUsdCents: 1_000_000,
115
+ dunningDays: 14,
116
+ freeCutMultiple: 1,
117
+ });
118
+ /**
119
+ * The DEFAULT spend milestones as percents of the plan's monthly price: the named steps, then
120
+ * doubling from `spendMilestoneDoublingFrom` until `maxSpendMilestones` are listed (50, 100, 200, 500,
121
+ * 1000, 2000, 4000, 8000, 16000, 32000). Inputs: none. Output: a new ascending list. Pure.
122
+ */
123
+ export function defaultSpendMilestonePercents() {
124
+ const list = [...OVERAGE_RULES.spendMilestonePercents];
125
+ for (let next = OVERAGE_RULES.spendMilestoneDoublingFrom; list.length < OVERAGE_RULES.maxSpendMilestones; next *= 2) {
126
+ list.push(next);
127
+ }
128
+ return list;
129
+ }
130
+ /**
131
+ * The rate of a metric for a paid plan.
132
+ * Inputs: a metric and a plan id. Output: micro USD per unit, or `null` for Free (it never pays an
133
+ * overage). Errors: none. No side effects.
134
+ */
135
+ export function overageMicroUsdPerUnit(metric, planId) {
136
+ // Free has no entry in the table: the lookup, not a comparison by name, makes it unbillable.
137
+ const rates = OVERAGE_RATES[metric].microUsdPerUnit;
138
+ return rates[planId] ?? null;
139
+ }
140
+ /** The gates as the code ships them: both on (TAG-1629); frozen, the literal types are `true`. */
141
+ export const OVERAGE_BILLING_GATES = Object.freeze({
142
+ loaderHits: true,
143
+ submissions: true,
144
+ });
145
+ /**
146
+ * Whether the excess of a metric is billed automatically (and so spends the customer's cap, raises a
147
+ * cap notice and counts in the cost estimate). Renders when `gates.loaderHits`; submissions and
148
+ * storage when `gates.submissions`; builds, extra sites and SSR always.
149
+ * Inputs: a metric and the gates (default: as shipped). Output: a boolean. Pure.
150
+ */
151
+ export function isOverageMetricBilled(metric, gates = OVERAGE_BILLING_GATES) {
152
+ if (metric === "renders")
153
+ return gates.loaderHits;
154
+ if (metric === "submissions" || metric === "storage")
155
+ return gates.submissions;
156
+ return true;
157
+ }
158
+ /**
159
+ * Whether going past a catalog limit is billed automatically today on a paid plan: the limit is
160
+ * `metered` on Pro (Agency matches) and its metric is billed (`isOverageMetricBilled`). A limit that
161
+ * is `hard` on every plan (a security or per-build cap), an unknown name and a metered limit whose
162
+ * metric's gate is off are all false. The portal and the CLI
163
+ * word a limit row with it.
164
+ * Inputs: a limit name (any string) and the gates (default: as shipped). Output: a boolean. Pure.
165
+ */
166
+ export function isLimitOverageBilled(name, gates = OVERAGE_BILLING_GATES) {
167
+ const policy = LIMIT_CATALOG.find((spec) => spec.name === name)?.policy.pro;
168
+ return policy?.mode === "metered" && isOverageMetricBilled(policy.metric, gates);
169
+ }
170
+ /** The metrics billed today, in the fixed metric order: derived from the gates, never typed. */
171
+ export const BILLED_OVERAGE_METRICS = Object.freeze(OVERAGE_METRICS.filter((metric) => isOverageMetricBilled(metric)));
172
+ //# sourceMappingURL=plan-pricing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-pricing.js","sourceRoot":"","sources":["../src/plan-pricing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,eAAe,EAAsB,MAAM,oBAAoB,CAAC;AA2BxF,MAAM,KAAK,GAAG,CAAC,OAAe,EAAE,MAAc,EAAa,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;AAEjG,MAAM,KAAK,GAAG;IACZ,IAAI,EAAE,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE;IACzD,GAAG,EAAE,EAAE,aAAa,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE;IAChE,MAAM,EAAE;QACN,aAAa,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC;QAClC,aAAa,EAAE,MAAM,CAAC,MAAM,CAAC;YAC3B,aAAa,EAAE,UAAU,CAAC,MAAM,CAAC,QAAQ;YACzC,QAAQ,EAAE,GAAG;YACb,sBAAsB,EAAE,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC;YACxC,mBAAmB,EAAE,OAAO;YAC5B,uBAAuB,EAAE,IAAI;SAC9B,CAAC;KACH;CAC6C,CAAC;AAEjD,qDAAqD;AACrD,MAAM,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CAAC,CAAC,CAClE,CAAC"}
@@ -0,0 +1,112 @@
1
+ import { type PlanId } from "./plan-limits.js";
2
+ /** What one plan allows in a UTC month, and how long submissions are kept. Sizes are bytes. */
3
+ export interface PlanUsage {
4
+ /** Loader deliveries that count toward the plan (`countsTowardUsage`), the proxy of renders. */
5
+ readonly rendersPerMonth: number;
6
+ /** Server renders of `GET /r/` (cache misses the edge rendered) in the month (ADR 0066). */
7
+ readonly ssrRendersPerMonth: number;
8
+ /** Submissions accepted (not spam, not from a staging site). */
9
+ readonly submissionsPerMonth: number;
10
+ /** Bytes the submissions of the workspace hold (the JSON of `data`). */
11
+ readonly storageBytes: number;
12
+ /**
13
+ * The most days a submission lives (decision 68, item 11: 12, 24 and 36 months). A collection
14
+ * may choose fewer days, never more (TAG-713).
15
+ */
16
+ readonly retentionDays: number;
17
+ }
18
+ /** The usage allowance of every plan, by plan id. Frozen. */
19
+ export declare const planUsage: Readonly<Record<PlanId, PlanUsage>>;
20
+ /** The percentages of an allowance at which a customer is warned (decision 69: 80% and 100%). */
21
+ export declare const USAGE_WARNING_PERCENTS: readonly [80, 100];
22
+ /**
23
+ * Where a count stands against its allowance. `ok` below 80%, `warning` from 80%, `reached` from
24
+ * 100%. Nothing acts on it in this ticket; TAG-1607 adds grace and the rest.
25
+ */
26
+ export declare const USAGE_LEVELS: readonly ["ok", "warning", "reached"];
27
+ export type UsageLevel = (typeof USAGE_LEVELS)[number];
28
+ /**
29
+ * The share of an allowance a count uses.
30
+ * Inputs: the count and the limit. Output: whole percent, rounded down (so 79.9% is still 79 and
31
+ * does not warn early); 0 for a limit of 0 or less. Errors: none. No side effects.
32
+ */
33
+ export declare function usagePercent(used: number, limit: number): number;
34
+ /**
35
+ * The level of a count.
36
+ * Inputs: the count and the limit. Output: `reached` at 100% or more, `warning` at 80% or more,
37
+ * `ok` otherwise. Errors: none. No side effects.
38
+ */
39
+ export declare function usageLevel(used: number, limit: number): UsageLevel;
40
+ /**
41
+ * The rule that marks a site's render count as suspect (security review of TAG-801): the key of a
42
+ * site and its allowed domains are public, so anyone can forge loader hits with a `Referer` and a
43
+ * `User-Agent`. A day with more than `multiplier` times the mean of the `baselineDays` days before
44
+ * it, and at least `minViews`, is a spike. PROVISIONAL, owner-confirmable.
45
+ *
46
+ * What it does NOT catch: a slow ramp (each day under `multiplier` times the days before it) and
47
+ * steady traffic below `minViews` a day. `minViews` here is only the ceiling of the floor; the
48
+ * plan's own floor is `spikeMinViews`, so steady forging is bounded by the plan (at most about
49
+ * `rendersPerMonth` in a month) instead of by a fixed 1,000 a day. The guard is a brake on sudden
50
+ * spikes, not proof of authenticity: nothing may cut a component for renders without a person
51
+ * having looked (TAG-1607).
52
+ */
53
+ export declare const VIEW_SPIKE_RULE: Readonly<{
54
+ multiplier: 10;
55
+ baselineDays: 7;
56
+ /** The most a day's floor is, whatever the plan; see `spikeMinViews`. */
57
+ minViews: 1000;
58
+ }>;
59
+ /** A spike rule with its own floor. */
60
+ export interface ViewSpikeRule {
61
+ readonly multiplier: number;
62
+ readonly baselineDays: number;
63
+ readonly minViews: number;
64
+ }
65
+ /**
66
+ * The daily floor of a plan: a day below it is never a spike.
67
+ * Inputs: the plan's renders per month. Output: the lower of `VIEW_SPIKE_RULE.minViews` and a
68
+ * thirtieth of the month (at least 1), so quietly forging the plan's whole month in 30 equal days
69
+ * is a spike on the first day that passes the floor rather than going unseen. Errors: none. No
70
+ * side effects.
71
+ */
72
+ export declare function spikeMinViews(rendersPerMonth: number): number;
73
+ /**
74
+ * The spike rule of a plan. Inputs: a plan id (unknown: the default plan). Output: the rule with
75
+ * the plan's floor. Errors: none. No side effects.
76
+ */
77
+ export declare function spikeRuleOf(planId: string | undefined): ViewSpikeRule;
78
+ /** Counted deliveries of one UTC day. */
79
+ export interface DailyViews {
80
+ /** `YYYY-MM-DD`, UTC. */
81
+ readonly day: string;
82
+ readonly views: number;
83
+ }
84
+ /** A day that broke the rule. */
85
+ export interface ViewSpike {
86
+ readonly day: string;
87
+ readonly views: number;
88
+ /** The mean of the baseline days (the days of the window without data count as zero). */
89
+ readonly baselineMean: number;
90
+ }
91
+ /**
92
+ * Every day of a series that is a spike, earliest first.
93
+ * Inputs: the counted views per UTC day (any order; a missing day is zero), the current time, the
94
+ * rule and the first day to examine (default: yesterday, so today, still partial, and yesterday are
95
+ * looked at). Output: the spikes among the days from `from` to today, each day against the mean of
96
+ * the `baselineDays` days before it (possibly none). All of them, not the first: a spike a person
97
+ * already judged `legit` must not hide a later one. A caller that measures a workspace only
98
+ * every few hours passes the first day of the month, so a spike that happened between two visits
99
+ * is still found. Days before the series count as zero: give it `baselineDays` days before `from`.
100
+ * Errors: none. No side effects.
101
+ */
102
+ export declare function detectViewSpikes(series: readonly DailyViews[], now: Date, rule?: ViewSpikeRule, from?: Date): ViewSpike[];
103
+ /**
104
+ * The first spike of `detectViewSpikes`, or `null`. Same inputs. Errors: none. No side effects.
105
+ */
106
+ export declare function detectViewSpike(series: readonly DailyViews[], now: Date, rule?: ViewSpikeRule, from?: Date): ViewSpike | null;
107
+ /**
108
+ * The usage allowance of a plan, falling back to the default plan's for an unknown id.
109
+ * Inputs: a plan id or `undefined`. Output: the plan's row. Errors: none. No side effects.
110
+ */
111
+ export declare function usageOfPlan(planId: string | undefined): PlanUsage;
112
+ //# sourceMappingURL=plan-usage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-usage.d.ts","sourceRoot":"","sources":["../src/plan-usage.ts"],"names":[],"mappings":"AAYA,OAAO,EAAuC,KAAK,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAEpF,+FAA+F;AAC/F,MAAM,WAAW,SAAS;IACxB,gGAAgG;IAChG,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,4FAA4F;IAC5F,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,gEAAgE;IAChE,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;IACrC,wEAAwE;IACxE,QAAQ,CAAC,YAAY,EAAE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,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,yFAAyF;IACzF,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC/B;AAQD;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,SAAS,UAAU,EAAE,EAC7B,GAAG,EAAE,IAAI,EACT,IAAI,GAAE,aAA+B,EACrC,IAAI,CAAC,EAAE,IAAI,GACV,SAAS,EAAE,CAkBb;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,SAAS,UAAU,EAAE,EAC7B,GAAG,EAAE,IAAI,EACT,IAAI,GAAE,aAA+B,EACrC,IAAI,CAAC,EAAE,IAAI,GACV,SAAS,GAAG,IAAI,CAElB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAEjE"}
@@ -0,0 +1,134 @@
1
+ /**
2
+ * What each plan allows a workspace to USE in a month and how long it may KEEP submissions
3
+ * (TAG-712, TAG-713), plus the pure rules that read a count against that allowance. Zod-free.
4
+ *
5
+ * The numbers are a PROPOSAL the owner has not approved (docs/pricing/planos-e-limites-proposta.md,
6
+ * decisions 67 to 69): they live only in `planUsage` and in the two rule constants below, so
7
+ * TAG-1603 (the entitlement catalog) replaces this table in one place and nothing else changes.
8
+ * Measuring is all this module supports: a level here never cuts a component or a submission
9
+ * (that is TAG-1607, under decision 69), and a count of renders that looks forged
10
+ * (`detectViewSpike`) is never a ground for any action until a person has reviewed it.
11
+ */
12
+ import { catalogRows } from "./limit-catalog.js";
13
+ import { DEFAULT_PLAN_ID, PLAN_IDS, isPlanId } from "./plan-limits.js";
14
+ // The rows come from the limit catalog (`limit-catalog.ts`); the types check the `usage` group.
15
+ const table = catalogRows("usage");
16
+ /** The usage allowance of every plan, by plan id. Frozen. */
17
+ export const planUsage = Object.freeze(Object.fromEntries(PLAN_IDS.map((id) => [id, Object.freeze({ ...table[id] })])));
18
+ /** The percentages of an allowance at which a customer is warned (decision 69: 80% and 100%). */
19
+ export const USAGE_WARNING_PERCENTS = Object.freeze([80, 100]);
20
+ /**
21
+ * Where a count stands against its allowance. `ok` below 80%, `warning` from 80%, `reached` from
22
+ * 100%. Nothing acts on it in this ticket; TAG-1607 adds grace and the rest.
23
+ */
24
+ export const USAGE_LEVELS = ["ok", "warning", "reached"];
25
+ /**
26
+ * The share of an allowance a count uses.
27
+ * Inputs: the count and the limit. Output: whole percent, rounded down (so 79.9% is still 79 and
28
+ * does not warn early); 0 for a limit of 0 or less. Errors: none. No side effects.
29
+ */
30
+ export function usagePercent(used, limit) {
31
+ if (!(limit > 0) || !(used > 0))
32
+ return 0;
33
+ return Math.floor((used / limit) * 100);
34
+ }
35
+ /**
36
+ * The level of a count.
37
+ * Inputs: the count and the limit. Output: `reached` at 100% or more, `warning` at 80% or more,
38
+ * `ok` otherwise. Errors: none. No side effects.
39
+ */
40
+ export function usageLevel(used, limit) {
41
+ const percent = usagePercent(used, limit);
42
+ const [warn, reached] = USAGE_WARNING_PERCENTS;
43
+ if (percent >= reached)
44
+ return "reached";
45
+ return percent >= warn ? "warning" : "ok";
46
+ }
47
+ /**
48
+ * The rule that marks a site's render count as suspect (security review of TAG-801): the key of a
49
+ * site and its allowed domains are public, so anyone can forge loader hits with a `Referer` and a
50
+ * `User-Agent`. A day with more than `multiplier` times the mean of the `baselineDays` days before
51
+ * it, and at least `minViews`, is a spike. PROVISIONAL, owner-confirmable.
52
+ *
53
+ * What it does NOT catch: a slow ramp (each day under `multiplier` times the days before it) and
54
+ * steady traffic below `minViews` a day. `minViews` here is only the ceiling of the floor; the
55
+ * plan's own floor is `spikeMinViews`, so steady forging is bounded by the plan (at most about
56
+ * `rendersPerMonth` in a month) instead of by a fixed 1,000 a day. The guard is a brake on sudden
57
+ * spikes, not proof of authenticity: nothing may cut a component for renders without a person
58
+ * having looked (TAG-1607).
59
+ */
60
+ export const VIEW_SPIKE_RULE = Object.freeze({
61
+ multiplier: 10,
62
+ baselineDays: 7,
63
+ /** The most a day's floor is, whatever the plan; see `spikeMinViews`. */
64
+ minViews: 1000,
65
+ });
66
+ /**
67
+ * The daily floor of a plan: a day below it is never a spike.
68
+ * Inputs: the plan's renders per month. Output: the lower of `VIEW_SPIKE_RULE.minViews` and a
69
+ * thirtieth of the month (at least 1), so quietly forging the plan's whole month in 30 equal days
70
+ * is a spike on the first day that passes the floor rather than going unseen. Errors: none. No
71
+ * side effects.
72
+ */
73
+ export function spikeMinViews(rendersPerMonth) {
74
+ return Math.max(1, Math.min(VIEW_SPIKE_RULE.minViews, Math.floor(rendersPerMonth / 30)));
75
+ }
76
+ /**
77
+ * The spike rule of a plan. Inputs: a plan id (unknown: the default plan). Output: the rule with
78
+ * the plan's floor. Errors: none. No side effects.
79
+ */
80
+ export function spikeRuleOf(planId) {
81
+ return Object.freeze({
82
+ ...VIEW_SPIKE_RULE,
83
+ minViews: spikeMinViews(usageOfPlan(planId).rendersPerMonth),
84
+ });
85
+ }
86
+ const DAY_MS = 24 * 60 * 60 * 1000;
87
+ function dayKey(time) {
88
+ return new Date(time).toISOString().slice(0, 10);
89
+ }
90
+ /**
91
+ * Every day of a series that is a spike, earliest first.
92
+ * Inputs: the counted views per UTC day (any order; a missing day is zero), the current time, the
93
+ * rule and the first day to examine (default: yesterday, so today, still partial, and yesterday are
94
+ * looked at). Output: the spikes among the days from `from` to today, each day against the mean of
95
+ * the `baselineDays` days before it (possibly none). All of them, not the first: a spike a person
96
+ * already judged `legit` must not hide a later one. A caller that measures a workspace only
97
+ * every few hours passes the first day of the month, so a spike that happened between two visits
98
+ * is still found. Days before the series count as zero: give it `baselineDays` days before `from`.
99
+ * Errors: none. No side effects.
100
+ */
101
+ export function detectViewSpikes(series, now, rule = VIEW_SPIKE_RULE, from) {
102
+ const byDay = new Map(series.map((entry) => [entry.day, entry.views]));
103
+ const today = Math.floor(now.getTime() / DAY_MS) * DAY_MS;
104
+ const first = from === undefined ? today - DAY_MS : Math.floor(from.getTime() / DAY_MS) * DAY_MS;
105
+ const spikes = [];
106
+ for (let at = first; at <= today; at += DAY_MS) {
107
+ const views = byDay.get(dayKey(at)) ?? 0;
108
+ if (views < rule.minViews)
109
+ continue;
110
+ let sum = 0;
111
+ for (let back = 1; back <= rule.baselineDays; back += 1) {
112
+ sum += byDay.get(dayKey(at - back * DAY_MS)) ?? 0;
113
+ }
114
+ const baselineMean = sum / rule.baselineDays;
115
+ if (views > rule.multiplier * baselineMean) {
116
+ spikes.push({ day: dayKey(at), views, baselineMean });
117
+ }
118
+ }
119
+ return spikes;
120
+ }
121
+ /**
122
+ * The first spike of `detectViewSpikes`, or `null`. Same inputs. Errors: none. No side effects.
123
+ */
124
+ export function detectViewSpike(series, now, rule = VIEW_SPIKE_RULE, from) {
125
+ return detectViewSpikes(series, now, rule, from)[0] ?? null;
126
+ }
127
+ /**
128
+ * The usage allowance of a plan, falling back to the default plan's for an unknown id.
129
+ * Inputs: a plan id or `undefined`. Output: the plan's row. Errors: none. No side effects.
130
+ */
131
+ export function usageOfPlan(planId) {
132
+ return planUsage[isPlanId(planId) ? planId : DEFAULT_PLAN_ID];
133
+ }
134
+ //# sourceMappingURL=plan-usage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-usage.js","sourceRoot":"","sources":["../src/plan-usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,QAAQ,EAAe,MAAM,kBAAkB,CAAC;AAmBpF,gGAAgG;AAChG,MAAM,KAAK,GAA8B,WAAW,CAAC,OAAO,CAAC,CAAC;AAE9D,6DAA6D;AAC7D,MAAM,CAAC,MAAM,SAAS,GAAwC,MAAM,CAAC,MAAM,CACzE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,CA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I,MAAM,EAAE,CAAC;QAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,KAAK,GAAG,IAAI,CAAC,QAAQ;YAAE,SAAS;QACpC,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,IAAI,IAAI,CAAC,YAAY,EAAE,IAAI,IAAI,CAAC,EAAE,CAAC;YACxD,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,GAAG,IAAI,GAAG,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC;QACpD,CAAC;QACD,MAAM,YAAY,GAAG,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC;QAC7C,IAAI,KAAK,GAAG,IAAI,CAAC,UAAU,GAAG,YAAY,EAAE,CAAC;YAC3C,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAC7B,MAA6B,EAC7B,GAAS,EACT,OAAsB,eAAe,EACrC,IAAW;IAEX,OAAO,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAC9D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,MAA0B;IACpD,OAAO,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC;AAChE,CAAC"}
@@ -0,0 +1,52 @@
1
+ /** How long a preview link lives after its build finished: seven days. */
2
+ export declare const PREVIEW_TTL_MS: number;
3
+ /** What a token says. */
4
+ export interface PreviewClaims {
5
+ /** The build the link was issued for (`bld_…` public id). */
6
+ readonly buildId: string;
7
+ /** SHA-256 of the build's bundle: the only object the link opens. */
8
+ readonly bundleSha256: string;
9
+ /**
10
+ * The contract version the build's manifest declares (`1.4.0`): the bundle's own definition has
11
+ * none until extraction, and the runtime refuses to mount a component without it, exactly as
12
+ * the loader gets it from the published manifest.
13
+ */
14
+ readonly contractVersion: string;
15
+ /** Whole seconds since the epoch; the link stops working at this instant. */
16
+ readonly expiresAt: number;
17
+ }
18
+ /** Port: the HMAC-SHA-256 under the signing key (Web Crypto in both Workers). */
19
+ export interface PreviewSigner {
20
+ /** The raw MAC of `message`. Errors: rejects when Web Crypto is unavailable. */
21
+ sign(message: string): Promise<Uint8Array>;
22
+ /** Whether `tag` is the MAC of `message`, compared in constant time. */
23
+ verify(message: string, tag: Uint8Array): Promise<boolean>;
24
+ }
25
+ /** Longest token `verifyPreviewToken` looks at; anything longer is refused unread. */
26
+ export declare const PREVIEW_TOKEN_MAX_LENGTH = 256;
27
+ /**
28
+ * The expiry of a build's link.
29
+ * Inputs: when the build finished, in milliseconds since the epoch. Output: whole seconds since
30
+ * the epoch, `PREVIEW_TTL_MS` later (rounded up). Errors: none. Side effects: none.
31
+ */
32
+ export declare function previewExpiry(finishedAtMs: number): number;
33
+ /**
34
+ * Whether claims are well formed: a build id, a SHA-256 and a positive whole-second expiry.
35
+ * Inputs: any claims. Output: a boolean. Errors: none. Side effects: none.
36
+ */
37
+ export declare function isPreviewClaims(claims: PreviewClaims): boolean;
38
+ /**
39
+ * Signs a token.
40
+ * Inputs: the signer and the claims. Output: the token. Errors: `TypeError` for malformed
41
+ * claims (a bug in the caller); rejects with the signer. Side effects: none.
42
+ */
43
+ export declare function issuePreviewToken(signer: PreviewSigner, claims: PreviewClaims): Promise<string>;
44
+ /**
45
+ * Verifies a token taken from a URL.
46
+ * Inputs: the signer, the token (untrusted) and the current time in milliseconds. Output: the
47
+ * claims, or `undefined` for a token that is malformed, tampered with, signed under another key
48
+ * or expired: the caller answers all of them with the same 404. Errors: none thrown; a signer
49
+ * failure also gives `undefined` (fail closed). Side effects: none.
50
+ */
51
+ export declare function verifyPreviewToken(signer: PreviewSigner, token: string, nowMs: number): Promise<PreviewClaims | undefined>;
52
+ //# sourceMappingURL=preview-token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preview-token.d.ts","sourceRoot":"","sources":["../src/preview-token.ts"],"names":[],"mappings":"AAiBA,0EAA0E;AAC1E,eAAO,MAAM,cAAc,QAA0B,CAAC;AAEtD,yBAAyB;AACzB,MAAM,WAAW,aAAa;IAC5B,6DAA6D;IAC7D,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,6EAA6E;IAC7E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,iFAAiF;AACjF,MAAM,WAAW,aAAa;IAC5B,gFAAgF;IAChF,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C,wEAAwE;IACxE,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC5D;AASD,sFAAsF;AACtF,eAAO,MAAM,wBAAwB,MAAM,CAAC;AAW5C;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAQ9D;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,MAAM,CAAC,CAWjB;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CA6BpC"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * The private preview token (TAG-415): the capability in `https://<preview host>/p/<token>` that
3
+ * lets anyone holding the link see ONE build's bundle for seven days. The API signs it when a
4
+ * client reads a succeeded build of its own workspace; the edge verifies it without a database
5
+ * (threat T-57: the edge has no Postgres binding, so the token carries everything it needs).
6
+ * Pure and import-free on purpose, like the other files the API and the edge share: the HMAC
7
+ * itself is injected (`PreviewSigner`), each Worker brings its own Web Crypto adapter.
8
+ *
9
+ * Format: `v1.<buildId>.<bundleSha256>.<contractVersion with dashes>.<expiresAt>.<mac>`; `expiresAt` is whole seconds since the
10
+ * epoch, `mac` is the hex HMAC-SHA-256 of `taggor-preview-v1\n<buildId>\n<bundleSha256>\n<contractVersion>\n<expiresAt>`
11
+ * under the preview signing key (a secret of the API and the edge). Every field is validated against a fixed pattern
12
+ * before the MAC is checked or the value is used, so a token never reaches an R2 key or a page
13
+ * with a character the patterns do not allow. The expiry is the build's finish time plus seven
14
+ * days (`previewExpiry`), so the same build always gets the same link.
15
+ */
16
+ import { SHA256_HEX_PATTERN } from "./artifact-keys.js";
17
+ /** How long a preview link lives after its build finished: seven days. */
18
+ export const PREVIEW_TTL_MS = 7 * 24 * 60 * 60 * 1000;
19
+ const VERSION = "v1";
20
+ const BUILD_ID = /^[a-z]{2,8}_[0-9a-z]{10,40}$/;
21
+ const SHA256 = new RegExp(SHA256_HEX_PATTERN);
22
+ const CONTRACT_VERSION = /^(0|[1-9][0-9]{0,2})\.(0|[1-9][0-9]{0,2})\.(0|[1-9][0-9]{0,2})$/;
23
+ const EXPIRY = /^[1-9][0-9]{0,11}$/;
24
+ const MAC_HEX = /^[0-9a-f]{64}$/;
25
+ /** Longest token `verifyPreviewToken` looks at; anything longer is refused unread. */
26
+ export const PREVIEW_TOKEN_MAX_LENGTH = 256;
27
+ const toHex = (bytes) => [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
28
+ const fromHex = (hex) => Uint8Array.from(hex.match(/../g) ?? [], (pair) => Number.parseInt(pair, 16));
29
+ const messageOf = (claims) => `taggor-preview-${VERSION}\n${claims.buildId}\n${claims.bundleSha256}\n${claims.contractVersion}\n${String(claims.expiresAt)}`;
30
+ /**
31
+ * The expiry of a build's link.
32
+ * Inputs: when the build finished, in milliseconds since the epoch. Output: whole seconds since
33
+ * the epoch, `PREVIEW_TTL_MS` later (rounded up). Errors: none. Side effects: none.
34
+ */
35
+ export function previewExpiry(finishedAtMs) {
36
+ return Math.ceil((finishedAtMs + PREVIEW_TTL_MS) / 1000);
37
+ }
38
+ /**
39
+ * Whether claims are well formed: a build id, a SHA-256 and a positive whole-second expiry.
40
+ * Inputs: any claims. Output: a boolean. Errors: none. Side effects: none.
41
+ */
42
+ export function isPreviewClaims(claims) {
43
+ return (BUILD_ID.test(claims.buildId) &&
44
+ SHA256.test(claims.bundleSha256) &&
45
+ CONTRACT_VERSION.test(claims.contractVersion) &&
46
+ Number.isSafeInteger(claims.expiresAt) &&
47
+ EXPIRY.test(String(claims.expiresAt)));
48
+ }
49
+ /**
50
+ * Signs a token.
51
+ * Inputs: the signer and the claims. Output: the token. Errors: `TypeError` for malformed
52
+ * claims (a bug in the caller); rejects with the signer. Side effects: none.
53
+ */
54
+ export async function issuePreviewToken(signer, claims) {
55
+ if (!isPreviewClaims(claims))
56
+ throw new TypeError("malformed preview claims");
57
+ const mac = toHex(await signer.sign(messageOf(claims)));
58
+ return [
59
+ VERSION,
60
+ claims.buildId,
61
+ claims.bundleSha256,
62
+ claims.contractVersion.replaceAll(".", "-"),
63
+ String(claims.expiresAt),
64
+ mac,
65
+ ].join(".");
66
+ }
67
+ /**
68
+ * Verifies a token taken from a URL.
69
+ * Inputs: the signer, the token (untrusted) and the current time in milliseconds. Output: the
70
+ * claims, or `undefined` for a token that is malformed, tampered with, signed under another key
71
+ * or expired: the caller answers all of them with the same 404. Errors: none thrown; a signer
72
+ * failure also gives `undefined` (fail closed). Side effects: none.
73
+ */
74
+ export async function verifyPreviewToken(signer, token, nowMs) {
75
+ if (token.length > PREVIEW_TOKEN_MAX_LENGTH)
76
+ return undefined;
77
+ const parts = token.split(".");
78
+ const [version, buildId, bundleSha256, dashed, expiry, mac] = parts;
79
+ if (parts.length !== 6 ||
80
+ version !== VERSION ||
81
+ buildId === undefined ||
82
+ bundleSha256 === undefined ||
83
+ dashed === undefined ||
84
+ expiry === undefined ||
85
+ mac === undefined ||
86
+ !MAC_HEX.test(mac)) {
87
+ return undefined;
88
+ }
89
+ const claims = {
90
+ buildId,
91
+ bundleSha256,
92
+ contractVersion: dashed.replaceAll("-", "."),
93
+ expiresAt: Number(expiry),
94
+ };
95
+ if (!isPreviewClaims(claims) || String(claims.expiresAt) !== expiry)
96
+ return undefined;
97
+ try {
98
+ if (!(await signer.verify(messageOf(claims), fromHex(mac))))
99
+ return undefined;
100
+ }
101
+ catch {
102
+ return undefined;
103
+ }
104
+ return claims.expiresAt * 1000 > nowMs ? claims : undefined;
105
+ }
106
+ //# sourceMappingURL=preview-token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preview-token.js","sourceRoot":"","sources":["../src/preview-token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAExD,0EAA0E;AAC1E,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AA0BtD,MAAM,OAAO,GAAG,IAAI,CAAC;AACrB,MAAM,QAAQ,GAAG,8BAA8B,CAAC;AAChD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAC9C,MAAM,gBAAgB,GAAG,iEAAiE,CAAC;AAC3F,MAAM,MAAM,GAAG,oBA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