@kaperkunde/tokens 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 (50) hide show
  1. package/README.md +415 -2
  2. package/dist/browser-api.d.mts +56 -0
  3. package/dist/browser-api.mjs +75 -0
  4. package/dist/errors.d.mts +41 -0
  5. package/dist/errors.mjs +97 -0
  6. package/dist/index.d.mts +5 -0
  7. package/dist/index.mjs +5 -0
  8. package/dist/react/autorefill.d.mts +21 -0
  9. package/dist/react/autorefill.mjs +269 -0
  10. package/dist/react/balance.d.mts +23 -0
  11. package/dist/react/balance.mjs +116 -0
  12. package/dist/react/buy.d.mts +27 -0
  13. package/dist/react/buy.mjs +175 -0
  14. package/dist/react/history.d.mts +23 -0
  15. package/dist/react/history.mjs +102 -0
  16. package/dist/react/hooks.d.mts +52 -0
  17. package/dist/react/hooks.mjs +150 -0
  18. package/dist/react/index.d.mts +11 -0
  19. package/dist/react/index.mjs +10 -0
  20. package/dist/react/labels.d.mts +114 -0
  21. package/dist/react/labels.mjs +125 -0
  22. package/dist/react/own-model.d.mts +31 -0
  23. package/dist/react/own-model.mjs +358 -0
  24. package/dist/react/ui.d.mts +61 -0
  25. package/dist/react/ui.mjs +128 -0
  26. package/dist/react/usage.d.mts +21 -0
  27. package/dist/react/usage.mjs +137 -0
  28. package/dist/server/client.d.mts +76 -0
  29. package/dist/server/client.mjs +137 -0
  30. package/dist/server/error-of.d.mts +12 -0
  31. package/dist/server/error-of.mjs +63 -0
  32. package/dist/server/gateway.d.mts +62 -0
  33. package/dist/server/gateway.mjs +110 -0
  34. package/dist/server/index.d.mts +7 -0
  35. package/dist/server/index.mjs +7 -0
  36. package/dist/server/node.d.mts +23 -0
  37. package/dist/server/node.mjs +81 -0
  38. package/dist/server/routes.d.mts +56 -0
  39. package/dist/server/routes.mjs +257 -0
  40. package/dist/server/webhook.d.mts +47 -0
  41. package/dist/server/webhook.mjs +99 -0
  42. package/dist/styles.css +380 -0
  43. package/dist/types.d.mts +635 -0
  44. package/dist/types.mjs +142 -0
  45. package/dist/units.d.mts +37 -0
  46. package/dist/units.mjs +76 -0
  47. package/package.json +73 -4
  48. package/stub/cli.mjs +8 -0
  49. package/stub/server.d.mts +36 -0
  50. package/stub/server.mjs +1385 -0
@@ -0,0 +1,99 @@
1
+ import { TOKEN_HEADERS, WEBHOOK_EVENT_TYPES } from "../types.mjs";
2
+ import { createHmac, timingSafeEqual } from "node:crypto";
3
+ // src/server/webhook.ts
4
+ /** The delivery is not from the center, is too old, or is not an event. Answer 400. */
5
+ var WebhookVerificationError = class extends Error {
6
+ reason;
7
+ constructor(reason, message) {
8
+ super(message);
9
+ this.name = "WebhookVerificationError";
10
+ this.reason = reason;
11
+ }
12
+ };
13
+ /** `t=<unix>,v1=<hex HMAC-SHA256(secret, "<t>.<rawBody>")>`, as the center signs it. */
14
+ function signWebhook(rawBody, secret, timestamp = Math.floor(Date.now() / 1e3)) {
15
+ return `t=${timestamp},v1=${createHmac("sha256", secret).update(`${timestamp}.${rawBody}`).digest("hex")}`;
16
+ }
17
+ /**
18
+ * Check a delivery and return its typed event. `rawBody` must be the bytes as
19
+ * received (not re-serialised JSON); `signatureHeader` the `token-signature`
20
+ * header; `secret` the app's webhook secret (an array while rotating: any one
21
+ * that matches is enough). Throws `WebhookVerificationError` on a malformed
22
+ * header, a wrong signature (constant-time compare), a timestamp more than the
23
+ * tolerance from now, or a body that is not a known event.
24
+ *
25
+ * Deliveries repeat and arrive out of order: handlers dedupe on `event.id`.
26
+ */
27
+ function verifyWebhook(rawBody, signatureHeader, secret, options = {}) {
28
+ const body = typeof rawBody === "string" ? rawBody : Buffer.from(rawBody).toString("utf8");
29
+ if (!signatureHeader) throw new WebhookVerificationError("malformed", "Missing token-signature header.");
30
+ let timestamp = NaN;
31
+ const candidates = [];
32
+ for (const part of signatureHeader.split(",")) {
33
+ const [key, ...rest] = part.trim().split("=");
34
+ const value = rest.join("=");
35
+ if (key === "t") timestamp = Number(value);
36
+ else if (key === "v1" && value) candidates.push(value);
37
+ }
38
+ if (!Number.isInteger(timestamp) || candidates.length === 0) throw new WebhookVerificationError("malformed", "Malformed token-signature header.");
39
+ const secrets = typeof secret === "string" ? [secret] : secret;
40
+ let matched = false;
41
+ for (const s of secrets) {
42
+ const expected = createHmac("sha256", s).update(`${timestamp}.${body}`).digest();
43
+ for (const candidate of candidates) {
44
+ const given = Buffer.from(candidate, "hex");
45
+ if (given.length === expected.length && timingSafeEqual(given, expected)) matched = true;
46
+ }
47
+ }
48
+ if (!matched) throw new WebhookVerificationError("signature", "Signature does not match.");
49
+ const now = options.now ?? Math.floor(Date.now() / 1e3);
50
+ const tolerance = options.toleranceSeconds ?? 300;
51
+ if (Math.abs(now - timestamp) > tolerance) throw new WebhookVerificationError("stale", "Signature timestamp is outside the tolerance.");
52
+ return parseWebhookEvent(body);
53
+ }
54
+ /** A body that is a known webhook event, typed; throws `WebhookVerificationError("body")` otherwise. */
55
+ function parseWebhookEvent(body) {
56
+ let value = body;
57
+ if (typeof body === "string") try {
58
+ value = JSON.parse(body);
59
+ } catch {
60
+ throw new WebhookVerificationError("body", "Body is not JSON.");
61
+ }
62
+ const e = value;
63
+ if (!e || typeof e !== "object" || typeof e.id !== "string" || typeof e.type !== "string" || typeof e.data !== "object" || e.data === null || !WEBHOOK_EVENT_TYPES.includes(e.type)) throw new WebhookVerificationError("body", "Body is not a known event.");
64
+ return e;
65
+ }
66
+ /**
67
+ * A Web `Request → Response` handler for the center's webhook: verifies, calls
68
+ * the handler for the type, answers 200 (even for a type with no handler),
69
+ * 400 for a bad signature or body and 500 when a handler throws (the center
70
+ * retries 1 m … 24 h, so a handler must be idempotent on `event.id`).
71
+ */
72
+ function webhookRoute(options) {
73
+ return async (request) => {
74
+ if (request.method !== "POST") return new Response("Method not allowed", { status: 405 });
75
+ const rawBody = await request.text();
76
+ let event;
77
+ try {
78
+ event = verifyWebhook(rawBody, request.headers.get(TOKEN_HEADERS.signature), typeof options.secret === "function" ? options.secret() : options.secret, { toleranceSeconds: options.toleranceSeconds });
79
+ } catch (error) {
80
+ if (error instanceof WebhookVerificationError) return Response.json({
81
+ error: error.reason,
82
+ message: error.message
83
+ }, { status: 400 });
84
+ throw error;
85
+ }
86
+ try {
87
+ const handler = options.handlers[event.type];
88
+ await handler?.(event);
89
+ } catch (error) {
90
+ options.onError?.(error, event);
91
+ return Response.json({ error: "handler_failed" }, { status: 500 });
92
+ }
93
+ return Response.json({
94
+ received: true,
95
+ id: event.id
96
+ });
97
+ };
98
+ }
99
+ export { WebhookVerificationError, parseWebhookEvent, signWebhook, verifyWebhook, webhookRoute };
@@ -0,0 +1,380 @@
1
+ /*
2
+ * @kaperkunde/tokens: a neutral default look for the components.
3
+ *
4
+ * Everything the components draw is set by the --tk-* variables below and the
5
+ * stable `tk-*` class names. Import this file for the default theme, or skip it
6
+ * and style the class names yourself. To use your own tokens, set the
7
+ * variables where you like (the defaults sit on `:where(:root)`, specificity
8
+ * zero, so any rule of yours wins):
9
+ *
10
+ * .my-app { --tk-fg: var(--app-text); --tk-accent: var(--app-accent); … }
11
+ *
12
+ * Variables
13
+ * --tk-font font family
14
+ * --tk-fg text
15
+ * --tk-muted secondary text
16
+ * --tk-bg the page behind a panel (used for inputs)
17
+ * --tk-surface panels and rows
18
+ * --tk-border lines around panels, inputs and buttons
19
+ * --tk-accent primary buttons, the pressed segment, links
20
+ * --tk-accent-fg text on --tk-accent
21
+ * --tk-danger errors and destructive buttons
22
+ * --tk-ok success notices
23
+ * --tk-focus the focus ring
24
+ * --tk-radius corner radius
25
+ * --tk-gap the spacing unit between things in a panel
26
+ * --tk-tap the least height of a pressable thing (44px)
27
+ */
28
+
29
+ :where(:root) {
30
+ --tk-font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
31
+ --tk-fg: #1b1b1f;
32
+ --tk-muted: #5f6368;
33
+ --tk-bg: #ffffff;
34
+ --tk-surface: #f6f6f8;
35
+ --tk-border: #d3d4da;
36
+ --tk-accent: #3b5bdb;
37
+ --tk-accent-fg: #ffffff;
38
+ --tk-danger: #c92a2a;
39
+ --tk-ok: #2b8a3e;
40
+ --tk-focus: #3b5bdb;
41
+ --tk-radius: 8px;
42
+ --tk-gap: 12px;
43
+ --tk-tap: 44px;
44
+ }
45
+
46
+ @media (prefers-color-scheme: dark) {
47
+ :where(:root:not([data-theme="light"])) {
48
+ --tk-fg: #ececf1;
49
+ --tk-muted: #a0a3ad;
50
+ --tk-bg: #15161a;
51
+ --tk-surface: #1e2026;
52
+ --tk-border: #3a3d47;
53
+ --tk-accent: #7c93f5;
54
+ --tk-accent-fg: #0f1118;
55
+ --tk-danger: #ff8787;
56
+ --tk-ok: #69db7c;
57
+ --tk-focus: #7c93f5;
58
+ }
59
+ }
60
+
61
+ :where(:root[data-theme="dark"]) {
62
+ --tk-fg: #ececf1;
63
+ --tk-muted: #a0a3ad;
64
+ --tk-bg: #15161a;
65
+ --tk-surface: #1e2026;
66
+ --tk-border: #3a3d47;
67
+ --tk-accent: #7c93f5;
68
+ --tk-accent-fg: #0f1118;
69
+ --tk-danger: #ff8787;
70
+ --tk-ok: #69db7c;
71
+ --tk-focus: #7c93f5;
72
+ }
73
+
74
+ .tk-panel,
75
+ .tk-balance,
76
+ .tk-form,
77
+ .tk-list {
78
+ font-family: var(--tk-font);
79
+ color: var(--tk-fg);
80
+ box-sizing: border-box;
81
+ }
82
+
83
+ .tk-panel {
84
+ background: var(--tk-surface);
85
+ border: 1px solid var(--tk-border);
86
+ border-radius: var(--tk-radius);
87
+ padding: var(--tk-gap);
88
+ display: flex;
89
+ flex-direction: column;
90
+ gap: var(--tk-gap);
91
+ max-width: 100%;
92
+ min-width: 0;
93
+ }
94
+
95
+ .tk-panel > * {
96
+ margin: 0;
97
+ }
98
+
99
+ .tk-title {
100
+ font-size: 1.1rem;
101
+ font-weight: 600;
102
+ }
103
+
104
+ .tk-muted,
105
+ .tk-hint,
106
+ .tk-row__meta {
107
+ color: var(--tk-muted);
108
+ font-size: 0.875rem;
109
+ }
110
+
111
+ .tk-form {
112
+ display: flex;
113
+ flex-direction: column;
114
+ gap: var(--tk-gap);
115
+ }
116
+
117
+ .tk-field {
118
+ display: flex;
119
+ flex-direction: column;
120
+ gap: 4px;
121
+ min-width: 0;
122
+ max-width: 440px;
123
+ }
124
+
125
+ .tk-field > * {
126
+ margin: 0;
127
+ }
128
+
129
+ .tk-label {
130
+ font-size: 0.875rem;
131
+ font-weight: 600;
132
+ }
133
+
134
+ .tk-needed {
135
+ font-weight: 400;
136
+ color: var(--tk-muted);
137
+ }
138
+
139
+ .tk-input {
140
+ font: inherit;
141
+ color: var(--tk-fg);
142
+ background: var(--tk-bg);
143
+ border: 1px solid var(--tk-border);
144
+ border-radius: var(--tk-radius);
145
+ padding: 0 10px;
146
+ min-height: var(--tk-tap);
147
+ width: 100%;
148
+ box-sizing: border-box;
149
+ }
150
+
151
+ .tk-textarea {
152
+ padding: 10px;
153
+ min-height: 5.5rem;
154
+ resize: vertical;
155
+ }
156
+
157
+ .tk-fieldset {
158
+ border: 0;
159
+ padding: 0;
160
+ margin: 0;
161
+ display: flex;
162
+ flex-wrap: wrap;
163
+ gap: 4px var(--tk-gap);
164
+ min-width: 0;
165
+ }
166
+
167
+ .tk-fieldset > .tk-label {
168
+ width: 100%;
169
+ padding: 0;
170
+ }
171
+
172
+ .tk-check,
173
+ .tk-switch {
174
+ display: inline-flex;
175
+ align-items: center;
176
+ gap: 6px;
177
+ min-height: var(--tk-tap);
178
+ }
179
+
180
+ .tk-check input,
181
+ .tk-switch input {
182
+ width: 1.15rem;
183
+ height: 1.15rem;
184
+ accent-color: var(--tk-accent);
185
+ }
186
+
187
+ .tk-button {
188
+ font: inherit;
189
+ color: var(--tk-fg);
190
+ background: var(--tk-bg);
191
+ border: 1px solid var(--tk-border);
192
+ border-radius: var(--tk-radius);
193
+ min-height: var(--tk-tap);
194
+ padding: 0 16px;
195
+ cursor: pointer;
196
+ }
197
+
198
+ .tk-button--primary {
199
+ background: var(--tk-accent);
200
+ border-color: var(--tk-accent);
201
+ color: var(--tk-accent-fg);
202
+ font-weight: 600;
203
+ }
204
+
205
+ .tk-button--danger {
206
+ color: var(--tk-danger);
207
+ border-color: var(--tk-danger);
208
+ }
209
+
210
+ .tk-button--quiet {
211
+ background: transparent;
212
+ }
213
+
214
+ .tk-button[aria-busy="true"] {
215
+ opacity: 0.7;
216
+ cursor: progress;
217
+ }
218
+
219
+ .tk-actions {
220
+ display: flex;
221
+ flex-wrap: wrap;
222
+ gap: 8px;
223
+ }
224
+
225
+ .tk-segmented {
226
+ display: flex;
227
+ flex-wrap: wrap;
228
+ gap: 6px;
229
+ }
230
+
231
+ .tk-segment {
232
+ font: inherit;
233
+ color: var(--tk-fg);
234
+ background: var(--tk-bg);
235
+ border: 1px solid var(--tk-border);
236
+ border-radius: var(--tk-radius);
237
+ min-height: var(--tk-tap);
238
+ padding: 0 14px;
239
+ cursor: pointer;
240
+ }
241
+
242
+ .tk-segment[aria-pressed="true"] {
243
+ background: var(--tk-accent);
244
+ border-color: var(--tk-accent);
245
+ color: var(--tk-accent-fg);
246
+ }
247
+
248
+ .tk-button:focus-visible,
249
+ .tk-segment:focus-visible,
250
+ .tk-input:focus-visible,
251
+ .tk-check input:focus-visible,
252
+ .tk-switch input:focus-visible,
253
+ .tk-balance:focus-visible {
254
+ outline: 2px solid var(--tk-focus);
255
+ outline-offset: 2px;
256
+ }
257
+
258
+ .tk-notice {
259
+ margin: 0;
260
+ padding: 8px 12px;
261
+ border-radius: var(--tk-radius);
262
+ border: 1px solid var(--tk-border);
263
+ font-size: 0.9rem;
264
+ }
265
+
266
+ .tk-notice--error {
267
+ border-color: var(--tk-danger);
268
+ color: var(--tk-danger);
269
+ }
270
+
271
+ .tk-notice--ok {
272
+ border-color: var(--tk-ok);
273
+ color: var(--tk-ok);
274
+ }
275
+
276
+ .tk-balance--pill {
277
+ display: inline-flex;
278
+ align-items: center;
279
+ gap: 0.25em;
280
+ padding: 0 12px;
281
+ min-height: 32px;
282
+ border: 1px solid var(--tk-border);
283
+ border-radius: 999px;
284
+ background: var(--tk-surface);
285
+ color: var(--tk-fg);
286
+ text-decoration: none;
287
+ font-size: 0.875rem;
288
+ white-space: nowrap;
289
+ }
290
+
291
+ .tk-balance--empty {
292
+ border-color: var(--tk-danger);
293
+ color: var(--tk-danger);
294
+ }
295
+
296
+ .tk-balance--full {
297
+ font-size: 2rem;
298
+ font-weight: 700;
299
+ }
300
+
301
+ .tk-balance--full .tk-balance__unit {
302
+ font-size: 1rem;
303
+ font-weight: 400;
304
+ color: var(--tk-muted);
305
+ }
306
+
307
+ .tk-breakdown {
308
+ margin: 0;
309
+ display: flex;
310
+ flex-wrap: wrap;
311
+ gap: var(--tk-gap);
312
+ }
313
+
314
+ .tk-breakdown dt {
315
+ color: var(--tk-muted);
316
+ font-size: 0.8rem;
317
+ }
318
+
319
+ .tk-breakdown dd {
320
+ margin: 0;
321
+ font-weight: 600;
322
+ }
323
+
324
+ .tk-list {
325
+ list-style: none;
326
+ margin: 0;
327
+ padding: 0;
328
+ display: flex;
329
+ flex-direction: column;
330
+ }
331
+
332
+ .tk-row {
333
+ display: flex;
334
+ justify-content: space-between;
335
+ align-items: baseline;
336
+ gap: var(--tk-gap);
337
+ padding: 8px 0;
338
+ border-top: 1px solid var(--tk-border);
339
+ }
340
+
341
+ .tk-row:first-child {
342
+ border-top: 0;
343
+ }
344
+
345
+ .tk-row__main {
346
+ display: flex;
347
+ flex-direction: column;
348
+ min-width: 0;
349
+ flex: 1 1 auto;
350
+ }
351
+
352
+ .tk-row__title {
353
+ overflow-wrap: break-word;
354
+ }
355
+
356
+ .tk-row__amount {
357
+ font-variant-numeric: tabular-nums;
358
+ white-space: nowrap;
359
+ }
360
+
361
+ @media (max-width: 560px) {
362
+ .tk-input,
363
+ .tk-textarea {
364
+ font-size: 16px;
365
+ }
366
+
367
+ .tk-field {
368
+ max-width: none;
369
+ }
370
+
371
+ .tk-segment {
372
+ flex: 1 1 auto;
373
+ }
374
+ }
375
+
376
+ @media (max-width: 430px) {
377
+ .tk-hide-narrow {
378
+ display: none;
379
+ }
380
+ }