@duplojs/http 0.6.1 → 0.7.0
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/dist/client/getBody.cjs +1 -1
- package/dist/client/getBody.mjs +1 -1
- package/dist/client/hooks.d.ts +1 -25
- package/dist/client/httpClient.d.ts +29 -25
- package/dist/client/promiseRequest.cjs +3 -0
- package/dist/client/promiseRequest.d.ts +31 -39
- package/dist/client/promiseRequest.mjs +4 -1
- package/dist/client/types/clientResponse.d.ts +6 -6
- package/dist/client/types/hooks.d.ts +25 -0
- package/dist/client/types/index.cjs +2 -0
- package/dist/client/types/index.d.ts +2 -0
- package/dist/client/types/index.mjs +2 -0
- package/dist/client/types/promiseRequestParams.cjs +2 -0
- package/dist/client/types/promiseRequestParams.d.ts +9 -0
- package/dist/client/types/promiseRequestParams.mjs +1 -0
- package/dist/client/types/serverRoute.d.ts +19 -1
- package/dist/client/unexpectedResponseError.d.ts +1 -2
- package/dist/core/builders/preflight/route.cjs +1 -0
- package/dist/core/builders/preflight/route.d.ts +4 -2
- package/dist/core/builders/preflight/route.mjs +1 -0
- package/dist/core/builders/route/builder.cjs +1 -0
- package/dist/core/builders/route/builder.d.ts +4 -2
- package/dist/core/builders/route/builder.mjs +1 -0
- package/dist/core/clean/constraint.cjs +24 -0
- package/dist/core/clean/constraint.d.ts +8 -0
- package/dist/core/clean/constraint.mjs +22 -0
- package/dist/core/clean/constraintsSet.cjs +27 -0
- package/dist/core/clean/constraintsSet.d.ts +8 -0
- package/dist/core/clean/constraintsSet.mjs +25 -0
- package/dist/core/clean/entity.cjs +33 -0
- package/dist/core/clean/entity.d.ts +20 -0
- package/dist/core/clean/entity.mjs +31 -0
- package/dist/core/clean/index.cjs +8 -0
- package/dist/core/clean/index.d.ts +5 -0
- package/dist/core/clean/index.mjs +5 -0
- package/dist/core/clean/newType.cjs +15 -0
- package/dist/core/clean/newType.d.ts +8 -0
- package/dist/core/clean/newType.mjs +13 -0
- package/dist/core/clean/primitive.cjs +12 -0
- package/dist/core/clean/primitive.d.ts +8 -0
- package/dist/core/clean/primitive.mjs +10 -0
- package/dist/core/defaultHooks/index.cjs +50 -0
- package/dist/core/defaultHooks/index.d.ts +5 -0
- package/dist/core/defaultHooks/index.mjs +48 -0
- package/dist/{interfaces/node/error → core/errors}/bodyParseWrongChunkReceived.cjs +6 -4
- package/dist/core/errors/bodyParseWrongChunkReceived.d.ts +9 -0
- package/dist/core/errors/bodyParseWrongChunkReceived.mjs +14 -0
- package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.cjs +2 -2
- package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.d.ts +2 -2
- package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.mjs +2 -2
- package/dist/{interfaces/node/error → core/errors}/index.cjs +6 -4
- package/dist/{interfaces/node/error → core/errors}/index.d.ts +3 -2
- package/dist/{interfaces/node/error → core/errors}/index.mjs +3 -2
- package/dist/core/errors/parseJsonError.cjs +16 -0
- package/dist/core/errors/parseJsonError.d.ts +9 -0
- package/dist/core/errors/parseJsonError.mjs +14 -0
- package/dist/core/errors/wrongContentTypeError.cjs +16 -0
- package/dist/core/errors/wrongContentTypeError.d.ts +9 -0
- package/dist/core/errors/wrongContentTypeError.mjs +14 -0
- package/dist/core/functionsBuilders/route/create.d.ts +2 -2
- package/dist/core/functionsBuilders/route/default.cjs +0 -11
- package/dist/core/functionsBuilders/route/default.mjs +0 -11
- package/dist/core/functionsBuilders/route/hook.d.ts +2 -2
- package/dist/core/functionsBuilders/steps/create.d.ts +2 -2
- package/dist/core/functionsBuilders/steps/defaults/cutStep.cjs +15 -18
- package/dist/core/functionsBuilders/steps/defaults/cutStep.mjs +15 -18
- package/dist/core/functionsBuilders/steps/defaults/extractStep.cjs +43 -19
- package/dist/core/functionsBuilders/steps/defaults/extractStep.d.ts +2 -1
- package/dist/core/functionsBuilders/steps/defaults/extractStep.mjs +44 -20
- package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +14 -8
- package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +14 -8
- package/dist/core/hub/defaultBodyController.cjs +8 -0
- package/dist/core/hub/defaultBodyController.d.ts +1 -0
- package/dist/core/hub/defaultBodyController.mjs +6 -0
- package/dist/core/hub/hooks.cjs +3 -1
- package/dist/core/hub/hooks.d.ts +2 -3
- package/dist/core/hub/hooks.mjs +3 -1
- package/dist/core/hub/index.cjs +101 -127
- package/dist/core/hub/index.d.ts +33 -34
- package/dist/core/hub/index.mjs +100 -128
- package/dist/core/implementHttpServer.cjs +5 -5
- package/dist/core/implementHttpServer.d.ts +2 -1
- package/dist/core/implementHttpServer.mjs +5 -5
- package/dist/core/index.cjs +35 -10
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.mjs +14 -2
- package/dist/core/request/bodyController/base.cjs +36 -0
- package/dist/core/request/bodyController/base.d.ts +28 -0
- package/dist/core/request/bodyController/base.mjs +34 -0
- package/dist/core/request/bodyController/formData.cjs +28 -0
- package/dist/core/request/bodyController/formData.d.ts +22 -0
- package/dist/core/request/bodyController/formData.mjs +25 -0
- package/dist/core/request/bodyController/index.cjs +13 -0
- package/dist/core/request/bodyController/index.d.ts +3 -0
- package/dist/core/request/bodyController/index.mjs +3 -0
- package/dist/core/request/bodyController/text.cjs +14 -0
- package/dist/core/request/bodyController/text.d.ts +10 -0
- package/dist/core/request/bodyController/text.mjs +11 -0
- package/dist/core/{request.cjs → request/index.cjs} +21 -3
- package/dist/core/{request.d.ts → request/index.d.ts} +10 -3
- package/dist/core/request/index.mjs +50 -0
- package/dist/core/response/contract.d.ts +1 -1
- package/dist/core/route/hooks.d.ts +0 -2
- package/dist/core/route/index.d.ts +2 -1
- package/dist/core/router/index.cjs +27 -8
- package/dist/core/router/index.d.ts +2 -1
- package/dist/core/router/index.mjs +28 -10
- package/dist/core/router/notFoundBodyReaderImplementationError.cjs +16 -0
- package/dist/core/router/notFoundBodyReaderImplementationError.d.ts +11 -0
- package/dist/core/router/notFoundBodyReaderImplementationError.mjs +14 -0
- package/dist/core/router/types/buildedRouter.d.ts +3 -3
- package/dist/core/steps/extract.d.ts +3 -1
- package/dist/core/steps/types/steps.d.ts +1 -3
- package/dist/core/types/hosts.cjs +2 -0
- package/dist/core/types/hosts.d.ts +4 -0
- package/dist/core/types/hosts.mjs +1 -0
- package/dist/core/types/httpServerParams.cjs +2 -0
- package/dist/core/types/httpServerParams.d.ts +11 -0
- package/dist/core/types/httpServerParams.mjs +1 -0
- package/dist/core/types/index.cjs +2 -0
- package/dist/core/types/index.d.ts +2 -0
- package/dist/core/types/index.mjs +2 -0
- package/dist/interfaces/bun/types/request.cjs +1 -1
- package/dist/interfaces/bun/types/request.mjs +1 -1
- package/dist/interfaces/node/bodyReaders/formData/error.cjs +14 -0
- package/dist/interfaces/node/bodyReaders/formData/error.d.ts +8 -0
- package/dist/interfaces/node/bodyReaders/formData/error.mjs +12 -0
- package/dist/interfaces/node/bodyReaders/formData/index.cjs +94 -0
- package/dist/interfaces/node/bodyReaders/formData/index.d.ts +4 -0
- package/dist/interfaces/node/bodyReaders/formData/index.mjs +90 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.cjs +175 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.d.ts +21 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.mjs +173 -0
- package/dist/interfaces/node/bodyReaders/index.cjs +9 -0
- package/dist/interfaces/node/bodyReaders/index.d.ts +2 -0
- package/dist/interfaces/node/bodyReaders/index.mjs +2 -0
- package/dist/interfaces/node/bodyReaders/text/index.cjs +41 -0
- package/dist/interfaces/node/bodyReaders/text/index.d.ts +3 -0
- package/dist/interfaces/node/bodyReaders/text/index.mjs +38 -0
- package/dist/interfaces/node/bodyReaders/text/readRequestText.cjs +37 -0
- package/dist/interfaces/node/bodyReaders/text/readRequestText.d.ts +6 -0
- package/dist/interfaces/node/bodyReaders/text/readRequestText.mjs +35 -0
- package/dist/interfaces/node/createHttpServer.cjs +13 -5
- package/dist/interfaces/node/createHttpServer.d.ts +13 -12
- package/dist/interfaces/node/createHttpServer.mjs +13 -5
- package/dist/interfaces/node/hooks/index.cjs +47 -0
- package/dist/interfaces/node/hooks/index.d.ts +5 -0
- package/dist/interfaces/node/hooks/index.mjs +45 -0
- package/dist/interfaces/node/index.cjs +13 -9
- package/dist/interfaces/node/index.d.ts +1 -1
- package/dist/interfaces/node/index.mjs +7 -5
- package/dist/interfaces/node/types/index.cjs +0 -1
- package/dist/interfaces/node/types/index.d.ts +0 -1
- package/dist/interfaces/node/types/index.mjs +0 -1
- package/dist/interfaces/node/types/request.cjs +1 -1
- package/dist/interfaces/node/types/request.mjs +1 -1
- package/dist/plugins/codeGenerator/index.cjs +2 -0
- package/dist/plugins/codeGenerator/index.mjs +1 -1
- package/dist/plugins/codeGenerator/plugin.cjs +8 -4
- package/dist/plugins/codeGenerator/plugin.mjs +9 -5
- package/dist/plugins/codeGenerator/routeToDataParser.cjs +24 -1
- package/dist/plugins/codeGenerator/routeToDataParser.d.ts +34 -0
- package/dist/plugins/codeGenerator/routeToDataParser.mjs +24 -3
- package/dist/plugins/codeGenerator/typescriptTransfomer.cjs +9 -0
- package/dist/plugins/codeGenerator/typescriptTransfomer.d.ts +1 -0
- package/dist/plugins/codeGenerator/typescriptTransfomer.mjs +7 -0
- package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +3 -0
- package/dist/plugins/openApiGenerator/plugin.cjs +3 -3
- package/dist/plugins/openApiGenerator/plugin.mjs +4 -4
- package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +16 -1
- package/dist/plugins/openApiGenerator/routeToOpenApi.d.ts +10 -1
- package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +16 -1
- package/dist/plugins/openApiGenerator/types/entrypoint.d.ts +6 -1
- package/dist/plugins/openApiGenerator/types/openApiMethod.d.ts +1 -1
- package/package.json +5 -6
- package/dist/core/request.mjs +0 -32
- package/dist/interfaces/node/error/bodyParseUnknownError.cjs +0 -16
- package/dist/interfaces/node/error/bodyParseUnknownError.d.ts +0 -9
- package/dist/interfaces/node/error/bodyParseUnknownError.mjs +0 -14
- package/dist/interfaces/node/error/bodyParseWrongChunkReceived.d.ts +0 -8
- package/dist/interfaces/node/error/bodyParseWrongChunkReceived.mjs +0 -12
- package/dist/interfaces/node/hooks.cjs +0 -126
- package/dist/interfaces/node/hooks.d.ts +0 -8
- package/dist/interfaces/node/hooks.mjs +0 -124
- package/dist/interfaces/node/types/host.d.ts +0 -4
- /package/dist/{interfaces/node/types/host.cjs → client/types/hooks.cjs} +0 -0
- /package/dist/{interfaces/node/types/host.mjs → client/types/hooks.mjs} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duplojs/http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"author": "mathcovax",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -65,8 +65,9 @@
|
|
|
65
65
|
"README.md"
|
|
66
66
|
],
|
|
67
67
|
"peerDependencies": {
|
|
68
|
-
"@duplojs/data-parser-tools": ">=0.2.
|
|
69
|
-
"@duplojs/utils": ">=
|
|
68
|
+
"@duplojs/data-parser-tools": ">=0.2.7 <1.0.0",
|
|
69
|
+
"@duplojs/server-utils": ">=0.2.0 <1.0.0",
|
|
70
|
+
"@duplojs/utils": ">=1.5.4 <2.0.0"
|
|
70
71
|
},
|
|
71
72
|
"devDependencies": {
|
|
72
73
|
"@commitlint/cli": "19.8.1",
|
|
@@ -76,6 +77,7 @@
|
|
|
76
77
|
"@types/bun": "^1.3.3",
|
|
77
78
|
"@types/deno": "^2.5.0",
|
|
78
79
|
"@types/node": "24.3.0",
|
|
80
|
+
"@types/web": "^0.0.317",
|
|
79
81
|
"@vitest/coverage-istanbul": "3.2.4",
|
|
80
82
|
"eslint": "9.34.0",
|
|
81
83
|
"form-data": "^4.0.5",
|
|
@@ -98,8 +100,5 @@
|
|
|
98
100
|
"keywords": [],
|
|
99
101
|
"engines": {
|
|
100
102
|
"node": ">=22.15.1"
|
|
101
|
-
},
|
|
102
|
-
"dependencies": {
|
|
103
|
-
"@types/web": "^0.0.317"
|
|
104
103
|
}
|
|
105
104
|
}
|
package/dist/core/request.mjs
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { kindHeritage } from '@duplojs/utils';
|
|
2
|
-
import { createCoreLibKind } from './kind.mjs';
|
|
3
|
-
|
|
4
|
-
class Request extends kindHeritage("request", createCoreLibKind("request")) {
|
|
5
|
-
method;
|
|
6
|
-
headers;
|
|
7
|
-
url;
|
|
8
|
-
host;
|
|
9
|
-
origin;
|
|
10
|
-
path;
|
|
11
|
-
params;
|
|
12
|
-
query;
|
|
13
|
-
matchedPath;
|
|
14
|
-
body = undefined;
|
|
15
|
-
constructor({ method, headers, url, host, origin, path, params, query, matchedPath, ...rest }) {
|
|
16
|
-
super();
|
|
17
|
-
this.method = method;
|
|
18
|
-
this.headers = headers;
|
|
19
|
-
this.url = url;
|
|
20
|
-
this.host = host;
|
|
21
|
-
this.origin = origin;
|
|
22
|
-
this.path = path;
|
|
23
|
-
this.params = params;
|
|
24
|
-
this.query = query;
|
|
25
|
-
this.matchedPath = matchedPath;
|
|
26
|
-
for (const key in rest) {
|
|
27
|
-
this[key] = rest[key];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export { Request };
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var utils = require('@duplojs/utils');
|
|
4
|
-
var kind = require('../kind.cjs');
|
|
5
|
-
|
|
6
|
-
class BodyParseUnknownError extends utils.kindHeritage("body-parse-unknown-error", kind.createInterfacesNodeLibKind("body-parse-unknown-error"), Error) {
|
|
7
|
-
contentType;
|
|
8
|
-
unknownError;
|
|
9
|
-
constructor(contentType, unknownError) {
|
|
10
|
-
super({}, [`Error when parsing body with '${contentType}' content-type.`]);
|
|
11
|
-
this.contentType = contentType;
|
|
12
|
-
this.unknownError = unknownError;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
exports.BodyParseUnknownError = BodyParseUnknownError;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
declare const BodyParseUnknownError_base: new (params: {
|
|
2
|
-
"@DuplojsHttpInterfacesNode/body-parse-unknown-error"?: unknown;
|
|
3
|
-
}, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpInterfacesNode/body-parse-unknown-error", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"body-parse-unknown-error", unknown>, unknown>;
|
|
4
|
-
export declare class BodyParseUnknownError extends BodyParseUnknownError_base {
|
|
5
|
-
contentType: string;
|
|
6
|
-
unknownError: unknown;
|
|
7
|
-
constructor(contentType: string, unknownError: unknown);
|
|
8
|
-
}
|
|
9
|
-
export {};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { kindHeritage } from '@duplojs/utils';
|
|
2
|
-
import { createInterfacesNodeLibKind } from '../kind.mjs';
|
|
3
|
-
|
|
4
|
-
class BodyParseUnknownError extends kindHeritage("body-parse-unknown-error", createInterfacesNodeLibKind("body-parse-unknown-error"), Error) {
|
|
5
|
-
contentType;
|
|
6
|
-
unknownError;
|
|
7
|
-
constructor(contentType, unknownError) {
|
|
8
|
-
super({}, [`Error when parsing body with '${contentType}' content-type.`]);
|
|
9
|
-
this.contentType = contentType;
|
|
10
|
-
this.unknownError = unknownError;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export { BodyParseUnknownError };
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
declare const BodyParseWrongChunkReceived_base: new (params: {
|
|
2
|
-
"@DuplojsHttpInterfacesNode/body-parse-wrong-chunk-received"?: unknown;
|
|
3
|
-
}, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpInterfacesNode/body-parse-wrong-chunk-received", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"body-parse-wrong-chunk-received", unknown>, unknown>;
|
|
4
|
-
export declare class BodyParseWrongChunkReceived extends BodyParseWrongChunkReceived_base {
|
|
5
|
-
wrongChunk: unknown;
|
|
6
|
-
constructor(wrongChunk: unknown);
|
|
7
|
-
}
|
|
8
|
-
export {};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { kindHeritage } from '@duplojs/utils';
|
|
2
|
-
import { createInterfacesNodeLibKind } from '../kind.mjs';
|
|
3
|
-
|
|
4
|
-
class BodyParseWrongChunkReceived extends kindHeritage("body-parse-wrong-chunk-received", createInterfacesNodeLibKind("body-parse-wrong-chunk-received"), Error) {
|
|
5
|
-
wrongChunk;
|
|
6
|
-
constructor(wrongChunk) {
|
|
7
|
-
super({}, ["Received chunk is not buffer or string."]);
|
|
8
|
-
this.wrongChunk = wrongChunk;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export { BodyParseWrongChunkReceived };
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
require('../../core/route/index.cjs');
|
|
4
|
-
var utils = require('@duplojs/utils');
|
|
5
|
-
require('./error/index.cjs');
|
|
6
|
-
require('../../core/response/index.cjs');
|
|
7
|
-
var hooks = require('../../core/route/hooks.cjs');
|
|
8
|
-
var predicted = require('../../core/response/predicted.cjs');
|
|
9
|
-
var hook = require('../../core/response/hook.cjs');
|
|
10
|
-
var bodySizeExceedsLimitError = require('./error/bodySizeExceedsLimitError.cjs');
|
|
11
|
-
var bodyParseWrongChunkReceived = require('./error/bodyParseWrongChunkReceived.cjs');
|
|
12
|
-
var bodyParseUnknownError = require('./error/bodyParseUnknownError.cjs');
|
|
13
|
-
|
|
14
|
-
function makeNodeHook(hub, serverParams) {
|
|
15
|
-
const informationHeaderKey = serverParams.informationHeaderKey;
|
|
16
|
-
const predictedHeaderKey = serverParams.predictedHeaderKey;
|
|
17
|
-
const fromHookHeaderKey = serverParams.fromHookHeaderKey;
|
|
18
|
-
const isDev = hub.config.environment === "DEV";
|
|
19
|
-
const maxBodySize = utils.stringToBytes(serverParams.maxBodySize);
|
|
20
|
-
return hooks.createHookRouteLifeCycle({
|
|
21
|
-
async parseBody({ request, exit }) {
|
|
22
|
-
const contentType = request.headers["content-type"] instanceof Array
|
|
23
|
-
? request.headers["content-type"].join(", ")
|
|
24
|
-
: request.headers["content-type"] ?? "";
|
|
25
|
-
const isText = contentType.includes("text/plain");
|
|
26
|
-
const isJson = contentType.includes("application/json");
|
|
27
|
-
if (!isText && !isJson) {
|
|
28
|
-
return exit();
|
|
29
|
-
}
|
|
30
|
-
const { request: rawRequest } = request.raw;
|
|
31
|
-
request.body = await new Promise((resolve, reject) => {
|
|
32
|
-
function errorCallback(error) {
|
|
33
|
-
if (error instanceof bodySizeExceedsLimitError.BodySizeExceedsLimitError
|
|
34
|
-
|| error instanceof bodyParseWrongChunkReceived.BodyParseWrongChunkReceived) {
|
|
35
|
-
reject(error);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
reject(new bodyParseUnknownError.BodyParseUnknownError(contentType, error));
|
|
39
|
-
}
|
|
40
|
-
let stringBody = "";
|
|
41
|
-
let byteLengthBody = 0;
|
|
42
|
-
rawRequest.on("error", errorCallback);
|
|
43
|
-
rawRequest.on("data", (chunk) => {
|
|
44
|
-
if (!(chunk instanceof Buffer) && typeof chunk !== "string") {
|
|
45
|
-
rawRequest.emit("error", new bodyParseWrongChunkReceived.BodyParseWrongChunkReceived(chunk));
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
byteLengthBody += chunk instanceof Buffer
|
|
49
|
-
? chunk.byteLength
|
|
50
|
-
: Buffer.byteLength(chunk);
|
|
51
|
-
if (byteLengthBody > maxBodySize) {
|
|
52
|
-
rawRequest.emit("error", new bodySizeExceedsLimitError.BodySizeExceedsLimitError(serverParams.maxBodySize));
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
stringBody += chunk.toString();
|
|
56
|
-
});
|
|
57
|
-
rawRequest.on("end", () => {
|
|
58
|
-
try {
|
|
59
|
-
resolve(isText
|
|
60
|
-
? stringBody
|
|
61
|
-
: JSON.parse(stringBody));
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
errorCallback(error);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
return exit();
|
|
69
|
-
},
|
|
70
|
-
error({ error, response, exit }) {
|
|
71
|
-
const displayedError = isDev ? error : undefined;
|
|
72
|
-
if (error instanceof bodySizeExceedsLimitError.BodySizeExceedsLimitError) {
|
|
73
|
-
return response("400", "body-size-exceeds-limit-error", displayedError);
|
|
74
|
-
}
|
|
75
|
-
else if (error instanceof bodyParseWrongChunkReceived.BodyParseWrongChunkReceived) {
|
|
76
|
-
return response("400", "body-parse-wrong-chunk-received", displayedError);
|
|
77
|
-
}
|
|
78
|
-
else if (error instanceof bodyParseUnknownError.BodyParseUnknownError) {
|
|
79
|
-
return response("400", "body-parse-unknown-error", displayedError);
|
|
80
|
-
}
|
|
81
|
-
return exit();
|
|
82
|
-
},
|
|
83
|
-
beforeSendResponse({ request, currentResponse, exit }) {
|
|
84
|
-
if (!currentResponse.headers?.["content-type"]) {
|
|
85
|
-
const body = currentResponse.body;
|
|
86
|
-
if (typeof body === "string"
|
|
87
|
-
|| body instanceof Error) {
|
|
88
|
-
currentResponse.setHeader("content-type", "text/plain; charset=utf-8");
|
|
89
|
-
}
|
|
90
|
-
else if (typeof body === "object"
|
|
91
|
-
|| typeof body === "number"
|
|
92
|
-
|| typeof body === "boolean") {
|
|
93
|
-
currentResponse.setHeader("content-type", "application/json; charset=utf-8");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
currentResponse.setHeader(informationHeaderKey, currentResponse.information);
|
|
97
|
-
if (currentResponse instanceof predicted.PredictedResponse) {
|
|
98
|
-
currentResponse.setHeader(predictedHeaderKey, "1");
|
|
99
|
-
}
|
|
100
|
-
else if (currentResponse instanceof hook.HookResponse) {
|
|
101
|
-
currentResponse.setHeader(fromHookHeaderKey, currentResponse.fromHook);
|
|
102
|
-
}
|
|
103
|
-
request.raw.response.writeHead(Number(currentResponse.code), currentResponse.headers);
|
|
104
|
-
return exit();
|
|
105
|
-
},
|
|
106
|
-
sendResponse({ request, currentResponse, exit }) {
|
|
107
|
-
const { response: rawResponse } = request.raw;
|
|
108
|
-
const body = currentResponse.body;
|
|
109
|
-
if (body instanceof Error) {
|
|
110
|
-
rawResponse.write(body.toString());
|
|
111
|
-
}
|
|
112
|
-
else if (typeof body === "object"
|
|
113
|
-
|| typeof body === "number"
|
|
114
|
-
|| typeof body === "boolean") {
|
|
115
|
-
rawResponse.write(JSON.stringify(body));
|
|
116
|
-
}
|
|
117
|
-
else if (typeof body === "string") {
|
|
118
|
-
rawResponse.write(body);
|
|
119
|
-
}
|
|
120
|
-
rawResponse.end();
|
|
121
|
-
return exit();
|
|
122
|
-
},
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
exports.makeNodeHook = makeNodeHook;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type HttpServerParams, type Hub } from "../../core/hub";
|
|
2
|
-
import { HookResponse } from "../../core/response";
|
|
3
|
-
export declare function makeNodeHook(hub: Hub, serverParams: HttpServerParams): {
|
|
4
|
-
parseBody({ request, exit }: import("../../core/route").RouteHookParams<import("../../core/request").Request>): Promise<import("../../core/route").RouteHookExit>;
|
|
5
|
-
error({ error, response, exit }: import("../../core/route").RouteHookErrorParams<import("../../core/request").Request>): import("../../core/route").RouteHookExit | HookResponse<"400", "body-size-exceeds-limit-error", unknown> | HookResponse<"400", "body-parse-wrong-chunk-received", unknown> | HookResponse<"400", "body-parse-unknown-error", unknown>;
|
|
6
|
-
beforeSendResponse({ request, currentResponse, exit }: import("../../core/route").RouteHookParamsAfter<import("../../core/request").Request>): import("../../core/route").RouteHookExit;
|
|
7
|
-
sendResponse({ request, currentResponse, exit }: import("../../core/route").RouteHookParamsAfter<import("../../core/request").Request>): import("../../core/route").RouteHookExit;
|
|
8
|
-
};
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import '../../core/route/index.mjs';
|
|
2
|
-
import { stringToBytes } from '@duplojs/utils';
|
|
3
|
-
import './error/index.mjs';
|
|
4
|
-
import '../../core/response/index.mjs';
|
|
5
|
-
import { createHookRouteLifeCycle } from '../../core/route/hooks.mjs';
|
|
6
|
-
import { PredictedResponse } from '../../core/response/predicted.mjs';
|
|
7
|
-
import { HookResponse } from '../../core/response/hook.mjs';
|
|
8
|
-
import { BodySizeExceedsLimitError } from './error/bodySizeExceedsLimitError.mjs';
|
|
9
|
-
import { BodyParseWrongChunkReceived } from './error/bodyParseWrongChunkReceived.mjs';
|
|
10
|
-
import { BodyParseUnknownError } from './error/bodyParseUnknownError.mjs';
|
|
11
|
-
|
|
12
|
-
function makeNodeHook(hub, serverParams) {
|
|
13
|
-
const informationHeaderKey = serverParams.informationHeaderKey;
|
|
14
|
-
const predictedHeaderKey = serverParams.predictedHeaderKey;
|
|
15
|
-
const fromHookHeaderKey = serverParams.fromHookHeaderKey;
|
|
16
|
-
const isDev = hub.config.environment === "DEV";
|
|
17
|
-
const maxBodySize = stringToBytes(serverParams.maxBodySize);
|
|
18
|
-
return createHookRouteLifeCycle({
|
|
19
|
-
async parseBody({ request, exit }) {
|
|
20
|
-
const contentType = request.headers["content-type"] instanceof Array
|
|
21
|
-
? request.headers["content-type"].join(", ")
|
|
22
|
-
: request.headers["content-type"] ?? "";
|
|
23
|
-
const isText = contentType.includes("text/plain");
|
|
24
|
-
const isJson = contentType.includes("application/json");
|
|
25
|
-
if (!isText && !isJson) {
|
|
26
|
-
return exit();
|
|
27
|
-
}
|
|
28
|
-
const { request: rawRequest } = request.raw;
|
|
29
|
-
request.body = await new Promise((resolve, reject) => {
|
|
30
|
-
function errorCallback(error) {
|
|
31
|
-
if (error instanceof BodySizeExceedsLimitError
|
|
32
|
-
|| error instanceof BodyParseWrongChunkReceived) {
|
|
33
|
-
reject(error);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
reject(new BodyParseUnknownError(contentType, error));
|
|
37
|
-
}
|
|
38
|
-
let stringBody = "";
|
|
39
|
-
let byteLengthBody = 0;
|
|
40
|
-
rawRequest.on("error", errorCallback);
|
|
41
|
-
rawRequest.on("data", (chunk) => {
|
|
42
|
-
if (!(chunk instanceof Buffer) && typeof chunk !== "string") {
|
|
43
|
-
rawRequest.emit("error", new BodyParseWrongChunkReceived(chunk));
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
byteLengthBody += chunk instanceof Buffer
|
|
47
|
-
? chunk.byteLength
|
|
48
|
-
: Buffer.byteLength(chunk);
|
|
49
|
-
if (byteLengthBody > maxBodySize) {
|
|
50
|
-
rawRequest.emit("error", new BodySizeExceedsLimitError(serverParams.maxBodySize));
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
stringBody += chunk.toString();
|
|
54
|
-
});
|
|
55
|
-
rawRequest.on("end", () => {
|
|
56
|
-
try {
|
|
57
|
-
resolve(isText
|
|
58
|
-
? stringBody
|
|
59
|
-
: JSON.parse(stringBody));
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
errorCallback(error);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
return exit();
|
|
67
|
-
},
|
|
68
|
-
error({ error, response, exit }) {
|
|
69
|
-
const displayedError = isDev ? error : undefined;
|
|
70
|
-
if (error instanceof BodySizeExceedsLimitError) {
|
|
71
|
-
return response("400", "body-size-exceeds-limit-error", displayedError);
|
|
72
|
-
}
|
|
73
|
-
else if (error instanceof BodyParseWrongChunkReceived) {
|
|
74
|
-
return response("400", "body-parse-wrong-chunk-received", displayedError);
|
|
75
|
-
}
|
|
76
|
-
else if (error instanceof BodyParseUnknownError) {
|
|
77
|
-
return response("400", "body-parse-unknown-error", displayedError);
|
|
78
|
-
}
|
|
79
|
-
return exit();
|
|
80
|
-
},
|
|
81
|
-
beforeSendResponse({ request, currentResponse, exit }) {
|
|
82
|
-
if (!currentResponse.headers?.["content-type"]) {
|
|
83
|
-
const body = currentResponse.body;
|
|
84
|
-
if (typeof body === "string"
|
|
85
|
-
|| body instanceof Error) {
|
|
86
|
-
currentResponse.setHeader("content-type", "text/plain; charset=utf-8");
|
|
87
|
-
}
|
|
88
|
-
else if (typeof body === "object"
|
|
89
|
-
|| typeof body === "number"
|
|
90
|
-
|| typeof body === "boolean") {
|
|
91
|
-
currentResponse.setHeader("content-type", "application/json; charset=utf-8");
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
currentResponse.setHeader(informationHeaderKey, currentResponse.information);
|
|
95
|
-
if (currentResponse instanceof PredictedResponse) {
|
|
96
|
-
currentResponse.setHeader(predictedHeaderKey, "1");
|
|
97
|
-
}
|
|
98
|
-
else if (currentResponse instanceof HookResponse) {
|
|
99
|
-
currentResponse.setHeader(fromHookHeaderKey, currentResponse.fromHook);
|
|
100
|
-
}
|
|
101
|
-
request.raw.response.writeHead(Number(currentResponse.code), currentResponse.headers);
|
|
102
|
-
return exit();
|
|
103
|
-
},
|
|
104
|
-
sendResponse({ request, currentResponse, exit }) {
|
|
105
|
-
const { response: rawResponse } = request.raw;
|
|
106
|
-
const body = currentResponse.body;
|
|
107
|
-
if (body instanceof Error) {
|
|
108
|
-
rawResponse.write(body.toString());
|
|
109
|
-
}
|
|
110
|
-
else if (typeof body === "object"
|
|
111
|
-
|| typeof body === "number"
|
|
112
|
-
|| typeof body === "boolean") {
|
|
113
|
-
rawResponse.write(JSON.stringify(body));
|
|
114
|
-
}
|
|
115
|
-
else if (typeof body === "string") {
|
|
116
|
-
rawResponse.write(body);
|
|
117
|
-
}
|
|
118
|
-
rawResponse.end();
|
|
119
|
-
return exit();
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export { makeNodeHook };
|
|
File without changes
|
|
File without changes
|