@antelopejs/api 0.2.0 → 1.0.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/implementations/api/index.d.ts +19 -0
- package/dist/implementations/api/index.js +71 -0
- package/dist/implementations/api/index.js.map +1 -0
- package/dist/index.d.ts +19 -14
- package/dist/index.js +97 -31
- package/dist/index.js.map +1 -1
- package/dist/middlewares/cors.d.ts +3 -3
- package/dist/middlewares/cors.js +50 -36
- package/dist/middlewares/cors.js.map +1 -1
- package/dist/server.d.ts +8 -7
- package/dist/server.js +229 -108
- package/dist/server.js.map +1 -1
- package/dist/test/antelope.test.d.ts +2 -0
- package/dist/test/antelope.test.js +25 -0
- package/dist/test/antelope.test.js.map +1 -0
- package/package.json +19 -36
- package/dist/implementations/api/beta.d.ts +0 -21
- package/dist/implementations/api/beta.js +0 -54
- package/dist/implementations/api/beta.js.map +0 -1
- package/dist/interfaces/api/beta/index.d.ts +0 -116
- package/dist/interfaces/api/beta/index.js +0 -305
- package/dist/interfaces/api/beta/index.js.map +0 -1
- package/dist/interfaces/api-util/dev.d.ts +0 -2
- package/dist/interfaces/api-util/dev.js +0 -20
- package/dist/interfaces/api-util/dev.js.map +0 -1
- package/dist/test/interface/api.d.ts +0 -69
- package/dist/test/interface/api.js +0 -739
- package/dist/test/interface/api.js.map +0 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type RouteHandler } from "@antelopejs/interface-api";
|
|
2
|
+
import type { Class } from "@antelopejs/interface-core/decorators";
|
|
3
|
+
import { type RequestContext } from "../../server";
|
|
4
|
+
export declare function GetControllerInstance(controllerClass: Class<unknown>, context: RequestContext): Promise<unknown>;
|
|
5
|
+
export declare function Listen(): Promise<void>;
|
|
6
|
+
interface RouteInfo {
|
|
7
|
+
id: string;
|
|
8
|
+
uri: string;
|
|
9
|
+
method: string;
|
|
10
|
+
mode: "prefix" | "postfix" | "handler" | "monitor" | "websocket";
|
|
11
|
+
priority?: number;
|
|
12
|
+
callbackName: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const routesProxy: {
|
|
15
|
+
register: (id: string, handler: RouteHandler) => void;
|
|
16
|
+
unregister: (id: string) => void;
|
|
17
|
+
getRoutes: () => RouteInfo[];
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.routesProxy = void 0;
|
|
4
|
+
exports.GetControllerInstance = GetControllerInstance;
|
|
5
|
+
exports.Listen = Listen;
|
|
6
|
+
const interface_api_1 = require("@antelopejs/interface-api");
|
|
7
|
+
const interface_core_1 = require("@antelopejs/interface-core");
|
|
8
|
+
const index_1 = require("../../index");
|
|
9
|
+
const server_1 = require("../../server");
|
|
10
|
+
const classCacheSymbol = Symbol();
|
|
11
|
+
const registeredRoutes = new Map();
|
|
12
|
+
function getControllerCache(context) {
|
|
13
|
+
if (!context[classCacheSymbol]) {
|
|
14
|
+
context[classCacheSymbol] = new Map();
|
|
15
|
+
}
|
|
16
|
+
return context[classCacheSymbol];
|
|
17
|
+
}
|
|
18
|
+
async function applyComputedProperties(controllerInstance, controllerMetadata, context) {
|
|
19
|
+
await Promise.all(Object.entries(controllerMetadata.computed_props).map(async ([propertyKey, parameter]) => {
|
|
20
|
+
const computedValue = await (0, interface_api_1.computeParameter)(context, parameter, controllerInstance);
|
|
21
|
+
controllerInstance[propertyKey] = computedValue;
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
async function GetControllerInstance(controllerClass, context) {
|
|
25
|
+
const controllerCache = getControllerCache(context);
|
|
26
|
+
const cacheKey = controllerClass.prototype;
|
|
27
|
+
const cachedController = controllerCache.get(cacheKey);
|
|
28
|
+
if (cachedController) {
|
|
29
|
+
return cachedController;
|
|
30
|
+
}
|
|
31
|
+
const typedControllerClass = controllerClass;
|
|
32
|
+
const controllerInstance = new typedControllerClass();
|
|
33
|
+
const controllerMetadata = (0, interface_core_1.GetMetadata)(typedControllerClass, interface_api_1.ControllerMeta);
|
|
34
|
+
await applyComputedProperties(controllerInstance, controllerMetadata, context);
|
|
35
|
+
controllerCache.set(cacheKey, controllerInstance);
|
|
36
|
+
return controllerInstance;
|
|
37
|
+
}
|
|
38
|
+
async function Listen() {
|
|
39
|
+
await (0, index_1.listenServers)();
|
|
40
|
+
}
|
|
41
|
+
async function invokeHandler(handler, context) {
|
|
42
|
+
const controllerClass = handler.proto.constructor;
|
|
43
|
+
const controllerInstance = await GetControllerInstance(controllerClass, context);
|
|
44
|
+
const resolvedParameters = await Promise.all(handler.parameters.map((parameter) => (0, interface_api_1.computeParameter)(context, parameter, controllerInstance)));
|
|
45
|
+
return handler.callback.apply(controllerInstance, resolvedParameters);
|
|
46
|
+
}
|
|
47
|
+
exports.routesProxy = {
|
|
48
|
+
register: (id, handler) => {
|
|
49
|
+
registeredRoutes.set(id, handler);
|
|
50
|
+
(0, server_1.registerHandler)(`dev/${id}`, handler.mode, handler.method, handler.location, async (context) => invokeHandler(handler, context), handler.priority);
|
|
51
|
+
},
|
|
52
|
+
unregister: (id) => {
|
|
53
|
+
registeredRoutes.delete(id);
|
|
54
|
+
(0, server_1.unregisterHandler)(`dev/${id}`);
|
|
55
|
+
},
|
|
56
|
+
getRoutes: () => {
|
|
57
|
+
const routes = [];
|
|
58
|
+
registeredRoutes.forEach((handler, id) => {
|
|
59
|
+
routes.push({
|
|
60
|
+
id,
|
|
61
|
+
uri: handler.location,
|
|
62
|
+
method: handler.method,
|
|
63
|
+
mode: handler.mode,
|
|
64
|
+
priority: handler.priority,
|
|
65
|
+
callbackName: handler.callback.name || "anonymous",
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
return routes;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/implementations/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AC,EAAU,EAAQ,EAAE;QAC/B,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC5B,IAAA,0BAAiB,EAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACjC,CAAC;IACD,SAAS,EAAE,GAAgB,EAAE;QAC3B,MAAM,MAAM,GAAgB,EAAE,CAAC;QAE/B,gBAAgB,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,EAAE,EAAE;YACvC,MAAM,CAAC,IAAI,CAAC;gBACV,EAAE;gBACF,GAAG,EAAE,OAAO,CAAC,QAAQ;gBACrB,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,IAAI,EAAE,OAAO,CAAC,IAAI;gBAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;gBAC1B,YAAY,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI,IAAI,WAAW;aACnD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
|
-
import * as http from
|
|
2
|
-
import * as https from
|
|
3
|
-
import
|
|
4
|
-
interface
|
|
5
|
-
protocol: 'http';
|
|
1
|
+
import * as http from "node:http";
|
|
2
|
+
import * as https from "node:https";
|
|
3
|
+
import "./middlewares/cors";
|
|
4
|
+
interface ServerNetworkConfig {
|
|
6
5
|
host?: string;
|
|
7
6
|
port?: number;
|
|
8
7
|
}
|
|
9
|
-
interface
|
|
10
|
-
protocol:
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
interface HTTPConfig extends http.ServerOptions, ServerNetworkConfig {
|
|
9
|
+
protocol: "http";
|
|
10
|
+
}
|
|
11
|
+
interface HTTPSConfig extends https.ServerOptions, ServerNetworkConfig {
|
|
12
|
+
protocol: "https";
|
|
13
|
+
}
|
|
14
|
+
type ServerConfig = HTTPConfig | HTTPSConfig;
|
|
15
|
+
type AllowedOrigin = string | RegExp | Array<string | RegExp>;
|
|
16
|
+
interface CorsConfig {
|
|
17
|
+
allowedOrigins?: AllowedOrigin;
|
|
18
|
+
allowedMethods?: string[];
|
|
13
19
|
}
|
|
14
20
|
interface Config {
|
|
15
|
-
servers?:
|
|
16
|
-
cors?:
|
|
17
|
-
|
|
18
|
-
allowedMethods?: string[];
|
|
19
|
-
};
|
|
21
|
+
servers?: ServerConfig[];
|
|
22
|
+
cors?: CorsConfig;
|
|
23
|
+
autoListen?: boolean;
|
|
20
24
|
}
|
|
21
25
|
export declare function getConfig(): Config;
|
|
22
26
|
export declare function construct(config: Config): Promise<void>;
|
|
23
27
|
export declare function destroy(): void;
|
|
24
28
|
export declare function start(): void;
|
|
29
|
+
export declare function listenServers(): Promise<void>;
|
|
25
30
|
export declare function stop(): void;
|
|
26
31
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -37,49 +37,115 @@ exports.getConfig = getConfig;
|
|
|
37
37
|
exports.construct = construct;
|
|
38
38
|
exports.destroy = destroy;
|
|
39
39
|
exports.start = start;
|
|
40
|
+
exports.listenServers = listenServers;
|
|
40
41
|
exports.stop = stop;
|
|
41
|
-
const http = __importStar(require("http"));
|
|
42
|
-
const https = __importStar(require("https"));
|
|
43
|
-
const
|
|
42
|
+
const http = __importStar(require("node:http"));
|
|
43
|
+
const https = __importStar(require("node:https"));
|
|
44
|
+
const interface_core_1 = require("@antelopejs/interface-core");
|
|
44
45
|
const server_1 = require("./server");
|
|
45
46
|
require("./middlewares/cors");
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const logging_1 = require("@antelopejs/interface-core/logging");
|
|
48
|
+
const DEFAULT_HTTP_PORT = 80;
|
|
49
|
+
const DEFAULT_HOST = "localhost";
|
|
50
|
+
const DEFAULT_SERVER_CONFIG = {
|
|
51
|
+
protocol: "http",
|
|
52
|
+
port: DEFAULT_HTTP_PORT,
|
|
53
|
+
};
|
|
54
|
+
const SERVER_FACTORY_BY_PROTOCOL = {
|
|
55
|
+
http: (config) => createHTTPServer(config),
|
|
56
|
+
https: (config) => createHTTPSServer(config),
|
|
57
|
+
};
|
|
58
|
+
let conf = {
|
|
59
|
+
servers: [],
|
|
60
|
+
};
|
|
61
|
+
let servers = [];
|
|
62
|
+
let listening = false;
|
|
63
|
+
function createHTTPServer(config) {
|
|
64
|
+
const server = http.createServer(config);
|
|
65
|
+
attachServerListeners(server, "http", "ws");
|
|
66
|
+
return server;
|
|
67
|
+
}
|
|
68
|
+
function createHTTPSServer(config) {
|
|
69
|
+
const server = https.createServer(config);
|
|
70
|
+
attachServerListeners(server, "https", "wss");
|
|
71
|
+
return server;
|
|
72
|
+
}
|
|
73
|
+
function attachServerListeners(server, protocol, socketProtocol) {
|
|
74
|
+
server.on("request", (req, res) => void (0, server_1.requestListener)(req, res, protocol));
|
|
75
|
+
server.on("upgrade", (req, socket, head) => void (0, server_1.upgradeListener)(req, socket, head, socketProtocol));
|
|
76
|
+
}
|
|
77
|
+
function resolveServers(config) {
|
|
78
|
+
if (config.servers && config.servers.length > 0) {
|
|
79
|
+
return config.servers;
|
|
80
|
+
}
|
|
81
|
+
return [DEFAULT_SERVER_CONFIG];
|
|
82
|
+
}
|
|
83
|
+
function createConfiguredServer(config) {
|
|
84
|
+
const serverFactory = SERVER_FACTORY_BY_PROTOCOL[config.protocol];
|
|
85
|
+
return serverFactory(config);
|
|
86
|
+
}
|
|
87
|
+
function listenServer(server, config) {
|
|
88
|
+
if (server.listening) {
|
|
89
|
+
return Promise.resolve();
|
|
90
|
+
}
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const onListening = () => {
|
|
93
|
+
cleanup();
|
|
94
|
+
logging_1.Logging.Info(`Server started, listening on ${config.protocol}://${config.host ?? DEFAULT_HOST}:${config.port}`);
|
|
95
|
+
resolve();
|
|
96
|
+
};
|
|
97
|
+
const onError = (error) => {
|
|
98
|
+
cleanup();
|
|
99
|
+
reject(error);
|
|
100
|
+
};
|
|
101
|
+
const cleanup = () => {
|
|
102
|
+
server.off("listening", onListening);
|
|
103
|
+
server.off("error", onError);
|
|
104
|
+
};
|
|
105
|
+
server.once("listening", onListening);
|
|
106
|
+
server.once("error", onError);
|
|
107
|
+
server.listen(config.port, config.host);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
49
110
|
function getConfig() {
|
|
50
111
|
return conf;
|
|
51
112
|
}
|
|
52
113
|
async function construct(config) {
|
|
53
|
-
conf =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
await (0, beta_1.ImplementInterface)(Promise.resolve().then(() => __importStar(require('@ajs.local/api/beta'))), Promise.resolve().then(() => __importStar(require('./implementations/api/beta'))));
|
|
114
|
+
conf = {
|
|
115
|
+
...config,
|
|
116
|
+
servers: resolveServers(config),
|
|
117
|
+
};
|
|
118
|
+
void (0, interface_core_1.ImplementInterface)(await Promise.resolve().then(() => __importStar(require("@antelopejs/interface-api"))), await Promise.resolve().then(() => __importStar(require("./implementations/api"))));
|
|
59
119
|
}
|
|
60
120
|
function destroy() { }
|
|
61
121
|
function start() {
|
|
62
|
-
servers = conf.servers.map((
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
122
|
+
servers = (conf.servers ?? []).map((serverConfig) => createConfiguredServer(serverConfig));
|
|
123
|
+
listening = false;
|
|
124
|
+
if (conf.autoListen !== false) {
|
|
125
|
+
void listenServers().catch((error) => {
|
|
126
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
logging_1.Logging.Info(`Unable to start listening servers: ${message}`);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async function listenServers() {
|
|
132
|
+
if (listening || servers.length === 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
listening = true;
|
|
136
|
+
try {
|
|
137
|
+
await Promise.all((conf.servers ?? []).map((serverConfig, index) => listenServer(servers[index], serverConfig)));
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
listening = false;
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
80
143
|
}
|
|
81
144
|
function stop() {
|
|
82
|
-
|
|
145
|
+
for (const server of servers) {
|
|
146
|
+
server.close();
|
|
147
|
+
}
|
|
83
148
|
servers = [];
|
|
149
|
+
listening = false;
|
|
84
150
|
}
|
|
85
151
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqIA,8BAEC;AAED,8BAUC;AAED,0BAAkC;AAElC,sBAYC;AAED,sCAiBC;AAED,oBAMC;AA9LD,gDAAkC;AAClC,kDAAoC;AAGpC,+DAAgE;AAChE,qCAA4D;AAC5D,8BAA4B;AAC5B,gEAA6D;AAmC7D,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B,MAAM,YAAY,GAAG,WAAW,CAAC;AACjC,MAAM,qBAAqB,GAAe;IACxC,QAAQ,EAAE,MAAM;IAChB,IAAI,EAAE,iBAAiB;CACxB,CAAC;AAEF,MAAM,0BAA0B,GAA0C;IACxE,IAAI,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,gBAAgB,CAAC,MAAoB,CAAC;IACxD,KAAK,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,iBAAiB,CAAC,MAAqB,CAAC;CAC5D,CAAC;AAEF,IAAI,IAAI,GAAW;IACjB,OAAO,EAAE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AC7B,MAAM,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC;IACD,OAAO,GAAG,EAAE,CAAC;IACb,SAAS,GAAG,KAAK,CAAC;AACpB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HTTPResult, RequestContext } from
|
|
2
|
-
declare const Cors_base: import("@
|
|
1
|
+
import { HTTPResult, type RequestContext } from "@antelopejs/interface-api";
|
|
2
|
+
declare const Cors_base: import("@antelopejs/interface-api").ControllerClass<object>;
|
|
3
3
|
export declare class Cors extends Cors_base {
|
|
4
|
-
cors(
|
|
4
|
+
cors(requestContext: RequestContext): HTTPResult | undefined;
|
|
5
5
|
}
|
|
6
6
|
export {};
|
package/dist/middlewares/cors.js
CHANGED
|
@@ -13,62 +13,76 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.Cors = void 0;
|
|
16
|
-
const
|
|
16
|
+
const interface_api_1 = require("@antelopejs/interface-api");
|
|
17
17
|
const __1 = require("..");
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const DEFAULT_ALLOWED_METHODS = "GET,HEAD,PUT,PATCH,POST,DELETE";
|
|
19
|
+
const FALSE_ORIGIN = "false";
|
|
20
|
+
const TRUE_CREDENTIALS = "true";
|
|
21
|
+
const NO_BODY_LENGTH = "0";
|
|
22
|
+
function isString(value) {
|
|
23
|
+
return typeof value === "string" || value instanceof String;
|
|
24
|
+
}
|
|
25
|
+
function toHeaderValue(value) {
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
return value.join(",");
|
|
28
|
+
}
|
|
29
|
+
return value ?? "";
|
|
20
30
|
}
|
|
21
31
|
function isOriginAllowed(origin, allowedOrigin) {
|
|
22
32
|
if (Array.isArray(allowedOrigin)) {
|
|
23
|
-
|
|
24
|
-
if (isOriginAllowed(origin, allowedOrigin[i])) {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
33
|
+
return allowedOrigin.some((item) => isOriginAllowed(origin, item));
|
|
29
34
|
}
|
|
30
|
-
|
|
35
|
+
if (isString(allowedOrigin)) {
|
|
31
36
|
return origin === allowedOrigin;
|
|
32
37
|
}
|
|
33
|
-
|
|
38
|
+
if (allowedOrigin instanceof RegExp) {
|
|
34
39
|
return allowedOrigin.test(origin);
|
|
35
40
|
}
|
|
36
|
-
|
|
37
|
-
return !!allowedOrigin;
|
|
38
|
-
}
|
|
41
|
+
return Boolean(allowedOrigin);
|
|
39
42
|
}
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
function addPreflightHeaders(response, requestContext, allowedOriginHeader) {
|
|
44
|
+
const config = (0, __1.getConfig)();
|
|
45
|
+
const allowedMethods = config.cors?.allowedMethods?.join(",") ?? DEFAULT_ALLOWED_METHODS;
|
|
46
|
+
const requestedHeaders = toHeaderValue(requestContext.rawRequest.headers["access-control-request-headers"]);
|
|
47
|
+
response.addHeader("Access-Control-Allow-Origin", allowedOriginHeader);
|
|
48
|
+
response.addHeader("Vary", "Origin");
|
|
49
|
+
response.addHeader("Access-Control-Allow-Methods", allowedMethods);
|
|
50
|
+
response.addHeader("Access-Control-Allow-Credentials", TRUE_CREDENTIALS);
|
|
51
|
+
response.addHeader("Access-Control-Allow-Headers", requestedHeaders);
|
|
52
|
+
response.addHeader("Vary", "Access-Control-Request-Headers");
|
|
53
|
+
response.addHeader("Content-Length", NO_BODY_LENGTH);
|
|
54
|
+
}
|
|
55
|
+
function addStandardCorsHeaders(requestContext, allowedOriginHeader) {
|
|
56
|
+
requestContext.response.addHeader("Access-Control-Allow-Origin", allowedOriginHeader);
|
|
57
|
+
requestContext.response.addHeader("Vary", "Origin");
|
|
58
|
+
requestContext.response.addHeader("Access-Control-Allow-Credentials", TRUE_CREDENTIALS);
|
|
59
|
+
}
|
|
60
|
+
class Cors extends (0, interface_api_1.Controller)("") {
|
|
61
|
+
cors(requestContext) {
|
|
42
62
|
const config = (0, __1.getConfig)();
|
|
43
63
|
if (!config.cors) {
|
|
44
|
-
return;
|
|
64
|
+
return undefined;
|
|
45
65
|
}
|
|
46
|
-
const requestOrigin =
|
|
47
|
-
const isAllowed =
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
response
|
|
53
|
-
response
|
|
54
|
-
response.addHeader('Access-Control-Allow-Headers', ctx.rawRequest.headers['access-control-request-headers']);
|
|
55
|
-
response.addHeader('Vary', 'Access-Control-Request-Headers');
|
|
56
|
-
response.addHeader('Content-Length', '0');
|
|
66
|
+
const requestOrigin = requestContext.rawRequest.headers.origin;
|
|
67
|
+
const isAllowed = requestOrigin
|
|
68
|
+
? isOriginAllowed(requestOrigin, config.cors.allowedOrigins)
|
|
69
|
+
: false;
|
|
70
|
+
const allowedOriginHeader = isAllowed && requestOrigin ? requestOrigin : FALSE_ORIGIN;
|
|
71
|
+
if (requestContext.rawRequest.method === "OPTIONS") {
|
|
72
|
+
const response = new interface_api_1.HTTPResult(204, null);
|
|
73
|
+
addPreflightHeaders(response, requestContext, allowedOriginHeader);
|
|
57
74
|
return response;
|
|
58
75
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
ctx.response?.addHeader('Vary', 'Origin');
|
|
62
|
-
ctx.response?.addHeader('Access-Control-Allow-Credentials', 'true');
|
|
63
|
-
}
|
|
76
|
+
addStandardCorsHeaders(requestContext, allowedOriginHeader);
|
|
77
|
+
return undefined;
|
|
64
78
|
}
|
|
65
79
|
}
|
|
66
80
|
exports.Cors = Cors;
|
|
67
81
|
__decorate([
|
|
68
|
-
(0,
|
|
69
|
-
__param(0, (0,
|
|
82
|
+
(0, interface_api_1.Prefix)("any", "/", interface_api_1.HandlerPriority.HIGHEST),
|
|
83
|
+
__param(0, (0, interface_api_1.Context)()),
|
|
70
84
|
__metadata("design:type", Function),
|
|
71
85
|
__metadata("design:paramtypes", [Object]),
|
|
72
|
-
__metadata("design:returntype",
|
|
86
|
+
__metadata("design:returntype", Object)
|
|
73
87
|
], Cors.prototype, "cors", null);
|
|
74
88
|
//# sourceMappingURL=cors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cors.js","sourceRoot":"","sources":["../../src/middlewares/cors.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"cors.js","sourceRoot":"","sources":["../../src/middlewares/cors.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,6DAOmC;AACnC,0BAA+B;AAM/B,MAAM,uBAAuB,GAAG,gCAAgC,CAAC;AACjE,MAAM,YAAY,GAAG,OAAO,CAAC;AAC7B,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAChC,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,MAAM,CAAC;AAC9D,CAAC;AAED,SAAS,aAAa,CAAC,KAAyB;IAC9C,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,KAAK,IAAI,EAAE,CAAC;AACrB,CAAC;AAED,SAAS,eAAe,CACtB,MAAc,EACd,aAA6B;IAE7B,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;QACjC,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IACrE,CAAC;IAED,IAAI,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;QAC5B,OAAO,MAAM,KAAK,aAAa,CAAC;IAClC,CAAC;IAED,IAAI,aAAa,YAAY,MAAM,EAAE,CAAC;QACpC,OAAO,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAA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C5C,MAAM,MAAM,GAAG,IAAA,aAAS,GAAE,CAAC;QAE3B,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,aAAa,GAAG,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC;QAC/D,MAAM,SAAS,GAAG,aAAa;YAC7B,CAAC,CAAC,eAAe,CAAC,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC;YAC5D,CAAC,CAAC,KAAK,CAAC;QACV,MAAM,mBAAmB,GACvB,SAAS,IAAI,aAAa,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,YAAY,CAAC;QAE5D,IAAI,cAAc,CAAC,UAAU,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACnD,MAAM,QAAQ,GAAG,IAAI,0BAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YAC3C,mBAAmB,CAAC,QAAQ,EAAE,cAAc,EAAE,mBAAmB,CAAC,CAAC;YACnE,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,sBAAsB,CAAC,cAAc,EAAE,mBAAmB,CAAC,CAAC;QAC5D,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAzBD,oBAyBC;AAvBC;IADC,IAAA,sBAAM,EAAC,KAAK,EAAE,GAAG,EAAE,+BAAe,CAAC,OAAO,CAAC;IACtC,WAAA,IAAA,uBAAO,GAAE,CAAA;;;;gCAsBd"}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
export type RouteCallback = (context: RequestContext) =>
|
|
1
|
+
import { type IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type stream from "node:stream";
|
|
3
|
+
import { HandlerPriority, HTTPResult } from "@antelopejs/interface-api";
|
|
4
|
+
export type RouteCallback = (context: RequestContext) => unknown;
|
|
5
5
|
export interface IdentifiableRouteCallback {
|
|
6
6
|
id: string;
|
|
7
7
|
callback: RouteCallback;
|
|
@@ -13,9 +13,10 @@ export interface RequestContext {
|
|
|
13
13
|
url: URL;
|
|
14
14
|
routeParameters: Record<string, string>;
|
|
15
15
|
response: HTTPResult;
|
|
16
|
+
error?: unknown;
|
|
16
17
|
connection?: unknown;
|
|
17
18
|
}
|
|
18
|
-
export declare function registerHandler(id: string, mode:
|
|
19
|
+
export declare function registerHandler(id: string, mode: "prefix" | "postfix" | "handler" | "monitor" | "websocket", method: string | undefined, location: string, handler: RouteCallback, priority?: HandlerPriority): void;
|
|
19
20
|
export declare function unregisterHandler(id: string): void;
|
|
20
|
-
export declare function requestListener(req: IncomingMessage, res: ServerResponse, protocol:
|
|
21
|
-
export declare function upgradeListener(req: IncomingMessage, socket: stream.Duplex, head: Buffer, protocol:
|
|
21
|
+
export declare function requestListener(req: IncomingMessage, res: ServerResponse, protocol: "http" | "https"): Promise<void>;
|
|
22
|
+
export declare function upgradeListener(req: IncomingMessage, socket: stream.Duplex, head: Buffer, protocol: "ws" | "wss"): Promise<void>;
|