@kodelyth/line 2026.5.42 → 2026.6.2
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/klaw.plugin.json +329 -2
- package/package.json +18 -6
- package/api.ts +0 -11
- package/channel-plugin-api.ts +0 -1
- package/contract-api.ts +0 -5
- package/index.ts +0 -53
- package/runtime-api.ts +0 -179
- package/secret-contract-api.ts +0 -4
- package/setup-api.ts +0 -2
- package/setup-entry.ts +0 -9
- package/src/account-helpers.ts +0 -16
- package/src/accounts.test.ts +0 -288
- package/src/accounts.ts +0 -187
- package/src/actions.ts +0 -61
- package/src/auto-reply-delivery.test.ts +0 -253
- package/src/auto-reply-delivery.ts +0 -200
- package/src/bindings.ts +0 -65
- package/src/bot-access.ts +0 -30
- package/src/bot-handlers.test.ts +0 -1094
- package/src/bot-handlers.ts +0 -620
- package/src/bot-message-context.test.ts +0 -420
- package/src/bot-message-context.ts +0 -586
- package/src/bot.ts +0 -66
- package/src/card-command.ts +0 -347
- package/src/channel-access-token.ts +0 -14
- package/src/channel-api.ts +0 -17
- package/src/channel-setup-status.contract.test.ts +0 -70
- package/src/channel-shared.ts +0 -48
- package/src/channel.logout.test.ts +0 -145
- package/src/channel.runtime.ts +0 -3
- package/src/channel.sendPayload.test.ts +0 -659
- package/src/channel.setup.ts +0 -11
- package/src/channel.status.test.ts +0 -63
- package/src/channel.ts +0 -155
- package/src/config-adapter.ts +0 -29
- package/src/config-schema.test.ts +0 -53
- package/src/config-schema.ts +0 -81
- package/src/download.test.ts +0 -164
- package/src/download.ts +0 -34
- package/src/flex-templates/basic-cards.ts +0 -395
- package/src/flex-templates/common.ts +0 -20
- package/src/flex-templates/media-control-cards.ts +0 -555
- package/src/flex-templates/message.ts +0 -13
- package/src/flex-templates/schedule-cards.ts +0 -467
- package/src/flex-templates/types.ts +0 -22
- package/src/flex-templates.ts +0 -32
- package/src/gateway.ts +0 -129
- package/src/group-keys.test.ts +0 -123
- package/src/group-keys.ts +0 -65
- package/src/group-policy.ts +0 -22
- package/src/markdown-to-line.test.ts +0 -348
- package/src/markdown-to-line.ts +0 -416
- package/src/message-cards.test.ts +0 -204
- package/src/monitor-durable.test.ts +0 -57
- package/src/monitor-durable.ts +0 -37
- package/src/monitor.lifecycle.test.ts +0 -499
- package/src/monitor.runtime.ts +0 -1
- package/src/monitor.ts +0 -507
- package/src/outbound-media.test.ts +0 -194
- package/src/outbound-media.ts +0 -120
- package/src/outbound.runtime.ts +0 -12
- package/src/outbound.ts +0 -427
- package/src/probe.contract.test.ts +0 -9
- package/src/probe.runtime.ts +0 -1
- package/src/probe.ts +0 -34
- package/src/quick-reply-fallback.ts +0 -10
- package/src/reply-chunks.test.ts +0 -180
- package/src/reply-chunks.ts +0 -110
- package/src/reply-payload-transform.test.ts +0 -392
- package/src/reply-payload-transform.ts +0 -317
- package/src/rich-menu.test.ts +0 -315
- package/src/rich-menu.ts +0 -326
- package/src/runtime.ts +0 -32
- package/src/send-receipt.ts +0 -32
- package/src/send.test.ts +0 -453
- package/src/send.ts +0 -531
- package/src/setup-core.ts +0 -149
- package/src/setup-runtime-api.ts +0 -9
- package/src/setup-surface.test.ts +0 -481
- package/src/setup-surface.ts +0 -229
- package/src/signature.test.ts +0 -34
- package/src/signature.ts +0 -24
- package/src/status.ts +0 -37
- package/src/template-messages.ts +0 -333
- package/src/types.ts +0 -130
- package/src/webhook-node.test.ts +0 -598
- package/src/webhook-node.ts +0 -155
- package/src/webhook-utils.ts +0 -10
- package/src/webhook.ts +0 -135
- package/tsconfig.json +0 -16
package/src/monitor.ts
DELETED
|
@@ -1,507 +0,0 @@
|
|
|
1
|
-
import type { webhook } from "@line/bot-sdk";
|
|
2
|
-
import { hasFinalChannelTurnDispatch } from "klaw/plugin-sdk/channel-message";
|
|
3
|
-
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
4
|
-
import { chunkMarkdownText } from "klaw/plugin-sdk/reply-runtime";
|
|
5
|
-
import {
|
|
6
|
-
danger,
|
|
7
|
-
logVerbose,
|
|
8
|
-
waitForAbortSignal,
|
|
9
|
-
type RuntimeEnv,
|
|
10
|
-
} from "klaw/plugin-sdk/runtime-env";
|
|
11
|
-
import {
|
|
12
|
-
isRequestBodyLimitError,
|
|
13
|
-
normalizePluginHttpPath,
|
|
14
|
-
registerWebhookTargetWithPluginRoute,
|
|
15
|
-
requestBodyErrorToText,
|
|
16
|
-
resolveSingleWebhookTarget,
|
|
17
|
-
} from "klaw/plugin-sdk/webhook-ingress";
|
|
18
|
-
import {
|
|
19
|
-
beginWebhookRequestPipelineOrReject,
|
|
20
|
-
createWebhookInFlightLimiter,
|
|
21
|
-
} from "klaw/plugin-sdk/webhook-request-guards";
|
|
22
|
-
import { resolveDefaultLineAccountId } from "./accounts.js";
|
|
23
|
-
import { deliverLineAutoReply } from "./auto-reply-delivery.js";
|
|
24
|
-
import { createLineBot } from "./bot.js";
|
|
25
|
-
import { processLineMessage } from "./markdown-to-line.js";
|
|
26
|
-
import { resolveLineDurableReplyOptions } from "./monitor-durable.js";
|
|
27
|
-
import { sendLineReplyChunks } from "./reply-chunks.js";
|
|
28
|
-
import { getLineRuntime } from "./runtime.js";
|
|
29
|
-
import {
|
|
30
|
-
createFlexMessage,
|
|
31
|
-
createImageMessage,
|
|
32
|
-
createLocationMessage,
|
|
33
|
-
createQuickReplyItems,
|
|
34
|
-
createTextMessageWithQuickReplies,
|
|
35
|
-
getUserDisplayName,
|
|
36
|
-
pushMessageLine,
|
|
37
|
-
pushMessagesLine,
|
|
38
|
-
pushTextMessageWithQuickReplies,
|
|
39
|
-
replyMessageLine,
|
|
40
|
-
showLoadingAnimation,
|
|
41
|
-
} from "./send.js";
|
|
42
|
-
import { buildTemplateMessageFromPayload } from "./template-messages.js";
|
|
43
|
-
import type { LineChannelData, ResolvedLineAccount } from "./types.js";
|
|
44
|
-
import { createLineNodeWebhookHandler, readLineWebhookRequestBody } from "./webhook-node.js";
|
|
45
|
-
import { parseLineWebhookBody, validateLineSignature } from "./webhook-utils.js";
|
|
46
|
-
|
|
47
|
-
export interface MonitorLineProviderOptions {
|
|
48
|
-
channelAccessToken: string;
|
|
49
|
-
channelSecret: string;
|
|
50
|
-
accountId?: string;
|
|
51
|
-
config: KlawConfig;
|
|
52
|
-
runtime: RuntimeEnv;
|
|
53
|
-
abortSignal?: AbortSignal;
|
|
54
|
-
webhookUrl?: string;
|
|
55
|
-
webhookPath?: string;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface LineProviderMonitor {
|
|
59
|
-
account: ResolvedLineAccount;
|
|
60
|
-
handleWebhook: (body: webhook.CallbackRequest) => Promise<void>;
|
|
61
|
-
stop: () => void;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const runtimeState = new Map<
|
|
65
|
-
string,
|
|
66
|
-
{
|
|
67
|
-
running: boolean;
|
|
68
|
-
lastStartAt: number | null;
|
|
69
|
-
lastStopAt: number | null;
|
|
70
|
-
lastError: string | null;
|
|
71
|
-
lastInboundAt?: number | null;
|
|
72
|
-
lastOutboundAt?: number | null;
|
|
73
|
-
}
|
|
74
|
-
>();
|
|
75
|
-
const lineWebhookInFlightLimiter = createWebhookInFlightLimiter();
|
|
76
|
-
const LINE_WEBHOOK_PREAUTH_MAX_BODY_BYTES = 64 * 1024;
|
|
77
|
-
const LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS = 5_000;
|
|
78
|
-
|
|
79
|
-
type LineWebhookTarget = {
|
|
80
|
-
accountId: string;
|
|
81
|
-
bot: ReturnType<typeof createLineBot>;
|
|
82
|
-
channelSecret: string;
|
|
83
|
-
path: string;
|
|
84
|
-
runtime: RuntimeEnv;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const lineWebhookTargets = new Map<string, LineWebhookTarget[]>();
|
|
88
|
-
|
|
89
|
-
function recordChannelRuntimeState(params: {
|
|
90
|
-
channel: string;
|
|
91
|
-
accountId: string;
|
|
92
|
-
state: Partial<{
|
|
93
|
-
running: boolean;
|
|
94
|
-
lastStartAt: number | null;
|
|
95
|
-
lastStopAt: number | null;
|
|
96
|
-
lastError: string | null;
|
|
97
|
-
lastInboundAt: number | null;
|
|
98
|
-
lastOutboundAt: number | null;
|
|
99
|
-
}>;
|
|
100
|
-
}): void {
|
|
101
|
-
const key = `${params.channel}:${params.accountId}`;
|
|
102
|
-
const existing = runtimeState.get(key) ?? {
|
|
103
|
-
running: false,
|
|
104
|
-
lastStartAt: null,
|
|
105
|
-
lastStopAt: null,
|
|
106
|
-
lastError: null,
|
|
107
|
-
};
|
|
108
|
-
runtimeState.set(key, { ...existing, ...params.state });
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function getLineRuntimeState(accountId: string) {
|
|
112
|
-
return runtimeState.get(`line:${accountId}`);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function clearLineRuntimeStateForTests() {
|
|
116
|
-
runtimeState.clear();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function startLineLoadingKeepalive(params: {
|
|
120
|
-
cfg: KlawConfig;
|
|
121
|
-
userId: string;
|
|
122
|
-
accountId?: string;
|
|
123
|
-
intervalMs?: number;
|
|
124
|
-
loadingSeconds?: number;
|
|
125
|
-
}): () => void {
|
|
126
|
-
const intervalMs = params.intervalMs ?? 18_000;
|
|
127
|
-
const loadingSeconds = params.loadingSeconds ?? 20;
|
|
128
|
-
let stopped = false;
|
|
129
|
-
|
|
130
|
-
const trigger = () => {
|
|
131
|
-
if (stopped) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
void showLoadingAnimation(params.userId, {
|
|
135
|
-
cfg: params.cfg,
|
|
136
|
-
accountId: params.accountId,
|
|
137
|
-
loadingSeconds,
|
|
138
|
-
}).catch(() => {});
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
trigger();
|
|
142
|
-
const timer = setInterval(trigger, intervalMs);
|
|
143
|
-
|
|
144
|
-
return () => {
|
|
145
|
-
if (stopped) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
stopped = true;
|
|
149
|
-
clearInterval(timer);
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export async function monitorLineProvider(
|
|
154
|
-
opts: MonitorLineProviderOptions,
|
|
155
|
-
): Promise<LineProviderMonitor> {
|
|
156
|
-
const {
|
|
157
|
-
channelAccessToken,
|
|
158
|
-
channelSecret,
|
|
159
|
-
accountId,
|
|
160
|
-
config,
|
|
161
|
-
runtime,
|
|
162
|
-
abortSignal,
|
|
163
|
-
webhookPath,
|
|
164
|
-
} = opts;
|
|
165
|
-
const resolvedAccountId = accountId ?? resolveDefaultLineAccountId(config);
|
|
166
|
-
const token = channelAccessToken.trim();
|
|
167
|
-
const secret = channelSecret.trim();
|
|
168
|
-
|
|
169
|
-
if (!token) {
|
|
170
|
-
throw new Error("LINE webhook mode requires a non-empty channel access token.");
|
|
171
|
-
}
|
|
172
|
-
if (!secret) {
|
|
173
|
-
throw new Error("LINE webhook mode requires a non-empty channel secret.");
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
recordChannelRuntimeState({
|
|
177
|
-
channel: "line",
|
|
178
|
-
accountId: resolvedAccountId,
|
|
179
|
-
state: {
|
|
180
|
-
running: true,
|
|
181
|
-
lastStartAt: Date.now(),
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const bot = createLineBot({
|
|
186
|
-
channelAccessToken: token,
|
|
187
|
-
channelSecret: secret,
|
|
188
|
-
accountId,
|
|
189
|
-
runtime,
|
|
190
|
-
config,
|
|
191
|
-
onMessage: async (ctx) => {
|
|
192
|
-
if (!ctx) {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const { ctxPayload, replyToken, route } = ctx;
|
|
197
|
-
|
|
198
|
-
recordChannelRuntimeState({
|
|
199
|
-
channel: "line",
|
|
200
|
-
accountId: resolvedAccountId,
|
|
201
|
-
state: {
|
|
202
|
-
lastInboundAt: Date.now(),
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
const shouldShowLoading = Boolean(ctx.userId && !ctx.isGroup);
|
|
207
|
-
|
|
208
|
-
const displayNamePromise = ctx.userId
|
|
209
|
-
? getUserDisplayName(ctx.userId, { cfg: config, accountId: ctx.accountId })
|
|
210
|
-
: Promise.resolve(ctxPayload.From);
|
|
211
|
-
|
|
212
|
-
const stopLoading = shouldShowLoading
|
|
213
|
-
? startLineLoadingKeepalive({
|
|
214
|
-
cfg: config,
|
|
215
|
-
userId: ctx.userId!,
|
|
216
|
-
accountId: ctx.accountId,
|
|
217
|
-
})
|
|
218
|
-
: null;
|
|
219
|
-
|
|
220
|
-
const displayName = await displayNamePromise;
|
|
221
|
-
logVerbose(`line: received message from ${displayName} (${ctxPayload.From})`);
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
const textLimit = 5000;
|
|
225
|
-
let replyTokenUsed = false;
|
|
226
|
-
const core = getLineRuntime();
|
|
227
|
-
const turnResult = await core.channel.turn.run({
|
|
228
|
-
channel: "line",
|
|
229
|
-
accountId: route.accountId,
|
|
230
|
-
raw: ctx,
|
|
231
|
-
adapter: {
|
|
232
|
-
ingest: () => ({
|
|
233
|
-
id: ctxPayload.MessageSid ?? `${ctxPayload.From}:${Date.now()}`,
|
|
234
|
-
rawText: ctxPayload.RawBody ?? ctxPayload.BodyForAgent ?? "",
|
|
235
|
-
}),
|
|
236
|
-
resolveTurn: () => ({
|
|
237
|
-
cfg: config,
|
|
238
|
-
channel: "line",
|
|
239
|
-
accountId: route.accountId,
|
|
240
|
-
agentId: route.agentId,
|
|
241
|
-
routeSessionKey: route.sessionKey,
|
|
242
|
-
storePath: ctx.turn.storePath,
|
|
243
|
-
ctxPayload,
|
|
244
|
-
recordInboundSession: core.channel.session.recordInboundSession,
|
|
245
|
-
dispatchReplyWithBufferedBlockDispatcher:
|
|
246
|
-
core.channel.reply.dispatchReplyWithBufferedBlockDispatcher,
|
|
247
|
-
record: ctx.turn.record,
|
|
248
|
-
replyPipeline: {},
|
|
249
|
-
delivery: {
|
|
250
|
-
durable: (payload, info) =>
|
|
251
|
-
resolveLineDurableReplyOptions({
|
|
252
|
-
payload,
|
|
253
|
-
infoKind: info.kind,
|
|
254
|
-
to: ctxPayload.From,
|
|
255
|
-
replyToken,
|
|
256
|
-
replyTokenUsed,
|
|
257
|
-
}),
|
|
258
|
-
deliver: async (payload) => {
|
|
259
|
-
const lineData = (payload.channelData?.line as LineChannelData | undefined) ?? {};
|
|
260
|
-
|
|
261
|
-
if (ctx.userId && !ctx.isGroup) {
|
|
262
|
-
void showLoadingAnimation(ctx.userId, {
|
|
263
|
-
cfg: config,
|
|
264
|
-
accountId: ctx.accountId,
|
|
265
|
-
}).catch(() => {});
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const { replyTokenUsed: nextReplyTokenUsed } = await deliverLineAutoReply({
|
|
269
|
-
payload,
|
|
270
|
-
lineData,
|
|
271
|
-
to: ctxPayload.From,
|
|
272
|
-
replyToken,
|
|
273
|
-
replyTokenUsed,
|
|
274
|
-
accountId: ctx.accountId,
|
|
275
|
-
cfg: config,
|
|
276
|
-
textLimit,
|
|
277
|
-
deps: {
|
|
278
|
-
buildTemplateMessageFromPayload,
|
|
279
|
-
processLineMessage,
|
|
280
|
-
chunkMarkdownText,
|
|
281
|
-
sendLineReplyChunks,
|
|
282
|
-
replyMessageLine,
|
|
283
|
-
pushMessageLine,
|
|
284
|
-
pushTextMessageWithQuickReplies,
|
|
285
|
-
createQuickReplyItems,
|
|
286
|
-
createTextMessageWithQuickReplies,
|
|
287
|
-
pushMessagesLine,
|
|
288
|
-
createFlexMessage,
|
|
289
|
-
createImageMessage,
|
|
290
|
-
createLocationMessage,
|
|
291
|
-
onReplyError: (replyErr) => {
|
|
292
|
-
logVerbose(
|
|
293
|
-
`line: reply token failed, falling back to push: ${String(replyErr)}`,
|
|
294
|
-
);
|
|
295
|
-
},
|
|
296
|
-
},
|
|
297
|
-
});
|
|
298
|
-
replyTokenUsed = nextReplyTokenUsed;
|
|
299
|
-
|
|
300
|
-
recordChannelRuntimeState({
|
|
301
|
-
channel: "line",
|
|
302
|
-
accountId: resolvedAccountId,
|
|
303
|
-
state: {
|
|
304
|
-
lastOutboundAt: Date.now(),
|
|
305
|
-
},
|
|
306
|
-
});
|
|
307
|
-
},
|
|
308
|
-
onError: (err, info) => {
|
|
309
|
-
runtime.error?.(danger(`line ${info.kind} reply failed: ${String(err)}`));
|
|
310
|
-
},
|
|
311
|
-
},
|
|
312
|
-
}),
|
|
313
|
-
},
|
|
314
|
-
});
|
|
315
|
-
const dispatchResult = turnResult.dispatched ? turnResult.dispatchResult : undefined;
|
|
316
|
-
if (!hasFinalChannelTurnDispatch(dispatchResult)) {
|
|
317
|
-
logVerbose(`line: no response generated for message from ${ctxPayload.From}`);
|
|
318
|
-
}
|
|
319
|
-
} catch (err) {
|
|
320
|
-
runtime.error?.(danger(`line: auto-reply failed: ${String(err)}`));
|
|
321
|
-
|
|
322
|
-
if (replyToken) {
|
|
323
|
-
try {
|
|
324
|
-
await replyMessageLine(
|
|
325
|
-
replyToken,
|
|
326
|
-
[{ type: "text", text: "Sorry, I encountered an error processing your message." }],
|
|
327
|
-
{ cfg: config, accountId: ctx.accountId },
|
|
328
|
-
);
|
|
329
|
-
} catch (replyErr) {
|
|
330
|
-
runtime.error?.(danger(`line: error reply failed: ${String(replyErr)}`));
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
} finally {
|
|
334
|
-
stopLoading?.();
|
|
335
|
-
}
|
|
336
|
-
},
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
const normalizedPath = normalizePluginHttpPath(webhookPath, "/line/webhook") ?? "/line/webhook";
|
|
340
|
-
const createScopedLineWebhookHandler = (target: LineWebhookTarget) =>
|
|
341
|
-
createLineNodeWebhookHandler({
|
|
342
|
-
channelSecret: target.channelSecret,
|
|
343
|
-
bot: target.bot,
|
|
344
|
-
runtime: target.runtime,
|
|
345
|
-
});
|
|
346
|
-
const { unregister: unregisterHttp } = registerWebhookTargetWithPluginRoute({
|
|
347
|
-
targetsByPath: lineWebhookTargets,
|
|
348
|
-
target: {
|
|
349
|
-
accountId: resolvedAccountId,
|
|
350
|
-
bot,
|
|
351
|
-
channelSecret: secret,
|
|
352
|
-
path: normalizedPath,
|
|
353
|
-
runtime,
|
|
354
|
-
},
|
|
355
|
-
route: {
|
|
356
|
-
auth: "plugin",
|
|
357
|
-
pluginId: "line",
|
|
358
|
-
accountId: resolvedAccountId,
|
|
359
|
-
log: (msg) => logVerbose(msg),
|
|
360
|
-
handler: async (req, res) => {
|
|
361
|
-
const targets = lineWebhookTargets.get(normalizedPath) ?? [];
|
|
362
|
-
const firstTarget = targets[0];
|
|
363
|
-
if (req.method !== "POST") {
|
|
364
|
-
if (!firstTarget) {
|
|
365
|
-
res.statusCode = 404;
|
|
366
|
-
res.end("Not Found");
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
369
|
-
await createScopedLineWebhookHandler(firstTarget)(req, res);
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
const requestLifecycle = beginWebhookRequestPipelineOrReject({
|
|
374
|
-
req,
|
|
375
|
-
res,
|
|
376
|
-
inFlightLimiter: lineWebhookInFlightLimiter,
|
|
377
|
-
inFlightKey: `line:${normalizedPath}`,
|
|
378
|
-
});
|
|
379
|
-
if (!requestLifecycle.ok) {
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
try {
|
|
384
|
-
const signatureHeader = req.headers["x-line-signature"];
|
|
385
|
-
const signature =
|
|
386
|
-
typeof signatureHeader === "string"
|
|
387
|
-
? signatureHeader.trim()
|
|
388
|
-
: Array.isArray(signatureHeader)
|
|
389
|
-
? (signatureHeader[0] ?? "").trim()
|
|
390
|
-
: "";
|
|
391
|
-
|
|
392
|
-
if (!signature) {
|
|
393
|
-
logVerbose("line: webhook missing X-Line-Signature header");
|
|
394
|
-
res.statusCode = 400;
|
|
395
|
-
res.setHeader("Content-Type", "application/json");
|
|
396
|
-
res.end(JSON.stringify({ error: "Missing X-Line-Signature header" }));
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
const rawBody = await readLineWebhookRequestBody(
|
|
401
|
-
req,
|
|
402
|
-
LINE_WEBHOOK_PREAUTH_MAX_BODY_BYTES,
|
|
403
|
-
LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS,
|
|
404
|
-
);
|
|
405
|
-
const match = resolveSingleWebhookTarget(targets, (target) =>
|
|
406
|
-
validateLineSignature(rawBody, signature, target.channelSecret),
|
|
407
|
-
);
|
|
408
|
-
if (match.kind === "none") {
|
|
409
|
-
logVerbose("line: webhook signature validation failed");
|
|
410
|
-
res.statusCode = 401;
|
|
411
|
-
res.setHeader("Content-Type", "application/json");
|
|
412
|
-
res.end(JSON.stringify({ error: "Invalid signature" }));
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
|
-
if (match.kind === "ambiguous") {
|
|
416
|
-
logVerbose("line: webhook signature matched multiple accounts");
|
|
417
|
-
res.statusCode = 401;
|
|
418
|
-
res.setHeader("Content-Type", "application/json");
|
|
419
|
-
res.end(JSON.stringify({ error: "Ambiguous webhook target" }));
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
const body = parseLineWebhookBody(rawBody);
|
|
424
|
-
if (!body) {
|
|
425
|
-
res.statusCode = 400;
|
|
426
|
-
res.setHeader("Content-Type", "application/json");
|
|
427
|
-
res.end(JSON.stringify({ error: "Invalid webhook payload" }));
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
requestLifecycle.release();
|
|
432
|
-
res.statusCode = 200;
|
|
433
|
-
res.setHeader("Content-Type", "application/json");
|
|
434
|
-
res.end(JSON.stringify({ status: "ok" }));
|
|
435
|
-
|
|
436
|
-
if (body.events && body.events.length > 0) {
|
|
437
|
-
logVerbose(`line: received ${body.events.length} webhook events`);
|
|
438
|
-
void Promise.resolve()
|
|
439
|
-
.then(() => match.target.bot.handleWebhook(body))
|
|
440
|
-
.catch((err) => {
|
|
441
|
-
match.target.runtime.error?.(
|
|
442
|
-
danger(`line webhook dispatch failed: ${String(err)}`),
|
|
443
|
-
);
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
} catch (err) {
|
|
447
|
-
if (isRequestBodyLimitError(err, "PAYLOAD_TOO_LARGE")) {
|
|
448
|
-
res.statusCode = 413;
|
|
449
|
-
res.setHeader("Content-Type", "application/json");
|
|
450
|
-
res.end(JSON.stringify({ error: "Payload too large" }));
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (isRequestBodyLimitError(err, "REQUEST_BODY_TIMEOUT")) {
|
|
454
|
-
res.statusCode = 408;
|
|
455
|
-
res.setHeader("Content-Type", "application/json");
|
|
456
|
-
res.end(JSON.stringify({ error: requestBodyErrorToText("REQUEST_BODY_TIMEOUT") }));
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
runtime.error?.(danger(`line webhook error: ${String(err)}`));
|
|
460
|
-
if (!res.headersSent) {
|
|
461
|
-
res.statusCode = 500;
|
|
462
|
-
res.setHeader("Content-Type", "application/json");
|
|
463
|
-
res.end(JSON.stringify({ error: "Internal server error" }));
|
|
464
|
-
}
|
|
465
|
-
} finally {
|
|
466
|
-
requestLifecycle.release();
|
|
467
|
-
}
|
|
468
|
-
},
|
|
469
|
-
},
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
logVerbose(`line: registered webhook handler at ${normalizedPath}`);
|
|
473
|
-
|
|
474
|
-
let stopped = false;
|
|
475
|
-
const stopHandler = () => {
|
|
476
|
-
if (stopped) {
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
stopped = true;
|
|
480
|
-
logVerbose(`line: stopping provider for account ${resolvedAccountId}`);
|
|
481
|
-
unregisterHttp();
|
|
482
|
-
recordChannelRuntimeState({
|
|
483
|
-
channel: "line",
|
|
484
|
-
accountId: resolvedAccountId,
|
|
485
|
-
state: {
|
|
486
|
-
running: false,
|
|
487
|
-
lastStopAt: Date.now(),
|
|
488
|
-
},
|
|
489
|
-
});
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
if (abortSignal?.aborted) {
|
|
493
|
-
stopHandler();
|
|
494
|
-
} else if (abortSignal) {
|
|
495
|
-
abortSignal.addEventListener("abort", stopHandler, { once: true });
|
|
496
|
-
await waitForAbortSignal(abortSignal);
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
return {
|
|
500
|
-
account: bot.account,
|
|
501
|
-
handleWebhook: bot.handleWebhook,
|
|
502
|
-
stop: () => {
|
|
503
|
-
stopHandler();
|
|
504
|
-
abortSignal?.removeEventListener("abort", stopHandler);
|
|
505
|
-
},
|
|
506
|
-
};
|
|
507
|
-
}
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
const ssrfMocks = vi.hoisted(() => ({
|
|
4
|
-
resolvePinnedHostnameWithPolicy: vi.fn(),
|
|
5
|
-
}));
|
|
6
|
-
|
|
7
|
-
vi.mock("klaw/plugin-sdk/ssrf-runtime", () => ({
|
|
8
|
-
resolvePinnedHostnameWithPolicy: ssrfMocks.resolvePinnedHostnameWithPolicy,
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
afterAll(() => {
|
|
12
|
-
vi.doUnmock("klaw/plugin-sdk/ssrf-runtime");
|
|
13
|
-
vi.resetModules();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
detectLineMediaKind,
|
|
18
|
-
resolveLineOutboundMedia,
|
|
19
|
-
validateLineMediaUrl,
|
|
20
|
-
} from "./outbound-media.js";
|
|
21
|
-
|
|
22
|
-
describe("validateLineMediaUrl", () => {
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
ssrfMocks.resolvePinnedHostnameWithPolicy.mockReset();
|
|
25
|
-
ssrfMocks.resolvePinnedHostnameWithPolicy.mockResolvedValue({
|
|
26
|
-
hostname: "example.com",
|
|
27
|
-
addresses: ["93.184.216.34"],
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("accepts HTTPS URL", async () => {
|
|
32
|
-
await expect(validateLineMediaUrl("https://example.com/image.jpg")).resolves.toBeUndefined();
|
|
33
|
-
expect(ssrfMocks.resolvePinnedHostnameWithPolicy).toHaveBeenCalledWith("example.com", {
|
|
34
|
-
policy: { allowPrivateNetwork: false },
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("accepts uppercase HTTPS scheme", async () => {
|
|
39
|
-
await expect(validateLineMediaUrl("HTTPS://EXAMPLE.COM/img.jpg")).resolves.toBeUndefined();
|
|
40
|
-
expect(ssrfMocks.resolvePinnedHostnameWithPolicy).toHaveBeenCalledWith("example.com", {
|
|
41
|
-
policy: { allowPrivateNetwork: false },
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("rejects HTTP URL", async () => {
|
|
46
|
-
await expect(validateLineMediaUrl("http://example.com/image.jpg")).rejects.toThrow(
|
|
47
|
-
/must use HTTPS/i,
|
|
48
|
-
);
|
|
49
|
-
expect(ssrfMocks.resolvePinnedHostnameWithPolicy).not.toHaveBeenCalled();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("rejects URL longer than 2000 chars", async () => {
|
|
53
|
-
const longUrl = `https://example.com/${"a".repeat(1981)}`;
|
|
54
|
-
expect(longUrl.length).toBeGreaterThan(2000);
|
|
55
|
-
await expect(validateLineMediaUrl(longUrl)).rejects.toThrow(/2000 chars or less/i);
|
|
56
|
-
expect(ssrfMocks.resolvePinnedHostnameWithPolicy).not.toHaveBeenCalled();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("rejects private-network targets through the shared SSRF policy", async () => {
|
|
60
|
-
ssrfMocks.resolvePinnedHostnameWithPolicy.mockRejectedValueOnce(
|
|
61
|
-
new Error("SSRF blocked private network target"),
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
await expect(validateLineMediaUrl("https://127.0.0.1/image.jpg")).rejects.toThrow(
|
|
65
|
-
/private network/i,
|
|
66
|
-
);
|
|
67
|
-
expect(ssrfMocks.resolvePinnedHostnameWithPolicy).toHaveBeenCalledWith("127.0.0.1", {
|
|
68
|
-
policy: { allowPrivateNetwork: false },
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe("detectLineMediaKind", () => {
|
|
74
|
-
it("maps image MIME to image", () => {
|
|
75
|
-
expect(detectLineMediaKind("image/jpeg")).toBe("image");
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("maps uppercase image MIME to image", () => {
|
|
79
|
-
expect(detectLineMediaKind("IMAGE/JPEG")).toBe("image");
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("maps video MIME to video", () => {
|
|
83
|
-
expect(detectLineMediaKind("video/mp4")).toBe("video");
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("maps audio MIME to audio", () => {
|
|
87
|
-
expect(detectLineMediaKind("audio/mpeg")).toBe("audio");
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("falls back unknown MIME to image", () => {
|
|
91
|
-
expect(detectLineMediaKind("application/octet-stream")).toBe("image");
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe("resolveLineOutboundMedia", () => {
|
|
96
|
-
beforeEach(() => {
|
|
97
|
-
ssrfMocks.resolvePinnedHostnameWithPolicy.mockReset();
|
|
98
|
-
ssrfMocks.resolvePinnedHostnameWithPolicy.mockResolvedValue({
|
|
99
|
-
hostname: "example.com",
|
|
100
|
-
addresses: ["93.184.216.34"],
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("respects explicit media kind without remote MIME probing", async () => {
|
|
105
|
-
await expect(
|
|
106
|
-
resolveLineOutboundMedia("https://example.com/download?id=123", { mediaKind: "video" }),
|
|
107
|
-
).resolves.toEqual({
|
|
108
|
-
mediaUrl: "https://example.com/download?id=123",
|
|
109
|
-
mediaKind: "video",
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it("preserves explicit video kind when a preview URL is provided", async () => {
|
|
114
|
-
await expect(
|
|
115
|
-
resolveLineOutboundMedia("https://example.com/download?id=123", {
|
|
116
|
-
mediaKind: "video",
|
|
117
|
-
previewImageUrl: "https://example.com/preview.jpg",
|
|
118
|
-
}),
|
|
119
|
-
).resolves.toEqual({
|
|
120
|
-
mediaUrl: "https://example.com/download?id=123",
|
|
121
|
-
mediaKind: "video",
|
|
122
|
-
previewImageUrl: "https://example.com/preview.jpg",
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it("infers audio kind from explicit duration metadata when mediaKind is omitted", async () => {
|
|
127
|
-
await expect(
|
|
128
|
-
resolveLineOutboundMedia("https://example.com/download?id=audio", {
|
|
129
|
-
durationMs: 60000,
|
|
130
|
-
}),
|
|
131
|
-
).resolves.toEqual({
|
|
132
|
-
mediaUrl: "https://example.com/download?id=audio",
|
|
133
|
-
mediaKind: "audio",
|
|
134
|
-
durationMs: 60000,
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("does not infer video from previewImageUrl alone", async () => {
|
|
139
|
-
await expect(
|
|
140
|
-
resolveLineOutboundMedia("https://example.com/image.jpg", {
|
|
141
|
-
previewImageUrl: "https://example.com/preview.jpg",
|
|
142
|
-
}),
|
|
143
|
-
).resolves.toEqual({
|
|
144
|
-
mediaUrl: "https://example.com/image.jpg",
|
|
145
|
-
mediaKind: "image",
|
|
146
|
-
previewImageUrl: "https://example.com/preview.jpg",
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("infers media kinds from known HTTPS file extensions", async () => {
|
|
151
|
-
await expect(resolveLineOutboundMedia("https://example.com/audio.mp3")).resolves.toEqual({
|
|
152
|
-
mediaUrl: "https://example.com/audio.mp3",
|
|
153
|
-
mediaKind: "audio",
|
|
154
|
-
});
|
|
155
|
-
await expect(resolveLineOutboundMedia("https://example.com/video.mp4")).resolves.toEqual({
|
|
156
|
-
mediaUrl: "https://example.com/video.mp4",
|
|
157
|
-
mediaKind: "video",
|
|
158
|
-
});
|
|
159
|
-
await expect(resolveLineOutboundMedia("https://example.com/image.jpg")).resolves.toEqual({
|
|
160
|
-
mediaUrl: "https://example.com/image.jpg",
|
|
161
|
-
mediaKind: "image",
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it("validates previewImageUrl when provided", async () => {
|
|
166
|
-
await expect(
|
|
167
|
-
resolveLineOutboundMedia("https://example.com/video.mp4", {
|
|
168
|
-
mediaKind: "video",
|
|
169
|
-
previewImageUrl: "http://example.com/preview.jpg",
|
|
170
|
-
}),
|
|
171
|
-
).rejects.toThrow(/must use HTTPS/i);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it("falls back to image when no explicit LINE media options or known extension are present", async () => {
|
|
175
|
-
await expect(
|
|
176
|
-
resolveLineOutboundMedia("https://example.com/download?id=audio"),
|
|
177
|
-
).resolves.toEqual({
|
|
178
|
-
mediaUrl: "https://example.com/download?id=audio",
|
|
179
|
-
mediaKind: "image",
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it("rejects local paths because LINE outbound media requires public HTTPS URLs", async () => {
|
|
184
|
-
await expect(resolveLineOutboundMedia("./assets/image.jpg")).rejects.toThrow(
|
|
185
|
-
/requires a public https url/i,
|
|
186
|
-
);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it("rejects non-HTTPS URL explicitly", async () => {
|
|
190
|
-
await expect(resolveLineOutboundMedia("http://example.com/image.jpg")).rejects.toThrow(
|
|
191
|
-
/must use HTTPS/i,
|
|
192
|
-
);
|
|
193
|
-
});
|
|
194
|
-
});
|