@kodelyth/synology-chat 2026.5.42 → 2026.6.1

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/src/client.ts DELETED
@@ -1,326 +0,0 @@
1
- /**
2
- * Synology Chat HTTP client.
3
- * Sends messages TO Synology Chat via the incoming webhook URL.
4
- */
5
-
6
- import * as http from "node:http";
7
- import * as https from "node:https";
8
- import { safeParseJsonWithSchema, safeParseWithSchema } from "klaw/plugin-sdk/extension-shared";
9
- import { sleep } from "klaw/plugin-sdk/runtime-env";
10
- import { formatErrorMessage, resolvePinnedHostnameWithPolicy } from "klaw/plugin-sdk/ssrf-runtime";
11
- import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
12
- import { z } from "zod";
13
-
14
- const MIN_SEND_INTERVAL_MS = 500;
15
- let lastSendTime = 0;
16
-
17
- // --- Chat user_id resolution ---
18
- // Synology Chat uses two different user_id spaces:
19
- // - Outgoing webhook user_id: per-integration sequential ID (e.g. 1)
20
- // - Chat API user_id: global internal ID (e.g. 4)
21
- // The chatbot API (method=chatbot) requires the Chat API user_id in the
22
- // user_ids array. We resolve via the user_list API and cache the result.
23
-
24
- interface ChatUser {
25
- user_id: number;
26
- username: string;
27
- nickname: string;
28
- }
29
-
30
- type ChatUserCacheEntry = {
31
- users: ChatUser[];
32
- cachedAt: number;
33
- };
34
-
35
- type ChatWebhookPayload = {
36
- text?: string;
37
- file_url?: string;
38
- user_ids?: number[];
39
- };
40
-
41
- const ChatUserSchema = z
42
- .object({
43
- user_id: z.number(),
44
- username: z.string().optional(),
45
- nickname: z.string().optional(),
46
- })
47
- .transform(
48
- (user): ChatUser => ({
49
- user_id: user.user_id,
50
- username: user.username ?? "",
51
- nickname: user.nickname ?? "",
52
- }),
53
- );
54
-
55
- const ChatUserListResponseSchema = z.object({
56
- success: z.boolean(),
57
- data: z
58
- .object({
59
- users: z
60
- .array(z.unknown())
61
- .optional()
62
- .transform((users) =>
63
- (users ?? []).flatMap((user) => {
64
- const parsed = safeParseWithSchema(ChatUserSchema, user);
65
- return parsed ? [parsed] : [];
66
- }),
67
- ),
68
- })
69
- .optional(),
70
- });
71
-
72
- // Cache user lists per bot endpoint to avoid cross-account bleed.
73
- const chatUserCache = new Map<string, ChatUserCacheEntry>();
74
- const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
75
-
76
- /**
77
- * Send a text message to Synology Chat via the incoming webhook.
78
- *
79
- * @param incomingUrl - Synology Chat incoming webhook URL
80
- * @param text - Message text to send
81
- * @param userId - Optional user ID to mention with @
82
- * @returns true if sent successfully
83
- */
84
- export async function sendMessage(
85
- incomingUrl: string,
86
- text: string,
87
- userId?: string | number,
88
- allowInsecureSsl = false,
89
- ): Promise<boolean> {
90
- // Synology Chat API requires user_ids (numeric) to specify the recipient
91
- // The @mention is optional but user_ids is mandatory
92
- const body = buildWebhookBody({ text }, userId);
93
-
94
- // Internal rate limit: min 500ms between sends
95
- const now = Date.now();
96
- const elapsed = now - lastSendTime;
97
- if (elapsed < MIN_SEND_INTERVAL_MS) {
98
- await sleep(MIN_SEND_INTERVAL_MS - elapsed);
99
- }
100
-
101
- // Retry with exponential backoff (3 attempts, 300ms base)
102
- const maxRetries = 3;
103
- const baseDelay = 300;
104
-
105
- for (let attempt = 0; attempt < maxRetries; attempt++) {
106
- try {
107
- const ok = await doPost(incomingUrl, body, allowInsecureSsl);
108
- lastSendTime = Date.now();
109
- if (ok) {
110
- return true;
111
- }
112
- } catch {
113
- // will retry
114
- }
115
-
116
- if (attempt < maxRetries - 1) {
117
- await sleep(baseDelay * 2 ** attempt);
118
- }
119
- }
120
-
121
- return false;
122
- }
123
-
124
- /**
125
- * Send a file URL to Synology Chat.
126
- */
127
- export async function sendFileUrl(
128
- incomingUrl: string,
129
- fileUrl: string,
130
- userId?: string | number,
131
- allowInsecureSsl = false,
132
- ): Promise<boolean> {
133
- try {
134
- const safeFileUrl = await assertSafeWebhookFileUrl(fileUrl);
135
- const body = buildWebhookBody({ file_url: safeFileUrl }, userId);
136
-
137
- const now = Date.now();
138
- const elapsed = now - lastSendTime;
139
- if (elapsed < MIN_SEND_INTERVAL_MS) {
140
- await sleep(MIN_SEND_INTERVAL_MS - elapsed);
141
- }
142
-
143
- const ok = await doPost(incomingUrl, body, allowInsecureSsl);
144
- lastSendTime = Date.now();
145
- return ok;
146
- } catch {
147
- return false;
148
- }
149
- }
150
-
151
- /**
152
- * Fetch the list of Chat users visible to this bot via the user_list API.
153
- * Results are cached for CACHE_TTL_MS to avoid excessive API calls.
154
- *
155
- * The user_list endpoint uses the same base URL as the chatbot API but
156
- * with method=user_list instead of method=chatbot.
157
- */
158
- export async function fetchChatUsers(
159
- incomingUrl: string,
160
- allowInsecureSsl = false,
161
- log?: { warn: (...args: unknown[]) => void },
162
- ): Promise<ChatUser[]> {
163
- const now = Date.now();
164
- const listUrl = incomingUrl.replace(/method=\w+/, "method=user_list");
165
- const cached = chatUserCache.get(listUrl);
166
- if (cached && now - cached.cachedAt < CACHE_TTL_MS) {
167
- return cached.users;
168
- }
169
-
170
- return new Promise((resolve) => {
171
- let parsedUrl: URL;
172
- try {
173
- parsedUrl = new URL(listUrl);
174
- } catch {
175
- log?.warn("fetchChatUsers: invalid user_list URL, using cached data");
176
- resolve(cached?.users ?? []);
177
- return;
178
- }
179
- const transport = parsedUrl.protocol === "https:" ? https : http;
180
- const requestOptions: http.RequestOptions | https.RequestOptions =
181
- parsedUrl.protocol === "https:" ? { rejectUnauthorized: !allowInsecureSsl } : {};
182
-
183
- transport
184
- .get(listUrl, requestOptions, (res) => {
185
- let data = "";
186
- res.on("data", (c: Buffer) => {
187
- data += c.toString();
188
- });
189
- res.on("end", () => {
190
- const result = safeParseJsonWithSchema(ChatUserListResponseSchema, data);
191
- if (!result) {
192
- log?.warn("fetchChatUsers: failed to parse user_list response");
193
- resolve(cached?.users ?? []);
194
- return;
195
- }
196
-
197
- if (result.success) {
198
- const users = result.data?.users ?? [];
199
- chatUserCache.set(listUrl, {
200
- users,
201
- cachedAt: now,
202
- });
203
- resolve(users);
204
- return;
205
- }
206
-
207
- log?.warn(`fetchChatUsers: API returned success=${result.success}, using cached data`);
208
- resolve(cached?.users ?? []);
209
- });
210
- })
211
- .on("error", (err) => {
212
- log?.warn(`fetchChatUsers: HTTP error — ${err instanceof Error ? err.message : err}`);
213
- resolve(cached?.users ?? []);
214
- });
215
- });
216
- }
217
-
218
- async function assertSafeWebhookFileUrl(fileUrl: string): Promise<string> {
219
- let parsed: URL;
220
- try {
221
- parsed = new URL(fileUrl);
222
- } catch (err) {
223
- throw new Error(`Invalid Synology Chat file URL: ${formatErrorMessage(err)}`, { cause: err });
224
- }
225
-
226
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
227
- throw new Error("Synology Chat file URL must use HTTP or HTTPS");
228
- }
229
-
230
- await resolvePinnedHostnameWithPolicy(parsed.hostname);
231
- return parsed.toString();
232
- }
233
-
234
- /**
235
- * Resolve a mutable webhook username/nickname to the correct Chat API user_id.
236
- *
237
- * Synology Chat outgoing webhooks send a user_id that may NOT match the
238
- * Chat-internal user_id needed by the chatbot API (method=chatbot).
239
- * The webhook's "username" field corresponds to the Chat user's "nickname".
240
- *
241
- * @returns The correct Chat user_id, or undefined if not found
242
- */
243
- export async function resolveLegacyWebhookNameToChatUserId(params: {
244
- incomingUrl: string;
245
- mutableWebhookUsername: string;
246
- allowInsecureSsl?: boolean;
247
- log?: { warn: (...args: unknown[]) => void };
248
- }): Promise<number | undefined> {
249
- const users = await fetchChatUsers(params.incomingUrl, params.allowInsecureSsl, params.log);
250
- const lower = normalizeLowercaseStringOrEmpty(params.mutableWebhookUsername);
251
-
252
- // Match by nickname first (webhook "username" field = Chat "nickname")
253
- const byNickname = users.find((u) => normalizeLowercaseStringOrEmpty(u.nickname) === lower);
254
- if (byNickname) {
255
- return byNickname.user_id;
256
- }
257
-
258
- // Then by username
259
- const byUsername = users.find((u) => normalizeLowercaseStringOrEmpty(u.username) === lower);
260
- if (byUsername) {
261
- return byUsername.user_id;
262
- }
263
-
264
- return undefined;
265
- }
266
-
267
- function buildWebhookBody(payload: ChatWebhookPayload, userId?: string | number): string {
268
- const numericId = parseNumericUserId(userId);
269
- if (numericId !== undefined) {
270
- payload.user_ids = [numericId];
271
- }
272
- return `payload=${encodeURIComponent(JSON.stringify(payload))}`;
273
- }
274
-
275
- function parseNumericUserId(userId?: string | number): number | undefined {
276
- if (userId === undefined) {
277
- return undefined;
278
- }
279
- const numericId = typeof userId === "number" ? userId : Number.parseInt(userId, 10);
280
- return Number.isNaN(numericId) ? undefined : numericId;
281
- }
282
-
283
- function doPost(url: string, body: string, allowInsecureSsl = false): Promise<boolean> {
284
- return new Promise((resolve, reject) => {
285
- let parsedUrl: URL;
286
- try {
287
- parsedUrl = new URL(url);
288
- } catch {
289
- reject(new Error(`Invalid URL: ${url}`));
290
- return;
291
- }
292
- const transport = parsedUrl.protocol === "https:" ? https : http;
293
-
294
- const req = transport.request(
295
- url,
296
- {
297
- method: "POST",
298
- headers: {
299
- "Content-Type": "application/x-www-form-urlencoded",
300
- "Content-Length": Buffer.byteLength(body),
301
- },
302
- timeout: 30_000,
303
- // Synology NAS may use self-signed certs on local network.
304
- // Set allowInsecureSsl: true in channel config to skip verification.
305
- rejectUnauthorized: !allowInsecureSsl,
306
- },
307
- (res) => {
308
- let data = "";
309
- res.on("data", (chunk: Buffer) => {
310
- data += chunk.toString();
311
- });
312
- res.on("end", () => {
313
- resolve(res.statusCode === 200);
314
- });
315
- },
316
- );
317
-
318
- req.on("error", reject);
319
- req.on("timeout", () => {
320
- req.destroy();
321
- reject(new Error("Request timeout"));
322
- });
323
- req.write(body);
324
- req.end();
325
- });
326
- }
@@ -1,11 +0,0 @@
1
- import { buildChannelConfigSchema } from "klaw/plugin-sdk/channel-config-schema";
2
- import { z } from "zod";
3
-
4
- export const SynologyChatChannelConfigSchema = buildChannelConfigSchema(
5
- z
6
- .object({
7
- dangerouslyAllowNameMatching: z.boolean().optional(),
8
- dangerouslyAllowInheritedWebhookPath: z.boolean().optional(),
9
- })
10
- .passthrough(),
11
- );