express-ext 0.1.20 → 0.1.21
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/lib/client.js +69 -0
- package/lib/index.js +74 -1
- package/package.json +1 -1
- package/src/client.ts +69 -0
- package/src/index.ts +28 -5
package/lib/client.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var http = require("http");
|
|
4
|
+
var https = require("https");
|
|
5
|
+
function getHealthSecure(url, timeout) {
|
|
6
|
+
return new Promise(function (resolve) {
|
|
7
|
+
https.get(url, { rejectUnauthorized: false }, function (res) {
|
|
8
|
+
var data = '';
|
|
9
|
+
res.on('data', function (d) {
|
|
10
|
+
data += d;
|
|
11
|
+
});
|
|
12
|
+
res.on('end', function () {
|
|
13
|
+
resolve({ statusCode: res.statusCode, data: data, statusMessage: res.statusMessage });
|
|
14
|
+
});
|
|
15
|
+
}).on('error', function (e) {
|
|
16
|
+
return { statusCode: 500, statusMessage: e };
|
|
17
|
+
});
|
|
18
|
+
setTimeout(function () { return resolve({ statusCode: 408, statusMessage: 'Time out' }); }, timeout);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function getHealth(url, timeout) {
|
|
22
|
+
return new Promise(function (resolve) {
|
|
23
|
+
http.get(url, function (res) {
|
|
24
|
+
var data = '';
|
|
25
|
+
res.on('data', function (d) {
|
|
26
|
+
data += d;
|
|
27
|
+
});
|
|
28
|
+
res.on('end', function () {
|
|
29
|
+
resolve({ statusCode: res.statusCode, data: data, statusMessage: res.statusMessage });
|
|
30
|
+
});
|
|
31
|
+
}).on('error', function (e) {
|
|
32
|
+
return { statusCode: 500, statusMessage: e };
|
|
33
|
+
});
|
|
34
|
+
setTimeout(function () { return resolve({ statusCode: 408, statusMessage: 'Time out' }); }, timeout);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
var ClientChecker = (function () {
|
|
38
|
+
function ClientChecker(service, url, timeout) {
|
|
39
|
+
this.service = service;
|
|
40
|
+
this.url = url;
|
|
41
|
+
this.timeout = (timeout ? timeout : 4200);
|
|
42
|
+
this.check = this.check.bind(this);
|
|
43
|
+
this.name = this.name.bind(this);
|
|
44
|
+
this.build = this.build.bind(this);
|
|
45
|
+
}
|
|
46
|
+
ClientChecker.prototype.check = function () {
|
|
47
|
+
var obj = {};
|
|
48
|
+
if (this.url.startsWith('https://')) {
|
|
49
|
+
return getHealthSecure(this.url, this.timeout).then(function (r) { return obj = r; });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return getHealth(this.url, this.timeout).then(function (r) { return obj = r; });
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
ClientChecker.prototype.name = function () {
|
|
56
|
+
return this.service;
|
|
57
|
+
};
|
|
58
|
+
ClientChecker.prototype.build = function (data, err) {
|
|
59
|
+
if (err) {
|
|
60
|
+
if (!data) {
|
|
61
|
+
data = {};
|
|
62
|
+
}
|
|
63
|
+
data['error'] = err;
|
|
64
|
+
}
|
|
65
|
+
return data;
|
|
66
|
+
};
|
|
67
|
+
return ClientChecker;
|
|
68
|
+
}());
|
|
69
|
+
exports.ClientChecker = ClientChecker;
|
package/lib/index.js
CHANGED
|
@@ -1 +1,74 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
function __export(m) {
|
|
3
|
+
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
|
+
}
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var GenericController_1 = require("./GenericController");
|
|
7
|
+
exports.GenericHandler = GenericController_1.GenericController;
|
|
8
|
+
var GenericSearchController_1 = require("./GenericSearchController");
|
|
9
|
+
exports.GenericSearchHandler = GenericSearchController_1.GenericSearchController;
|
|
10
|
+
var HealthController_1 = require("./HealthController");
|
|
11
|
+
exports.HealthHandler = HealthController_1.HealthController;
|
|
12
|
+
var LoadController_1 = require("./LoadController");
|
|
13
|
+
exports.LoadHandler = LoadController_1.LoadController;
|
|
14
|
+
exports.ViewHandler = LoadController_1.LoadController;
|
|
15
|
+
exports.ViewController = LoadController_1.LoadController;
|
|
16
|
+
var LoadSearchController_1 = require("./LoadSearchController");
|
|
17
|
+
exports.LoadSearchHandler = LoadSearchController_1.LoadSearchController;
|
|
18
|
+
var LogController_1 = require("./LogController");
|
|
19
|
+
exports.LogHandler = LogController_1.LogController;
|
|
20
|
+
var LowCodeController_1 = require("./LowCodeController");
|
|
21
|
+
exports.LowCodeHandler = LowCodeController_1.Controller;
|
|
22
|
+
exports.LowCodeController = LowCodeController_1.Controller;
|
|
23
|
+
var SearchController_1 = require("./SearchController");
|
|
24
|
+
exports.SearchHandler = SearchController_1.SearchController;
|
|
25
|
+
__export(require("./health"));
|
|
26
|
+
__export(require("./client"));
|
|
27
|
+
__export(require("./HealthController"));
|
|
28
|
+
__export(require("./LogController"));
|
|
29
|
+
__export(require("./log"));
|
|
30
|
+
__export(require("./http"));
|
|
31
|
+
__export(require("./view"));
|
|
32
|
+
__export(require("./LoadController"));
|
|
33
|
+
__export(require("./search_func"));
|
|
34
|
+
__export(require("./search"));
|
|
35
|
+
__export(require("./SearchController"));
|
|
36
|
+
__export(require("./LoadSearchController"));
|
|
37
|
+
__export(require("./resources"));
|
|
38
|
+
__export(require("./edit"));
|
|
39
|
+
__export(require("./GenericController"));
|
|
40
|
+
__export(require("./GenericSearchController"));
|
|
41
|
+
__export(require("./LowCodeController"));
|
|
42
|
+
function allow(access) {
|
|
43
|
+
var ao = access.origin;
|
|
44
|
+
if (typeof ao === 'string') {
|
|
45
|
+
return function (req, res, next) {
|
|
46
|
+
res.header('Access-Control-Allow-Origin', access.origin);
|
|
47
|
+
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
48
|
+
res.header('Access-Control-Allow-Methods', access.methods);
|
|
49
|
+
res.setHeader('Access-Control-Allow-Headers', access.headers);
|
|
50
|
+
next();
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
else if (Array.isArray(ao) && ao.length > 0) {
|
|
54
|
+
return function (req, res, next) {
|
|
55
|
+
var origin = req.headers.origin;
|
|
56
|
+
if (origin) {
|
|
57
|
+
if (ao.includes(origin)) {
|
|
58
|
+
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
62
|
+
res.header('Access-Control-Allow-Methods', access.methods);
|
|
63
|
+
res.setHeader('Access-Control-Allow-Headers', access.headers);
|
|
64
|
+
next();
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return function (req, res, next) {
|
|
68
|
+
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
69
|
+
res.header('Access-Control-Allow-Methods', access.methods);
|
|
70
|
+
res.setHeader('Access-Control-Allow-Headers', access.headers);
|
|
71
|
+
next();
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exports.allow = allow;
|
package/package.json
CHANGED
package/src/client.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as http from 'http';
|
|
2
|
+
import * as https from 'https';
|
|
3
|
+
import { AnyMap, HealthChecker } from './health';
|
|
4
|
+
|
|
5
|
+
function getHealthSecure(url: string, timeout: number): Promise<AnyMap> {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
https.get(url, { rejectUnauthorized: false }, (res: any) => {
|
|
8
|
+
let data = '';
|
|
9
|
+
res.on('data', (d: any) => {
|
|
10
|
+
data += d;
|
|
11
|
+
});
|
|
12
|
+
res.on('end', () => {
|
|
13
|
+
resolve({ statusCode: res.statusCode, data, statusMessage: res.statusMessage });
|
|
14
|
+
});
|
|
15
|
+
}).on('error', (e: any) => {
|
|
16
|
+
return { statusCode: 500, statusMessage: e };
|
|
17
|
+
});
|
|
18
|
+
setTimeout(() => resolve({ statusCode: 408, statusMessage: 'Time out' }), timeout);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function getHealth(url: string, timeout: number): Promise<AnyMap> {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
http.get(url, (res: any) => {
|
|
24
|
+
let data = '';
|
|
25
|
+
res.on('data', (d: any) => {
|
|
26
|
+
data += d;
|
|
27
|
+
});
|
|
28
|
+
res.on('end', () => {
|
|
29
|
+
resolve({ statusCode: res.statusCode, data, statusMessage: res.statusMessage });
|
|
30
|
+
});
|
|
31
|
+
}).on('error', (e: any) => {
|
|
32
|
+
return { statusCode: 500, statusMessage: e };
|
|
33
|
+
});
|
|
34
|
+
setTimeout(() => resolve({ statusCode: 408, statusMessage: 'Time out' }), timeout);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export class ClientChecker implements HealthChecker {
|
|
38
|
+
timeout: number;
|
|
39
|
+
constructor(private service: string, private url: string, timeout: number) {
|
|
40
|
+
this.timeout = (timeout ? timeout : 4200);
|
|
41
|
+
this.check = this.check.bind(this);
|
|
42
|
+
this.name = this.name.bind(this);
|
|
43
|
+
this.build = this.build.bind(this);
|
|
44
|
+
}
|
|
45
|
+
check(): Promise<AnyMap> {
|
|
46
|
+
let obj = {} as AnyMap;
|
|
47
|
+
if (this.url.startsWith('https://')) {
|
|
48
|
+
return getHealthSecure(this.url, this.timeout).then(
|
|
49
|
+
r => obj = r
|
|
50
|
+
);
|
|
51
|
+
} else {
|
|
52
|
+
return getHealth(this.url, this.timeout).then(
|
|
53
|
+
r => obj = r
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
name(): string {
|
|
58
|
+
return this.service;
|
|
59
|
+
}
|
|
60
|
+
build(data: AnyMap, err: any): AnyMap {
|
|
61
|
+
if (err) {
|
|
62
|
+
if (!data) {
|
|
63
|
+
data = {} as AnyMap;
|
|
64
|
+
}
|
|
65
|
+
data['error'] = err;
|
|
66
|
+
}
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -25,6 +25,7 @@ export {Controller as LowCodeController};
|
|
|
25
25
|
export {Service as LowCodeService};
|
|
26
26
|
|
|
27
27
|
export * from './health';
|
|
28
|
+
export * from './client';
|
|
28
29
|
export * from './HealthController';
|
|
29
30
|
export * from './LogController';
|
|
30
31
|
export * from './log';
|
|
@@ -43,15 +44,37 @@ export * from './GenericSearchController';
|
|
|
43
44
|
export * from './LowCodeController';
|
|
44
45
|
|
|
45
46
|
export interface AccessConfig {
|
|
46
|
-
origin
|
|
47
|
-
credentials
|
|
48
|
-
methods
|
|
49
|
-
headers: string
|
|
47
|
+
origin?: string | string[];
|
|
48
|
+
credentials?: string | string[];
|
|
49
|
+
methods?: string | string[];
|
|
50
|
+
headers: number | string | ReadonlyArray<string>;
|
|
50
51
|
}
|
|
51
52
|
export type AccessControlAllowConfig = AccessConfig;
|
|
52
53
|
export function allow(access: AccessConfig): (req: Request, res: Response, next: NextFunction) => void {
|
|
54
|
+
const ao = access.origin;
|
|
55
|
+
if (typeof ao === 'string') {
|
|
56
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
57
|
+
res.header('Access-Control-Allow-Origin', access.origin);
|
|
58
|
+
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
59
|
+
res.header('Access-Control-Allow-Methods', access.methods);
|
|
60
|
+
res.setHeader('Access-Control-Allow-Headers', access.headers);
|
|
61
|
+
next();
|
|
62
|
+
};
|
|
63
|
+
} else if (Array.isArray(ao) && ao.length > 0) {
|
|
64
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
65
|
+
const origin = req.headers.origin;
|
|
66
|
+
if (origin) {
|
|
67
|
+
if (ao.includes(origin)) {
|
|
68
|
+
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
72
|
+
res.header('Access-Control-Allow-Methods', access.methods);
|
|
73
|
+
res.setHeader('Access-Control-Allow-Headers', access.headers);
|
|
74
|
+
next();
|
|
75
|
+
};
|
|
76
|
+
}
|
|
53
77
|
return (req: Request, res: Response, next: NextFunction) => {
|
|
54
|
-
res.header('Access-Control-Allow-Origin', access.origin);
|
|
55
78
|
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
56
79
|
res.header('Access-Control-Allow-Methods', access.methods);
|
|
57
80
|
res.setHeader('Access-Control-Allow-Headers', access.headers);
|