Skip to content

Commit 1ca2887

Browse files
author
Hyunje Jun
committed
Handle pre-parsed string and buffer
1 parent 3c3eccb commit 1ca2887

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

lib/middleware.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ export default function middleware(config: Line.Config & { channelSecret: string
2626
return;
2727
}
2828

29-
raw({ type: "*/*" })(
30-
req as any,
31-
res as any,
32-
(() => {
33-
if (!validateSignature(req.body, secret, signature)) {
34-
next(new SignatureValidationFailed("signature validation failed", signature));
35-
return;
36-
}
37-
38-
const strBody = req.body.toString();
39-
try {
40-
req.body = JSON.parse(strBody);
41-
next();
42-
} catch (err) {
43-
next(new JSONParseError(err.message, strBody));
44-
}
45-
}) as any,
46-
);
29+
const validate = (body: string | Buffer) => {
30+
if (!validateSignature(body, secret, signature)) {
31+
next(new SignatureValidationFailed("signature validation failed", signature));
32+
return;
33+
}
34+
35+
const strBody = Buffer.isBuffer(body) ? body.toString() : body;
36+
37+
try {
38+
req.body = JSON.parse(strBody);
39+
next();
40+
} catch (err) {
41+
next(new JSONParseError(err.message, strBody));
42+
}
43+
};
44+
45+
if (typeof req.body === "string" || Buffer.isBuffer(req.body)) {
46+
return validate(req.body);
47+
}
48+
49+
// if body is not parsed yet, parse it to a buffer
50+
raw({ type: "*/*" })(req as any, res as any, () => validate(req.body));
4751
};
4852
}

test/helpers/test-server.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ function listen(port: number, middleware?: express.RequestHandler) {
1010
const app = express();
1111

1212
if (middleware) {
13+
app.use((req: express.Request, res, next) => {
14+
if (req.path === "/mid-text") {
15+
bodyParser.text({ type: "*/*" })(req, res, next);
16+
} else if (req.path === "/mid-buffer") {
17+
bodyParser.raw({ type: "*/*" })(req, res, next);
18+
} else if (req.path === "/mid-json") {
19+
bodyParser.json({ type: "*/*" })(req, res, next);
20+
} else {
21+
next();
22+
}
23+
});
24+
1325
app.use(middleware);
1426
}
1527

test/middleware.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ describe("middleware", () => {
3737
});
3838
});
3939

40+
it("succeed with pre-parsed string", () => {
41+
const auth: any = { "X-Line-Signature": "qeDy61PbQK+aO97Bs8zjaFgYjQxFruGd13pfXPQoBRU=" };
42+
43+
return post(`${TEST_URL}/mid-text`, auth, { events: [webhook] })
44+
.then((res: any) => {
45+
deepEqual(res.body.events, [webhook]);
46+
});
47+
});
48+
49+
it("succeed with pre-parsed buffer", () => {
50+
const auth: any = { "X-Line-Signature": "qeDy61PbQK+aO97Bs8zjaFgYjQxFruGd13pfXPQoBRU=" };
51+
52+
return post(`${TEST_URL}/mid-buffer`, auth, { events: [webhook] })
53+
.then((res: any) => {
54+
deepEqual(res.body.events, [webhook]);
55+
});
56+
});
57+
4058
it("fails on wrong signature", () => {
4159
const auth: any = { "X-Line-Signature": "qeDy61PbQK+aO97Bs8zjbFgYjQxFruGd13pfXPQoBRU=" };
4260

0 commit comments

Comments
 (0)