@darksheep/request-body 1.0.4
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/CHANGELOG.md +81 -0
- package/README.md +126 -0
- package/package.json +59 -0
- package/src/buffer.js +107 -0
- package/src/form.js +79 -0
- package/src/index.js +12 -0
- package/src/json.js +54 -0
- package/src/querystring.js +24 -0
- package/src/stream.js +24 -0
- package/src/text.js +23 -0
- package/src/utilities/max-length.js +34 -0
- package/types/buffer.d.ts +9 -0
- package/types/form.d.ts +9 -0
- package/types/index.d.ts +6 -0
- package/types/json.d.ts +17 -0
- package/types/querystring.d.ts +15 -0
- package/types/stream.d.ts +17 -0
- package/types/text.d.ts +17 -0
- package/types/utilities/max-length.d.ts +16 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [1.0.4](https://github.com/DarkSheepSoftware/node-packages/compare/request-body-v1.0.3...request-body-v1.0.4) (2025-04-13)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Dependencies
|
|
7
|
+
|
|
8
|
+
* The following workspace dependencies were updated
|
|
9
|
+
* dependencies
|
|
10
|
+
* @darksheep/types bumped to 1.2.0
|
|
11
|
+
|
|
12
|
+
## [1.0.3](https://github.com/DarkSheepSoftware/node-packages/compare/request-body-v1.0.2...request-body-v1.0.3) (2025-03-25)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Dependencies
|
|
16
|
+
|
|
17
|
+
* The following workspace dependencies were updated
|
|
18
|
+
* dependencies
|
|
19
|
+
* @darksheep/types bumped to 1.1.0
|
|
20
|
+
|
|
21
|
+
## [1.0.2](https://github.com/DarkSheepSoftware/node-packages/compare/request-body-v1.0.1...request-body-v1.0.2) (2025-01-28)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### ๐งน Chores
|
|
25
|
+
|
|
26
|
+
* Refactor http packages for new json-schema package ([3e4b38b](https://github.com/DarkSheepSoftware/node-packages/commit/3e4b38b12aa9d22e6404712706f1f2f838e23343))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Dependencies
|
|
30
|
+
|
|
31
|
+
* The following workspace dependencies were updated
|
|
32
|
+
* dependencies
|
|
33
|
+
* @darksheep/json-schema bumped to 0.1.0
|
|
34
|
+
|
|
35
|
+
## [1.0.1](https://github.com/DarkSheepSoftware/node-packages/compare/request-body-v1.0.0...request-body-v1.0.1) (2024-12-22)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### ๐ฆ Dependencies
|
|
39
|
+
|
|
40
|
+
* **pkg:** update dependency @fastify/busboy to v3.1.0 ([#511](https://github.com/DarkSheepSoftware/node-packages/issues/511)) ([8fc054e](https://github.com/DarkSheepSoftware/node-packages/commit/8fc054eabd90e6d41f82d499ffa7d40f5b2e0a2a))
|
|
41
|
+
* **pkg:** update dependency @fastify/busboy to v3.1.1 ([#523](https://github.com/DarkSheepSoftware/node-packages/issues/523)) ([a8e8c60](https://github.com/DarkSheepSoftware/node-packages/commit/a8e8c609f8a23d0982cd94c68ca3f4d9f6b931ed))
|
|
42
|
+
|
|
43
|
+
## 1.0.0 (2024-11-20)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
### ๐ Features
|
|
47
|
+
|
|
48
|
+
* Enforce request body "maxBytes" ([a60298b](https://github.com/DarkSheepSoftware/node-packages/commit/a60298b55288998f7489de26e3607769f35ce780))
|
|
49
|
+
* More adjustments to the request-body interfaces ([b72deaa](https://github.com/DarkSheepSoftware/node-packages/commit/b72deaaf58cb6164eb258a6b6d6f01e40100c448))
|
|
50
|
+
* **request-body:** Move request-body to its own package ([#462](https://github.com/DarkSheepSoftware/node-packages/issues/462)) ([eb00425](https://github.com/DarkSheepSoftware/node-packages/commit/eb004258f30a800457af81b18e833d3ab177a0b4))
|
|
51
|
+
* **request-body:** Use new json-schema package ([008d2c5](https://github.com/DarkSheepSoftware/node-packages/commit/008d2c546993b0c2c617dd7208d19973cdfb768d))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
### ๐ฉน Fixes
|
|
55
|
+
|
|
56
|
+
* Cascading toX options ([3295c0e](https://github.com/DarkSheepSoftware/node-packages/commit/3295c0e92a8e0046cf95a1c2a1b6aa4c61a4df6c))
|
|
57
|
+
* yarn.lock updates ([889e723](https://github.com/DarkSheepSoftware/node-packages/commit/889e723e78c2963f2b25bf20cc002b5d4ce114d9))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
### ๐ Documentation
|
|
61
|
+
|
|
62
|
+
* Add basic docs to request-body ([d0e3d45](https://github.com/DarkSheepSoftware/node-packages/commit/d0e3d45eb8bc61139c35fe278b9261e78d5434d8))
|
|
63
|
+
* Update the readme ([97ba8dd](https://github.com/DarkSheepSoftware/node-packages/commit/97ba8dd6b7de3f091bf22f97edd23484aabb97e0))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
### ๐ฆ Dependencies
|
|
67
|
+
|
|
68
|
+
* **pkg:** pin dependency @fastify/busboy to 3.0.0 ([#468](https://github.com/DarkSheepSoftware/node-packages/issues/468)) ([cf8eebf](https://github.com/DarkSheepSoftware/node-packages/commit/cf8eebf0ef2c46f57baeca31f57aef0a988c3bf8))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
### ๐งน Chores
|
|
72
|
+
|
|
73
|
+
* **request-body:** Use new import jsdoc tag ([481d4dc](https://github.com/DarkSheepSoftware/node-packages/commit/481d4dc7f38159ddea7c37cd3db284784fc36d81))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
### Dependencies
|
|
77
|
+
|
|
78
|
+
* The following workspace dependencies were updated
|
|
79
|
+
* dependencies
|
|
80
|
+
* @darksheep/errors bumped from 1.1.0 to 1.1.1
|
|
81
|
+
* @darksheep/types bumped to 1.0.0
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @darksheep/request-body
|
|
2
|
+
Convert your `http.IncomingMessage` to something!
|
|
3
|
+
|
|
4
|
+
```js
|
|
5
|
+
import { createServer } from 'node:http';
|
|
6
|
+
import { toJson } from '@darksheep/request-body';
|
|
7
|
+
|
|
8
|
+
const schema = /** @type {const} */ ({
|
|
9
|
+
type: 'object',
|
|
10
|
+
required: [ 'hello' ],
|
|
11
|
+
properties: {
|
|
12
|
+
hello: {
|
|
13
|
+
enum: [
|
|
14
|
+
'Hello {name}',
|
|
15
|
+
'Whats up {name}',
|
|
16
|
+
'Welcome back {name}',
|
|
17
|
+
],
|
|
18
|
+
default: 'Hello {name}',
|
|
19
|
+
},
|
|
20
|
+
name: { type: 'string' },
|
|
21
|
+
},
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
createServer()
|
|
26
|
+
.on('request', (request, response) => {
|
|
27
|
+
// The schema for data is `{ hello: 'Hello {name}' | 'Whats up {name}' | 'Welcome back {name}'; name?: string; }`
|
|
28
|
+
const {
|
|
29
|
+
hello,
|
|
30
|
+
name = 'my friend'
|
|
31
|
+
} = toJson(request, { schema });
|
|
32
|
+
|
|
33
|
+
response.end(hello.replaceAll('{name}', name));
|
|
34
|
+
})
|
|
35
|
+
.listen(2222);
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Detailed docs:
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
/**
|
|
43
|
+
* @import { IncomingMessage } from 'node:http';
|
|
44
|
+
* @import { ReadableOptions } from 'node:stream';
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef MaxLengthOptions
|
|
49
|
+
* @property {number} [maxBytes] - The maximum number of bytes.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @typedef {(
|
|
54
|
+
* & MaxLengthOptions
|
|
55
|
+
* )} ToBufferOptions
|
|
56
|
+
*/
|
|
57
|
+
/**
|
|
58
|
+
* @param {IncomingMessage} request - The http request object.
|
|
59
|
+
* @param {ToBufferOptions} [options] - The request to buffer options.
|
|
60
|
+
* @returns {Promise<Buffer>}
|
|
61
|
+
*/
|
|
62
|
+
toBuffer(request);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {(
|
|
66
|
+
* & ToBufferOptions
|
|
67
|
+
* & { encoding?: BufferEncoding }
|
|
68
|
+
* )} ToTextOptions
|
|
69
|
+
*/
|
|
70
|
+
/**
|
|
71
|
+
* @param {IncomingMessage} request - The http request object.
|
|
72
|
+
* @param {ToTextOptions} [options] - The options for converting to text.
|
|
73
|
+
* @returns {Promise<string>}
|
|
74
|
+
*/
|
|
75
|
+
toText(request, options);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @typedef {(
|
|
79
|
+
* & ToBufferOptions
|
|
80
|
+
* )} ToFormOptions
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* @param {IncomingMessage} request - The http request object.
|
|
84
|
+
* @param {ToFormOptions} [options] - The request to FormData options.
|
|
85
|
+
* @returns {Promise<FormData>}
|
|
86
|
+
*/
|
|
87
|
+
toForm(request);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @typedef {(
|
|
91
|
+
* & ToTextOptions
|
|
92
|
+
* & { schema: JSONSchema }
|
|
93
|
+
* )} ToJsonOptions
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* @param {IncomingMessage} request - The http request object.
|
|
97
|
+
* @param {ToJsonOptions} [options] - The json options to match.
|
|
98
|
+
* @returns {Promise<FromSchema<JSONSchema> | unknown>}
|
|
99
|
+
*/
|
|
100
|
+
toJson(request, options);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @typedef {(
|
|
104
|
+
* & ToTextOptions
|
|
105
|
+
* )} ToQueryStringOptions
|
|
106
|
+
*/
|
|
107
|
+
/**
|
|
108
|
+
* @param {IncomingMessage} request - The http request object.
|
|
109
|
+
* @param {ToQueryStringOptions} [options] - The request to QueryString options.
|
|
110
|
+
* @returns {Promise<URLSearchParams>}
|
|
111
|
+
*/
|
|
112
|
+
toQuerystring(request);
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @typedef {(
|
|
116
|
+
* & ToBufferOptions
|
|
117
|
+
* & ReadableOptions
|
|
118
|
+
* )} ToStreamOptions
|
|
119
|
+
*/
|
|
120
|
+
/**
|
|
121
|
+
* @param {IncomingMessage} request - The http request object.
|
|
122
|
+
* @param {ToStreamOptions} [options] - The options for the readable stream.
|
|
123
|
+
* @returns {Promise<Readable>}
|
|
124
|
+
*/
|
|
125
|
+
toStream(request, options);
|
|
126
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@darksheep/request-body",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "http.IncomingMessage to something a little more useful",
|
|
5
|
+
"license": "UNLICENCED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./types/index.d.ts",
|
|
10
|
+
"import": "./src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./buffer": {
|
|
13
|
+
"types": "./types/buffer.d.ts",
|
|
14
|
+
"import": "./src/buffer.js"
|
|
15
|
+
},
|
|
16
|
+
"./form": {
|
|
17
|
+
"types": "./types/form.d.ts",
|
|
18
|
+
"import": "./src/form.js"
|
|
19
|
+
},
|
|
20
|
+
"./json": {
|
|
21
|
+
"types": "./types/json.d.ts",
|
|
22
|
+
"import": "./src/json.js"
|
|
23
|
+
},
|
|
24
|
+
"./querystring": {
|
|
25
|
+
"types": "./types/querystring.d.ts",
|
|
26
|
+
"import": "./src/querystring.js"
|
|
27
|
+
},
|
|
28
|
+
"./stream": {
|
|
29
|
+
"types": "./types/stream.d.ts",
|
|
30
|
+
"import": "./src/stream.js"
|
|
31
|
+
},
|
|
32
|
+
"./text": {
|
|
33
|
+
"types": "./types/text.d.ts",
|
|
34
|
+
"import": "./src/text.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"src",
|
|
39
|
+
"types",
|
|
40
|
+
"!*.test.*"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"prepack": "yarn dlx -q -p typescript tsc",
|
|
44
|
+
"test": "node --test src/**/*.test.js",
|
|
45
|
+
"test:watch": "node --watch --test src/**/*.test.js"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@darksheep/errors": "1.1.1",
|
|
49
|
+
"@darksheep/json-schema": "",
|
|
50
|
+
"@darksheep/types": "",
|
|
51
|
+
"@fastify/busboy": "3.1.1"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@darksheep/fake-fetch": ""
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": "^20.13.1 || ^22.2.0"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/buffer.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Duplex, Readable } from 'node:stream'
|
|
3
|
+
* @import { IncomingMessage } from 'node:http'
|
|
4
|
+
* @import { MaxLengthOptions } from './utilities/max-length.js'
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { buffer } from 'node:stream/consumers';
|
|
8
|
+
import { limitMaxLength } from './utilities/max-length.js';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
createBrotliDecompress,
|
|
12
|
+
createGunzip,
|
|
13
|
+
createInflate,
|
|
14
|
+
} from 'node:zlib';
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
InternalServerError,
|
|
18
|
+
UnsupportedMediaType,
|
|
19
|
+
} from '@darksheep/errors';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {(
|
|
23
|
+
* & MaxLengthOptions
|
|
24
|
+
* )} ToBufferOptions
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** @type {WeakMap<IncomingMessage, Promise<Buffer>>} */
|
|
28
|
+
const cache = new WeakMap();
|
|
29
|
+
|
|
30
|
+
/** @type {Map<'br' | 'gzip'| 'deflate', () => Duplex>} */
|
|
31
|
+
const compressionMap = new Map([
|
|
32
|
+
[ 'br', createBrotliDecompress ],
|
|
33
|
+
[ 'gzip', createGunzip ],
|
|
34
|
+
[ 'deflate', createInflate ],
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {unknown} directive - The possible directive.
|
|
39
|
+
* @returns {directive is ('br' | 'gzip'| 'deflate')}
|
|
40
|
+
*/
|
|
41
|
+
function isValidCompression(directive) {
|
|
42
|
+
return typeof directive === 'string' &&
|
|
43
|
+
[ 'br', 'gzip', 'deflate' ].includes(directive);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {IncomingMessage} request - The http request object.
|
|
48
|
+
* @returns {('br' | 'gzip'| 'deflate')[]}
|
|
49
|
+
*/
|
|
50
|
+
function getDecompressDirectives({ headers }) {
|
|
51
|
+
if (headers['content-encoding'] == null) {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const directives = headers['content-encoding']
|
|
56
|
+
.split(',')
|
|
57
|
+
.map((directive) => directive.trim());
|
|
58
|
+
|
|
59
|
+
if (directives.every(isValidCompression)) {
|
|
60
|
+
return directives;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
throw new UnsupportedMediaType(`Invalid Content-Encoding: ${headers['content-encoding']}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {IncomingMessage} request - The http request object.
|
|
68
|
+
* @param {ToBufferOptions} [options] - The request to buffer options.
|
|
69
|
+
* @returns {Promise<Buffer>}
|
|
70
|
+
*/
|
|
71
|
+
function getBuffer(request, options) {
|
|
72
|
+
/** @type {Readable} */
|
|
73
|
+
let stream = request;
|
|
74
|
+
|
|
75
|
+
for (const directive of getDecompressDirectives(request)) {
|
|
76
|
+
const createDuplex = compressionMap.get(directive);
|
|
77
|
+
if (createDuplex == null) {
|
|
78
|
+
throw new InternalServerError('Cannot create Decompression stream');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
stream = stream.pipe(createDuplex());
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// -1 is explicitly disabling the maxBytes
|
|
85
|
+
if (options?.maxBytes !== -1) {
|
|
86
|
+
stream = stream.pipe(limitMaxLength(options));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return buffer(stream);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param {IncomingMessage} request - The http request object.
|
|
94
|
+
* @param {MaxLengthOptions} [options] - The request to buffer options.
|
|
95
|
+
* @returns {Promise<Buffer>}
|
|
96
|
+
*/
|
|
97
|
+
export async function toBuffer(request, options) {
|
|
98
|
+
if (cache.has(request) === false) {
|
|
99
|
+
cache.set(request, getBuffer(request, options));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const buffer = await cache.get(request);
|
|
103
|
+
if (buffer == null) {
|
|
104
|
+
throw new InternalServerError('The buffer has been discarded lost');
|
|
105
|
+
}
|
|
106
|
+
return buffer;
|
|
107
|
+
}
|
package/src/form.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { IncomingMessage } from 'node:http'
|
|
3
|
+
* @import { ToBufferOptions } from './buffer.js'
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BadRequest } from '@darksheep/errors';
|
|
7
|
+
import { fromAsync } from '@darksheep/types';
|
|
8
|
+
import { Busboy } from '@fastify/busboy';
|
|
9
|
+
|
|
10
|
+
import { toBuffer } from './buffer.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {(
|
|
14
|
+
* & ToBufferOptions
|
|
15
|
+
* )} ToFormOptions
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {IncomingMessage} request - The http request object.
|
|
20
|
+
* @param {string} headerName - The target header.
|
|
21
|
+
* @returns {string}
|
|
22
|
+
*/
|
|
23
|
+
function assertiveGetHeader(request, headerName) {
|
|
24
|
+
const header = request.headers[headerName];
|
|
25
|
+
if (typeof header === 'string') {
|
|
26
|
+
return header;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
throw new BadRequest(`Missing required header: ${headerName}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {IncomingMessage} request - The http request object.
|
|
34
|
+
* @param {ToFormOptions} [options] - The request to FormData options.
|
|
35
|
+
* @returns {Promise<FormData>}
|
|
36
|
+
*/
|
|
37
|
+
export async function toForm(request, options) {
|
|
38
|
+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
39
|
+
const formData = new FormData();
|
|
40
|
+
const busboy = new Busboy({
|
|
41
|
+
headers: {
|
|
42
|
+
...request.headers,
|
|
43
|
+
'content-type': assertiveGetHeader(request, 'content-type'),
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
const body = await toBuffer(request, options);
|
|
47
|
+
|
|
48
|
+
/** @type {Promise<unknown>[]} */
|
|
49
|
+
const pQueue = [];
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
busboy
|
|
52
|
+
.on('file', (fieldname, stream, filename, _encoding, mimetype) => {
|
|
53
|
+
pQueue.push((async () => {
|
|
54
|
+
const file = new File(
|
|
55
|
+
await fromAsync(stream.iterator()),
|
|
56
|
+
filename,
|
|
57
|
+
{ type: mimetype },
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
formData.append(fieldname, file, filename);
|
|
61
|
+
})());
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
.on('field', (fieldname, value) => {
|
|
65
|
+
formData.append(fieldname, value);
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
.on('finish', async () => {
|
|
69
|
+
try {
|
|
70
|
+
await Promise.all(pQueue);
|
|
71
|
+
resolve(formData);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
reject(error);
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
.on('error', reject)
|
|
77
|
+
.end(body);
|
|
78
|
+
});
|
|
79
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { toBuffer } from './buffer.js';
|
|
2
|
+
export { toJson } from './json.js';
|
|
3
|
+
export { toStream } from './stream.js';
|
|
4
|
+
export { toText } from './text.js';
|
|
5
|
+
export {
|
|
6
|
+
toForm,
|
|
7
|
+
toForm as toFormData,
|
|
8
|
+
} from './form.js';
|
|
9
|
+
export {
|
|
10
|
+
toQuerystring,
|
|
11
|
+
toQuerystring as toUrlEncoded,
|
|
12
|
+
} from './querystring.js';
|
package/src/json.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { IncomingMessage } from 'node:http';
|
|
3
|
+
* @import { FromSchema, JSONSchema } from '@darksheep/json-schema';
|
|
4
|
+
* @import { ToTextOptions } from './text.js';
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BadRequest } from '@darksheep/errors';
|
|
8
|
+
import { validate, RequestSchemaError } from '@darksheep/json-schema';
|
|
9
|
+
|
|
10
|
+
import { toText } from './text.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {(
|
|
14
|
+
* & ToTextOptions
|
|
15
|
+
* & { schema: JSONSchema }
|
|
16
|
+
* )} ToJsonOptions
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} input - The raw input string.
|
|
21
|
+
* @returns {unknown}
|
|
22
|
+
*/
|
|
23
|
+
function parseJson(input) {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(input);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (error instanceof Error === false) {
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
throw new BadRequest(
|
|
32
|
+
`[${error.name}] ${error.message}`,
|
|
33
|
+
{ cause: error },
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @template {JSONSchema} const Schema - The raw JSON schema.
|
|
40
|
+
* @template [Data=FromSchema<Schema>] - The typescript type that matches the JSON Schema.
|
|
41
|
+
* @param {IncomingMessage} request - The http request object.
|
|
42
|
+
* @param {ToJsonOptions & { schema: Schema }} [options] - The json options to match.
|
|
43
|
+
* @returns {Promise<Data>}
|
|
44
|
+
*/
|
|
45
|
+
export async function toJson(request, options) {
|
|
46
|
+
const string = await toText(request, options);
|
|
47
|
+
const data = parseJson(string);
|
|
48
|
+
|
|
49
|
+
if (options?.schema == null) {
|
|
50
|
+
return /** @type {Data} */ (data);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return validate(options.schema, data, RequestSchemaError);
|
|
54
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { IncomingMessage } from 'node:http'
|
|
3
|
+
* @import { ToTextOptions } from './text.js'
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { URLSearchParams } from 'node:url';
|
|
7
|
+
import { toText } from './text.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {(
|
|
11
|
+
* & ToTextOptions
|
|
12
|
+
* )} ToQueryStringOptions
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {IncomingMessage} request - The http request object.
|
|
17
|
+
* @param {ToQueryStringOptions} [options] - The request to QueryString options.
|
|
18
|
+
* @returns {Promise<URLSearchParams>}
|
|
19
|
+
*/
|
|
20
|
+
export async function toQuerystring(request, options) {
|
|
21
|
+
return new URLSearchParams(
|
|
22
|
+
await toText(request, options),
|
|
23
|
+
);
|
|
24
|
+
}
|
package/src/stream.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { IncomingMessage } from 'node:http'
|
|
3
|
+
* @import { ReadableOptions } from 'node:stream'
|
|
4
|
+
* @import { ToBufferOptions } from './buffer.js'
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
import { toBuffer } from './buffer.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {(
|
|
12
|
+
* & ToBufferOptions
|
|
13
|
+
* & ReadableOptions
|
|
14
|
+
* )} ToStreamOptions
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {IncomingMessage} request - The http request object.
|
|
19
|
+
* @param {ToStreamOptions} [options] - The options for the readable stream.
|
|
20
|
+
* @returns {Promise<Readable>}
|
|
21
|
+
*/
|
|
22
|
+
export async function toStream(request, options) {
|
|
23
|
+
return Readable.from(await toBuffer(request, options), options);
|
|
24
|
+
}
|
package/src/text.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { IncomingMessage } from 'node:http'
|
|
3
|
+
* @import { ToBufferOptions } from './buffer.js'
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { toBuffer } from './buffer.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {(
|
|
10
|
+
* & ToBufferOptions
|
|
11
|
+
* & { encoding?: BufferEncoding }
|
|
12
|
+
* )} ToTextOptions
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {IncomingMessage} request - The http request object.
|
|
17
|
+
* @param {ToTextOptions} [options] - The options for converting to text.
|
|
18
|
+
* @returns {Promise<string>}
|
|
19
|
+
*/
|
|
20
|
+
export async function toText(request, options) {
|
|
21
|
+
const buffer = await toBuffer(request, options);
|
|
22
|
+
return buffer.toString(options?.encoding);
|
|
23
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @type {WeakMap<Transform, number>};
|
|
5
|
+
*/
|
|
6
|
+
const lengths = new WeakMap();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef MaxLengthOptions
|
|
10
|
+
* @property {number} [maxBytes] - The maximum number of bytes.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {MaxLengthOptions} [options] - The maximum length of the stream allowed.
|
|
15
|
+
* @returns {Transform}
|
|
16
|
+
*/
|
|
17
|
+
export function limitMaxLength({ maxBytes = 1_000_000 } = {}) {
|
|
18
|
+
return new Transform({
|
|
19
|
+
transform(chunk, _encoding, callback) {
|
|
20
|
+
if (Buffer.isBuffer(chunk) === false) {
|
|
21
|
+
return callback(new Error('Cannot chunk, not buffer'));
|
|
22
|
+
}
|
|
23
|
+
const current = lengths.get(this) ?? 0;
|
|
24
|
+
const next = current + chunk.length;
|
|
25
|
+
lengths.set(this, next);
|
|
26
|
+
|
|
27
|
+
if (next > maxBytes) {
|
|
28
|
+
return callback(new Error('Maximum stream length exceeded'));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
callback(null, chunk);
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {IncomingMessage} request - The http request object.
|
|
3
|
+
* @param {MaxLengthOptions} [options] - The request to buffer options.
|
|
4
|
+
* @returns {Promise<Buffer>}
|
|
5
|
+
*/
|
|
6
|
+
export function toBuffer(request: IncomingMessage, options?: MaxLengthOptions): Promise<Buffer>;
|
|
7
|
+
export type ToBufferOptions = (MaxLengthOptions);
|
|
8
|
+
import type { IncomingMessage } from 'node:http';
|
|
9
|
+
import type { MaxLengthOptions } from './utilities/max-length.js';
|
package/types/form.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {IncomingMessage} request - The http request object.
|
|
3
|
+
* @param {ToFormOptions} [options] - The request to FormData options.
|
|
4
|
+
* @returns {Promise<FormData>}
|
|
5
|
+
*/
|
|
6
|
+
export function toForm(request: IncomingMessage, options?: ToFormOptions): Promise<FormData>;
|
|
7
|
+
export type ToFormOptions = (ToBufferOptions);
|
|
8
|
+
import type { IncomingMessage } from 'node:http';
|
|
9
|
+
import type { ToBufferOptions } from './buffer.js';
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { toBuffer } from "./buffer.js";
|
|
2
|
+
export { toJson } from "./json.js";
|
|
3
|
+
export { toStream } from "./stream.js";
|
|
4
|
+
export { toText } from "./text.js";
|
|
5
|
+
export { toForm, toForm as toFormData } from "./form.js";
|
|
6
|
+
export { toQuerystring, toQuerystring as toUrlEncoded } from "./querystring.js";
|
package/types/json.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {JSONSchema} const Schema - The raw JSON schema.
|
|
3
|
+
* @template [Data=FromSchema<Schema>] - The typescript type that matches the JSON Schema.
|
|
4
|
+
* @param {IncomingMessage} request - The http request object.
|
|
5
|
+
* @param {ToJsonOptions & { schema: Schema }} [options] - The json options to match.
|
|
6
|
+
* @returns {Promise<Data>}
|
|
7
|
+
*/
|
|
8
|
+
export function toJson<const Schema extends JSONSchema, Data = FromSchema<Schema>>(request: IncomingMessage, options?: ToJsonOptions & {
|
|
9
|
+
schema: Schema;
|
|
10
|
+
}): Promise<Data>;
|
|
11
|
+
export type ToJsonOptions = (ToTextOptions & {
|
|
12
|
+
schema: JSONSchema;
|
|
13
|
+
});
|
|
14
|
+
import type { JSONSchema } from '@darksheep/json-schema';
|
|
15
|
+
import type { FromSchema } from '@darksheep/json-schema';
|
|
16
|
+
import type { IncomingMessage } from 'node:http';
|
|
17
|
+
import type { ToTextOptions } from './text.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {(
|
|
3
|
+
* & ToTextOptions
|
|
4
|
+
* )} ToQueryStringOptions
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* @param {IncomingMessage} request - The http request object.
|
|
8
|
+
* @param {ToQueryStringOptions} [options] - The request to QueryString options.
|
|
9
|
+
* @returns {Promise<URLSearchParams>}
|
|
10
|
+
*/
|
|
11
|
+
export function toQuerystring(request: IncomingMessage, options?: ToQueryStringOptions): Promise<URLSearchParams>;
|
|
12
|
+
export type ToQueryStringOptions = (ToTextOptions);
|
|
13
|
+
import type { IncomingMessage } from 'node:http';
|
|
14
|
+
import { URLSearchParams } from 'node:url';
|
|
15
|
+
import type { ToTextOptions } from './text.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {(
|
|
3
|
+
* & ToBufferOptions
|
|
4
|
+
* & ReadableOptions
|
|
5
|
+
* )} ToStreamOptions
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @param {IncomingMessage} request - The http request object.
|
|
9
|
+
* @param {ToStreamOptions} [options] - The options for the readable stream.
|
|
10
|
+
* @returns {Promise<Readable>}
|
|
11
|
+
*/
|
|
12
|
+
export function toStream(request: IncomingMessage, options?: ToStreamOptions): Promise<Readable>;
|
|
13
|
+
export type ToStreamOptions = (ToBufferOptions & ReadableOptions);
|
|
14
|
+
import type { IncomingMessage } from 'node:http';
|
|
15
|
+
import { Readable } from 'node:stream';
|
|
16
|
+
import type { ToBufferOptions } from './buffer.js';
|
|
17
|
+
import type { ReadableOptions } from 'node:stream';
|
package/types/text.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {(
|
|
3
|
+
* & ToBufferOptions
|
|
4
|
+
* & { encoding?: BufferEncoding }
|
|
5
|
+
* )} ToTextOptions
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @param {IncomingMessage} request - The http request object.
|
|
9
|
+
* @param {ToTextOptions} [options] - The options for converting to text.
|
|
10
|
+
* @returns {Promise<string>}
|
|
11
|
+
*/
|
|
12
|
+
export function toText(request: IncomingMessage, options?: ToTextOptions): Promise<string>;
|
|
13
|
+
export type ToTextOptions = (ToBufferOptions & {
|
|
14
|
+
encoding?: BufferEncoding;
|
|
15
|
+
});
|
|
16
|
+
import type { IncomingMessage } from 'node:http';
|
|
17
|
+
import type { ToBufferOptions } from './buffer.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef MaxLengthOptions
|
|
3
|
+
* @property {number} [maxBytes] - The maximum number of bytes.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @param {MaxLengthOptions} [options] - The maximum length of the stream allowed.
|
|
7
|
+
* @returns {Transform}
|
|
8
|
+
*/
|
|
9
|
+
export function limitMaxLength({ maxBytes }?: MaxLengthOptions): Transform;
|
|
10
|
+
export type MaxLengthOptions = {
|
|
11
|
+
/**
|
|
12
|
+
* - The maximum number of bytes.
|
|
13
|
+
*/
|
|
14
|
+
maxBytes?: number;
|
|
15
|
+
};
|
|
16
|
+
import { Transform } from 'node:stream';
|