milliparsec 5.0.0 → 5.0.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/README.md CHANGED
@@ -3,7 +3,9 @@
3
3
  <img src="logo.png" width="400px" />
4
4
  <br /><br />
5
5
 
6
- [![Version][v-badge-url]][npm-url] [![Coverage][cov-img]][cov-url] [![Github actions][gh-actions-img]][github-actions] [![Downloads][dl-badge-url]][npm-url]
6
+ [![Version][v-badge-url]][npm-url] [![Coverage][cov-img]][cov-url]
7
+ [![Github actions][gh-actions-img]][github-actions]
8
+ [![Downloads][dl-badge-url]][npm-url]
7
9
 
8
10
  </div>
9
11
  <br />
@@ -18,7 +20,7 @@ Check out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.
18
20
  - 📦 tiny package size (8KB dist size)
19
21
  - 🔥 no dependencies
20
22
  - ✨ [tinyhttp](https://github.com/tinyhttp/tinyhttp) and Express support
21
- - ⚡ 30% faster than body-parser
23
+ - ⚡ 40% faster than body-parser and 20x faster than formidable
22
24
 
23
25
  ## Install
24
26
 
@@ -51,7 +53,8 @@ const server = createServer(async (req: ReqWithBody, res) => {
51
53
 
52
54
  ### What is "parsec"?
53
55
 
54
- The parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.
56
+ The parsec is a unit of length used to measure large distances to astronomical
57
+ objects outside the Solar System.
55
58
 
56
59
  [v-badge-url]: https://img.shields.io/npm/v/milliparsec.svg?style=for-the-badge&color=25608B&logo=npm&label=
57
60
  [npm-url]: https://www.npmjs.com/package/milliparsec
package/dist/index.d.ts CHANGED
@@ -20,33 +20,33 @@ export type ParserOptions = Partial<{
20
20
  */
21
21
  payloadLimitErrorFn: LimitErrorFn;
22
22
  }>;
23
- export declare const p: <T = any>(fn: (body: Buffer) => void, payloadLimit?: number, payloadLimitErrorFn?: LimitErrorFn) => (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => Promise<void>;
23
+ export declare const p: <T = any>(fn: (body: Buffer) => void, payloadLimit?: number, payloadLimitErrorFn?: LimitErrorFn) => (req: ReqWithBody<T>, _res: Response, next?: (err?: any) => void) => Promise<void>;
24
24
  /**
25
25
  * Parse payload with a custom function
26
26
  * @param fn
27
27
  */
28
- declare const custom: <T = any>(fn: (body: Buffer) => any) => (req: ReqWithBody, _res: Response, next: NextFunction) => Promise<void>;
28
+ declare const custom: <T = any>(fn: (body: Buffer) => any) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
29
29
  /**
30
30
  * Parse JSON payload
31
31
  * @param options
32
32
  */
33
- declare const json: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, res: Response, next: NextFunction) => Promise<void>;
33
+ declare const json: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, res: Response, next?: NextFunction) => Promise<void>;
34
34
  /**
35
35
  * Parse raw payload
36
36
  * @param options
37
37
  */
38
- declare const raw: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next: NextFunction) => Promise<void>;
38
+ declare const raw: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
39
39
  /**
40
40
  * Stringify request payload
41
41
  * @param param0
42
42
  * @returns
43
43
  */
44
- declare const text: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next: NextFunction) => Promise<void>;
44
+ declare const text: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
45
45
  /**
46
46
  * Parse urlencoded payload
47
47
  * @param options
48
48
  */
49
- declare const urlencoded: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next: NextFunction) => Promise<void>;
49
+ declare const urlencoded: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
50
50
  type MultipartOptions = Partial<{
51
51
  /**
52
52
  * Limit number of files
@@ -67,5 +67,5 @@ type MultipartOptions = Partial<{
67
67
  * Does not restrict total payload size by default.
68
68
  * @param options
69
69
  */
70
- declare const multipart: ({ payloadLimit, payloadLimitErrorFn, ...opts }?: MultipartOptions & ParserOptions) => (req: ReqWithBody, res: Response, next: NextFunction) => Promise<void>;
70
+ declare const multipart: ({ payloadLimit, payloadLimitErrorFn, ...opts }?: MultipartOptions & ParserOptions) => (req: ReqWithBody, res: Response, next?: NextFunction) => Promise<void>;
71
71
  export { custom, json, raw, text, urlencoded, multipart };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Buffer } from 'node:buffer';
1
+ import { Buffer, File } from 'node:buffer';
2
2
  export const hasBody = (method) => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method);
3
3
  const defaultPayloadLimit = 102400; // 100KB
4
4
  const defaultErrorFn = (payloadLimit) => new Error(`Payload too large. Limit: ${payloadLimit} bytes`);
@@ -15,7 +15,7 @@ export const p = (fn, payloadLimit = defaultPayloadLimit, payloadLimitErrorFn =
15
15
  return fn(Buffer.concat(body));
16
16
  }
17
17
  catch (e) {
18
- next(e);
18
+ next === null || next === void 0 ? void 0 : next(e);
19
19
  }
20
20
  };
21
21
  /**
@@ -25,7 +25,7 @@ export const p = (fn, payloadLimit = defaultPayloadLimit, payloadLimitErrorFn =
25
25
  const custom = (fn) => async (req, _res, next) => {
26
26
  if (hasBody(req.method))
27
27
  req.body = await p(fn)(req, _res, next);
28
- next();
28
+ next === null || next === void 0 ? void 0 : next();
29
29
  };
30
30
  /**
31
31
  * Parse JSON payload
@@ -38,8 +38,7 @@ const json = ({ payloadLimit, payloadLimitErrorFn } = {}) => async (req, res, ne
38
38
  return str ? JSON.parse(str) : {};
39
39
  }, payloadLimit, payloadLimitErrorFn)(req, res, next);
40
40
  }
41
- else
42
- next();
41
+ next === null || next === void 0 ? void 0 : next();
43
42
  };
44
43
  /**
45
44
  * Parse raw payload
@@ -49,8 +48,7 @@ const raw = ({ payloadLimit, payloadLimitErrorFn } = {}) => async (req, _res, ne
49
48
  if (hasBody(req.method)) {
50
49
  req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next);
51
50
  }
52
- else
53
- next();
51
+ next === null || next === void 0 ? void 0 : next();
54
52
  };
55
53
  const td = new TextDecoder();
56
54
  /**
@@ -62,8 +60,7 @@ const text = ({ payloadLimit, payloadLimitErrorFn } = {}) => async (req, _res, n
62
60
  if (hasBody(req.method)) {
63
61
  req.body = await p((x) => td.decode(x), payloadLimit, payloadLimitErrorFn)(req, _res, next);
64
62
  }
65
- else
66
- next();
63
+ next === null || next === void 0 ? void 0 : next();
67
64
  };
68
65
  /**
69
66
  * Parse urlencoded payload
@@ -73,8 +70,7 @@ const urlencoded = ({ payloadLimit, payloadLimitErrorFn } = {}) => async (req, _
73
70
  if (hasBody(req.method)) {
74
71
  req.body = await p((x) => Object.fromEntries(new URLSearchParams(x.toString()).entries()), payloadLimit, payloadLimitErrorFn)(req, _res, next);
75
72
  }
76
- else
77
- next();
73
+ next === null || next === void 0 ? void 0 : next();
78
74
  };
79
75
  const getBoundary = (contentType) => {
80
76
  const match = /boundary=(.+);?/.exec(contentType);
@@ -123,7 +119,6 @@ const multipart = ({ payloadLimit = Number.POSITIVE_INFINITY, payloadLimitErrorF
123
119
  return {};
124
120
  }, payloadLimit, payloadLimitErrorFn)(req, res, next);
125
121
  }
126
- else
127
- next();
122
+ next === null || next === void 0 ? void 0 : next();
128
123
  };
129
124
  export { custom, json, raw, text, urlencoded, multipart };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "milliparsec",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "description": "tiniest body parser in the universe",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,12 +17,13 @@
17
17
  "body-parsing"
18
18
  ],
19
19
  "engines": {
20
- "node": ">=20"
20
+ "node": ">=18.13 || >=19.20 || >=20"
21
21
  },
22
22
  "exports": "./dist/index.js",
23
23
  "devDependencies": {
24
24
  "@biomejs/biome": "1.9.3",
25
- "@types/node": "^22.7.4",
25
+ "@tinyhttp/app": "^2.4.0",
26
+ "@types/node": "^18.19.76",
26
27
  "c8": "10.1.2",
27
28
  "supertest-fetch": "^2.0.0",
28
29
  "tsx": "^4.19.1",
@@ -38,6 +39,7 @@
38
39
  "test": "tsx --test test.ts",
39
40
  "test:coverage": "c8 --include=src pnpm test",
40
41
  "test:report": "c8 report --reporter=text-lcov > coverage.lcov",
41
- "build": "tsc -p tsconfig.build.json"
42
+ "build": "tsc -p tsconfig.build.json",
43
+ "check": "biome check --write"
42
44
  }
43
45
  }