milliparsec 2.1.1 → 2.2.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 +10 -27
- package/dist/index.js +10 -5
- package/package.json +40 -49
- package/dist/koa.d.ts +0 -10
- package/dist/koa.js +0 -48
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
<
|
|
2
|
-
|
|
3
|
-
<
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br /><br /><br />
|
|
3
|
+
<img src="logo.png" width="400px" />
|
|
4
|
+
<br /><br />
|
|
4
5
|
|
|
5
6
|
![Vulnerabilities][vulns-badge-url]
|
|
6
|
-
[![Version][v-badge-url]][npm-url] [![
|
|
7
|
+
[![Version][v-badge-url]][npm-url] [![Coverage][cov-img]][cov-url] [![Github actions][gh-actions-img]][github-actions] [![Downloads][dl-badge-url]][npm-url]
|
|
7
8
|
|
|
8
9
|
</div>
|
|
9
|
-
|
|
10
|
-
> _Photo by NASA published on [Unsplash](https://unsplash.com/photos/rTZW4f02zY8)_
|
|
10
|
+
<br />
|
|
11
11
|
|
|
12
12
|
Tiniest body parser in the universe. Built for modern Node.js.
|
|
13
13
|
|
|
@@ -19,7 +19,7 @@ Check out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.
|
|
|
19
19
|
- 🛠 JSON / raw / urlencoded data support
|
|
20
20
|
- 📦 tiny package size (728B)
|
|
21
21
|
- 🔥 no dependencies
|
|
22
|
-
- ⚡ [tinyhttp](https://github.com/talentlessguy/tinyhttp)
|
|
22
|
+
- ⚡ [tinyhttp](https://github.com/talentlessguy/tinyhttp) and Express support
|
|
23
23
|
|
|
24
24
|
## Install
|
|
25
25
|
|
|
@@ -67,23 +67,6 @@ new App()
|
|
|
67
67
|
.listen(3000, () => console.log(`Started on http://localhost:3000`))
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
#### Koa
|
|
71
|
-
|
|
72
|
-
```ts
|
|
73
|
-
import Koa from 'koa'
|
|
74
|
-
import { json, CtxWithBody } from 'milliparsec/koa'
|
|
75
|
-
|
|
76
|
-
new Koa()
|
|
77
|
-
.use(json())
|
|
78
|
-
.use((ctx: CtxWithBody) => {
|
|
79
|
-
if (ctx.method === 'POST') {
|
|
80
|
-
ctx.type = 'application/json'
|
|
81
|
-
ctx.body = ctx.parsedBody
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
.listen(3000, () => console.log(`Running on http://localhost:3000`))
|
|
85
|
-
```
|
|
86
|
-
|
|
87
70
|
## API
|
|
88
71
|
|
|
89
72
|
### `raw(req, res, cb)`
|
|
@@ -96,7 +79,7 @@ Converts request body to string.
|
|
|
96
79
|
|
|
97
80
|
### `urlencoded(req, res, cb)`
|
|
98
81
|
|
|
99
|
-
Parses request body using `
|
|
82
|
+
Parses request body using `new URLSearchParams`.
|
|
100
83
|
|
|
101
84
|
### `json(req, res, cb)`
|
|
102
85
|
|
|
@@ -123,8 +106,8 @@ The parsec is a unit of length used to measure large distances to astronomical o
|
|
|
123
106
|
[vulns-badge-url]: https://img.shields.io/snyk/vulnerabilities/npm/milliparsec.svg?style=for-the-badge&color=25608B&label=vulns
|
|
124
107
|
[v-badge-url]: https://img.shields.io/npm/v/milliparsec.svg?style=for-the-badge&color=25608B&logo=npm&label=
|
|
125
108
|
[npm-url]: https://www.npmjs.com/package/milliparsec
|
|
126
|
-
[cov-badge-url]: https://img.shields.io/codecov/c/gh/talentlessguy/milliparsec?style=for-the-badge&color=25608B
|
|
127
|
-
[cov-url]: https://codecov.io/gh/talentlessguy/milliparsec
|
|
128
109
|
[dl-badge-url]: https://img.shields.io/npm/dt/milliparsec?style=for-the-badge&color=25608B
|
|
129
110
|
[github-actions]: https://github.com/talentlessguy/milliparsec/actions
|
|
130
111
|
[gh-actions-img]: https://img.shields.io/github/workflow/status/talentlessguy/milliparsec/CI?style=for-the-badge&color=25608B&label=&logo=github
|
|
112
|
+
[cov-img]: https://img.shields.io/coveralls/github/tinyhttp/milliparsec?style=for-the-badge&color=25608B
|
|
113
|
+
[cov-url]: https://coveralls.io/github/tinyhttp/milliparsec
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import * as qs from 'querystring';
|
|
2
|
-
|
|
3
1
|
const hasBody = (method) => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method);
|
|
4
2
|
// Main function
|
|
5
3
|
const p = (fn) => async (req, _res, next) => {
|
|
@@ -27,20 +25,27 @@ const json = () => async (req, res, next) => {
|
|
|
27
25
|
next();
|
|
28
26
|
};
|
|
29
27
|
const raw = () => async (req, _res, next) => {
|
|
30
|
-
if (hasBody(req.method))
|
|
28
|
+
if (hasBody(req.method)) {
|
|
31
29
|
req.body = await p((x) => x)(req, _res, next);
|
|
30
|
+
next();
|
|
31
|
+
}
|
|
32
32
|
else
|
|
33
33
|
next();
|
|
34
34
|
};
|
|
35
35
|
const text = () => async (req, _res, next) => {
|
|
36
|
-
if (hasBody(req.method))
|
|
36
|
+
if (hasBody(req.method)) {
|
|
37
37
|
req.body = await p((x) => x.toString())(req, _res, next);
|
|
38
|
+
next();
|
|
39
|
+
}
|
|
38
40
|
else
|
|
39
41
|
next();
|
|
40
42
|
};
|
|
41
43
|
const urlencoded = () => async (req, res, next) => {
|
|
42
44
|
if (hasBody(req.method)) {
|
|
43
|
-
req.body = await p((x) =>
|
|
45
|
+
req.body = await p((x) => {
|
|
46
|
+
const urlSearchParam = new URLSearchParams(x.toString());
|
|
47
|
+
return Object.fromEntries(urlSearchParam.entries());
|
|
48
|
+
})(req, res, next);
|
|
44
49
|
next();
|
|
45
50
|
}
|
|
46
51
|
else
|
package/package.json
CHANGED
|
@@ -1,50 +1,41 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"koa": "^2.13.1"
|
|
43
|
-
},
|
|
44
|
-
"scripts": {
|
|
45
|
-
"test": "node --experimental-loader esbuild-node-loader node_modules/uvu/bin.js tests",
|
|
46
|
-
"test:coverage": "c8 --include=src pnpm test",
|
|
47
|
-
"test:report": "c8 report --reporter=text-lcov > coverage.lcov",
|
|
48
|
-
"build": "rollup -c"
|
|
49
|
-
}
|
|
50
|
-
}
|
|
2
|
+
"name": "milliparsec",
|
|
3
|
+
"version": "2.2.2",
|
|
4
|
+
"description": "tiniest body parser in the universe",
|
|
5
|
+
"repository": "https://github.com/talentlessguy/parsec.git",
|
|
6
|
+
"author": "talentlessguy <pilll.PL22@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"body-parser",
|
|
12
|
+
"express",
|
|
13
|
+
"http",
|
|
14
|
+
"body-parsing"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=12.4"
|
|
18
|
+
},
|
|
19
|
+
"exports": "./dist/index.js",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@rollup/plugin-typescript": "6",
|
|
22
|
+
"@tinyhttp/app": "^2.0.16",
|
|
23
|
+
"@types/node": "^17.0.12",
|
|
24
|
+
"c8": "7.11.0",
|
|
25
|
+
"esbuild-node-loader": "^0.6.4",
|
|
26
|
+
"rollup": "^2.66.1",
|
|
27
|
+
"supertest-fetch": "^1.5.0",
|
|
28
|
+
"tslib": "^2.3.1",
|
|
29
|
+
"typescript": "^4.5.5",
|
|
30
|
+
"uvu": "^0.5.3"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node --experimental-loader esbuild-node-loader test.ts",
|
|
37
|
+
"test:coverage": "c8 --include=src pnpm test",
|
|
38
|
+
"test:report": "c8 report --reporter=text-lcov > coverage.lcov",
|
|
39
|
+
"build": "rollup -c"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/dist/koa.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Next, ParameterizedContext, DefaultState, DefaultContext } from 'koa';
|
|
2
|
-
export declare type CtxWithBody<T = any> = ParameterizedContext<DefaultState, DefaultContext & {
|
|
3
|
-
parsedBody: T;
|
|
4
|
-
}>;
|
|
5
|
-
declare const p: (fn?: (body: any) => any) => <T = any>(ctx: CtxWithBody<T>, next: Next) => Promise<void>;
|
|
6
|
-
declare const json: () => <T = any>(ctx: CtxWithBody<T>, next: Next) => Promise<void>;
|
|
7
|
-
declare const raw: () => <T = any>(ctx: CtxWithBody<T>, next: Next) => Promise<void>;
|
|
8
|
-
declare const text: () => <T = any>(ctx: CtxWithBody<T>, next: Next) => Promise<void>;
|
|
9
|
-
declare const urlencoded: () => <T = any>(ctx: CtxWithBody, next: Next) => Promise<void>;
|
|
10
|
-
export { p as custom, json, raw, text, urlencoded };
|
package/dist/koa.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import * as qs from 'querystring';
|
|
2
|
-
|
|
3
|
-
const hasBody = (method) => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method);
|
|
4
|
-
// Main function
|
|
5
|
-
const p$1 = (fn) => async (req, _res, next) => {
|
|
6
|
-
try {
|
|
7
|
-
let body = '';
|
|
8
|
-
for await (const chunk of req)
|
|
9
|
-
body += chunk;
|
|
10
|
-
return fn(body);
|
|
11
|
-
}
|
|
12
|
-
catch (e) {
|
|
13
|
-
next(e);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const p = (fn = (body) => body) => async (ctx, next) => {
|
|
18
|
-
ctx.parsedBody = await p$1(fn)(ctx.req, ctx.res, next);
|
|
19
|
-
next();
|
|
20
|
-
};
|
|
21
|
-
const json = () => async (ctx, next) => {
|
|
22
|
-
if (hasBody(ctx.method)) {
|
|
23
|
-
await p((x) => JSON.parse(x.toString()))(ctx, next);
|
|
24
|
-
}
|
|
25
|
-
else
|
|
26
|
-
next();
|
|
27
|
-
};
|
|
28
|
-
const raw = () => async (ctx, next) => {
|
|
29
|
-
if (hasBody(ctx.method))
|
|
30
|
-
await p((x) => x)(ctx, next);
|
|
31
|
-
else
|
|
32
|
-
next();
|
|
33
|
-
};
|
|
34
|
-
const text = () => async (ctx, next) => {
|
|
35
|
-
if (hasBody(ctx.method))
|
|
36
|
-
await p((x) => x.toString())(ctx, next);
|
|
37
|
-
else
|
|
38
|
-
next();
|
|
39
|
-
};
|
|
40
|
-
const urlencoded = () => async (ctx, next) => {
|
|
41
|
-
if (hasBody(ctx.method)) {
|
|
42
|
-
await p((x) => qs.parse(x.toString()))(ctx, next);
|
|
43
|
-
}
|
|
44
|
-
else
|
|
45
|
-
next();
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export { p as custom, json, raw, text, urlencoded };
|