@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.
Files changed (90) hide show
  1. package/klaw.plugin.json +329 -2
  2. package/package.json +18 -6
  3. package/api.ts +0 -11
  4. package/channel-plugin-api.ts +0 -1
  5. package/contract-api.ts +0 -5
  6. package/index.ts +0 -53
  7. package/runtime-api.ts +0 -179
  8. package/secret-contract-api.ts +0 -4
  9. package/setup-api.ts +0 -2
  10. package/setup-entry.ts +0 -9
  11. package/src/account-helpers.ts +0 -16
  12. package/src/accounts.test.ts +0 -288
  13. package/src/accounts.ts +0 -187
  14. package/src/actions.ts +0 -61
  15. package/src/auto-reply-delivery.test.ts +0 -253
  16. package/src/auto-reply-delivery.ts +0 -200
  17. package/src/bindings.ts +0 -65
  18. package/src/bot-access.ts +0 -30
  19. package/src/bot-handlers.test.ts +0 -1094
  20. package/src/bot-handlers.ts +0 -620
  21. package/src/bot-message-context.test.ts +0 -420
  22. package/src/bot-message-context.ts +0 -586
  23. package/src/bot.ts +0 -66
  24. package/src/card-command.ts +0 -347
  25. package/src/channel-access-token.ts +0 -14
  26. package/src/channel-api.ts +0 -17
  27. package/src/channel-setup-status.contract.test.ts +0 -70
  28. package/src/channel-shared.ts +0 -48
  29. package/src/channel.logout.test.ts +0 -145
  30. package/src/channel.runtime.ts +0 -3
  31. package/src/channel.sendPayload.test.ts +0 -659
  32. package/src/channel.setup.ts +0 -11
  33. package/src/channel.status.test.ts +0 -63
  34. package/src/channel.ts +0 -155
  35. package/src/config-adapter.ts +0 -29
  36. package/src/config-schema.test.ts +0 -53
  37. package/src/config-schema.ts +0 -81
  38. package/src/download.test.ts +0 -164
  39. package/src/download.ts +0 -34
  40. package/src/flex-templates/basic-cards.ts +0 -395
  41. package/src/flex-templates/common.ts +0 -20
  42. package/src/flex-templates/media-control-cards.ts +0 -555
  43. package/src/flex-templates/message.ts +0 -13
  44. package/src/flex-templates/schedule-cards.ts +0 -467
  45. package/src/flex-templates/types.ts +0 -22
  46. package/src/flex-templates.ts +0 -32
  47. package/src/gateway.ts +0 -129
  48. package/src/group-keys.test.ts +0 -123
  49. package/src/group-keys.ts +0 -65
  50. package/src/group-policy.ts +0 -22
  51. package/src/markdown-to-line.test.ts +0 -348
  52. package/src/markdown-to-line.ts +0 -416
  53. package/src/message-cards.test.ts +0 -204
  54. package/src/monitor-durable.test.ts +0 -57
  55. package/src/monitor-durable.ts +0 -37
  56. package/src/monitor.lifecycle.test.ts +0 -499
  57. package/src/monitor.runtime.ts +0 -1
  58. package/src/monitor.ts +0 -507
  59. package/src/outbound-media.test.ts +0 -194
  60. package/src/outbound-media.ts +0 -120
  61. package/src/outbound.runtime.ts +0 -12
  62. package/src/outbound.ts +0 -427
  63. package/src/probe.contract.test.ts +0 -9
  64. package/src/probe.runtime.ts +0 -1
  65. package/src/probe.ts +0 -34
  66. package/src/quick-reply-fallback.ts +0 -10
  67. package/src/reply-chunks.test.ts +0 -180
  68. package/src/reply-chunks.ts +0 -110
  69. package/src/reply-payload-transform.test.ts +0 -392
  70. package/src/reply-payload-transform.ts +0 -317
  71. package/src/rich-menu.test.ts +0 -315
  72. package/src/rich-menu.ts +0 -326
  73. package/src/runtime.ts +0 -32
  74. package/src/send-receipt.ts +0 -32
  75. package/src/send.test.ts +0 -453
  76. package/src/send.ts +0 -531
  77. package/src/setup-core.ts +0 -149
  78. package/src/setup-runtime-api.ts +0 -9
  79. package/src/setup-surface.test.ts +0 -481
  80. package/src/setup-surface.ts +0 -229
  81. package/src/signature.test.ts +0 -34
  82. package/src/signature.ts +0 -24
  83. package/src/status.ts +0 -37
  84. package/src/template-messages.ts +0 -333
  85. package/src/types.ts +0 -130
  86. package/src/webhook-node.test.ts +0 -598
  87. package/src/webhook-node.ts +0 -155
  88. package/src/webhook-utils.ts +0 -10
  89. package/src/webhook.ts +0 -135
  90. package/tsconfig.json +0 -16
@@ -1,155 +0,0 @@
1
- import type { IncomingMessage, ServerResponse } from "node:http";
2
- import type { webhook } from "@line/bot-sdk";
3
- import {
4
- createMessageReceiveContext,
5
- type MessageReceiveContext,
6
- } from "klaw/plugin-sdk/channel-message";
7
- import { danger, logVerbose, type RuntimeEnv } from "klaw/plugin-sdk/runtime-env";
8
- import {
9
- isRequestBodyLimitError,
10
- readRequestBodyWithLimit,
11
- requestBodyErrorToText,
12
- } from "klaw/plugin-sdk/webhook-request-guards";
13
- import { parseLineWebhookBody, validateLineSignature } from "./webhook-utils.js";
14
-
15
- const LINE_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
16
- const LINE_WEBHOOK_PREAUTH_MAX_BODY_BYTES = 64 * 1024;
17
- const LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS = 5_000;
18
-
19
- export async function readLineWebhookRequestBody(
20
- req: IncomingMessage,
21
- maxBytes = LINE_WEBHOOK_MAX_BODY_BYTES,
22
- timeoutMs = LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS,
23
- ): Promise<string> {
24
- return await readRequestBodyWithLimit(req, {
25
- maxBytes,
26
- timeoutMs,
27
- });
28
- }
29
-
30
- type ReadBodyFn = (req: IncomingMessage, maxBytes: number, timeoutMs?: number) => Promise<string>;
31
-
32
- function logLineWebhookDispatchError(runtime: RuntimeEnv | undefined, err: unknown): void {
33
- runtime?.error?.(danger(`line webhook dispatch failed: ${String(err)}`));
34
- }
35
-
36
- export function createLineNodeWebhookHandler(params: {
37
- channelSecret: string;
38
- bot: { handleWebhook: (body: webhook.CallbackRequest) => Promise<void> };
39
- runtime: RuntimeEnv;
40
- readBody?: ReadBodyFn;
41
- maxBodyBytes?: number;
42
- onRequestAuthenticated?: () => void;
43
- }): (req: IncomingMessage, res: ServerResponse) => Promise<void> {
44
- const maxBodyBytes = params.maxBodyBytes ?? LINE_WEBHOOK_MAX_BODY_BYTES;
45
- const readBody = params.readBody ?? readLineWebhookRequestBody;
46
-
47
- return async (req: IncomingMessage, res: ServerResponse) => {
48
- if (req.method === "GET" || req.method === "HEAD") {
49
- if (req.method === "HEAD") {
50
- res.statusCode = 204;
51
- res.end();
52
- return;
53
- }
54
- res.statusCode = 200;
55
- res.setHeader("Content-Type", "text/plain");
56
- res.end("OK");
57
- return;
58
- }
59
-
60
- if (req.method !== "POST") {
61
- res.statusCode = 405;
62
- res.setHeader("Allow", "GET, HEAD, POST");
63
- res.setHeader("Content-Type", "application/json");
64
- res.end(JSON.stringify({ error: "Method Not Allowed" }));
65
- return;
66
- }
67
-
68
- let receiveContext: MessageReceiveContext<webhook.CallbackRequest> | undefined;
69
- try {
70
- const signatureHeader = req.headers["x-line-signature"];
71
- const signature =
72
- typeof signatureHeader === "string"
73
- ? signatureHeader.trim()
74
- : Array.isArray(signatureHeader)
75
- ? (signatureHeader[0] ?? "").trim()
76
- : "";
77
-
78
- if (!signature) {
79
- logVerbose("line: webhook missing X-Line-Signature header");
80
- res.statusCode = 400;
81
- res.setHeader("Content-Type", "application/json");
82
- res.end(JSON.stringify({ error: "Missing X-Line-Signature header" }));
83
- return;
84
- }
85
-
86
- const rawBody = await readBody(
87
- req,
88
- Math.min(maxBodyBytes, LINE_WEBHOOK_PREAUTH_MAX_BODY_BYTES),
89
- LINE_WEBHOOK_PREAUTH_BODY_TIMEOUT_MS,
90
- );
91
-
92
- if (!validateLineSignature(rawBody, signature, params.channelSecret)) {
93
- logVerbose("line: webhook signature validation failed");
94
- res.statusCode = 401;
95
- res.setHeader("Content-Type", "application/json");
96
- res.end(JSON.stringify({ error: "Invalid signature" }));
97
- return;
98
- }
99
-
100
- const body = parseLineWebhookBody(rawBody);
101
-
102
- if (!body) {
103
- res.statusCode = 400;
104
- res.setHeader("Content-Type", "application/json");
105
- res.end(JSON.stringify({ error: "Invalid webhook payload" }));
106
- return;
107
- }
108
-
109
- params.onRequestAuthenticated?.();
110
-
111
- receiveContext = createMessageReceiveContext({
112
- id: `${Date.now()}:line:webhook`,
113
- channel: "line",
114
- message: body,
115
- ackPolicy: "after_receive_record",
116
- onAck: () => {
117
- res.statusCode = 200;
118
- res.setHeader("Content-Type", "application/json");
119
- res.end(JSON.stringify({ status: "ok" }));
120
- },
121
- });
122
-
123
- if (receiveContext.shouldAckAfter("receive_record")) {
124
- await receiveContext.ack();
125
- }
126
-
127
- if (body.events && body.events.length > 0) {
128
- logVerbose(`line: received ${body.events.length} webhook events`);
129
- void Promise.resolve()
130
- .then(() => params.bot.handleWebhook(body))
131
- .catch((err) => logLineWebhookDispatchError(params.runtime, err));
132
- }
133
- } catch (err) {
134
- await receiveContext?.nack(err);
135
- if (isRequestBodyLimitError(err, "PAYLOAD_TOO_LARGE")) {
136
- res.statusCode = 413;
137
- res.setHeader("Content-Type", "application/json");
138
- res.end(JSON.stringify({ error: "Payload too large" }));
139
- return;
140
- }
141
- if (isRequestBodyLimitError(err, "REQUEST_BODY_TIMEOUT")) {
142
- res.statusCode = 408;
143
- res.setHeader("Content-Type", "application/json");
144
- res.end(JSON.stringify({ error: requestBodyErrorToText("REQUEST_BODY_TIMEOUT") }));
145
- return;
146
- }
147
- params.runtime.error?.(danger(`line webhook error: ${String(err)}`));
148
- if (!res.headersSent) {
149
- res.statusCode = 500;
150
- res.setHeader("Content-Type", "application/json");
151
- res.end(JSON.stringify({ error: "Internal server error" }));
152
- }
153
- }
154
- };
155
- }
@@ -1,10 +0,0 @@
1
- import type { webhook } from "@line/bot-sdk";
2
- export { validateLineSignature } from "./signature.js";
3
-
4
- export function parseLineWebhookBody(rawBody: string): webhook.CallbackRequest | null {
5
- try {
6
- return JSON.parse(rawBody) as webhook.CallbackRequest;
7
- } catch {
8
- return null;
9
- }
10
- }
package/src/webhook.ts DELETED
@@ -1,135 +0,0 @@
1
- import type { webhook } from "@line/bot-sdk";
2
- import type { NextFunction, Request, Response } from "express";
3
- import {
4
- createMessageReceiveContext,
5
- type MessageReceiveContext,
6
- } from "klaw/plugin-sdk/channel-message";
7
- import { danger, logVerbose, type RuntimeEnv } from "klaw/plugin-sdk/runtime-env";
8
- import { parseLineWebhookBody, validateLineSignature } from "./webhook-utils.js";
9
-
10
- const LINE_WEBHOOK_MAX_RAW_BODY_BYTES = 64 * 1024;
11
-
12
- export interface LineWebhookOptions {
13
- channelSecret: string;
14
- onEvents: (body: webhook.CallbackRequest) => Promise<void>;
15
- runtime?: RuntimeEnv;
16
- }
17
-
18
- function readRawBody(req: Request): string | null {
19
- const rawBody =
20
- (req as { rawBody?: string | Buffer }).rawBody ??
21
- (typeof req.body === "string" || Buffer.isBuffer(req.body) ? req.body : null);
22
- if (!rawBody) {
23
- return null;
24
- }
25
- return Buffer.isBuffer(rawBody) ? rawBody.toString("utf-8") : rawBody;
26
- }
27
-
28
- function parseWebhookBody(rawBody?: string | null): webhook.CallbackRequest | null {
29
- if (!rawBody) {
30
- return null;
31
- }
32
- return parseLineWebhookBody(rawBody);
33
- }
34
-
35
- function logLineWebhookDispatchError(runtime: RuntimeEnv | undefined, err: unknown): void {
36
- runtime?.error?.(danger(`line webhook dispatch failed: ${String(err)}`));
37
- }
38
-
39
- export function createLineWebhookMiddleware(
40
- options: LineWebhookOptions,
41
- ): (req: Request, res: Response, _next: NextFunction) => Promise<void> {
42
- const { channelSecret, onEvents, runtime } = options;
43
-
44
- return async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
45
- let receiveContext: MessageReceiveContext<webhook.CallbackRequest> | undefined;
46
- try {
47
- const signature = req.headers["x-line-signature"];
48
-
49
- if (!signature || typeof signature !== "string") {
50
- res.status(400).json({ error: "Missing X-Line-Signature header" });
51
- return;
52
- }
53
-
54
- const rawBody = readRawBody(req);
55
-
56
- if (!rawBody) {
57
- res.status(400).json({ error: "Missing raw request body for signature verification" });
58
- return;
59
- }
60
- if (Buffer.byteLength(rawBody, "utf-8") > LINE_WEBHOOK_MAX_RAW_BODY_BYTES) {
61
- res.status(413).json({ error: "Payload too large" });
62
- return;
63
- }
64
-
65
- if (!validateLineSignature(rawBody, signature, channelSecret)) {
66
- logVerbose("line: webhook signature validation failed");
67
- res.status(401).json({ error: "Invalid signature" });
68
- return;
69
- }
70
-
71
- const body = parseWebhookBody(rawBody);
72
-
73
- if (!body) {
74
- res.status(400).json({ error: "Invalid webhook payload" });
75
- return;
76
- }
77
-
78
- receiveContext = createMessageReceiveContext({
79
- id: `${Date.now()}:line:webhook`,
80
- channel: "line",
81
- message: body,
82
- ackPolicy: "after_receive_record",
83
- onAck: () => {
84
- res.status(200).json({ status: "ok" });
85
- },
86
- });
87
-
88
- if (receiveContext.shouldAckAfter("receive_record")) {
89
- await receiveContext.ack();
90
- }
91
-
92
- if (body.events && body.events.length > 0) {
93
- logVerbose(`line: received ${body.events.length} webhook events`);
94
- void Promise.resolve()
95
- .then(() => onEvents(body))
96
- .catch((err) => logLineWebhookDispatchError(runtime, err));
97
- }
98
- } catch (err) {
99
- await receiveContext?.nack(err);
100
- runtime?.error?.(danger(`line webhook error: ${String(err)}`));
101
- if (!res.headersSent) {
102
- res.status(500).json({ error: "Internal server error" });
103
- }
104
- }
105
- };
106
- }
107
-
108
- export interface StartLineWebhookOptions {
109
- channelSecret: string;
110
- onEvents: (body: webhook.CallbackRequest) => Promise<void>;
111
- runtime?: RuntimeEnv;
112
- path?: string;
113
- }
114
-
115
- export function startLineWebhook(options: StartLineWebhookOptions): {
116
- path: string;
117
- handler: (req: Request, res: Response, _next: NextFunction) => Promise<void>;
118
- } {
119
- const channelSecret =
120
- typeof options.channelSecret === "string" ? options.channelSecret.trim() : "";
121
- if (!channelSecret) {
122
- throw new Error(
123
- "LINE webhook mode requires a non-empty channel secret. " +
124
- "Set channels.line.channelSecret in your config.",
125
- );
126
- }
127
- const path = options.path ?? "/line/webhook";
128
- const middleware = createLineWebhookMiddleware({
129
- channelSecret,
130
- onEvents: options.onEvents,
131
- runtime: options.runtime,
132
- });
133
-
134
- return { path, handler: middleware };
135
- }
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "extends": "../tsconfig.package-boundary.base.json",
3
- "compilerOptions": {
4
- "rootDir": "."
5
- },
6
- "include": ["./*.ts", "./src/**/*.ts"],
7
- "exclude": [
8
- "./**/*.test.ts",
9
- "./dist/**",
10
- "./node_modules/**",
11
- "./src/test-support/**",
12
- "./src/**/*test-helpers.ts",
13
- "./src/**/*test-harness.ts",
14
- "./src/**/*test-support.ts"
15
- ]
16
- }