@trypattern/sdk 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 (247) hide show
  1. package/README.md +127 -2
  2. package/dist/access.d.ts +31 -0
  3. package/dist/access.js +72 -0
  4. package/dist/actions.d.ts +57 -0
  5. package/dist/actions.js +198 -0
  6. package/dist/api.d.ts +20 -0
  7. package/dist/api.js +34 -0
  8. package/dist/app.d.ts +106 -0
  9. package/dist/app.js +224 -0
  10. package/dist/appearance.d.ts +7 -0
  11. package/dist/appearance.js +2 -0
  12. package/dist/auth/sessionApi.d.ts +41 -0
  13. package/dist/auth/sessionApi.js +158 -0
  14. package/dist/auth/sessionStore.d.ts +20 -0
  15. package/dist/auth/sessionStore.js +40 -0
  16. package/dist/auth/user.d.ts +11 -0
  17. package/dist/auth/user.js +9 -0
  18. package/dist/builtins/Account.d.ts +25 -0
  19. package/dist/builtins/Account.js +15 -0
  20. package/dist/builtins/Palette.d.ts +29 -0
  21. package/dist/builtins/Palette.js +63 -0
  22. package/dist/builtins/SignIn.d.ts +28 -0
  23. package/dist/builtins/SignIn.js +60 -0
  24. package/dist/builtins/Toasts.d.ts +32 -0
  25. package/dist/builtins/Toasts.js +24 -0
  26. package/dist/builtins/actions.d.ts +2 -0
  27. package/dist/builtins/actions.js +45 -0
  28. package/dist/cache.d.ts +24 -0
  29. package/dist/cache.js +112 -0
  30. package/dist/cli/app.d.ts +2 -0
  31. package/dist/cli/app.js +1329 -0
  32. package/dist/cli/screenshot.d.ts +1 -0
  33. package/dist/cli/screenshot.js +96 -0
  34. package/dist/cli/tools.d.ts +12 -0
  35. package/dist/cli/tools.js +35 -0
  36. package/dist/components/AppFrame.d.ts +14 -0
  37. package/dist/components/AppFrame.js +64 -0
  38. package/dist/components/AppearancePicker.d.ts +1 -0
  39. package/dist/components/AppearancePicker.js +12 -0
  40. package/dist/components/Avatar.d.ts +9 -0
  41. package/dist/components/Avatar.js +63 -0
  42. package/dist/components/Badge.d.ts +11 -0
  43. package/dist/components/Badge.js +87 -0
  44. package/dist/components/Button.d.ts +23 -0
  45. package/dist/components/Button.js +249 -0
  46. package/dist/components/Card.d.ts +12 -0
  47. package/dist/components/Card.js +38 -0
  48. package/dist/components/CommandMenu.d.ts +23 -0
  49. package/dist/components/CommandMenu.js +214 -0
  50. package/dist/components/CommandPalette.d.ts +1 -0
  51. package/dist/components/CommandPalette.js +7 -0
  52. package/dist/components/Content.d.ts +7 -0
  53. package/dist/components/Content.js +24 -0
  54. package/dist/components/ContextMenu.d.ts +8 -0
  55. package/dist/components/ContextMenu.js +23 -0
  56. package/dist/components/Dialog.d.ts +11 -0
  57. package/dist/components/Dialog.js +81 -0
  58. package/dist/components/Divider.d.ts +6 -0
  59. package/dist/components/Divider.js +15 -0
  60. package/dist/components/EmptyState.d.ts +8 -0
  61. package/dist/components/EmptyState.js +24 -0
  62. package/dist/components/Form.d.ts +9 -0
  63. package/dist/components/Form.js +23 -0
  64. package/dist/components/Icon.d.ts +10 -0
  65. package/dist/components/Icon.js +22 -0
  66. package/dist/components/IconButton.d.ts +14 -0
  67. package/dist/components/IconButton.js +53 -0
  68. package/dist/components/Kbd.d.ts +6 -0
  69. package/dist/components/Kbd.js +67 -0
  70. package/dist/components/Link.d.ts +9 -0
  71. package/dist/components/Link.js +35 -0
  72. package/dist/components/ListRow.d.ts +12 -0
  73. package/dist/components/ListRow.js +62 -0
  74. package/dist/components/LoadingRows.d.ts +5 -0
  75. package/dist/components/LoadingRows.js +6 -0
  76. package/dist/components/Menu.d.ts +12 -0
  77. package/dist/components/Menu.js +33 -0
  78. package/dist/components/MenuItems.d.ts +21 -0
  79. package/dist/components/MenuItems.js +32 -0
  80. package/dist/components/Placement.d.ts +21 -0
  81. package/dist/components/Placement.js +1 -0
  82. package/dist/components/Popup.d.ts +8 -0
  83. package/dist/components/Popup.js +43 -0
  84. package/dist/components/PopupItem.d.ts +8 -0
  85. package/dist/components/PopupItem.js +51 -0
  86. package/dist/components/PropertyRow.d.ts +7 -0
  87. package/dist/components/PropertyRow.js +23 -0
  88. package/dist/components/Row.d.ts +16 -0
  89. package/dist/components/Row.js +27 -0
  90. package/dist/components/Screen.d.ts +13 -0
  91. package/dist/components/Screen.js +56 -0
  92. package/dist/components/Scrim.d.ts +8 -0
  93. package/dist/components/Scrim.js +40 -0
  94. package/dist/components/ScrollArea.d.ts +15 -0
  95. package/dist/components/ScrollArea.js +13 -0
  96. package/dist/components/SegmentedControl.d.ts +17 -0
  97. package/dist/components/SegmentedControl.js +68 -0
  98. package/dist/components/Select.d.ts +17 -0
  99. package/dist/components/Select.js +107 -0
  100. package/dist/components/SettingsRow.d.ts +8 -0
  101. package/dist/components/SettingsRow.js +32 -0
  102. package/dist/components/Sidebar.d.ts +20 -0
  103. package/dist/components/Sidebar.js +138 -0
  104. package/dist/components/SignInScreen.d.ts +1 -0
  105. package/dist/components/SignInScreen.js +40 -0
  106. package/dist/components/Size.d.ts +1 -0
  107. package/dist/components/Size.js +1 -0
  108. package/dist/components/Skeleton.d.ts +8 -0
  109. package/dist/components/Skeleton.js +48 -0
  110. package/dist/components/Spinner.d.ts +6 -0
  111. package/dist/components/Spinner.js +54 -0
  112. package/dist/components/Stack.d.ts +14 -0
  113. package/dist/components/Stack.js +19 -0
  114. package/dist/components/Switch.d.ts +10 -0
  115. package/dist/components/Switch.js +71 -0
  116. package/dist/components/Text.d.ts +21 -0
  117. package/dist/components/Text.js +77 -0
  118. package/dist/components/TextField.d.ts +25 -0
  119. package/dist/components/TextField.js +68 -0
  120. package/dist/components/Toaster.d.ts +1 -0
  121. package/dist/components/Toaster.js +83 -0
  122. package/dist/components/Tooltip.d.ts +8 -0
  123. package/dist/components/Tooltip.js +43 -0
  124. package/dist/components/controlLook.d.ts +60 -0
  125. package/dist/components/controlLook.js +67 -0
  126. package/dist/components/frame.d.ts +4 -0
  127. package/dist/components/frame.js +2 -0
  128. package/dist/components/icons.d.ts +58 -0
  129. package/dist/components/icons.js +105 -0
  130. package/dist/components/interaction.d.ts +12 -0
  131. package/dist/components/interaction.js +22 -0
  132. package/dist/components/spacing.d.ts +118 -0
  133. package/dist/components/spacing.js +46 -0
  134. package/dist/components/tones.d.ts +28 -0
  135. package/dist/components/tones.js +12 -0
  136. package/dist/entityStore.d.ts +46 -0
  137. package/dist/entityStore.js +160 -0
  138. package/dist/flows/accessFlows.d.ts +9 -0
  139. package/dist/flows/accessFlows.js +151 -0
  140. package/dist/flows/browser.d.ts +32 -0
  141. package/dist/flows/browser.js +164 -0
  142. package/dist/flows/contactSheet.d.ts +1 -0
  143. package/dist/flows/contactSheet.js +63 -0
  144. package/dist/flows/flowBrowser.d.ts +23 -0
  145. package/dist/flows/flowBrowser.js +21 -0
  146. package/dist/flows/flowSetup.d.ts +3 -0
  147. package/dist/flows/flowSetup.js +35 -0
  148. package/dist/flows/flows.d.ts +41 -0
  149. package/dist/flows/flows.js +148 -0
  150. package/dist/flows/screenshotName.d.ts +1 -0
  151. package/dist/flows/screenshotName.js +6 -0
  152. package/dist/gate.d.ts +8 -0
  153. package/dist/gate.js +52 -0
  154. package/dist/hooks/appContext.d.ts +14 -0
  155. package/dist/hooks/appContext.js +5 -0
  156. package/dist/hooks/appHooks.d.ts +21 -0
  157. package/dist/hooks/appHooks.js +24 -0
  158. package/dist/hooks/useAnimatesOnChange.d.ts +1 -0
  159. package/dist/hooks/useAnimatesOnChange.js +14 -0
  160. package/dist/hooks/useStoreState.d.ts +6 -0
  161. package/dist/hooks/useStoreState.js +5 -0
  162. package/dist/index.d.ts +40 -0
  163. package/dist/index.js +19 -0
  164. package/dist/memoryApi.d.ts +19 -0
  165. package/dist/memoryApi.js +42 -0
  166. package/dist/model.d.ts +94 -0
  167. package/dist/model.js +243 -0
  168. package/dist/page.d.ts +11 -0
  169. package/dist/page.js +9 -0
  170. package/dist/platform.d.ts +1 -0
  171. package/dist/platform.js +5 -0
  172. package/dist/react.d.ts +46 -0
  173. package/dist/react.js +38 -0
  174. package/dist/registered.d.ts +54 -0
  175. package/dist/registered.js +17 -0
  176. package/dist/routes.d.ts +38 -0
  177. package/dist/routes.js +21 -0
  178. package/dist/rules/checkAllFiles.d.ts +1 -0
  179. package/dist/rules/checkAllFiles.js +741 -0
  180. package/dist/rules/checkApiRouting.d.ts +1 -0
  181. package/dist/rules/checkApiRouting.js +112 -0
  182. package/dist/rules/lintPlugin.d.ts +252 -0
  183. package/dist/rules/lintPlugin.js +1157 -0
  184. package/dist/rules/messages.d.ts +48 -0
  185. package/dist/rules/messages.js +48 -0
  186. package/dist/rules/themeTokens.d.ts +6 -0
  187. package/dist/rules/themeTokens.js +64 -0
  188. package/dist/schema.d.ts +2 -0
  189. package/dist/schema.js +1 -0
  190. package/dist/server/auth.d.ts +115 -0
  191. package/dist/server/auth.js +131 -0
  192. package/dist/server/authService.d.ts +9 -0
  193. package/dist/server/authService.js +50 -0
  194. package/dist/server/createServer.d.ts +357 -0
  195. package/dist/server/createServer.js +62 -0
  196. package/dist/server/env.d.ts +15 -0
  197. package/dist/server/env.js +1 -0
  198. package/dist/server/index.d.ts +8 -0
  199. package/dist/server/index.js +5 -0
  200. package/dist/server/session.d.ts +28 -0
  201. package/dist/server/session.js +54 -0
  202. package/dist/server/sessions.d.ts +92 -0
  203. package/dist/server/sessions.js +47 -0
  204. package/dist/server/signInWire.d.ts +52 -0
  205. package/dist/server/signInWire.js +22 -0
  206. package/dist/server/tables.d.ts +242 -0
  207. package/dist/server/tables.js +246 -0
  208. package/dist/server/users.d.ts +189 -0
  209. package/dist/server/users.js +49 -0
  210. package/dist/shortcuts.d.ts +3 -0
  211. package/dist/shortcuts.js +40 -0
  212. package/dist/start.d.ts +20 -0
  213. package/dist/start.js +121 -0
  214. package/dist/state.d.ts +14 -0
  215. package/dist/state.js +17 -0
  216. package/dist/store.d.ts +43 -0
  217. package/dist/store.js +198 -0
  218. package/dist/subscribers.d.ts +5 -0
  219. package/dist/subscribers.js +13 -0
  220. package/dist/table.d.ts +143 -0
  221. package/dist/table.js +91 -0
  222. package/dist/tables.d.ts +77 -0
  223. package/dist/tables.js +216 -0
  224. package/dist/theme/reset.css +84 -0
  225. package/dist/theme/scales.d.ts +76 -0
  226. package/dist/theme/scales.js +82 -0
  227. package/dist/theme/tokens.stylex.d.ts +218 -0
  228. package/dist/theme/tokens.stylex.js +233 -0
  229. package/dist/vite/commandBridge.d.ts +2 -0
  230. package/dist/vite/commandBridge.js +46 -0
  231. package/dist/vite/designManifest.d.ts +25 -0
  232. package/dist/vite/designManifest.js +191 -0
  233. package/dist/vite/localWorker.d.ts +4 -0
  234. package/dist/vite/localWorker.js +59 -0
  235. package/dist/vite/rootAlias.d.ts +2 -0
  236. package/dist/vite/rootAlias.js +15 -0
  237. package/dist/vite/vite.d.ts +7 -0
  238. package/dist/vite/vite.js +88 -0
  239. package/dist/wire.d.ts +7 -0
  240. package/dist/wire.js +1 -0
  241. package/package.json +99 -3
  242. package/presets/lintPlugin.js +7 -0
  243. package/presets/oxlint.json +167 -0
  244. package/presets/register.d.ts +19 -0
  245. package/presets/server.d.ts +2 -0
  246. package/presets/tsconfig.app.json +29 -0
  247. package/presets/tsconfig.server.json +27 -0
@@ -0,0 +1,357 @@
1
+ import type { Tables } from '../table.ts';
2
+ import type { ServerEnv } from './env.ts';
3
+ export declare function createServer<Group extends Tables>({ tables, admins }: {
4
+ tables: Group;
5
+ admins: readonly string[];
6
+ }): import("hono/hono-base").HonoBase<ServerEnv, (({
7
+ "/api/health": {
8
+ $get: {
9
+ input: {};
10
+ output: {
11
+ ok: true;
12
+ };
13
+ outputFormat: "json";
14
+ status: import("hono/utils/http-status").ContentfulStatusCode;
15
+ };
16
+ };
17
+ } | import("hono/types").MergeSchemaPath<{
18
+ "/google": {
19
+ $get: {
20
+ input: {};
21
+ output: {
22
+ error: string;
23
+ };
24
+ outputFormat: "json";
25
+ status: 503;
26
+ } | {
27
+ input: {};
28
+ output: undefined;
29
+ outputFormat: "redirect";
30
+ status: 302;
31
+ };
32
+ };
33
+ } & {
34
+ "/finish": {
35
+ $get: {
36
+ input: {};
37
+ output: undefined;
38
+ outputFormat: "redirect";
39
+ status: 302;
40
+ };
41
+ };
42
+ } & {
43
+ "/email": {
44
+ $post: {
45
+ input: {};
46
+ output: {
47
+ error: string;
48
+ };
49
+ outputFormat: "json";
50
+ status: 400;
51
+ } | {
52
+ input: {};
53
+ output: {
54
+ error: string;
55
+ };
56
+ outputFormat: "json";
57
+ status: 503;
58
+ } | {
59
+ input: {};
60
+ output: {
61
+ error: string;
62
+ };
63
+ outputFormat: "json";
64
+ status: 429;
65
+ } | {
66
+ input: {};
67
+ output: {
68
+ ok: true;
69
+ };
70
+ outputFormat: "json";
71
+ status: import("hono/utils/http-status").ContentfulStatusCode;
72
+ };
73
+ };
74
+ } & {
75
+ "/email/verify": {
76
+ $post: {
77
+ input: {};
78
+ output: {
79
+ isAdmin: boolean;
80
+ id: string;
81
+ name: string;
82
+ email: string;
83
+ avatarUrl: string | null;
84
+ };
85
+ outputFormat: "json";
86
+ status: import("hono/utils/http-status").ContentfulStatusCode;
87
+ } | {
88
+ input: {};
89
+ output: {
90
+ error: string;
91
+ };
92
+ outputFormat: "json";
93
+ status: 401;
94
+ };
95
+ };
96
+ } & {
97
+ "/sign-out": {
98
+ $post: {
99
+ input: {};
100
+ output: {
101
+ ok: true;
102
+ };
103
+ outputFormat: "json";
104
+ status: import("hono/utils/http-status").ContentfulStatusCode;
105
+ };
106
+ };
107
+ }, "/api/auth">) & {
108
+ "/api/me": {
109
+ $get: {
110
+ input: {};
111
+ output: {
112
+ error: string;
113
+ };
114
+ outputFormat: "json";
115
+ status: 401;
116
+ } | {
117
+ input: {};
118
+ output: {
119
+ isAdmin: boolean;
120
+ id: string;
121
+ name: string;
122
+ email: string;
123
+ avatarUrl: string | null;
124
+ };
125
+ outputFormat: "json";
126
+ status: 200;
127
+ };
128
+ };
129
+ }) | import("hono/types").MergeSchemaPath<{
130
+ "/:table": {
131
+ $get: {
132
+ input: {
133
+ param: {
134
+ table: string;
135
+ };
136
+ };
137
+ output: {
138
+ error: "forbidden" | "Signed out" | "No such row";
139
+ };
140
+ outputFormat: "json";
141
+ status: 401 | 403 | 404;
142
+ } | {
143
+ input: {
144
+ param: {
145
+ table: string;
146
+ };
147
+ };
148
+ output: {}[];
149
+ outputFormat: "json";
150
+ status: import("hono/utils/http-status").ContentfulStatusCode;
151
+ } | {
152
+ input: {
153
+ param: {
154
+ table: string;
155
+ };
156
+ };
157
+ output: {
158
+ error: string;
159
+ };
160
+ outputFormat: "json";
161
+ status: 404;
162
+ };
163
+ };
164
+ } & {
165
+ "/:table/public": {
166
+ $get: {
167
+ input: {
168
+ param: {
169
+ table: string;
170
+ };
171
+ };
172
+ output: {}[];
173
+ outputFormat: "json";
174
+ status: import("hono/utils/http-status").ContentfulStatusCode;
175
+ } | {
176
+ input: {
177
+ param: {
178
+ table: string;
179
+ };
180
+ };
181
+ output: {
182
+ error: string;
183
+ };
184
+ outputFormat: "json";
185
+ status: 404;
186
+ };
187
+ };
188
+ } & {
189
+ "/:table": {
190
+ $post: {
191
+ input: {
192
+ param: {
193
+ table: string;
194
+ };
195
+ };
196
+ output: {
197
+ error: "forbidden" | "Signed out" | "No such row";
198
+ };
199
+ outputFormat: "json";
200
+ status: 401 | 403 | 404;
201
+ } | {
202
+ input: {
203
+ param: {
204
+ table: string;
205
+ };
206
+ };
207
+ output: {};
208
+ outputFormat: "json";
209
+ status: 201;
210
+ } | {
211
+ input: {
212
+ param: {
213
+ table: string;
214
+ };
215
+ };
216
+ output: {
217
+ error: string;
218
+ };
219
+ outputFormat: "json";
220
+ status: 404;
221
+ } | {
222
+ input: {
223
+ param: {
224
+ table: string;
225
+ };
226
+ };
227
+ output: {
228
+ error: string;
229
+ };
230
+ outputFormat: "json";
231
+ status: 400;
232
+ } | {
233
+ input: {
234
+ param: {
235
+ table: string;
236
+ };
237
+ };
238
+ output: {
239
+ error: string;
240
+ unique: readonly (readonly string[])[];
241
+ };
242
+ outputFormat: "json";
243
+ status: 409;
244
+ };
245
+ };
246
+ } & {
247
+ "/:table/:id": {
248
+ $put: {
249
+ input: {
250
+ param: {
251
+ table: string;
252
+ } & {
253
+ id: string;
254
+ };
255
+ };
256
+ output: {
257
+ error: "forbidden" | "Signed out" | "No such row";
258
+ };
259
+ outputFormat: "json";
260
+ status: 401 | 403 | 404;
261
+ } | {
262
+ input: {
263
+ param: {
264
+ table: string;
265
+ } & {
266
+ id: string;
267
+ };
268
+ };
269
+ output: {
270
+ error: string;
271
+ unique: readonly (readonly string[])[];
272
+ };
273
+ outputFormat: "json";
274
+ status: 409;
275
+ } | {
276
+ input: {
277
+ param: {
278
+ table: string;
279
+ } & {
280
+ id: string;
281
+ };
282
+ };
283
+ output: {};
284
+ outputFormat: "json";
285
+ status: 200;
286
+ } | {
287
+ input: {
288
+ param: {
289
+ table: string;
290
+ } & {
291
+ id: string;
292
+ };
293
+ };
294
+ output: {
295
+ error: string;
296
+ };
297
+ outputFormat: "json";
298
+ status: 404;
299
+ } | {
300
+ input: {
301
+ param: {
302
+ table: string;
303
+ } & {
304
+ id: string;
305
+ };
306
+ };
307
+ output: {
308
+ error: string;
309
+ };
310
+ outputFormat: "json";
311
+ status: 400;
312
+ };
313
+ };
314
+ } & {
315
+ "/:table/:id": {
316
+ $delete: {
317
+ input: {
318
+ param: {
319
+ table: string;
320
+ } & {
321
+ id: string;
322
+ };
323
+ };
324
+ output: {
325
+ error: "forbidden" | "Signed out" | "No such row";
326
+ };
327
+ outputFormat: "json";
328
+ status: 401 | 403 | 404;
329
+ } | {
330
+ input: {
331
+ param: {
332
+ table: string;
333
+ } & {
334
+ id: string;
335
+ };
336
+ };
337
+ output: {
338
+ error: string;
339
+ };
340
+ outputFormat: "json";
341
+ status: 404;
342
+ } | {
343
+ input: {
344
+ param: {
345
+ table: string;
346
+ } & {
347
+ id: string;
348
+ };
349
+ };
350
+ output: {
351
+ ok: true;
352
+ };
353
+ outputFormat: "json";
354
+ status: import("hono/utils/http-status").ContentfulStatusCode;
355
+ };
356
+ };
357
+ }, "/api/tables">, "/api", "/api/me">;
@@ -0,0 +1,62 @@
1
+ import { Hono } from 'hono';
2
+ import { csrf } from 'hono/csrf';
3
+ import { adminEmails } from "../access.js";
4
+ import { auth, devSignIn } from "./auth.js";
5
+ import { forgetSession, issuePass, needsRecheck, readPass, rememberUser } from "./session.js";
6
+ import { renewSession } from "./sessions.js";
7
+ import { serveTables } from "./tables.js";
8
+ import { findUser } from "./users.js";
9
+ const PUBLIC_PATHS = ['/api/health', '/api/auth/'];
10
+ const VIEWER_OPTIONAL_PATHS = ['/api/tables/'];
11
+ function knowAdmins(admins) {
12
+ const emails = adminEmails(admins);
13
+ return (c, next) => {
14
+ c.set('admins', emails);
15
+ return next();
16
+ };
17
+ }
18
+ async function attachViewer(c, next) {
19
+ if (PUBLIC_PATHS.some((path) => c.req.path.startsWith(path)))
20
+ return next();
21
+ const isOptional = VIEWER_OPTIONAL_PATHS.some((path) => c.req.path.startsWith(path));
22
+ let pass = await readPass(c);
23
+ if (pass !== undefined && needsRecheck(pass)) {
24
+ const user = await renewSession(c.env.DB, pass.sessionId);
25
+ pass =
26
+ user === undefined ? undefined : await issuePass(c, user, pass.sessionId);
27
+ }
28
+ if (pass === undefined) {
29
+ forgetSession(c);
30
+ if (!isOptional)
31
+ return c.json({ error: 'Signed out' }, 401);
32
+ c.set('viewer', null);
33
+ return next();
34
+ }
35
+ c.set('viewer', {
36
+ userId: pass.userId,
37
+ isAdmin: c.get('admins').has(pass.email.toLowerCase())
38
+ });
39
+ return next();
40
+ }
41
+ export function createServer({ tables, admins }) {
42
+ const authRoutes = import.meta.env.DEV ? auth.route('/', devSignIn) : auth;
43
+ return new Hono()
44
+ .basePath('/api')
45
+ .use(csrf())
46
+ .use(knowAdmins(admins))
47
+ .use(attachViewer)
48
+ .get('/health', (c) => c.json({ ok: true }))
49
+ .route('/auth', authRoutes)
50
+ .get('/me', async (c) => {
51
+ const viewer = c.get('viewer');
52
+ const user = viewer === null ? undefined : await findUser(c.env.DB, viewer.userId);
53
+ if (user === undefined) {
54
+ forgetSession(c);
55
+ return c.json({ error: 'Signed out' }, 401);
56
+ }
57
+ rememberUser(c, user);
58
+ return c.json({ ...user, isAdmin: viewer?.isAdmin ?? false }, 200);
59
+ })
60
+ .route('/tables', serveTables(tables))
61
+ .notFound((c) => c.json({ error: `No route for ${c.req.path}` }, 404));
62
+ }
@@ -0,0 +1,15 @@
1
+ import type { D1Database } from '@cloudflare/workers-types';
2
+ import type { Viewer } from '../access.ts';
3
+ export type { Viewer };
4
+ export type ServerBindings = {
5
+ DB: D1Database;
6
+ SESSION_SECRET: string;
7
+ AUTH_SECRET?: string;
8
+ };
9
+ export type ServerEnv = {
10
+ Bindings: ServerBindings;
11
+ Variables: {
12
+ viewer: Viewer | null;
13
+ admins: ReadonlySet<string>;
14
+ };
15
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export { createServer } from './createServer.ts';
2
+ export type { ServerBindings, ServerEnv, Viewer } from './env.ts';
3
+ export { seedSql, serveTables, sqlTable, sqlTables } from './tables.ts';
4
+ export { sessions } from './sessions.ts';
5
+ export { exchangeReply, exchangeRequest, sendCodeReply, sendCodeRequest, verifiedIdentity, verifyCodeReply, verifyCodeRequest } from './signInWire.ts';
6
+ export type { VerifiedIdentity } from './signInWire.ts';
7
+ export { users } from './users.ts';
8
+ export type { D1Database } from '@cloudflare/workers-types';
@@ -0,0 +1,5 @@
1
+ export { createServer } from "./createServer.js";
2
+ export { seedSql, serveTables, sqlTable, sqlTables } from "./tables.js";
3
+ export { sessions } from "./sessions.js";
4
+ export { exchangeReply, exchangeRequest, sendCodeReply, sendCodeRequest, verifiedIdentity, verifyCodeReply, verifyCodeRequest } from "./signInWire.js";
5
+ export { users } from "./users.js";
@@ -0,0 +1,28 @@
1
+ import type { Context } from 'hono';
2
+ import { z } from 'zod';
3
+ import type { StoredUser } from '../auth/user.ts';
4
+ import type { ServerEnv } from './env.ts';
5
+ declare const passSchema: z.ZodObject<{
6
+ sessionId: z.ZodString;
7
+ userId: z.ZodString;
8
+ email: z.ZodString;
9
+ recheckAt: z.ZodNumber;
10
+ }, z.core.$strip>;
11
+ export type Pass = z.infer<typeof passSchema>;
12
+ export declare function rememberUser(c: Context<ServerEnv>, user: StoredUser): void;
13
+ export declare function issuePass(c: Context<ServerEnv>, user: StoredUser, sessionId: string): Promise<{
14
+ sessionId: string;
15
+ userId: string;
16
+ email: string;
17
+ recheckAt: number;
18
+ }>;
19
+ export declare function startSession(c: Context<ServerEnv>, user: StoredUser): Promise<void>;
20
+ export declare function readPass(c: Context<ServerEnv>): Promise<{
21
+ sessionId: string;
22
+ userId: string;
23
+ email: string;
24
+ recheckAt: number;
25
+ } | undefined>;
26
+ export declare const needsRecheck: (pass: Pass) => boolean;
27
+ export declare function forgetSession(c: Context<ServerEnv>): void;
28
+ export {};
@@ -0,0 +1,54 @@
1
+ import { deleteCookie, getSignedCookie, setCookie, setSignedCookie } from 'hono/cookie';
2
+ import { z } from 'zod';
3
+ import { USER_COOKIE } from "../auth/user.js";
4
+ import { createSession } from "./sessions.js";
5
+ const SESSION_COOKIE = 'pattern_session';
6
+ const RECHECK_SECONDS = 15 * 60;
7
+ const SESSION_SECONDS = 30 * 86_400;
8
+ const cookieOptions = {
9
+ path: '/',
10
+ secure: true,
11
+ sameSite: 'Lax',
12
+ maxAge: SESSION_SECONDS
13
+ };
14
+ const passSchema = z.object({
15
+ sessionId: z.string(),
16
+ userId: z.string(),
17
+ email: z.string(),
18
+ recheckAt: z.number()
19
+ });
20
+ const nowInSeconds = () => Math.floor(Date.now() / 1000);
21
+ export function rememberUser(c, user) {
22
+ setCookie(c, USER_COOKIE, JSON.stringify(user), cookieOptions);
23
+ }
24
+ export async function issuePass(c, user, sessionId) {
25
+ const pass = {
26
+ sessionId,
27
+ userId: user.id,
28
+ email: user.email,
29
+ recheckAt: nowInSeconds() + RECHECK_SECONDS
30
+ };
31
+ await setSignedCookie(c, SESSION_COOKIE, JSON.stringify(pass), c.env.SESSION_SECRET, { ...cookieOptions, httpOnly: true });
32
+ rememberUser(c, user);
33
+ return pass;
34
+ }
35
+ export async function startSession(c, user) {
36
+ await issuePass(c, user, await createSession(c.env.DB, user.id));
37
+ }
38
+ export async function readPass(c) {
39
+ const value = await getSignedCookie(c, c.env.SESSION_SECRET, SESSION_COOKIE);
40
+ if (typeof value !== 'string')
41
+ return undefined;
42
+ try {
43
+ const parsed = passSchema.safeParse(JSON.parse(value));
44
+ return parsed.success ? parsed.data : undefined;
45
+ }
46
+ catch {
47
+ return undefined;
48
+ }
49
+ }
50
+ export const needsRecheck = (pass) => pass.recheckAt <= nowInSeconds();
51
+ export function forgetSession(c) {
52
+ deleteCookie(c, SESSION_COOKIE, { path: '/' });
53
+ deleteCookie(c, USER_COOKIE, { path: '/' });
54
+ }
@@ -0,0 +1,92 @@
1
+ import type { D1Database } from '@cloudflare/workers-types';
2
+ export declare const sessions: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
3
+ name: "sessions";
4
+ schema: undefined;
5
+ columns: {
6
+ id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
7
+ name: "id";
8
+ tableName: "sessions";
9
+ dataType: "string";
10
+ columnType: "SQLiteText";
11
+ data: string;
12
+ driverParam: string;
13
+ notNull: true;
14
+ hasDefault: false;
15
+ isPrimaryKey: true;
16
+ isAutoincrement: false;
17
+ hasRuntimeDefault: false;
18
+ enumValues: [string, ...string[]];
19
+ baseColumn: never;
20
+ identity: undefined;
21
+ generated: undefined;
22
+ }, {}, {
23
+ length: number | undefined;
24
+ }>;
25
+ userId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
26
+ name: "user_id";
27
+ tableName: "sessions";
28
+ dataType: "string";
29
+ columnType: "SQLiteText";
30
+ data: string;
31
+ driverParam: string;
32
+ notNull: true;
33
+ hasDefault: false;
34
+ isPrimaryKey: false;
35
+ isAutoincrement: false;
36
+ hasRuntimeDefault: false;
37
+ enumValues: [string, ...string[]];
38
+ baseColumn: never;
39
+ identity: undefined;
40
+ generated: undefined;
41
+ }, {}, {
42
+ length: number | undefined;
43
+ }>;
44
+ expiresAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
45
+ name: "expires_at";
46
+ tableName: "sessions";
47
+ dataType: "string";
48
+ columnType: "SQLiteText";
49
+ data: string;
50
+ driverParam: string;
51
+ notNull: true;
52
+ hasDefault: false;
53
+ isPrimaryKey: false;
54
+ isAutoincrement: false;
55
+ hasRuntimeDefault: false;
56
+ enumValues: [string, ...string[]];
57
+ baseColumn: never;
58
+ identity: undefined;
59
+ generated: undefined;
60
+ }, {}, {
61
+ length: number | undefined;
62
+ }>;
63
+ createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
64
+ name: "created_at";
65
+ tableName: "sessions";
66
+ dataType: "string";
67
+ columnType: "SQLiteText";
68
+ data: string;
69
+ driverParam: string;
70
+ notNull: true;
71
+ hasDefault: true;
72
+ isPrimaryKey: false;
73
+ isAutoincrement: false;
74
+ hasRuntimeDefault: false;
75
+ enumValues: [string, ...string[]];
76
+ baseColumn: never;
77
+ identity: undefined;
78
+ generated: undefined;
79
+ }, {}, {
80
+ length: number | undefined;
81
+ }>;
82
+ };
83
+ dialect: "sqlite";
84
+ }>;
85
+ export declare function createSession(d1: D1Database, userId: string): Promise<`${string}-${string}-${string}-${string}-${string}`>;
86
+ export declare function renewSession(d1: D1Database, id: string): Promise<{
87
+ id: string;
88
+ email: string;
89
+ name: string;
90
+ avatarUrl: string | null;
91
+ } | undefined>;
92
+ export declare function deleteSession(d1: D1Database, id: string): Promise<void>;
@@ -0,0 +1,47 @@
1
+ import { and, eq, gt, lt, sql } from 'drizzle-orm';
2
+ import { drizzle } from 'drizzle-orm/d1';
3
+ import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
4
+ import { userColumns, users } from "./users.js";
5
+ export const sessions = sqliteTable('sessions', {
6
+ id: text('id').primaryKey(),
7
+ userId: text('user_id')
8
+ .notNull()
9
+ .references(() => users.id, { onDelete: 'cascade' }),
10
+ expiresAt: text('expires_at').notNull(),
11
+ createdAt: text('created_at')
12
+ .notNull()
13
+ .default(sql `(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`)
14
+ });
15
+ const SESSION_DAYS = 30;
16
+ const DAY_MILLISECONDS = 86_400_000;
17
+ const sessionEnd = () => new Date(Date.now() + SESSION_DAYS * DAY_MILLISECONDS).toISOString();
18
+ export async function createSession(d1, userId) {
19
+ const db = drizzle(d1);
20
+ const id = crypto.randomUUID();
21
+ await db.batch([
22
+ db
23
+ .delete(sessions)
24
+ .where(and(eq(sessions.userId, userId), lt(sessions.expiresAt, new Date().toISOString()))),
25
+ db.insert(sessions).values({ id, userId, expiresAt: sessionEnd() })
26
+ ]);
27
+ return id;
28
+ }
29
+ export async function renewSession(d1, id) {
30
+ const db = drizzle(d1);
31
+ const user = await db
32
+ .select(userColumns)
33
+ .from(sessions)
34
+ .innerJoin(users, eq(users.id, sessions.userId))
35
+ .where(and(eq(sessions.id, id), gt(sessions.expiresAt, new Date().toISOString())))
36
+ .get();
37
+ if (user === undefined)
38
+ return undefined;
39
+ await db
40
+ .update(sessions)
41
+ .set({ expiresAt: sessionEnd() })
42
+ .where(eq(sessions.id, id));
43
+ return user;
44
+ }
45
+ export async function deleteSession(d1, id) {
46
+ await drizzle(d1).delete(sessions).where(eq(sessions.id, id));
47
+ }