@hatk/hatk 0.0.1-alpha.6 → 0.0.1-alpha.60
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/adapter.d.ts +19 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +108 -0
- package/dist/backfill.d.ts +2 -2
- package/dist/backfill.d.ts.map +1 -1
- package/dist/backfill.js +78 -31
- package/dist/car.d.ts +42 -10
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +154 -14
- package/dist/cli.js +243 -1043
- package/dist/config.d.ts +31 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +40 -9
- package/dist/database/adapter-factory.d.ts +6 -0
- package/dist/database/adapter-factory.d.ts.map +1 -0
- package/dist/database/adapter-factory.js +20 -0
- package/dist/database/adapters/duckdb-search.d.ts +12 -0
- package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
- package/dist/database/adapters/duckdb-search.js +27 -0
- package/dist/database/adapters/duckdb.d.ts +25 -0
- package/dist/database/adapters/duckdb.d.ts.map +1 -0
- package/dist/database/adapters/duckdb.js +161 -0
- package/dist/database/adapters/sqlite-search.d.ts +23 -0
- package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
- package/dist/database/adapters/sqlite-search.js +74 -0
- package/dist/database/adapters/sqlite.d.ts +18 -0
- package/dist/database/adapters/sqlite.d.ts.map +1 -0
- package/dist/database/adapters/sqlite.js +88 -0
- package/dist/{db.d.ts → database/db.d.ts} +57 -6
- package/dist/database/db.d.ts.map +1 -0
- package/dist/{db.js → database/db.js} +730 -549
- package/dist/database/dialect.d.ts +45 -0
- package/dist/database/dialect.d.ts.map +1 -0
- package/dist/database/dialect.js +72 -0
- package/dist/{fts.d.ts → database/fts.d.ts} +7 -0
- package/dist/database/fts.d.ts.map +1 -0
- package/dist/{fts.js → database/fts.js} +116 -32
- package/dist/database/index.d.ts +7 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +6 -0
- package/dist/database/ports.d.ts +50 -0
- package/dist/database/ports.d.ts.map +1 -0
- package/dist/database/ports.js +1 -0
- package/dist/{schema.d.ts → database/schema.d.ts} +14 -3
- package/dist/database/schema.d.ts.map +1 -0
- package/dist/{schema.js → database/schema.js} +81 -41
- package/dist/dev-entry.d.ts +8 -0
- package/dist/dev-entry.d.ts.map +1 -0
- package/dist/dev-entry.js +112 -0
- package/dist/feeds.d.ts +12 -8
- package/dist/feeds.d.ts.map +1 -1
- package/dist/feeds.js +51 -6
- package/dist/hooks.d.ts +85 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +161 -0
- package/dist/hydrate.d.ts +7 -6
- package/dist/hydrate.d.ts.map +1 -1
- package/dist/hydrate.js +4 -16
- package/dist/indexer.d.ts +22 -0
- package/dist/indexer.d.ts.map +1 -1
- package/dist/indexer.js +123 -32
- package/dist/labels.d.ts +36 -0
- package/dist/labels.d.ts.map +1 -1
- package/dist/labels.js +71 -6
- package/dist/lexicon-resolve.d.ts.map +1 -1
- package/dist/lexicon-resolve.js +27 -112
- package/dist/lexicons/com/atproto/label/defs.json +75 -0
- package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
- package/dist/lexicons/com/atproto/repo/strongRef.json +24 -0
- package/dist/lexicons/dev/hatk/applyWrites.json +87 -0
- package/dist/lexicons/dev/hatk/createRecord.json +40 -0
- package/dist/lexicons/dev/hatk/createReport.json +48 -0
- package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
- package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
- package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
- package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
- package/dist/lexicons/dev/hatk/getFeed.json +30 -0
- package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
- package/dist/lexicons/dev/hatk/getRecord.json +26 -0
- package/dist/lexicons/dev/hatk/getRecords.json +32 -0
- package/dist/lexicons/dev/hatk/putPreference.json +28 -0
- package/dist/lexicons/dev/hatk/putRecord.json +41 -0
- package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
- package/dist/lexicons/dev/hatk/uploadBlob.json +23 -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 +137 -67
- package/dist/mst.d.ts +18 -1
- package/dist/mst.d.ts.map +1 -1
- package/dist/mst.js +19 -8
- package/dist/oauth/db.d.ts +3 -1
- package/dist/oauth/db.d.ts.map +1 -1
- package/dist/oauth/db.js +48 -19
- package/dist/oauth/server.d.ts +24 -0
- package/dist/oauth/server.d.ts.map +1 -1
- package/dist/oauth/server.js +198 -22
- package/dist/oauth/session.d.ts +11 -0
- package/dist/oauth/session.d.ts.map +1 -0
- package/dist/oauth/session.js +65 -0
- package/dist/opengraph.d.ts +10 -0
- package/dist/opengraph.d.ts.map +1 -1
- package/dist/opengraph.js +80 -40
- package/dist/pds-proxy.d.ts +60 -0
- package/dist/pds-proxy.d.ts.map +1 -0
- package/dist/pds-proxy.js +277 -0
- package/dist/push.d.ts +34 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/push.js +184 -0
- package/dist/renderer.d.ts +27 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +46 -0
- package/dist/resolve-hatk.d.ts +6 -0
- package/dist/resolve-hatk.d.ts.map +1 -0
- package/dist/resolve-hatk.js +20 -0
- package/dist/response.d.ts +16 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +69 -0
- package/dist/scanner.d.ts +21 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +88 -0
- package/dist/seed.d.ts +19 -0
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +43 -4
- package/dist/server-init.d.ts +8 -0
- package/dist/server-init.d.ts.map +1 -0
- package/dist/server-init.js +62 -0
- package/dist/server.d.ts +26 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +629 -635
- package/dist/setup.d.ts +28 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +50 -3
- package/dist/templates/feed.tpl +14 -0
- package/dist/templates/hook.tpl +5 -0
- package/dist/templates/label.tpl +15 -0
- package/dist/templates/og.tpl +17 -0
- package/dist/templates/seed.tpl +11 -0
- package/dist/templates/setup.tpl +5 -0
- package/dist/templates/test-feed.tpl +19 -0
- package/dist/templates/test-xrpc.tpl +19 -0
- package/dist/templates/xrpc.tpl +41 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js +39 -32
- package/dist/views.js +1 -1
- package/dist/vite-plugin.d.ts +1 -1
- package/dist/vite-plugin.d.ts.map +1 -1
- package/dist/vite-plugin.js +254 -66
- package/dist/xrpc.d.ts +75 -11
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +189 -39
- package/package.json +14 -7
- package/public/admin.html +133 -54
- package/dist/db.d.ts.map +0 -1
- package/dist/fts.d.ts.map +0 -1
- package/dist/oauth/hooks.d.ts +0 -10
- package/dist/oauth/hooks.d.ts.map +0 -1
- package/dist/oauth/hooks.js +0 -40
- package/dist/schema.d.ts.map +0 -1
- package/dist/test-browser.d.ts +0 -14
- package/dist/test-browser.d.ts.map +0 -1
- package/dist/test-browser.js +0 -26
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type SessionData = {
|
|
2
|
+
did: string;
|
|
3
|
+
handle: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function getSessionCookieName(): string;
|
|
6
|
+
export declare function initSession(privateJwk: JsonWebKey, cookieName?: string): void;
|
|
7
|
+
export declare function createSessionCookie(data: SessionData): Promise<string>;
|
|
8
|
+
export declare function sessionCookieHeader(value: string, secure: boolean): string;
|
|
9
|
+
export declare function clearSessionCookieHeader(): string;
|
|
10
|
+
export declare function parseSessionCookie(request: Request): Promise<SessionData | null>;
|
|
11
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/oauth/session.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,WAAW,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAEzD,wBAAgB,oBAAoB,IAAI,MAAM,CAE7C;AAED,wBAAgB,WAAW,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAG7E;AAcD,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAM5E;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAI1E;AAED,wBAAgB,wBAAwB,IAAI,MAAM,CAEjD;AAED,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAuBtF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// SSR session cookie — AES-GCM encrypted HttpOnly cookie for server-side viewer resolution.
|
|
2
|
+
// Separate from OAuth protocol flows but uses the same server keypair for key derivation.
|
|
3
|
+
import { base64UrlEncode, base64UrlDecode } from "./crypto.js";
|
|
4
|
+
let _privateJwk;
|
|
5
|
+
let _cookieName = '__hatk_session';
|
|
6
|
+
const MAX_AGE = 30 * 24 * 60 * 60; // 30 days in seconds
|
|
7
|
+
export function getSessionCookieName() {
|
|
8
|
+
return _cookieName;
|
|
9
|
+
}
|
|
10
|
+
export function initSession(privateJwk, cookieName) {
|
|
11
|
+
_privateJwk = privateJwk;
|
|
12
|
+
if (cookieName)
|
|
13
|
+
_cookieName = cookieName;
|
|
14
|
+
}
|
|
15
|
+
async function aesKey() {
|
|
16
|
+
const raw = new TextEncoder().encode(JSON.stringify(_privateJwk, Object.keys(_privateJwk).sort()));
|
|
17
|
+
const keyMaterial = await crypto.subtle.importKey('raw', raw, 'HKDF', false, ['deriveKey']);
|
|
18
|
+
return crypto.subtle.deriveKey({ name: 'HKDF', hash: 'SHA-256', salt: new Uint8Array(0), info: new TextEncoder().encode('hatk-session-cookie') }, keyMaterial, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
|
|
19
|
+
}
|
|
20
|
+
export async function createSessionCookie(data) {
|
|
21
|
+
const payload = JSON.stringify({ ...data, ts: Math.floor(Date.now() / 1000) });
|
|
22
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
23
|
+
const key = await aesKey();
|
|
24
|
+
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, new TextEncoder().encode(payload));
|
|
25
|
+
return `${base64UrlEncode(iv)}.${base64UrlEncode(new Uint8Array(ciphertext))}`;
|
|
26
|
+
}
|
|
27
|
+
export function sessionCookieHeader(value, secure) {
|
|
28
|
+
const parts = [`${_cookieName}=${value}`, 'HttpOnly', 'SameSite=Lax', 'Path=/', `Max-Age=${MAX_AGE}`];
|
|
29
|
+
if (secure)
|
|
30
|
+
parts.push('Secure');
|
|
31
|
+
return parts.join('; ');
|
|
32
|
+
}
|
|
33
|
+
export function clearSessionCookieHeader() {
|
|
34
|
+
return `${_cookieName}=; HttpOnly; SameSite=Lax; Path=/; Max-Age=0`;
|
|
35
|
+
}
|
|
36
|
+
export async function parseSessionCookie(request) {
|
|
37
|
+
const cookieHeader = request.headers.get('cookie');
|
|
38
|
+
if (!cookieHeader)
|
|
39
|
+
return null;
|
|
40
|
+
const match = cookieHeader
|
|
41
|
+
.split(';')
|
|
42
|
+
.map((c) => c.trim())
|
|
43
|
+
.find((c) => c.startsWith(`${_cookieName}=`));
|
|
44
|
+
if (!match)
|
|
45
|
+
return null;
|
|
46
|
+
const value = match.slice(_cookieName.length + 1);
|
|
47
|
+
const parts = value.split('.');
|
|
48
|
+
if (parts.length !== 2)
|
|
49
|
+
return null;
|
|
50
|
+
try {
|
|
51
|
+
const iv = base64UrlDecode(parts[0]);
|
|
52
|
+
const ciphertext = base64UrlDecode(parts[1]);
|
|
53
|
+
const key = await aesKey();
|
|
54
|
+
const plaintext = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext);
|
|
55
|
+
const data = JSON.parse(new TextDecoder().decode(plaintext));
|
|
56
|
+
if (!data.did || !data.handle || !data.ts)
|
|
57
|
+
return null;
|
|
58
|
+
if (Date.now() / 1000 - data.ts > MAX_AGE)
|
|
59
|
+
return null;
|
|
60
|
+
return { did: data.did, handle: data.handle };
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
package/dist/opengraph.d.ts
CHANGED
|
@@ -28,7 +28,17 @@ export interface OpengraphResult {
|
|
|
28
28
|
description?: string;
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
+
export declare function defineOG(path: string, generate: (ctx: OpengraphContext) => Promise<OpengraphResult>): {
|
|
32
|
+
__type: "og";
|
|
33
|
+
path: string;
|
|
34
|
+
generate: (ctx: OpengraphContext) => Promise<OpengraphResult>;
|
|
35
|
+
};
|
|
31
36
|
export declare function initOpengraph(ogDir: string): Promise<void>;
|
|
37
|
+
/** Register a single OG handler from a scanned server/ module. */
|
|
38
|
+
export declare function registerOgHandler(ogMod: {
|
|
39
|
+
path: string;
|
|
40
|
+
generate: (ctx: OpengraphContext) => Promise<OpengraphResult>;
|
|
41
|
+
}): void;
|
|
32
42
|
export declare function handleOpengraphRequest(pathname: string): Promise<Buffer | null>;
|
|
33
43
|
export declare function buildOgMeta(pathname: string, origin: string): string | null;
|
|
34
44
|
//# sourceMappingURL=opengraph.d.ts.map
|
package/dist/opengraph.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opengraph.d.ts","sourceRoot":"","sources":["../src/opengraph.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"opengraph.d.ts","sourceRoot":"","sources":["../src/opengraph.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAE5C,4CAA4C;AAC5C,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE;QACL,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;QAC3B,QAAQ,CAAC,EAAE,CAAC,UAAU,GAAG,MAAM,CAAC,EAAE,GAAG,MAAM,CAAA;QAC3C,GAAG,CAAC,EAAE,MAAM,CAAA;QACZ,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,MAAM,CAAC,EAAE,MAAM,CAAA;QACf,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KACnB,CAAA;CACF;AAED,uDAAuD;AACvD,MAAM,WAAW,gBAAiB,SAAQ,WAAW;IACnD,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;CACpD;AAED,qDAAqD;AACrD,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,UAAU,CAAA;IACnB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAA;KAAE,CAAA;IAC5D,IAAI,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CAChD;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,eAAe,CAAC;;;oBAA7C,gBAAgB,KAAK,OAAO,CAAC,eAAe,CAAC;EAEnG;AAkCD,wBAAsB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA0EhE;AAED,kEAAkE;AAClE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IACvC,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;CAC9D,GAAG,IAAI,CAsDP;AAED,wBAAsB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA+BrF;AAED,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAyC3E"}
|
package/dist/opengraph.js
CHANGED
|
@@ -9,11 +9,23 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
9
9
|
import { resolve } from 'node:path';
|
|
10
10
|
import { readFileSync, readdirSync } from 'node:fs';
|
|
11
11
|
import { log } from "./logger.js";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
// Lazy-imported to avoid CJS require() issues in Vite's module runner
|
|
13
|
+
let _satori = null;
|
|
14
|
+
let _Resvg = null;
|
|
15
|
+
async function getSatori() {
|
|
16
|
+
if (!_satori)
|
|
17
|
+
_satori = (await import('satori')).default;
|
|
18
|
+
return _satori;
|
|
19
|
+
}
|
|
20
|
+
async function getResvg() {
|
|
21
|
+
if (!_Resvg)
|
|
22
|
+
_Resvg = (await import('@resvg/resvg-js')).Resvg;
|
|
23
|
+
return _Resvg;
|
|
24
|
+
}
|
|
25
|
+
import { buildXrpcContext } from "./xrpc.js";
|
|
26
|
+
export function defineOG(path, generate) {
|
|
27
|
+
return { __type: 'og', path, generate };
|
|
28
|
+
}
|
|
17
29
|
const handlers = [];
|
|
18
30
|
const pageRoutes = [];
|
|
19
31
|
let defaultFont = null;
|
|
@@ -51,7 +63,7 @@ export async function initOpengraph(ogDir) {
|
|
|
51
63
|
for (const file of files) {
|
|
52
64
|
const name = file.replace(/\.(ts|js)$/, '');
|
|
53
65
|
const scriptPath = resolve(ogDir, file);
|
|
54
|
-
const mod = await import(__rewriteRelativeImportExtension(scriptPath));
|
|
66
|
+
const mod = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `${scriptPath}?t=${Date.now()}`));
|
|
55
67
|
const handler = mod.default;
|
|
56
68
|
if (!handler.path) {
|
|
57
69
|
console.warn(`[opengraph] ${file} missing 'path' export, skipping`);
|
|
@@ -64,38 +76,10 @@ export async function initOpengraph(ogDir) {
|
|
|
64
76
|
pattern,
|
|
65
77
|
paramNames,
|
|
66
78
|
execute: async (params) => {
|
|
67
|
-
const ctx =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
limit: 1,
|
|
72
|
-
viewer: null,
|
|
73
|
-
packCursor,
|
|
74
|
-
unpackCursor,
|
|
75
|
-
isTakendown: isTakendownDid,
|
|
76
|
-
filterTakendownDids,
|
|
77
|
-
search: searchRecords,
|
|
78
|
-
resolve: resolveRecords,
|
|
79
|
-
lookup: async (collection, field, values) => {
|
|
80
|
-
if (values.length === 0)
|
|
81
|
-
return new Map();
|
|
82
|
-
const unique = [...new Set(values.filter(Boolean))];
|
|
83
|
-
return lookupByFieldBatch(collection, field, unique);
|
|
84
|
-
},
|
|
85
|
-
count: async (collection, field, values) => {
|
|
86
|
-
if (values.length === 0)
|
|
87
|
-
return new Map();
|
|
88
|
-
const unique = [...new Set(values.filter(Boolean))];
|
|
89
|
-
return countByFieldBatch(collection, field, unique);
|
|
90
|
-
},
|
|
91
|
-
exists: async (collection, filters) => {
|
|
92
|
-
const conditions = Object.entries(filters).map(([field, value]) => ({ field, value }));
|
|
93
|
-
const uri = await findUriByFields(collection, conditions);
|
|
94
|
-
return uri !== null;
|
|
95
|
-
},
|
|
96
|
-
labels: queryLabelsForUris,
|
|
97
|
-
blobUrl,
|
|
98
|
-
};
|
|
79
|
+
const ctx = buildXrpcContext(params, undefined, 1, null);
|
|
80
|
+
// Override blobUrl to use _og presets (jpeg) — satori doesn't support webp
|
|
81
|
+
const origBlobUrl = ctx.blobUrl;
|
|
82
|
+
ctx.blobUrl = (did, ref, preset = 'avatar') => origBlobUrl(did, ref, `${preset}_og`);
|
|
99
83
|
ctx.fetchImage = async (url) => {
|
|
100
84
|
try {
|
|
101
85
|
const resp = await fetch(url, { redirect: 'follow' });
|
|
@@ -117,7 +101,7 @@ export async function initOpengraph(ogDir) {
|
|
|
117
101
|
...result.options,
|
|
118
102
|
fonts: [...(defaultFont ? [defaultFont] : []), ...(result.options?.fonts || [])],
|
|
119
103
|
};
|
|
120
|
-
const svg = await
|
|
104
|
+
const svg = await (await getSatori())(element, options);
|
|
121
105
|
return { svg, meta: result.meta };
|
|
122
106
|
},
|
|
123
107
|
});
|
|
@@ -129,6 +113,61 @@ export async function initOpengraph(ogDir) {
|
|
|
129
113
|
}
|
|
130
114
|
}
|
|
131
115
|
}
|
|
116
|
+
/** Register a single OG handler from a scanned server/ module. */
|
|
117
|
+
export function registerOgHandler(ogMod) {
|
|
118
|
+
const { pattern, paramNames } = compilePath(ogMod.path);
|
|
119
|
+
const name = ogMod.path.replace(/^\//, '').replace(/\//g, '-').replace(/:/g, '');
|
|
120
|
+
// Load default font if not already loaded
|
|
121
|
+
if (!defaultFont) {
|
|
122
|
+
try {
|
|
123
|
+
const fontPath = resolve(import.meta.dirname, '..', 'fonts', 'Inter-Regular.woff');
|
|
124
|
+
const fontData = readFileSync(fontPath);
|
|
125
|
+
defaultFont = { name: 'Inter', data: fontData.buffer, weight: 400, style: 'normal' };
|
|
126
|
+
}
|
|
127
|
+
catch { }
|
|
128
|
+
}
|
|
129
|
+
handlers.push({
|
|
130
|
+
name,
|
|
131
|
+
path: ogMod.path,
|
|
132
|
+
pattern,
|
|
133
|
+
paramNames,
|
|
134
|
+
execute: async (params) => {
|
|
135
|
+
const ctx = buildXrpcContext(params, undefined, 1, null);
|
|
136
|
+
// Override blobUrl to use _og presets (jpeg) — satori doesn't support webp
|
|
137
|
+
const origBlobUrl = ctx.blobUrl;
|
|
138
|
+
ctx.blobUrl = (did, ref, preset = 'avatar') => origBlobUrl(did, ref, `${preset}_og`);
|
|
139
|
+
ctx.fetchImage = async (url) => {
|
|
140
|
+
try {
|
|
141
|
+
const resp = await fetch(url, { redirect: 'follow' });
|
|
142
|
+
if (!resp.ok)
|
|
143
|
+
return null;
|
|
144
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
145
|
+
const contentType = resp.headers.get('content-type') || 'image/jpeg';
|
|
146
|
+
return `data:${contentType};base64,${buf.toString('base64')}`;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const result = await ogMod.generate(ctx);
|
|
153
|
+
const element = result.element;
|
|
154
|
+
const options = {
|
|
155
|
+
width: 1200,
|
|
156
|
+
height: 630,
|
|
157
|
+
...result.options,
|
|
158
|
+
fonts: [...(defaultFont ? [defaultFont] : []), ...(result.options?.fonts || [])],
|
|
159
|
+
};
|
|
160
|
+
const svg = await (await getSatori())(element, options);
|
|
161
|
+
return { svg, meta: result.meta };
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
const pagePath = ogMod.path.replace(/^\/og/, '');
|
|
165
|
+
if (pagePath !== ogMod.path) {
|
|
166
|
+
const compiled = compilePath(pagePath);
|
|
167
|
+
pageRoutes.push({ ogPath: ogMod.path, pattern: compiled.pattern, paramNames: compiled.paramNames, name });
|
|
168
|
+
}
|
|
169
|
+
log(`[opengraph] registered: ${name} → ${ogMod.path}`);
|
|
170
|
+
}
|
|
132
171
|
export async function handleOpengraphRequest(pathname) {
|
|
133
172
|
const cached = cache.get(pathname);
|
|
134
173
|
if (cached && cached.expires > Date.now())
|
|
@@ -143,6 +182,7 @@ export async function handleOpengraphRequest(pathname) {
|
|
|
143
182
|
});
|
|
144
183
|
try {
|
|
145
184
|
const { svg, meta } = await handler.execute(params);
|
|
185
|
+
const Resvg = await getResvg();
|
|
146
186
|
const png = new Resvg(svg, { fitTo: { mode: 'width', value: 1200 } }).render().asPng();
|
|
147
187
|
if (cache.size >= CACHE_MAX) {
|
|
148
188
|
const oldest = cache.keys().next().value;
|
|
@@ -153,7 +193,7 @@ export async function handleOpengraphRequest(pathname) {
|
|
|
153
193
|
return png;
|
|
154
194
|
}
|
|
155
195
|
catch (err) {
|
|
156
|
-
console.error(`[opengraph] error in ${handler.name}:`, err.message);
|
|
196
|
+
console.error(`[opengraph] error in ${handler.name}:`, err.message, err.stack);
|
|
157
197
|
return null;
|
|
158
198
|
}
|
|
159
199
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { OAuthConfig } from './config.ts';
|
|
2
|
+
export declare class ProxyError extends Error {
|
|
3
|
+
status: number;
|
|
4
|
+
constructor(status: number, message: string);
|
|
5
|
+
}
|
|
6
|
+
export declare class ScopeMissingProxyError extends ProxyError {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
export declare function pdsCreateRecord(oauthConfig: OAuthConfig, viewer: {
|
|
10
|
+
did: string;
|
|
11
|
+
}, input: {
|
|
12
|
+
collection: string;
|
|
13
|
+
repo?: string;
|
|
14
|
+
rkey?: string;
|
|
15
|
+
record: Record<string, unknown>;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
uri?: string;
|
|
18
|
+
cid?: string;
|
|
19
|
+
}>;
|
|
20
|
+
export declare function pdsDeleteRecord(oauthConfig: OAuthConfig, viewer: {
|
|
21
|
+
did: string;
|
|
22
|
+
}, input: {
|
|
23
|
+
collection: string;
|
|
24
|
+
rkey: string;
|
|
25
|
+
}): Promise<Record<string, unknown>>;
|
|
26
|
+
export declare function pdsPutRecord(oauthConfig: OAuthConfig, viewer: {
|
|
27
|
+
did: string;
|
|
28
|
+
}, input: {
|
|
29
|
+
collection: string;
|
|
30
|
+
rkey: string;
|
|
31
|
+
record: Record<string, unknown>;
|
|
32
|
+
repo?: string;
|
|
33
|
+
}): Promise<{
|
|
34
|
+
uri?: string;
|
|
35
|
+
cid?: string;
|
|
36
|
+
}>;
|
|
37
|
+
export interface ApplyWritesOp {
|
|
38
|
+
$type: string;
|
|
39
|
+
collection: string;
|
|
40
|
+
rkey?: string;
|
|
41
|
+
value?: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
export interface ApplyWritesResult {
|
|
44
|
+
$type: string;
|
|
45
|
+
uri?: string;
|
|
46
|
+
cid?: string;
|
|
47
|
+
}
|
|
48
|
+
export declare function pdsApplyWrites(oauthConfig: OAuthConfig, viewer: {
|
|
49
|
+
did: string;
|
|
50
|
+
}, input: {
|
|
51
|
+
writes: ApplyWritesOp[];
|
|
52
|
+
}): Promise<{
|
|
53
|
+
results?: ApplyWritesResult[];
|
|
54
|
+
}>;
|
|
55
|
+
export declare function pdsUploadBlob(oauthConfig: OAuthConfig, viewer: {
|
|
56
|
+
did: string;
|
|
57
|
+
}, body: Uint8Array, contentType: string): Promise<{
|
|
58
|
+
blob: unknown;
|
|
59
|
+
}>;
|
|
60
|
+
//# sourceMappingURL=pds-proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pds-proxy.d.ts","sourceRoot":"","sources":["../src/pds-proxy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAU9C,qBAAa,UAAW,SAAQ,KAAK;IAE1B,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM;CAIlB;AAED,qBAAa,sBAAuB,SAAQ,UAAU;;CAIrD;AAuHD,wBAAsB,eAAe,CACnC,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,EACvB,KAAK,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,GAC3F,OAAO,CAAC;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAwCzC;AAED,wBAAsB,eAAe,CACnC,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,EACvB,KAAK,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC1C,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAyBlC;AAED,wBAAsB,YAAY,CAChC,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,EACvB,KAAK,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GAC1F,OAAO,CAAC;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAqCzC;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAChC;AAED,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAYD,wBAAsB,cAAc,CAClC,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,EACvB,KAAK,EAAE;IAAE,MAAM,EAAE,aAAa,EAAE,CAAA;CAAE,GACjC,OAAO,CAAC;IAAE,OAAO,CAAC,EAAE,iBAAiB,EAAE,CAAA;CAAE,CAAC,CAgE5C;AAED,wBAAsB,aAAa,CACjC,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,EACvB,IAAI,EAAE,UAAU,EAChB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,CAS5B"}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Shared PDS proxy functions — used by both HTTP route handlers and XRPC handlers.
|
|
2
|
+
import { getSession, getServerKey, deleteSession } from "./oauth/db.js";
|
|
3
|
+
import { createDpopProof } from "./oauth/dpop.js";
|
|
4
|
+
import { refreshPdsSession } from "./oauth/server.js";
|
|
5
|
+
import { validateRecord } from '@bigmoves/lexicon';
|
|
6
|
+
import { getLexiconArray } from "./database/schema.js";
|
|
7
|
+
import { insertRecord, deleteRecord as dbDeleteRecord } from "./database/db.js";
|
|
8
|
+
import { emit } from "./logger.js";
|
|
9
|
+
import { runLabelRules } from "./labels.js";
|
|
10
|
+
export class ProxyError extends Error {
|
|
11
|
+
status;
|
|
12
|
+
constructor(status, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.status = status;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class ScopeMissingProxyError extends ProxyError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super(401, 'ScopeMissingError');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Shared retry logic: DPoP nonce handling + token refresh. */
|
|
23
|
+
async function withDpopRetry(oauthConfig, session, doFetch) {
|
|
24
|
+
let accessToken = session.access_token;
|
|
25
|
+
let result = await doFetch(accessToken);
|
|
26
|
+
if (result.ok)
|
|
27
|
+
return result;
|
|
28
|
+
let nonce;
|
|
29
|
+
// Step 1: handle DPoP nonce requirement
|
|
30
|
+
if (result.body.error === 'use_dpop_nonce') {
|
|
31
|
+
nonce = result.headers.get('DPoP-Nonce') || undefined;
|
|
32
|
+
if (nonce) {
|
|
33
|
+
result = await doFetch(accessToken, nonce);
|
|
34
|
+
if (result.ok)
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Step 2: handle insufficient scope — clear session so user re-authenticates with updated scopes
|
|
39
|
+
if (result.body.error === 'ScopeMissingError') {
|
|
40
|
+
await deleteSession(session.did);
|
|
41
|
+
throw new ScopeMissingProxyError();
|
|
42
|
+
}
|
|
43
|
+
// Step 3: handle expired PDS token — refresh and retry
|
|
44
|
+
// The PDS returns 'InvalidToken' or 'ExpiredToken' (AT Proto PascalCase convention)
|
|
45
|
+
// while the OAuth spec uses 'invalid_token' (RFC 6750 snake_case)
|
|
46
|
+
const err = result.body.error;
|
|
47
|
+
if (err === 'invalid_token' || err === 'InvalidToken' || err === 'ExpiredToken') {
|
|
48
|
+
const refreshed = await refreshPdsSession(oauthConfig, session);
|
|
49
|
+
if (refreshed) {
|
|
50
|
+
accessToken = refreshed.accessToken;
|
|
51
|
+
result = await doFetch(accessToken, nonce);
|
|
52
|
+
if (result.ok)
|
|
53
|
+
return result;
|
|
54
|
+
if (result.body.error === 'use_dpop_nonce') {
|
|
55
|
+
nonce = result.headers.get('DPoP-Nonce') || undefined;
|
|
56
|
+
if (nonce)
|
|
57
|
+
result = await doFetch(accessToken, nonce);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
async function proxyToPds(oauthConfig, session, method, pdsUrl, body) {
|
|
64
|
+
const serverKey = await getServerKey('appview-oauth-key');
|
|
65
|
+
const privateJwk = JSON.parse(serverKey.privateKey);
|
|
66
|
+
const publicJwk = JSON.parse(serverKey.publicKey);
|
|
67
|
+
return withDpopRetry(oauthConfig, session, async (token, nonce) => {
|
|
68
|
+
const proof = await createDpopProof(privateJwk, publicJwk, method, pdsUrl, token, nonce);
|
|
69
|
+
const res = await fetch(pdsUrl, {
|
|
70
|
+
method,
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'application/json',
|
|
73
|
+
Authorization: `DPoP ${token}`,
|
|
74
|
+
DPoP: proof,
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify(body),
|
|
77
|
+
});
|
|
78
|
+
const resBody = await res.json().catch(() => ({}));
|
|
79
|
+
return { ok: res.ok, status: res.status, body: resBody, headers: res.headers };
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** Proxy a raw binary request to the user's PDS with DPoP + nonce retry + token refresh. */
|
|
83
|
+
async function proxyToPdsRaw(oauthConfig, session, pdsUrl, body, contentType) {
|
|
84
|
+
const serverKey = await getServerKey('appview-oauth-key');
|
|
85
|
+
const privateJwk = JSON.parse(serverKey.privateKey);
|
|
86
|
+
const publicJwk = JSON.parse(serverKey.publicKey);
|
|
87
|
+
return withDpopRetry(oauthConfig, session, async (token, nonce) => {
|
|
88
|
+
const proof = await createDpopProof(privateJwk, publicJwk, 'POST', pdsUrl, token, nonce);
|
|
89
|
+
const res = await fetch(pdsUrl, {
|
|
90
|
+
method: 'POST',
|
|
91
|
+
headers: {
|
|
92
|
+
'Content-Type': contentType,
|
|
93
|
+
'Content-Length': String(body.length),
|
|
94
|
+
Authorization: `DPoP ${token}`,
|
|
95
|
+
DPoP: proof,
|
|
96
|
+
},
|
|
97
|
+
body: Buffer.from(body),
|
|
98
|
+
});
|
|
99
|
+
const resBody = await res.json().catch(() => ({}));
|
|
100
|
+
return { ok: res.ok, status: res.status, body: resBody, headers: res.headers };
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
// --- High-level proxy functions ---
|
|
104
|
+
export async function pdsCreateRecord(oauthConfig, viewer, input) {
|
|
105
|
+
const validationError = validateRecord(getLexiconArray(), input.collection, input.record);
|
|
106
|
+
if (validationError) {
|
|
107
|
+
throw new ProxyError(400, `InvalidRecord: ${validationError.path ? validationError.path + ': ' : ''}${validationError.message}`);
|
|
108
|
+
}
|
|
109
|
+
const session = await getSession(viewer.did);
|
|
110
|
+
if (!session)
|
|
111
|
+
throw new ProxyError(401, 'No PDS session for user');
|
|
112
|
+
const pdsUrl = `${session.pds_endpoint}/xrpc/com.atproto.repo.createRecord`;
|
|
113
|
+
const pdsBody = {
|
|
114
|
+
repo: viewer.did,
|
|
115
|
+
collection: input.collection,
|
|
116
|
+
rkey: input.rkey,
|
|
117
|
+
record: input.record,
|
|
118
|
+
};
|
|
119
|
+
const pdsRes = await proxyToPds(oauthConfig, session, 'POST', pdsUrl, pdsBody);
|
|
120
|
+
if (!pdsRes.ok)
|
|
121
|
+
throw new ProxyError(pdsRes.status, String(pdsRes.body.error || 'PDS write failed'));
|
|
122
|
+
try {
|
|
123
|
+
await insertRecord(input.collection, String(pdsRes.body.uri), String(pdsRes.body.cid), viewer.did, input.record);
|
|
124
|
+
await runLabelRules({
|
|
125
|
+
uri: String(pdsRes.body.uri),
|
|
126
|
+
cid: String(pdsRes.body.cid),
|
|
127
|
+
did: viewer.did,
|
|
128
|
+
collection: input.collection,
|
|
129
|
+
value: input.record,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
emit('pds-proxy', 'local_index_error', {
|
|
134
|
+
op: 'createRecord',
|
|
135
|
+
error: err instanceof Error ? err.message : String(err),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return pdsRes.body;
|
|
139
|
+
}
|
|
140
|
+
export async function pdsDeleteRecord(oauthConfig, viewer, input) {
|
|
141
|
+
const session = await getSession(viewer.did);
|
|
142
|
+
if (!session)
|
|
143
|
+
throw new ProxyError(401, 'No PDS session for user');
|
|
144
|
+
const pdsUrl = `${session.pds_endpoint}/xrpc/com.atproto.repo.deleteRecord`;
|
|
145
|
+
const pdsBody = {
|
|
146
|
+
repo: viewer.did,
|
|
147
|
+
collection: input.collection,
|
|
148
|
+
rkey: input.rkey,
|
|
149
|
+
};
|
|
150
|
+
const pdsRes = await proxyToPds(oauthConfig, session, 'POST', pdsUrl, pdsBody);
|
|
151
|
+
if (!pdsRes.ok)
|
|
152
|
+
throw new ProxyError(pdsRes.status, String(pdsRes.body.error || 'PDS delete failed'));
|
|
153
|
+
try {
|
|
154
|
+
const uri = `at://${viewer.did}/${input.collection}/${input.rkey}`;
|
|
155
|
+
await dbDeleteRecord(input.collection, uri);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
emit('pds-proxy', 'local_index_error', {
|
|
159
|
+
op: 'deleteRecord',
|
|
160
|
+
error: err instanceof Error ? err.message : String(err),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return pdsRes.body;
|
|
164
|
+
}
|
|
165
|
+
export async function pdsPutRecord(oauthConfig, viewer, input) {
|
|
166
|
+
const validationError = validateRecord(getLexiconArray(), input.collection, input.record);
|
|
167
|
+
if (validationError) {
|
|
168
|
+
throw new ProxyError(400, `InvalidRecord: ${validationError.path ? validationError.path + ': ' : ''}${validationError.message}`);
|
|
169
|
+
}
|
|
170
|
+
const session = await getSession(viewer.did);
|
|
171
|
+
if (!session)
|
|
172
|
+
throw new ProxyError(401, 'No PDS session for user');
|
|
173
|
+
const pdsUrl = `${session.pds_endpoint}/xrpc/com.atproto.repo.putRecord`;
|
|
174
|
+
const pdsBody = {
|
|
175
|
+
repo: viewer.did,
|
|
176
|
+
collection: input.collection,
|
|
177
|
+
rkey: input.rkey,
|
|
178
|
+
record: input.record,
|
|
179
|
+
};
|
|
180
|
+
const pdsRes = await proxyToPds(oauthConfig, session, 'POST', pdsUrl, pdsBody);
|
|
181
|
+
if (!pdsRes.ok)
|
|
182
|
+
throw new ProxyError(pdsRes.status, String(pdsRes.body.error || 'PDS write failed'));
|
|
183
|
+
try {
|
|
184
|
+
await insertRecord(input.collection, String(pdsRes.body.uri), String(pdsRes.body.cid), viewer.did, input.record);
|
|
185
|
+
await runLabelRules({
|
|
186
|
+
uri: String(pdsRes.body.uri),
|
|
187
|
+
cid: String(pdsRes.body.cid),
|
|
188
|
+
did: viewer.did,
|
|
189
|
+
collection: input.collection,
|
|
190
|
+
value: input.record,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
emit('pds-proxy', 'local_index_error', { op: 'putRecord', error: err instanceof Error ? err.message : String(err) });
|
|
195
|
+
}
|
|
196
|
+
return pdsRes.body;
|
|
197
|
+
}
|
|
198
|
+
/** Map dev.hatk.applyWrites#* types to com.atproto.repo.applyWrites#* for PDS. */
|
|
199
|
+
function toPdsWriteType($type) {
|
|
200
|
+
return $type.replace('dev.hatk.applyWrites#', 'com.atproto.repo.applyWrites#');
|
|
201
|
+
}
|
|
202
|
+
function isCreateOrUpdate($type) {
|
|
203
|
+
const mapped = toPdsWriteType($type);
|
|
204
|
+
return mapped === 'com.atproto.repo.applyWrites#create' || mapped === 'com.atproto.repo.applyWrites#update';
|
|
205
|
+
}
|
|
206
|
+
export async function pdsApplyWrites(oauthConfig, viewer, input) {
|
|
207
|
+
// Validate all create/update records before sending
|
|
208
|
+
for (const write of input.writes) {
|
|
209
|
+
if (isCreateOrUpdate(write.$type) && write.value) {
|
|
210
|
+
const validationError = validateRecord(getLexiconArray(), write.collection, write.value);
|
|
211
|
+
if (validationError) {
|
|
212
|
+
throw new ProxyError(400, `InvalidRecord: ${validationError.path ? validationError.path + ': ' : ''}${validationError.message}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const session = await getSession(viewer.did);
|
|
217
|
+
if (!session)
|
|
218
|
+
throw new ProxyError(401, 'No PDS session for user');
|
|
219
|
+
const pdsUrl = `${session.pds_endpoint}/xrpc/com.atproto.repo.applyWrites`;
|
|
220
|
+
const pdsBody = {
|
|
221
|
+
repo: viewer.did,
|
|
222
|
+
writes: input.writes.map((w) => ({ ...w, $type: toPdsWriteType(w.$type) })),
|
|
223
|
+
};
|
|
224
|
+
const pdsRes = await proxyToPds(oauthConfig, session, 'POST', pdsUrl, pdsBody);
|
|
225
|
+
if (!pdsRes.ok)
|
|
226
|
+
throw new ProxyError(pdsRes.status, String(pdsRes.body.error || 'PDS applyWrites failed'));
|
|
227
|
+
// Index results locally
|
|
228
|
+
const results = pdsRes.body.results ?? [];
|
|
229
|
+
for (let i = 0; i < input.writes.length; i++) {
|
|
230
|
+
const write = input.writes[i];
|
|
231
|
+
const result = results[i];
|
|
232
|
+
try {
|
|
233
|
+
const mapped = toPdsWriteType(write.$type);
|
|
234
|
+
if (mapped === 'com.atproto.repo.applyWrites#create' && result?.uri && result?.cid && write.value) {
|
|
235
|
+
await insertRecord(write.collection, result.uri, result.cid, viewer.did, write.value);
|
|
236
|
+
await runLabelRules({
|
|
237
|
+
uri: result.uri,
|
|
238
|
+
cid: result.cid,
|
|
239
|
+
did: viewer.did,
|
|
240
|
+
collection: write.collection,
|
|
241
|
+
value: write.value,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
else if (mapped === 'com.atproto.repo.applyWrites#update' && result?.uri && result?.cid && write.value) {
|
|
245
|
+
await insertRecord(write.collection, result.uri, result.cid, viewer.did, write.value);
|
|
246
|
+
await runLabelRules({
|
|
247
|
+
uri: result.uri,
|
|
248
|
+
cid: result.cid,
|
|
249
|
+
did: viewer.did,
|
|
250
|
+
collection: write.collection,
|
|
251
|
+
value: write.value,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
else if (mapped === 'com.atproto.repo.applyWrites#delete' && write.rkey) {
|
|
255
|
+
const uri = `at://${viewer.did}/${write.collection}/${write.rkey}`;
|
|
256
|
+
await dbDeleteRecord(write.collection, uri);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
catch (err) {
|
|
260
|
+
emit('pds-proxy', 'local_index_error', {
|
|
261
|
+
op: 'applyWrites',
|
|
262
|
+
error: err instanceof Error ? err.message : String(err),
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return pdsRes.body;
|
|
267
|
+
}
|
|
268
|
+
export async function pdsUploadBlob(oauthConfig, viewer, body, contentType) {
|
|
269
|
+
const session = await getSession(viewer.did);
|
|
270
|
+
if (!session)
|
|
271
|
+
throw new ProxyError(401, 'No PDS session for user');
|
|
272
|
+
const pdsUrl = `${session.pds_endpoint}/xrpc/com.atproto.repo.uploadBlob`;
|
|
273
|
+
const pdsRes = await proxyToPdsRaw(oauthConfig, session, pdsUrl, body, contentType);
|
|
274
|
+
if (!pdsRes.ok)
|
|
275
|
+
throw new ProxyError(pdsRes.status, String(pdsRes.body.error || 'PDS upload failed'));
|
|
276
|
+
return pdsRes.body;
|
|
277
|
+
}
|
package/dist/push.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface ApnsConfig {
|
|
2
|
+
keyFile: string;
|
|
3
|
+
keyId: string;
|
|
4
|
+
teamId: string;
|
|
5
|
+
bundleId: string;
|
|
6
|
+
production?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface PushConfig {
|
|
9
|
+
apns: ApnsConfig;
|
|
10
|
+
}
|
|
11
|
+
export interface PushPayload {
|
|
12
|
+
did: string;
|
|
13
|
+
title: string;
|
|
14
|
+
body: string;
|
|
15
|
+
data?: Record<string, string>;
|
|
16
|
+
collapseId?: string;
|
|
17
|
+
badge?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface PushInterface {
|
|
20
|
+
send: (payload: PushPayload) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/** Initialize push with config. Must be called before send(). */
|
|
23
|
+
export declare function initPush(config: PushConfig, configDir: string): void;
|
|
24
|
+
/** Check if push is configured and available. */
|
|
25
|
+
export declare function isPushEnabled(): boolean;
|
|
26
|
+
/** Build the push interface injected into hook contexts. */
|
|
27
|
+
export declare function buildPushInterface(): PushInterface;
|
|
28
|
+
/** Register a push token for a DID. Upserts on conflict. */
|
|
29
|
+
export declare function registerToken(did: string, token: string, platform: string): Promise<void>;
|
|
30
|
+
/** Remove a push token. */
|
|
31
|
+
export declare function removeToken(token: string): Promise<void>;
|
|
32
|
+
/** Unregister a specific token for a DID. */
|
|
33
|
+
export declare function unregisterToken(did: string, token: string): Promise<void>;
|
|
34
|
+
//# sourceMappingURL=push.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push.d.ts","sourceRoot":"","sources":["../src/push.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,UAAU,CAAA;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC9C;AAOD,iEAAiE;AACjE,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,GAAG,IAAI,CASpE;AAED,iDAAiD;AACjD,wBAAgB,aAAa,IAAI,OAAO,CAEvC;AAED,4DAA4D;AAC5D,wBAAgB,kBAAkB,IAAI,aAAa,CAElD;AAoJD,4DAA4D;AAC5D,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAO/F;AAED,2BAA2B;AAC3B,wBAAsB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9D;AAED,6CAA6C;AAC7C,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/E"}
|