1 | "use strict";
|
2 | var __asyncValues = (this && this.__asyncValues) || function (o) {
|
3 | if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
4 | var m = o[Symbol.asyncIterator], i;
|
5 | return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
6 | function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
7 | function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
8 | };
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.default = middleware;
|
11 | const node_buffer_1 = require("node:buffer");
|
12 | const exceptions_js_1 = require("./exceptions.js");
|
13 | const Types = require("./types.js");
|
14 | const validate_signature_js_1 = require("./validate-signature.js");
|
15 | function isValidBody(body) {
|
16 | return (body && typeof body === "string") || node_buffer_1.Buffer.isBuffer(body);
|
17 | }
|
18 | const readRequestBody = async (req) => {
|
19 | var _a, e_1, _b, _c;
|
20 | const chunks = [];
|
21 | try {
|
22 | for (var _d = true, req_1 = __asyncValues(req), req_1_1; req_1_1 = await req_1.next(), _a = req_1_1.done, !_a; _d = true) {
|
23 | _c = req_1_1.value;
|
24 | _d = false;
|
25 | const chunk = _c;
|
26 | chunks.push(chunk);
|
27 | }
|
28 | }
|
29 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
30 | finally {
|
31 | try {
|
32 | if (!_d && !_a && (_b = req_1.return)) await _b.call(req_1);
|
33 | }
|
34 | finally { if (e_1) throw e_1.error; }
|
35 | }
|
36 | return node_buffer_1.Buffer.concat(chunks);
|
37 | };
|
38 | function middleware(config) {
|
39 | if (!config.channelSecret) {
|
40 | throw new Error("no channel secret");
|
41 | }
|
42 | const secret = config.channelSecret;
|
43 | const _middleware = async (req, res, next) => {
|
44 |
|
45 |
|
46 | const signature = req.headers[Types.LINE_SIGNATURE_HTTP_HEADER_NAME];
|
47 | if (!signature) {
|
48 | next(new exceptions_js_1.SignatureValidationFailed("no signature"));
|
49 | return;
|
50 | }
|
51 | const body = await (async () => {
|
52 | if (isValidBody(req.rawBody)) {
|
53 |
|
54 | return req.rawBody;
|
55 | }
|
56 | else if (isValidBody(req.body)) {
|
57 | return req.body;
|
58 | }
|
59 | else {
|
60 |
|
61 | const rawBody = await readRequestBody(req);
|
62 | if (isValidBody(rawBody)) {
|
63 | return rawBody;
|
64 | }
|
65 | else {
|
66 | throw new exceptions_js_1.JSONParseError("Invalid body", { raw: rawBody });
|
67 | }
|
68 | }
|
69 | })();
|
70 | if (!(0, validate_signature_js_1.default)(body, secret, signature)) {
|
71 | next(new exceptions_js_1.SignatureValidationFailed("signature validation failed", {
|
72 | signature,
|
73 | }));
|
74 | return;
|
75 | }
|
76 | const strBody = node_buffer_1.Buffer.isBuffer(body) ? body.toString() : body;
|
77 | try {
|
78 | req.body = JSON.parse(strBody);
|
79 | next();
|
80 | }
|
81 | catch (err) {
|
82 | const { message } = err;
|
83 | next(new exceptions_js_1.JSONParseError(message, { raw: strBody }));
|
84 | }
|
85 | };
|
86 | return (req, res, next) => {
|
87 | _middleware(req, res, next).catch(next);
|
88 | };
|
89 | }
|
90 |
|
\ | No newline at end of file |