@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 @@
|
|
|
1
|
+
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../../src/transport/media.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAExC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,6BAA6B;AAC7B,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,UAAU,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;;;GAMG;AACH,qBAAa,YAAY;IAIrB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAJzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiC;gBAGtC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM;IAGjC;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM;IAOjC;;;;;OAKG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS;IAI3C,6EAA6E;IAC7E,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF;AAED;;;;;GAKG;AACH,qBAAa,cAAc;IAIvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAJ5B,OAAO,CAAC,OAAO,CAA0B;gBAGtB,GAAG,EAAE,OAAO,EACZ,SAAS,EAAE,aAAa;IAG3C,iCAAiC;IACjC,KAAK,IAAI,IAAI;IAUb,yBAAyB;IACzB,IAAI,IAAI,IAAI;YAKE,KAAK;CAqBpB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound media: the public route LINE fetches staged bytes from.
|
|
3
|
+
*
|
|
4
|
+
* The Messaging API has no endpoint that uploads message media; an image, video or audio message carries only HTTPS URLs
|
|
5
|
+
* that the LINE servers fetch themselves. The adapter therefore stages the bytes of every non-HTTPS source in memory
|
|
6
|
+
* and serves them from an anonymous, unguessable URL for the lifetime of the process.
|
|
7
|
+
*/
|
|
8
|
+
import { randomBytes } from 'node:crypto';
|
|
9
|
+
import { routePath } from "../config.js";
|
|
10
|
+
/**
|
|
11
|
+
* Staged bytes by opaque token.
|
|
12
|
+
*
|
|
13
|
+
* Entries live until the process exits: the platform may fetch a URL minutes after a send, and the W1 rule forbids
|
|
14
|
+
* inventing a TTL or a byte cap the platform does not document. The `prepare` cache means the same media is staged
|
|
15
|
+
* once, not once per send.
|
|
16
|
+
*/
|
|
17
|
+
export class MediaStaging {
|
|
18
|
+
base;
|
|
19
|
+
prefix;
|
|
20
|
+
entries = new Map();
|
|
21
|
+
constructor(base, prefix) {
|
|
22
|
+
this.base = base;
|
|
23
|
+
this.prefix = prefix;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Stage one attachment.
|
|
27
|
+
*
|
|
28
|
+
* @param media - the bytes, their MIME type and name.
|
|
29
|
+
* @returns the public URL LINE fetches the bytes from.
|
|
30
|
+
*/
|
|
31
|
+
stage(media) {
|
|
32
|
+
// 128 random bits: the route is anonymous, so the URL is the only thing keeping unrelated readers out.
|
|
33
|
+
const token = randomBytes(16).toString('base64url');
|
|
34
|
+
this.entries.set(token, media);
|
|
35
|
+
return `${this.base}${this.prefix}/${token}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Read staged bytes back.
|
|
39
|
+
*
|
|
40
|
+
* @param token - the last path segment of a URL returned by {@link stage}.
|
|
41
|
+
* @returns the staged attachment, or undefined for an unknown token.
|
|
42
|
+
*/
|
|
43
|
+
get(token) {
|
|
44
|
+
return this.entries.get(token);
|
|
45
|
+
}
|
|
46
|
+
/** The number of attachments currently staged, for tests and diagnostics. */
|
|
47
|
+
get size() {
|
|
48
|
+
return this.entries.size;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The public media route.
|
|
53
|
+
*
|
|
54
|
+
* It is registered as a `prefix` resource, so the web server hands it a Fetch `Request` and takes the `Response` back;
|
|
55
|
+
* the route is anonymous because LINE's servers carry no DSH peer credential.
|
|
56
|
+
*/
|
|
57
|
+
export class LineMediaRoute {
|
|
58
|
+
bot;
|
|
59
|
+
webServer;
|
|
60
|
+
dispose;
|
|
61
|
+
constructor(bot, webServer) {
|
|
62
|
+
this.bot = bot;
|
|
63
|
+
this.webServer = webServer;
|
|
64
|
+
}
|
|
65
|
+
/** Register the prefix route. */
|
|
66
|
+
start() {
|
|
67
|
+
const path = routePath(this.bot.config.media.path);
|
|
68
|
+
this.dispose = this.webServer.register({
|
|
69
|
+
kind: 'prefix',
|
|
70
|
+
path,
|
|
71
|
+
auth: 'anonymous',
|
|
72
|
+
resource: (request) => this.serve(request),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** Release the route. */
|
|
76
|
+
stop() {
|
|
77
|
+
this.dispose?.();
|
|
78
|
+
this.dispose = undefined;
|
|
79
|
+
}
|
|
80
|
+
async serve(request) {
|
|
81
|
+
const prefix = `${routePath(this.bot.config.media.path)}/`;
|
|
82
|
+
const { pathname } = new URL(request.url);
|
|
83
|
+
if (!pathname.startsWith(prefix)) {
|
|
84
|
+
return new Response(null, { status: 404 });
|
|
85
|
+
}
|
|
86
|
+
const segment = pathname.slice(prefix.length);
|
|
87
|
+
let token;
|
|
88
|
+
try {
|
|
89
|
+
token = decodeURIComponent(segment);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// The web server answers a malformed escape before this route runs; a token it still cannot decode names nothing.
|
|
93
|
+
return new Response(null, { status: 404 });
|
|
94
|
+
}
|
|
95
|
+
const media = this.bot.staging.get(token);
|
|
96
|
+
if (media === undefined)
|
|
97
|
+
return new Response(null, { status: 404 });
|
|
98
|
+
const headers = new Headers();
|
|
99
|
+
headers.set('content-type', media.mime ?? 'application/octet-stream');
|
|
100
|
+
headers.set('content-length', String(media.data.byteLength));
|
|
101
|
+
return new Response(media.data, { status: 200, headers });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/transport/media.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAUxC;;;;;;GAMG;AACH,MAAM,OAAO,YAAY;IAIJ;IACA;IAJF,OAAO,GAAG,IAAI,GAAG,EAAuB,CAAA;IAEzD,YACmB,IAAY,EACZ,MAAc;QADd,SAAI,GAAJ,IAAI,CAAQ;QACZ,WAAM,GAAN,MAAM,CAAQ;IAC9B,CAAC;IAEJ;;;;;OAKG;IACH,KAAK,CAAC,KAAkB;QACtB,uGAAuG;QACvG,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA;QACnD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QAC9B,OAAO,GAAG,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK,EAAE,CAAA;IAC9C,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,KAAa;QACf,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IAChC,CAAC;IAED,6EAA6E;IAC7E,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;IAC1B,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,OAAO,cAAc;IAIN;IACA;IAJX,OAAO,CAA0B;IAEzC,YACmB,GAAY,EACZ,SAAwB;QADxB,QAAG,GAAH,GAAG,CAAS;QACZ,cAAS,GAAT,SAAS,CAAe;IACxC,CAAC;IAEJ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I,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QACzC,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;QACnE,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAA;QAC7B,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,IAAI,IAAI,0BAA0B,CAAC,CAAA;QACrE,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAA;QAC5D,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAA;IAC3D,CAAC;CACF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
12
|
+
import type { LineBot } from '../bot.ts';
|
|
13
|
+
/** The part of `ctx.webServer` this adapter registers on. */
|
|
14
|
+
export interface WebServerLike {
|
|
15
|
+
register(route: WebHandlerRoute | WebResourceRoute): () => void;
|
|
16
|
+
}
|
|
17
|
+
/** One exact path answered by a handler that owns the whole response. */
|
|
18
|
+
export interface WebHandlerRoute {
|
|
19
|
+
kind: 'exact';
|
|
20
|
+
path: string;
|
|
21
|
+
auth: 'anonymous';
|
|
22
|
+
handler: (request: IncomingMessage, response: ServerResponse) => void | Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
/** One path prefix answered by a Fetch resource. */
|
|
25
|
+
export interface WebResourceRoute {
|
|
26
|
+
kind: 'prefix';
|
|
27
|
+
path: string;
|
|
28
|
+
auth: 'anonymous';
|
|
29
|
+
resource: (request: Request) => Promise<Response>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The webhook route.
|
|
33
|
+
*
|
|
34
|
+
* `start` registers the route and `announce` points the channel at it, so the media route of the same connect is in
|
|
35
|
+
* place before the channel starts delivering anything.
|
|
36
|
+
*/
|
|
37
|
+
export declare class LineWebhook {
|
|
38
|
+
private readonly bot;
|
|
39
|
+
private readonly webServer;
|
|
40
|
+
private dispose;
|
|
41
|
+
constructor(bot: LineBot, webServer: WebServerLike);
|
|
42
|
+
/** Register the route. */
|
|
43
|
+
start(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Point the channel at the registered route and read the endpoint state back.
|
|
46
|
+
*
|
|
47
|
+
* A profile that cannot receive anything fails at connect with an actionable message instead of silently getting no
|
|
48
|
+
* events; the route is released on that failure so a half-configured connect leaves nothing behind.
|
|
49
|
+
*/
|
|
50
|
+
announce(): Promise<void>;
|
|
51
|
+
/** Release the route. The channel keeps its endpoint: clearing it would point LINE at a 404. */
|
|
52
|
+
stop(): void;
|
|
53
|
+
/**
|
|
54
|
+
* Read the endpoint state back after registering it.
|
|
55
|
+
*
|
|
56
|
+
* A channel whose "Use webhook" switch is off answers `active: false`; the console takes up to a minute to apply the
|
|
57
|
+
* new endpoint, so this only warns and never fails the connect.
|
|
58
|
+
*/
|
|
59
|
+
private readBackEndpoint;
|
|
60
|
+
private handle;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=webhook.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhook.d.ts","sourceRoot":"","sources":["../../../src/transport/webhook.ts"],"names":[],"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ection, 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
|
+
import { publicBase, routePath } from "../config.js";
|
|
12
|
+
import { decodeEvent } from "../decode.js";
|
|
13
|
+
import { matchesSignature, signatureOf } from "../signature.js";
|
|
14
|
+
/** Header LINE signs the request with. */
|
|
15
|
+
const SIGNATURE_HEADER = 'x-line-signature';
|
|
16
|
+
/** Message of any caught value, for a log line. */
|
|
17
|
+
function messageOf(error) {
|
|
18
|
+
return error instanceof Error ? error.message : String(error);
|
|
19
|
+
}
|
|
20
|
+
/** One header of an inbound request, when it arrived exactly once. */
|
|
21
|
+
function headerOf(request, name) {
|
|
22
|
+
const value = request.headers[name];
|
|
23
|
+
return typeof value === 'string' ? value : undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Read a request body to its end. LINE publishes no body-size limit, so the adapter imposes none. */
|
|
26
|
+
async function readBody(request) {
|
|
27
|
+
const chunks = [];
|
|
28
|
+
for await (const chunk of request) {
|
|
29
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
30
|
+
}
|
|
31
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
32
|
+
}
|
|
33
|
+
/** Write one bodiless answer. */
|
|
34
|
+
function writeStatus(response, status, headers = {}) {
|
|
35
|
+
response.writeHead(status, headers);
|
|
36
|
+
response.end();
|
|
37
|
+
}
|
|
38
|
+
/** One event source, when the payload names a conversation kind this adapter knows. */
|
|
39
|
+
function isEventSource(value) {
|
|
40
|
+
if (typeof value !== 'object' || value === null)
|
|
41
|
+
return false;
|
|
42
|
+
const record = value;
|
|
43
|
+
switch (record.type) {
|
|
44
|
+
case 'user':
|
|
45
|
+
return typeof record.userId === 'string';
|
|
46
|
+
case 'group':
|
|
47
|
+
return typeof record.groupId === 'string';
|
|
48
|
+
case 'room':
|
|
49
|
+
return typeof record.roomId === 'string';
|
|
50
|
+
default:
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** One event envelope, as far as decoding needs it: a type, a channel mode, a time and a source. */
|
|
55
|
+
function isWebhookEvent(value) {
|
|
56
|
+
if (typeof value !== 'object' || value === null)
|
|
57
|
+
return false;
|
|
58
|
+
const record = value;
|
|
59
|
+
return (typeof record.type === 'string' &&
|
|
60
|
+
(record.mode === 'active' || record.mode === 'standby') &&
|
|
61
|
+
typeof record.timestamp === 'number' &&
|
|
62
|
+
isEventSource(record.source));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The parsed webhook body, when its envelope is complete.
|
|
66
|
+
*
|
|
67
|
+
* @param value - the parsed JSON value.
|
|
68
|
+
* @returns the typed body, or undefined when the value is not a webhook body.
|
|
69
|
+
*/
|
|
70
|
+
function parseWebhookBody(value) {
|
|
71
|
+
if (typeof value !== 'object' || value === null)
|
|
72
|
+
return undefined;
|
|
73
|
+
const record = value;
|
|
74
|
+
if (typeof record.destination !== 'string')
|
|
75
|
+
return undefined;
|
|
76
|
+
if (!Array.isArray(record.events))
|
|
77
|
+
return undefined;
|
|
78
|
+
const events = [];
|
|
79
|
+
for (const item of record.events) {
|
|
80
|
+
if (!isWebhookEvent(item))
|
|
81
|
+
return undefined;
|
|
82
|
+
events.push(item);
|
|
83
|
+
}
|
|
84
|
+
return { destination: record.destination, events };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The webhook route.
|
|
88
|
+
*
|
|
89
|
+
* `start` registers the route and `announce` points the channel at it, so the media route of the same connect is in
|
|
90
|
+
* place before the channel starts delivering anything.
|
|
91
|
+
*/
|
|
92
|
+
export class LineWebhook {
|
|
93
|
+
bot;
|
|
94
|
+
webServer;
|
|
95
|
+
dispose;
|
|
96
|
+
constructor(bot, webServer) {
|
|
97
|
+
this.bot = bot;
|
|
98
|
+
this.webServer = webServer;
|
|
99
|
+
}
|
|
100
|
+
/** Register the route. */
|
|
101
|
+
start() {
|
|
102
|
+
const path = routePath(this.bot.config.webhook.path);
|
|
103
|
+
this.dispose = this.webServer.register({
|
|
104
|
+
kind: 'exact',
|
|
105
|
+
path,
|
|
106
|
+
auth: 'anonymous',
|
|
107
|
+
handler: (request, response) => this.handle(request, response),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Point the channel at the registered route and read the endpoint state back.
|
|
112
|
+
*
|
|
113
|
+
* A profile that cannot receive anything fails at connect with an actionable message instead of silently getting no
|
|
114
|
+
* events; the route is released on that failure so a half-configured connect leaves nothing behind.
|
|
115
|
+
*/
|
|
116
|
+
async announce() {
|
|
117
|
+
try {
|
|
118
|
+
await this.bot.internal.setWebhookEndpoint({
|
|
119
|
+
endpoint: `${publicBase(this.bot.config.publicUrl)}${routePath(this.bot.config.webhook.path)}`,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
this.stop();
|
|
124
|
+
this.bot.ctx.logger.error('line: setWebhookEndpoint failed: %s', messageOf(error));
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
await this.readBackEndpoint();
|
|
128
|
+
}
|
|
129
|
+
/** Release the route. The channel keeps its endpoint: clearing it would point LINE at a 404. */
|
|
130
|
+
stop() {
|
|
131
|
+
this.dispose?.();
|
|
132
|
+
this.dispose = undefined;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Read the endpoint state back after registering it.
|
|
136
|
+
*
|
|
137
|
+
* A channel whose "Use webhook" switch is off answers `active: false`; the console takes up to a minute to apply the
|
|
138
|
+
* new endpoint, so this only warns and never fails the connect.
|
|
139
|
+
*/
|
|
140
|
+
async readBackEndpoint() {
|
|
141
|
+
try {
|
|
142
|
+
const state = await this.bot.internal.getWebhookEndpoint();
|
|
143
|
+
if (!state.active) {
|
|
144
|
+
this.bot.ctx.logger.warn('line: the webhook endpoint is not active; turn on "Use webhook" in the LINE console');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
this.bot.ctx.logger.warn('line: the webhook endpoint could not be read back: %s', messageOf(error));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async handle(request, response) {
|
|
152
|
+
try {
|
|
153
|
+
if (request.method !== 'POST') {
|
|
154
|
+
writeStatus(response, 405, { allow: 'POST' });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
// A request without a signature is not from LINE; there is no reason to read its body.
|
|
158
|
+
const received = headerOf(request, SIGNATURE_HEADER);
|
|
159
|
+
if (received === undefined) {
|
|
160
|
+
writeStatus(response, 401);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const body = await readBody(request);
|
|
164
|
+
// The signature covers the body as it arrived, so it is verified before the body is interpreted.
|
|
165
|
+
if (!matchesSignature(signatureOf(this.bot.config.secret, body), received)) {
|
|
166
|
+
writeStatus(response, 401);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
let parsed;
|
|
170
|
+
try {
|
|
171
|
+
parsed = JSON.parse(body);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
this.bot.ctx.logger.warn('line: cannot parse a webhook request: %s', messageOf(error));
|
|
175
|
+
writeStatus(response, 400);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const payload = parseWebhookBody(parsed);
|
|
179
|
+
if (payload === undefined) {
|
|
180
|
+
writeStatus(response, 400);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (payload.destination !== this.bot.selfId) {
|
|
184
|
+
writeStatus(response, 403);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (payload.events.length === 0) {
|
|
188
|
+
// The console sends an eventless body to probe the endpoint; it has to answer 200.
|
|
189
|
+
writeStatus(response, 200);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
for (const event of payload.events) {
|
|
193
|
+
const session = decodeEvent(this.bot, event);
|
|
194
|
+
if (session !== undefined)
|
|
195
|
+
await this.bot.receive(session);
|
|
196
|
+
}
|
|
197
|
+
writeStatus(response, 200);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
this.bot.ctx.logger.warn('line: webhook delivery failed: %s', messageOf(error));
|
|
201
|
+
if (!response.headersSent)
|
|
202
|
+
writeStatus(response, 500);
|
|
203
|
+
else
|
|
204
|
+
response.end();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=webhook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhook.js","sourceRoot":"","sources":["../../../src/transport/webhook.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC1C,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AA+B/D,0CAA0C;AAC1C,MAAM,gBAAgB,GAAG,kBAAkB,CAAA;AAE3C,mDAAmD;AACnD,SAAS,SAAS,CAAC,KAAc;IAC/B,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC/D,CAAC;AAED,sEAAsE;AACtE,SAAS,QAAQ,CAAC,OAAwB,EAAE,IAAY;IACtD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACtD,CAAC;AAED,sGAAs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