@fabrika/app 0.6.0 → 0.8.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 (82) hide show
  1. package/package.json +10 -9
  2. package/skill/SKILL.md +29 -21
  3. package/skill/references/app-setup.md +58 -36
  4. package/skill/references/dev.md +11 -9
  5. package/skill/references/endpoint.md +154 -25
  6. package/skill/references/errors.md +26 -23
  7. package/skill/references/files.md +187 -137
  8. package/skill/references/jobs.md +82 -35
  9. package/skill/references/public-intake.md +450 -255
  10. package/skill/references/spa.md +6 -5
  11. package/skill/references/start.md +2 -1
  12. package/skill/references/table-and-migration.md +24 -2
  13. package/skill/references/tests.md +14 -8
  14. package/skill/references/webhooks-links-connectors.md +65 -12
  15. package/src/bun.ts +3 -2
  16. package/src/client/contract.ts +221 -79
  17. package/src/client/index.ts +3 -1
  18. package/src/client/transport.ts +7 -2
  19. package/src/cloudflare.ts +2 -0
  20. package/src/contract/artifact-schema.ts +168 -14
  21. package/src/contract/build.ts +1282 -619
  22. package/src/contract/compile.ts +44 -10
  23. package/src/contract/connect.ts +79 -5
  24. package/src/contract/context.ts +132 -23
  25. package/src/contract/declaration.ts +78 -63
  26. package/src/contract/defaults.ts +142 -60
  27. package/src/contract/define.ts +92 -121
  28. package/src/contract/file.ts +78 -0
  29. package/src/contract/group.ts +244 -37
  30. package/src/contract/index.ts +18 -0
  31. package/src/contract/kit.ts +206 -126
  32. package/src/contract/platform.ts +8 -9
  33. package/src/contract/redirect.ts +49 -0
  34. package/src/contract/schema-from-shape.ts +7 -0
  35. package/src/contract/schema-shape.ts +10 -0
  36. package/src/contract/snapshot-source.ts +5 -0
  37. package/src/contract/token.ts +89 -0
  38. package/src/contract/webhook-binding.ts +26 -0
  39. package/src/contract/webhook-scheme.ts +32 -0
  40. package/src/files/handle.ts +22 -0
  41. package/src/files/kinds.ts +454 -0
  42. package/src/files/scan.ts +15 -9
  43. package/src/files/streams.ts +111 -0
  44. package/src/honeypot.ts +7 -0
  45. package/src/implement/anonymous.ts +18 -42
  46. package/src/implement/boot.ts +210 -157
  47. package/src/implement/bounded.ts +23 -0
  48. package/src/implement/browser-config.ts +6 -5
  49. package/src/implement/capability.ts +78 -2
  50. package/src/implement/challenge.ts +203 -47
  51. package/src/implement/{form-admission.ts → client-address.ts} +3 -38
  52. package/src/implement/dispatch.ts +831 -409
  53. package/src/implement/egress.ts +84 -0
  54. package/src/implement/faults.ts +11 -1
  55. package/src/implement/index.ts +3 -2
  56. package/src/implement/invocation.ts +135 -35
  57. package/src/implement/params.ts +88 -0
  58. package/src/implement/principal.ts +7 -0
  59. package/src/implement/public.ts +167 -0
  60. package/src/implement/tokens.ts +219 -0
  61. package/src/implement/types.ts +24 -3
  62. package/src/implement/urls.ts +78 -0
  63. package/src/implement/verifiers.ts +299 -0
  64. package/src/implement/webhook.ts +136 -55
  65. package/src/implement/workload.ts +16 -14
  66. package/src/index.ts +3 -0
  67. package/src/jobs/mail.ts +98 -10
  68. package/src/jobs/runner.ts +103 -71
  69. package/src/jobs/scrub.ts +4 -1
  70. package/src/jobs/types.ts +19 -2
  71. package/src/react/hooks.ts +2 -2
  72. package/src/react/provider.tsx +2 -2
  73. package/src/testing/challenge.ts +35 -1
  74. package/src/testing/dev.ts +2 -2
  75. package/src/testing/endpoint-access.ts +168 -128
  76. package/src/testing/kit.ts +90 -25
  77. package/src/testing/policy-isolation.ts +55 -29
  78. package/src/testing/proxy.ts +6 -2
  79. package/src/testing/sample-file.ts +30 -0
  80. package/src/client/files.ts +0 -135
  81. package/src/contract/file-schemas.ts +0 -25
  82. package/src/files/collections.ts +0 -848
@@ -1,21 +1,34 @@
1
1
  // What the anonymous kinds need beyond the request pipeline: a body read under its limit (the proxy never sees a
2
- // body, so the framework enforces it), the share SPA's fragment bootstrap and GitHub's delivery signature. A form's
3
- // admission is `form-admission.ts`.
2
+ // body, so the framework enforces it) and the share SPA's fragment bootstrap. A form's admission is
3
+ // `form-admission.ts`, a webhook's signature `verifiers.ts`.
4
4
 
5
5
  import { LINK_BOOTSTRAP_FILE, SHARE_KEY_GLOBAL } from '../contract/defaults'
6
6
  import { HttpError } from '../errors'
7
7
  import { readBoundedBytes } from './bounded'
8
8
 
9
+ /** The slowest body a procedure waits for, in bytes per second: its deadline is `maxBytes` at this rate, 30 s at least. */
10
+ export const BODY_RATE_FLOOR = 16 * 1024
11
+ const MIN_BODY_DEADLINE_MS = 30_000
12
+
13
+ /** How long a body of at most `maxBytes` may take to arrive. */
14
+ export function bodyDeadlineMs(maxBytes: number): number {
15
+ return Math.max(MIN_BODY_DEADLINE_MS, Math.ceil((maxBytes / BODY_RATE_FLOOR) * 1000))
16
+ }
17
+
9
18
  /**
10
19
  * The body as bytes, read until `maxBytes`; a longer one stops the read and is a 413. A body whose stream fails before
11
20
  * its end (the client closed the connection) is the sender's 400 `aborted`, never a failure of the application.
12
21
  */
13
- export async function readBoundedBody(request: Request, maxBytes: number): Promise<Uint8Array<ArrayBuffer>> {
22
+ export async function readBoundedBody(request: Request, maxBytes: number, deadline?: AbortSignal): Promise<Uint8Array<ArrayBuffer>> {
14
23
  let body: Uint8Array<ArrayBuffer> | null
15
24
  try {
16
- body = await readBoundedBytes(request.body, { maxBytes, declaredLength: request.headers.get('Content-Length') })
25
+ body = await readBoundedBytes(request.body, {
26
+ maxBytes,
27
+ declaredLength: request.headers.get('Content-Length'),
28
+ ...(deadline === undefined ? {} : { signal: deadline }),
29
+ })
17
30
  } catch {
18
- throw abortedBody()
31
+ throw deadline?.aborted === true ? new HttpError(408, 'body_timeout', 'The body did not arrive within its deadline') : abortedBody()
19
32
  }
20
33
  if (body === null) {
21
34
  throw new HttpError(413, 'too_large', `The body is larger than ${maxBytes} bytes`)
@@ -76,40 +89,3 @@ export async function withBootstrap(response: Response, prefix: string): Promise
76
89
  headers.delete('Content-Length')
77
90
  return new Response(injected, { status: response.status, statusText: response.statusText, headers })
78
91
  }
79
-
80
- const SIGNATURE_PREFIX = 'sha256='
81
- const HEX = /^[0-9a-f]{64}$/
82
-
83
- /** Whether `X-Hub-Signature-256` is GitHub's HMAC-SHA256 of the raw body under `secret`, compared in constant time. */
84
- export async function verifyGithubSignature(header: string | null, secret: string, body: Uint8Array<ArrayBuffer>): Promise<boolean> {
85
- if (header === null || !header.startsWith(SIGNATURE_PREFIX)) {
86
- return false
87
- }
88
- const hex = header.slice(SIGNATURE_PREFIX.length).toLowerCase()
89
- if (!HEX.test(hex)) {
90
- return false
91
- }
92
- const signature = new Uint8Array(32)
93
- for (let index = 0; index < 32; index++) {
94
- signature[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16)
95
- }
96
- const key = await crypto.subtle.importKey('raw', new TextEncoder().encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['verify'])
97
- return crypto.subtle.verify('HMAC', key, signature, body)
98
- }
99
-
100
- /** The GitHub account a payload names, as a binding records it: `installation:<id>` or `repository:<id>`. */
101
- export function githubAccounts(payload: unknown): readonly string[] {
102
- const accounts: string[] = []
103
- for (const kind of ['installation', 'repository'] as const) {
104
- const holder = ownField(payload, kind)
105
- const id = ownField(holder, 'id')
106
- if (typeof id === 'number' && Number.isSafeInteger(id) && id > 0) {
107
- accounts.push(`${kind}:${id}`)
108
- }
109
- }
110
- return accounts
111
- }
112
-
113
- function ownField(value: unknown, name: string): unknown {
114
- return typeof value === 'object' && value !== null && Object.hasOwn(value, name) ? Reflect.get(value, name) : undefined
115
- }