@duplojs/http 0.8.4 → 0.9.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/dist/core/builders/preflight/builder.d.ts +5 -6
- package/dist/core/builders/preflight/process.d.ts +3 -4
- package/dist/core/builders/preflight/route.d.ts +4 -5
- package/dist/core/builders/process/builder.d.ts +4 -5
- package/dist/core/builders/process/checker.d.ts +2 -3
- package/dist/core/builders/process/cut.d.ts +4 -5
- package/dist/core/builders/process/exports.d.ts +3 -4
- package/dist/core/builders/process/extract.d.ts +2 -3
- package/dist/core/builders/process/presetChecker.d.ts +2 -3
- package/dist/core/builders/process/process.d.ts +3 -4
- package/dist/core/builders/route/builder.d.ts +6 -6
- package/dist/core/builders/route/checker.d.ts +2 -3
- package/dist/core/builders/route/cut.d.ts +4 -5
- package/dist/core/builders/route/extract.d.ts +3 -3
- package/dist/core/builders/route/handler.d.ts +3 -4
- package/dist/core/builders/route/presetChecker.d.ts +2 -3
- package/dist/core/builders/route/process.d.ts +3 -4
- package/dist/core/defaultHooks/index.d.ts +1 -1
- package/dist/core/functionsBuilders/index.cjs +1 -0
- package/dist/core/functionsBuilders/index.d.ts +1 -0
- package/dist/core/functionsBuilders/index.mjs +1 -0
- package/dist/core/functionsBuilders/route/build.d.ts +5 -2
- package/dist/core/functionsBuilders/route/create.d.ts +3 -5
- package/dist/core/functionsBuilders/route/{hook.cjs → default/hook.cjs} +16 -7
- package/dist/core/functionsBuilders/route/{hook.d.ts → default/hook.d.ts} +5 -5
- package/dist/core/functionsBuilders/route/{hook.mjs → default/hook.mjs} +16 -7
- package/dist/core/functionsBuilders/route/{default.cjs → default/index.cjs} +22 -34
- package/dist/core/functionsBuilders/route/default/index.d.ts +2 -0
- package/dist/core/functionsBuilders/route/{default.mjs → default/index.mjs} +11 -29
- package/dist/core/functionsBuilders/route/index.cjs +2 -9
- package/dist/core/functionsBuilders/route/index.d.ts +0 -1
- package/dist/core/functionsBuilders/route/index.mjs +1 -2
- package/dist/core/functionsBuilders/router/build.cjs +7 -0
- package/dist/core/functionsBuilders/router/build.d.ts +14 -0
- package/dist/core/functionsBuilders/router/build.mjs +5 -0
- package/dist/core/functionsBuilders/router/create.cjs +7 -0
- package/dist/core/functionsBuilders/router/create.d.ts +15 -0
- package/dist/core/functionsBuilders/router/create.mjs +5 -0
- package/dist/core/functionsBuilders/router/default/index.cjs +52 -0
- package/dist/core/functionsBuilders/router/default/index.d.ts +1 -0
- package/dist/core/functionsBuilders/router/default/index.mjs +50 -0
- package/dist/core/functionsBuilders/router/index.cjs +11 -0
- package/dist/core/functionsBuilders/router/index.d.ts +3 -0
- package/dist/core/functionsBuilders/router/index.mjs +3 -0
- package/dist/core/functionsBuilders/steps/create.d.ts +3 -6
- package/dist/core/hub/defaultMalformedUrlHandler.d.ts +1 -1
- package/dist/core/hub/defaultNotfoundHandler.d.ts +1 -1
- package/dist/core/hub/hooks.d.ts +2 -2
- package/dist/core/hub/index.cjs +5 -0
- package/dist/core/hub/index.d.ts +6 -3
- package/dist/core/hub/index.mjs +5 -0
- package/dist/core/implementHttpServer.cjs +1 -1
- package/dist/core/implementHttpServer.d.ts +2 -2
- package/dist/core/implementHttpServer.mjs +2 -2
- package/dist/core/index.cjs +15 -14
- package/dist/core/index.mjs +8 -6
- package/dist/core/process/index.d.ts +0 -7
- package/dist/core/request/bodyController/formData.cjs +1 -0
- package/dist/core/request/bodyController/formData.d.ts +2 -0
- package/dist/core/request/bodyController/formData.mjs +1 -0
- package/dist/core/route/hooks.cjs +2 -17
- package/dist/core/route/hooks.d.ts +15 -32
- package/dist/core/route/hooks.mjs +3 -17
- package/dist/core/route/index.cjs +0 -1
- package/dist/core/route/index.mjs +1 -1
- package/dist/core/router/{buildSystemRoute.cjs → createRouterElementSystem.cjs} +4 -3
- package/dist/core/router/createRouterElementSystem.d.ts +9 -0
- package/dist/core/router/{buildSystemRoute.mjs → createRouterElementSystem.mjs} +4 -3
- package/dist/core/router/index.cjs +19 -60
- package/dist/core/router/index.d.ts +3 -4
- package/dist/core/router/index.mjs +17 -56
- package/dist/core/router/types/buildedRouter.d.ts +2 -12
- package/dist/core/router/types/index.cjs +4 -0
- package/dist/core/router/types/index.d.ts +4 -0
- package/dist/core/router/types/index.mjs +4 -0
- package/dist/core/router/types/router.cjs +2 -0
- package/dist/core/router/types/router.d.ts +12 -0
- package/dist/core/router/types/router.mjs +1 -0
- package/dist/core/router/types/routerElement.cjs +2 -0
- package/dist/core/router/types/routerElement.d.ts +8 -0
- package/dist/core/router/types/routerElement.mjs +1 -0
- package/dist/core/router/types/routerElementSystem.cjs +2 -0
- package/dist/core/router/types/routerElementSystem.d.ts +6 -0
- package/dist/core/router/types/routerElementSystem.mjs +1 -0
- package/dist/core/router/types/routerElementWrapper.cjs +2 -0
- package/dist/core/router/types/routerElementWrapper.d.ts +2 -0
- package/dist/core/router/types/routerElementWrapper.mjs +1 -0
- package/dist/core/steps/cut.d.ts +1 -2
- package/dist/core/steps/handler.d.ts +1 -2
- package/dist/core/steps/types/stepFunctionParams.d.ts +2 -2
- package/dist/interfaces/node/bodyReaders/formData/index.cjs +1 -0
- package/dist/interfaces/node/bodyReaders/formData/index.mjs +1 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.cjs +10 -1
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.d.ts +2 -1
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.mjs +10 -1
- package/dist/interfaces/node/createHttpServer.cjs +2 -2
- package/dist/interfaces/node/createHttpServer.mjs +2 -2
- package/dist/interfaces/node/hooks/index.d.ts +3 -3
- package/dist/plugins/cacheController/hooks.cjs +2 -1
- package/dist/plugins/cacheController/hooks.d.ts +1 -1
- package/dist/plugins/cacheController/hooks.mjs +2 -1
- package/dist/plugins/cors/headerFunctions/allowHeaders.cjs +11 -0
- package/dist/plugins/cors/headerFunctions/allowHeaders.d.ts +5 -0
- package/dist/plugins/cors/headerFunctions/allowHeaders.mjs +9 -0
- package/dist/plugins/cors/headerFunctions/allowMethods.cjs +16 -0
- package/dist/plugins/cors/headerFunctions/allowMethods.d.ts +6 -0
- package/dist/plugins/cors/headerFunctions/allowMethods.mjs +14 -0
- package/dist/plugins/cors/headerFunctions/allowOrigin.cjs +24 -0
- package/dist/plugins/cors/headerFunctions/allowOrigin.d.ts +7 -0
- package/dist/plugins/cors/headerFunctions/allowOrigin.mjs +22 -0
- package/dist/plugins/cors/headerFunctions/credentials.cjs +11 -0
- package/dist/plugins/cors/headerFunctions/credentials.d.ts +5 -0
- package/dist/plugins/cors/headerFunctions/credentials.mjs +9 -0
- package/dist/plugins/cors/headerFunctions/exposeHeaders.cjs +11 -0
- package/dist/plugins/cors/headerFunctions/exposeHeaders.d.ts +5 -0
- package/dist/plugins/cors/headerFunctions/exposeHeaders.mjs +9 -0
- package/dist/plugins/cors/headerFunctions/index.cjs +19 -0
- package/dist/plugins/cors/headerFunctions/index.d.ts +7 -0
- package/dist/plugins/cors/headerFunctions/index.mjs +7 -0
- package/dist/plugins/cors/headerFunctions/maxAge.cjs +11 -0
- package/dist/plugins/cors/headerFunctions/maxAge.d.ts +5 -0
- package/dist/plugins/cors/headerFunctions/maxAge.mjs +9 -0
- package/dist/plugins/cors/headerFunctions/vary.cjs +35 -0
- package/dist/plugins/cors/headerFunctions/vary.d.ts +5 -0
- package/dist/plugins/cors/headerFunctions/vary.mjs +33 -0
- package/dist/plugins/cors/index.cjs +9 -0
- package/dist/plugins/cors/index.d.ts +2 -0
- package/dist/plugins/cors/index.mjs +2 -0
- package/dist/plugins/cors/metadata.cjs +8 -0
- package/dist/plugins/cors/metadata.d.ts +1 -0
- package/dist/plugins/cors/metadata.mjs +6 -0
- package/dist/plugins/cors/plugin.cjs +108 -0
- package/dist/plugins/cors/plugin.d.ts +12 -0
- package/dist/plugins/cors/plugin.mjs +106 -0
- package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +1 -1
- package/dist/plugins/static/makeRouteFile.d.ts +2 -2
- package/dist/plugins/static/makeRouteFolder.cjs +1 -1
- package/dist/plugins/static/makeRouteFolder.d.ts +2 -2
- package/dist/plugins/static/makeRouteFolder.mjs +1 -1
- package/dist/plugins/static/plugin.d.ts +1 -1
- package/package.json +30 -5
- package/dist/core/functionsBuilders/route/default.d.ts +0 -1
- package/dist/core/router/buildSystemRoute.d.ts +0 -11
- /package/dist/core/{router/decodeUrl.cjs → decodeUrl.cjs} +0 -0
- /package/dist/core/{router/decodeUrl.d.ts → decodeUrl.d.ts} +0 -0
- /package/dist/core/{router/decodeUrl.mjs → decodeUrl.mjs} +0 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var allowHeaders = require('./allowHeaders.cjs');
|
|
4
|
+
var allowMethods = require('./allowMethods.cjs');
|
|
5
|
+
var allowOrigin = require('./allowOrigin.cjs');
|
|
6
|
+
var credentials = require('./credentials.cjs');
|
|
7
|
+
var exposeHeaders = require('./exposeHeaders.cjs');
|
|
8
|
+
var maxAge = require('./maxAge.cjs');
|
|
9
|
+
var vary = require('./vary.cjs');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.allowHeadersFunction = allowHeaders.allowHeadersFunction;
|
|
14
|
+
exports.allowMethodsFunction = allowMethods.allowMethodsFunction;
|
|
15
|
+
exports.allowOriginFunction = allowOrigin.allowOriginFunction;
|
|
16
|
+
exports.credentialsFunction = credentials.credentialsFunction;
|
|
17
|
+
exports.exposeHeadersFunction = exposeHeaders.exposeHeadersFunction;
|
|
18
|
+
exports.maxAgeFunction = maxAge.maxAgeFunction;
|
|
19
|
+
exports.varyFunction = vary.varyFunction;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { allowHeadersFunction } from './allowHeaders.mjs';
|
|
2
|
+
export { allowMethodsFunction } from './allowMethods.mjs';
|
|
3
|
+
export { allowOriginFunction } from './allowOrigin.mjs';
|
|
4
|
+
export { credentialsFunction } from './credentials.mjs';
|
|
5
|
+
export { exposeHeadersFunction } from './exposeHeaders.mjs';
|
|
6
|
+
export { maxAgeFunction } from './maxAge.mjs';
|
|
7
|
+
export { varyFunction } from './vary.mjs';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const starRegex = /(^|,) *\* *(?=,|$)/;
|
|
4
|
+
const originRegex = /(^|,) *origin *(?=,|$)/i;
|
|
5
|
+
const varyFunction = {
|
|
6
|
+
default() {
|
|
7
|
+
const maxStoreSize = 500;
|
|
8
|
+
const store = new Map();
|
|
9
|
+
return (request, response) => {
|
|
10
|
+
const cachedVary = store.get(request.origin);
|
|
11
|
+
if (cachedVary) {
|
|
12
|
+
response.setHeader("vary", cachedVary);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
let varyValue = Array.isArray(response.headers?.vary)
|
|
16
|
+
? response.headers.vary.join(", ")
|
|
17
|
+
: response.headers?.vary;
|
|
18
|
+
if (varyValue === undefined) {
|
|
19
|
+
varyValue = "Origin";
|
|
20
|
+
}
|
|
21
|
+
else if (starRegex.test(varyValue)) {
|
|
22
|
+
varyValue = "*";
|
|
23
|
+
}
|
|
24
|
+
else if (!originRegex.test(varyValue)) {
|
|
25
|
+
varyValue = `${varyValue}, Origin`;
|
|
26
|
+
}
|
|
27
|
+
if (store.size < maxStoreSize) {
|
|
28
|
+
store.set(request.origin, varyValue);
|
|
29
|
+
}
|
|
30
|
+
response.setHeader("vary", varyValue);
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.varyFunction = varyFunction;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const starRegex = /(^|,) *\* *(?=,|$)/;
|
|
2
|
+
const originRegex = /(^|,) *origin *(?=,|$)/i;
|
|
3
|
+
const varyFunction = {
|
|
4
|
+
default() {
|
|
5
|
+
const maxStoreSize = 500;
|
|
6
|
+
const store = new Map();
|
|
7
|
+
return (request, response) => {
|
|
8
|
+
const cachedVary = store.get(request.origin);
|
|
9
|
+
if (cachedVary) {
|
|
10
|
+
response.setHeader("vary", cachedVary);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
let varyValue = Array.isArray(response.headers?.vary)
|
|
14
|
+
? response.headers.vary.join(", ")
|
|
15
|
+
: response.headers?.vary;
|
|
16
|
+
if (varyValue === undefined) {
|
|
17
|
+
varyValue = "Origin";
|
|
18
|
+
}
|
|
19
|
+
else if (starRegex.test(varyValue)) {
|
|
20
|
+
varyValue = "*";
|
|
21
|
+
}
|
|
22
|
+
else if (!originRegex.test(varyValue)) {
|
|
23
|
+
varyValue = `${varyValue}, Origin`;
|
|
24
|
+
}
|
|
25
|
+
if (store.size < maxStoreSize) {
|
|
26
|
+
store.set(request.origin, varyValue);
|
|
27
|
+
}
|
|
28
|
+
response.setHeader("vary", varyValue);
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { varyFunction };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const IgnoreRouteCorsMetadata: import("../../core/metadata").MetadataHandler<"ignore-by-cors", unknown>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@duplojs/utils');
|
|
4
|
+
var metadata = require('./metadata.cjs');
|
|
5
|
+
var index = require('../../core/route/index.cjs');
|
|
6
|
+
require('./headerFunctions/index.cjs');
|
|
7
|
+
var vary = require('./headerFunctions/vary.cjs');
|
|
8
|
+
var allowOrigin = require('./headerFunctions/allowOrigin.cjs');
|
|
9
|
+
var exposeHeaders = require('./headerFunctions/exposeHeaders.cjs');
|
|
10
|
+
var credentials = require('./headerFunctions/credentials.cjs');
|
|
11
|
+
var hooks = require('../../core/route/hooks.cjs');
|
|
12
|
+
var allowMethods = require('./headerFunctions/allowMethods.cjs');
|
|
13
|
+
var allowHeaders = require('./headerFunctions/allowHeaders.cjs');
|
|
14
|
+
var maxAge = require('./headerFunctions/maxAge.cjs');
|
|
15
|
+
|
|
16
|
+
/* eslint-disable @typescript-eslint/prefer-for-of */
|
|
17
|
+
function corsPlugin(params) {
|
|
18
|
+
const headerFunctionOtherRoutes = [];
|
|
19
|
+
if (params.allowOrigin) {
|
|
20
|
+
headerFunctionOtherRoutes.push(vary.varyFunction.default());
|
|
21
|
+
headerFunctionOtherRoutes.push(typeof params.allowOrigin === "function"
|
|
22
|
+
? allowOrigin.allowOriginFunction.isFunction(params.allowOrigin)
|
|
23
|
+
: allowOrigin.allowOriginFunction.default(utils.toRegExp(params.allowOrigin === true
|
|
24
|
+
? "*"
|
|
25
|
+
: params.allowOrigin)));
|
|
26
|
+
}
|
|
27
|
+
if (params.exposeHeaders) {
|
|
28
|
+
headerFunctionOtherRoutes.push(utils.pipe(params.exposeHeaders, utils.A.coalescing, utils.A.join(","), exposeHeaders.exposeHeadersFunction.default));
|
|
29
|
+
}
|
|
30
|
+
if (params.credentials) {
|
|
31
|
+
headerFunctionOtherRoutes.push(credentials.credentialsFunction.default());
|
|
32
|
+
}
|
|
33
|
+
const hookOtherRoute = hooks.createHookRouteLifeCycle({
|
|
34
|
+
beforeSendResponse: (params) => {
|
|
35
|
+
for (let index = 0; index < headerFunctionOtherRoutes.length; index++) {
|
|
36
|
+
headerFunctionOtherRoutes[index](params.request, params.currentResponse);
|
|
37
|
+
}
|
|
38
|
+
return params.next();
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
return () => ({
|
|
42
|
+
name: "cors",
|
|
43
|
+
hooksHubLifeCycle: [
|
|
44
|
+
{
|
|
45
|
+
beforeServerBuildRoutes: (hub) => {
|
|
46
|
+
const headerFunctionRouteOptions = [];
|
|
47
|
+
if (params.allowMethods === true) {
|
|
48
|
+
const allowMethodsFunctionIsBool = utils.pipe(hub.routes, utils.G.filter((route) => !utils.A.some(route.definition.metadata, metadata.IgnoreRouteCorsMetadata.is)), utils.G.map((route) => utils.A.map(route.definition.paths, (path) => ({
|
|
49
|
+
path,
|
|
50
|
+
method: route.definition.method,
|
|
51
|
+
}))), utils.G.flat, utils.G.reduce(utils.G.reduceFrom({}), ({ element, lastValue, next }) => {
|
|
52
|
+
lastValue[element.path] = lastValue[element.path]
|
|
53
|
+
? `${lastValue[element.path]},${element.method}`
|
|
54
|
+
: element.method;
|
|
55
|
+
return next(lastValue);
|
|
56
|
+
}), allowMethods.allowMethodsFunction.isBool);
|
|
57
|
+
headerFunctionRouteOptions.push(allowMethodsFunctionIsBool);
|
|
58
|
+
}
|
|
59
|
+
else if (params.allowMethods) {
|
|
60
|
+
headerFunctionRouteOptions.push(utils.pipe(params.allowMethods, utils.A.coalescing, utils.A.join(","), allowMethods.allowMethodsFunction.default));
|
|
61
|
+
}
|
|
62
|
+
if (params.allowHeaders) {
|
|
63
|
+
headerFunctionRouteOptions.push(allowHeaders.allowHeadersFunction.default(params.allowHeaders === true
|
|
64
|
+
? "*"
|
|
65
|
+
: utils.pipe(params.allowHeaders, utils.A.coalescing, utils.A.join(","))));
|
|
66
|
+
}
|
|
67
|
+
if (params.maxAge) {
|
|
68
|
+
headerFunctionRouteOptions.push(maxAge.maxAgeFunction.default(params.maxAge.toString()));
|
|
69
|
+
}
|
|
70
|
+
const hookRouteOptions = hooks.createHookRouteLifeCycle({
|
|
71
|
+
beforeRouteExecution: (params) => {
|
|
72
|
+
const response = params.response("204", "cors");
|
|
73
|
+
for (let index = 0; index < headerFunctionRouteOptions.length; index++) {
|
|
74
|
+
headerFunctionRouteOptions[index](params.request, response);
|
|
75
|
+
}
|
|
76
|
+
return response;
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
const routeOptions = index.createRoute({
|
|
80
|
+
paths: ["/*"],
|
|
81
|
+
method: "OPTIONS",
|
|
82
|
+
hooks: [hookRouteOptions],
|
|
83
|
+
metadata: [metadata.IgnoreRouteCorsMetadata()],
|
|
84
|
+
steps: [],
|
|
85
|
+
preflightSteps: [],
|
|
86
|
+
bodyController: null,
|
|
87
|
+
});
|
|
88
|
+
hub.register(routeOptions);
|
|
89
|
+
return hub;
|
|
90
|
+
},
|
|
91
|
+
beforeBuildRoute: (route) => {
|
|
92
|
+
if (route.definition.method === "OPTIONS" || utils.A.some(route.definition.metadata, metadata.IgnoreRouteCorsMetadata.is)) {
|
|
93
|
+
return route;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
...route,
|
|
97
|
+
definition: {
|
|
98
|
+
...route.definition,
|
|
99
|
+
hooks: [...route.definition.hooks, hookOtherRoute],
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
exports.corsPlugin = corsPlugin;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type AnyTuple, type MaybePromise, type O } from "@duplojs/utils";
|
|
2
|
+
import { type RequestMethods } from "../../core/request";
|
|
3
|
+
import { type HubPlugin } from "../../core/hub";
|
|
4
|
+
export interface CorsPluginParams {
|
|
5
|
+
readonly allowOrigin?: string | RegExp | AnyTuple<string> | ((origin: string) => MaybePromise<boolean>) | true;
|
|
6
|
+
readonly allowHeaders?: string | AnyTuple<string> | true;
|
|
7
|
+
readonly exposeHeaders?: string | AnyTuple<string>;
|
|
8
|
+
readonly maxAge?: number;
|
|
9
|
+
readonly credentials?: boolean;
|
|
10
|
+
readonly allowMethods?: RequestMethods | AnyTuple<RequestMethods> | true;
|
|
11
|
+
}
|
|
12
|
+
export declare function corsPlugin<GenericParams extends CorsPluginParams>(params: GenericParams & O.RequireAtLeastOne<GenericParams>): () => HubPlugin;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { toRegExp, pipe, A, G } from '@duplojs/utils';
|
|
2
|
+
import { IgnoreRouteCorsMetadata } from './metadata.mjs';
|
|
3
|
+
import { createRoute } from '../../core/route/index.mjs';
|
|
4
|
+
import './headerFunctions/index.mjs';
|
|
5
|
+
import { varyFunction } from './headerFunctions/vary.mjs';
|
|
6
|
+
import { allowOriginFunction } from './headerFunctions/allowOrigin.mjs';
|
|
7
|
+
import { exposeHeadersFunction } from './headerFunctions/exposeHeaders.mjs';
|
|
8
|
+
import { credentialsFunction } from './headerFunctions/credentials.mjs';
|
|
9
|
+
import { createHookRouteLifeCycle } from '../../core/route/hooks.mjs';
|
|
10
|
+
import { allowMethodsFunction } from './headerFunctions/allowMethods.mjs';
|
|
11
|
+
import { allowHeadersFunction } from './headerFunctions/allowHeaders.mjs';
|
|
12
|
+
import { maxAgeFunction } from './headerFunctions/maxAge.mjs';
|
|
13
|
+
|
|
14
|
+
/* eslint-disable @typescript-eslint/prefer-for-of */
|
|
15
|
+
function corsPlugin(params) {
|
|
16
|
+
const headerFunctionOtherRoutes = [];
|
|
17
|
+
if (params.allowOrigin) {
|
|
18
|
+
headerFunctionOtherRoutes.push(varyFunction.default());
|
|
19
|
+
headerFunctionOtherRoutes.push(typeof params.allowOrigin === "function"
|
|
20
|
+
? allowOriginFunction.isFunction(params.allowOrigin)
|
|
21
|
+
: allowOriginFunction.default(toRegExp(params.allowOrigin === true
|
|
22
|
+
? "*"
|
|
23
|
+
: params.allowOrigin)));
|
|
24
|
+
}
|
|
25
|
+
if (params.exposeHeaders) {
|
|
26
|
+
headerFunctionOtherRoutes.push(pipe(params.exposeHeaders, A.coalescing, A.join(","), exposeHeadersFunction.default));
|
|
27
|
+
}
|
|
28
|
+
if (params.credentials) {
|
|
29
|
+
headerFunctionOtherRoutes.push(credentialsFunction.default());
|
|
30
|
+
}
|
|
31
|
+
const hookOtherRoute = createHookRouteLifeCycle({
|
|
32
|
+
beforeSendResponse: (params) => {
|
|
33
|
+
for (let index = 0; index < headerFunctionOtherRoutes.length; index++) {
|
|
34
|
+
headerFunctionOtherRoutes[index](params.request, params.currentResponse);
|
|
35
|
+
}
|
|
36
|
+
return params.next();
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return () => ({
|
|
40
|
+
name: "cors",
|
|
41
|
+
hooksHubLifeCycle: [
|
|
42
|
+
{
|
|
43
|
+
beforeServerBuildRoutes: (hub) => {
|
|
44
|
+
const headerFunctionRouteOptions = [];
|
|
45
|
+
if (params.allowMethods === true) {
|
|
46
|
+
const allowMethodsFunctionIsBool = pipe(hub.routes, G.filter((route) => !A.some(route.definition.metadata, IgnoreRouteCorsMetadata.is)), G.map((route) => A.map(route.definition.paths, (path) => ({
|
|
47
|
+
path,
|
|
48
|
+
method: route.definition.method,
|
|
49
|
+
}))), G.flat, G.reduce(G.reduceFrom({}), ({ element, lastValue, next }) => {
|
|
50
|
+
lastValue[element.path] = lastValue[element.path]
|
|
51
|
+
? `${lastValue[element.path]},${element.method}`
|
|
52
|
+
: element.method;
|
|
53
|
+
return next(lastValue);
|
|
54
|
+
}), allowMethodsFunction.isBool);
|
|
55
|
+
headerFunctionRouteOptions.push(allowMethodsFunctionIsBool);
|
|
56
|
+
}
|
|
57
|
+
else if (params.allowMethods) {
|
|
58
|
+
headerFunctionRouteOptions.push(pipe(params.allowMethods, A.coalescing, A.join(","), allowMethodsFunction.default));
|
|
59
|
+
}
|
|
60
|
+
if (params.allowHeaders) {
|
|
61
|
+
headerFunctionRouteOptions.push(allowHeadersFunction.default(params.allowHeaders === true
|
|
62
|
+
? "*"
|
|
63
|
+
: pipe(params.allowHeaders, A.coalescing, A.join(","))));
|
|
64
|
+
}
|
|
65
|
+
if (params.maxAge) {
|
|
66
|
+
headerFunctionRouteOptions.push(maxAgeFunction.default(params.maxAge.toString()));
|
|
67
|
+
}
|
|
68
|
+
const hookRouteOptions = createHookRouteLifeCycle({
|
|
69
|
+
beforeRouteExecution: (params) => {
|
|
70
|
+
const response = params.response("204", "cors");
|
|
71
|
+
for (let index = 0; index < headerFunctionRouteOptions.length; index++) {
|
|
72
|
+
headerFunctionRouteOptions[index](params.request, response);
|
|
73
|
+
}
|
|
74
|
+
return response;
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
const routeOptions = createRoute({
|
|
78
|
+
paths: ["/*"],
|
|
79
|
+
method: "OPTIONS",
|
|
80
|
+
hooks: [hookRouteOptions],
|
|
81
|
+
metadata: [IgnoreRouteCorsMetadata()],
|
|
82
|
+
steps: [],
|
|
83
|
+
preflightSteps: [],
|
|
84
|
+
bodyController: null,
|
|
85
|
+
});
|
|
86
|
+
hub.register(routeOptions);
|
|
87
|
+
return hub;
|
|
88
|
+
},
|
|
89
|
+
beforeBuildRoute: (route) => {
|
|
90
|
+
if (route.definition.method === "OPTIONS" || A.some(route.definition.metadata, IgnoreRouteCorsMetadata.is)) {
|
|
91
|
+
return route;
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
...route,
|
|
95
|
+
definition: {
|
|
96
|
+
...route.definition,
|
|
97
|
+
hooks: [...route.definition.hooks, hookOtherRoute],
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { corsPlugin };
|
|
@@ -17,7 +17,7 @@ export declare function makeOpenApiRoute(routePath: RoutePath, openApiPage: stri
|
|
|
17
17
|
readonly coerce: boolean;
|
|
18
18
|
readonly checkers: readonly [];
|
|
19
19
|
}>>>;
|
|
20
|
-
theFunction(floor: {}, param: import("../../core/steps").HandlerStepFunctionParams<import("../../core/
|
|
20
|
+
theFunction(floor: {}, param: import("../../core/steps").HandlerStepFunctionParams<import("../../core/response").PredictedResponse<"200", "swaggerUi", string>>): import("@duplojs/utils").MaybePromise<import("../../core/response").PredictedResponse<"200", "swaggerUi", string>>;
|
|
21
21
|
readonly metadata: readonly [];
|
|
22
22
|
}>];
|
|
23
23
|
}>;
|
|
@@ -26,7 +26,7 @@ export declare function makeRouteFile(params: MakeRouteFileParams): import("../.
|
|
|
26
26
|
readonly method: "GET";
|
|
27
27
|
readonly metadata: readonly [import("../../core/metadata").Metadata<"ignore-by-route-store", unknown>];
|
|
28
28
|
readonly hooks: readonly [{
|
|
29
|
-
readonly beforeSendResponse: ({ currentResponse, next }: import("../../core/route").RouteHookParamsAfter
|
|
29
|
+
readonly beforeSendResponse: ({ currentResponse, next }: import("../../core/route").RouteHookParamsAfter) => import("../../core/route").RouteHookNext;
|
|
30
30
|
}];
|
|
31
31
|
readonly paths: readonly [`/${string}`] | AnyTuple<`/${string}`>;
|
|
32
32
|
readonly preflightSteps: readonly [];
|
|
@@ -41,7 +41,7 @@ export declare function makeRouteFile(params: MakeRouteFileParams): import("../.
|
|
|
41
41
|
readonly checkExist: boolean;
|
|
42
42
|
readonly checkers: readonly [];
|
|
43
43
|
}>>>, NoInfer<ResponseContract.Contract<"304", "resource.notModified", import("@duplojs/utils/dataParser").DataParserEmpty<import("@duplojs/utils/dataParser").DataParserDefinitionEmpty>>>];
|
|
44
|
-
theFunction(floor: {}, param: import("../../core/steps").HandlerStepFunctionParams<import("../../core/
|
|
44
|
+
theFunction(floor: {}, param: import("../../core/steps").HandlerStepFunctionParams<import("../../core/response").PredictedResponse<"200", "resource.found", SF.FileInterface> | import("../../core/response").PredictedResponse<"304", "resource.notModified", undefined>>): import("@duplojs/utils").MaybePromise<import("../../core/response").PredictedResponse<"200", "resource.found", SF.FileInterface> | import("../../core/response").PredictedResponse<"304", "resource.notModified", undefined>>;
|
|
45
45
|
readonly metadata: readonly [];
|
|
46
46
|
}>];
|
|
47
47
|
}>;
|
|
@@ -15,7 +15,7 @@ function makeRouteFolder(params) {
|
|
|
15
15
|
const localPrefix = utils.A.coalescing(params.prefix);
|
|
16
16
|
const routePath = utils.A.mapTuple(localPrefix, (prefix) => `${prefix}/*`);
|
|
17
17
|
const prefixRegex = utils.pipe(localPrefix, utils.A.map(utils.escapeRegExp), utils.A.join("|"), (value) => new RegExp(`^(?:${value})(?:/|$)`));
|
|
18
|
-
const getResourcePath = utils.innerPipe(utils.S.replace(prefixRegex, ""), utils.S.prepend(sourcePath));
|
|
18
|
+
const getResourcePath = utils.innerPipe(utils.S.replace(prefixRegex, ""), utils.S.prepend(sourcePath), utils.S.replace(/\/+$/, ""));
|
|
19
19
|
return builder.useRouteBuilder("GET", routePath, {
|
|
20
20
|
metadata: [ignoreByRouteStore.IgnoreByRouteStoreMetadata()],
|
|
21
21
|
hooks: [hooks.createCacheControllerHooks(params.cacheControlConfig)],
|
|
@@ -13,7 +13,7 @@ export declare function makeRouteFolder(params: MakeRouteFolderParams): import("
|
|
|
13
13
|
readonly method: "GET";
|
|
14
14
|
readonly metadata: readonly [import("../../core/metadata").Metadata<"ignore-by-route-store", unknown>];
|
|
15
15
|
readonly hooks: readonly [{
|
|
16
|
-
readonly beforeSendResponse: ({ currentResponse, next }: import("../../core/route").RouteHookParamsAfter
|
|
16
|
+
readonly beforeSendResponse: ({ currentResponse, next }: import("../../core/route").RouteHookParamsAfter) => import("../../core/route").RouteHookNext;
|
|
17
17
|
}];
|
|
18
18
|
readonly paths: [`/${string}/*`] | [`/${string}/*`, ...`/${string}/*`[]];
|
|
19
19
|
readonly preflightSteps: readonly [];
|
|
@@ -32,7 +32,7 @@ export declare function makeRouteFolder(params: MakeRouteFolderParams): import("
|
|
|
32
32
|
readonly coerce: boolean;
|
|
33
33
|
readonly checkers: readonly [];
|
|
34
34
|
}>>>, NoInfer<ResponseContract.Contract<"304", "resource.notModified", import("@duplojs/utils/dataParser").DataParserEmpty<import("@duplojs/utils/dataParser").DataParserDefinitionEmpty>>>];
|
|
35
|
-
theFunction(floor: {}, param: import("../../core/steps").HandlerStepFunctionParams<import("../../core/
|
|
35
|
+
theFunction(floor: {}, param: import("../../core/steps").HandlerStepFunctionParams<import("../../core/response").PredictedResponse<"200", "resource.found", SF.FileInterface> | import("../../core/response").PredictedResponse<"404", "resource.notfound", undefined> | import("../../core/response").PredictedResponse<"304", "resource.notModified", undefined>>): import("@duplojs/utils").MaybePromise<import("../../core/response").PredictedResponse<"200", "resource.found", SF.FileInterface> | import("../../core/response").PredictedResponse<"404", "resource.notfound", undefined> | import("../../core/response").PredictedResponse<"304", "resource.notModified", undefined>>;
|
|
36
36
|
readonly metadata: readonly [];
|
|
37
37
|
}>];
|
|
38
38
|
}>;
|
|
@@ -13,7 +13,7 @@ function makeRouteFolder(params) {
|
|
|
13
13
|
const localPrefix = A.coalescing(params.prefix);
|
|
14
14
|
const routePath = A.mapTuple(localPrefix, (prefix) => `${prefix}/*`);
|
|
15
15
|
const prefixRegex = pipe(localPrefix, A.map(escapeRegExp), A.join("|"), (value) => new RegExp(`^(?:${value})(?:/|$)`));
|
|
16
|
-
const getResourcePath = innerPipe(S.replace(prefixRegex, ""), S.prepend(sourcePath));
|
|
16
|
+
const getResourcePath = innerPipe(S.replace(prefixRegex, ""), S.prepend(sourcePath), S.replace(/\/+$/, ""));
|
|
17
17
|
return useRouteBuilder("GET", routePath, {
|
|
18
18
|
metadata: [IgnoreByRouteStoreMetadata()],
|
|
19
19
|
hooks: [createCacheControllerHooks(params.cacheControlConfig)],
|
|
@@ -11,7 +11,7 @@ export interface StaticPluginFileParams extends BaseStaticPluginParams {
|
|
|
11
11
|
}
|
|
12
12
|
export interface StaticPluginFolderParams extends BaseStaticPluginParams {
|
|
13
13
|
readonly prefix: RoutePath | AnyTuple<RoutePath>;
|
|
14
|
-
readonly
|
|
14
|
+
readonly directoryFallBackFile?: string;
|
|
15
15
|
}
|
|
16
16
|
declare const StaticPluginError_base: new (params: {
|
|
17
17
|
"@DuplojsStaticPlugin/static-plugin"?: unknown;
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duplojs/http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"author":
|
|
3
|
+
"version": "0.9.5",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "mathcovax",
|
|
6
|
+
"url": "https://github.com/mathcovax"
|
|
7
|
+
},
|
|
5
8
|
"license": "MIT",
|
|
6
9
|
"type": "module",
|
|
7
10
|
"repository": {
|
|
@@ -68,6 +71,11 @@
|
|
|
68
71
|
"import": "./dist/plugins/static/index.mjs",
|
|
69
72
|
"require": "./dist/plugins/static/index.cjs",
|
|
70
73
|
"types": "./dist/plugins/static/index.d.ts"
|
|
74
|
+
},
|
|
75
|
+
"./cors": {
|
|
76
|
+
"import": "./dist/plugins/cors/index.mjs",
|
|
77
|
+
"require": "./dist/plugins/cors/index.cjs",
|
|
78
|
+
"types": "./dist/plugins/cors/index.d.ts"
|
|
71
79
|
}
|
|
72
80
|
},
|
|
73
81
|
"files": [
|
|
@@ -77,7 +85,7 @@
|
|
|
77
85
|
"peerDependencies": {
|
|
78
86
|
"@duplojs/data-parser-tools": ">=0.2.9 <1.0.0",
|
|
79
87
|
"@duplojs/server-utils": ">=0.2.2 <1.0.0",
|
|
80
|
-
"@duplojs/utils": ">=1.5.
|
|
88
|
+
"@duplojs/utils": ">=1.5.15 <2.0.0"
|
|
81
89
|
},
|
|
82
90
|
"devDependencies": {
|
|
83
91
|
"@commitlint/cli": "19.8.1",
|
|
@@ -108,8 +116,25 @@
|
|
|
108
116
|
"integration",
|
|
109
117
|
"docs"
|
|
110
118
|
],
|
|
111
|
-
"keywords": [],
|
|
112
119
|
"engines": {
|
|
113
120
|
"node": ">=22.15.1"
|
|
114
|
-
}
|
|
121
|
+
},
|
|
122
|
+
"keywords": [
|
|
123
|
+
"duplojs",
|
|
124
|
+
"http",
|
|
125
|
+
"client",
|
|
126
|
+
"sse",
|
|
127
|
+
"static",
|
|
128
|
+
"codeGenerator",
|
|
129
|
+
"openApiGenerator",
|
|
130
|
+
"cors",
|
|
131
|
+
"cross-platform"
|
|
132
|
+
],
|
|
133
|
+
"homepage": "https://http.duplojs.dev/",
|
|
134
|
+
"contributors": [
|
|
135
|
+
{
|
|
136
|
+
"name": "ZeRiix",
|
|
137
|
+
"url": "https://github.com/ZeRiix"
|
|
138
|
+
}
|
|
139
|
+
]
|
|
115
140
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const defaultRouteFunctionBuilder: (route: import("../../route").Route, params: import("./create").RouteFunctionBuilderParams) => import("@duplojs/utils").MaybePromise<import("./create").BuildRouteNotSupportEither | import("./create").BuildRouteSuccessEither | import("..").BuildStepNotSupportEither>;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { type BuildRouteFunctionParams } from "../functionsBuilders";
|
|
2
|
-
import { type HandlerStep } from "../steps";
|
|
3
|
-
interface BuildSystemRouteParams {
|
|
4
|
-
handlerStep: HandlerStep;
|
|
5
|
-
buildParams: BuildRouteFunctionParams;
|
|
6
|
-
}
|
|
7
|
-
export declare function buildSystemRoute(params: BuildSystemRouteParams): Promise<{
|
|
8
|
-
bodyReader: import("../request").BodyReader<"empty">;
|
|
9
|
-
buildedRoute: import("../functionsBuilders").BuildedRouteFunction;
|
|
10
|
-
}>;
|
|
11
|
-
export {};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|