@kodelyth/line 2026.5.39 → 2026.5.42

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 (120) hide show
  1. package/api.ts +11 -0
  2. package/channel-plugin-api.ts +1 -0
  3. package/contract-api.ts +5 -0
  4. package/dist/accounts-CD4A1FE7.js +105 -0
  5. package/dist/api.js +11 -0
  6. package/dist/basic-cards-BISytiSa.js +307 -0
  7. package/dist/card-command-dQBX3fVN.js +240 -0
  8. package/dist/channel-DV5h44-j.js +649 -0
  9. package/dist/channel-plugin-api.js +2 -0
  10. package/dist/channel.runtime-Cc-v3szZ.js +4 -0
  11. package/dist/contract-api.js +2 -0
  12. package/dist/index.js +45 -0
  13. package/dist/markdown-to-line-CC3BU6CC.js +810 -0
  14. package/dist/monitor-Ci8Hg8ay.js +1485 -0
  15. package/dist/monitor.runtime-t6-QvlDB.js +2 -0
  16. package/dist/outbound.runtime-D1CxEvcL.js +2 -0
  17. package/dist/probe-BPSs_A_8.js +30 -0
  18. package/dist/probe.runtime-7u2o9QN5.js +2 -0
  19. package/dist/reply-payload-transform-CDuBzoT4.js +855 -0
  20. package/dist/runtime-api.js +291 -0
  21. package/dist/schedule-cards-D-yZMHDE.js +359 -0
  22. package/dist/secret-contract-api.js +5 -0
  23. package/dist/setup-api.js +2 -0
  24. package/dist/setup-entry.js +11 -0
  25. package/dist/setup-surface-CHfQ6Z4i.js +282 -0
  26. package/index.ts +53 -0
  27. package/klaw.plugin.json +2 -329
  28. package/package.json +4 -4
  29. package/runtime-api.ts +179 -0
  30. package/secret-contract-api.ts +4 -0
  31. package/setup-api.ts +2 -0
  32. package/setup-entry.ts +9 -0
  33. package/src/account-helpers.ts +16 -0
  34. package/src/accounts.test.ts +288 -0
  35. package/src/accounts.ts +187 -0
  36. package/src/actions.ts +61 -0
  37. package/src/auto-reply-delivery.test.ts +253 -0
  38. package/src/auto-reply-delivery.ts +200 -0
  39. package/src/bindings.ts +65 -0
  40. package/src/bot-access.ts +30 -0
  41. package/src/bot-handlers.test.ts +1094 -0
  42. package/src/bot-handlers.ts +620 -0
  43. package/src/bot-message-context.test.ts +420 -0
  44. package/src/bot-message-context.ts +586 -0
  45. package/src/bot.ts +66 -0
  46. package/src/card-command.ts +347 -0
  47. package/src/channel-access-token.ts +14 -0
  48. package/src/channel-api.ts +17 -0
  49. package/src/channel-setup-status.contract.test.ts +70 -0
  50. package/src/channel-shared.ts +48 -0
  51. package/src/channel.logout.test.ts +145 -0
  52. package/src/channel.runtime.ts +3 -0
  53. package/src/channel.sendPayload.test.ts +659 -0
  54. package/src/channel.setup.ts +11 -0
  55. package/src/channel.status.test.ts +63 -0
  56. package/src/channel.ts +155 -0
  57. package/src/config-adapter.ts +29 -0
  58. package/src/config-schema.test.ts +53 -0
  59. package/src/config-schema.ts +81 -0
  60. package/src/download.test.ts +164 -0
  61. package/src/download.ts +34 -0
  62. package/src/flex-templates/basic-cards.ts +395 -0
  63. package/src/flex-templates/common.ts +20 -0
  64. package/src/flex-templates/media-control-cards.ts +555 -0
  65. package/src/flex-templates/message.ts +13 -0
  66. package/src/flex-templates/schedule-cards.ts +467 -0
  67. package/src/flex-templates/types.ts +22 -0
  68. package/src/flex-templates.ts +32 -0
  69. package/src/gateway.ts +129 -0
  70. package/src/group-keys.test.ts +123 -0
  71. package/src/group-keys.ts +65 -0
  72. package/src/group-policy.ts +22 -0
  73. package/src/markdown-to-line.test.ts +348 -0
  74. package/src/markdown-to-line.ts +416 -0
  75. package/src/message-cards.test.ts +204 -0
  76. package/src/monitor-durable.test.ts +57 -0
  77. package/src/monitor-durable.ts +37 -0
  78. package/src/monitor.lifecycle.test.ts +499 -0
  79. package/src/monitor.runtime.ts +1 -0
  80. package/src/monitor.ts +507 -0
  81. package/src/outbound-media.test.ts +194 -0
  82. package/src/outbound-media.ts +120 -0
  83. package/src/outbound.runtime.ts +12 -0
  84. package/src/outbound.ts +427 -0
  85. package/src/probe.contract.test.ts +9 -0
  86. package/src/probe.runtime.ts +1 -0
  87. package/src/probe.ts +34 -0
  88. package/src/quick-reply-fallback.ts +10 -0
  89. package/src/reply-chunks.test.ts +180 -0
  90. package/src/reply-chunks.ts +110 -0
  91. package/src/reply-payload-transform.test.ts +392 -0
  92. package/src/reply-payload-transform.ts +317 -0
  93. package/src/rich-menu.test.ts +315 -0
  94. package/src/rich-menu.ts +326 -0
  95. package/src/runtime.ts +32 -0
  96. package/src/send-receipt.ts +32 -0
  97. package/src/send.test.ts +453 -0
  98. package/src/send.ts +531 -0
  99. package/src/setup-core.ts +149 -0
  100. package/src/setup-runtime-api.ts +9 -0
  101. package/src/setup-surface.test.ts +481 -0
  102. package/src/setup-surface.ts +229 -0
  103. package/src/signature.test.ts +34 -0
  104. package/src/signature.ts +24 -0
  105. package/src/status.ts +37 -0
  106. package/src/template-messages.ts +333 -0
  107. package/src/types.ts +130 -0
  108. package/src/webhook-node.test.ts +598 -0
  109. package/src/webhook-node.ts +155 -0
  110. package/src/webhook-utils.ts +10 -0
  111. package/src/webhook.ts +135 -0
  112. package/tsconfig.json +16 -0
  113. package/api.js +0 -7
  114. package/channel-plugin-api.js +0 -7
  115. package/contract-api.js +0 -7
  116. package/index.js +0 -7
  117. package/runtime-api.js +0 -7
  118. package/secret-contract-api.js +0 -7
  119. package/setup-api.js +0 -7
  120. package/setup-entry.js +0 -7
@@ -0,0 +1,155 @@
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
+ }
@@ -0,0 +1,10 @@
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 ADDED
@@ -0,0 +1,135 @@
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 ADDED
@@ -0,0 +1,16 @@
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
+ }
package/api.js DELETED
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/api.js";
2
- import * as module from "../../../dist/extensions/line/api.js";
3
- let defaultExport = "default" in module ? module.default : module;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/channel-plugin-api.js";
2
- import * as module from "../../../dist/extensions/line/channel-plugin-api.js";
3
- let defaultExport = "default" in module ? module.default : module;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
package/contract-api.js DELETED
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/contract-api.js";
2
- import * as module from "../../../dist/extensions/line/contract-api.js";
3
- let defaultExport = "default" in module ? module.default : module;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
package/index.js DELETED
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/index.js";
2
- import defaultModule from "../../../dist/extensions/line/index.js";
3
- let defaultExport = defaultModule;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
package/runtime-api.js DELETED
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/runtime-api.js";
2
- import * as module from "../../../dist/extensions/line/runtime-api.js";
3
- let defaultExport = "default" in module ? module.default : module;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/secret-contract-api.js";
2
- import * as module from "../../../dist/extensions/line/secret-contract-api.js";
3
- let defaultExport = "default" in module ? module.default : module;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
package/setup-api.js DELETED
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/setup-api.js";
2
- import * as module from "../../../dist/extensions/line/setup-api.js";
3
- let defaultExport = "default" in module ? module.default : module;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };
package/setup-entry.js DELETED
@@ -1,7 +0,0 @@
1
- export * from "../../../dist/extensions/line/setup-entry.js";
2
- import defaultModule from "../../../dist/extensions/line/setup-entry.js";
3
- let defaultExport = defaultModule;
4
- for (let index = 0; index < 4 && defaultExport && typeof defaultExport === "object" && "default" in defaultExport; index += 1) {
5
- defaultExport = defaultExport.default;
6
- }
7
- export { defaultExport as default };