easy-ping 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -4
- package/dist/{adapter-D3lTy-4E.d.cts → adapter-BJ8Iejvs.d.cts} +1 -1
- package/dist/{adapter-D3lTy-4E.d.ts → adapter-BJ8Iejvs.d.ts} +1 -1
- package/dist/adapters/drizzle.d.cts +1 -1
- package/dist/adapters/drizzle.d.ts +1 -1
- package/dist/adapters/mongodb.d.cts +2 -2
- package/dist/adapters/mongodb.d.ts +2 -2
- package/dist/adapters/mysql.d.cts +2 -2
- package/dist/adapters/mysql.d.ts +2 -2
- package/dist/adapters/postgres.d.cts +2 -2
- package/dist/adapters/postgres.d.ts +2 -2
- package/dist/adapters/sqlite.d.cts +2 -2
- package/dist/adapters/sqlite.d.ts +2 -2
- package/dist/chunk-R3BE67L7.js +13 -0
- package/dist/chunk-R3BE67L7.js.map +1 -0
- package/dist/chunk-SSHVZHSJ.js +100 -0
- package/dist/chunk-SSHVZHSJ.js.map +1 -0
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -11
- package/dist/index.js.map +1 -1
- package/dist/{plugin-Cg9usuUi.d.ts → plugin-CSLKKgXT.d.ts} +1 -1
- package/dist/{plugin-CHd2ZiGC.d.cts → plugin-Wwp1nvo0.d.cts} +1 -1
- package/dist/plugins/digests.d.cts +2 -2
- package/dist/plugins/digests.d.ts +2 -2
- package/dist/plugins/preferences-client.d.cts +2 -2
- package/dist/plugins/preferences-client.d.ts +2 -2
- package/dist/plugins/preferences-react.d.cts +1 -1
- package/dist/plugins/preferences-react.d.ts +1 -1
- package/dist/plugins/preferences.cjs +1 -1
- package/dist/plugins/preferences.cjs.map +1 -1
- package/dist/plugins/preferences.d.cts +2 -2
- package/dist/plugins/preferences.d.ts +2 -2
- package/dist/plugins/preferences.js +1 -1
- package/dist/plugins/preferences.js.map +1 -1
- package/dist/plugins/push.d.cts +2 -2
- package/dist/plugins/push.d.ts +2 -2
- package/dist/plugins/telegram.cjs +379 -0
- package/dist/plugins/telegram.cjs.map +1 -0
- package/dist/plugins/telegram.d.cts +153 -0
- package/dist/plugins/telegram.d.ts +153 -0
- package/dist/plugins/telegram.js +313 -0
- package/dist/plugins/telegram.js.map +1 -0
- package/dist/providers/telegram.cjs +103 -0
- package/dist/providers/telegram.cjs.map +1 -0
- package/dist/providers/telegram.d.cts +83 -0
- package/dist/providers/telegram.d.ts +83 -0
- package/dist/providers/telegram.js +3 -0
- package/dist/providers/telegram.js.map +1 -0
- package/dist/providers/web-push.d.cts +2 -2
- package/dist/providers/web-push.d.ts +2 -2
- package/dist/schema.d.cts +2 -2
- package/dist/schema.d.ts +2 -2
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/package.json +13 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugins/preferences/index.ts"],"names":[],"mappings":";;;;;AAWO,IAAM,mBAAA,GAAsB;AAEnC,IAAM,KAAA,GAAQ,yBAAA;AAEd,IAAM,WAA+B,CAAC,OAAA,EAAS,OAAA,EAAS,MAAA,EAAQ,OAAO,OAAO,CAAA;AAC9E,IAAM,WAAA,GAAoC,CAAC,SAAA,EAAW,OAAA,EAAS,UAAU,KAAK,CAAA;AAE9E,IAAM,SAAA,GAAY,CAAC,KAAA,KAAqC,QAAA,CAAS,SAAS,KAAgB,CAAA;AAC1F,IAAM,WAAA,GAAc,CAAC,KAAA,KACnB,WAAA,CAAY,SAAS,KAAkB,CAAA;AAoBzC,IAAM,GAAA,GAAM,CAAC,MAAA,EAAgB,IAAA,EAAc,OAAA,KAAoB,GAAG,MAAM,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,EAAI,OAAO,CAAA,CAAA;AAIpF,SAAS,sBACd,MAAA,EACA,MAAA,EACA,IAAA,EACA,OAAA,EACA,aAAa,yBAAA,EACI;AACjB,EAAA,OAAO,UAAU,MAA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CAAA;AAAA,UAClE;AAEA,UAAA,MAAM,IAAA,CAAK;AAAA,YACT,QAAQ,MAAA,CAAO,GAAA;AAAA,YACf,IAAA;AAAA,YACA,OAAA;AAAA,YACA,OAAA,EAAS,KAAA;AAAA,YACT,SAAA,EAAW;AAAA,WACZ,CAAA;AAGD,UAAA,OAAO,QAAA,CAAS,IAAA,CAAK,EAAE,EAAA,EAAI,IAAA,EAAM,cAAc,EAAE,IAAA,EAAM,OAAA,EAAQ,EAAG,CAAA;AAAA,QACpE;AAAA;AACF;AACF,GACF;AACF","file":"preferences.js","sourcesContent":["import { readJsonBody } from \"../../core/handler\";\nimport type {\n ChannelDecision,\n EasyPingPlugin,\n PluginInitContext,\n PrepareContext,\n ResolveChannelsContext,\n} from \"../../core/plugin\";\nimport { DEFAULT_TOKEN_TTL_SECONDS, expiresIn, signToken } from \"../../core/tokens\";\nimport type { Channel, Frequency } from \"../../core/types\";\n\nexport const UNSUBSCRIBE_PURPOSE = \"unsubscribe\";\n\nconst TABLE = \"notification_preference\";\n\nconst CHANNELS: readonly Channel[] = [\"inApp\", \"email\", \"push\", \"sms\", \"slack\"];\nconst FREQUENCIES: readonly Frequency[] = [\"instant\", \"daily\", \"weekly\", \"off\"];\n\nconst isChannel = (value: unknown): value is Channel => CHANNELS.includes(value as Channel);\nconst isFrequency = (value: unknown): value is Frequency =>\n FREQUENCIES.includes(value as Frequency);\n\nexport type PreferenceRow = {\n userId: string;\n type: string;\n channel: Channel;\n enabled: boolean;\n frequency: Frequency;\n /** Last explicit change. An unsubscribe token issued before it is refused. */\n updatedAt?: Date;\n};\n\nexport type PreferencesOptions = {\n /** What an absent row means. Flip to false for opt-in-only notifications. */\n defaultEnabled?: boolean;\n /** Types that ignore preferences entirely: receipts, security alerts. */\n alwaysSend?: readonly string[];\n unsubscribeTtlSeconds?: number;\n};\n\nconst key = (userId: string, type: string, channel: string) => `${userId} ${type} ${channel}`;\n\ntype PreparedPreferences = Map<string, PreferenceRow>;\n\nexport function buildUnsubscribeToken(\n secret: string,\n userId: string,\n type: string,\n channel: Channel,\n ttlSeconds = DEFAULT_TOKEN_TTL_SECONDS,\n): Promise<string> {\n return signToken(secret, {\n uid: userId,\n purpose: UNSUBSCRIBE_PURPOSE,\n exp: expiresIn(ttlSeconds),\n data: { type, channel },\n });\n}\n\nexport function preferences(options: PreferencesOptions = {}): EasyPingPlugin<\"preferences\"> {\n const defaultEnabled = options.defaultEnabled ?? true;\n const alwaysSend = new Set(options.alwaysSend ?? []);\n\n // Assigned by init(), which easyPing() runs before any hook or route.\n let ctx: PluginInitContext;\n\n const isAllowed = (userId: string, type: string, channel: Channel, rows: PreparedPreferences) => {\n const row = rows.get(key(userId, type, channel));\n return row ? row.enabled && row.frequency !== \"off\" : defaultEnabled;\n };\n\n const load = (userIds: readonly string[]) =>\n ctx.store.find<PreferenceRow>(TABLE, { userId: { in: [...userIds] } });\n\n const save = (row: PreferenceRow) =>\n ctx.store.upsert(TABLE, [{ ...row, updatedAt: new Date() }], {\n onConflict: [\"userId\", \"type\", \"channel\"],\n });\n\n return {\n id: \"preferences\",\n\n init: (context) => {\n ctx = context;\n },\n\n schema: {\n notificationPreference: {\n tableName: TABLE,\n fields: {\n userId: { type: \"string\", required: true },\n type: { type: \"string\", required: true },\n channel: { type: \"string\", required: true },\n enabled: { type: \"boolean\", required: true, default: true },\n frequency: { type: \"string\", required: true, default: \"instant\" },\n updatedAt: { type: \"date\", required: true, defaultNow: true },\n },\n primaryKey: [\"userId\", \"type\", \"channel\"],\n },\n },\n\n hooks: {\n /** One query per send; doing this per recipient would be an N+1. */\n prepare: async (context: PrepareContext): Promise<PreparedPreferences> => {\n if (alwaysSend.has(context.type)) return new Map();\n\n const rows = await load(context.recipients.map((recipient) => recipient.userId));\n return new Map(rows.map((row) => [key(row.userId, row.type, row.channel), row]));\n },\n\n resolveChannels: (context: ResolveChannelsContext) => {\n if (alwaysSend.has(context.type)) return context.decisions;\n\n // prepare() failed: fail closed rather than assume everyone opted in.\n if (!(context.prepared instanceof Map)) {\n return context.decisions.map((decision) => ({ ...decision, action: \"skip\" as const }));\n }\n\n const rows = context.prepared as PreparedPreferences;\n\n return context.decisions.map<ChannelDecision>((decision) =>\n isAllowed(context.recipient.userId, context.type, decision.channel, rows)\n ? decision\n : { ...decision, action: \"skip\" },\n );\n },\n },\n\n routes: [\n {\n path: \"/preferences\",\n method: \"GET\",\n scope: { type: \"user\" },\n handler: async ({ userId }) => {\n const rows = await load([userId ?? \"\"]);\n return Response.json({ preferences: rows, defaultEnabled });\n },\n },\n\n {\n path: \"/preferences\",\n method: \"POST\",\n scope: { type: \"user\" },\n handler: async ({ request, userId }) => {\n const parsed = await readJsonBody(request);\n if (\"error\" in parsed) return parsed.error;\n const body = parsed.body;\n\n const bad = (error: string) => Response.json({ error }, { status: 400 });\n\n // Only configured types, or every invented string becomes a row.\n if (typeof body.type !== \"string\" || !ctx.notificationTypes.includes(body.type)) {\n return bad(\"type must be a configured notification type\");\n }\n if (!isChannel(body.channel)) return bad(`channel must be one of ${CHANNELS.join(\", \")}`);\n if (body.enabled !== undefined && typeof body.enabled !== \"boolean\") {\n return bad(\"enabled must be a boolean\");\n }\n if (body.frequency !== undefined && !isFrequency(body.frequency)) {\n return bad(`frequency must be one of ${FREQUENCIES.join(\", \")}`);\n }\n\n // userId comes from the resolved session, never from the body.\n await save({\n userId: userId ?? \"\",\n type: body.type,\n channel: body.channel,\n enabled: body.enabled ?? true,\n frequency: body.frequency ?? \"instant\",\n });\n\n return Response.json({ ok: true });\n },\n },\n\n {\n // No session in a mail client; the link carries the proof.\n path: \"/unsubscribe\",\n method: \"POST\",\n scope: { type: \"signed\", purpose: UNSUBSCRIBE_PURPOSE },\n handler: async ({ claims }) => {\n const type = claims?.data?.type;\n const channel = claims?.data?.channel;\n if (!claims || !type || !isChannel(channel)) {\n return Response.json({ error: \"invalid token\" }, { status: 400 });\n }\n\n const [current] = await ctx.store.find<PreferenceRow>(\n TABLE,\n { userId: claims.uid, type, channel },\n { limit: 1 },\n );\n\n // Already off: a second click, or a mail client retrying, is fine.\n if (current && !current.enabled && current.frequency === \"off\") {\n return Response.json({ ok: true, unsubscribed: { type, channel } });\n }\n\n // Changed after the email went out: an old link must not undo a newer decision.\n if (\n current?.updatedAt &&\n claims.iat !== undefined &&\n current.updatedAt.getTime() > (claims.iat + 1) * 1000\n ) {\n return Response.json({ error: \"invalid token\" }, { status: 400 });\n }\n\n await save({\n userId: claims.uid,\n type,\n channel,\n enabled: false,\n frequency: \"off\",\n });\n\n // One-click (Gmail, Yahoo) requires the POST itself to unsubscribe.\n return Response.json({ ok: true, unsubscribed: { type, channel } });\n },\n },\n ],\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/plugins/preferences/index.ts"],"names":[],"mappings":";;;;;AAWO,IAAM,mBAAA,GAAsB;AAEnC,IAAM,KAAA,GAAQ,yBAAA;AAEd,IAAM,WAA+B,CAAC,OAAA,EAAS,SAAS,MAAA,EAAQ,UAAA,EAAY,OAAO,OAAO,CAAA;AAC1F,IAAM,WAAA,GAAoC,CAAC,SAAA,EAAW,OAAA,EAAS,UAAU,KAAK,CAAA;AAE9E,IAAM,SAAA,GAAY,CAAC,KAAA,KAAqC,QAAA,CAAS,SAAS,KAAgB,CAAA;AAC1F,IAAM,WAAA,GAAc,CAAC,KAAA,KACnB,WAAA,CAAY,SAAS,KAAkB,CAAA;AAoBzC,IAAM,GAAA,GAAM,CAAC,MAAA,EAAgB,IAAA,EAAc,OAAA,KAAoB,GAAG,MAAM,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,EAAI,OAAO,CAAA,CAAA;AAIpF,SAAS,sBACd,MAAA,EACA,MAAA,EACA,IAAA,EACA,OAAA,EACA,aAAa,yBAAA,EACI;AACjB,EAAA,OAAO,UAAU,MAAA,EAAQ;AAAA,IACvB,GAAA,EAAK,MAAA;AAAA,IACL,OAAA,EAAS,mBAAA;AAAA,IACT,GAAA,EAAK,UAAU,UAAU,CAAA;AAAA,IACzB,IAA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A,OAAA,EAAS,KAAA;AAAA,YACT,SAAA,EAAW;AAAA,WACZ,CAAA;AAGD,UAAA,OAAO,QAAA,CAAS,IAAA,CAAK,EAAE,EAAA,EAAI,IAAA,EAAM,cAAc,EAAE,IAAA,EAAM,OAAA,EAAQ,EAAG,CAAA;AAAA,QACpE;AAAA;AACF;AACF,GACF;AACF","file":"preferences.js","sourcesContent":["import { readJsonBody } from \"../../core/handler\";\nimport type {\n ChannelDecision,\n EasyPingPlugin,\n PluginInitContext,\n PrepareContext,\n ResolveChannelsContext,\n} from \"../../core/plugin\";\nimport { DEFAULT_TOKEN_TTL_SECONDS, expiresIn, signToken } from \"../../core/tokens\";\nimport type { Channel, Frequency } from \"../../core/types\";\n\nexport const UNSUBSCRIBE_PURPOSE = \"unsubscribe\";\n\nconst TABLE = \"notification_preference\";\n\nconst CHANNELS: readonly Channel[] = [\"inApp\", \"email\", \"push\", \"telegram\", \"sms\", \"slack\"];\nconst FREQUENCIES: readonly Frequency[] = [\"instant\", \"daily\", \"weekly\", \"off\"];\n\nconst isChannel = (value: unknown): value is Channel => CHANNELS.includes(value as Channel);\nconst isFrequency = (value: unknown): value is Frequency =>\n FREQUENCIES.includes(value as Frequency);\n\nexport type PreferenceRow = {\n userId: string;\n type: string;\n channel: Channel;\n enabled: boolean;\n frequency: Frequency;\n /** Last explicit change. An unsubscribe token issued before it is refused. */\n updatedAt?: Date;\n};\n\nexport type PreferencesOptions = {\n /** What an absent row means. Flip to false for opt-in-only notifications. */\n defaultEnabled?: boolean;\n /** Types that ignore preferences entirely: receipts, security alerts. */\n alwaysSend?: readonly string[];\n unsubscribeTtlSeconds?: number;\n};\n\nconst key = (userId: string, type: string, channel: string) => `${userId} ${type} ${channel}`;\n\ntype PreparedPreferences = Map<string, PreferenceRow>;\n\nexport function buildUnsubscribeToken(\n secret: string,\n userId: string,\n type: string,\n channel: Channel,\n ttlSeconds = DEFAULT_TOKEN_TTL_SECONDS,\n): Promise<string> {\n return signToken(secret, {\n uid: userId,\n purpose: UNSUBSCRIBE_PURPOSE,\n exp: expiresIn(ttlSeconds),\n data: { type, channel },\n });\n}\n\nexport function preferences(options: PreferencesOptions = {}): EasyPingPlugin<\"preferences\"> {\n const defaultEnabled = options.defaultEnabled ?? true;\n const alwaysSend = new Set(options.alwaysSend ?? []);\n\n // Assigned by init(), which easyPing() runs before any hook or route.\n let ctx: PluginInitContext;\n\n const isAllowed = (userId: string, type: string, channel: Channel, rows: PreparedPreferences) => {\n const row = rows.get(key(userId, type, channel));\n return row ? row.enabled && row.frequency !== \"off\" : defaultEnabled;\n };\n\n const load = (userIds: readonly string[]) =>\n ctx.store.find<PreferenceRow>(TABLE, { userId: { in: [...userIds] } });\n\n const save = (row: PreferenceRow) =>\n ctx.store.upsert(TABLE, [{ ...row, updatedAt: new Date() }], {\n onConflict: [\"userId\", \"type\", \"channel\"],\n });\n\n return {\n id: \"preferences\",\n\n init: (context) => {\n ctx = context;\n },\n\n schema: {\n notificationPreference: {\n tableName: TABLE,\n fields: {\n userId: { type: \"string\", required: true },\n type: { type: \"string\", required: true },\n channel: { type: \"string\", required: true },\n enabled: { type: \"boolean\", required: true, default: true },\n frequency: { type: \"string\", required: true, default: \"instant\" },\n updatedAt: { type: \"date\", required: true, defaultNow: true },\n },\n primaryKey: [\"userId\", \"type\", \"channel\"],\n },\n },\n\n hooks: {\n /** One query per send; doing this per recipient would be an N+1. */\n prepare: async (context: PrepareContext): Promise<PreparedPreferences> => {\n if (alwaysSend.has(context.type)) return new Map();\n\n const rows = await load(context.recipients.map((recipient) => recipient.userId));\n return new Map(rows.map((row) => [key(row.userId, row.type, row.channel), row]));\n },\n\n resolveChannels: (context: ResolveChannelsContext) => {\n if (alwaysSend.has(context.type)) return context.decisions;\n\n // prepare() failed: fail closed rather than assume everyone opted in.\n if (!(context.prepared instanceof Map)) {\n return context.decisions.map((decision) => ({ ...decision, action: \"skip\" as const }));\n }\n\n const rows = context.prepared as PreparedPreferences;\n\n return context.decisions.map<ChannelDecision>((decision) =>\n isAllowed(context.recipient.userId, context.type, decision.channel, rows)\n ? decision\n : { ...decision, action: \"skip\" },\n );\n },\n },\n\n routes: [\n {\n path: \"/preferences\",\n method: \"GET\",\n scope: { type: \"user\" },\n handler: async ({ userId }) => {\n const rows = await load([userId ?? \"\"]);\n return Response.json({ preferences: rows, defaultEnabled });\n },\n },\n\n {\n path: \"/preferences\",\n method: \"POST\",\n scope: { type: \"user\" },\n handler: async ({ request, userId }) => {\n const parsed = await readJsonBody(request);\n if (\"error\" in parsed) return parsed.error;\n const body = parsed.body;\n\n const bad = (error: string) => Response.json({ error }, { status: 400 });\n\n // Only configured types, or every invented string becomes a row.\n if (typeof body.type !== \"string\" || !ctx.notificationTypes.includes(body.type)) {\n return bad(\"type must be a configured notification type\");\n }\n if (!isChannel(body.channel)) return bad(`channel must be one of ${CHANNELS.join(\", \")}`);\n if (body.enabled !== undefined && typeof body.enabled !== \"boolean\") {\n return bad(\"enabled must be a boolean\");\n }\n if (body.frequency !== undefined && !isFrequency(body.frequency)) {\n return bad(`frequency must be one of ${FREQUENCIES.join(\", \")}`);\n }\n\n // userId comes from the resolved session, never from the body.\n await save({\n userId: userId ?? \"\",\n type: body.type,\n channel: body.channel,\n enabled: body.enabled ?? true,\n frequency: body.frequency ?? \"instant\",\n });\n\n return Response.json({ ok: true });\n },\n },\n\n {\n // No session in a mail client; the link carries the proof.\n path: \"/unsubscribe\",\n method: \"POST\",\n scope: { type: \"signed\", purpose: UNSUBSCRIBE_PURPOSE },\n handler: async ({ claims }) => {\n const type = claims?.data?.type;\n const channel = claims?.data?.channel;\n if (!claims || !type || !isChannel(channel)) {\n return Response.json({ error: \"invalid token\" }, { status: 400 });\n }\n\n const [current] = await ctx.store.find<PreferenceRow>(\n TABLE,\n { userId: claims.uid, type, channel },\n { limit: 1 },\n );\n\n // Already off: a second click, or a mail client retrying, is fine.\n if (current && !current.enabled && current.frequency === \"off\") {\n return Response.json({ ok: true, unsubscribed: { type, channel } });\n }\n\n // Changed after the email went out: an old link must not undo a newer decision.\n if (\n current?.updatedAt &&\n claims.iat !== undefined &&\n current.updatedAt.getTime() > (claims.iat + 1) * 1000\n ) {\n return Response.json({ error: \"invalid token\" }, { status: 400 });\n }\n\n await save({\n userId: claims.uid,\n type,\n channel,\n enabled: false,\n frequency: \"off\",\n });\n\n // One-click (Gmail, Yahoo) requires the POST itself to unsubscribe.\n return Response.json({ ok: true, unsubscribed: { type, channel } });\n },\n },\n ],\n };\n}\n"]}
|
package/dist/plugins/push.d.cts
CHANGED
package/dist/plugins/push.d.ts
CHANGED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/core/base64url.ts
|
|
4
|
+
function encodeBase64Url(input) {
|
|
5
|
+
const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
|
|
6
|
+
let binary = "";
|
|
7
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
8
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/core/handler.ts
|
|
12
|
+
var MAX_BODY_BYTES = 64 * 1024;
|
|
13
|
+
var PRIVATE_HEADERS = {
|
|
14
|
+
"cache-control": "private, no-store",
|
|
15
|
+
"x-content-type-options": "nosniff"
|
|
16
|
+
};
|
|
17
|
+
var json = (body, status = 200, headers = {}) => new Response(JSON.stringify(body), {
|
|
18
|
+
status,
|
|
19
|
+
headers: { "content-type": "application/json", ...PRIVATE_HEADERS, ...headers }
|
|
20
|
+
});
|
|
21
|
+
new TextEncoder();
|
|
22
|
+
async function readJsonBody(request, maxBytes = MAX_BODY_BYTES) {
|
|
23
|
+
const tooLarge = () => ({ error: json({ error: `body exceeds ${maxBytes} bytes` }, 413) });
|
|
24
|
+
const declared = Number(request.headers.get("content-length"));
|
|
25
|
+
if (Number.isFinite(declared) && declared > maxBytes) return tooLarge();
|
|
26
|
+
if (!request.body) return { body: {} };
|
|
27
|
+
const reader = request.body.getReader();
|
|
28
|
+
const chunks = [];
|
|
29
|
+
let total = 0;
|
|
30
|
+
for (; ; ) {
|
|
31
|
+
const { done, value } = await reader.read();
|
|
32
|
+
if (done) break;
|
|
33
|
+
total += value.byteLength;
|
|
34
|
+
if (total > maxBytes) {
|
|
35
|
+
await reader.cancel();
|
|
36
|
+
return tooLarge();
|
|
37
|
+
}
|
|
38
|
+
chunks.push(value);
|
|
39
|
+
}
|
|
40
|
+
const bytes = new Uint8Array(total);
|
|
41
|
+
let offset = 0;
|
|
42
|
+
for (const chunk of chunks) {
|
|
43
|
+
bytes.set(chunk, offset);
|
|
44
|
+
offset += chunk.byteLength;
|
|
45
|
+
}
|
|
46
|
+
const text = new TextDecoder().decode(bytes);
|
|
47
|
+
if (text.trim() === "") return { body: {} };
|
|
48
|
+
try {
|
|
49
|
+
const parsed = JSON.parse(text);
|
|
50
|
+
return {
|
|
51
|
+
body: parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {}
|
|
52
|
+
};
|
|
53
|
+
} catch {
|
|
54
|
+
return { error: json({ error: "body must be a JSON object" }, 400) };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/core/html.ts
|
|
59
|
+
var ESCAPES = {
|
|
60
|
+
"&": "&",
|
|
61
|
+
"<": "<",
|
|
62
|
+
">": ">",
|
|
63
|
+
'"': """,
|
|
64
|
+
"'": "'"
|
|
65
|
+
};
|
|
66
|
+
var escapeHtml = (value) => String(value ?? "").replace(/[&<>"']/g, (char) => ESCAPES[char] ?? char);
|
|
67
|
+
|
|
68
|
+
// src/providers/telegram/index.ts
|
|
69
|
+
var telegramStartLink = (botUsername, code) => `https://t.me/${botUsername.replace(/^@/, "")}?start=${encodeURIComponent(code)}`;
|
|
70
|
+
|
|
71
|
+
// src/plugins/telegram/index.ts
|
|
72
|
+
var CHAT = "notification_telegram_chat";
|
|
73
|
+
var LINK = "notification_telegram_link";
|
|
74
|
+
var DEFAULT_MESSAGES = {
|
|
75
|
+
connected: "Connected. You will receive notifications here.",
|
|
76
|
+
disconnected: "Disconnected. You will not receive notifications here any more.",
|
|
77
|
+
expired: "That link has expired. Open the app and connect Telegram again.",
|
|
78
|
+
start: "Open the app and use \u201CConnect Telegram\u201D to link this chat."
|
|
79
|
+
};
|
|
80
|
+
var telegramSchema = {
|
|
81
|
+
telegramChat: {
|
|
82
|
+
tableName: CHAT,
|
|
83
|
+
fields: {
|
|
84
|
+
id: { type: "string", required: true },
|
|
85
|
+
userId: { type: "string", required: true },
|
|
86
|
+
chatId: { type: "string", required: true },
|
|
87
|
+
chatType: { type: "string", required: true },
|
|
88
|
+
username: { type: "string" },
|
|
89
|
+
title: { type: "string" },
|
|
90
|
+
linkedAt: { type: "date", required: true, defaultNow: true },
|
|
91
|
+
lastSeenAt: { type: "date", required: true, defaultNow: true }
|
|
92
|
+
},
|
|
93
|
+
primaryKey: ["id"],
|
|
94
|
+
indexes: [
|
|
95
|
+
{ on: ["userId"], name: "telegram_chat_user_idx" },
|
|
96
|
+
// A chat belongs to one account at a time; re-linking moves it.
|
|
97
|
+
{ on: ["chatId"], unique: true, name: "telegram_chat_chat_idx" }
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
telegramLink: {
|
|
101
|
+
tableName: LINK,
|
|
102
|
+
fields: {
|
|
103
|
+
code: { type: "string", required: true },
|
|
104
|
+
userId: { type: "string", required: true },
|
|
105
|
+
createdAt: { type: "date", required: true, defaultNow: true },
|
|
106
|
+
expiresAt: { type: "date", required: true }
|
|
107
|
+
},
|
|
108
|
+
primaryKey: ["code"],
|
|
109
|
+
indexes: [{ on: ["userId"], name: "telegram_link_user_idx" }]
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var randomCode = () => encodeBase64Url(crypto.getRandomValues(new Uint8Array(24)));
|
|
113
|
+
function isButtonUrlAllowed(value) {
|
|
114
|
+
let url;
|
|
115
|
+
try {
|
|
116
|
+
url = new URL(value);
|
|
117
|
+
} catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return url.protocol === "tg:";
|
|
121
|
+
const host = url.hostname.toLowerCase();
|
|
122
|
+
return !(host === "localhost" || /\.(localhost|local|internal|lan)$/.test(host) || /^\d+\.\d+\.\d+\.\d+$/.test(host) || host.startsWith("["));
|
|
123
|
+
}
|
|
124
|
+
var encoder2 = new TextEncoder();
|
|
125
|
+
async function timingSafeEqual(a, b) {
|
|
126
|
+
const [left, right] = await Promise.all(
|
|
127
|
+
[a, b].map(
|
|
128
|
+
async (value) => new Uint8Array(await crypto.subtle.digest("SHA-256", encoder2.encode(value)))
|
|
129
|
+
)
|
|
130
|
+
);
|
|
131
|
+
let diff = 0;
|
|
132
|
+
for (let i = 0; i < 32; i += 1) diff |= (left?.[i] ?? 0) ^ (right?.[i] ?? 0);
|
|
133
|
+
return diff === 0;
|
|
134
|
+
}
|
|
135
|
+
function telegram(options) {
|
|
136
|
+
let ctx;
|
|
137
|
+
const messages = { ...DEFAULT_MESSAGES, ...options.messages };
|
|
138
|
+
const ttlMs = (options.linkTtlMinutes ?? 10) * 6e4;
|
|
139
|
+
const maxChats = Math.max(1, options.maxChatsPerUser ?? 5);
|
|
140
|
+
const botUsername = options.botUsername.replace(/^@/, "");
|
|
141
|
+
const reply = async (chatId, text) => {
|
|
142
|
+
const result = await options.provider.send({ chatId, text: escapeHtml(text) });
|
|
143
|
+
if (!result.ok) ctx.logger.warn(`telegram reply to chat failed: ${result.error}`);
|
|
144
|
+
};
|
|
145
|
+
async function link(code, chat) {
|
|
146
|
+
const [pending] = await ctx.store.find(LINK, { code }, { limit: 1 });
|
|
147
|
+
const chatId = String(chat.id);
|
|
148
|
+
if (!pending || pending.expiresAt.getTime() < Date.now()) {
|
|
149
|
+
if (pending) await ctx.store.remove(LINK, { code });
|
|
150
|
+
await reply(chatId, messages.expired);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
await ctx.store.remove(LINK, { code });
|
|
154
|
+
const now = /* @__PURE__ */ new Date();
|
|
155
|
+
const details = {
|
|
156
|
+
chatType: chat.type,
|
|
157
|
+
username: chat.username ?? null,
|
|
158
|
+
title: chat.title ?? null,
|
|
159
|
+
lastSeenAt: now
|
|
160
|
+
};
|
|
161
|
+
const [existing] = await ctx.store.find(CHAT, { chatId }, { limit: 1 });
|
|
162
|
+
if (existing) {
|
|
163
|
+
await ctx.store.update(
|
|
164
|
+
CHAT,
|
|
165
|
+
{ id: existing.id },
|
|
166
|
+
{ userId: pending.userId, linkedAt: now, ...details }
|
|
167
|
+
);
|
|
168
|
+
} else {
|
|
169
|
+
const chats = await ctx.store.find(
|
|
170
|
+
CHAT,
|
|
171
|
+
{ userId: pending.userId },
|
|
172
|
+
{ orderBy: { field: "lastSeenAt", direction: "asc" } }
|
|
173
|
+
);
|
|
174
|
+
const excess = chats.length - maxChats + 1;
|
|
175
|
+
if (excess > 0) {
|
|
176
|
+
await ctx.store.remove(CHAT, { id: { in: chats.slice(0, excess).map((row) => row.id) } });
|
|
177
|
+
}
|
|
178
|
+
await ctx.store.insert(CHAT, [
|
|
179
|
+
{ id: crypto.randomUUID(), userId: pending.userId, chatId, linkedAt: now, ...details }
|
|
180
|
+
]);
|
|
181
|
+
}
|
|
182
|
+
await reply(chatId, messages.connected);
|
|
183
|
+
}
|
|
184
|
+
async function handleUpdate(update) {
|
|
185
|
+
const member = update.my_chat_member;
|
|
186
|
+
if (member && ["kicked", "left"].includes(member.new_chat_member.status)) {
|
|
187
|
+
await ctx.store.remove(CHAT, { chatId: String(member.chat.id) });
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const message = update.message;
|
|
191
|
+
if (!message?.text) return;
|
|
192
|
+
const chatId = String(message.chat.id);
|
|
193
|
+
const [command, argument] = message.text.trim().split(/\s+/, 2);
|
|
194
|
+
const name = command?.split("@")[0];
|
|
195
|
+
if (name === "/start") {
|
|
196
|
+
if (argument) await link(argument, message.chat);
|
|
197
|
+
else await reply(chatId, messages.start);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (name === "/stop") {
|
|
201
|
+
const removed = await ctx.store.remove(CHAT, { chatId });
|
|
202
|
+
if (removed > 0) await reply(chatId, messages.disconnected);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const routes = [
|
|
206
|
+
{
|
|
207
|
+
path: "/telegram/link",
|
|
208
|
+
method: "POST",
|
|
209
|
+
scope: { type: "user" },
|
|
210
|
+
handler: async ({ userId }) => {
|
|
211
|
+
const owner = userId ?? "";
|
|
212
|
+
const now = Date.now();
|
|
213
|
+
await ctx.store.remove(LINK, { userId: owner, expiresAt: { lt: new Date(now) } });
|
|
214
|
+
const code = randomCode();
|
|
215
|
+
const expiresAt = new Date(now + ttlMs);
|
|
216
|
+
await ctx.store.insert(LINK, [
|
|
217
|
+
{ code, userId: owner, createdAt: new Date(now), expiresAt }
|
|
218
|
+
]);
|
|
219
|
+
return Response.json({
|
|
220
|
+
url: telegramStartLink(botUsername, code),
|
|
221
|
+
code,
|
|
222
|
+
expiresAt: expiresAt.toISOString()
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
path: "/telegram/chats",
|
|
228
|
+
method: "GET",
|
|
229
|
+
scope: { type: "user" },
|
|
230
|
+
handler: async ({ userId }) => {
|
|
231
|
+
const chats = await ctx.store.find(
|
|
232
|
+
CHAT,
|
|
233
|
+
{ userId: userId ?? "" },
|
|
234
|
+
{ orderBy: { field: "linkedAt", direction: "desc" } }
|
|
235
|
+
);
|
|
236
|
+
return Response.json({
|
|
237
|
+
chats: chats.map((chat) => ({
|
|
238
|
+
chatId: chat.chatId,
|
|
239
|
+
chatType: chat.chatType,
|
|
240
|
+
username: chat.username,
|
|
241
|
+
title: chat.title,
|
|
242
|
+
linkedAt: chat.linkedAt.toISOString()
|
|
243
|
+
}))
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
path: "/telegram/unlink",
|
|
249
|
+
method: "POST",
|
|
250
|
+
scope: { type: "user" },
|
|
251
|
+
handler: async ({ request, userId }) => {
|
|
252
|
+
const parsed = await readJsonBody(request);
|
|
253
|
+
if ("error" in parsed) return parsed.error;
|
|
254
|
+
const { chatId } = parsed.body;
|
|
255
|
+
const owner = userId ?? "";
|
|
256
|
+
const removed = await ctx.store.remove(
|
|
257
|
+
CHAT,
|
|
258
|
+
typeof chatId === "string" && chatId !== "" ? { userId: owner, chatId } : { userId: owner }
|
|
259
|
+
);
|
|
260
|
+
return Response.json({ removed });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
];
|
|
264
|
+
if (options.webhookSecret) {
|
|
265
|
+
const secret = options.webhookSecret;
|
|
266
|
+
routes.push({
|
|
267
|
+
path: "/telegram/webhook",
|
|
268
|
+
method: "POST",
|
|
269
|
+
scope: {
|
|
270
|
+
type: "custom",
|
|
271
|
+
justification: "called by Telegram, authenticated by the X-Telegram-Bot-Api-Secret-Token header set through setWebhook"
|
|
272
|
+
},
|
|
273
|
+
handler: async ({ request }) => {
|
|
274
|
+
const presented = request.headers.get("x-telegram-bot-api-secret-token") ?? "";
|
|
275
|
+
if (!await timingSafeEqual(presented, secret)) return new Response(null, { status: 401 });
|
|
276
|
+
const parsed = await readJsonBody(request);
|
|
277
|
+
if ("error" in parsed) return parsed.error;
|
|
278
|
+
try {
|
|
279
|
+
await handleUpdate(parsed.body);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
ctx.logger.error("telegram update failed", { error });
|
|
282
|
+
}
|
|
283
|
+
return Response.json({ ok: true });
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
id: "telegram",
|
|
289
|
+
channels: ["telegram"],
|
|
290
|
+
schema: telegramSchema,
|
|
291
|
+
init: (context) => {
|
|
292
|
+
ctx = context;
|
|
293
|
+
},
|
|
294
|
+
hooks: {
|
|
295
|
+
deliver: async (context) => {
|
|
296
|
+
const chats = await ctx.store.find(CHAT, {
|
|
297
|
+
userId: context.notification.userId
|
|
298
|
+
});
|
|
299
|
+
if (chats.length === 0) return { result: "skipped", reason: "no linked telegram chat" };
|
|
300
|
+
const rendered = options.render({
|
|
301
|
+
type: context.notification.type,
|
|
302
|
+
payload: context.notification.payload
|
|
303
|
+
});
|
|
304
|
+
let button = rendered.button;
|
|
305
|
+
if (button && !isButtonUrlAllowed(button.url)) {
|
|
306
|
+
ctx.logger.warn(
|
|
307
|
+
`telegram: dropped button "${button.url}"; Telegram accepts only public http(s) links`
|
|
308
|
+
);
|
|
309
|
+
button = void 0;
|
|
310
|
+
}
|
|
311
|
+
const results = await Promise.all(
|
|
312
|
+
chats.map(
|
|
313
|
+
(chat) => options.provider.send({
|
|
314
|
+
chatId: chat.chatId,
|
|
315
|
+
text: rendered.text,
|
|
316
|
+
buttons: button ? [button] : void 0,
|
|
317
|
+
signal: context.signal
|
|
318
|
+
})
|
|
319
|
+
)
|
|
320
|
+
);
|
|
321
|
+
const prune = [];
|
|
322
|
+
let delivered = 0;
|
|
323
|
+
let retryable = false;
|
|
324
|
+
let lastError = "";
|
|
325
|
+
results.forEach((result, index) => {
|
|
326
|
+
const chat = chats[index];
|
|
327
|
+
if (!chat) return;
|
|
328
|
+
if (result.ok) {
|
|
329
|
+
delivered += 1;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
lastError = result.error;
|
|
333
|
+
if (result.gone) prune.push(chat.id);
|
|
334
|
+
else if (result.retryable) retryable = true;
|
|
335
|
+
});
|
|
336
|
+
if (prune.length > 0) await ctx.store.remove(CHAT, { id: { in: prune } });
|
|
337
|
+
if (delivered > 0) return { result: "sent" };
|
|
338
|
+
if (prune.length === chats.length) {
|
|
339
|
+
return { result: "failed", error: "every linked chat is gone", retryable: false };
|
|
340
|
+
}
|
|
341
|
+
return { result: "failed", error: lastError || "telegram refused the message", retryable };
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
routes,
|
|
345
|
+
handleUpdate,
|
|
346
|
+
poll(pollOptions = {}) {
|
|
347
|
+
const logger = pollOptions.logger ?? ctx.logger;
|
|
348
|
+
const controller = new AbortController();
|
|
349
|
+
let offset;
|
|
350
|
+
void (async () => {
|
|
351
|
+
while (!controller.signal.aborted) {
|
|
352
|
+
try {
|
|
353
|
+
const updates = await options.provider.getUpdates({
|
|
354
|
+
offset,
|
|
355
|
+
signal: controller.signal
|
|
356
|
+
});
|
|
357
|
+
for (const update of updates) {
|
|
358
|
+
offset = update.update_id + 1;
|
|
359
|
+
await handleUpdate(update).catch(
|
|
360
|
+
(error) => logger.error("telegram update failed", { error })
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
} catch (error) {
|
|
364
|
+
if (controller.signal.aborted) return;
|
|
365
|
+
logger.error("telegram polling failed", { error });
|
|
366
|
+
await new Promise((resolve) => setTimeout(resolve, 3e3));
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
})();
|
|
370
|
+
return { stop: () => controller.abort() };
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
exports.isButtonUrlAllowed = isButtonUrlAllowed;
|
|
376
|
+
exports.telegram = telegram;
|
|
377
|
+
exports.telegramSchema = telegramSchema;
|
|
378
|
+
//# sourceMappingURL=telegram.cjs.map
|
|
379
|
+
//# sourceMappingURL=telegram.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/base64url.ts","../../src/core/handler.ts","../../src/core/html.ts","../../src/providers/telegram/index.ts","../../src/plugins/telegram/index.ts"],"names":["encoder"],"mappings":";;;AAEO,SAAS,gBAAgB,KAAA,EAAoC;AAClE,EAAA,MAAM,KAAA,GAAQ,OAAO,KAAA,KAAU,QAAA,GAAW,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,KAAK,CAAA,GAAI,KAAA;AAC5E,EAAA,IAAI,MA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AA,CAAY,MAAA,IAAU,GAAA,CAAI,MAAA;AACzC,MAAA,MAAM,UAAA,GAAa,IAAI,eAAA,EAAgB;AACvC,MAAA,IAAI,MAAA;AAEJ,MAAA,KAAA,CAAM,YAAY;AAChB,QAAA,OAAO,CAAC,UAAA,CAAW,MAAA,CAAO,OAAA,EAAS;AACjC,UAAA,IAAI;AACF,YAAA,MAAM,OAAA,GAAU,MAAM,OAAA,CAAQ,QAAA,CAAS,UAAA,CAAW;AAAA,cAChD,MAAA;AAAA,cACA,QAAQ,UAAA,CAAW;AAAA,aACpB,CAAA;AACD,YAAA,KAAA,MAAW,UAAU,OAAA,EAAS;AAC5B,cAAA,MAAA,GAAS,OAAO,SAAA,GAAY,CAAA;AAC5B,cAAA,MAAM,YAAA,CAAa,MAAM,CAAA,CAAE,KAAA;AAAA,gBAAM,CAAC,KAAA,KAChC,MAAA,CAAO,MAAM,wBAAA,EAA0B,EAAE,OAAO;AAAA,eAClD;AAAA,YACF;AAAA,UACF,SAAS,KAAA,EAAO;AACd,YAAA,IAAI,UAAA,CAAW,OAAO,OAAA,EAAS;AAC/B,YAAA,MAAA,CAAO,KAAA,CAAM,yBAAA,EAA2B,EAAE,KAAA,EAAO,CAAA;AACjD,YAAA,MAAM,IAAI,OAAA,CAAQ,CAAC,YAAY,UAAA,CAAW,OAAA,EAAS,GAAK,CAAC,CAAA;AAAA,UAC3D;AAAA,QACF;AAAA,MACF,CAAA,GAAG;AAEH,MAAA,OAAO,EAAE,IAAA,EAAM,MAAM,UAAA,CAAW,OAAM,EAAE;AAAA,IAC1C;AAAA,GACF;AACF","file":"telegram.cjs","sourcesContent":["// Web APIs, not Buffer: this runs on Workers and Vercel Edge.\n\nexport function encodeBase64Url(input: string | Uint8Array): string {\n const bytes = typeof input === \"string\" ? new TextEncoder().encode(input) : input;\n let binary = \"\";\n for (const byte of bytes) binary += String.fromCharCode(byte);\n return btoa(binary).replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=+$/, \"\");\n}\n\n// Uint8Array<ArrayBuffer>: bare Uint8Array widens and is not a BufferSource.\nexport function decodeBase64UrlBytes(value: string): Uint8Array<ArrayBuffer> | null {\n try {\n const binary = atob(value.replace(/-/g, \"+\").replace(/_/g, \"/\"));\n return Uint8Array.from(binary, (char) => char.charCodeAt(0));\n } catch {\n return null;\n }\n}\n\nexport function decodeBase64Url(value: string): string | null {\n const bytes = decodeBase64UrlBytes(value);\n return bytes ? new TextDecoder().decode(bytes) : null;\n}\n","import type { DatabaseAdapter } from \"./adapter\";\nimport type { SessionConfig } from \"./config\";\nimport type { Logger } from \"./errors\";\nimport type { Promisable, RouteDefinition } from \"./plugin\";\nimport type { Runner } from \"./runner\";\nimport { inboxChannel, type Signals } from \"./signals\";\nimport { verifyToken } from \"./tokens\";\n\nexport type EventsSettings = {\n heartbeatMs: number;\n probeIntervalMs: number;\n maxDurationMs: number;\n maxStreamsPerUser: number;\n maxStreams: number;\n};\n\n/** Unread chunks a stream may hold before it is closed on a consumer that stopped reading. */\nconst STREAM_HIGH_WATER_MARK = 256;\n\nexport type HandlerDeps = {\n adapter: DatabaseAdapter;\n session: SessionConfig;\n runner: Runner;\n logger: Logger;\n basePath: string;\n secret: string;\n signals: Signals;\n /** `false` unmounts GET /events. */\n events: EventsSettings | false;\n /** Fire-and-forget delivery pass after a request. Rate-limited by the instance. */\n sweepOnRequest?: (() => void) | undefined;\n cronSecret?: string | undefined;\n /** Guards plugin machine routes. The instance defaults it to cronSecret. */\n machineSecret?: string | undefined;\n cronMaxSweeps?: number | undefined;\n pluginRoutes?: readonly RouteDefinition[] | undefined;\n /** Returns a 429 to send, or null to proceed. Runs after onRequest, before routing. */\n rateLimit?: ((request: Request) => Response | null) | undefined;\n /** Origins other than the request's own host that may POST. `*.example.com` allowed. */\n trustedOrigins?: readonly string[] | undefined;\n /** Runs before routing. Returning a Response short-circuits; use it for rate limiting. */\n // biome-ignore lint/suspicious/noConfusingVoidType: a hook that returns nothing is the common case\n onRequest?: ((request: Request) => Promisable<Response | undefined | void>) | undefined;\n maxBodyBytes?: number | undefined;\n};\n\n/** Every JSON body is capped here. Nothing legitimate the routes accept is bigger. */\nexport const MAX_BODY_BYTES = 64 * 1024;\n\nconst MAX_READ_IDS = 200;\n\n// Per-user data: no shared caching, no sniffing.\nconst PRIVATE_HEADERS: Record<string, string> = {\n \"cache-control\": \"private, no-store\",\n \"x-content-type-options\": \"nosniff\",\n};\n\nconst json = (body: unknown, status = 200, headers: Record<string, string> = {}) =>\n new Response(JSON.stringify(body), {\n status,\n headers: { \"content-type\": \"application/json\", ...PRIVATE_HEADERS, ...headers },\n });\n\nconst empty = (status: number) => new Response(null, { status, headers: PRIVATE_HEADERS });\n\n/** Plugin responses get the same headers, without touching their body. */\nfunction harden(response: Response): Response {\n const headers = new Headers(response.headers);\n for (const [name, value] of Object.entries(PRIVATE_HEADERS)) {\n if (!headers.has(name)) headers.set(name, value);\n }\n return new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers,\n });\n}\n\nconst encoder = new TextEncoder();\n\n/** Constant time over SHA-256 digests, so neither content nor length leaks. Works on edge runtimes. */\nasync function timingSafeEqual(a: string, b: string): Promise<boolean> {\n const [left, right] = await Promise.all(\n [a, b].map(\n async (value) => new Uint8Array(await crypto.subtle.digest(\"SHA-256\", encoder.encode(value))),\n ),\n );\n let diff = 0;\n for (let i = 0; i < 32; i += 1) diff |= (left?.[i] ?? 0) ^ (right?.[i] ?? 0);\n return diff === 0;\n}\n\n/** Anchored at a segment boundary: `/foo/api/notifications/cron` is not a cron request. */\nfunction routePath(url: URL, basePath: string): string {\n const { pathname } = url;\n if (pathname === basePath) return \"/\";\n if (pathname.startsWith(`${basePath}/`)) return pathname.slice(basePath.length);\n return pathname;\n}\n\nexport type JsonBody = { body: Record<string, unknown> } | { error: Response };\n\n/** Reads a JSON object body with a streaming byte cap. Malformed JSON is 400; an empty body is `{}`. */\nexport async function readJsonBody(\n request: Request,\n maxBytes: number = MAX_BODY_BYTES,\n): Promise<JsonBody> {\n const tooLarge = () => ({ error: json({ error: `body exceeds ${maxBytes} bytes` }, 413) });\n\n const declared = Number(request.headers.get(\"content-length\"));\n if (Number.isFinite(declared) && declared > maxBytes) return tooLarge();\n if (!request.body) return { body: {} };\n\n const reader = request.body.getReader();\n const chunks: Uint8Array[] = [];\n let total = 0;\n\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n total += value.byteLength;\n if (total > maxBytes) {\n await reader.cancel();\n return tooLarge();\n }\n chunks.push(value);\n }\n\n const bytes = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n bytes.set(chunk, offset);\n offset += chunk.byteLength;\n }\n\n const text = new TextDecoder().decode(bytes);\n if (text.trim() === \"\") return { body: {} };\n\n try {\n const parsed: unknown = JSON.parse(text);\n return {\n body:\n parsed && typeof parsed === \"object\" && !Array.isArray(parsed)\n ? (parsed as Record<string, unknown>)\n : {},\n };\n } catch {\n return { error: json({ error: \"body must be a JSON object\" }, 400) };\n }\n}\n\nconst isJsonContentType = (request: Request) =>\n /^application\\/json\\s*(;|$)/i.test((request.headers.get(\"content-type\") ?? \"\").trim());\n\nfunction originAllowed(origin: string, url: URL, request: Request, trusted: readonly string[]) {\n let host: string;\n try {\n host = new URL(origin).host;\n } catch {\n return false;\n }\n\n if (host === url.host) return true;\n // Behind a proxy that rewrites Host, the public host arrives here instead.\n const forwarded = request.headers.get(\"x-forwarded-host\")?.split(\",\")[0]?.trim();\n if (forwarded && host === forwarded) return true;\n\n return trusted.some((entry) =>\n entry.startsWith(\"*.\") ? host.endsWith(entry.slice(1)) : entry === origin,\n );\n}\n\n/** CSRF: a cross-site form can neither send `application/json` without a preflight nor forge `Origin`. */\nfunction csrfCheck(request: Request, url: URL, trusted: readonly string[]): Response | null {\n if (!isJsonContentType(request)) {\n return json({ error: \"content-type must be application/json\" }, 415);\n }\n\n const origin = request.headers.get(\"origin\");\n if (origin === null) return null;\n if (origin === \"null\" || !originAllowed(origin, url, request, trusted)) return empty(403);\n\n return null;\n}\n\nexport function createHandler(deps: HandlerDeps) {\n const trustedOrigins = deps.trustedOrigins ?? [];\n const maxBodyBytes = deps.maxBodyBytes ?? MAX_BODY_BYTES;\n\n async function resolveUserId(request: Request): Promise<string | null | \"error\"> {\n try {\n return await deps.session.getUserId(request);\n } catch (error) {\n // A broken session lookup is a 500, never a 401. RFC 0002 §1.\n deps.logger.error(\"session.getUserId threw\", { error });\n return \"error\";\n }\n }\n\n async function bearerMatches(request: Request, secret: string | undefined): Promise<boolean> {\n if (!secret) return false;\n const header = request.headers.get(\"authorization\") ?? \"\";\n const presented = header.startsWith(\"Bearer \") ? header.slice(7) : header;\n return timingSafeEqual(presented, secret);\n }\n\n async function handleCron(request: Request): Promise<Response> {\n if (!deps.cronSecret) return empty(404);\n if (!(await bearerMatches(request, deps.cronSecret))) return empty(401);\n\n // Bounded so an overlapping scheduler does not hold a request open for the whole backlog.\n const result = await deps.runner.drain(\n deps.cronMaxSweeps === undefined ? {} : { maxSweeps: deps.cronMaxSweeps },\n );\n return json(result);\n }\n\n /** Plugin routes first; auth is enforced here so a plugin cannot forget it. */\n async function handlePluginRoute(\n route: RouteDefinition,\n request: Request,\n url: URL,\n ): Promise<Response> {\n if (route.scope.type === \"user\") {\n if (request.method === \"POST\") {\n const rejected = csrfCheck(request, url, trustedOrigins);\n if (rejected) return rejected;\n }\n\n const userId = await resolveUserId(request);\n if (userId === \"error\") return empty(500);\n if (userId === null) return empty(401);\n return harden(await route.handler({ request, userId, claims: null, params: {} }));\n }\n\n if (route.scope.type === \"machine\") {\n if (!deps.machineSecret) return empty(404);\n if (!(await bearerMatches(request, deps.machineSecret))) return empty(401);\n return harden(await route.handler({ request, userId: null, claims: null, params: {} }));\n }\n\n if (route.scope.type === \"signed\") {\n const token =\n url.searchParams.get(\"token\") ??\n (request.headers.get(\"authorization\") ?? \"\").replace(/^Bearer /, \"\");\n\n const claims = await verifyToken(deps.secret, token, route.scope.purpose);\n // One response for every failure mode. RFC 0002 §5.\n if (!claims) return empty(400);\n\n return harden(await route.handler({ request, userId: claims.uid, claims, params: {} }));\n }\n\n return harden(await route.handler({ request, userId: null, claims: null, params: {} }));\n }\n\n async function route(request: Request): Promise<Response> {\n const url = new URL(request.url);\n const path = routePath(url, deps.basePath);\n\n if (deps.onRequest) {\n const early = await deps.onRequest(request);\n if (early instanceof Response) return early;\n }\n\n if (deps.rateLimit) {\n const limited = deps.rateLimit(request);\n if (limited) return limited;\n }\n\n if (path === \"/cron\") {\n if (request.method !== \"POST\") return empty(405);\n return handleCron(request);\n }\n\n const pluginRoute = deps.pluginRoutes?.find(\n (candidate) => candidate.path === path && candidate.method === request.method,\n );\n if (pluginRoute) return handlePluginRoute(pluginRoute, request, url);\n\n if (request.method === \"POST\") {\n const rejected = csrfCheck(request, url, trustedOrigins);\n if (rejected) return rejected;\n }\n\n const userId = await resolveUserId(request);\n if (userId === \"error\") return empty(500);\n if (userId === null) return empty(401);\n\n if (request.method === \"GET\" && path === \"/events\") {\n if (!deps.events) return empty(404);\n // A session can open streams for free; without a cap one account can hold every socket.\n if ((openStreams.get(userId) ?? 0) >= deps.events.maxStreamsPerUser) {\n return json({ error: \"too many open event streams\" }, 429, { \"retry-after\": \"30\" });\n }\n if (totalStreams >= deps.events.maxStreams) {\n return json({ error: \"event streams unavailable\" }, 503, { \"retry-after\": \"30\" });\n }\n return events(request, userId, deps.events);\n }\n\n if (request.method === \"GET\" && path === \"/\") {\n const limitParam = Number(url.searchParams.get(\"limit\") ?? 20);\n const limit = Number.isFinite(limitParam) ? Math.min(Math.max(limitParam, 1), 100) : 20;\n const cursor = url.searchParams.get(\"cursor\");\n\n // unseenCount rides on the first page only: a cursor request is scrollback, not a poll.\n const [page, unseenCount] = await Promise.all([\n deps.adapter.listNotifications({\n userId,\n limit,\n ...(cursor ? { cursor } : {}),\n ...(url.searchParams.get(\"unreadOnly\") === \"true\" ? { unreadOnly: true } : {}),\n }),\n cursor ? Promise.resolve(null) : deps.adapter.countUnseen(userId),\n ]);\n\n return json(unseenCount === null ? page : { ...page, unseenCount });\n }\n\n if (request.method === \"GET\" && path === \"/count\") {\n return json({ unseen: await deps.adapter.countUnseen(userId) });\n }\n\n if (request.method === \"POST\" && path === \"/seen\") {\n const parsed = await readJsonBody(request, maxBodyBytes);\n if (\"error\" in parsed) return parsed.error;\n const before = typeof parsed.body.before === \"string\" ? new Date(parsed.body.before) : null;\n\n // +1ms: Postgres keeps microseconds, an ISO string milliseconds; without it markSeen misses the newest row.\n const cutoff =\n before && !Number.isNaN(before.getTime()) ? new Date(before.getTime() + 1) : new Date();\n\n await deps.adapter.markSeen(userId, cutoff);\n deps.signals.publish(inboxChannel(userId));\n return json({ ok: true });\n }\n\n if (request.method === \"POST\" && path === \"/read\") {\n const parsed = await readJsonBody(request, maxBodyBytes);\n if (\"error\" in parsed) return parsed.error;\n const ids = Array.isArray(parsed.body.ids)\n ? parsed.body.ids.filter((id): id is string => typeof id === \"string\")\n : [];\n if (ids.length === 0) return json({ error: \"ids must be a non-empty string array\" }, 400);\n\n if (ids.length > MAX_READ_IDS) {\n return json({ error: `at most ${MAX_READ_IDS} ids per request` }, 400);\n }\n\n // 404, not 403: a 403 confirms the row exists. RFC 0002 §2.\n const updated = await deps.adapter.markRead(userId, ids);\n if (updated > 0) deps.signals.publish(inboxChannel(userId));\n return updated === 0 ? empty(404) : json({ updated });\n }\n\n if (request.method === \"POST\" && path === \"/read-all\") {\n const updated = await deps.adapter.markAllRead(userId);\n if (updated > 0) deps.signals.publish(inboxChannel(userId));\n return json({ updated });\n }\n\n return empty(404);\n }\n\n const openStreams = new Map<string, number>();\n let totalStreams = 0;\n\n const trackStream = (userId: string) => {\n openStreams.set(userId, (openStreams.get(userId) ?? 0) + 1);\n totalStreams += 1;\n return () => {\n const remaining = (openStreams.get(userId) ?? 1) - 1;\n if (remaining > 0) openStreams.set(userId, remaining);\n else openStreams.delete(userId);\n totalStreams -= 1;\n };\n };\n\n /** A change fingerprint cheap enough to take every probe interval. */\n async function fingerprint(userId: string): Promise<string> {\n const [page, unseen] = await Promise.all([\n deps.adapter.listNotifications({ userId, limit: 1 }),\n deps.adapter.countUnseen(userId),\n ]);\n const newest = page.notifications[0];\n return `${newest?.id ?? \"\"}|${newest?.readAt ? 1 : 0}|${unseen}`;\n }\n\n // One probe per user, however many streams that user holds: N tabs must not mean N queries.\n type Probe = { listeners: Set<() => void>; timer: ReturnType<typeof setInterval>; last?: string };\n const probes = new Map<string, Probe>();\n\n function subscribeProbe(userId: string, onChanged: () => void, intervalMs: number) {\n let probe = probes.get(userId);\n if (!probe) {\n const created: Probe = {\n listeners: new Set(),\n timer: setInterval(() => {\n fingerprint(userId)\n .then((value) => {\n if (created.last !== undefined && value !== created.last) {\n for (const listener of created.listeners) listener();\n }\n created.last = value;\n })\n .catch((error) => deps.logger.error(\"events probe failed\", { error }));\n }, intervalMs),\n };\n probe = created;\n probes.set(userId, created);\n fingerprint(userId)\n .then((value) => {\n if (created.last === undefined) created.last = value;\n })\n .catch(() => {});\n }\n probe.listeners.add(onChanged);\n return () => {\n const current = probes.get(userId);\n if (!current) return;\n current.listeners.delete(onChanged);\n if (current.listeners.size === 0) {\n clearInterval(current.timer);\n probes.delete(userId);\n }\n };\n }\n\n /**\n * Server-Sent Events: `ready` once, `changed` whenever this user's inbox\n * moves, comments as keepalive. No payload ever travels on the stream; the\n * client fetches the feed on `changed`. RFC 0006 §4D.\n */\n function events(request: Request, userId: string, settings: EventsSettings): Response {\n const encoder = new TextEncoder();\n const timers: ReturnType<typeof setInterval>[] = [];\n const unsubscribes: (() => void)[] = [];\n let closed = false;\n let cleanup = () => {};\n\n const stream = new ReadableStream<Uint8Array>(\n {\n start(controller) {\n const write = (text: string) => {\n if (closed) return;\n // A consumer that stopped reading would make this queue grow without bound.\n if (controller.desiredSize !== null && controller.desiredSize <= 0) {\n cleanup();\n return;\n }\n try {\n controller.enqueue(encoder.encode(text));\n } catch {\n cleanup();\n }\n };\n cleanup = () => {\n if (closed) return;\n closed = true;\n for (const unsubscribe of unsubscribes) unsubscribe();\n for (const timer of timers) clearInterval(timer);\n try {\n controller.close();\n } catch {\n // already closed by the consumer\n }\n };\n const changed = () => write(\"event: changed\\ndata: {}\\n\\n\");\n\n unsubscribes.push(trackStream(userId));\n write(\"retry: 3000\\n\\nevent: ready\\ndata: {}\\n\\n\");\n unsubscribes.push(deps.signals.subscribe(inboxChannel(userId), changed));\n timers.push(setInterval(() => write(\": keepalive\\n\\n\"), settings.heartbeatMs));\n\n if (!deps.signals.crossProcess && settings.probeIntervalMs > 0) {\n unsubscribes.push(subscribeProbe(userId, changed, settings.probeIntervalMs));\n }\n\n if (settings.maxDurationMs > 0) {\n timers.push(\n setTimeout(cleanup, settings.maxDurationMs) as ReturnType<typeof setInterval>,\n );\n }\n request.signal?.addEventListener(\"abort\", cleanup, { once: true });\n },\n cancel() {\n cleanup();\n },\n },\n { highWaterMark: STREAM_HIGH_WATER_MARK },\n );\n\n return new Response(stream, {\n headers: {\n \"content-type\": \"text/event-stream; charset=utf-8\",\n \"cache-control\": \"no-cache, no-store\",\n connection: \"keep-alive\",\n \"x-accel-buffering\": \"no\",\n \"x-content-type-options\": \"nosniff\",\n },\n });\n }\n\n async function handle(request: Request): Promise<Response> {\n let response: Response | undefined;\n try {\n response = await route(request);\n return response;\n } catch (error) {\n // A thrown driver error must not escape: unhandled rejection on Node, stack trace elsewhere.\n deps.logger.error(\"request failed\", { url: request.url, error });\n return empty(500);\n } finally {\n // Only a served request earns a sweep; anonymous 401s must not drive database work.\n if (response && response.status < 400) deps.sweepOnRequest?.();\n }\n }\n\n return {\n handle,\n GET: handle,\n POST: handle,\n };\n}\n","const ESCAPES: Record<string, string> = {\n \"&\": \"&\",\n \"<\": \"<\",\n \">\": \">\",\n '\"': \""\",\n \"'\": \"'\",\n};\n\n/** For template literals: anything a user typed goes through this, or their HTML ships from your domain. */\nexport const escapeHtml = (value: unknown): string =>\n String(value ?? \"\").replace(/[&<>\"']/g, (char) => ESCAPES[char] ?? char);\n","export type TelegramButton = { text: string; url: string };\n\nexport type TelegramMessage = {\n chatId: string;\n /** Telegram HTML: <b>, <i>, <a href>, <code>, <pre>. Escape user text with escapeHtml(). */\n text: string;\n buttons?: readonly TelegramButton[] | undefined;\n signal?: AbortSignal | undefined;\n};\n\nexport type TelegramSendResult =\n | { ok: true; messageId: number }\n | {\n ok: false;\n error: string;\n /** Worth another attempt: rate limit, 5xx, network. */\n retryable: boolean;\n /** The chat can never receive again (user blocked the bot, chat deleted). Prune it. */\n gone: boolean;\n retryAfterSeconds?: number | undefined;\n };\n\n/** The slice of a Bot API update the plugin reads. Everything else passes through untouched. */\nexport type TelegramUpdate = {\n update_id: number;\n message?: {\n text?: string;\n chat: { id: number | string; type: string; username?: string; title?: string };\n from?: { id: number; username?: string; first_name?: string };\n };\n my_chat_member?: {\n chat: { id: number | string; type: string; username?: string; title?: string };\n new_chat_member: { status: string };\n };\n};\n\nexport type TelegramProvider = {\n name: string;\n send(message: TelegramMessage): Promise<TelegramSendResult>;\n /** Long-polls for updates. For development or hosts without a public URL. */\n getUpdates(options: {\n offset?: number | undefined;\n timeoutSeconds?: number | undefined;\n signal?: AbortSignal | undefined;\n }): Promise<TelegramUpdate[]>;\n /** Points Telegram at your webhook route. Run once per deployment URL. */\n setWebhook(url: string, secretToken: string): Promise<void>;\n deleteWebhook(): Promise<void>;\n getMe(): Promise<{ id: number; username: string }>;\n};\n\nexport type TelegramBotOptions = {\n /** From @BotFather. Never appears in errors or logs. */\n token: string;\n fetch?: typeof globalThis.fetch | undefined;\n /** Default https://api.telegram.org. A local fake for tests. */\n apiBase?: string | undefined;\n timeoutMs?: number | undefined;\n};\n\ntype ApiResponse<T> =\n | { ok: true; result: T }\n | { ok: false; description?: string; error_code?: number; parameters?: { retry_after?: number } };\n\n/** Deep link that opens the bot with a start parameter. Codes are [A-Za-z0-9_-], at most 64 chars. */\nexport const telegramStartLink = (botUsername: string, code: string) =>\n `https://t.me/${botUsername.replace(/^@/, \"\")}?start=${encodeURIComponent(code)}`;\n\n/** Bot API over fetch, so it runs on edge runtimes. Failures are classified, never thrown, on send. */\nexport function telegramBot(options: TelegramBotOptions): TelegramProvider {\n const doFetch = options.fetch ?? globalThis.fetch;\n const base = `${(options.apiBase ?? \"https://api.telegram.org\").replace(/\\/$/, \"\")}/bot${options.token}`;\n const timeoutMs = options.timeoutMs ?? 10_000;\n const redact = (text: string) => text.split(options.token).join(\"<token>\");\n\n async function call<T>(\n method: string,\n body: Record<string, unknown>,\n signal?: AbortSignal,\n budgetMs = timeoutMs,\n ): Promise<{ status: number; data: ApiResponse<T> }> {\n const timeout = AbortSignal.timeout(budgetMs);\n const merged = signal ? AbortSignal.any([signal, timeout]) : timeout;\n const response = await doFetch(`${base}/${method}`, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n body: JSON.stringify(body),\n signal: merged,\n });\n const data = (await response.json().catch(() => ({ ok: false }))) as ApiResponse<T>;\n return { status: response.status, data };\n }\n\n async function must<T>(method: string, body: Record<string, unknown>): Promise<T> {\n const { data } = await call<T>(method, body);\n if (!data.ok)\n throw new Error(redact(`telegram ${method} failed: ${data.description ?? \"unknown\"}`));\n return data.result;\n }\n\n return {\n name: \"telegram\",\n\n async send(message) {\n const body: Record<string, unknown> = {\n chat_id: message.chatId,\n text: message.text,\n parse_mode: \"HTML\",\n link_preview_options: { is_disabled: true },\n };\n if (message.buttons?.length) {\n body.reply_markup = {\n inline_keyboard: [\n message.buttons.map((button) => ({ text: button.text, url: button.url })),\n ],\n };\n }\n\n let status: number;\n let data: ApiResponse<{ message_id: number }>;\n try {\n ({ status, data } = await call<{ message_id: number }>(\n \"sendMessage\",\n body,\n message.signal,\n ));\n } catch (error) {\n const text = error instanceof Error ? error.message : String(error);\n return { ok: false, error: redact(text), retryable: true, gone: false };\n }\n\n if (data.ok) return { ok: true, messageId: data.result.message_id };\n\n const description = redact(data.description ?? `HTTP ${status}`);\n const code = data.error_code ?? status;\n if (code === 429) {\n return {\n ok: false,\n error: description,\n retryable: true,\n gone: false,\n retryAfterSeconds: data.parameters?.retry_after,\n };\n }\n // 403: the user blocked the bot or was deactivated. 400 \"chat not found\": deleted or never existed.\n const gone =\n code === 403 ||\n (code === 400 && /chat not found|user is deactivated|bot was kicked/i.test(description));\n return { ok: false, error: description, retryable: code >= 500, gone };\n },\n\n async getUpdates({ offset, timeoutSeconds = 25, signal }) {\n // Telegram holds the request open for `timeout` seconds; the budget must outlive it.\n const { data } = await call<TelegramUpdate[]>(\n \"getUpdates\",\n {\n ...(offset === undefined ? {} : { offset }),\n timeout: timeoutSeconds,\n allowed_updates: [\"message\", \"my_chat_member\"],\n },\n signal,\n (timeoutSeconds + 10) * 1000,\n );\n if (!data.ok)\n throw new Error(redact(`telegram getUpdates failed: ${data.description ?? \"unknown\"}`));\n return data.result;\n },\n\n setWebhook: async (url, secretToken) => {\n await must(\"setWebhook\", {\n url,\n secret_token: secretToken,\n allowed_updates: [\"message\", \"my_chat_member\"],\n });\n },\n deleteWebhook: async () => {\n await must(\"deleteWebhook\", {});\n },\n getMe: () => must(\"getMe\", {}),\n };\n}\n","import type { DeliveryOutcome } from \"../../core/adapter\";\nimport { encodeBase64Url } from \"../../core/base64url\";\nimport type { Logger } from \"../../core/errors\";\nimport { readJsonBody } from \"../../core/handler\";\nimport { escapeHtml } from \"../../core/html\";\nimport type {\n DeliverContext,\n EasyPingPlugin,\n PluginInitContext,\n RouteDefinition,\n SchemaDeclaration,\n} from \"../../core/plugin\";\nimport {\n type TelegramProvider,\n type TelegramUpdate,\n telegramStartLink,\n} from \"../../providers/telegram\";\n\nconst CHAT = \"notification_telegram_chat\";\nconst LINK = \"notification_telegram_link\";\n\nexport type TelegramChat = {\n id: string;\n userId: string;\n chatId: string;\n /** \"private\", \"group\", \"supergroup\" or \"channel\". */\n chatType: string;\n username: string | null;\n title: string | null;\n linkedAt: Date;\n lastSeenAt: Date;\n};\n\nexport type TelegramLink = {\n code: string;\n userId: string;\n createdAt: Date;\n expiresAt: Date;\n};\n\nexport type TelegramRendered = {\n /** Telegram HTML. Anything user-typed inside it goes through escapeHtml(). */\n text: string;\n /** Adds one inline button under the message. */\n button?: { text: string; url: string } | undefined;\n};\n\nexport type TelegramOptions = {\n provider: TelegramProvider;\n /** Without the @. Builds the https://t.me/<bot>?start=<code> link. */\n botUsername: string;\n render: (input: { type: string; payload: unknown }) => TelegramRendered;\n /**\n * Shared with Telegram through setWebhook; Telegram echoes it in\n * X-Telegram-Bot-Api-Secret-Token and the webhook route accepts nothing else.\n * Unset, the route is not mounted and updates must come through poll().\n */\n webhookSecret?: string | undefined;\n /** How long a \"Connect Telegram\" link stays valid. Default 10 minutes. */\n linkTtlMinutes?: number | undefined;\n /** Linking past this evicts the user's least recently seen chat. Default 5. */\n maxChatsPerUser?: number | undefined;\n /** Bot replies, if the defaults are not your voice. */\n messages?: Partial<typeof DEFAULT_MESSAGES> | undefined;\n};\n\nconst DEFAULT_MESSAGES = {\n connected: \"Connected. You will receive notifications here.\",\n disconnected: \"Disconnected. You will not receive notifications here any more.\",\n expired: \"That link has expired. Open the app and connect Telegram again.\",\n start: \"Open the app and use “Connect Telegram” to link this chat.\",\n};\n\n/** The tables this plugin owns, exported so DDL needs no plugin instance. */\nexport const telegramSchema = {\n telegramChat: {\n tableName: CHAT,\n fields: {\n id: { type: \"string\", required: true },\n userId: { type: \"string\", required: true },\n chatId: { type: \"string\", required: true },\n chatType: { type: \"string\", required: true },\n username: { type: \"string\" },\n title: { type: \"string\" },\n linkedAt: { type: \"date\", required: true, defaultNow: true },\n lastSeenAt: { type: \"date\", required: true, defaultNow: true },\n },\n primaryKey: [\"id\"],\n indexes: [\n { on: [\"userId\"], name: \"telegram_chat_user_idx\" },\n // A chat belongs to one account at a time; re-linking moves it.\n { on: [\"chatId\"], unique: true, name: \"telegram_chat_chat_idx\" },\n ],\n },\n telegramLink: {\n tableName: LINK,\n fields: {\n code: { type: \"string\", required: true },\n userId: { type: \"string\", required: true },\n createdAt: { type: \"date\", required: true, defaultNow: true },\n expiresAt: { type: \"date\", required: true },\n },\n primaryKey: [\"code\"],\n indexes: [{ on: [\"userId\"], name: \"telegram_link_user_idx\" }],\n },\n} as const satisfies SchemaDeclaration;\n\nconst randomCode = () => encodeBase64Url(crypto.getRandomValues(new Uint8Array(24)));\n\n/** Telegram refuses buttons to localhost, private hosts and non-http schemes with a 400 for the whole message. */\nexport function isButtonUrlAllowed(value: string): boolean {\n let url: URL;\n try {\n url = new URL(value);\n } catch {\n return false;\n }\n if (url.protocol !== \"https:\" && url.protocol !== \"http:\") return url.protocol === \"tg:\";\n const host = url.hostname.toLowerCase();\n return !(\n host === \"localhost\" ||\n /\\.(localhost|local|internal|lan)$/.test(host) ||\n /^\\d+\\.\\d+\\.\\d+\\.\\d+$/.test(host) ||\n host.startsWith(\"[\")\n );\n}\n\nconst encoder = new TextEncoder();\n\nasync function timingSafeEqual(a: string, b: string): Promise<boolean> {\n const [left, right] = await Promise.all(\n [a, b].map(\n async (value) => new Uint8Array(await crypto.subtle.digest(\"SHA-256\", encoder.encode(value))),\n ),\n );\n let diff = 0;\n for (let i = 0; i < 32; i += 1) diff |= (left?.[i] ?? 0) ^ (right?.[i] ?? 0);\n return diff === 0;\n}\n\nexport type TelegramPlugin = EasyPingPlugin<\"telegram\", typeof telegramSchema> & {\n /** Feeds one Bot API update through the linking logic. The webhook route and poll() both call it. */\n handleUpdate(update: TelegramUpdate): Promise<void>;\n /**\n * Long-polls getUpdates instead of a webhook: for development, or a host\n * with no public URL. One poller per bot token, or Telegram splits updates\n * between them. Call stop() on shutdown.\n */\n poll(options?: { logger?: Logger | undefined }): { stop(): void };\n};\n\n/** Telegram over a bot the user connects with one tap. Fans out per linked chat; blocked chats are pruned. */\nexport function telegram(options: TelegramOptions): TelegramPlugin {\n let ctx: PluginInitContext;\n const messages = { ...DEFAULT_MESSAGES, ...options.messages };\n const ttlMs = (options.linkTtlMinutes ?? 10) * 60_000;\n const maxChats = Math.max(1, options.maxChatsPerUser ?? 5);\n const botUsername = options.botUsername.replace(/^@/, \"\");\n\n const reply = async (chatId: string, text: string) => {\n const result = await options.provider.send({ chatId, text: escapeHtml(text) });\n if (!result.ok) ctx.logger.warn(`telegram reply to chat failed: ${result.error}`);\n };\n\n async function link(code: string, chat: NonNullable<TelegramUpdate[\"message\"]>[\"chat\"]) {\n const [pending] = await ctx.store.find<TelegramLink>(LINK, { code }, { limit: 1 });\n const chatId = String(chat.id);\n\n if (!pending || pending.expiresAt.getTime() < Date.now()) {\n if (pending) await ctx.store.remove(LINK, { code });\n await reply(chatId, messages.expired);\n return;\n }\n // One-time: the code is spent whether or not the rest succeeds.\n await ctx.store.remove(LINK, { code });\n\n const now = new Date();\n const details = {\n chatType: chat.type,\n username: chat.username ?? null,\n title: chat.title ?? null,\n lastSeenAt: now,\n };\n const [existing] = await ctx.store.find<TelegramChat>(CHAT, { chatId }, { limit: 1 });\n\n if (existing) {\n // Re-linking moves the chat to whoever holds the fresh code: the Telegram\n // user tapped it, and the app session that minted it is theirs.\n await ctx.store.update(\n CHAT,\n { id: existing.id },\n { userId: pending.userId, linkedAt: now, ...details },\n );\n } else {\n const chats = await ctx.store.find<TelegramChat>(\n CHAT,\n { userId: pending.userId },\n { orderBy: { field: \"lastSeenAt\", direction: \"asc\" } },\n );\n const excess = chats.length - maxChats + 1;\n if (excess > 0) {\n await ctx.store.remove(CHAT, { id: { in: chats.slice(0, excess).map((row) => row.id) } });\n }\n await ctx.store.insert(CHAT, [\n { id: crypto.randomUUID(), userId: pending.userId, chatId, linkedAt: now, ...details },\n ]);\n }\n await reply(chatId, messages.connected);\n }\n\n async function handleUpdate(update: TelegramUpdate): Promise<void> {\n const member = update.my_chat_member;\n if (member && [\"kicked\", \"left\"].includes(member.new_chat_member.status)) {\n await ctx.store.remove(CHAT, { chatId: String(member.chat.id) });\n return;\n }\n\n const message = update.message;\n if (!message?.text) return;\n const chatId = String(message.chat.id);\n const [command, argument] = message.text.trim().split(/\\s+/, 2);\n // \"/start@botname\" arrives in groups.\n const name = command?.split(\"@\")[0];\n\n if (name === \"/start\") {\n if (argument) await link(argument, message.chat);\n else await reply(chatId, messages.start);\n return;\n }\n if (name === \"/stop\") {\n const removed = await ctx.store.remove(CHAT, { chatId });\n if (removed > 0) await reply(chatId, messages.disconnected);\n }\n }\n\n const routes: RouteDefinition[] = [\n {\n path: \"/telegram/link\",\n method: \"POST\",\n scope: { type: \"user\" },\n handler: async ({ userId }) => {\n const owner = userId ?? \"\";\n const now = Date.now();\n // A user's stale codes go, so the table cannot fill with abandoned links.\n await ctx.store.remove(LINK, { userId: owner, expiresAt: { lt: new Date(now) } });\n\n const code = randomCode();\n const expiresAt = new Date(now + ttlMs);\n await ctx.store.insert(LINK, [\n { code, userId: owner, createdAt: new Date(now), expiresAt },\n ]);\n return Response.json({\n url: telegramStartLink(botUsername, code),\n code,\n expiresAt: expiresAt.toISOString(),\n });\n },\n },\n {\n path: \"/telegram/chats\",\n method: \"GET\",\n scope: { type: \"user\" },\n handler: async ({ userId }) => {\n const chats = await ctx.store.find<TelegramChat>(\n CHAT,\n { userId: userId ?? \"\" },\n { orderBy: { field: \"linkedAt\", direction: \"desc\" } },\n );\n return Response.json({\n chats: chats.map((chat) => ({\n chatId: chat.chatId,\n chatType: chat.chatType,\n username: chat.username,\n title: chat.title,\n linkedAt: chat.linkedAt.toISOString(),\n })),\n });\n },\n },\n {\n path: \"/telegram/unlink\",\n method: \"POST\",\n scope: { type: \"user\" },\n handler: async ({ request, userId }) => {\n const parsed = await readJsonBody(request);\n if (\"error\" in parsed) return parsed.error;\n const { chatId } = parsed.body;\n const owner = userId ?? \"\";\n // Scoped by userId too: a chat id is not a secret.\n const removed = await ctx.store.remove(\n CHAT,\n typeof chatId === \"string\" && chatId !== \"\"\n ? { userId: owner, chatId }\n : { userId: owner },\n );\n return Response.json({ removed });\n },\n },\n ];\n\n if (options.webhookSecret) {\n const secret = options.webhookSecret;\n routes.push({\n path: \"/telegram/webhook\",\n method: \"POST\",\n scope: {\n type: \"custom\",\n justification:\n \"called by Telegram, authenticated by the X-Telegram-Bot-Api-Secret-Token header set through setWebhook\",\n },\n handler: async ({ request }) => {\n const presented = request.headers.get(\"x-telegram-bot-api-secret-token\") ?? \"\";\n if (!(await timingSafeEqual(presented, secret))) return new Response(null, { status: 401 });\n\n const parsed = await readJsonBody(request);\n if (\"error\" in parsed) return parsed.error;\n try {\n await handleUpdate(parsed.body as unknown as TelegramUpdate);\n } catch (error) {\n // Telegram retries non-2xx for hours; a bad update is logged and acknowledged.\n ctx.logger.error(\"telegram update failed\", { error });\n }\n return Response.json({ ok: true });\n },\n });\n }\n\n return {\n id: \"telegram\",\n channels: [\"telegram\"],\n schema: telegramSchema,\n\n init: (context) => {\n ctx = context;\n },\n\n hooks: {\n deliver: async (context: DeliverContext): Promise<DeliveryOutcome> => {\n const chats = await ctx.store.find<TelegramChat>(CHAT, {\n userId: context.notification.userId,\n });\n if (chats.length === 0) return { result: \"skipped\", reason: \"no linked telegram chat\" };\n\n const rendered = options.render({\n type: context.notification.type,\n payload: context.notification.payload,\n });\n let button = rendered.button;\n if (button && !isButtonUrlAllowed(button.url)) {\n ctx.logger.warn(\n `telegram: dropped button \"${button.url}\"; Telegram accepts only public http(s) links`,\n );\n button = undefined;\n }\n\n const results = await Promise.all(\n chats.map((chat) =>\n options.provider.send({\n chatId: chat.chatId,\n text: rendered.text,\n buttons: button ? [button] : undefined,\n signal: context.signal,\n }),\n ),\n );\n\n const prune: string[] = [];\n let delivered = 0;\n let retryable = false;\n let lastError = \"\";\n results.forEach((result, index) => {\n const chat = chats[index];\n if (!chat) return;\n if (result.ok) {\n delivered += 1;\n return;\n }\n lastError = result.error;\n if (result.gone) prune.push(chat.id);\n else if (result.retryable) retryable = true;\n });\n\n if (prune.length > 0) await ctx.store.remove(CHAT, { id: { in: prune } });\n if (delivered > 0) return { result: \"sent\" };\n if (prune.length === chats.length) {\n return { result: \"failed\", error: \"every linked chat is gone\", retryable: false };\n }\n return { result: \"failed\", error: lastError || \"telegram refused the message\", retryable };\n },\n },\n\n routes,\n handleUpdate,\n\n poll(pollOptions = {}) {\n const logger = pollOptions.logger ?? ctx.logger;\n const controller = new AbortController();\n let offset: number | undefined;\n\n void (async () => {\n while (!controller.signal.aborted) {\n try {\n const updates = await options.provider.getUpdates({\n offset,\n signal: controller.signal,\n });\n for (const update of updates) {\n offset = update.update_id + 1;\n await handleUpdate(update).catch((error) =>\n logger.error(\"telegram update failed\", { error }),\n );\n }\n } catch (error) {\n if (controller.signal.aborted) return;\n logger.error(\"telegram polling failed\", { error });\n await new Promise((resolve) => setTimeout(resolve, 3_000));\n }\n }\n })();\n\n return { stop: () => controller.abort() };\n },\n };\n}\n"]}
|