@hatk/hatk 0.0.1-alpha.5 → 0.0.1-alpha.50

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 (165) hide show
  1. package/dist/adapter.d.ts +19 -0
  2. package/dist/adapter.d.ts.map +1 -0
  3. package/dist/adapter.js +107 -0
  4. package/dist/backfill.d.ts +60 -1
  5. package/dist/backfill.d.ts.map +1 -1
  6. package/dist/backfill.js +167 -33
  7. package/dist/car.d.ts +59 -1
  8. package/dist/car.d.ts.map +1 -1
  9. package/dist/car.js +179 -7
  10. package/dist/cbor.d.ts +37 -0
  11. package/dist/cbor.d.ts.map +1 -1
  12. package/dist/cbor.js +36 -3
  13. package/dist/cid.d.ts +37 -0
  14. package/dist/cid.d.ts.map +1 -1
  15. package/dist/cid.js +38 -3
  16. package/dist/cli.js +243 -996
  17. package/dist/config.d.ts +12 -1
  18. package/dist/config.d.ts.map +1 -1
  19. package/dist/config.js +36 -9
  20. package/dist/database/adapter-factory.d.ts +6 -0
  21. package/dist/database/adapter-factory.d.ts.map +1 -0
  22. package/dist/database/adapter-factory.js +20 -0
  23. package/dist/database/adapters/duckdb-search.d.ts +12 -0
  24. package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
  25. package/dist/database/adapters/duckdb-search.js +27 -0
  26. package/dist/database/adapters/duckdb.d.ts +25 -0
  27. package/dist/database/adapters/duckdb.d.ts.map +1 -0
  28. package/dist/database/adapters/duckdb.js +161 -0
  29. package/dist/database/adapters/sqlite-search.d.ts +23 -0
  30. package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
  31. package/dist/database/adapters/sqlite-search.js +74 -0
  32. package/dist/database/adapters/sqlite.d.ts +18 -0
  33. package/dist/database/adapters/sqlite.d.ts.map +1 -0
  34. package/dist/database/adapters/sqlite.js +88 -0
  35. package/dist/{db.d.ts → database/db.d.ts} +56 -6
  36. package/dist/database/db.d.ts.map +1 -0
  37. package/dist/{db.js → database/db.js} +719 -549
  38. package/dist/database/dialect.d.ts +45 -0
  39. package/dist/database/dialect.d.ts.map +1 -0
  40. package/dist/database/dialect.js +72 -0
  41. package/dist/{fts.d.ts → database/fts.d.ts} +7 -0
  42. package/dist/database/fts.d.ts.map +1 -0
  43. package/dist/{fts.js → database/fts.js} +116 -32
  44. package/dist/database/index.d.ts +7 -0
  45. package/dist/database/index.d.ts.map +1 -0
  46. package/dist/database/index.js +6 -0
  47. package/dist/database/ports.d.ts +50 -0
  48. package/dist/database/ports.d.ts.map +1 -0
  49. package/dist/database/ports.js +1 -0
  50. package/dist/{schema.d.ts → database/schema.d.ts} +14 -3
  51. package/dist/database/schema.d.ts.map +1 -0
  52. package/dist/{schema.js → database/schema.js} +81 -41
  53. package/dist/dev-entry.d.ts +8 -0
  54. package/dist/dev-entry.d.ts.map +1 -0
  55. package/dist/dev-entry.js +111 -0
  56. package/dist/feeds.d.ts +12 -8
  57. package/dist/feeds.d.ts.map +1 -1
  58. package/dist/feeds.js +45 -6
  59. package/dist/hooks.d.ts +43 -0
  60. package/dist/hooks.d.ts.map +1 -0
  61. package/dist/hooks.js +102 -0
  62. package/dist/hydrate.d.ts +6 -5
  63. package/dist/hydrate.d.ts.map +1 -1
  64. package/dist/hydrate.js +4 -16
  65. package/dist/indexer.d.ts +22 -0
  66. package/dist/indexer.d.ts.map +1 -1
  67. package/dist/indexer.js +80 -8
  68. package/dist/labels.d.ts +36 -0
  69. package/dist/labels.d.ts.map +1 -1
  70. package/dist/labels.js +71 -6
  71. package/dist/lexicon-resolve.d.ts.map +1 -1
  72. package/dist/lexicon-resolve.js +27 -112
  73. package/dist/lexicons/com/atproto/label/defs.json +75 -0
  74. package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
  75. package/dist/lexicons/com/atproto/repo/strongRef.json +24 -0
  76. package/dist/lexicons/dev/hatk/createRecord.json +40 -0
  77. package/dist/lexicons/dev/hatk/createReport.json +48 -0
  78. package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
  79. package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
  80. package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
  81. package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
  82. package/dist/lexicons/dev/hatk/getFeed.json +30 -0
  83. package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
  84. package/dist/lexicons/dev/hatk/getRecord.json +26 -0
  85. package/dist/lexicons/dev/hatk/getRecords.json +32 -0
  86. package/dist/lexicons/dev/hatk/putPreference.json +28 -0
  87. package/dist/lexicons/dev/hatk/putRecord.json +41 -0
  88. package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
  89. package/dist/lexicons/dev/hatk/uploadBlob.json +23 -0
  90. package/dist/logger.d.ts +29 -0
  91. package/dist/logger.d.ts.map +1 -1
  92. package/dist/logger.js +29 -0
  93. package/dist/main.js +126 -67
  94. package/dist/mst.d.ts +18 -1
  95. package/dist/mst.d.ts.map +1 -1
  96. package/dist/mst.js +19 -8
  97. package/dist/oauth/db.d.ts +3 -1
  98. package/dist/oauth/db.d.ts.map +1 -1
  99. package/dist/oauth/db.js +48 -19
  100. package/dist/oauth/server.d.ts +24 -0
  101. package/dist/oauth/server.d.ts.map +1 -1
  102. package/dist/oauth/server.js +198 -22
  103. package/dist/oauth/session.d.ts +11 -0
  104. package/dist/oauth/session.d.ts.map +1 -0
  105. package/dist/oauth/session.js +65 -0
  106. package/dist/opengraph.d.ts +10 -0
  107. package/dist/opengraph.d.ts.map +1 -1
  108. package/dist/opengraph.js +73 -39
  109. package/dist/pds-proxy.d.ts +42 -0
  110. package/dist/pds-proxy.d.ts.map +1 -0
  111. package/dist/pds-proxy.js +207 -0
  112. package/dist/renderer.d.ts +27 -0
  113. package/dist/renderer.d.ts.map +1 -0
  114. package/dist/renderer.js +46 -0
  115. package/dist/resolve-hatk.d.ts +6 -0
  116. package/dist/resolve-hatk.d.ts.map +1 -0
  117. package/dist/resolve-hatk.js +20 -0
  118. package/dist/response.d.ts +16 -0
  119. package/dist/response.d.ts.map +1 -0
  120. package/dist/response.js +69 -0
  121. package/dist/scanner.d.ts +21 -0
  122. package/dist/scanner.d.ts.map +1 -0
  123. package/dist/scanner.js +88 -0
  124. package/dist/seed.d.ts +19 -0
  125. package/dist/seed.d.ts.map +1 -1
  126. package/dist/seed.js +43 -4
  127. package/dist/server-init.d.ts +8 -0
  128. package/dist/server-init.d.ts.map +1 -0
  129. package/dist/server-init.js +62 -0
  130. package/dist/server.d.ts +26 -3
  131. package/dist/server.d.ts.map +1 -1
  132. package/dist/server.js +601 -635
  133. package/dist/setup.d.ts +28 -1
  134. package/dist/setup.d.ts.map +1 -1
  135. package/dist/setup.js +50 -3
  136. package/dist/templates/feed.tpl +14 -0
  137. package/dist/templates/hook.tpl +5 -0
  138. package/dist/templates/label.tpl +15 -0
  139. package/dist/templates/og.tpl +17 -0
  140. package/dist/templates/seed.tpl +11 -0
  141. package/dist/templates/setup.tpl +5 -0
  142. package/dist/templates/test-feed.tpl +19 -0
  143. package/dist/templates/test-xrpc.tpl +19 -0
  144. package/dist/templates/xrpc.tpl +41 -0
  145. package/dist/test.d.ts +1 -1
  146. package/dist/test.d.ts.map +1 -1
  147. package/dist/test.js +38 -32
  148. package/dist/views.js +1 -1
  149. package/dist/vite-plugin.d.ts +1 -1
  150. package/dist/vite-plugin.d.ts.map +1 -1
  151. package/dist/vite-plugin.js +254 -66
  152. package/dist/xrpc.d.ts +60 -10
  153. package/dist/xrpc.d.ts.map +1 -1
  154. package/dist/xrpc.js +155 -39
  155. package/package.json +15 -7
  156. package/public/admin.html +133 -54
  157. package/dist/db.d.ts.map +0 -1
  158. package/dist/fts.d.ts.map +0 -1
  159. package/dist/oauth/hooks.d.ts +0 -10
  160. package/dist/oauth/hooks.d.ts.map +0 -1
  161. package/dist/oauth/hooks.js +0 -40
  162. package/dist/schema.d.ts.map +0 -1
  163. package/dist/test-browser.d.ts +0 -14
  164. package/dist/test-browser.d.ts.map +0 -1
  165. package/dist/test-browser.js +0 -26
package/dist/cli.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import { mkdirSync, writeFileSync, existsSync, unlinkSync, readdirSync, readFileSync } from 'node:fs';
3
3
  import { resolve, join } from 'node:path';
4
- import { execSync } from 'node:child_process';
5
- import { loadLexicons } from "./schema.js";
4
+ import { execSync, spawn } from 'node:child_process';
5
+ import { loadLexicons } from "./database/schema.js";
6
6
  import { loadConfig } from "./config.js";
7
7
  const args = process.argv.slice(2);
8
8
  const command = args[0];
@@ -34,6 +34,31 @@ async function ensurePds() {
34
34
  console.error('[dev] PDS failed to start');
35
35
  process.exit(1);
36
36
  }
37
+ /** Spawn a long-running process and forward SIGINT/SIGTERM for clean shutdown. */
38
+ function spawnForward(cmd, args, env) {
39
+ return new Promise((resolve, reject) => {
40
+ const child = spawn(cmd, args, {
41
+ stdio: 'inherit',
42
+ cwd: process.cwd(),
43
+ env: { ...process.env, ...env },
44
+ });
45
+ const onSignal = (sig) => {
46
+ child.kill(sig);
47
+ };
48
+ process.on('SIGINT', onSignal);
49
+ process.on('SIGTERM', onSignal);
50
+ child.on('close', (code, signal) => {
51
+ process.removeListener('SIGINT', onSignal);
52
+ process.removeListener('SIGTERM', onSignal);
53
+ if (signal === 'SIGINT' || signal === 'SIGTERM')
54
+ process.exit(0);
55
+ if (code === 0 || code === null)
56
+ resolve();
57
+ else
58
+ reject(new Error(`Process exited with code ${code}`));
59
+ });
60
+ });
61
+ }
37
62
  function runSeed() {
38
63
  const seedFile = resolve('seeds/seed.ts');
39
64
  if (!existsSync(seedFile))
@@ -44,15 +69,11 @@ function usage() {
44
69
  console.log(`
45
70
  Usage: hatk <command> [options]
46
71
 
47
- Getting Started
48
- new <name> [--svelte] [--template <t>] Create a new hatk project
49
-
50
72
  Running
51
73
  start Start the hatk server
52
74
  dev Start PDS, seed, and run hatk
53
75
  seed Seed local PDS with fixture data
54
76
  reset Reset database and PDS for a clean slate
55
- schema Show database schema from lexicons
56
77
 
57
78
  Code Quality
58
79
  check Type-check and lint the project
@@ -70,7 +91,8 @@ function usage() {
70
91
  generate xrpc <nsid> Generate an XRPC handler
71
92
  generate label <name> Generate a label definition
72
93
  generate og <name> Generate an OpenGraph route
73
- generate job <name> Generate a periodic job
94
+ generate hook <name> Generate a lifecycle hook
95
+ generate setup <name> Generate a setup script
74
96
  generate types Regenerate TypeScript types from lexicons
75
97
  destroy <type> <name> Remove a generated file
76
98
 
@@ -82,151 +104,16 @@ function usage() {
82
104
  if (!command)
83
105
  usage();
84
106
  // --- Templates ---
85
- const templates = {
86
- feed: (name) => `import { defineFeed } from '../hatk.generated.ts'
87
-
88
- export default defineFeed({
89
- collection: 'your.collection.here',
90
- label: '${name.charAt(0).toUpperCase() + name.slice(1)}',
91
-
92
- async generate(ctx) {
93
- const { rows, cursor } = await ctx.paginate<{ uri: string }>(
94
- \`SELECT uri, cid, indexed_at FROM "your.collection.here"\`,
95
- )
96
-
97
- return ctx.ok({ uris: rows.map((r) => r.uri), cursor })
98
- },
99
- })
100
- `,
101
- xrpc: (name) => `import { defineQuery } from '${xrpcImportPath(name)}'
102
-
103
- export default defineQuery('${name}', async (ctx) => {
104
- const { ok, db, params, packCursor, unpackCursor } = ctx
105
- const limit = params.limit ?? 30
106
- const cursor = params.cursor
107
-
108
- const conditions: string[] = []
109
- const sqlParams: (string | number)[] = []
110
- let paramIdx = 1
111
-
112
- if (cursor) {
113
- const parsed = unpackCursor(cursor)
114
- if (parsed) {
115
- conditions.push(\`(s.indexed_at < $\${paramIdx} OR (s.indexed_at = $\${paramIdx + 1} AND s.cid < $\${paramIdx + 2}))\`)
116
- sqlParams.push(parsed.primary, parsed.primary, parsed.cid)
117
- paramIdx += 3
118
- }
119
- }
120
-
121
- const where = conditions.length ? 'WHERE ' + conditions.join(' AND ') : ''
122
-
123
- const rows = await db.query(
124
- \`SELECT s.* FROM "your.collection.here" s \${where} ORDER BY s.indexed_at DESC, s.cid DESC LIMIT $\${paramIdx}\`,
125
- sqlParams.concat([limit + 1]),
126
- )
127
-
128
- const hasMore = rows.length > limit
129
- if (hasMore) rows.pop()
130
- const lastRow = rows[rows.length - 1]
131
-
132
- return ok({
133
- items: rows,
134
- cursor: hasMore && lastRow ? packCursor(lastRow.indexed_at, lastRow.cid) : undefined,
135
- })
136
- })
137
- `,
138
- label: (name) => `import type { LabelRuleContext } from '@hatk/hatk/labels'
139
-
140
- export default {
141
- definition: {
142
- identifier: '${name}',
143
- severity: 'inform',
144
- blurs: 'none',
145
- defaultSetting: 'warn',
146
- locales: [{ lang: 'en', name: '${name.charAt(0).toUpperCase() + name.slice(1)}', description: 'Description here' }],
147
- },
148
- async evaluate(ctx: LabelRuleContext) {
149
- // Return array of label identifiers to apply, or empty array
150
- return []
151
- },
152
- }
153
- `,
154
- og: (name) => `import type { OpengraphContext, OpengraphResult } from '@hatk/hatk/opengraph'
155
-
156
- export default {
157
- path: '/og/${name}/:id',
158
- async generate(ctx: OpengraphContext): Promise<OpengraphResult> {
159
- const { db, params } = ctx
160
- return {
161
- element: {
162
- type: 'div',
163
- props: {
164
- style: { display: 'flex', width: '100%', height: '100%', background: '#080b12', color: 'white', alignItems: 'center', justifyContent: 'center' },
165
- children: params.id,
166
- },
167
- },
168
- }
169
- },
107
+ const templateDir = join(import.meta.dirname, 'templates');
108
+ function loadTemplate(file, name) {
109
+ const raw = readFileSync(join(templateDir, file), 'utf-8');
110
+ const capitalized = name.charAt(0).toUpperCase() + name.slice(1);
111
+ return raw.replaceAll('{{name}}', name).replaceAll('{{Name}}', capitalized);
170
112
  }
171
- `,
172
- job: (_name) => `export default {
173
- interval: 300, // seconds
174
- async run(_ctx: any) {
175
- // Periodic task logic here
176
- },
177
- }
178
- `,
179
- };
180
- // Compute relative import path from xrpc/ns/id/method.ts back to hatk.generated.ts
181
- // e.g. fm.teal.getStats → xrpc/fm/teal/getStats.ts → needs ../../../hatk.generated.ts
182
- // Parts: [fm, teal, getStats] → 2 namespace dirs + xrpc/ dir = 3 levels up
183
- function xrpcImportPath(nsid) {
184
- const parts = nsid.split('.');
185
- const namespaceDirs = parts.length - 1; // dirs created from namespace segments
186
- return '../'.repeat(namespaceDirs + 1) + 'hatk.generated.ts'; // +1 for xrpc/ dir itself
187
- }
188
- const testTemplates = {
189
- feed: (name) => `import { describe, test, expect, beforeAll, afterAll } from 'vitest'
190
- import { createTestContext } from '@hatk/hatk/test'
191
-
192
- let ctx: Awaited<ReturnType<typeof createTestContext>>
193
-
194
- beforeAll(async () => {
195
- ctx = await createTestContext()
196
- await ctx.loadFixtures()
197
- })
198
-
199
- afterAll(async () => ctx?.close())
200
-
201
- describe('${name} feed', () => {
202
- test('returns results', async () => {
203
- const feed = ctx.loadFeed('${name}')
204
- const result = await feed.generate(ctx.feedContext({ limit: 10 }))
205
- expect(result).toBeDefined()
206
- })
207
- })
208
- `,
209
- xrpc: (name) => `import { describe, test, expect, beforeAll, afterAll } from 'vitest'
210
- import { createTestContext } from '@hatk/hatk/test'
211
-
212
- let ctx: Awaited<ReturnType<typeof createTestContext>>
213
-
214
- beforeAll(async () => {
215
- ctx = await createTestContext()
216
- await ctx.loadFixtures()
217
- })
218
-
219
- afterAll(async () => ctx?.close())
220
-
221
- describe('${name}', () => {
222
- test('returns response', async () => {
223
- const handler = ctx.loadXrpc('${name}')
224
- const result = await handler.handler({ params: {} })
225
- expect(result).toBeDefined()
226
- })
227
- })
228
- `,
229
- };
113
+ const templateTypes = ['feed', 'xrpc', 'label', 'og', 'hook', 'setup'];
114
+ const testTemplateTypes = ['feed', 'xrpc'];
115
+ const templates = Object.fromEntries(templateTypes.map((t) => [t, (name) => loadTemplate(`${t}.tpl`, name)]));
116
+ const testTemplates = Object.fromEntries(testTemplateTypes.map((t) => [t, (name) => loadTemplate(`test-${t}.tpl`, name)]));
230
117
  const lexiconTemplates = {
231
118
  record: (nsid) => ({
232
119
  lexicon: 1,
@@ -293,768 +180,21 @@ const lexiconTemplates = {
293
180
  }),
294
181
  };
295
182
  const dirs = {
296
- feed: 'feeds',
297
- xrpc: 'xrpc',
298
- label: 'labels',
299
- og: 'og',
300
- job: 'jobs',
183
+ feed: 'server',
184
+ xrpc: 'server',
185
+ label: 'server',
186
+ og: 'server',
187
+ hook: 'server',
188
+ setup: 'server',
301
189
  };
302
190
  // --- Commands ---
303
191
  if (command === 'new') {
304
- const name = args[1];
305
- if (!name) {
306
- console.error('Usage: hatk new <name> [--svelte] [--template <template-name>]');
307
- process.exit(1);
308
- }
309
- const templateIdx = args.indexOf('--template');
310
- const templateName = templateIdx !== -1 ? args[templateIdx + 1] : null;
311
- if (templateIdx !== -1 && !templateName) {
312
- console.error('Usage: hatk new <name> --template <template-name>');
313
- process.exit(1);
314
- }
315
- const dir = resolve(name);
316
- if (existsSync(dir)) {
317
- console.error(`Directory ${name} already exists`);
318
- process.exit(1);
319
- }
320
- if (templateName) {
321
- const repo = `https://github.com/hatk-dev/hatk-template-${templateName}.git`;
322
- console.log(`Cloning template ${templateName}...`);
323
- try {
324
- execSync(`git clone --depth 1 ${repo} ${dir}`, { stdio: 'inherit' });
325
- }
326
- catch {
327
- console.error(`Failed to clone template: ${repo}`);
328
- process.exit(1);
329
- }
330
- execSync(`rm -rf ${join(dir, '.git')}`);
331
- const pkgPath = join(dir, 'package.json');
332
- if (existsSync(pkgPath)) {
333
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
334
- pkg.name = name;
335
- writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
336
- }
337
- console.log(`\nCreated ${name}/ from template ${templateName}`);
338
- console.log(`\n cd ${name}`);
339
- console.log(` npm install`);
340
- console.log(` hatk dev`);
341
- process.exit(0);
342
- }
343
- const withSvelte = args.includes('--svelte');
344
- mkdirSync(dir);
345
- const subs = [
346
- 'lexicons',
347
- 'feeds',
348
- 'xrpc',
349
- 'og',
350
- 'labels',
351
- 'jobs',
352
- 'seeds',
353
- 'setup',
354
- 'public',
355
- 'test',
356
- 'test/feeds',
357
- 'test/xrpc',
358
- 'test/integration',
359
- 'test/browser',
360
- 'test/fixtures',
361
- ];
362
- if (withSvelte)
363
- subs.push('src', 'src/routes', 'src/lib');
364
- for (const sub of subs) {
365
- mkdirSync(join(dir, sub));
366
- }
367
- writeFileSync(join(dir, 'config.yaml'), `relay: ws://localhost:2583
368
- plc: http://localhost:2582
369
- port: 3000
370
- database: data/hatk.db
371
- admins: []
372
-
373
- backfill:
374
- parallelism: 10
375
- `);
376
- writeFileSync(join(dir, 'public', 'index.html'), `<!DOCTYPE html>
377
- <html><head><title>${name}</title></head>
378
- <body><h1>${name}</h1></body></html>
379
- `);
380
- // Scaffold core framework lexicons under dev.hatk namespace
381
- const coreLexDir = join(dir, 'lexicons', 'dev', 'hatk');
382
- mkdirSync(coreLexDir, { recursive: true });
383
- writeFileSync(join(coreLexDir, 'describeCollections.json'), JSON.stringify({
384
- lexicon: 1,
385
- id: 'dev.hatk.describeCollections',
386
- defs: {
387
- main: {
388
- type: 'query',
389
- description: 'List indexed collections and their schemas.',
390
- output: {
391
- encoding: 'application/json',
392
- schema: {
393
- type: 'object',
394
- properties: {
395
- collections: {
396
- type: 'array',
397
- items: {
398
- type: 'object',
399
- required: ['collection'],
400
- properties: {
401
- collection: { type: 'string' },
402
- columns: {
403
- type: 'array',
404
- items: {
405
- type: 'object',
406
- required: ['name', 'originalName', 'type', 'required'],
407
- properties: {
408
- name: { type: 'string' },
409
- originalName: { type: 'string' },
410
- type: { type: 'string' },
411
- required: { type: 'boolean' },
412
- },
413
- },
414
- },
415
- },
416
- },
417
- },
418
- },
419
- },
420
- },
421
- },
422
- },
423
- }, null, 2) + '\n');
424
- writeFileSync(join(coreLexDir, 'describeFeeds.json'), JSON.stringify({
425
- lexicon: 1,
426
- id: 'dev.hatk.describeFeeds',
427
- defs: {
428
- main: {
429
- type: 'query',
430
- description: 'List available feeds.',
431
- output: {
432
- encoding: 'application/json',
433
- schema: {
434
- type: 'object',
435
- properties: {
436
- feeds: {
437
- type: 'array',
438
- items: {
439
- type: 'object',
440
- required: ['name', 'label'],
441
- properties: {
442
- name: { type: 'string' },
443
- label: { type: 'string' },
444
- },
445
- },
446
- },
447
- },
448
- },
449
- },
450
- },
451
- },
452
- }, null, 2) + '\n');
453
- writeFileSync(join(coreLexDir, 'describeLabels.json'), JSON.stringify({
454
- lexicon: 1,
455
- id: 'dev.hatk.describeLabels',
456
- defs: {
457
- main: {
458
- type: 'query',
459
- description: 'List available label definitions.',
460
- output: {
461
- encoding: 'application/json',
462
- schema: {
463
- type: 'object',
464
- properties: {
465
- definitions: {
466
- type: 'array',
467
- items: {
468
- type: 'object',
469
- required: ['identifier', 'severity', 'blurs', 'defaultSetting'],
470
- properties: {
471
- identifier: { type: 'string' },
472
- severity: { type: 'string' },
473
- blurs: { type: 'string' },
474
- defaultSetting: { type: 'string' },
475
- },
476
- },
477
- },
478
- },
479
- },
480
- },
481
- },
482
- },
483
- }, null, 2) + '\n');
484
- writeFileSync(join(coreLexDir, 'createRecord.json'), JSON.stringify({
485
- lexicon: 1,
486
- id: 'dev.hatk.createRecord',
487
- defs: {
488
- main: {
489
- type: 'procedure',
490
- description: "Create a record via the user's PDS.",
491
- input: {
492
- encoding: 'application/json',
493
- schema: {
494
- type: 'object',
495
- required: ['collection', 'repo', 'record'],
496
- properties: {
497
- collection: { type: 'string' },
498
- repo: { type: 'string', format: 'did' },
499
- record: { type: 'unknown' },
500
- },
501
- },
502
- },
503
- output: {
504
- encoding: 'application/json',
505
- schema: {
506
- type: 'object',
507
- properties: {
508
- uri: { type: 'string', format: 'at-uri' },
509
- cid: { type: 'string', format: 'cid' },
510
- },
511
- },
512
- },
513
- },
514
- },
515
- }, null, 2) + '\n');
516
- writeFileSync(join(coreLexDir, 'deleteRecord.json'), JSON.stringify({
517
- lexicon: 1,
518
- id: 'dev.hatk.deleteRecord',
519
- defs: {
520
- main: {
521
- type: 'procedure',
522
- description: "Delete a record via the user's PDS.",
523
- input: {
524
- encoding: 'application/json',
525
- schema: {
526
- type: 'object',
527
- required: ['collection', 'rkey'],
528
- properties: {
529
- collection: { type: 'string' },
530
- rkey: { type: 'string' },
531
- },
532
- },
533
- },
534
- output: { encoding: 'application/json', schema: { type: 'object', properties: {} } },
535
- },
536
- },
537
- }, null, 2) + '\n');
538
- writeFileSync(join(coreLexDir, 'putRecord.json'), JSON.stringify({
539
- lexicon: 1,
540
- id: 'dev.hatk.putRecord',
541
- defs: {
542
- main: {
543
- type: 'procedure',
544
- description: "Create or update a record via the user's PDS.",
545
- input: {
546
- encoding: 'application/json',
547
- schema: {
548
- type: 'object',
549
- required: ['collection', 'rkey', 'record'],
550
- properties: {
551
- collection: { type: 'string' },
552
- rkey: { type: 'string' },
553
- record: { type: 'unknown' },
554
- repo: { type: 'string', format: 'did' },
555
- },
556
- },
557
- },
558
- output: {
559
- encoding: 'application/json',
560
- schema: {
561
- type: 'object',
562
- properties: {
563
- uri: { type: 'string', format: 'at-uri' },
564
- cid: { type: 'string', format: 'cid' },
565
- },
566
- },
567
- },
568
- },
569
- },
570
- }, null, 2) + '\n');
571
- writeFileSync(join(coreLexDir, 'uploadBlob.json'), JSON.stringify({
572
- lexicon: 1,
573
- id: 'dev.hatk.uploadBlob',
574
- defs: {
575
- main: {
576
- type: 'procedure',
577
- description: "Upload a blob via the user's PDS.",
578
- input: {
579
- encoding: '*/*',
580
- },
581
- output: {
582
- encoding: 'application/json',
583
- schema: {
584
- type: 'object',
585
- required: ['blob'],
586
- properties: {
587
- blob: { type: 'blob' },
588
- },
589
- },
590
- },
591
- },
592
- },
593
- }, null, 2) + '\n');
594
- writeFileSync(join(coreLexDir, 'getFeed.json'), JSON.stringify({
595
- lexicon: 1,
596
- id: 'dev.hatk.getFeed',
597
- defs: {
598
- main: {
599
- type: 'query',
600
- description: 'Retrieve a named feed of items.',
601
- parameters: {
602
- type: 'params',
603
- required: ['feed'],
604
- properties: {
605
- feed: { type: 'string', description: 'Feed name' },
606
- limit: { type: 'integer', minimum: 1, maximum: 100, default: 30 },
607
- cursor: { type: 'string' },
608
- },
609
- },
610
- output: {
611
- encoding: 'application/json',
612
- schema: {
613
- type: 'object',
614
- required: ['items'],
615
- properties: {
616
- items: { type: 'array', items: { type: 'unknown' } },
617
- cursor: { type: 'string' },
618
- },
619
- },
620
- },
621
- },
622
- },
623
- }, null, 2) + '\n');
624
- writeFileSync(join(coreLexDir, 'getRecord.json'), JSON.stringify({
625
- lexicon: 1,
626
- id: 'dev.hatk.getRecord',
627
- defs: {
628
- main: {
629
- type: 'query',
630
- description: 'Fetch a single record by AT URI.',
631
- parameters: {
632
- type: 'params',
633
- required: ['uri'],
634
- properties: {
635
- uri: { type: 'string', format: 'at-uri' },
636
- },
637
- },
638
- output: {
639
- encoding: 'application/json',
640
- schema: {
641
- type: 'object',
642
- properties: {
643
- record: { type: 'unknown' },
644
- },
645
- },
646
- },
647
- },
648
- },
649
- }, null, 2) + '\n');
650
- writeFileSync(join(coreLexDir, 'getRecords.json'), JSON.stringify({
651
- lexicon: 1,
652
- id: 'dev.hatk.getRecords',
653
- defs: {
654
- main: {
655
- type: 'query',
656
- description: 'List records from a collection with optional filters.',
657
- parameters: {
658
- type: 'params',
659
- required: ['collection'],
660
- properties: {
661
- collection: { type: 'string' },
662
- limit: { type: 'integer', minimum: 1, maximum: 100, default: 20 },
663
- cursor: { type: 'string' },
664
- sort: { type: 'string' },
665
- order: { type: 'string' },
666
- },
667
- },
668
- output: {
669
- encoding: 'application/json',
670
- schema: {
671
- type: 'object',
672
- required: ['items'],
673
- properties: {
674
- items: { type: 'array', items: { type: 'unknown' } },
675
- cursor: { type: 'string' },
676
- },
677
- },
678
- },
679
- },
680
- },
681
- }, null, 2) + '\n');
682
- writeFileSync(join(coreLexDir, 'searchRecords.json'), JSON.stringify({
683
- lexicon: 1,
684
- id: 'dev.hatk.searchRecords',
685
- defs: {
686
- main: {
687
- type: 'query',
688
- description: 'Full-text search across a collection.',
689
- parameters: {
690
- type: 'params',
691
- required: ['collection', 'q'],
692
- properties: {
693
- collection: { type: 'string' },
694
- q: { type: 'string', description: 'Search query' },
695
- limit: { type: 'integer', minimum: 1, maximum: 100, default: 20 },
696
- cursor: { type: 'string' },
697
- fuzzy: { type: 'boolean', default: true },
698
- },
699
- },
700
- output: {
701
- encoding: 'application/json',
702
- schema: {
703
- type: 'object',
704
- required: ['items'],
705
- properties: {
706
- items: { type: 'array', items: { type: 'unknown' } },
707
- cursor: { type: 'string' },
708
- },
709
- },
710
- },
711
- },
712
- },
713
- }, null, 2) + '\n');
714
- writeFileSync(join(dir, 'seeds', 'seed.ts'), `import { seed } from '../hatk.generated.ts'
715
-
716
- const { createAccount, createRecord } = seed()
717
-
718
- const alice = await createAccount('alice.test')
719
-
720
- // await createRecord(alice, 'your.collection.here', {
721
- // field: 'value',
722
- // }, { rkey: 'my-record' })
723
-
724
- console.log('\\n[seed] Done!')
725
- `);
726
- writeFileSync(join(dir, 'docker-compose.yml'), `services:
727
- plc:
728
- build:
729
- context: https://github.com/did-method-plc/did-method-plc.git
730
- dockerfile: packages/server/Dockerfile
731
- ports:
732
- - '2582:2582'
733
- environment:
734
- - DATABASE_URL=postgres://plc:plc@postgres:5432/plc
735
- - PORT=2582
736
- command: ['dumb-init', 'node', '--enable-source-maps', '../dist/bin.js']
737
- depends_on:
738
- postgres:
739
- condition: service_healthy
740
- healthcheck:
741
- test: ['CMD-SHELL', 'wget -q --spider http://localhost:2582/_health || exit 1']
742
- interval: 2s
743
- timeout: 5s
744
- retries: 15
745
-
746
- pds:
747
- image: ghcr.io/bluesky-social/pds:latest
748
- ports:
749
- - '2583:2583'
750
- environment:
751
- - PDS_HOSTNAME=localhost
752
- - PDS_PORT=2583
753
- - PDS_DID_PLC_URL=http://plc:2582
754
- - PDS_DATA_DIRECTORY=/pds
755
- - PDS_BLOBSTORE_DISK_LOCATION=/pds/blobs
756
- - PDS_JWT_SECRET=dev-jwt-secret
757
- - PDS_ADMIN_PASSWORD=dev-admin
758
- - PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
759
- - PDS_INVITE_REQUIRED=false
760
- - PDS_DEV_MODE=true
761
- - LOG_ENABLED=true
762
- volumes:
763
- - pds_data:/pds
764
- depends_on:
765
- plc:
766
- condition: service_healthy
767
- healthcheck:
768
- test: ['CMD-SHELL', 'wget -q --spider http://localhost:2583/xrpc/_health || exit 1']
769
- interval: 2s
770
- timeout: 5s
771
- retries: 15
772
-
773
- postgres:
774
- image: postgres:16-alpine
775
- environment:
776
- - POSTGRES_USER=plc
777
- - POSTGRES_PASSWORD=plc
778
- - POSTGRES_DB=plc
779
- volumes:
780
- - plc_data:/var/lib/postgresql/data
781
- healthcheck:
782
- test: ['CMD-SHELL', 'pg_isready -U plc']
783
- interval: 2s
784
- timeout: 5s
785
- retries: 10
786
-
787
- volumes:
788
- pds_data:
789
- plc_data:
790
- `);
791
- writeFileSync(join(dir, '.dockerignore'), `node_modules
792
- data
793
- .svelte-kit
794
- public
795
- `);
796
- writeFileSync(join(dir, 'Dockerfile'), `FROM node:25-slim
797
- WORKDIR /app
798
- COPY package.json package-lock.json ./
799
- RUN npm ci
800
- COPY . .
801
- RUN node_modules/.bin/hatk build
802
- RUN npm prune --omit=dev
803
- EXPOSE 3000
804
- CMD ["node", "node_modules/@hatk/hatk/dist/main.js", "config.yaml"]
805
- `);
806
- const pkgDeps = { '@hatk/oauth-client': '*', hatk: '*' };
807
- const pkgDevDeps = {
808
- '@playwright/test': '^1',
809
- oxfmt: '^0.35.0',
810
- oxlint: '^1',
811
- typescript: '^5',
812
- vite: '^6',
813
- vitest: '^4',
814
- '@types/node': '^22',
815
- };
816
- if (withSvelte) {
817
- pkgDevDeps['@sveltejs/adapter-static'] = '^3';
818
- pkgDevDeps['@sveltejs/kit'] = '^2';
819
- pkgDevDeps['@sveltejs/vite-plugin-svelte'] = '^5';
820
- pkgDevDeps['svelte'] = '^5';
821
- pkgDevDeps['svelte-check'] = '^4';
822
- }
823
- writeFileSync(join(dir, 'package.json'), JSON.stringify({
824
- name,
825
- private: true,
826
- type: 'module',
827
- scripts: {
828
- start: 'hatk start',
829
- dev: 'hatk dev',
830
- build: 'hatk build',
831
- check: 'hatk check',
832
- format: 'hatk format',
833
- },
834
- dependencies: pkgDeps,
835
- devDependencies: pkgDevDeps,
836
- }, null, 2) + '\n');
837
- writeFileSync(join(dir, 'tsconfig.server.json'), JSON.stringify({
838
- compilerOptions: {
839
- target: 'ES2022',
840
- module: 'Node16',
841
- moduleResolution: 'Node16',
842
- strict: true,
843
- esModuleInterop: true,
844
- skipLibCheck: true,
845
- noEmit: true,
846
- allowImportingTsExtensions: true,
847
- resolveJsonModule: true,
848
- },
849
- include: ['feeds', 'xrpc', 'og', 'seeds', 'labels', 'jobs', 'setup', 'hatk.generated.ts'],
850
- }, null, 2) + '\n');
851
- writeFileSync(join(dir, 'playwright.config.ts'), `import { defineConfig } from '@playwright/test'
852
-
853
- export default defineConfig({
854
- testDir: 'test/browser',
855
- use: { baseURL: 'http://127.0.0.1:3000' },
856
- globalSetup: './test/browser/global-setup.ts',
857
- })
858
- `);
859
- writeFileSync(join(dir, 'test/browser/global-setup.ts'), `import { execSync } from 'node:child_process'
860
- import { existsSync } from 'node:fs'
861
-
862
- export default function globalSetup() {
863
- if (existsSync('src/app.html')) {
864
- execSync('npx vite build', { stdio: 'inherit' })
865
- }
866
- }
867
- `);
868
- writeFileSync(join(dir, '.gitignore'), `node_modules/
869
- *.db
870
- data/
871
- test-results/
872
- .svelte-kit/
873
- .DS_Store
874
- public/
875
- `);
876
- writeFileSync(join(dir, '.oxlintrc.json'), `{
877
- "ignorePatterns": ["public", "data", ".svelte-kit", "hatk.generated.ts"]
878
- }
879
- `);
880
- writeFileSync(join(dir, '.oxfmtrc.json'), `{
881
- "semi": false,
882
- "singleQuote": true,
883
- "trailingComma": "all",
884
- "printWidth": 120,
885
- "tabWidth": 2,
886
- "ignorePatterns": ["public", "data", ".svelte-kit", "hatk.generated.ts"]
887
- }
888
- `);
889
- if (withSvelte) {
890
- writeFileSync(join(dir, 'svelte.config.js'), `import adapter from '@sveltejs/adapter-static'
891
-
892
- export default {
893
- kit: {
894
- adapter: adapter({
895
- pages: 'public',
896
- assets: 'public',
897
- fallback: 'index.html',
898
- }),
899
- paths: { base: '' },
900
- alias: {
901
- $hatk: './hatk.generated.ts',
902
- },
903
- },
904
- }
905
- `);
906
- writeFileSync(join(dir, 'vite.config.ts'), `import { sveltekit } from '@sveltejs/kit/vite'
907
- import { hatk } from '@hatk/hatk/vite-plugin'
908
- import { defineConfig } from 'vite'
909
-
910
- export default defineConfig({
911
- plugins: [sveltekit(), hatk()],
912
- })
913
- `);
914
- writeFileSync(join(dir, 'tsconfig.json'), JSON.stringify({
915
- extends: './.svelte-kit/tsconfig.json',
916
- compilerOptions: {
917
- allowJs: true,
918
- checkJs: false,
919
- esModuleInterop: true,
920
- forceConsistentCasingInFileNames: true,
921
- resolveJsonModule: true,
922
- skipLibCheck: true,
923
- sourceMap: true,
924
- strict: true,
925
- moduleResolution: 'bundler',
926
- allowImportingTsExtensions: true,
927
- },
928
- }, null, 2) + '\n');
929
- writeFileSync(join(dir, 'src/app.html'), `<!doctype html>
930
- <html lang="en">
931
- <head>
932
- <meta charset="utf-8" />
933
- <meta name="viewport" content="width=device-width, initial-scale=1" />
934
- <meta name="description" content="${name}" />
935
- <title>${name}</title>
936
- %sveltekit.head%
937
- </head>
938
- <body data-sveltekit-preload-data="hover">
939
- <div style="display: contents">%sveltekit.body%</div>
940
- </body>
941
- </html>
942
- `);
943
- writeFileSync(join(dir, 'src/app.css'), `*,
944
- *::before,
945
- *::after {
946
- box-sizing: border-box;
947
- margin: 0;
948
- padding: 0;
949
- }
950
-
951
- :root {
952
- --bg-root: #080b12;
953
- --bg-surface: #0f1419;
954
- --bg-elevated: #161d27;
955
- --bg-hover: #1c2633;
956
- --border: #1e293b;
957
- --teal: #14b8a6;
958
- --text-primary: #e2e8f0;
959
- --text-secondary: #94a3b8;
960
- --text-muted: #64748b;
961
- }
962
-
963
- html {
964
- background: var(--bg-root);
965
- color: var(--text-primary);
966
- }
967
-
968
- body {
969
- font-family: -apple-system, system-ui, sans-serif;
970
- font-size: 15px;
971
- line-height: 1.5;
972
- min-height: 100vh;
973
- }
974
-
975
- a {
976
- color: inherit;
977
- text-decoration: none;
978
- }
979
- `);
980
- writeFileSync(join(dir, 'src/routes/+layout.svelte'), `<script lang="ts">
981
- import type { Snippet } from 'svelte'
982
- import '../app.css'
983
-
984
- let { children }: { children: Snippet } = $props()
985
- </script>
986
-
987
- {@render children()}
988
- `);
989
- writeFileSync(join(dir, 'src/routes/+page.svelte'), `<h1>${name}</h1>
990
- <p>Your hatk server is running.</p>
991
- `);
992
- writeFileSync(join(dir, 'src/error.html'), `<!doctype html>
993
- <html lang="en">
994
- <head>
995
- <meta charset="utf-8" />
996
- <meta name="viewport" content="width=device-width, initial-scale=1" />
997
- <title>%sveltekit.error.message% — ${name}</title>
998
- <style>
999
- * { box-sizing: border-box; margin: 0; padding: 0; }
1000
- body {
1001
- font-family: -apple-system, system-ui, sans-serif;
1002
- background: #080b12; color: #e2e8f0;
1003
- min-height: 100vh; display: flex; align-items: center; justify-content: center;
1004
- }
1005
- .error-page { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 8px; padding: 24px; }
1006
- .error-code { font-size: 72px; font-weight: 800; color: #14b8a6; line-height: 1; }
1007
- .error-title { font-size: 24px; font-weight: 800; }
1008
- .error-link {
1009
- margin-top: 16px; padding: 10px 24px; background: #14b8a6; color: #000;
1010
- border-radius: 20px; font-weight: 600; font-size: 14px; text-decoration: none;
1011
- }
1012
- </style>
1013
- </head>
1014
- <body>
1015
- <div class="error-page">
1016
- <span class="error-code">%sveltekit.status%</span>
1017
- <h1 class="error-title">%sveltekit.error.message%</h1>
1018
- <a href="/" class="error-link">Back to home</a>
1019
- </div>
1020
- </body>
1021
- </html>
1022
- `);
1023
- writeFileSync(join(dir, 'src/routes/+error.svelte'), `<script lang="ts">
1024
- import { page } from '$app/state'
1025
- </script>
1026
-
1027
- <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 80vh; gap: 8px;">
1028
- <span style="font-size: 72px; font-weight: 800; color: var(--teal);">{page.status}</span>
1029
- <h1 style="font-size: 24px; font-weight: 800;">{page.error?.message}</h1>
1030
- <a href="/" style="margin-top: 16px; padding: 10px 24px; background: var(--teal); color: #000; border-radius: 20px; font-weight: 600; font-size: 14px;">Back to home</a>
1031
- </div>
1032
- `);
1033
- }
1034
- console.log(`Created ${name}/`);
1035
- console.log(` config.yaml`);
1036
- console.log(` lexicons/ — lexicon JSON files (core + your own)`);
1037
- console.log(` feeds/ — feed generators`);
1038
- console.log(` xrpc/ — XRPC method handlers`);
1039
- console.log(` og/ — OpenGraph image routes`);
1040
- console.log(` labels/ — label definitions + rules`);
1041
- console.log(` jobs/ — periodic tasks`);
1042
- console.log(` seeds/ — seed fixture data (hatk seed)`);
1043
- console.log(` setup/ — boot-time setup scripts (run before server starts)`);
1044
- console.log(` test/ — test files (hatk test)`);
1045
- console.log(` public/ — static files`);
1046
- console.log(` docker-compose.yml — local PDS for development`);
1047
- console.log(` Dockerfile — production container`);
1048
- if (withSvelte) {
1049
- console.log(` src/ — SvelteKit frontend`);
1050
- console.log(` svelte.config.js`);
1051
- console.log(` vite.config.ts`);
1052
- }
1053
- // Generate types so the project is ready to go
1054
- execSync('npx hatk generate types', { stdio: 'inherit', cwd: dir });
1055
- if (withSvelte) {
1056
- execSync('npx svelte-kit sync', { stdio: 'inherit', cwd: dir });
1057
- }
192
+ console.error('`hatk new` has been removed. Create a new project with:');
193
+ console.error('');
194
+ console.error(' vp create github:hatk-dev/hatk-template-starter');
195
+ console.error('');
196
+ console.error('Install vp first: curl -fsSL https://vite.plus | bash');
197
+ process.exit(1);
1058
198
  }
1059
199
  else if (command === 'generate') {
1060
200
  const type = args[1];
@@ -1079,6 +219,21 @@ else if (command === 'generate') {
1079
219
  }
1080
220
  }
1081
221
  entries.sort((a, b) => a.nsid.localeCompare(b.nsid));
222
+ // Collect procedure nsids and blob-input nsids for client callXrpc
223
+ const procedureNsids = [];
224
+ const blobInputNsids = [];
225
+ for (const { nsid, defType } of entries) {
226
+ if (defType === 'procedure') {
227
+ const lex = lexicons.get(nsid);
228
+ const inputEncoding = lex?.defs?.main?.input?.encoding;
229
+ if (inputEncoding === '*/*') {
230
+ blobInputNsids.push(nsid);
231
+ }
232
+ else {
233
+ procedureNsids.push(nsid);
234
+ }
235
+ }
236
+ }
1082
237
  if (entries.length === 0) {
1083
238
  console.error('No lexicons found');
1084
239
  process.exit(1);
@@ -1126,7 +281,8 @@ else if (command === 'generate') {
1126
281
  let out = '// Auto-generated from lexicons. Do not edit.\n';
1127
282
  out += `import type { ${[...usedWrappers].sort().join(', ')}, LexServerParams, Checked, Prettify, StrictArg } from '@hatk/hatk/lex-types'\n`;
1128
283
  out += `import type { XrpcContext } from '@hatk/hatk/xrpc'\n`;
1129
- out += `import { defineFeed as _defineFeed, type FeedResult, type FeedContext, type HydrateContext } from '@hatk/hatk/feeds'\n`;
284
+ out += `import { callXrpc as _callXrpc } from '@hatk/hatk/xrpc'\n`;
285
+ out += `import { defineFeed as _defineFeed, type FeedResult, type FeedContext, type BaseContext, type Row } from '@hatk/hatk/feeds'\n`;
1130
286
  out += `import { seed as _seed, type SeedOpts } from '@hatk/hatk/seed'\n`;
1131
287
  // Emit ALL lexicons as `const ... = {...} as const` (including defs-only)
1132
288
  out += `\n// ─── Lexicon Definitions ────────────────────────────────────────────\n\n`;
@@ -1282,8 +438,13 @@ else if (command === 'generate') {
1282
438
  out += `}\n`;
1283
439
  // Emit Ctx helper for typesafe XRPC handler contexts
1284
440
  out += `\n// ─── XRPC Helpers ───────────────────────────────────────────────────\n\n`;
1285
- out += `export type { HydrateContext } from '@hatk/hatk/feeds'\n`;
441
+ out += `export type { BaseContext, Row } from '@hatk/hatk/feeds'\n`;
1286
442
  out += `export { InvalidRequestError, NotFoundError } from '@hatk/hatk/xrpc'\n`;
443
+ out += `export { defineSetup } from '@hatk/hatk/setup'\n`;
444
+ out += `export { defineHook } from '@hatk/hatk/hooks'\n`;
445
+ out += `export { defineLabel } from '@hatk/hatk/labels'\n`;
446
+ out += `export { defineOG } from '@hatk/hatk/opengraph'\n`;
447
+ out += `export { defineRenderer } from '@hatk/hatk/renderer'\n`;
1287
448
  out += `export type Ctx<K extends keyof XrpcSchema & keyof Registry> = XrpcContext<\n`;
1288
449
  out += ` LexServerParams<Registry[K], Registry>,\n`;
1289
450
  out += ` RecordRegistry,\n`;
@@ -1296,21 +457,29 @@ else if (command === 'generate') {
1296
457
  out += ` nsid: K,\n`;
1297
458
  out += ` handler: (ctx: Ctx<K> & { ok: <T extends OutputOf<K>>(value: StrictArg<T, OutputOf<K>>) => Checked<OutputOf<K>> }) => Promise<Checked<OutputOf<K>>>,\n`;
1298
459
  out += `) {\n`;
1299
- out += ` return { handler: (ctx: any) => handler({ ...ctx, ok: (v: any) => v }) }\n`;
460
+ out += ` return { __type: 'query' as const, nsid, handler: (ctx: any) => handler({ ...ctx, ok: (v: any) => v }) }\n`;
1300
461
  out += `}\n\n`;
1301
462
  out += `export function defineProcedure<K extends keyof XrpcSchema & string>(\n`;
1302
463
  out += ` nsid: K,\n`;
1303
464
  out += ` handler: (ctx: Ctx<K> & { ok: <T extends OutputOf<K>>(value: StrictArg<T, OutputOf<K>>) => Checked<OutputOf<K>> }) => Promise<Checked<OutputOf<K>>>,\n`;
1304
465
  out += `) {\n`;
1305
- out += ` return { handler: (ctx: any) => handler({ ...ctx, ok: (v: any) => v }) }\n`;
466
+ out += ` return { __type: 'procedure' as const, nsid, handler: (ctx: any) => handler({ ...ctx, ok: (v: any) => v }) }\n`;
467
+ out += `}\n\n`;
468
+ out += `// ─── Server-side XRPC Caller ────────────────────────────────────────\n\n`;
469
+ out += `type ExtractParams<T> = T extends { params: infer P } ? P : Record<string, unknown>\n`;
470
+ out += `export async function callXrpc<K extends keyof XrpcSchema & string>(\n`;
471
+ out += ` nsid: K,\n`;
472
+ out += ` params?: ExtractParams<XrpcSchema[K]>,\n`;
473
+ out += `): Promise<OutputOf<K>> {\n`;
474
+ out += ` return _callXrpc(nsid, params as any) as Promise<OutputOf<K>>\n`;
1306
475
  out += `}\n\n`;
1307
476
  out += `// ─── Feed & Seed Helpers ────────────────────────────────────────────\n\n`;
1308
477
  out += `type FeedGenerate = (ctx: FeedContext & { ok: (value: FeedResult) => Checked<FeedResult> }) => Promise<Checked<FeedResult>>\n`;
1309
478
  out += `export function defineFeed<K extends keyof RecordRegistry>(\n`;
1310
- out += ` opts: { collection: K; view?: string; label: string; generate: FeedGenerate; hydrate?: (ctx: HydrateContext<RecordRegistry[K]>) => Promise<unknown[]> }\n`;
479
+ out += ` opts: { collection: K; view?: string; label: string; generate: FeedGenerate; hydrate?: (ctx: BaseContext, items: Row<RecordRegistry[K]>[]) => Promise<unknown[]> }\n`;
1311
480
  out += `): ReturnType<typeof _defineFeed>\n`;
1312
481
  out += `export function defineFeed(\n`;
1313
- out += ` opts: { collection?: never; view?: never; label: string; generate: FeedGenerate; hydrate: (ctx: HydrateContext<any>) => Promise<unknown[]> }\n`;
482
+ out += ` opts: { collection?: never; view?: never; label: string; generate: FeedGenerate; hydrate: (ctx: BaseContext, items: Row<unknown>[]) => Promise<unknown[]> }\n`;
1314
483
  out += `): ReturnType<typeof _defineFeed>\n`;
1315
484
  out += `export function defineFeed(opts: any) { return _defineFeed(opts) }\n`;
1316
485
  out += `export function seed(opts?: SeedOpts) { return _seed<RecordRegistry>(opts) }\n`;
@@ -1333,7 +502,146 @@ else if (command === 'generate') {
1333
502
  out = out.replace(/import type \{ ([^}]+) \} from '@hatk\/hatk\/lex-types'/, `import type { ${[...usedWrappers].sort().join(', ')}, LexServerParams, Checked, Prettify, StrictArg } from '@hatk/hatk/lex-types'`);
1334
503
  }
1335
504
  writeFileSync(outPath, out);
505
+ // Generate client-safe version (types + callXrpc only, no server module re-exports)
506
+ // Types use `export type` from main file (erased at compile time, no runtime import).
507
+ // callXrpc imports from @hatk/hatk/xrpc directly to avoid pulling in server deps.
508
+ let clientOut = '// Auto-generated client-safe subset. Do not edit.\n';
509
+ clientOut += `// Import this in app components instead of hatk.generated.ts\n`;
510
+ clientOut += `// to avoid pulling in server-only dependencies.\n`;
511
+ clientOut += `export type { XrpcSchema } from './hatk.generated.ts'\n`;
512
+ clientOut += `import type { XrpcSchema } from './hatk.generated.ts'\n`;
513
+ // Re-export all types
514
+ const typeExports = [];
515
+ for (const { nsid, defType } of entries) {
516
+ if (!defType)
517
+ continue;
518
+ if (nsid === 'dev.hatk.createRecord' || nsid === 'dev.hatk.deleteRecord' || nsid === 'dev.hatk.putRecord')
519
+ continue;
520
+ typeExports.push(capitalize(varNames.get(nsid)));
521
+ }
522
+ if (recordEntries.length > 0) {
523
+ typeExports.push('RecordRegistry', 'CreateRecord', 'DeleteRecord', 'PutRecord');
524
+ }
525
+ // Named defs (views, objects) — collect from emittedDefNames minus main types
526
+ const mainTypeNames = new Set(entries.filter((e) => e.defType).map((e) => capitalize(varNames.get(e.nsid))));
527
+ for (const name of emittedDefNames) {
528
+ if (!mainTypeNames.has(name) && !typeExports.includes(name)) {
529
+ typeExports.push(name);
530
+ }
531
+ }
532
+ if (typeExports.length > 0) {
533
+ clientOut += `export type { ${typeExports.join(', ')} } from './hatk.generated.ts'\n`;
534
+ }
535
+ // Typed callXrpc — environment-aware:
536
+ // SSR: uses globalThis.__hatk_callXrpc bridge (direct handler invocation)
537
+ // Client: fetches via HTTP (GET for queries, POST for procedures, raw POST for blobs)
538
+ if (procedureNsids.length > 0) {
539
+ clientOut += `\nconst _procedures = new Set([${procedureNsids.map((n) => `'${n}'`).join(', ')}])\n`;
540
+ }
541
+ if (blobInputNsids.length > 0) {
542
+ clientOut += `const _blobInputs = new Set([${blobInputNsids.map((n) => `'${n}'`).join(', ')}])\n`;
543
+ }
544
+ clientOut += `\ntype CallArg<K extends keyof XrpcSchema> =\n`;
545
+ clientOut += ` XrpcSchema[K] extends { input: infer I } ? I :\n`;
546
+ clientOut += ` XrpcSchema[K] extends { params: infer P } ? P :\n`;
547
+ clientOut += ` Record<string, unknown>\n`;
548
+ clientOut += `type OutputOf<K extends keyof XrpcSchema> = XrpcSchema[K]['output']\n\n`;
549
+ clientOut += `export async function callXrpc<K extends keyof XrpcSchema & string>(\n`;
550
+ clientOut += ` nsid: K,\n`;
551
+ clientOut += ` arg?: CallArg<K>,\n`;
552
+ clientOut += ` customFetch?: typeof globalThis.fetch,\n`;
553
+ clientOut += `): Promise<OutputOf<K>> {\n`;
554
+ // Server-side bridge (skip when customFetch is provided — let SvelteKit's fetch handle it)
555
+ clientOut += ` if (typeof window === 'undefined' && !customFetch) {\n`;
556
+ clientOut += ` const bridge = (globalThis as any).__hatk_callXrpc\n`;
557
+ clientOut += ` if (!bridge) throw new Error('callXrpc: server bridge not available — is hatk initialized?')\n`;
558
+ if (procedureNsids.length > 0 || blobInputNsids.length > 0) {
559
+ const checks = [];
560
+ if (procedureNsids.length > 0)
561
+ checks.push('_procedures.has(nsid)');
562
+ if (blobInputNsids.length > 0)
563
+ checks.push('_blobInputs.has(nsid)');
564
+ clientOut += ` if (${checks.join(' || ')}) return bridge(nsid, {}, arg) as Promise<OutputOf<K>>\n`;
565
+ }
566
+ clientOut += ` return bridge(nsid, arg) as Promise<OutputOf<K>>\n`;
567
+ clientOut += ` }\n`;
568
+ // Client-side fetch (or server-side with customFetch for SSR deduplication)
569
+ clientOut += ` const _fetch = customFetch ?? globalThis.fetch\n`;
570
+ clientOut += ` // Use relative URL so SvelteKit's fetch can deduplicate server/client requests\n`;
571
+ clientOut += ` let path = \`/xrpc/\${nsid}\`\n`;
572
+ if (blobInputNsids.length > 0) {
573
+ clientOut += ` if (_blobInputs.has(nsid)) {\n`;
574
+ clientOut += ` const blob = arg as Blob | ArrayBuffer\n`;
575
+ clientOut += ` const ct = blob instanceof Blob ? blob.type : 'application/octet-stream'\n`;
576
+ clientOut += ` const res = await _fetch(path, { method: 'POST', headers: { 'Content-Type': ct }, body: blob })\n`;
577
+ clientOut += ` if (typeof window !== 'undefined' && res.status === 401) { const _b = await res.json().catch(() => ({})); const _h = _b.handle ?? getViewer()?.handle; window.location.href = _h ? \`/oauth/login?handle=\${encodeURIComponent(_h)}\` : '/oauth/login'; return new Promise(() => {}) as any }\n`;
578
+ clientOut += ` if (!res.ok) throw new Error(\`XRPC \${nsid} failed: \${res.status}\`)\n`;
579
+ clientOut += ` return res.json() as Promise<OutputOf<K>>\n`;
580
+ clientOut += ` }\n`;
581
+ }
582
+ if (procedureNsids.length > 0) {
583
+ clientOut += ` if (_procedures.has(nsid)) {\n`;
584
+ clientOut += ` const res = await _fetch(path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(arg) })\n`;
585
+ clientOut += ` if (typeof window !== 'undefined' && res.status === 401) { const _b = await res.json().catch(() => ({})); const _h = _b.handle ?? getViewer()?.handle; window.location.href = _h ? \`/oauth/login?handle=\${encodeURIComponent(_h)}\` : '/oauth/login'; return new Promise(() => {}) as any }\n`;
586
+ clientOut += ` if (!res.ok) throw new Error(\`XRPC \${nsid} failed: \${res.status}\`)\n`;
587
+ clientOut += ` return res.json() as Promise<OutputOf<K>>\n`;
588
+ clientOut += ` }\n`;
589
+ }
590
+ clientOut += ` const params = new URLSearchParams()\n`;
591
+ clientOut += ` for (const [k, v] of Object.entries(arg || {})) {\n`;
592
+ clientOut += ` if (v != null) params.set(k, String(v))\n`;
593
+ clientOut += ` }\n`;
594
+ clientOut += ` const qs = params.toString()\n`;
595
+ clientOut += ` if (qs) path += \`?\${qs}\`\n`;
596
+ clientOut += ` const res = await _fetch(path)\n`;
597
+ clientOut += ` if (typeof window !== 'undefined' && res.status === 401) { const _b = await res.json().catch(() => ({})); const _h = _b.handle ?? getViewer()?.handle; window.location.href = _h ? \`/oauth/login?handle=\${encodeURIComponent(_h)}\` : '/oauth/login'; return new Promise(() => {}) as any }\n`;
598
+ clientOut += ` if (!res.ok) throw new Error(\`XRPC \${nsid} failed: \${res.status}\`)\n`;
599
+ clientOut += ` return res.json() as Promise<OutputOf<K>>\n`;
600
+ clientOut += `}\n`;
601
+ // getViewer — returns the viewer set by layout load (server) or $effect (client)
602
+ clientOut += `\nexport function getViewer(): { did: string; handle: string } | null {\n`;
603
+ clientOut += ` return (globalThis as any).__hatk_viewer ?? null\n`;
604
+ clientOut += `}\n`;
605
+ // Auth helpers — login, logout, viewerDid
606
+ clientOut += `\n// ─── Auth Helpers ────────────────────────────────────────────────────\n\n`;
607
+ clientOut += `export async function login(handle: string): Promise<void> {\n`;
608
+ clientOut += ` const res = await fetch(\`/oauth/login?handle=\${encodeURIComponent(handle)}\`, { redirect: 'manual' })\n`;
609
+ clientOut += ` if (res.type === 'opaqueredirect') {\n`;
610
+ clientOut += ` window.location.href = \`/oauth/login?handle=\${encodeURIComponent(handle)}\`\n`;
611
+ clientOut += ` return\n`;
612
+ clientOut += ` }\n`;
613
+ clientOut += ` if (res.ok) return\n`;
614
+ clientOut += ` const body = await res.json().catch(() => ({ error: 'Login failed' }))\n`;
615
+ clientOut += ` throw new Error(body.error || 'Login failed')\n`;
616
+ clientOut += `}\n\n`;
617
+ clientOut += `export async function logout(): Promise<void> {\n`;
618
+ clientOut += ` ;(globalThis as any).__hatk_viewer = null\n`;
619
+ clientOut += ` await fetch('/auth/logout', { method: 'POST' }).catch(() => {})\n`;
620
+ clientOut += `}\n\n`;
621
+ clientOut += `export function viewerDid(): string | null {\n`;
622
+ clientOut += ` if (typeof window === 'undefined') return null\n`;
623
+ clientOut += ` const viewer = (globalThis as any).__hatk_viewer\n`;
624
+ clientOut += ` return viewer?.did ?? null\n`;
625
+ clientOut += `}\n\n`;
626
+ clientOut += `// Expose viewer for getViewer() bridge\n`;
627
+ clientOut += `;(globalThis as any).__hatk_auth = { viewerDid }\n`;
628
+ // parseViewer — server-side session cookie resolution for +layout.server.ts
629
+ clientOut += `\n// ─── Server Helpers ──────────────────────────────────────────────────\n\n`;
630
+ clientOut += `export async function parseViewer(cookies: { get(name: string): string | undefined }): Promise<{ did: string; handle?: string } | null> {\n`;
631
+ clientOut += ` const parseSessionCookie = (globalThis as any).__hatk_parseSessionCookie\n`;
632
+ clientOut += ` if (!parseSessionCookie) return null\n`;
633
+ clientOut += ` const cookieValue = cookies.get('__hatk_session')\n`;
634
+ clientOut += ` if (!cookieValue) return null\n`;
635
+ clientOut += ` try {\n`;
636
+ clientOut += ` const request = new Request('http://localhost', { headers: { cookie: \`__hatk_session=\${cookieValue}\` } })\n`;
637
+ clientOut += ` const viewer = await parseSessionCookie(request)\n`;
638
+ clientOut += ` if (viewer) (globalThis as any).__hatk_viewer = viewer\n`;
639
+ clientOut += ` return viewer\n`;
640
+ clientOut += ` } catch { return null }\n`;
641
+ clientOut += `}\n`;
642
+ writeFileSync('./hatk.generated.client.ts', clientOut);
1336
643
  console.log(`Generated ${outPath} with ${entries.length} types: ${entries.map((e) => capitalize(varNames.get(e.nsid))).join(', ')}`);
644
+ console.log(`Generated ./hatk.generated.client.ts (client-safe subset)`);
1337
645
  }
1338
646
  else if (lexiconTemplates[type]) {
1339
647
  const nsid = args[2];
@@ -1361,18 +669,9 @@ else if (command === 'generate') {
1361
669
  process.exit(1);
1362
670
  }
1363
671
  const baseDir = dirs[type];
1364
- let filePath;
1365
- if (type === 'xrpc') {
1366
- // NSID folder path: fm.teal.getStats → xrpc/fm/teal/getStats.ts
1367
- const parts = name.split('.');
1368
- const subDir = join(baseDir, ...parts.slice(0, -1));
1369
- mkdirSync(subDir, { recursive: true });
1370
- filePath = join(subDir, `${parts[parts.length - 1]}.ts`);
1371
- }
1372
- else {
1373
- mkdirSync(baseDir, { recursive: true });
1374
- filePath = join(baseDir, `${name}.ts`);
1375
- }
672
+ mkdirSync(baseDir, { recursive: true });
673
+ const fileName = type === 'xrpc' ? name.split('.').pop() : name;
674
+ const filePath = join(baseDir, `${fileName}.ts`);
1376
675
  if (existsSync(filePath)) {
1377
676
  console.error(`${filePath} already exists`);
1378
677
  process.exit(1);
@@ -1382,7 +681,7 @@ else if (command === 'generate') {
1382
681
  // Scaffold test file if template exists
1383
682
  const testTemplate = testTemplates[type];
1384
683
  if (testTemplate) {
1385
- const testDir = type === 'xrpc' ? 'test/xrpc' : `test/${baseDir}`;
684
+ const testDir = 'test/server';
1386
685
  mkdirSync(testDir, { recursive: true });
1387
686
  const testName = type === 'xrpc' ? name.split('.').pop() : name;
1388
687
  const testPath = join(testDir, `${testName}.test.ts`);
@@ -1401,18 +700,9 @@ else if (command === 'destroy') {
1401
700
  process.exit(1);
1402
701
  }
1403
702
  const baseDir = dirs[type];
1404
- let tsPath, jsPath;
1405
- if (type === 'xrpc') {
1406
- const parts = name.split('.');
1407
- const leaf = parts[parts.length - 1];
1408
- const subDir = join(baseDir, ...parts.slice(0, -1));
1409
- tsPath = join(subDir, `${leaf}.ts`);
1410
- jsPath = join(subDir, `${leaf}.js`);
1411
- }
1412
- else {
1413
- tsPath = join(baseDir, `${name}.ts`);
1414
- jsPath = join(baseDir, `${name}.js`);
1415
- }
703
+ const fileName = type === 'xrpc' ? name.split('.').pop() : name;
704
+ const tsPath = join(baseDir, `${fileName}.ts`);
705
+ const jsPath = join(baseDir, `${fileName}.js`);
1416
706
  const filePath = existsSync(tsPath) ? tsPath : existsSync(jsPath) ? jsPath : null;
1417
707
  if (!filePath) {
1418
708
  console.error(`No file found for ${type} "${name}"`);
@@ -1421,7 +711,7 @@ else if (command === 'destroy') {
1421
711
  unlinkSync(filePath);
1422
712
  console.log(`Removed ${filePath}`);
1423
713
  // Clean up test file
1424
- const testDir = type === 'xrpc' ? 'test/xrpc' : `test/${baseDir}`;
714
+ const testDir = 'test/server';
1425
715
  const testName = type === 'xrpc' ? name.split('.').pop() : name;
1426
716
  const testFile = join(testDir, `${testName}.test.ts`);
1427
717
  if (existsSync(testFile)) {
@@ -1435,25 +725,14 @@ else if (command === 'destroy') {
1435
725
  else if (command === 'dev') {
1436
726
  await ensurePds();
1437
727
  runSeed();
1438
- try {
1439
- if (existsSync(resolve('svelte.config.js')) && existsSync(resolve('src/app.html'))) {
1440
- // SvelteKit project — vite dev starts the hatk server via the plugin
1441
- execSync('npx vite dev', { stdio: 'inherit', cwd: process.cwd() });
1442
- }
1443
- else {
1444
- // No frontend — just run the hatk server directly
1445
- const mainPath = resolve(import.meta.dirname, 'main.js');
1446
- execSync(`npx tsx ${mainPath} config.yaml`, {
1447
- stdio: 'inherit',
1448
- cwd: process.cwd(),
1449
- env: { ...process.env, DEV_MODE: '1' },
1450
- });
1451
- }
728
+ if (existsSync(resolve('vite.config.ts')) || existsSync(resolve('vite.config.js'))) {
729
+ // Vite project vite dev starts the hatk server via the plugin
730
+ await spawnForward('npx', ['vite', 'dev']);
1452
731
  }
1453
- catch (e) {
1454
- if (e.signal === 'SIGINT' || e.signal === 'SIGTERM')
1455
- process.exit(0);
1456
- throw e;
732
+ else {
733
+ // No frontend just run the hatk server directly
734
+ const mainPath = resolve(import.meta.dirname, 'main.js');
735
+ await spawnForward('npx', ['tsx', mainPath, 'hatk.config.ts'], { DEV_MODE: '1' });
1457
736
  }
1458
737
  }
1459
738
  else if (command === 'format' || command === 'fmt') {
@@ -1473,9 +752,9 @@ else if (command === 'build') {
1473
752
  }
1474
753
  }
1475
754
  else if (command === 'reset') {
1476
- const config = loadConfig(resolve('config.yaml'));
755
+ const config = await loadConfig(resolve('hatk.config.ts'));
1477
756
  if (config.database !== ':memory:') {
1478
- for (const suffix of ['', '.wal']) {
757
+ for (const suffix of ['', '.wal', '-shm', '-wal']) {
1479
758
  const file = config.database + suffix;
1480
759
  if (existsSync(file)) {
1481
760
  unlinkSync(file);
@@ -1632,41 +911,9 @@ else if (command === 'resolve') {
1632
911
  console.log(`\nResolved ${resolved.size} lexicon(s). Regenerating types...`);
1633
912
  execSync('npx hatk generate types', { stdio: 'inherit', cwd: process.cwd() });
1634
913
  }
1635
- else if (command === 'schema') {
1636
- const config = loadConfig(resolve('config.yaml'));
1637
- if (config.database === ':memory:') {
1638
- console.error('No database file configured (database is :memory:)');
1639
- process.exit(1);
1640
- }
1641
- if (!existsSync(config.database)) {
1642
- console.error(`Database not found: ${config.database}`);
1643
- console.error('Run "hatk dev" first to create it.');
1644
- process.exit(1);
1645
- }
1646
- const { DuckDBInstance } = await import('@duckdb/node-api');
1647
- const instance = await DuckDBInstance.create(config.database);
1648
- const con = await instance.connect();
1649
- const tables = (await (await con.runAndReadAll(`SELECT table_name FROM information_schema.tables WHERE table_schema = 'main' ORDER BY table_name`)).getRowObjects());
1650
- for (const { table_name } of tables) {
1651
- console.log(`"${table_name}"`);
1652
- const cols = (await (await con.runAndReadAll(`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = '${table_name}' ORDER BY ordinal_position`)).getRowObjects());
1653
- for (const col of cols) {
1654
- const nullable = col.is_nullable === 'YES' ? '' : ' NOT NULL';
1655
- console.log(` ${col.column_name.padEnd(20)} ${col.data_type}${nullable}`);
1656
- }
1657
- console.log();
1658
- }
1659
- }
1660
914
  else if (command === 'start') {
1661
- try {
1662
- const mainPath = resolve(import.meta.dirname, 'main.js');
1663
- execSync(`npx tsx ${mainPath} config.yaml`, { stdio: 'inherit', cwd: process.cwd() });
1664
- }
1665
- catch (e) {
1666
- if (e.signal === 'SIGINT' || e.signal === 'SIGTERM')
1667
- process.exit(0);
1668
- throw e;
1669
- }
915
+ const mainPath = resolve(import.meta.dirname, 'main.js');
916
+ await spawnForward('npx', ['tsx', mainPath, 'hatk.config.ts']);
1670
917
  }
1671
918
  else {
1672
919
  usage();