milliparsec 2.0.5 â 2.2.1
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 +18 -46
- package/dist/index.js +7 -15
- package/package.json +41 -53
- package/dist/koa.d.ts +0 -10
- package/dist/koa.js +0 -61
package/README.md
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
<
|
|
2
|
-
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
> _Photo by NASA published on [Unsplash](https://unsplash.com/photos/rTZW4f02zY8)_
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br /><br /><br />
|
|
3
|
+
<img src="logo.png" width="400px" />
|
|
4
|
+
<br /><br />
|
|
6
5
|
|
|
7
6
|
![Vulnerabilities][vulns-badge-url]
|
|
8
|
-
[![Version][v-badge-url]][npm-url]
|
|
9
|
-
|
|
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]
|
|
8
|
+
|
|
9
|
+
</div>
|
|
10
|
+
<br />
|
|
10
11
|
|
|
11
12
|
Tiniest body parser in the universe. Built for modern Node.js.
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
Check out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.
|
|
14
15
|
|
|
15
16
|
## Features
|
|
16
17
|
|
|
@@ -18,8 +19,7 @@ Tiniest body parser in the universe. Built for modern Node.js.
|
|
|
18
19
|
- đ JSON / raw / urlencoded data support
|
|
19
20
|
- đĻ tiny package size (728B)
|
|
20
21
|
- đĨ no dependencies
|
|
21
|
-
- ⥠[tinyhttp](https://github.com/talentlessguy/tinyhttp)
|
|
22
|
-
- đ send `415 Unsupported Media Type` on incorrect `Content-Type`
|
|
22
|
+
- ⥠[tinyhttp](https://github.com/talentlessguy/tinyhttp) and Express support
|
|
23
23
|
|
|
24
24
|
## Install
|
|
25
25
|
|
|
@@ -67,35 +67,6 @@ new App()
|
|
|
67
67
|
.listen(3000, () => console.log(`Started on http://localhost:3000`))
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
#### Express
|
|
71
|
-
|
|
72
|
-
```ts
|
|
73
|
-
import Express from 'express'
|
|
74
|
-
import { urlencoded } from 'milliparsec'
|
|
75
|
-
|
|
76
|
-
Express()
|
|
77
|
-
.use(urlencoded())
|
|
78
|
-
.post('/', (req, res) => void res.send(`Hello ${req.body.name}!`))
|
|
79
|
-
.listen(3000, () => console.log(`Running on http://localhost:3000`))
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
#### Koa
|
|
83
|
-
|
|
84
|
-
```ts
|
|
85
|
-
import Koa from 'koa'
|
|
86
|
-
import { json, CtxWithBody } from 'milliparsec/koa'
|
|
87
|
-
|
|
88
|
-
new Koa()
|
|
89
|
-
.use(json())
|
|
90
|
-
.use((ctx: CtxWithBody) => {
|
|
91
|
-
if (ctx.method === 'POST') {
|
|
92
|
-
ctx.type = 'application/json'
|
|
93
|
-
ctx.body = ctx.parsedBody
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
.listen(3000, () => console.log(`Running on http://localhost:3000`))
|
|
97
|
-
```
|
|
98
|
-
|
|
99
70
|
## API
|
|
100
71
|
|
|
101
72
|
### `raw(req, res, cb)`
|
|
@@ -108,7 +79,7 @@ Converts request body to string.
|
|
|
108
79
|
|
|
109
80
|
### `urlencoded(req, res, cb)`
|
|
110
81
|
|
|
111
|
-
Parses request body using `
|
|
82
|
+
Parses request body using `new URLSearchParams`.
|
|
112
83
|
|
|
113
84
|
### `json(req, res, cb)`
|
|
114
85
|
|
|
@@ -132,10 +103,11 @@ res.end(req.body) // "THIS TEXT MUST BE UPPERCASED"
|
|
|
132
103
|
|
|
133
104
|
The parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.
|
|
134
105
|
|
|
135
|
-
[vulns-badge-url]: https://img.shields.io/snyk/vulnerabilities/npm/milliparsec.svg?style=
|
|
136
|
-
[v-badge-url]: https://img.shields.io/npm/v/milliparsec.svg?style=
|
|
106
|
+
[vulns-badge-url]: https://img.shields.io/snyk/vulnerabilities/npm/milliparsec.svg?style=for-the-badge&color=25608B&label=vulns
|
|
107
|
+
[v-badge-url]: https://img.shields.io/npm/v/milliparsec.svg?style=for-the-badge&color=25608B&logo=npm&label=
|
|
137
108
|
[npm-url]: https://www.npmjs.com/package/milliparsec
|
|
138
|
-
[
|
|
139
|
-
[
|
|
140
|
-
[
|
|
141
|
-
[
|
|
109
|
+
[dl-badge-url]: https://img.shields.io/npm/dt/milliparsec?style=for-the-badge&color=25608B
|
|
110
|
+
[github-actions]: https://github.com/talentlessguy/milliparsec/actions
|
|
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,6 +1,3 @@
|
|
|
1
|
-
import { STATUS_CODES } from 'http';
|
|
2
|
-
import * as qs from 'querystring';
|
|
3
|
-
|
|
4
1
|
const hasBody = (method) => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method);
|
|
5
2
|
// Main function
|
|
6
3
|
const p = (fn) => async (req, _res, next) => {
|
|
@@ -21,12 +18,8 @@ const custom = (fn) => async (req, _res, next) => {
|
|
|
21
18
|
};
|
|
22
19
|
const json = () => async (req, res, next) => {
|
|
23
20
|
if (hasBody(req.method)) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
next();
|
|
27
|
-
}
|
|
28
|
-
else
|
|
29
|
-
res.writeHead(415).end(STATUS_CODES[415]);
|
|
21
|
+
req.body = await p((x) => JSON.parse(x.toString()))(req, res, next);
|
|
22
|
+
next();
|
|
30
23
|
}
|
|
31
24
|
else
|
|
32
25
|
next();
|
|
@@ -45,12 +38,11 @@ const text = () => async (req, _res, next) => {
|
|
|
45
38
|
};
|
|
46
39
|
const urlencoded = () => async (req, res, next) => {
|
|
47
40
|
if (hasBody(req.method)) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
res.writeHead(415).end(STATUS_CODES[415]);
|
|
41
|
+
req.body = await p((x) => {
|
|
42
|
+
const urlSearchParam = new URLSearchParams(x.toString());
|
|
43
|
+
return Object.fromEntries(urlSearchParam.entries());
|
|
44
|
+
})(req, res, next);
|
|
45
|
+
next();
|
|
54
46
|
}
|
|
55
47
|
else
|
|
56
48
|
next();
|
package/package.json
CHANGED
|
@@ -1,54 +1,42 @@
|
|
|
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
|
-
|
|
43
|
-
"dist"
|
|
44
|
-
],
|
|
45
|
-
"optionalDependencies": {
|
|
46
|
-
"koa": "^2.13.1"
|
|
47
|
-
},
|
|
48
|
-
"scripts": {
|
|
49
|
-
"test": "uvu -r ts-node/register tests",
|
|
50
|
-
"test:coverage": "c8 --include=src pnpm test",
|
|
51
|
-
"test:report": "c8 report --reporter=text-lcov > coverage.lcov",
|
|
52
|
-
"build": "rollup -c"
|
|
53
|
-
}
|
|
54
|
-
}
|
|
2
|
+
"name": "milliparsec",
|
|
3
|
+
"version": "2.2.1",
|
|
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
|
+
"readme": "<div align=\"center\">\n<br /><br /><br />\n<img src=\"logo.png\" width=\"400px\" />\n<br /><br />\n\n![Vulnerabilities][vulns-badge-url]\n[![Version][v-badge-url]][npm-url] [![Coverage][cov-img]][cov-url] [![Github actions][gh-actions-img]][github-actions] [![Downloads][dl-badge-url]][npm-url]\n\n</div>\n<br />\n\nTiniest body parser in the universe. Built for modern Node.js.\n\nCheck out [deno-libs/parsec](https://github.com/deno-libs/parsec) for Deno port.\n\n## Features\n\n- ⊠built with `async` / `await`\n- đ JSON / raw / urlencoded data support\n- đĻ tiny package size (728B)\n- đĨ no dependencies\n- ⥠[tinyhttp](https://github.com/talentlessguy/tinyhttp) and Express support\n\n## Install\n\n```sh\n# pnpm\npnpm i milliparsec\n\n# yarn\nyarn add milliparsec\n\n# npm\nnpm i milliparsec\n```\n\n## Usage\n\n### Basic example\n\nUse a middleware inside a server:\n\n```js\nimport { createServer } from 'http'\nimport { json } from 'milliparsec'\n\nconst server = createServer(async (req: ReqWithBody, res) => {\n await json()(req, res, (err) => void err && console.log(err))\n\n res.setHeader('Content-Type', 'application/json')\n\n res.end(JSON.stringify(req.body))\n})\n```\n\n### Web frameworks integration\n\n#### tinyhttp\n\n```ts\nimport { App } from '@tinyhttp/app'\nimport { urlencoded } from 'milliparsec'\n\nnew App()\n .use(urlencoded())\n .post('/', (req, res) => void res.send(req.body))\n .listen(3000, () => console.log(`Started on http://localhost:3000`))\n```\n\n## API\n\n### `raw(req, res, cb)`\n\nMinimal body parsing without any formatting.\n\n### `text(req, res, cb)`\n\nConverts request body to string.\n\n### `urlencoded(req, res, cb)`\n\nParses request body using `new URLSearchParams`.\n\n### `json(req, res, cb)`\n\nParses request body using `JSON.parse`.\n\n### `custom(fn)(req, res, cb)`\n\nCustom function for `parsec`.\n\n```js\n// curl -d \"this text must be uppercased\" localhost\nawait custom(\n req,\n (d) => d.toUpperCase(),\n (err) => {}\n)\nres.end(req.body) // \"THIS TEXT MUST BE UPPERCASED\"\n```\n\n### What is \"parsec\"?\n\nThe parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.\n\n[vulns-badge-url]: https://img.shields.io/snyk/vulnerabilities/npm/milliparsec.svg?style=for-the-badge&color=25608B&label=vulns\n[v-badge-url]: https://img.shields.io/npm/v/milliparsec.svg?style=for-the-badge&color=25608B&logo=npm&label=\n[npm-url]: https://www.npmjs.com/package/milliparsec\n[dl-badge-url]: https://img.shields.io/npm/dt/milliparsec?style=for-the-badge&color=25608B\n[github-actions]: https://github.com/talentlessguy/milliparsec/actions\n[gh-actions-img]: https://img.shields.io/github/workflow/status/talentlessguy/milliparsec/CI?style=for-the-badge&color=25608B&label=&logo=github\n[cov-img]: https://img.shields.io/coveralls/github/tinyhttp/milliparsec?style=for-the-badge&color=25608B\n[cov-url]: https://coveralls.io/github/tinyhttp/milliparsec\n"
|
|
42
|
+
}
|
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,61 +0,0 @@
|
|
|
1
|
-
import { STATUS_CODES } from 'http';
|
|
2
|
-
import * as qs from 'querystring';
|
|
3
|
-
|
|
4
|
-
const hasBody = (method) => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method);
|
|
5
|
-
// Main function
|
|
6
|
-
const p$1 = (fn) => async (req, _res, next) => {
|
|
7
|
-
try {
|
|
8
|
-
let body = '';
|
|
9
|
-
for await (const chunk of req)
|
|
10
|
-
body += chunk;
|
|
11
|
-
return fn(body);
|
|
12
|
-
}
|
|
13
|
-
catch (e) {
|
|
14
|
-
next(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const p = (fn = (body) => body) => async (ctx, next) => {
|
|
19
|
-
ctx.parsedBody = await p$1(fn)(ctx.req, ctx.res, next);
|
|
20
|
-
next();
|
|
21
|
-
};
|
|
22
|
-
const json = () => async (ctx, next) => {
|
|
23
|
-
if (hasBody(ctx.method)) {
|
|
24
|
-
if (ctx.header['content-type'] === 'application/json') {
|
|
25
|
-
await p((x) => JSON.parse(x.toString()))(ctx, next);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
ctx.status = 415;
|
|
29
|
-
ctx.body = STATUS_CODES[415];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
else
|
|
33
|
-
next();
|
|
34
|
-
};
|
|
35
|
-
const raw = () => async (ctx, next) => {
|
|
36
|
-
if (hasBody(ctx.method))
|
|
37
|
-
await p((x) => x)(ctx, next);
|
|
38
|
-
else
|
|
39
|
-
next();
|
|
40
|
-
};
|
|
41
|
-
const text = () => async (ctx, next) => {
|
|
42
|
-
if (hasBody(ctx.method))
|
|
43
|
-
await p((x) => x.toString())(ctx, next);
|
|
44
|
-
else
|
|
45
|
-
next();
|
|
46
|
-
};
|
|
47
|
-
const urlencoded = () => async (ctx, next) => {
|
|
48
|
-
if (hasBody(ctx.method)) {
|
|
49
|
-
if (ctx.header['content-type'] === 'application/x-www-form-urlencoded') {
|
|
50
|
-
await p((x) => qs.parse(x.toString()))(ctx, next);
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
ctx.response.status = 415;
|
|
54
|
-
ctx.response.body = STATUS_CODES[415];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else
|
|
58
|
-
next();
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export { p as custom, json, raw, text, urlencoded };
|