@hatk/hatk 0.0.1-alpha.21 → 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/backfill.d.ts.map +1 -1
- package/dist/car.js +1 -1
- package/dist/cli.js +102 -33
- package/dist/hooks.d.ts +15 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +65 -0
- package/dist/indexer.d.ts +19 -0
- package/dist/indexer.d.ts.map +1 -1
- package/dist/indexer.js +31 -1
- package/dist/labels.d.ts +20 -0
- package/dist/labels.d.ts.map +1 -1
- package/dist/labels.js +48 -0
- package/dist/logger.d.ts +29 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +29 -0
- package/dist/main.js +9 -24
- package/dist/mst.d.ts +15 -0
- package/dist/mst.d.ts.map +1 -1
- package/dist/mst.js +13 -0
- package/dist/oauth/server.d.ts.map +1 -1
- package/dist/oauth/server.js +7 -2
- package/dist/schema.d.ts +8 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +29 -0
- package/dist/seed.d.ts +19 -0
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +42 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +8 -2
- package/dist/setup.d.ts +11 -0
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +35 -0
- package/dist/test.js +1 -1
- package/dist/xrpc.d.ts +23 -0
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +35 -0
- package/package.json +1 -1
package/dist/backfill.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backfill.d.ts","sourceRoot":"","sources":["../src/backfill.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,6CAA6C;AAC7C,UAAU,YAAY;IACpB,wFAAwF;IACxF,MAAM,EAAE,MAAM,CAAA;IACd,8FAA8F;IAC9F,MAAM,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,wDAAwD;IACxD,MAAM,EAAE,cAAc,CAAA;CACvB;
|
|
1
|
+
{"version":3,"file":"backfill.d.ts","sourceRoot":"","sources":["../src/backfill.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,6CAA6C;AAC7C,UAAU,YAAY;IACpB,wFAAwF;IACxF,MAAM,EAAE,MAAM,CAAA;IACd,8FAA8F;IAC9F,MAAM,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,wDAAwD;IACxD,MAAM,EAAE,cAAc,CAAA;CACvB;AAoGD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsJ/G;AA8BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAkIrE"}
|
package/dist/car.js
CHANGED
|
@@ -107,7 +107,7 @@ export async function parseCarStream(body) {
|
|
|
107
107
|
while (len - pos < need) {
|
|
108
108
|
const { done, value } = await reader.read();
|
|
109
109
|
if (done)
|
|
110
|
-
return
|
|
110
|
+
return len - pos >= need;
|
|
111
111
|
byteLength += value.length;
|
|
112
112
|
// Compact: shift remaining data to front when read cursor passes midpoint
|
|
113
113
|
if (pos > 0 && pos > buf.length >>> 1) {
|
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
|
-
import { resolve, join } from 'node:path';
|
|
4
|
-
import { execSync } from 'node:child_process';
|
|
5
|
-
import { loadLexicons } from "./schema.js";
|
|
3
|
+
import { resolve, join, dirname } from 'node:path';
|
|
4
|
+
import { execSync, spawn } from 'node:child_process';
|
|
5
|
+
import { loadLexicons, discoverCollections, buildSchemas } from "./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))
|
|
@@ -511,6 +536,14 @@ export default defineConfig({
|
|
|
511
536
|
properties: {
|
|
512
537
|
uri: { type: 'string', format: 'at-uri' },
|
|
513
538
|
cid: { type: 'string', format: 'cid' },
|
|
539
|
+
commit: {
|
|
540
|
+
type: 'object',
|
|
541
|
+
properties: {
|
|
542
|
+
cid: { type: 'string', format: 'cid' },
|
|
543
|
+
rev: { type: 'string' },
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
validationStatus: { type: 'string' },
|
|
514
547
|
},
|
|
515
548
|
},
|
|
516
549
|
},
|
|
@@ -566,6 +599,14 @@ export default defineConfig({
|
|
|
566
599
|
properties: {
|
|
567
600
|
uri: { type: 'string', format: 'at-uri' },
|
|
568
601
|
cid: { type: 'string', format: 'cid' },
|
|
602
|
+
commit: {
|
|
603
|
+
type: 'object',
|
|
604
|
+
properties: {
|
|
605
|
+
cid: { type: 'string', format: 'cid' },
|
|
606
|
+
rev: { type: 'string' },
|
|
607
|
+
},
|
|
608
|
+
},
|
|
609
|
+
validationStatus: { type: 'string' },
|
|
569
610
|
},
|
|
570
611
|
},
|
|
571
612
|
},
|
|
@@ -1082,6 +1123,43 @@ a {
|
|
|
1082
1123
|
</div>
|
|
1083
1124
|
`);
|
|
1084
1125
|
}
|
|
1126
|
+
writeFileSync(join(dir, 'AGENTS.md'), `# hatk project
|
|
1127
|
+
|
|
1128
|
+
This is an AT Protocol application built with [hatk](https://github.com/hatk-dev/hatk).
|
|
1129
|
+
Read the project's lexicons in \`lexicons/\` to understand the data model.
|
|
1130
|
+
Types are generated from lexicons into \`hatk.generated.ts\` — never edit this file directly.
|
|
1131
|
+
|
|
1132
|
+
## Project structure
|
|
1133
|
+
|
|
1134
|
+
| Directory | Purpose |
|
|
1135
|
+
|-------------|------------------------------------------------------|
|
|
1136
|
+
| \`lexicons/\` | AT Protocol lexicon schemas (JSON). Defines collections and XRPC methods |
|
|
1137
|
+
| \`feeds/\` | Feed generators — each file exports a feed via \`defineFeed\` |
|
|
1138
|
+
| \`xrpc/\` | XRPC method handlers — directory nesting maps to NSID segments |
|
|
1139
|
+
| \`labels/\` | Label definitions and rules for moderation |
|
|
1140
|
+
| \`setup/\` | Boot-time scripts (run before server starts). Prefix with numbers for ordering |
|
|
1141
|
+
| \`seeds/\` | Test data seeding scripts for local development |
|
|
1142
|
+
| \`hooks/\` | Lifecycle hooks (e.g. \`on-login.ts\`) |
|
|
1143
|
+
| \`og/\` | OpenGraph image routes |
|
|
1144
|
+
| \`jobs/\` | Periodic background tasks |
|
|
1145
|
+
| \`test/\` | Test files (vitest). Run with \`hatk test\` |
|
|
1146
|
+
| \`public/\` | Static files served at the root |
|
|
1147
|
+
|
|
1148
|
+
## Key files
|
|
1149
|
+
|
|
1150
|
+
- \`hatk.config.ts\` — project configuration (see \`defineConfig\` for type info)
|
|
1151
|
+
- \`hatk.generated.ts\` — auto-generated types and typed helpers. Regenerate with \`hatk generate types\`
|
|
1152
|
+
|
|
1153
|
+
## Commands
|
|
1154
|
+
|
|
1155
|
+
Run \`npx hatk --help\` for the full list of commands.
|
|
1156
|
+
|
|
1157
|
+
Use \`npx hatk generate\` to scaffold new feeds, xrpc handlers, labels, and lexicons
|
|
1158
|
+
rather than creating files manually. These generate files with the correct imports
|
|
1159
|
+
from \`hatk.generated.ts\`.
|
|
1160
|
+
|
|
1161
|
+
After modifying lexicons, always run \`npx hatk generate types\` to update the generated types.
|
|
1162
|
+
`);
|
|
1085
1163
|
console.log(`Created ${name}/`);
|
|
1086
1164
|
console.log(` hatk.config.ts`);
|
|
1087
1165
|
console.log(` lexicons/ — lexicon JSON files (core + your own)`);
|
|
@@ -1486,25 +1564,14 @@ else if (command === 'destroy') {
|
|
|
1486
1564
|
else if (command === 'dev') {
|
|
1487
1565
|
await ensurePds();
|
|
1488
1566
|
runSeed();
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
execSync('npx vite dev', { stdio: 'inherit', cwd: process.cwd() });
|
|
1493
|
-
}
|
|
1494
|
-
else {
|
|
1495
|
-
// No frontend — just run the hatk server directly
|
|
1496
|
-
const mainPath = resolve(import.meta.dirname, 'main.js');
|
|
1497
|
-
execSync(`npx tsx ${mainPath} hatk.config.ts`, {
|
|
1498
|
-
stdio: 'inherit',
|
|
1499
|
-
cwd: process.cwd(),
|
|
1500
|
-
env: { ...process.env, DEV_MODE: '1' },
|
|
1501
|
-
});
|
|
1502
|
-
}
|
|
1567
|
+
if (existsSync(resolve('svelte.config.js')) && existsSync(resolve('src/app.html'))) {
|
|
1568
|
+
// SvelteKit project — vite dev starts the hatk server via the plugin
|
|
1569
|
+
await spawnForward('npx', ['vite', 'dev']);
|
|
1503
1570
|
}
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1571
|
+
else {
|
|
1572
|
+
// No frontend — just run the hatk server directly
|
|
1573
|
+
const mainPath = resolve(import.meta.dirname, 'main.js');
|
|
1574
|
+
await spawnForward('npx', ['tsx', mainPath, 'hatk.config.ts'], { DEV_MODE: '1' });
|
|
1508
1575
|
}
|
|
1509
1576
|
}
|
|
1510
1577
|
else if (command === 'format' || command === 'fmt') {
|
|
@@ -1689,10 +1756,19 @@ else if (command === 'schema') {
|
|
|
1689
1756
|
console.error('No database file configured (database is :memory:)');
|
|
1690
1757
|
process.exit(1);
|
|
1691
1758
|
}
|
|
1759
|
+
// Init DB from lexicons if it doesn't exist yet
|
|
1692
1760
|
if (!existsSync(config.database)) {
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1761
|
+
const configDir = resolve('.');
|
|
1762
|
+
const lexicons = loadLexicons(resolve(configDir, 'lexicons'));
|
|
1763
|
+
const collections = config.collections.length > 0 ? config.collections : discoverCollections(lexicons);
|
|
1764
|
+
if (collections.length === 0) {
|
|
1765
|
+
console.error('No record collections found. Add record lexicons to the lexicons/ directory.');
|
|
1766
|
+
process.exit(1);
|
|
1767
|
+
}
|
|
1768
|
+
mkdirSync(dirname(config.database), { recursive: true });
|
|
1769
|
+
const { initDatabase } = await import("./db.js");
|
|
1770
|
+
const { schemas, ddlStatements } = buildSchemas(lexicons, collections);
|
|
1771
|
+
await initDatabase(config.database, schemas, ddlStatements);
|
|
1696
1772
|
}
|
|
1697
1773
|
const { DuckDBInstance } = await import('@duckdb/node-api');
|
|
1698
1774
|
const instance = await DuckDBInstance.create(config.database);
|
|
@@ -1709,15 +1785,8 @@ else if (command === 'schema') {
|
|
|
1709
1785
|
}
|
|
1710
1786
|
}
|
|
1711
1787
|
else if (command === 'start') {
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
execSync(`npx tsx ${mainPath} hatk.config.ts`, { stdio: 'inherit', cwd: process.cwd() });
|
|
1715
|
-
}
|
|
1716
|
-
catch (e) {
|
|
1717
|
-
if (e.signal === 'SIGINT' || e.signal === 'SIGTERM')
|
|
1718
|
-
process.exit(0);
|
|
1719
|
-
throw e;
|
|
1720
|
-
}
|
|
1788
|
+
const mainPath = resolve(import.meta.dirname, 'main.js');
|
|
1789
|
+
await spawnForward('npx', ['tsx', mainPath, 'hatk.config.ts']);
|
|
1721
1790
|
}
|
|
1722
1791
|
else {
|
|
1723
1792
|
usage();
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Context passed to the on-login hook after a successful OAuth login. */
|
|
2
|
+
export type OnLoginCtx = {
|
|
3
|
+
/** DID of the user who just logged in. */
|
|
4
|
+
did: string;
|
|
5
|
+
/** Trigger a backfill for a DID if it hasn't been indexed yet. */
|
|
6
|
+
ensureRepo: (did: string) => Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Discover and load the on-login hook from the project's `hooks/` directory.
|
|
10
|
+
* Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
|
|
11
|
+
*/
|
|
12
|
+
export declare function loadOnLoginHook(hooksDir: string): Promise<void>;
|
|
13
|
+
/** Fire the on-login hook if loaded. Errors are logged but never block login. */
|
|
14
|
+
export declare function fireOnLoginHook(did: string): Promise<void>;
|
|
15
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AA2BA,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG;IACvB,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,kEAAkE;IAClE,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3C,CAAA;AAMD;;;GAGG;AACH,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQrE;AAQD,iFAAiF;AACjF,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAOhE"}
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Lifecycle hooks that run in response to server events.
|
|
11
|
+
*
|
|
12
|
+
* Place hook modules in the `hooks/` directory. Currently supported hooks:
|
|
13
|
+
*
|
|
14
|
+
* - `on-login.ts` — called after each successful OAuth login
|
|
15
|
+
*
|
|
16
|
+
* Each hook default-exports an async function that receives an event-specific
|
|
17
|
+
* context object.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* // hooks/on-login.ts
|
|
22
|
+
* import type { OnLoginCtx } from '@hatk/hatk/hooks'
|
|
23
|
+
*
|
|
24
|
+
* export default async function (ctx: OnLoginCtx) {
|
|
25
|
+
* // Ensure the user's repo is backfilled on first login
|
|
26
|
+
* await ctx.ensureRepo(ctx.did)
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
import { existsSync } from 'node:fs';
|
|
31
|
+
import { resolve } from 'node:path';
|
|
32
|
+
import { log } from "./logger.js";
|
|
33
|
+
import { setRepoStatus } from "./db.js";
|
|
34
|
+
import { triggerAutoBackfill } from "./indexer.js";
|
|
35
|
+
let onLoginHook = null;
|
|
36
|
+
/**
|
|
37
|
+
* Discover and load the on-login hook from the project's `hooks/` directory.
|
|
38
|
+
* Looks for `on-login.ts` or `on-login.js`. Safe to call if no hook exists.
|
|
39
|
+
*/
|
|
40
|
+
export async function loadOnLoginHook(hooksDir) {
|
|
41
|
+
const tsPath = resolve(hooksDir, 'on-login.ts');
|
|
42
|
+
const jsPath = resolve(hooksDir, 'on-login.js');
|
|
43
|
+
const path = existsSync(tsPath) ? tsPath : existsSync(jsPath) ? jsPath : null;
|
|
44
|
+
if (!path)
|
|
45
|
+
return;
|
|
46
|
+
const mod = await import(__rewriteRelativeImportExtension(path));
|
|
47
|
+
onLoginHook = mod.default;
|
|
48
|
+
log('[hooks] on-login hook loaded');
|
|
49
|
+
}
|
|
50
|
+
/** Mark a DID as pending and trigger auto-backfill. */
|
|
51
|
+
async function ensureRepo(did) {
|
|
52
|
+
await setRepoStatus(did, 'pending');
|
|
53
|
+
triggerAutoBackfill(did);
|
|
54
|
+
}
|
|
55
|
+
/** Fire the on-login hook if loaded. Errors are logged but never block login. */
|
|
56
|
+
export async function fireOnLoginHook(did) {
|
|
57
|
+
if (!onLoginHook)
|
|
58
|
+
return;
|
|
59
|
+
try {
|
|
60
|
+
await onLoginHook({ did, ensureRepo });
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
console.error('[hooks] onLogin hook error:', err.message);
|
|
64
|
+
}
|
|
65
|
+
}
|
package/dist/indexer.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-backfill a DID's repo when first seen on the firehose.
|
|
3
|
+
*
|
|
4
|
+
* Fetches the full repo via CAR export, inserts all records, then replays any
|
|
5
|
+
* firehose events that arrived during the backfill. Concurrency is capped at
|
|
6
|
+
* `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
|
|
7
|
+
* to `maxRetries`.
|
|
8
|
+
*/
|
|
1
9
|
export declare function triggerAutoBackfill(did: string, attempt?: number): Promise<void>;
|
|
10
|
+
/** Configuration for the firehose indexer. */
|
|
2
11
|
interface IndexerOpts {
|
|
3
12
|
relayUrl: string;
|
|
4
13
|
collections: Set<string>;
|
|
@@ -10,6 +19,16 @@ interface IndexerOpts {
|
|
|
10
19
|
parallelism?: number;
|
|
11
20
|
ftsRebuildInterval?: number;
|
|
12
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Connect to the AT Protocol relay firehose and begin indexing.
|
|
24
|
+
*
|
|
25
|
+
* Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
|
|
26
|
+
* on the event loop to minimize backpressure, and batches writes through
|
|
27
|
+
* {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
|
|
28
|
+
* Reconnects automatically on disconnect after a 3s delay.
|
|
29
|
+
*
|
|
30
|
+
* @returns The WebSocket connection (for shutdown coordination)
|
|
31
|
+
*/
|
|
13
32
|
export declare function startIndexer(opts: IndexerOpts): Promise<WebSocket>;
|
|
14
33
|
export {};
|
|
15
34
|
//# sourceMappingURL=indexer.d.ts.map
|
package/dist/indexer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"indexer.d.ts","sourceRoot":"","sources":["../src/indexer.ts"],"names":[],"mappings":"AA2IA;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,SAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAgEjF;AAED,8CAA8C;AAC9C,UAAU,WAAW;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACxB,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,WAAW,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACzB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAyBD;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAmDxE"}
|
package/dist/indexer.js
CHANGED
|
@@ -28,6 +28,11 @@ let indexerPinnedRepos = null;
|
|
|
28
28
|
let indexerFetchTimeout;
|
|
29
29
|
let indexerMaxRetries;
|
|
30
30
|
let maxConcurrentBackfills = 3;
|
|
31
|
+
/**
|
|
32
|
+
* Flush the write buffer — insert all buffered records, update the relay cursor,
|
|
33
|
+
* run label rules on inserted records, and trigger FTS rebuilds when the write
|
|
34
|
+
* threshold is reached. Emits a wide event with batch stats.
|
|
35
|
+
*/
|
|
31
36
|
async function flushBuffer() {
|
|
32
37
|
if (buffer.length === 0)
|
|
33
38
|
return;
|
|
@@ -90,6 +95,7 @@ async function flushBuffer() {
|
|
|
90
95
|
rebuildAllIndexes([...indexerCollections]).catch(() => { });
|
|
91
96
|
}
|
|
92
97
|
}
|
|
98
|
+
/** Schedule a flush after FLUSH_INTERVAL_MS if one isn't already pending. */
|
|
93
99
|
function scheduleFlush() {
|
|
94
100
|
if (flushTimer)
|
|
95
101
|
return;
|
|
@@ -98,6 +104,7 @@ function scheduleFlush() {
|
|
|
98
104
|
await flushBuffer();
|
|
99
105
|
}, FLUSH_INTERVAL_MS);
|
|
100
106
|
}
|
|
107
|
+
/** Add a record to the write buffer. Flushes immediately if BATCH_SIZE is reached. */
|
|
101
108
|
function bufferWrite(item) {
|
|
102
109
|
buffer.push(item);
|
|
103
110
|
if (buffer.length >= BATCH_SIZE) {
|
|
@@ -111,6 +118,14 @@ function bufferWrite(item) {
|
|
|
111
118
|
scheduleFlush();
|
|
112
119
|
}
|
|
113
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Auto-backfill a DID's repo when first seen on the firehose.
|
|
123
|
+
*
|
|
124
|
+
* Fetches the full repo via CAR export, inserts all records, then replays any
|
|
125
|
+
* firehose events that arrived during the backfill. Concurrency is capped at
|
|
126
|
+
* `maxConcurrentBackfills`. Failed backfills retry with exponential delay up
|
|
127
|
+
* to `maxRetries`.
|
|
128
|
+
*/
|
|
114
129
|
export async function triggerAutoBackfill(did, attempt = 0) {
|
|
115
130
|
if (backfillInFlight.has(did))
|
|
116
131
|
return;
|
|
@@ -173,7 +188,7 @@ export async function triggerAutoBackfill(did, attempt = 0) {
|
|
|
173
188
|
}, delayMs);
|
|
174
189
|
}
|
|
175
190
|
}
|
|
176
|
-
|
|
191
|
+
/** Emit a memory diagnostics wide event every 30s for observability. */
|
|
177
192
|
function startMemoryDiagnostics() {
|
|
178
193
|
setInterval(() => {
|
|
179
194
|
const mem = process.memoryUsage();
|
|
@@ -195,6 +210,16 @@ function startMemoryDiagnostics() {
|
|
|
195
210
|
});
|
|
196
211
|
}, 30_000);
|
|
197
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Connect to the AT Protocol relay firehose and begin indexing.
|
|
215
|
+
*
|
|
216
|
+
* Opens a WebSocket to `subscribeRepos`, processes commit messages synchronously
|
|
217
|
+
* on the event loop to minimize backpressure, and batches writes through
|
|
218
|
+
* {@link flushBuffer}. New DIDs trigger auto-backfill via {@link triggerAutoBackfill}.
|
|
219
|
+
* Reconnects automatically on disconnect after a 3s delay.
|
|
220
|
+
*
|
|
221
|
+
* @returns The WebSocket connection (for shutdown coordination)
|
|
222
|
+
*/
|
|
198
223
|
export async function startIndexer(opts) {
|
|
199
224
|
const { relayUrl, collections, cursor, fetchTimeout } = opts;
|
|
200
225
|
if (opts.ftsRebuildInterval != null)
|
|
@@ -243,6 +268,11 @@ export async function startIndexer(opts) {
|
|
|
243
268
|
});
|
|
244
269
|
return ws;
|
|
245
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Process a single firehose message. Decodes the CBOR header/body, filters
|
|
273
|
+
* for relevant collections, validates records against lexicons, and routes
|
|
274
|
+
* writes to the buffer (or pending buffer if the DID is mid-backfill).
|
|
275
|
+
*/
|
|
246
276
|
function processMessage(bytes, collections) {
|
|
247
277
|
const header = cborDecode(bytes, 0);
|
|
248
278
|
const body = cborDecode(bytes, header.offset);
|
package/dist/labels.d.ts
CHANGED
|
@@ -13,7 +13,20 @@ export interface LabelRuleContext {
|
|
|
13
13
|
value: Record<string, any>;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Discover and load label rule modules from the `labels/` directory.
|
|
18
|
+
*
|
|
19
|
+
* Each module should default-export an object with an optional `definition`
|
|
20
|
+
* (label metadata like severity and blur behavior) and an optional `evaluate`
|
|
21
|
+
* function that returns label values to apply to a record.
|
|
22
|
+
*
|
|
23
|
+
* @param labelsDir - Absolute path to the `labels/` directory
|
|
24
|
+
*/
|
|
16
25
|
export declare function initLabels(labelsDir: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Evaluate all loaded label rules against a record and persist any resulting labels.
|
|
28
|
+
* Called after each record is indexed. Rule errors are logged but never block indexing.
|
|
29
|
+
*/
|
|
17
30
|
export declare function runLabelRules(record: {
|
|
18
31
|
uri: string;
|
|
19
32
|
cid: string;
|
|
@@ -21,9 +34,16 @@ export declare function runLabelRules(record: {
|
|
|
21
34
|
collection: string;
|
|
22
35
|
value: Record<string, any>;
|
|
23
36
|
}): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Re-evaluate all label rules against every existing record in the given collections.
|
|
39
|
+
* Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
|
|
40
|
+
*
|
|
41
|
+
* @returns Count of records scanned and new labels applied
|
|
42
|
+
*/
|
|
24
43
|
export declare function rescanLabels(collections: string[]): Promise<{
|
|
25
44
|
scanned: number;
|
|
26
45
|
labeled: number;
|
|
27
46
|
}>;
|
|
47
|
+
/** Return all label definitions discovered during {@link initLabels}. */
|
|
28
48
|
export declare function getLabelDefinitions(): LabelDefinition[];
|
|
29
49
|
//# sourceMappingURL=labels.d.ts.map
|
package/dist/labels.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../src/labels.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../src/labels.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIlD,wDAAwD;AACxD,MAAM,WAAW,gBAAgB;IAC/B,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;QACN,GAAG,EAAE,MAAM,CAAA;QACX,GAAG,EAAE,MAAM,CAAA;QACX,GAAG,EAAE,MAAM,CAAA;QACX,UAAU,EAAE,MAAM,CAAA;QAClB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAC3B,CAAA;CACF;AAYD;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAmCjE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC3B,GAAG,OAAO,CAAC,IAAI,CAAC,CAyBhB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAuCvG;AAED,yEAAyE;AACzE,wBAAgB,mBAAmB,IAAI,eAAe,EAAE,CAEvD"}
|
package/dist/labels.js
CHANGED
|
@@ -6,6 +6,34 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* Label system for applying moderation labels to records as they are indexed.
|
|
11
|
+
*
|
|
12
|
+
* Place label modules in the `labels/` directory. Each module default-exports
|
|
13
|
+
* an object with a `definition` (label metadata) and/or an `evaluate` function
|
|
14
|
+
* (rule that returns label values for a given record).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // labels/nsfw.ts
|
|
19
|
+
* import type { LabelRuleContext } from '@hatk/hatk/labels'
|
|
20
|
+
*
|
|
21
|
+
* export default {
|
|
22
|
+
* definition: {
|
|
23
|
+
* identifier: 'nsfw',
|
|
24
|
+
* severity: 'alert',
|
|
25
|
+
* blurs: 'media',
|
|
26
|
+
* defaultSetting: 'warn',
|
|
27
|
+
* locales: [{ lang: 'en', name: 'NSFW', description: 'Not safe for work' }],
|
|
28
|
+
* },
|
|
29
|
+
*
|
|
30
|
+
* async evaluate(ctx: LabelRuleContext): Promise<string[]> {
|
|
31
|
+
* if (ctx.record.value.nsfw === true) return ['nsfw']
|
|
32
|
+
* return []
|
|
33
|
+
* },
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
9
37
|
import { resolve } from 'node:path';
|
|
10
38
|
import { readdirSync } from 'node:fs';
|
|
11
39
|
import { querySQL, runSQL, insertLabels, getSchema } from "./db.js";
|
|
@@ -13,6 +41,15 @@ import { log, emit } from "./logger.js";
|
|
|
13
41
|
const rules = [];
|
|
14
42
|
let labelDefs = [];
|
|
15
43
|
let labelSrc = 'self';
|
|
44
|
+
/**
|
|
45
|
+
* Discover and load label rule modules from the `labels/` directory.
|
|
46
|
+
*
|
|
47
|
+
* Each module should default-export an object with an optional `definition`
|
|
48
|
+
* (label metadata like severity and blur behavior) and an optional `evaluate`
|
|
49
|
+
* function that returns label values to apply to a record.
|
|
50
|
+
*
|
|
51
|
+
* @param labelsDir - Absolute path to the `labels/` directory
|
|
52
|
+
*/
|
|
16
53
|
export async function initLabels(labelsDir) {
|
|
17
54
|
let files;
|
|
18
55
|
try {
|
|
@@ -45,6 +82,10 @@ export async function initLabels(labelsDir) {
|
|
|
45
82
|
log(`[labels] ${labelDefs.length} label definitions loaded`);
|
|
46
83
|
}
|
|
47
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Evaluate all loaded label rules against a record and persist any resulting labels.
|
|
87
|
+
* Called after each record is indexed. Rule errors are logged but never block indexing.
|
|
88
|
+
*/
|
|
48
89
|
export async function runLabelRules(record) {
|
|
49
90
|
if (rules.length === 0)
|
|
50
91
|
return;
|
|
@@ -69,6 +110,12 @@ export async function runLabelRules(record) {
|
|
|
69
110
|
emit('labels', 'applied', { count: allLabels.length, uri: record.uri, vals: allLabels.map((l) => l.val) });
|
|
70
111
|
}
|
|
71
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Re-evaluate all label rules against every existing record in the given collections.
|
|
115
|
+
* Used by `/admin/rescan-labels` to apply new or updated rules retroactively.
|
|
116
|
+
*
|
|
117
|
+
* @returns Count of records scanned and new labels applied
|
|
118
|
+
*/
|
|
72
119
|
export async function rescanLabels(collections) {
|
|
73
120
|
const beforeRows = await querySQL(`SELECT COUNT(*) as count FROM _labels`);
|
|
74
121
|
const beforeCount = Number(beforeRows[0]?.count || 0);
|
|
@@ -106,6 +153,7 @@ export async function rescanLabels(collections) {
|
|
|
106
153
|
const afterCount = Number(afterRows[0]?.count || 0);
|
|
107
154
|
return { scanned, labeled: afterCount - beforeCount };
|
|
108
155
|
}
|
|
156
|
+
/** Return all label definitions discovered during {@link initLabels}. */
|
|
109
157
|
export function getLabelDefinitions() {
|
|
110
158
|
return labelDefs;
|
|
111
159
|
}
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,4 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unstructured debug log — use sparingly for human-readable dev output.
|
|
3
|
+
* Prefer {@link emit} for anything that should be queryable in production.
|
|
4
|
+
* Disabled when `DEBUG=0`.
|
|
5
|
+
*/
|
|
1
6
|
export declare function log(...args: unknown[]): void;
|
|
7
|
+
/**
|
|
8
|
+
* Emit a structured wide event as a single JSON line to stdout.
|
|
9
|
+
*
|
|
10
|
+
* Each call produces one canonical log line with a timestamp, module, operation,
|
|
11
|
+
* and arbitrary key-value fields — designed for columnar search and aggregation,
|
|
12
|
+
* not string grep. Pack as much context as possible into `fields` (request IDs,
|
|
13
|
+
* durations, status codes, user DIDs, counts) so a single event tells the full
|
|
14
|
+
* story. See https://loggingsucks.com for the philosophy behind this approach.
|
|
15
|
+
*
|
|
16
|
+
* Disabled when `DEBUG=0`.
|
|
17
|
+
*
|
|
18
|
+
* @param module - Subsystem emitting the event (e.g. "server", "indexer", "backfill")
|
|
19
|
+
* @param op - Operation name (e.g. "request", "commit", "memory")
|
|
20
|
+
* @param fields - High-cardinality key-value context — include everything relevant
|
|
21
|
+
*/
|
|
2
22
|
export declare function emit(module: string, op: string, fields: Record<string, unknown>): void;
|
|
23
|
+
/**
|
|
24
|
+
* Start a millisecond timer. Call the returned function to get elapsed ms.
|
|
25
|
+
* Use with {@link emit} to add `duration_ms` to wide events.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const elapsed = timer()
|
|
29
|
+
* await doWork()
|
|
30
|
+
* emit('server', 'request', { path, status_code, duration_ms: elapsed() })
|
|
31
|
+
*/
|
|
3
32
|
export declare function timer(): () => number;
|
|
4
33
|
//# sourceMappingURL=logger.d.ts.map
|
package/dist/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAG5C;AAED,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAWtF;AAED,wBAAgB,KAAK,IAAI,MAAM,MAAM,CAGpC"}
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAG5C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAWtF;AAED;;;;;;;;GAQG;AACH,wBAAgB,KAAK,IAAI,MAAM,MAAM,CAGpC"}
|
package/dist/logger.js
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unstructured debug log — use sparingly for human-readable dev output.
|
|
3
|
+
* Prefer {@link emit} for anything that should be queryable in production.
|
|
4
|
+
* Disabled when `DEBUG=0`.
|
|
5
|
+
*/
|
|
1
6
|
export function log(...args) {
|
|
2
7
|
if (process.env.DEBUG === '0')
|
|
3
8
|
return;
|
|
4
9
|
console.log(...args);
|
|
5
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Emit a structured wide event as a single JSON line to stdout.
|
|
13
|
+
*
|
|
14
|
+
* Each call produces one canonical log line with a timestamp, module, operation,
|
|
15
|
+
* and arbitrary key-value fields — designed for columnar search and aggregation,
|
|
16
|
+
* not string grep. Pack as much context as possible into `fields` (request IDs,
|
|
17
|
+
* durations, status codes, user DIDs, counts) so a single event tells the full
|
|
18
|
+
* story. See https://loggingsucks.com for the philosophy behind this approach.
|
|
19
|
+
*
|
|
20
|
+
* Disabled when `DEBUG=0`.
|
|
21
|
+
*
|
|
22
|
+
* @param module - Subsystem emitting the event (e.g. "server", "indexer", "backfill")
|
|
23
|
+
* @param op - Operation name (e.g. "request", "commit", "memory")
|
|
24
|
+
* @param fields - High-cardinality key-value context — include everything relevant
|
|
25
|
+
*/
|
|
6
26
|
export function emit(module, op, fields) {
|
|
7
27
|
if (process.env.DEBUG === '0')
|
|
8
28
|
return;
|
|
@@ -17,6 +37,15 @@ export function emit(module, op, fields) {
|
|
|
17
37
|
}
|
|
18
38
|
process.stdout.write(JSON.stringify(entry) + '\n');
|
|
19
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Start a millisecond timer. Call the returned function to get elapsed ms.
|
|
42
|
+
* Use with {@link emit} to add `duration_ms` to wide events.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const elapsed = timer()
|
|
46
|
+
* await doWork()
|
|
47
|
+
* emit('server', 'request', { path, status_code, duration_ms: elapsed() })
|
|
48
|
+
*/
|
|
20
49
|
export function timer() {
|
|
21
50
|
const start = performance.now();
|
|
22
51
|
return () => Math.round(performance.now() - start);
|
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,
|
|
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 "./
|
|
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
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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,
|
|
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,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EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAyHxF;AAID,wBAAsB,WAAW,CAC/B,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,GAAG,CAAC,CAUd;AA0JD,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACtF,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,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"}
|
package/dist/oauth/server.js
CHANGED
|
@@ -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 "
|
|
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
|
-
|
|
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
|
package/dist/schema.d.ts.map
CHANGED
|
@@ -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,OA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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
|
};
|
package/dist/seed.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../src/seed.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../src/seed.ts"],"names":[],"mappings":"AA8BA,0EAA0E;AAC1E,MAAM,MAAM,OAAO,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAExE,gFAAgF;AAChF,MAAM,MAAM,OAAO,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAA;AAE/F,0FAA0F;AAC1F,MAAM,MAAM,QAAQ,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAE7E;;;;;;;;;;GAUG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,EAAE,QAAQ;4BAO1D,MAAM,KAAG,OAAO,CAAC,OAAO,CAAC;mBA6BlC,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,WAC3C,OAAO,cACJ,CAAC,UACL,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QACvE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,KACrB,OAAO,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE;YAAE,GAAG,EAAE,MAAM,CAAC;YAAC,GAAG,EAAE,MAAM,CAAA;SAAE,CAAC;QAAC,gBAAgB,EAAE,MAAM,CAAA;KAAE,CAAC;0BAkCrE,OAAO,YAAY,MAAM,KAAG,OAAO,CAAC,OAAO,CAAC;EA4BhF"}
|
package/dist/seed.js
CHANGED
|
@@ -1,12 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test data seeding helpers for populating a local PDS.
|
|
3
|
+
*
|
|
4
|
+
* Place a seed script at `seeds/seed.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
|
|
57
|
-
console.log(`[seed] [${session.handle}] ${collection} → ${uri}`);
|
|
58
|
-
return
|
|
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() || '';
|
package/dist/server.d.ts.map
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
601
|
-
|
|
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
|
package/dist/setup.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"
|
|
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 "./
|
|
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
|
package/dist/xrpc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../src/xrpc.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../src/xrpc.ts"],"names":[],"mappings":"AAsCA,OAAO,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAElD,YAAY,EAAE,GAAG,EAAE,OAAO,EAAE,CAAA;AAE5B,gFAAgF;AAChF,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,MAAM,SAAM;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;gBACN,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CAIhD;AACD,0DAA0D;AAC1D,qBAAa,aAAc,SAAQ,mBAAmB;IACpD,MAAM,SAAM;gBACA,OAAO,SAAc;CAGlC;AAED;;;;;;;;GAQG;AACH,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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;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
|
}
|