@im-more/dsh-line 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +12 -0
- package/lib/index.js +1701 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +55 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +169 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +47 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +77 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +60 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +321 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +47 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +112 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +17 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +24 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +17 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +16 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/media.d.ts +19 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +55 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/shared.d.ts +126 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +316 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/modes/text.d.ts +13 -0
- package/lib/types/modes/text.d.ts.map +1 -0
- package/lib/types/modes/text.js +143 -0
- package/lib/types/modes/text.js.map +1 -0
- package/lib/types/request.d.ts +49 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +162 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +43 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +74 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/signature.d.ts +19 -0
- package/lib/types/signature.d.ts.map +1 -0
- package/lib/types/signature.js +30 -0
- package/lib/types/signature.js.map +1 -0
- package/lib/types/transport/media.d.ts +62 -0
- package/lib/types/transport/media.d.ts.map +1 -0
- package/lib/types/transport/media.js +104 -0
- package/lib/types/transport/media.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +62 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +208 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types/definition.d.ts +1006 -0
- package/lib/types/types/definition.d.ts.map +1 -0
- package/lib/types/types/definition.js +2 -0
- package/lib/types/types/definition.js.map +1 -0
- package/lib/types/types/index.d.ts +542 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +4 -0
- package/lib/types/types/index.js.map +1 -0
- package/lib/types/types/internal.d.ts +8 -0
- package/lib/types/types/internal.d.ts.map +1 -0
- package/lib/types/types/internal.js +234 -0
- package/lib/types/types/internal.js.map +1 -0
- package/lib/types/types/request.d.ts +341 -0
- package/lib/types/types/request.d.ts.map +1 -0
- package/lib/types/types/request.js +2 -0
- package/lib/types/types/request.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +229 -0
- package/src/config.ts +113 -0
- package/src/decode.ts +418 -0
- package/src/errors.ts +138 -0
- package/src/identity.ts +28 -0
- package/src/index.ts +60 -0
- package/src/media.ts +66 -0
- package/src/modes/shared.ts +408 -0
- package/src/modes/text.ts +177 -0
- package/src/request.ts +215 -0
- package/src/send.ts +104 -0
- package/src/signature.ts +32 -0
- package/src/transport/media.ts +117 -0
- package/src/transport/webhook.ts +266 -0
- package/src/types/.eslintrc.yml +2 -0
- package/src/types/definition.ts +1008 -0
- package/src/types/index.ts +593 -0
- package/src/types/internal.ts +233 -0
- package/src/types/request.ts +341 -0
- package/README.md +0 -3
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The webhook transport.
|
|
3
|
+
*
|
|
4
|
+
* LINE has no long connection, polling or gateway: the only inbound path is an HTTPS POST to a URL configured in the
|
|
5
|
+
* console. The route is registered on the profile's `webServer`, which owns the listener and the public address; the
|
|
6
|
+
* adapter keeps the handler anonymous and authenticates every request with the channel secret.
|
|
7
|
+
*
|
|
8
|
+
* The handler answers `200` only after intake stored the events, so a retrying platform can resend a failed delivery,
|
|
9
|
+
* and it answers every refusal before any work is done.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
13
|
+
import type { LineBot } from '../bot.ts'
|
|
14
|
+
import { publicBase, routePath } from '../config.ts'
|
|
15
|
+
import { decodeEvent } from '../decode.ts'
|
|
16
|
+
import { matchesSignature, signatureOf } from '../signature.ts'
|
|
17
|
+
import type {
|
|
18
|
+
EventSource,
|
|
19
|
+
WebhookEvent,
|
|
20
|
+
WebhookRequestBody,
|
|
21
|
+
} from '../types/index.ts'
|
|
22
|
+
|
|
23
|
+
/** The part of `ctx.webServer` this adapter registers on. */
|
|
24
|
+
export interface WebServerLike {
|
|
25
|
+
register(route: WebHandlerRoute | WebResourceRoute): () => void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** One exact path answered by a handler that owns the whole response. */
|
|
29
|
+
export interface WebHandlerRoute {
|
|
30
|
+
kind: 'exact'
|
|
31
|
+
path: string
|
|
32
|
+
auth: 'anonymous'
|
|
33
|
+
handler: (
|
|
34
|
+
request: IncomingMessage,
|
|
35
|
+
response: ServerResponse,
|
|
36
|
+
) => void | Promise<void>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** One path prefix answered by a Fetch resource. */
|
|
40
|
+
export interface WebResourceRoute {
|
|
41
|
+
kind: 'prefix'
|
|
42
|
+
path: string
|
|
43
|
+
auth: 'anonymous'
|
|
44
|
+
resource: (request: Request) => Promise<Response>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Header LINE signs the request with. */
|
|
48
|
+
const SIGNATURE_HEADER = 'x-line-signature'
|
|
49
|
+
|
|
50
|
+
/** Message of any caught value, for a log line. */
|
|
51
|
+
function messageOf(error: unknown): string {
|
|
52
|
+
return error instanceof Error ? error.message : String(error)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One header of an inbound request, when it arrived exactly once. */
|
|
56
|
+
function headerOf(request: IncomingMessage, name: string): string | undefined {
|
|
57
|
+
const value = request.headers[name]
|
|
58
|
+
return typeof value === 'string' ? value : undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Read a request body to its end. LINE publishes no body-size limit, so the adapter imposes none. */
|
|
62
|
+
async function readBody(request: IncomingMessage): Promise<string> {
|
|
63
|
+
const chunks: Buffer[] = []
|
|
64
|
+
for await (const chunk of request) {
|
|
65
|
+
chunks.push(
|
|
66
|
+
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as Uint8Array),
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
return Buffer.concat(chunks).toString('utf8')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Write one bodiless answer. */
|
|
73
|
+
function writeStatus(
|
|
74
|
+
response: ServerResponse,
|
|
75
|
+
status: number,
|
|
76
|
+
headers: Record<string, string> = {},
|
|
77
|
+
): void {
|
|
78
|
+
response.writeHead(status, headers)
|
|
79
|
+
response.end()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** One event source, when the payload names a conversation kind this adapter knows. */
|
|
83
|
+
function isEventSource(value: unknown): value is EventSource {
|
|
84
|
+
if (typeof value !== 'object' || value === null) return false
|
|
85
|
+
const record = value as Record<string, unknown>
|
|
86
|
+
switch (record.type) {
|
|
87
|
+
case 'user':
|
|
88
|
+
return typeof record.userId === 'string'
|
|
89
|
+
case 'group':
|
|
90
|
+
return typeof record.groupId === 'string'
|
|
91
|
+
case 'room':
|
|
92
|
+
return typeof record.roomId === 'string'
|
|
93
|
+
default:
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** One event envelope, as far as decoding needs it: a type, a channel mode, a time and a source. */
|
|
99
|
+
function isWebhookEvent(value: unknown): value is WebhookEvent {
|
|
100
|
+
if (typeof value !== 'object' || value === null) return false
|
|
101
|
+
const record = value as Record<string, unknown>
|
|
102
|
+
return (
|
|
103
|
+
typeof record.type === 'string' &&
|
|
104
|
+
(record.mode === 'active' || record.mode === 'standby') &&
|
|
105
|
+
typeof record.timestamp === 'number' &&
|
|
106
|
+
isEventSource(record.source)
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The parsed webhook body, when its envelope is complete.
|
|
112
|
+
*
|
|
113
|
+
* @param value - the parsed JSON value.
|
|
114
|
+
* @returns the typed body, or undefined when the value is not a webhook body.
|
|
115
|
+
*/
|
|
116
|
+
function parseWebhookBody(value: unknown): WebhookRequestBody | undefined {
|
|
117
|
+
if (typeof value !== 'object' || value === null) return undefined
|
|
118
|
+
const record = value as Record<string, unknown>
|
|
119
|
+
if (typeof record.destination !== 'string') return undefined
|
|
120
|
+
if (!Array.isArray(record.events)) return undefined
|
|
121
|
+
const events: WebhookEvent[] = []
|
|
122
|
+
for (const item of record.events) {
|
|
123
|
+
if (!isWebhookEvent(item)) return undefined
|
|
124
|
+
events.push(item)
|
|
125
|
+
}
|
|
126
|
+
return { destination: record.destination, events }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The webhook route.
|
|
131
|
+
*
|
|
132
|
+
* `start` registers the route and `announce` points the channel at it, so the media route of the same connect is in
|
|
133
|
+
* place before the channel starts delivering anything.
|
|
134
|
+
*/
|
|
135
|
+
export class LineWebhook {
|
|
136
|
+
private dispose: (() => void) | undefined
|
|
137
|
+
|
|
138
|
+
constructor(
|
|
139
|
+
private readonly bot: LineBot,
|
|
140
|
+
private readonly webServer: WebServerLike,
|
|
141
|
+
) {}
|
|
142
|
+
|
|
143
|
+
/** Register the route. */
|
|
144
|
+
start(): void {
|
|
145
|
+
const path = routePath(this.bot.config.webhook.path)
|
|
146
|
+
this.dispose = this.webServer.register({
|
|
147
|
+
kind: 'exact',
|
|
148
|
+
path,
|
|
149
|
+
auth: 'anonymous',
|
|
150
|
+
handler: (request, response) => this.handle(request, response),
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Point the channel at the registered route and read the endpoint state back.
|
|
156
|
+
*
|
|
157
|
+
* A profile that cannot receive anything fails at connect with an actionable message instead of silently getting no
|
|
158
|
+
* events; the route is released on that failure so a half-configured connect leaves nothing behind.
|
|
159
|
+
*/
|
|
160
|
+
async announce(): Promise<void> {
|
|
161
|
+
try {
|
|
162
|
+
await this.bot.internal.setWebhookEndpoint({
|
|
163
|
+
endpoint: `${publicBase(this.bot.config.publicUrl)}${routePath(this.bot.config.webhook.path)}`,
|
|
164
|
+
})
|
|
165
|
+
} catch (error) {
|
|
166
|
+
this.stop()
|
|
167
|
+
this.bot.ctx.logger.error(
|
|
168
|
+
'line: setWebhookEndpoint failed: %s',
|
|
169
|
+
messageOf(error),
|
|
170
|
+
)
|
|
171
|
+
throw error
|
|
172
|
+
}
|
|
173
|
+
await this.readBackEndpoint()
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Release the route. The channel keeps its endpoint: clearing it would point LINE at a 404. */
|
|
177
|
+
stop(): void {
|
|
178
|
+
this.dispose?.()
|
|
179
|
+
this.dispose = undefined
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Read the endpoint state back after registering it.
|
|
184
|
+
*
|
|
185
|
+
* A channel whose "Use webhook" switch is off answers `active: false`; the console takes up to a minute to apply the
|
|
186
|
+
* new endpoint, so this only warns and never fails the connect.
|
|
187
|
+
*/
|
|
188
|
+
private async readBackEndpoint(): Promise<void> {
|
|
189
|
+
try {
|
|
190
|
+
const state = await this.bot.internal.getWebhookEndpoint()
|
|
191
|
+
if (!state.active) {
|
|
192
|
+
this.bot.ctx.logger.warn(
|
|
193
|
+
'line: the webhook endpoint is not active; turn on "Use webhook" in the LINE console',
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
this.bot.ctx.logger.warn(
|
|
198
|
+
'line: the webhook endpoint could not be read back: %s',
|
|
199
|
+
messageOf(error),
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private async handle(
|
|
205
|
+
request: IncomingMessage,
|
|
206
|
+
response: ServerResponse,
|
|
207
|
+
): Promise<void> {
|
|
208
|
+
try {
|
|
209
|
+
if (request.method !== 'POST') {
|
|
210
|
+
writeStatus(response, 405, { allow: 'POST' })
|
|
211
|
+
return
|
|
212
|
+
}
|
|
213
|
+
// A request without a signature is not from LINE; there is no reason to read its body.
|
|
214
|
+
const received = headerOf(request, SIGNATURE_HEADER)
|
|
215
|
+
if (received === undefined) {
|
|
216
|
+
writeStatus(response, 401)
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
const body = await readBody(request)
|
|
220
|
+
// The signature covers the body as it arrived, so it is verified before the body is interpreted.
|
|
221
|
+
if (
|
|
222
|
+
!matchesSignature(signatureOf(this.bot.config.secret, body), received)
|
|
223
|
+
) {
|
|
224
|
+
writeStatus(response, 401)
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
let parsed: unknown
|
|
228
|
+
try {
|
|
229
|
+
parsed = JSON.parse(body)
|
|
230
|
+
} catch (error) {
|
|
231
|
+
this.bot.ctx.logger.warn(
|
|
232
|
+
'line: cannot parse a webhook request: %s',
|
|
233
|
+
messageOf(error),
|
|
234
|
+
)
|
|
235
|
+
writeStatus(response, 400)
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
const payload = parseWebhookBody(parsed)
|
|
239
|
+
if (payload === undefined) {
|
|
240
|
+
writeStatus(response, 400)
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
if (payload.destination !== this.bot.selfId) {
|
|
244
|
+
writeStatus(response, 403)
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
if (payload.events.length === 0) {
|
|
248
|
+
// The console sends an eventless body to probe the endpoint; it has to answer 200.
|
|
249
|
+
writeStatus(response, 200)
|
|
250
|
+
return
|
|
251
|
+
}
|
|
252
|
+
for (const event of payload.events) {
|
|
253
|
+
const session = decodeEvent(this.bot, event)
|
|
254
|
+
if (session !== undefined) await this.bot.receive(session)
|
|
255
|
+
}
|
|
256
|
+
writeStatus(response, 200)
|
|
257
|
+
} catch (error) {
|
|
258
|
+
this.bot.ctx.logger.warn(
|
|
259
|
+
'line: webhook delivery failed: %s',
|
|
260
|
+
messageOf(error),
|
|
261
|
+
)
|
|
262
|
+
if (!response.headersSent) writeStatus(response, 500)
|
|
263
|
+
else response.end()
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|