webhoster 0.3.3 → 0.3.5
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 +4 -4
- package/data/CookieObject.js +1 -1
- package/{types/index.js → data/custom-types.js} +1 -1
- package/{types/typings.d.ts → data/middleware.d.ts} +1 -1
- package/helpers/RequestReader.js +1 -1
- package/helpers/ResponseHeaders.js +9 -10
- package/lib/HttpHandler.js +4 -4
- package/lib/HttpRequest.js +2 -2
- package/lib/HttpResponse.js +2 -2
- package/lib/HttpTransaction.js +2 -2
- package/middleware/AutoHeadersMiddleware.js +2 -2
- package/middleware/CORSMiddleware.js +3 -3
- package/middleware/CaseInsensitiveHeadersMiddleware.js +2 -2
- package/middleware/ContentDecoderMiddleware.js +1 -1
- package/middleware/ContentEncoderMiddleware.js +2 -2
- package/middleware/ContentLengthMiddleware.js +2 -2
- package/middleware/HashMiddleware.js +2 -2
- package/middleware/HeadMethodMiddleware.js +3 -3
- package/middleware/MethodMiddleware.js +3 -3
- package/middleware/PathMiddleware.js +4 -4
- package/middleware/ReadFormData.js +1 -1
- package/middleware/SendJsonMiddleware.js +4 -4
- package/middleware/SendStringMiddleware.js +3 -3
- package/package.json +21 -4
- package/templates/starter.js +2 -2
- package/types/data/CookieObject.d.ts +34 -0
- package/types/data/CookieObject.d.ts.map +1 -0
- package/types/data/custom-types.d.ts +111 -0
- package/types/data/custom-types.d.ts.map +1 -0
- package/types/errata/socketio.d.ts +11 -0
- package/types/errata/socketio.d.ts.map +1 -0
- package/types/examples/starter.d.ts +2 -0
- package/types/examples/starter.d.ts.map +1 -0
- package/types/helpers/HeadersParser.d.ts +27 -0
- package/types/helpers/HeadersParser.d.ts.map +1 -0
- package/types/helpers/HttpListener.d.ts +84 -0
- package/types/helpers/HttpListener.d.ts.map +1 -0
- package/types/helpers/RequestHeaders.d.ts +25 -0
- package/types/helpers/RequestHeaders.d.ts.map +1 -0
- package/types/helpers/RequestReader.d.ts +52 -0
- package/types/helpers/RequestReader.d.ts.map +1 -0
- package/types/helpers/ResponseHeaders.d.ts +126 -0
- package/types/helpers/ResponseHeaders.d.ts.map +1 -0
- package/types/helpers/index.d.ts +6 -0
- package/types/helpers/index.d.ts.map +1 -0
- package/types/lib/HttpHandler.d.ts +79 -0
- package/types/lib/HttpHandler.d.ts.map +1 -0
- package/types/lib/HttpRequest.d.ts +175 -0
- package/types/lib/HttpRequest.d.ts.map +1 -0
- package/types/lib/HttpResponse.d.ts +117 -0
- package/types/lib/HttpResponse.d.ts.map +1 -0
- package/types/lib/HttpTransaction.d.ts +114 -0
- package/types/lib/HttpTransaction.d.ts.map +1 -0
- package/types/middleware/AutoHeadersMiddleware.d.ts +27 -0
- package/types/middleware/AutoHeadersMiddleware.d.ts.map +1 -0
- package/types/middleware/CORSMiddleware.d.ts +65 -0
- package/types/middleware/CORSMiddleware.d.ts.map +1 -0
- package/types/middleware/CaseInsensitiveHeadersMiddleware.d.ts +27 -0
- package/types/middleware/CaseInsensitiveHeadersMiddleware.d.ts.map +1 -0
- package/types/middleware/ContentDecoderMiddleware.d.ts +17 -0
- package/types/middleware/ContentDecoderMiddleware.d.ts.map +1 -0
- package/types/middleware/ContentEncoderMiddleware.d.ts +48 -0
- package/types/middleware/ContentEncoderMiddleware.d.ts.map +1 -0
- package/types/middleware/ContentLengthMiddleware.d.ts +38 -0
- package/types/middleware/ContentLengthMiddleware.d.ts.map +1 -0
- package/types/middleware/HashMiddleware.d.ts +28 -0
- package/types/middleware/HashMiddleware.d.ts.map +1 -0
- package/types/middleware/HeadMethodMiddleware.d.ts +12 -0
- package/types/middleware/HeadMethodMiddleware.d.ts.map +1 -0
- package/types/middleware/MethodMiddleware.d.ts +40 -0
- package/types/middleware/MethodMiddleware.d.ts.map +1 -0
- package/types/middleware/PathMiddleware.d.ts +81 -0
- package/types/middleware/PathMiddleware.d.ts.map +1 -0
- package/types/middleware/ReadFormData.d.ts +7 -0
- package/types/middleware/ReadFormData.d.ts.map +1 -0
- package/types/middleware/SendJsonMiddleware.d.ts +51 -0
- package/types/middleware/SendJsonMiddleware.d.ts.map +1 -0
- package/types/middleware/SendStringMiddleware.d.ts +35 -0
- package/types/middleware/SendStringMiddleware.d.ts.map +1 -0
- package/types/templates/starter.d.ts +15 -0
- package/types/templates/starter.d.ts.map +1 -0
- package/types/utils/AsyncObject.d.ts +38 -0
- package/types/utils/AsyncObject.d.ts.map +1 -0
- package/types/utils/CaseInsensitiveObject.d.ts +9 -0
- package/types/utils/CaseInsensitiveObject.d.ts.map +1 -0
- package/types/utils/function.d.ts +2 -0
- package/types/utils/function.d.ts.map +1 -0
- package/types/utils/headers.d.ts +7 -0
- package/types/utils/headers.d.ts.map +1 -0
- package/types/utils/qualityValues.d.ts +12 -0
- package/types/utils/qualityValues.d.ts.map +1 -0
- package/types/utils/stream.d.ts +6 -0
- package/types/utils/stream.d.ts.map +1 -0
- package/utils/headers.js +1 -1
- package/.eslintrc.json +0 -96
- package/.github/FUNDING.yml +0 -4
- package/.github/copilot-instructions.md +0 -100
- package/.github/workflows/publish.yml +0 -29
- package/.github/workflows/test-matrix.yml +0 -37
- package/.test/benchmark.js +0 -28
- package/.test/constants.js +0 -4
- package/.test/cookietester.js +0 -19
- package/.test/http2server.js +0 -35
- package/.test/httpserver.js +0 -32
- package/.test/index.js +0 -498
- package/.test/multipromise.js +0 -32
- package/.test/tls.js +0 -75
- package/.test/urlencoded.js +0 -54
- package/.vscode/launch.json +0 -35
- package/.vscode/settings.json +0 -5
- package/CHANGELOG.md +0 -10
- package/CODE_OF_CONDUCT.md +0 -76
- package/errata/index.js +0 -1
- package/examples/starter.js +0 -11
- package/index.js +0 -4
- package/jsconfig.json +0 -12
- package/lib/index.js +0 -3
- package/middleware/ContentReaderMiddleware.js +0 -249
- package/middleware/ContentWriterMiddleware.js +0 -161
- package/middleware/SendHeadersMiddleware.js +0 -47
- package/middleware/index.js +0 -11
- package/polyfill/FormData.js +0 -164
- package/rollup.config.js +0 -8
- package/scripts/check-teapot.mjs +0 -40
- package/scripts/test-all-sync.sh +0 -6
- package/scripts/test-all.sh +0 -7
- package/test/fixtures/stream.js +0 -68
- package/test/helpers/HttpListener/construct.js +0 -18
- package/test/helpers/HttpListener/customOptions.js +0 -22
- package/test/helpers/HttpListener/doubleCreate.js +0 -40
- package/test/helpers/HttpListener/events.js +0 -77
- package/test/helpers/HttpListener/http.js +0 -31
- package/test/helpers/HttpListener/http2.js +0 -41
- package/test/helpers/HttpListener/https.js +0 -38
- package/test/helpers/HttpListener/startAll.js +0 -31
- package/test/helpers/HttpListener/stopNotStarted.js +0 -23
- package/test/lib/HttpHandler/class.js +0 -8
- package/test/lib/HttpHandler/handleRequest.js +0 -11
- package/test/lib/HttpHandler/middleware.js +0 -941
- package/test/lib/HttpHandler/parse.js +0 -41
- package/test/lib/HttpRequest/class.js +0 -8
- package/test/lib/HttpRequest/downstream.js +0 -171
- package/test/lib/HttpRequest/properties.js +0 -101
- package/test/lib/HttpRequest/read.js +0 -518
- package/test/lib/HttpResponse/async-iterable-middleware.js +0 -37
- package/test/lib/HttpResponse/async-iterable.js +0 -31
- package/test/lib/HttpResponse/class.js +0 -8
- package/test/lib/HttpResponse/properties.js +0 -59
- package/test/lib/HttpResponse/send.js +0 -275
- package/test/lib/HttpTransaction/class.js +0 -8
- package/test/lib/HttpTransaction/ping.js +0 -50
- package/test/lib/HttpTransaction/push.js +0 -89
- package/test/middleware/SendJsonMiddleware.js +0 -222
- package/test/sanity.js +0 -10
- package/test/templates/error-teapot.js +0 -47
- package/test/templates/starter.js +0 -93
- package/tsconfig.json +0 -30
package/.test/urlencoded.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param buffer
|
|
3
|
-
*/
|
|
4
|
-
function decode(buffer) {
|
|
5
|
-
const sequences = [];
|
|
6
|
-
let startIndex = 0;
|
|
7
|
-
for (let i = 0; i < buffer.length; i += 1) {
|
|
8
|
-
if (buffer[i] === 0x26) {
|
|
9
|
-
sequences.push(buffer.subarray(startIndex, i));
|
|
10
|
-
startIndex = i + 1;
|
|
11
|
-
}
|
|
12
|
-
if (i === buffer.length - 1) {
|
|
13
|
-
sequences.push(buffer.subarray(startIndex, i));
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
console.log(buffer);
|
|
17
|
-
console.log(sequences);
|
|
18
|
-
/** @type {[string, string][]} */
|
|
19
|
-
const output = [];
|
|
20
|
-
sequences.forEach((bytes) => {
|
|
21
|
-
if (!bytes.length) return;
|
|
22
|
-
|
|
23
|
-
// Find 0x3D and replace 0x2B in one loop for better performance
|
|
24
|
-
let indexOf0x3D = -1;
|
|
25
|
-
for (let i = 0; i < bytes.length; i += 1) {
|
|
26
|
-
switch (bytes[i]) {
|
|
27
|
-
case 0x3D:
|
|
28
|
-
if (indexOf0x3D === -1) {
|
|
29
|
-
indexOf0x3D = i;
|
|
30
|
-
}
|
|
31
|
-
break;
|
|
32
|
-
case 0x2B:
|
|
33
|
-
bytes[i] = 0x20;
|
|
34
|
-
break;
|
|
35
|
-
default:
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
let name;
|
|
39
|
-
let value;
|
|
40
|
-
if (indexOf0x3D === -1) {
|
|
41
|
-
name = bytes;
|
|
42
|
-
value = bytes.subarray(bytes.length, 0);
|
|
43
|
-
} else {
|
|
44
|
-
name = bytes.subarray(0, indexOf0x3D);
|
|
45
|
-
value = bytes.subarray(indexOf0x3D + 1);
|
|
46
|
-
}
|
|
47
|
-
const nameString = decodeURIComponent(name.toString('utf-8'));
|
|
48
|
-
const valueString = decodeURIComponent(value.toString('utf-8'));
|
|
49
|
-
output.push([nameString, valueString]);
|
|
50
|
-
});
|
|
51
|
-
return output;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
console.log(decode(Buffer.from('field1=Ƣvalue1&field2=value2&&&=&==')));
|
package/.vscode/launch.json
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Use IntelliSense to learn about possible attributes.
|
|
3
|
-
// Hover to view descriptions of existing attributes.
|
|
4
|
-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"name": "Debug AVA",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"runtimeArgs": [
|
|
11
|
-
"run-script",
|
|
12
|
-
"debug-test"
|
|
13
|
-
],
|
|
14
|
-
"runtimeExecutable": "npm",
|
|
15
|
-
"skipFiles": [
|
|
16
|
-
"<node_internals>/**"
|
|
17
|
-
],
|
|
18
|
-
"type": "pwa-node"
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
"type": "node",
|
|
22
|
-
"request": "launch",
|
|
23
|
-
"name": "Debug AVA test file",
|
|
24
|
-
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
|
|
25
|
-
"runtimeArgs": [
|
|
26
|
-
"--serial",
|
|
27
|
-
"${file}"
|
|
28
|
-
],
|
|
29
|
-
"outputCapture": "std",
|
|
30
|
-
"skipFiles": [
|
|
31
|
-
"<node_internals>/**/*.js"
|
|
32
|
-
]
|
|
33
|
-
},
|
|
34
|
-
]
|
|
35
|
-
}
|
package/.vscode/settings.json
DELETED
package/CHANGELOG.md
DELETED
package/CODE_OF_CONDUCT.md
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
|
2
|
-
|
|
3
|
-
## Our Pledge
|
|
4
|
-
|
|
5
|
-
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
-
contributors and maintainers pledge to making participation in our project and
|
|
7
|
-
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
-
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
|
9
|
-
level of experience, education, socio-economic status, nationality, personal
|
|
10
|
-
appearance, race, religion, or sexual identity and orientation.
|
|
11
|
-
|
|
12
|
-
## Our Standards
|
|
13
|
-
|
|
14
|
-
Examples of behavior that contributes to creating a positive environment
|
|
15
|
-
include:
|
|
16
|
-
|
|
17
|
-
* Using welcoming and inclusive language
|
|
18
|
-
* Being respectful of differing viewpoints and experiences
|
|
19
|
-
* Gracefully accepting constructive criticism
|
|
20
|
-
* Focusing on what is best for the community
|
|
21
|
-
* Showing empathy towards other community members
|
|
22
|
-
|
|
23
|
-
Examples of unacceptable behavior by participants include:
|
|
24
|
-
|
|
25
|
-
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
-
advances
|
|
27
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
-
* Public or private harassment
|
|
29
|
-
* Publishing others' private information, such as a physical or electronic
|
|
30
|
-
address, without explicit permission
|
|
31
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
-
professional setting
|
|
33
|
-
|
|
34
|
-
## Our Responsibilities
|
|
35
|
-
|
|
36
|
-
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
-
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
-
response to any instances of unacceptable behavior.
|
|
39
|
-
|
|
40
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
-
threatening, offensive, or harmful.
|
|
45
|
-
|
|
46
|
-
## Scope
|
|
47
|
-
|
|
48
|
-
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
-
when an individual is representing the project or its community. Examples of
|
|
50
|
-
representing a project or community include using an official project e-mail
|
|
51
|
-
address, posting via an official social media account, or acting as an appointed
|
|
52
|
-
representative at an online or offline event. Representation of a project may be
|
|
53
|
-
further defined and clarified by project maintainers.
|
|
54
|
-
|
|
55
|
-
## Enforcement
|
|
56
|
-
|
|
57
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
-
reported by contacting the project team at clshortfuse@gmail.com. All
|
|
59
|
-
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
-
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
-
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
-
Further details of specific enforcement policies may be posted separately.
|
|
63
|
-
|
|
64
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
-
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
-
members of the project's leadership.
|
|
67
|
-
|
|
68
|
-
## Attribution
|
|
69
|
-
|
|
70
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
-
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
|
72
|
-
|
|
73
|
-
[homepage]: https://www.contributor-covenant.org
|
|
74
|
-
|
|
75
|
-
For answers to common questions about this code of conduct, see
|
|
76
|
-
https://www.contributor-covenant.org/faq
|
package/errata/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * as socketio from './socketio.js';
|
package/examples/starter.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as starter from '../templates/starter.js';
|
|
2
|
-
|
|
3
|
-
const middleware = [
|
|
4
|
-
() => 'Hello world!',
|
|
5
|
-
];
|
|
6
|
-
|
|
7
|
-
// Start the singleton HTTP server using the starter convenience API.
|
|
8
|
-
await starter.start({ middleware });
|
|
9
|
-
|
|
10
|
-
// Export nothing — this example file is intended as a tiny runnable demo.
|
|
11
|
-
export {};
|
package/index.js
DELETED
package/jsconfig.json
DELETED
package/lib/index.js
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
import { PassThrough, Transform } from 'stream';
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('stream').Readable} Readable */
|
|
4
|
-
/** @typedef {import('../types').IMiddleware} IMiddleware */
|
|
5
|
-
/** @typedef {import('../types').MiddlewareFunction} MiddlewareFunction */
|
|
6
|
-
/** @typedef {import('../types').MiddlewareFunctionParams} MiddlewareFunctionParams */
|
|
7
|
-
/** @typedef {import('../types').MiddlewareFunctionResult} MiddlewareFunctionResult */
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {Object} ContentReaderMiddlewareOptions
|
|
11
|
-
* @prop {string} [defaultMediaType]
|
|
12
|
-
* Assumed mediatype if not specified
|
|
13
|
-
* @prop {boolean} [parseJSON=false]
|
|
14
|
-
* Automatically parses JSON if `application/json` mediatype
|
|
15
|
-
* @prop {'entries'|'object'|'string'|'none'} [formURLEncodedFormat='none']
|
|
16
|
-
* Automatically converts to object if `application/x-www-form-urlencoded` mediatype
|
|
17
|
-
* @prop {boolean} [buildString=false]
|
|
18
|
-
* Automatically builds string into one `read()` response
|
|
19
|
-
* @prop {boolean|string} [cache=false]
|
|
20
|
-
* Caches content in req.local.content or req.local[cacheName]
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/** @implements {IMiddleware} */
|
|
24
|
-
export default class ContentReaderMiddleware {
|
|
25
|
-
/** @param {ContentReaderMiddlewareOptions} [options] */
|
|
26
|
-
constructor(options = {}) {
|
|
27
|
-
this.defaultMediaType = options.defaultMediaType;
|
|
28
|
-
this.parseJSON = options.parseJSON === true;
|
|
29
|
-
this.formURLEncodedFormat = options.formURLEncodedFormat || 'none';
|
|
30
|
-
this.buildString = options.buildString === true;
|
|
31
|
-
this.cache = options.cache;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @param {string} charset
|
|
36
|
-
* @return {BufferEncoding}
|
|
37
|
-
*/
|
|
38
|
-
static charsetAsBufferEncoding(charset) {
|
|
39
|
-
switch (charset) {
|
|
40
|
-
case 'iso-8859-1':
|
|
41
|
-
case 'ascii':
|
|
42
|
-
case 'binary':
|
|
43
|
-
case 'latin1':
|
|
44
|
-
return 'latin1';
|
|
45
|
-
case 'utf-16le':
|
|
46
|
-
case 'ucs-2':
|
|
47
|
-
case 'ucs2':
|
|
48
|
-
case 'utf16le':
|
|
49
|
-
return 'utf16le';
|
|
50
|
-
default:
|
|
51
|
-
case 'utf-8':
|
|
52
|
-
case 'utf8':
|
|
53
|
-
return 'utf-8';
|
|
54
|
-
case 'base64':
|
|
55
|
-
case 'hex':
|
|
56
|
-
return /** @type {BufferEncoding} */ (charset);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The application/x-www-form-urlencoded format is in many ways an aberrant monstrosity,
|
|
62
|
-
* the result of many years of implementation accidents and compromises leading to a set of
|
|
63
|
-
* requirements necessary for interoperability, but in no way representing good design practices.
|
|
64
|
-
* In particular, readers are cautioned to pay close attention to the twisted details
|
|
65
|
-
* involving repeated (and in some cases nested) conversions between character encodings and byte sequences.
|
|
66
|
-
* Unfortunately the format is in widespread use due to the prevalence of HTML forms. [HTML]
|
|
67
|
-
* @param {Buffer} buffer
|
|
68
|
-
* @param {string} charset
|
|
69
|
-
* @return {[string, string][]} Tuple
|
|
70
|
-
*/
|
|
71
|
-
static readUrlEncoded(buffer, charset) {
|
|
72
|
-
// https://url.spec.whatwg.org/#urlencoded-parsing
|
|
73
|
-
const bufferEncoding = ContentReaderMiddleware.charsetAsBufferEncoding(charset);
|
|
74
|
-
|
|
75
|
-
const sequences = [];
|
|
76
|
-
let startIndex = 0;
|
|
77
|
-
for (let i = 0; i < buffer.length; i += 1) {
|
|
78
|
-
if (buffer[i] === 0x26) {
|
|
79
|
-
sequences.push(buffer.subarray(startIndex, i));
|
|
80
|
-
startIndex = i + 1;
|
|
81
|
-
}
|
|
82
|
-
if (i === buffer.length - 1) {
|
|
83
|
-
sequences.push(buffer.subarray(startIndex, i + 1));
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
/** @type {[string, string][]} */
|
|
88
|
-
const output = [];
|
|
89
|
-
sequences.forEach((bytes) => {
|
|
90
|
-
if (!bytes.length) return;
|
|
91
|
-
|
|
92
|
-
// Find 0x3D and replace 0x2B in one loop for better performance
|
|
93
|
-
let indexOf0x3D = -1;
|
|
94
|
-
for (let i = 0; i < bytes.length; i += 1) {
|
|
95
|
-
switch (bytes[i]) {
|
|
96
|
-
case 0x3D:
|
|
97
|
-
if (indexOf0x3D === -1) {
|
|
98
|
-
indexOf0x3D = i;
|
|
99
|
-
}
|
|
100
|
-
break;
|
|
101
|
-
case 0x2B:
|
|
102
|
-
// Replace bytes on original stream for memory conservation
|
|
103
|
-
// eslint-disable-next-line no-param-reassign
|
|
104
|
-
bytes[i] = 0x20;
|
|
105
|
-
break;
|
|
106
|
-
default:
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
let name;
|
|
110
|
-
let value;
|
|
111
|
-
if (indexOf0x3D === -1) {
|
|
112
|
-
name = bytes;
|
|
113
|
-
value = bytes.subarray(bytes.length, 0);
|
|
114
|
-
} else {
|
|
115
|
-
name = bytes.subarray(0, indexOf0x3D);
|
|
116
|
-
value = bytes.subarray(indexOf0x3D + 1);
|
|
117
|
-
}
|
|
118
|
-
const nameString = decodeURIComponent(name.toString(bufferEncoding));
|
|
119
|
-
const valueString = decodeURIComponent(value.toString(bufferEncoding));
|
|
120
|
-
output.push([nameString, valueString]);
|
|
121
|
-
});
|
|
122
|
-
return output;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* @param {MiddlewareFunctionParams} params
|
|
127
|
-
* @return {MiddlewareFunctionResult}
|
|
128
|
-
*/
|
|
129
|
-
execute({ req }) {
|
|
130
|
-
switch (req.method) {
|
|
131
|
-
case 'HEAD':
|
|
132
|
-
case 'GET':
|
|
133
|
-
return 'continue';
|
|
134
|
-
default:
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const contentType = (req.headers['content-type']);
|
|
138
|
-
/** @type {string} */
|
|
139
|
-
let mediaType;
|
|
140
|
-
/** @type {string} */
|
|
141
|
-
let charset;
|
|
142
|
-
if (contentType) {
|
|
143
|
-
contentType.split(';').forEach((directive) => {
|
|
144
|
-
const parameters = directive.split('=');
|
|
145
|
-
if (parameters.length === 1) {
|
|
146
|
-
mediaType = directive.trim().toLowerCase();
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
if (parameters[0].trim().toLowerCase() !== 'charset') {
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
charset = parameters[1]?.trim().toLowerCase();
|
|
153
|
-
const firstQuote = charset.indexOf('"');
|
|
154
|
-
const lastQuote = charset.lastIndexOf('"');
|
|
155
|
-
if (firstQuote !== -1 && lastQuote !== -1) {
|
|
156
|
-
charset = charset.substring(firstQuote + 1, lastQuote);
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (!mediaType) {
|
|
162
|
-
mediaType = this.defaultMediaType;
|
|
163
|
-
}
|
|
164
|
-
const isFormUrlEncoded = mediaType === 'application/x-www-form-urlencoded';
|
|
165
|
-
const isJSON = /application\/(.+\+)?json/i.test(mediaType);
|
|
166
|
-
if (!charset) {
|
|
167
|
-
if (!mediaType) {
|
|
168
|
-
return 'continue';
|
|
169
|
-
}
|
|
170
|
-
if (isFormUrlEncoded && (this.formURLEncodedFormat || 'none') === 'none') {
|
|
171
|
-
return 'continue';
|
|
172
|
-
}
|
|
173
|
-
if (!isFormUrlEncoded && !isJSON && !mediaType.startsWith('text/')) {
|
|
174
|
-
return 'continue';
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const readAll = isJSON || isFormUrlEncoded;
|
|
179
|
-
|
|
180
|
-
let fullString = '';
|
|
181
|
-
/** @type {Buffer[]} */
|
|
182
|
-
const pendingChunks = [];
|
|
183
|
-
const source = req.stream;
|
|
184
|
-
const {
|
|
185
|
-
buildString, formURLEncodedFormat, cache, parseJSON,
|
|
186
|
-
} = this;
|
|
187
|
-
const newReadable = new Transform({
|
|
188
|
-
objectMode: true,
|
|
189
|
-
read(...args) {
|
|
190
|
-
if (source.isPaused()) source.resume();
|
|
191
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
192
|
-
Transform.prototype._read.call(this, ...args);
|
|
193
|
-
},
|
|
194
|
-
transform(chunk, encoding, callback) {
|
|
195
|
-
if (typeof chunk === 'string') {
|
|
196
|
-
if (readAll || buildString) {
|
|
197
|
-
fullString += chunk;
|
|
198
|
-
} else {
|
|
199
|
-
this.push(chunk);
|
|
200
|
-
}
|
|
201
|
-
} else if (isFormUrlEncoded) {
|
|
202
|
-
pendingChunks.push(chunk);
|
|
203
|
-
} else {
|
|
204
|
-
this.push(chunk);
|
|
205
|
-
}
|
|
206
|
-
callback();
|
|
207
|
-
},
|
|
208
|
-
flush(callback) {
|
|
209
|
-
let result = null;
|
|
210
|
-
if (isFormUrlEncoded) {
|
|
211
|
-
const combinedBuffer = Buffer.concat(pendingChunks);
|
|
212
|
-
if (formURLEncodedFormat === 'object') {
|
|
213
|
-
result = Object.fromEntries(ContentReaderMiddleware.readUrlEncoded(combinedBuffer, charset));
|
|
214
|
-
} else if (formURLEncodedFormat === 'string') {
|
|
215
|
-
result = combinedBuffer.toString(ContentReaderMiddleware.charsetAsBufferEncoding(charset));
|
|
216
|
-
} else {
|
|
217
|
-
result = ContentReaderMiddleware.readUrlEncoded(combinedBuffer, charset);
|
|
218
|
-
}
|
|
219
|
-
} else if (isJSON && parseJSON) {
|
|
220
|
-
try {
|
|
221
|
-
result = JSON.parse(fullString);
|
|
222
|
-
} catch {
|
|
223
|
-
result = fullString;
|
|
224
|
-
}
|
|
225
|
-
} else if (fullString) {
|
|
226
|
-
result = fullString;
|
|
227
|
-
}
|
|
228
|
-
if (cache && result) {
|
|
229
|
-
const cacheName = cache === true ? 'content' : cache;
|
|
230
|
-
req.locals[cacheName] = result;
|
|
231
|
-
}
|
|
232
|
-
callback(null, result);
|
|
233
|
-
},
|
|
234
|
-
});
|
|
235
|
-
req.replaceStream(newReadable);
|
|
236
|
-
if (!isFormUrlEncoded) {
|
|
237
|
-
// Data read from source will be decoded as a string
|
|
238
|
-
const encoding = ContentReaderMiddleware.charsetAsBufferEncoding(charset);
|
|
239
|
-
const stringDecoder = new PassThrough({ encoding });
|
|
240
|
-
newReadable.setDefaultEncoding(encoding);
|
|
241
|
-
source.pipe(stringDecoder).pipe(newReadable);
|
|
242
|
-
} else {
|
|
243
|
-
source.pipe(newReadable);
|
|
244
|
-
}
|
|
245
|
-
source.pause();
|
|
246
|
-
|
|
247
|
-
return 'continue';
|
|
248
|
-
}
|
|
249
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { Transform } from 'node:stream';
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../types').IMiddleware} IMiddleware */
|
|
4
|
-
/** @typedef {import('../types').MiddlewareFunction} MiddlewareFunction */
|
|
5
|
-
/** @typedef {import('../types').MiddlewareFunctionParams} MiddlewareFunctionParams */
|
|
6
|
-
/** @typedef {import('../types').MiddlewareFunctionResult} MiddlewareFunctionResult */
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @typedef {Object} ContentWriterMiddlewareOptions
|
|
10
|
-
* @prop {string} [defaultCharset='utf-8']
|
|
11
|
-
* @prop {boolean} [setCharset=false]
|
|
12
|
-
* Automatically applies charset in `Content-Type`
|
|
13
|
-
* @prop {boolean} [setJSON=false]
|
|
14
|
-
* Automatically applies `application/json` mediatype in `Content-Type`
|
|
15
|
-
* @prop {boolean|string} [cache=false]
|
|
16
|
-
* Caches content in res.local.content or res.local[cacheName]
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/** @implements {IMiddleware} */
|
|
20
|
-
export default class ContentWriterMiddleware {
|
|
21
|
-
/** @param {ContentWriterMiddlewareOptions} [options] */
|
|
22
|
-
constructor(options = {}) {
|
|
23
|
-
this.defaultCharset = options.defaultCharset || 'utf-8';
|
|
24
|
-
this.setCharset = options.setCharset === true;
|
|
25
|
-
this.setJSON = options.setJSON === true;
|
|
26
|
-
this.cache = options.cache;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @param {string} charset
|
|
31
|
-
* @return {BufferEncoding}
|
|
32
|
-
*/
|
|
33
|
-
static charsetAsBufferEncoding(charset) {
|
|
34
|
-
switch (charset) {
|
|
35
|
-
case 'iso-8859-1':
|
|
36
|
-
case 'ascii':
|
|
37
|
-
case 'binary':
|
|
38
|
-
case 'latin1':
|
|
39
|
-
return 'latin1';
|
|
40
|
-
case 'utf-16le':
|
|
41
|
-
case 'ucs-2':
|
|
42
|
-
case 'ucs2':
|
|
43
|
-
case 'utf16le':
|
|
44
|
-
return 'utf16le';
|
|
45
|
-
case 'base64':
|
|
46
|
-
case 'hex':
|
|
47
|
-
return /** @type {BufferEncoding} */ (charset);
|
|
48
|
-
case 'utf-8':
|
|
49
|
-
case 'utf8':
|
|
50
|
-
default:
|
|
51
|
-
return 'utf-8';
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @param {MiddlewareFunctionParams} params
|
|
57
|
-
* @return {MiddlewareFunctionResult}
|
|
58
|
-
*/
|
|
59
|
-
execute({ req, res }) {
|
|
60
|
-
if (req.method === 'HEAD') {
|
|
61
|
-
return 'continue';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** @type {string} */
|
|
65
|
-
let charset = null;
|
|
66
|
-
/** @type {BufferEncoding} */
|
|
67
|
-
let encoding = null;
|
|
68
|
-
let hasSetJSON = false;
|
|
69
|
-
|
|
70
|
-
/** @return {string} */
|
|
71
|
-
const parseCharset = () => {
|
|
72
|
-
if (charset) return charset;
|
|
73
|
-
/** @type {string} */
|
|
74
|
-
const contentType = (res.headers['content-type']);
|
|
75
|
-
if (contentType) {
|
|
76
|
-
contentType.split(';').some((directive) => {
|
|
77
|
-
const parameters = directive.split('=');
|
|
78
|
-
if (parameters[0].trim().toLowerCase() !== 'charset') {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
charset = parameters[1]?.trim().toLowerCase();
|
|
82
|
-
const firstQuote = charset.indexOf('"');
|
|
83
|
-
const lastQuote = charset.lastIndexOf('"');
|
|
84
|
-
if (firstQuote !== -1 && lastQuote !== -1) {
|
|
85
|
-
charset = charset.substring(firstQuote + 1, lastQuote);
|
|
86
|
-
}
|
|
87
|
-
return true;
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
if (!charset) {
|
|
91
|
-
charset = this.defaultCharset || 'utf-8';
|
|
92
|
-
if (this.setCharset && !res.headersSent) {
|
|
93
|
-
res.headers['content-type'] = `${contentType || ''};charset=${charset}`;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return charset;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/** @return {void} */
|
|
100
|
-
const setJSONMediaType = () => {
|
|
101
|
-
if (hasSetJSON) return;
|
|
102
|
-
/** @type {string} */
|
|
103
|
-
const contentType = (res.headers['content-type']);
|
|
104
|
-
res.headers['content-type'] = (contentType || '')
|
|
105
|
-
.split(';')
|
|
106
|
-
.map((directive) => {
|
|
107
|
-
const isKeyPair = directive.includes('=');
|
|
108
|
-
if (isKeyPair) return directive;
|
|
109
|
-
return 'application/json';
|
|
110
|
-
})
|
|
111
|
-
.join(';');
|
|
112
|
-
|
|
113
|
-
hasSetJSON = true;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const newWritable = new Transform({
|
|
117
|
-
writableObjectMode: true,
|
|
118
|
-
transform: (chunk, e, callback) => {
|
|
119
|
-
if (Buffer.isBuffer(chunk)) {
|
|
120
|
-
callback(null, chunk);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
const cacheName = this.cache && (this.cache === true ? 'content' : this.cache);
|
|
124
|
-
if (typeof chunk === 'string') {
|
|
125
|
-
if (!encoding) {
|
|
126
|
-
encoding = ContentWriterMiddleware.charsetAsBufferEncoding(parseCharset());
|
|
127
|
-
}
|
|
128
|
-
if (cacheName) {
|
|
129
|
-
if (typeof res.locals[cacheName] === 'string') {
|
|
130
|
-
res.locals[cacheName] += chunk;
|
|
131
|
-
} else {
|
|
132
|
-
res.locals[cacheName] = chunk;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
const callbackData = Buffer.from(chunk, encoding);
|
|
136
|
-
callback(null, callbackData);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (cacheName) {
|
|
140
|
-
res.locals[cacheName] = chunk;
|
|
141
|
-
}
|
|
142
|
-
if (typeof chunk === 'object') {
|
|
143
|
-
if (!encoding) {
|
|
144
|
-
encoding = ContentWriterMiddleware.charsetAsBufferEncoding(parseCharset());
|
|
145
|
-
}
|
|
146
|
-
if (this.setJSON && !hasSetJSON && !res.headersSent) {
|
|
147
|
-
setJSONMediaType();
|
|
148
|
-
}
|
|
149
|
-
callback(null, Buffer.from(JSON.stringify(chunk), encoding));
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
callback(null, chunk);
|
|
154
|
-
},
|
|
155
|
-
});
|
|
156
|
-
const destination = res.replaceStream(newWritable);
|
|
157
|
-
newWritable.pipe(destination);
|
|
158
|
-
|
|
159
|
-
return 'continue';
|
|
160
|
-
}
|
|
161
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { PassThrough } from 'stream';
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../types').IMiddleware} IMiddleware */
|
|
4
|
-
/** @typedef {import('../types').MiddlewareFunction} MiddlewareFunction */
|
|
5
|
-
/** @typedef {import('../types').MiddlewareFunctionParams} MiddlewareFunctionParams */
|
|
6
|
-
/** @typedef {import('../types').MiddlewareFunctionResult} MiddlewareFunctionResult */
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @typedef {Object} SendHeadersMiddlewareOptions
|
|
10
|
-
* @prop {boolean} [setStatus=false]
|
|
11
|
-
* Automatically set `200` or `204` status if not set
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** @implements {IMiddleware} */
|
|
15
|
-
export default class SendHeadersMiddleware {
|
|
16
|
-
/** @param {SendHeadersMiddlewareOptions} options */
|
|
17
|
-
constructor(options = {}) {
|
|
18
|
-
this.setStatus = options.setStatus === true;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {MiddlewareFunctionParams} params
|
|
23
|
-
* @return {MiddlewareFunctionResult}
|
|
24
|
-
*/
|
|
25
|
-
execute({ res }) {
|
|
26
|
-
const newWritable = new PassThrough();
|
|
27
|
-
const destination = res.replaceStream(newWritable);
|
|
28
|
-
newWritable.once('data', () => {
|
|
29
|
-
if (!res.headersSent) {
|
|
30
|
-
if (this.setStatus && res.status == null) {
|
|
31
|
-
res.status = 200;
|
|
32
|
-
}
|
|
33
|
-
res.sendHeaders(false);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
newWritable.on('end', () => {
|
|
37
|
-
if (!res.headersSent) {
|
|
38
|
-
if (this.setStatus && res.status == null) {
|
|
39
|
-
res.status = 204;
|
|
40
|
-
}
|
|
41
|
-
res.sendHeaders(false);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
newWritable.pipe(destination);
|
|
45
|
-
return 'continue';
|
|
46
|
-
}
|
|
47
|
-
}
|