1 | import { Buffer } from "node:buffer";
|
2 | import { JSONParseError, SignatureValidationFailed } from "./exceptions.js";
|
3 | import * as Types from "./types.js";
|
4 | import validateSignature from "./validate-signature.js";
|
5 | function isValidBody(body) {
|
6 | return (body && typeof body === "string") || Buffer.isBuffer(body);
|
7 | }
|
8 | const readRequestBody = async (req) => {
|
9 | const chunks = [];
|
10 | for await (const chunk of req) {
|
11 | chunks.push(chunk);
|
12 | }
|
13 | return Buffer.concat(chunks);
|
14 | };
|
15 | export 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 |
|
22 |
|
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 |
|
31 | return req.rawBody;
|
32 | }
|
33 | else if (isValidBody(req.body)) {
|
34 | return req.body;
|
35 | }
|
36 | else {
|
37 |
|
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 |
|
\ | No newline at end of file |