UNPKG

2.35 kBJavaScriptView Raw
1import { Buffer } from "node:buffer";
2import { JSONParseError, SignatureValidationFailed } from "./exceptions.js";
3import * as Types from "./types.js";
4import validateSignature from "./validate-signature.js";
5function isValidBody(body) {
6 return (body && typeof body === "string") || Buffer.isBuffer(body);
7}
8const readRequestBody = async (req) => {
9 const chunks = [];
10 for await (const chunk of req) {
11 chunks.push(chunk);
12 }
13 return Buffer.concat(chunks);
14};
15export default function middleware(config) {
16 if (!config.channelSecret) {
17 throw new Error("no channel secret");
18 }
19 const secret = config.channelSecret;
20 const _middleware = async (req, res, next) => {
21 // header names are lower-cased
22 // https://nodejs.org/api/http.html#http_message_headers
23 const signature = req.headers[Types.LINE_SIGNATURE_HTTP_HEADER_NAME];
24 if (!signature) {
25 next(new SignatureValidationFailed("no signature"));
26 return;
27 }
28 const body = await (async () => {
29 if (isValidBody(req.rawBody)) {
30 // rawBody is provided in Google Cloud Functions and others
31 return req.rawBody;
32 }
33 else if (isValidBody(req.body)) {
34 return req.body;
35 }
36 else {
37 // body may not be parsed yet, parse it to a buffer
38 const rawBody = await readRequestBody(req);
39 if (isValidBody(rawBody)) {
40 return rawBody;
41 }
42 else {
43 throw new JSONParseError("Invalid body", { raw: rawBody });
44 }
45 }
46 })();
47 if (!validateSignature(body, secret, signature)) {
48 next(new SignatureValidationFailed("signature validation failed", {
49 signature,
50 }));
51 return;
52 }
53 const strBody = Buffer.isBuffer(body) ? body.toString() : body;
54 try {
55 req.body = JSON.parse(strBody);
56 next();
57 }
58 catch (err) {
59 const { message } = err;
60 next(new JSONParseError(message, { raw: strBody }));
61 }
62 };
63 return (req, res, next) => {
64 _middleware(req, res, next).catch(next);
65 };
66}
67//# sourceMappingURL=middleware.js.map
\No newline at end of file