UNPKG

3.56 kBJavaScriptView Raw
1"use strict";
2var __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};
9Object.defineProperty(exports, "__esModule", { value: true });
10const exceptions_1 = require("./exceptions");
11const Types = require("./types");
12const validate_signature_1 = require("./validate-signature");
13function isValidBody(body) {
14 return (body && typeof body === "string") || Buffer.isBuffer(body);
15}
16const readRequestBody = async (req) => {
17 var _a, e_1, _b, _c;
18 const chunks = [];
19 try {
20 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) {
21 _c = req_1_1.value;
22 _d = false;
23 const chunk = _c;
24 chunks.push(chunk);
25 }
26 }
27 catch (e_1_1) { e_1 = { error: e_1_1 }; }
28 finally {
29 try {
30 if (!_d && !_a && (_b = req_1.return)) await _b.call(req_1);
31 }
32 finally { if (e_1) throw e_1.error; }
33 }
34 return Buffer.concat(chunks);
35};
36function middleware(config) {
37 if (!config.channelSecret) {
38 throw new Error("no channel secret");
39 }
40 const secret = config.channelSecret;
41 const _middleware = async (req, res, next) => {
42 // header names are lower-cased
43 // https://nodejs.org/api/http.html#http_message_headers
44 const signature = req.headers[Types.LINE_SIGNATURE_HTTP_HEADER_NAME];
45 if (!signature) {
46 next(new exceptions_1.SignatureValidationFailed("no signature"));
47 return;
48 }
49 const body = await (async () => {
50 if (isValidBody(req.rawBody)) {
51 // rawBody is provided in Google Cloud Functions and others
52 return req.rawBody;
53 }
54 else if (isValidBody(req.body)) {
55 return req.body;
56 }
57 else {
58 // body may not be parsed yet, parse it to a buffer
59 const rawBody = await readRequestBody(req);
60 if (isValidBody(rawBody)) {
61 return rawBody;
62 }
63 else {
64 throw new exceptions_1.JSONParseError("Invalid body", { raw: rawBody });
65 }
66 }
67 })();
68 if (!(0, validate_signature_1.default)(body, secret, signature)) {
69 next(new exceptions_1.SignatureValidationFailed("signature validation failed", {
70 signature,
71 }));
72 return;
73 }
74 const strBody = Buffer.isBuffer(body) ? body.toString() : body;
75 try {
76 req.body = JSON.parse(strBody);
77 next();
78 }
79 catch (err) {
80 const { message } = err;
81 next(new exceptions_1.JSONParseError(message, { raw: strBody }));
82 }
83 };
84 return (req, res, next) => {
85 _middleware(req, res, next).catch(next);
86 };
87}
88exports.default = middleware;