@tylix/core 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylix/core",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "type": "module",
5
5
  "keywords": [
6
6
  "tylix",
@@ -15,4 +15,6 @@ export declare class Server {
15
15
  constructor(router: Router, options?: ServerOptions)
16
16
  createHandler(): (req: any, res: any) => Promise<void>
17
17
  listen(port: number, callback?: () => void): import('node:http').Server
18
- }
18
+ }
19
+
20
+ export declare function readBody(req: any): Promise<{ raw: string; body: any }>
@@ -1,6 +1,6 @@
1
1
  import http from 'node:http'
2
2
  import { enhanceResponse } from './enhanceResponse.js'
3
- import { parseBody } from './parseBody.js'
3
+ import { readBody } from './parseBody.js'
4
4
  import { parseQuery } from './parseQuery.js'
5
5
  import { parseCookies } from './parseCookies.js'
6
6
 
@@ -82,7 +82,11 @@ export class Server {
82
82
  req.cookies = parseCookies(req)
83
83
 
84
84
  try {
85
- req.body = await parseBody(req)
85
+ const { raw, body } = await readBody(req)
86
+ // The exact text the client sent. Webhook signatures are computed
87
+ // over these bytes, so they must not be re-serialised from req.body.
88
+ req.rawBody = raw
89
+ req.body = body
86
90
  } catch (err) {
87
91
  res.status(400).json({ error: err.message })
88
92
  return
@@ -1,25 +1,36 @@
1
- export function parseBody(req) {
1
+ /**
2
+ * Reads the whole request body once.
3
+ * Returns { raw, body }: raw is the exact UTF-8 text the client sent,
4
+ * body is the parsed JSON (or {} for empty or non-JSON bodies).
5
+ *
6
+ * Chunks are collected as Buffers and decoded once at the end. Joining
7
+ * them as strings can split a multi-byte character across two chunks,
8
+ * which would change the text and break webhook signature checks.
9
+ */
10
+ export function readBody(req) {
2
11
  return new Promise((resolve, reject) => {
3
- let chunks = "";
12
+ const chunks = [];
4
13
 
5
14
  req.on("data", (chunk) => {
6
- chunks += chunk;
15
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
7
16
  });
8
17
 
9
18
  req.on("end", () => {
10
- if (!chunks) {
11
- resolve({});
19
+ const raw = Buffer.concat(chunks).toString("utf8");
20
+
21
+ if (!raw) {
22
+ resolve({ raw: "", body: {} });
12
23
  return;
13
24
  }
14
25
 
15
26
  const contentType = req.headers["content-type"] || "";
16
27
  if (!contentType.includes("application/json")) {
17
- resolve({});
28
+ resolve({ raw, body: {} });
18
29
  return;
19
30
  }
20
31
 
21
32
  try {
22
- resolve(JSON.parse(chunks));
33
+ resolve({ raw, body: JSON.parse(raw) });
23
34
  } catch (err) {
24
35
  reject(new Error("Invalid JSON body"));
25
36
  }
@@ -28,3 +39,8 @@ export function parseBody(req) {
28
39
  req.on("error", reject);
29
40
  });
30
41
  }
42
+
43
+ export async function parseBody(req) {
44
+ const { body } = await readBody(req);
45
+ return body;
46
+ }