@hatk/hatk 0.0.1-alpha.20 → 0.0.1-alpha.22

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.
package/dist/main.js CHANGED
@@ -3,7 +3,7 @@ import { mkdirSync } from 'node:fs';
3
3
  import { dirname, resolve } from 'node:path';
4
4
  import { log } from "./logger.js";
5
5
  import { loadConfig } from "./config.js";
6
- import { loadLexicons, storeLexicons, discoverCollections, generateTableSchema, generateCreateTableSQL, } from "./schema.js";
6
+ import { loadLexicons, storeLexicons, discoverCollections, buildSchemas, } from "./schema.js";
7
7
  import { discoverViews } from "./views.js";
8
8
  import { initDatabase, getCursor, querySQL } from "./db.js";
9
9
  import { initFeeds, listFeeds } from "./feeds.js";
@@ -17,7 +17,7 @@ import { validateLexicons } from '@bigmoves/lexicon';
17
17
  import { relayHttpUrl } from "./config.js";
18
18
  import { runBackfill } from "./backfill.js";
19
19
  import { initOAuth } from "./oauth/server.js";
20
- import { loadOnLoginHook } from "./oauth/hooks.js";
20
+ import { loadOnLoginHook } from "./hooks.js";
21
21
  import { initSetup } from "./setup.js";
22
22
  function logMemory(phase) {
23
23
  const mem = process.memoryUsage();
@@ -50,29 +50,14 @@ log(`[main] Loaded config: ${collections.length} collections`);
50
50
  // Discover view defs from lexicons
51
51
  discoverViews();
52
52
  await loadOnLoginHook(resolve(configDir, 'hooks'));
53
- const schemas = [];
54
- const ddlStatements = [];
55
- for (const nsid of collections) {
56
- const lexicon = lexicons.get(nsid);
57
- if (!lexicon) {
58
- log(`[main] No lexicon found for ${nsid}, using generic JSON storage`);
59
- const genericDDL = `CREATE TABLE IF NOT EXISTS "${nsid}" (
60
- uri TEXT PRIMARY KEY,
61
- cid TEXT,
62
- did TEXT NOT NULL,
63
- indexed_at TIMESTAMP NOT NULL,
64
- data JSON
65
- );
66
- CREATE INDEX IF NOT EXISTS idx_${nsid.replace(/\./g, '_')}_indexed ON "${nsid}"(indexed_at DESC);
67
- CREATE INDEX IF NOT EXISTS idx_${nsid.replace(/\./g, '_')}_author ON "${nsid}"(did);`;
68
- schemas.push({ collection: nsid, tableName: `"${nsid}"`, columns: [], refColumns: [], children: [], unions: [] });
69
- ddlStatements.push(genericDDL);
70
- continue;
53
+ const { schemas, ddlStatements } = buildSchemas(lexicons, collections);
54
+ for (const s of schemas) {
55
+ if (s.columns.length === 0) {
56
+ log(`[main] No lexicon found for ${s.collection}, using generic JSON storage`);
57
+ }
58
+ else {
59
+ log(`[main] Schema for ${s.collection}: ${s.columns.length} columns, ${s.unions.length} unions`);
71
60
  }
72
- const schema = generateTableSchema(nsid, lexicon, lexicons);
73
- schemas.push(schema);
74
- ddlStatements.push(generateCreateTableSQL(schema));
75
- log(`[main] Schema for ${nsid}: ${schema.columns.length} columns, ${schema.unions.length} unions`);
76
61
  }
77
62
  // 3. Ensure data directory exists and initialize DuckDB
78
63
  if (config.database !== ':memory:') {
package/dist/mst.d.ts CHANGED
@@ -1,7 +1,22 @@
1
+ /** A single entry from a Merkle Search Tree — a record path paired with its content CID. */
1
2
  export interface MstEntry {
3
+ /** Record path, e.g. "xyz.marketplace.listing/3mfniulnr7c2g" */
2
4
  path: string;
5
+ /** CID of the record's CBOR block */
3
6
  cid: string;
4
7
  }
8
+ /**
9
+ * Walk an AT Protocol Merkle Search Tree (MST) in key order, yielding every record entry.
10
+ *
11
+ * The MST is a prefix-compressed B+ tree used by AT Protocol repositories to map
12
+ * record paths to CIDs. Each node contains a left subtree pointer, an array of entries
13
+ * (each with a prefix length, key suffix, value CID, and right subtree pointer), and
14
+ * keys are reconstructed by combining the prefix of the previous key with the suffix.
15
+ *
16
+ * @param blocks - Block store that resolves CIDs to raw CBOR bytes
17
+ * @param rootCid - CID of the MST root node
18
+ * @yields {MstEntry} Record entries in lexicographic key order
19
+ */
5
20
  export declare function walkMst(blocks: {
6
21
  get(cid: string): Uint8Array | undefined;
7
22
  }, rootCid: string): Generator<MstEntry>;
package/dist/mst.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mst.d.ts","sourceRoot":"","sources":["../src/mst.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,wBAAiB,OAAO,CAAC,MAAM,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAA;CAAE,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CA8BnH"}
1
+ {"version":3,"file":"mst.d.ts","sourceRoot":"","sources":["../src/mst.ts"],"names":[],"mappings":"AAEA,4FAA4F;AAC5F,MAAM,WAAW,QAAQ;IACvB,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAA;IACZ,qCAAqC;IACrC,GAAG,EAAE,MAAM,CAAA;CACZ;AAED;;;;;;;;;;;GAWG;AACH,wBAAiB,OAAO,CAAC,MAAM,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAA;CAAE,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CA+BnH"}
package/dist/mst.js CHANGED
@@ -1,5 +1,18 @@
1
1
  import { cborDecode } from "./cbor.js";
2
+ /**
3
+ * Walk an AT Protocol Merkle Search Tree (MST) in key order, yielding every record entry.
4
+ *
5
+ * The MST is a prefix-compressed B+ tree used by AT Protocol repositories to map
6
+ * record paths to CIDs. Each node contains a left subtree pointer, an array of entries
7
+ * (each with a prefix length, key suffix, value CID, and right subtree pointer), and
8
+ * keys are reconstructed by combining the prefix of the previous key with the suffix.
9
+ *
10
+ * @param blocks - Block store that resolves CIDs to raw CBOR bytes
11
+ * @param rootCid - CID of the MST root node
12
+ * @yields {MstEntry} Record entries in lexicographic key order
13
+ */
2
14
  export function* walkMst(blocks, rootCid) {
15
+ /** Recursively visit an MST node, reconstructing full keys from prefix-compressed entries. */
3
16
  function* visit(cid, prefix) {
4
17
  const data = blocks.get(cid);
5
18
  if (!data)
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/oauth/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AA0E/C,wBAAsB,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAmBrG;AAID,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW;;;;;;;;;;;;;;;;;;;EAqBxE;AAED,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW;;;;;EAO/E;AAED,wBAAgB,OAAO;;;;;;;;;;;;;;;;;;;;;;EAWtB;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW;;;;;;;;;EAcpE;AAID,wBAAsB,SAAS,CAC7B,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAuItD;AAID,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,GAAG,MAAM,CAShF;AAID,wBAAsB,cAAc,CAClC,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,GAAG,EAAE,MAAM,GAAG,IAAI,GACjB,OAAO,CAAC;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,GAAG,IAAI,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M,CAAA;CAAE,GAAG,IAAI,CAAC,CAmEpF;AAID,wBAAsB,YAAY,CAChC,UAAU,EAAE,MAAM,GAAG,IAAI,EACzB,UAAU,EAAE,MAAM,GAAG,IAAI,EACzB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CA0BjC"}
@@ -6,7 +6,7 @@ import { discoverAuthServer, resolveHandle } from "./discovery.js";
6
6
  import { getServerKey, storeServerKey, storeOAuthRequest, getOAuthRequest, deleteOAuthRequest, storeAuthCode, consumeAuthCode, storeSession, checkAndStoreDpopJti, cleanupExpiredOAuth, storeRefreshToken, getRefreshToken, revokeRefreshToken, } from "./db.js";
7
7
  import { emit } from "../logger.js";
8
8
  import { querySQL } from "../db.js";
9
- import { fireOnLoginHook } from "./hooks.js";
9
+ import { fireOnLoginHook } from "../hooks.js";
10
10
  const SERVER_KEY_KID = 'appview-oauth-key';
11
11
  async function resolveHandleForDid(did) {
12
12
  const rows = (await querySQL('SELECT handle FROM _repos WHERE did = $1', [did]));
@@ -146,7 +146,12 @@ export async function handlePar(config, body, dpopHeader, requestUrl) {
146
146
  // Resolve DID from login_hint
147
147
  let did = body.login_hint;
148
148
  if (did && !did.startsWith('did:')) {
149
- did = await resolveHandle(did, _relayUrl);
149
+ try {
150
+ did = await resolveHandle(did, _relayUrl);
151
+ }
152
+ catch {
153
+ throw new Error('Handle not found');
154
+ }
150
155
  }
151
156
  // Discover user's PDS auth server
152
157
  let pdsRequestUri;
package/dist/schema.d.ts CHANGED
@@ -48,4 +48,12 @@ export declare function getAllLexicons(): Array<{
48
48
  export declare function getLexiconArray(): any[];
49
49
  export declare function generateTableSchema(nsid: string, lexicon: any, lexicons?: Map<string, any>): TableSchema;
50
50
  export declare function generateCreateTableSQL(schema: TableSchema): string;
51
+ /**
52
+ * Build table schemas and DDL from lexicons and collections.
53
+ * Shared by main.ts (server boot) and cli.ts (hatk schema command).
54
+ */
55
+ export declare function buildSchemas(lexicons: Map<string, any>, collections: string[]): {
56
+ schemas: TableSchema[];
57
+ ddlStatements: string[];
58
+ };
51
59
  //# sourceMappingURL=schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,OAAO,CAAA;CACf;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,SAAS,EAAE,CAAA;IACpB,OAAO,EAAE,OAAO,CAAA;IAChB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,iBAAiB,EAAE,CAAA;CAC9B;AAED,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,SAAS,EAAE,CAAA;IACpB,UAAU,EAAE,MAAM,EAAE,CAAA;IACpB,QAAQ,EAAE,gBAAgB,EAAE,CAAA;IAC5B,MAAM,EAAE,gBAAgB,EAAE,CAAA;CAC3B;AAED,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,MAAM,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,SAAS,EAAE,CAAA;CACrB;AAGD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AA8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AAG,EAAE,QAAQ,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,WAAW,CA0GxG;AAGD,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAoElE;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,WAAW,EAAE,MAAM,EAAE,GACpB;IAAE,OAAO,EAAE,WAAW,EAAE,CAAC;IAAC,aAAa,EAAE,MAAM,EAAE,CAAA;CAAE,CA2BrD"}
package/dist/schema.js CHANGED
@@ -356,3 +356,32 @@ export function generateCreateTableSQL(schema) {
356
356
  }
357
357
  return [createTable, ...indexes, ...childDDL].join('\n');
358
358
  }
359
+ /**
360
+ * Build table schemas and DDL from lexicons and collections.
361
+ * Shared by main.ts (server boot) and cli.ts (hatk schema command).
362
+ */
363
+ export function buildSchemas(lexicons, collections) {
364
+ const schemas = [];
365
+ const ddlStatements = [];
366
+ for (const nsid of collections) {
367
+ const lexicon = lexicons.get(nsid);
368
+ if (!lexicon) {
369
+ const genericDDL = `CREATE TABLE IF NOT EXISTS "${nsid}" (
370
+ uri TEXT PRIMARY KEY,
371
+ cid TEXT,
372
+ did TEXT NOT NULL,
373
+ indexed_at TIMESTAMP NOT NULL,
374
+ data JSON
375
+ );
376
+ CREATE INDEX IF NOT EXISTS idx_${nsid.replace(/\./g, '_')}_indexed ON "${nsid}"(indexed_at DESC);
377
+ CREATE INDEX IF NOT EXISTS idx_${nsid.replace(/\./g, '_')}_author ON "${nsid}"(did);`;
378
+ schemas.push({ collection: nsid, tableName: `"${nsid}"`, columns: [], refColumns: [], children: [], unions: [] });
379
+ ddlStatements.push(genericDDL);
380
+ continue;
381
+ }
382
+ const schema = generateTableSchema(nsid, lexicon, lexicons);
383
+ schemas.push(schema);
384
+ ddlStatements.push(generateCreateTableSQL(schema));
385
+ }
386
+ return { schemas, ddlStatements };
387
+ }
package/dist/seed.d.ts CHANGED
@@ -1,8 +1,10 @@
1
+ /** Authenticated PDS session — returned by {@link seed.createAccount}. */
1
2
  export type Session = {
2
3
  did: string;
3
4
  accessJwt: string;
4
5
  handle: string;
5
6
  };
7
+ /** AT Protocol blob reference, as returned by `com.atproto.repo.uploadBlob`. */
6
8
  export type BlobRef = {
7
9
  $type: 'blob';
8
10
  ref: {
@@ -11,11 +13,23 @@ export type BlobRef = {
11
13
  mimeType: string;
12
14
  size: number;
13
15
  };
16
+ /** Options for the seed helper. All fields fall back to env vars or sensible defaults. */
14
17
  export type SeedOpts = {
15
18
  pds?: string;
16
19
  password?: string;
17
20
  lexicons?: string;
18
21
  };
22
+ /**
23
+ * Create a seed helper for populating a local PDS with test data.
24
+ *
25
+ * Returns `createAccount`, `createRecord`, and `uploadBlob` functions bound to
26
+ * the target PDS. Records are validated against the project's lexicons before
27
+ * being written. Generic parameter `R` maps collection NSIDs to their record types
28
+ * for type-safe seeding.
29
+ *
30
+ * @typeParam R - Map of collection NSID → record type (defaults to untyped)
31
+ * @param opts - PDS URL, password, and lexicon directory overrides
32
+ */
19
33
  export declare function seed<R extends Record<string, unknown> = Record<string, unknown>>(opts?: SeedOpts): {
20
34
  createAccount: (handle: string) => Promise<Session>;
21
35
  createRecord: <K extends keyof R & string>(session: Session, collection: K, record: R[K] extends Record<string, unknown> ? R[K] : Record<string, unknown>, opts: {
@@ -23,6 +37,11 @@ export declare function seed<R extends Record<string, unknown> = Record<string,
23
37
  }) => Promise<{
24
38
  uri: string;
25
39
  cid: string;
40
+ commit: {
41
+ cid: string;
42
+ rev: string;
43
+ };
44
+ validationStatus: string;
26
45
  }>;
27
46
  uploadBlob: (session: Session, filePath: string) => Promise<BlobRef>;
28
47
  };
@@ -1 +1 @@
1
- {"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../src/seed.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,OAAO,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,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.ts`. It runs during `hatk dev` to create
5
+ * accounts and records against your local PDS. Records are validated against
6
+ * your project's lexicons before being written.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // seeds/seed.ts
11
+ * import { seed } from '../hatk.generated.ts'
12
+ *
13
+ * const { createAccount, createRecord } = seed()
14
+ *
15
+ * const alice = await createAccount('alice.test')
16
+ * const bob = await createAccount('bob.test')
17
+ *
18
+ * await createRecord(
19
+ * alice,
20
+ * 'xyz.statusphere.status',
21
+ * { status: '👍', createdAt: new Date().toISOString() },
22
+ * { rkey: 'status1' },
23
+ * )
24
+ * ```
25
+ */
1
26
  import { loadLexicons } from "./schema.js";
2
27
  import { validateRecord } from '@bigmoves/lexicon';
3
28
  import { resolve } from 'node:path';
4
29
  import { readFileSync } from 'node:fs';
30
+ /**
31
+ * Create a seed helper for populating a local PDS with test data.
32
+ *
33
+ * Returns `createAccount`, `createRecord`, and `uploadBlob` functions bound to
34
+ * the target PDS. Records are validated against the project's lexicons before
35
+ * being written. Generic parameter `R` maps collection NSIDs to their record types
36
+ * for type-safe seeding.
37
+ *
38
+ * @typeParam R - Map of collection NSID → record type (defaults to untyped)
39
+ * @param opts - PDS URL, password, and lexicon directory overrides
40
+ */
5
41
  export function seed(opts) {
6
42
  const pdsUrl = opts?.pds || process.env.PDS_URL || 'http://localhost:2583';
7
43
  const password = opts?.password || process.env.SEED_PASSWORD || 'password';
8
44
  const lexiconsDir = resolve(opts?.lexicons || 'lexicons');
9
45
  const lexiconArray = [...loadLexicons(lexiconsDir).values()];
46
+ /** Create a PDS account (or reuse an existing one) and return an authenticated session. */
10
47
  async function createAccount(handle) {
11
48
  const res = await fetch(`${pdsUrl}/xrpc/com.atproto.server.createAccount`, {
12
49
  method: 'POST',
@@ -34,6 +71,7 @@ export function seed(opts) {
34
71
  const session = (await sessionRes.json());
35
72
  return { ...session, handle };
36
73
  }
74
+ /** Validate a record against its lexicon and write it to the PDS via `putRecord`. */
37
75
  async function createRecord(session, collection, record, opts) {
38
76
  const error = validateRecord(lexiconArray, collection, record);
39
77
  if (error) {
@@ -53,10 +91,11 @@ export function seed(opts) {
53
91
  if (!res.ok) {
54
92
  throw new Error(`[seed] [${session.handle}] failed to create ${collection}: ${await res.text()}`);
55
93
  }
56
- const { uri, cid } = (await res.json());
57
- console.log(`[seed] [${session.handle}] ${collection} → ${uri}`);
58
- return { uri, cid };
94
+ const result = (await res.json());
95
+ console.log(`[seed] [${session.handle}] ${collection} → ${result.uri}`);
96
+ return result;
59
97
  }
98
+ /** Upload a file to the PDS as a blob. MIME type is inferred from the file extension. */
60
99
  async function uploadBlob(session, filePath) {
61
100
  const data = readFileSync(resolve(filePath));
62
101
  const ext = filePath.split('.').pop()?.toLowerCase() || '';
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,MAAM,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAA;AAmD3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AA2B9C,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EAAE,EACrB,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,KAAK,EAAE,WAAW,GAAG,IAAI,EACzB,MAAM,GAAE,MAAM,EAAO,EACrB,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,eAAe,KAAK;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,EAChE,QAAQ,CAAC,EAAE,MAAM,IAAI,GACpB,MAAM,CA28BR"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,MAAM,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAA;AAmD3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AA2B9C,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EAAE,EACrB,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,KAAK,EAAE,WAAW,GAAG,IAAI,EACzB,MAAM,GAAE,MAAM,EAAO,EACrB,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,eAAe,KAAK;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,EAChE,QAAQ,CAAC,EAAE,MAAM,IAAI,GACpB,MAAM,CAg9BR"}
package/dist/server.js CHANGED
@@ -597,8 +597,14 @@ export function startServer(port, collections, publicDir, oauth, admins = [], re
597
597
  jsonError(res, 400, 'DPoP header required');
598
598
  return;
599
599
  }
600
- const result = await handlePar(oauth, body, dpopHeader, `${requestOrigin}/oauth/par`);
601
- jsonResponse(res, result);
600
+ try {
601
+ const result = await handlePar(oauth, body, dpopHeader, `${requestOrigin}/oauth/par`);
602
+ jsonResponse(res, result);
603
+ }
604
+ catch (err) {
605
+ const message = err instanceof Error ? err.message : 'Unknown error';
606
+ jsonError(res, 400, message);
607
+ }
602
608
  return;
603
609
  }
604
610
  // OAuth Authorize
package/dist/setup.d.ts CHANGED
@@ -1,8 +1,19 @@
1
+ /** Context passed to each setup script's handler function. */
1
2
  export interface SetupContext {
2
3
  db: {
3
4
  query: (sql: string, params?: any[]) => Promise<any[]>;
4
5
  run: (sql: string, ...params: any[]) => Promise<void>;
5
6
  };
6
7
  }
8
+ /**
9
+ * Run all setup scripts in the given directory on server boot.
10
+ *
11
+ * Each script should export a default handler function (or `{ handler }`) that
12
+ * receives a {@link SetupContext} with database access. Scripts run in sorted
13
+ * filename order — prefix with numbers (e.g. `01-create-tables.ts`) to control
14
+ * execution order. Files starting with `_` are ignored.
15
+ *
16
+ * @param setupDir - Absolute path to the `setup/` directory
17
+ */
7
18
  export declare function initSetup(setupDir: string): Promise<void>;
8
19
  //# sourceMappingURL=setup.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE;QACF,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;QACtD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtD,CAAA;CACF;AAiBD,wBAAsB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqB/D"}
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AA6BA,8DAA8D;AAC9D,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE;QACF,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;QACtD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtD,CAAA;CACF;AAkBD;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqB/D"}
package/dist/setup.js CHANGED
@@ -6,10 +6,35 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
6
6
  }
7
7
  return path;
8
8
  };
9
+ /**
10
+ * Setup scripts that run once on server boot for initializing custom tables,
11
+ * views, or other database state.
12
+ *
13
+ * Place scripts in the `setup/` directory. Each module default-exports a handler
14
+ * function (or `{ handler }`) that receives a {@link SetupContext} with database
15
+ * access. Scripts run in sorted filename order — prefix with numbers to control
16
+ * execution order. Files starting with `_` are ignored.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * // setup/01-leaderboard.ts
21
+ * import type { SetupContext } from '@hatk/hatk/setup'
22
+ *
23
+ * export default async function (ctx: SetupContext) {
24
+ * await ctx.db.run(`
25
+ * CREATE TABLE IF NOT EXISTS leaderboard (
26
+ * did TEXT PRIMARY KEY,
27
+ * score INTEGER DEFAULT 0
28
+ * )
29
+ * `)
30
+ * }
31
+ * ```
32
+ */
9
33
  import { resolve, relative } from 'node:path';
10
34
  import { readdirSync, statSync } from 'node:fs';
11
35
  import { log } from "./logger.js";
12
36
  import { querySQL, runSQL } from "./db.js";
37
+ /** Recursively collect .ts/.js files in a directory, skipping files prefixed with `_`. */
13
38
  function walkDir(dir) {
14
39
  const results = [];
15
40
  try {
@@ -26,6 +51,16 @@ function walkDir(dir) {
26
51
  catch { }
27
52
  return results.sort();
28
53
  }
54
+ /**
55
+ * Run all setup scripts in the given directory on server boot.
56
+ *
57
+ * Each script should export a default handler function (or `{ handler }`) that
58
+ * receives a {@link SetupContext} with database access. Scripts run in sorted
59
+ * filename order — prefix with numbers (e.g. `01-create-tables.ts`) to control
60
+ * execution order. Files starting with `_` are ignored.
61
+ *
62
+ * @param setupDir - Absolute path to the `setup/` directory
63
+ */
29
64
  export async function initSetup(setupDir) {
30
65
  const files = walkDir(setupDir);
31
66
  if (files.length === 0)
package/dist/test.js CHANGED
@@ -9,7 +9,7 @@ import { initXrpc, executeXrpc, listXrpc, configureRelay } from "./xrpc.js";
9
9
  import { initOpengraph } from "./opengraph.js";
10
10
  import { initLabels } from "./labels.js";
11
11
  import { discoverViews } from "./views.js";
12
- import { loadOnLoginHook } from "./oauth/hooks.js";
12
+ import { loadOnLoginHook } from "./hooks.js";
13
13
  import { validateLexicons } from '@bigmoves/lexicon';
14
14
  import { packCursor, unpackCursor, isTakendownDid, filterTakendownDids } from "./db.js";
15
15
  import { seed as createSeedHelpers } from "./seed.js";
package/dist/xrpc.d.ts CHANGED
@@ -1,14 +1,25 @@
1
1
  import type { Row, FlatRow } from './lex-types.ts';
2
2
  export type { Row, FlatRow };
3
+ /** Thrown from XRPC handlers to return a 400 response with an error message. */
3
4
  export declare class InvalidRequestError extends Error {
4
5
  status: number;
5
6
  errorName?: string;
6
7
  constructor(message: string, errorName?: string);
7
8
  }
9
+ /** Thrown from XRPC handlers to return a 404 response. */
8
10
  export declare class NotFoundError extends InvalidRequestError {
9
11
  status: number;
10
12
  constructor(message?: string);
11
13
  }
14
+ /**
15
+ * Context passed to every XRPC handler. Provides database access, pagination
16
+ * helpers, viewer auth, record resolution, full-text search, label queries,
17
+ * and blob URL generation.
18
+ *
19
+ * @typeParam P - Query parameter types (derived from lexicon)
20
+ * @typeParam Records - Map of collection NSID → record type (from generated types)
21
+ * @typeParam I - Input body type for procedure calls
22
+ */
12
23
  export interface XrpcContext<P = Record<string, string>, Records extends Record<string, any> = Record<string, any>, I = unknown> {
13
24
  db: {
14
25
  query: (sql: string, params?: any[]) => Promise<any[]>;
@@ -43,11 +54,23 @@ export interface XrpcContext<P = Record<string, string>, Records extends Record<
43
54
  labels: (uris: string[]) => Promise<Map<string, any[]>>;
44
55
  blobUrl: (did: string, ref: unknown, preset?: 'avatar' | 'banner' | 'feed_thumbnail' | 'feed_fullsize') => string | undefined;
45
56
  }
57
+ /** Set the relay URL used for blob URL generation. Called once during boot. */
46
58
  export declare function configureRelay(relay: string): void;
59
+ /**
60
+ * Generate a CDN URL for a blob ref. Uses the PDS directly in local dev,
61
+ * or the Bluesky CDN (`cdn.bsky.app`) in production.
62
+ */
47
63
  export declare function blobUrl(did: string, ref: unknown, preset?: 'avatar' | 'banner' | 'feed_thumbnail' | 'feed_fullsize'): string | undefined;
64
+ /**
65
+ * Discover and load XRPC handler modules from the `xrpc/` directory.
66
+ * Directory nesting maps to NSID segments. Parameters are validated and
67
+ * coerced against the matching lexicon definition.
68
+ */
48
69
  export declare function initXrpc(xrpcDir: string): Promise<void>;
70
+ /** Execute a registered XRPC handler by name. Returns null if no handler matches. */
49
71
  export declare function executeXrpc(name: string, params: Record<string, string>, cursor: string | undefined, limit: number, viewer?: {
50
72
  did: string;
51
73
  } | null, input?: unknown): Promise<any | null>;
74
+ /** Return all registered XRPC method names. */
52
75
  export declare function listXrpc(): string[];
53
76
  //# sourceMappingURL=xrpc.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../src/xrpc.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAElD,YAAY,EAAE,GAAG,EAAE,OAAO,EAAE,CAAA;AAE5B,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,MAAM,SAAM;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;gBACN,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CAIhD;AACD,qBAAa,aAAc,SAAQ,mBAAmB;IACpD,MAAM,SAAM;gBACA,OAAO,SAAc;CAGlC;AAED,MAAM,WAAW,WAAW,CAC1B,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzD,CAAC,GAAG,OAAO;IAEX,EAAE,EAAE;QACF,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;QACtD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtD,CAAA;IACD,MAAM,EAAE,CAAC,CAAA;IACT,KAAK,EAAE,CAAC,CAAA;IACR,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,IAA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AC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IACjF,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;IACvD,OAAO,EAAE,CACP,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,OAAO,EACZ,MAAM,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,gBAAgB,GAAG,eAAe,KAC9D,MAAM,GAAG,SAAS,CAAA;CACxB;AAgBD,+EAA+E;AAC/E,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,QAE3C;AAED;;;GAGG;AACH,wBAAgB,OAAO,CACrB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,OAAO,EACZ,MAAM,GAAE,QAAQ,GAAG,QAAQ,GAAG,gBAAgB,GAAG,eAA0B,GAC1E,MAAM,GAAG,SAAS,CAQpB;AAoBD;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAsE7D;AAED,qFAAqF;AACrF,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,EAC/B,KAAK,CAAC,EAAE,OAAO,GACd,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAIrB;AAED,+CAA+C;AAC/C,wBAAgB,QAAQ,IAAI,MAAM,EAAE,CAEnC"}
package/dist/xrpc.js CHANGED
@@ -6,12 +6,33 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
6
6
  }
7
7
  return path;
8
8
  };
9
+ /**
10
+ * XRPC method handler system for serving AT Protocol endpoints.
11
+ *
12
+ * Place handler modules in the `xrpc/` directory, nested by NSID segments
13
+ * (e.g. `xrpc/app/bsky/feed/getAuthorFeed.ts` → `app.bsky.feed.getAuthorFeed`).
14
+ * Each module default-exports a `{ handler }` function that receives an
15
+ * {@link XrpcContext} with database access, query params, pagination, and
16
+ * viewer auth.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * // xrpc/xyz/statusphere/getStatuses.ts
21
+ * import { defineXrpc } from '../../hatk.generated.ts'
22
+ *
23
+ * export default defineXrpc('xyz.statusphere.getStatuses', async (ctx) => {
24
+ * const rows = await ctx.db.query('SELECT * FROM statusphere_status LIMIT ?', [ctx.limit])
25
+ * return { statuses: rows }
26
+ * })
27
+ * ```
28
+ */
9
29
  import { resolve, relative } from 'node:path';
10
30
  import { readdirSync, statSync } from 'node:fs';
11
31
  import { log } from "./logger.js";
12
32
  import { querySQL, runSQL, packCursor, unpackCursor, isTakendownDid, filterTakendownDids, searchRecords, findUriByFields, lookupByFieldBatch, countByFieldBatch, queryLabelsForUris, } from "./db.js";
13
33
  import { resolveRecords } from "./hydrate.js";
14
34
  import { getLexicon } from "./schema.js";
35
+ /** Thrown from XRPC handlers to return a 400 response with an error message. */
15
36
  export class InvalidRequestError extends Error {
16
37
  status = 400;
17
38
  errorName;
@@ -20,6 +41,7 @@ export class InvalidRequestError extends Error {
20
41
  this.errorName = errorName;
21
42
  }
22
43
  }
44
+ /** Thrown from XRPC handlers to return a 404 response. */
23
45
  export class NotFoundError extends InvalidRequestError {
24
46
  status = 404;
25
47
  constructor(message = 'Not found') {
@@ -27,9 +49,14 @@ export class NotFoundError extends InvalidRequestError {
27
49
  }
28
50
  }
29
51
  let _relayUrl = '';
52
+ /** Set the relay URL used for blob URL generation. Called once during boot. */
30
53
  export function configureRelay(relay) {
31
54
  _relayUrl = relay;
32
55
  }
56
+ /**
57
+ * Generate a CDN URL for a blob ref. Uses the PDS directly in local dev,
58
+ * or the Bluesky CDN (`cdn.bsky.app`) in production.
59
+ */
33
60
  export function blobUrl(did, ref, preset = 'avatar') {
34
61
  if (!ref)
35
62
  return undefined;
@@ -42,6 +69,7 @@ export function blobUrl(did, ref, preset = 'avatar') {
42
69
  return `https://cdn.bsky.app/img/${preset}/plain/${did}/${p.ref.$link}@jpeg`;
43
70
  }
44
71
  const handlers = new Map();
72
+ /** Recursively collect .ts/.js files in a directory, skipping files prefixed with `_`. */
45
73
  function walkDir(dir) {
46
74
  const results = [];
47
75
  try {
@@ -58,6 +86,11 @@ function walkDir(dir) {
58
86
  catch { }
59
87
  return results.sort();
60
88
  }
89
+ /**
90
+ * Discover and load XRPC handler modules from the `xrpc/` directory.
91
+ * Directory nesting maps to NSID segments. Parameters are validated and
92
+ * coerced against the matching lexicon definition.
93
+ */
61
94
  export async function initXrpc(xrpcDir) {
62
95
  const files = walkDir(xrpcDir);
63
96
  if (files.length === 0)
@@ -128,12 +161,14 @@ export async function initXrpc(xrpcDir) {
128
161
  log(`[xrpc] discovered: ${name}`);
129
162
  }
130
163
  }
164
+ /** Execute a registered XRPC handler by name. Returns null if no handler matches. */
131
165
  export async function executeXrpc(name, params, cursor, limit, viewer, input) {
132
166
  const handler = handlers.get(name);
133
167
  if (!handler)
134
168
  return null;
135
169
  return handler.execute(params, cursor, limit, viewer || null, input);
136
170
  }
171
+ /** Return all registered XRPC method names. */
137
172
  export function listXrpc() {
138
173
  return Array.from(handlers.keys());
139
174
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatk/hatk",
3
- "version": "0.0.1-alpha.20",
3
+ "version": "0.0.1-alpha.22",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "hatk": "dist/cli.js"