namirasoft-node 1.4.101 → 1.4.103
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/BaseFilterItemBuilder.d.ts +1 -0
- package/dist/BaseFilterItemBuilder.js +9 -8
- package/dist/BaseFilterItemBuilder.js.map +1 -1
- package/dist/BaseFilterItemBuilderObject.d.ts +1 -0
- package/dist/BaseFilterItemBuilderObject.js +3 -0
- package/dist/BaseFilterItemBuilderObject.js.map +1 -1
- package/dist/OTPOperation.js +3 -3
- package/dist/OTPOperation.js.map +1 -1
- package/package.json +43 -43
- package/src/AnomalyDetector.ts +84 -84
- package/src/BaseApplication.ts +440 -440
- package/src/BaseApplicationLink.ts +6 -6
- package/src/BaseController.ts +225 -225
- package/src/BaseCron.ts +104 -104
- package/src/BaseDatabase.ts +45 -45
- package/src/BaseEmailService.ts +38 -38
- package/src/BaseFilterItemBuilder.ts +191 -189
- package/src/BaseFilterItemBuilderDatabase.ts +45 -45
- package/src/BaseFilterItemBuilderObject.ts +95 -91
- package/src/BaseTable.ts +137 -137
- package/src/BaseTableColumnOptions.ts +8 -8
- package/src/CommandOperation.ts +32 -32
- package/src/GmailService.ts +22 -22
- package/src/IPOperation.ts +38 -38
- package/src/Meta.ts +36 -36
- package/src/OTPOperation.ts +94 -94
- package/src/RequestHeaderService.ts +27 -27
- package/src/SMTPService.ts +26 -26
- package/src/ServerToServerOperation.ts +23 -23
- package/src/Timer.ts +17 -17
- package/src/Validator.ts +15 -15
- package/src/index.ts +21 -21
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export type BaseApplicationLink =
|
|
2
|
-
{
|
|
3
|
-
name: string;
|
|
4
|
-
url: string;
|
|
5
|
-
logo: string;
|
|
6
|
-
description: string;
|
|
1
|
+
export type BaseApplicationLink =
|
|
2
|
+
{
|
|
3
|
+
name: string;
|
|
4
|
+
url: string;
|
|
5
|
+
logo: string;
|
|
6
|
+
description: string;
|
|
7
7
|
};
|
package/src/BaseController.ts
CHANGED
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
import * as express from 'express';
|
|
2
|
-
import { BaseDatabase } from './BaseDatabase';
|
|
3
|
-
import { AnomalyDetector } from './AnomalyDetector';
|
|
4
|
-
import { Meta } from './Meta';
|
|
5
|
-
import { EnvService, ErrorOperation, HTTPError, HTTPMethod, ObjectService } from 'namirasoft-core';
|
|
6
|
-
import { BaseApplication } from './BaseApplication';
|
|
7
|
-
import { BaseTypeSchema, BaseVariableSchema, ControllerSchema, JoiValidator } from 'namirasoft-schema';
|
|
8
|
-
|
|
9
|
-
export abstract class BaseController<D extends { [name: string]: BaseDatabase }, State, Props, Output>
|
|
10
|
-
{
|
|
11
|
-
protected log: {
|
|
12
|
-
show: boolean;
|
|
13
|
-
can: (() => Promise<boolean>)[];
|
|
14
|
-
onBefore: (() => Promise<void>)[];
|
|
15
|
-
} = {
|
|
16
|
-
show: true,
|
|
17
|
-
can: [],
|
|
18
|
-
onBefore: []
|
|
19
|
-
};
|
|
20
|
-
public app: BaseApplication<D>;
|
|
21
|
-
public req: express.Request;
|
|
22
|
-
public res: express.Response;
|
|
23
|
-
public meta!: Meta;
|
|
24
|
-
protected output!: Output | null;
|
|
25
|
-
protected result!: any;
|
|
26
|
-
protected state!: State;
|
|
27
|
-
protected props!: Props;
|
|
28
|
-
protected validate_query: boolean = true;
|
|
29
|
-
protected bodyAs: {
|
|
30
|
-
json: { enabled: boolean, limit: string },
|
|
31
|
-
raw: { enabled: boolean, type: string }
|
|
32
|
-
} = {
|
|
33
|
-
json: {
|
|
34
|
-
enabled: true,
|
|
35
|
-
limit: "100kb"
|
|
36
|
-
},
|
|
37
|
-
raw: {
|
|
38
|
-
enabled: false,
|
|
39
|
-
type: "application/json"
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
public generate_sdk: boolean = true;
|
|
43
|
-
public generate_swagger: boolean = true;
|
|
44
|
-
constructor(app: BaseApplication<D>, req: express.Request, res: express.Response)
|
|
45
|
-
{
|
|
46
|
-
this.app = app;
|
|
47
|
-
this.req = req;
|
|
48
|
-
this.res = res;
|
|
49
|
-
}
|
|
50
|
-
protected getAnomaly(): AnomalyDetector | null
|
|
51
|
-
{
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
abstract getInfo(): { name: string, tag: string, method: HTTPMethod, path: string, summary: string };
|
|
55
|
-
abstract getHeadersSchema(): Promise<BaseVariableSchema[]>;
|
|
56
|
-
abstract getParametersSchema(): Promise<BaseVariableSchema[]>;
|
|
57
|
-
abstract getBodySchema(): Promise<BaseTypeSchema | null>;
|
|
58
|
-
abstract getQueriesSchema(): Promise<BaseVariableSchema[]>;
|
|
59
|
-
async getQueriesSchemaSimple(): Promise<BaseVariableSchema[] | null>
|
|
60
|
-
{
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
abstract getOutputSchema(): Promise<BaseTypeSchema | null>;
|
|
64
|
-
abstract getState(): Promise<State>;
|
|
65
|
-
abstract getProps(): Promise<Props>;
|
|
66
|
-
abstract preHandle(): Promise<void>;
|
|
67
|
-
abstract handle(): Promise<Output>;
|
|
68
|
-
abstract postHandle(): Promise<void>;
|
|
69
|
-
|
|
70
|
-
async run()
|
|
71
|
-
{
|
|
72
|
-
let info = this.getInfo();
|
|
73
|
-
this.meta = new Meta(info, this.req);
|
|
74
|
-
// output
|
|
75
|
-
this.output = null;
|
|
76
|
-
this.result = null;
|
|
77
|
-
|
|
78
|
-
let showLog = async (log: () => void) =>
|
|
79
|
-
{
|
|
80
|
-
if (this.log.show)
|
|
81
|
-
if (!this.log.can.some((x) => !x()))
|
|
82
|
-
{
|
|
83
|
-
try
|
|
84
|
-
{
|
|
85
|
-
for (let i = 0; i < this.log.onBefore.length; i++)
|
|
86
|
-
await this.log.onBefore[i]();
|
|
87
|
-
} catch (error)
|
|
88
|
-
{
|
|
89
|
-
this.app.logger.onCatchFatal(error);
|
|
90
|
-
}
|
|
91
|
-
log();
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
let failed = false;
|
|
96
|
-
|
|
97
|
-
try
|
|
98
|
-
{
|
|
99
|
-
// meta
|
|
100
|
-
this.meta.onStart();
|
|
101
|
-
|
|
102
|
-
let accpet: string = new ObjectService(this.req.headers.accept).getString("");
|
|
103
|
-
let isJson = accpet.includes("application/json") || accpet.includes("*/*");
|
|
104
|
-
let isSchema = accpet.includes("application/schema");
|
|
105
|
-
if (isJson)
|
|
106
|
-
{
|
|
107
|
-
this.state = await this.getState();
|
|
108
|
-
this.props = await this.getProps();
|
|
109
|
-
|
|
110
|
-
// preHandle
|
|
111
|
-
await this.preHandle();
|
|
112
|
-
|
|
113
|
-
// check for anomaly
|
|
114
|
-
let anomaly: AnomalyDetector | null = this.getAnomaly();
|
|
115
|
-
if (anomaly != null)
|
|
116
|
-
if (anomaly.isAnomaly(this.meta.ip, this.meta.url))
|
|
117
|
-
ErrorOperation.throwHTTP(403, 'Suspicious activity detected.');
|
|
118
|
-
|
|
119
|
-
// check
|
|
120
|
-
JoiValidator.check(await this.getBodySchema(), this.req.body, "Body");
|
|
121
|
-
if (this.validate_query)
|
|
122
|
-
{
|
|
123
|
-
let query = this.req.query;
|
|
124
|
-
let schemas = await this.getQueriesSchemaSimple();
|
|
125
|
-
if (schemas == null)
|
|
126
|
-
schemas = await this.getQueriesSchema();
|
|
127
|
-
else
|
|
128
|
-
{
|
|
129
|
-
for (let key of Object.keys(query))
|
|
130
|
-
{
|
|
131
|
-
let element = query[key];
|
|
132
|
-
if (Array.isArray(element))
|
|
133
|
-
query[key] = element.join(",");
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
JoiValidator.checkArray(schemas, query, "Query String");
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// call controller
|
|
140
|
-
this.output = await this.handle();
|
|
141
|
-
|
|
142
|
-
// postHandle
|
|
143
|
-
await this.postHandle();
|
|
144
|
-
}
|
|
145
|
-
else if (isSchema)
|
|
146
|
-
this.result = await this.getSchema();
|
|
147
|
-
else
|
|
148
|
-
ErrorOperation.throwHTTP(400, "Invalid accept: " + accpet);
|
|
149
|
-
} catch (error)
|
|
150
|
-
{
|
|
151
|
-
failed = true;
|
|
152
|
-
let message: string;
|
|
153
|
-
if (error instanceof Error)
|
|
154
|
-
{
|
|
155
|
-
this.meta.error = error;
|
|
156
|
-
message = error.message;
|
|
157
|
-
}
|
|
158
|
-
else
|
|
159
|
-
message = error + "";
|
|
160
|
-
|
|
161
|
-
if (error instanceof HTTPError)
|
|
162
|
-
{
|
|
163
|
-
this.meta.code = error.code;
|
|
164
|
-
await showLog(() =>
|
|
165
|
-
{
|
|
166
|
-
this.app.logger.error(error.message, error.stack, JSON.stringify(this.meta, undefined, 4));
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
else
|
|
170
|
-
{
|
|
171
|
-
this.meta.code = 500;
|
|
172
|
-
if (error instanceof Error)
|
|
173
|
-
{
|
|
174
|
-
await showLog(() =>
|
|
175
|
-
{
|
|
176
|
-
this.app.logger.critical(error.message, error.stack, JSON.stringify(this.meta, undefined, 4));
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
this.meta.message = message;
|
|
181
|
-
if (error instanceof HTTPError)
|
|
182
|
-
this.result = this.meta.message;
|
|
183
|
-
else
|
|
184
|
-
this.result = "Sorry, internl server error.";
|
|
185
|
-
}
|
|
186
|
-
// result
|
|
187
|
-
if (!this.result)
|
|
188
|
-
{
|
|
189
|
-
if (this.output == null)
|
|
190
|
-
this.result = "Success";
|
|
191
|
-
else
|
|
192
|
-
this.result = this.output;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// finish
|
|
196
|
-
this.meta.onFinish();
|
|
197
|
-
|
|
198
|
-
if (!failed)
|
|
199
|
-
await showLog(() =>
|
|
200
|
-
{
|
|
201
|
-
this.app.logger.info(JSON.stringify(this.meta, undefined, 4));
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
if (this.meta.redirect_location)
|
|
205
|
-
return this.res.redirect(this.meta.code, this.meta.redirect_location);
|
|
206
|
-
return this.res.status(this.meta.code).send(this.result);
|
|
207
|
-
}
|
|
208
|
-
async getSchema(): Promise<ControllerSchema>
|
|
209
|
-
{
|
|
210
|
-
let info = this.getInfo();
|
|
211
|
-
let schema = new ControllerSchema(info.name, info.tag, info.method, info.path, info.summary);
|
|
212
|
-
schema.headers = await this.getHeadersSchema();
|
|
213
|
-
schema.parameters = await this.getParametersSchema();
|
|
214
|
-
schema.body = await this.getBodySchema();
|
|
215
|
-
schema.queries = await this.getQueriesSchema();
|
|
216
|
-
schema.output = await this.getOutputSchema();
|
|
217
|
-
schema.check();
|
|
218
|
-
return schema;
|
|
219
|
-
}
|
|
220
|
-
protected _setCookie(name: string, value: any, expires: Date | undefined = undefined)
|
|
221
|
-
{
|
|
222
|
-
let DOMAIN = new EnvService("DOMAIN").getString();
|
|
223
|
-
let domain = "." + DOMAIN;
|
|
224
|
-
this.res.cookie(name, value, { domain, path: "/", sameSite: "none", secure: true, expires });
|
|
225
|
-
}
|
|
1
|
+
import * as express from 'express';
|
|
2
|
+
import { BaseDatabase } from './BaseDatabase';
|
|
3
|
+
import { AnomalyDetector } from './AnomalyDetector';
|
|
4
|
+
import { Meta } from './Meta';
|
|
5
|
+
import { EnvService, ErrorOperation, HTTPError, HTTPMethod, ObjectService } from 'namirasoft-core';
|
|
6
|
+
import { BaseApplication } from './BaseApplication';
|
|
7
|
+
import { BaseTypeSchema, BaseVariableSchema, ControllerSchema, JoiValidator } from 'namirasoft-schema';
|
|
8
|
+
|
|
9
|
+
export abstract class BaseController<D extends { [name: string]: BaseDatabase }, State, Props, Output>
|
|
10
|
+
{
|
|
11
|
+
protected log: {
|
|
12
|
+
show: boolean;
|
|
13
|
+
can: (() => Promise<boolean>)[];
|
|
14
|
+
onBefore: (() => Promise<void>)[];
|
|
15
|
+
} = {
|
|
16
|
+
show: true,
|
|
17
|
+
can: [],
|
|
18
|
+
onBefore: []
|
|
19
|
+
};
|
|
20
|
+
public app: BaseApplication<D>;
|
|
21
|
+
public req: express.Request;
|
|
22
|
+
public res: express.Response;
|
|
23
|
+
public meta!: Meta;
|
|
24
|
+
protected output!: Output | null;
|
|
25
|
+
protected result!: any;
|
|
26
|
+
protected state!: State;
|
|
27
|
+
protected props!: Props;
|
|
28
|
+
protected validate_query: boolean = true;
|
|
29
|
+
protected bodyAs: {
|
|
30
|
+
json: { enabled: boolean, limit: string },
|
|
31
|
+
raw: { enabled: boolean, type: string }
|
|
32
|
+
} = {
|
|
33
|
+
json: {
|
|
34
|
+
enabled: true,
|
|
35
|
+
limit: "100kb"
|
|
36
|
+
},
|
|
37
|
+
raw: {
|
|
38
|
+
enabled: false,
|
|
39
|
+
type: "application/json"
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
public generate_sdk: boolean = true;
|
|
43
|
+
public generate_swagger: boolean = true;
|
|
44
|
+
constructor(app: BaseApplication<D>, req: express.Request, res: express.Response)
|
|
45
|
+
{
|
|
46
|
+
this.app = app;
|
|
47
|
+
this.req = req;
|
|
48
|
+
this.res = res;
|
|
49
|
+
}
|
|
50
|
+
protected getAnomaly(): AnomalyDetector | null
|
|
51
|
+
{
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
abstract getInfo(): { name: string, tag: string, method: HTTPMethod, path: string, summary: string };
|
|
55
|
+
abstract getHeadersSchema(): Promise<BaseVariableSchema[]>;
|
|
56
|
+
abstract getParametersSchema(): Promise<BaseVariableSchema[]>;
|
|
57
|
+
abstract getBodySchema(): Promise<BaseTypeSchema | null>;
|
|
58
|
+
abstract getQueriesSchema(): Promise<BaseVariableSchema[]>;
|
|
59
|
+
async getQueriesSchemaSimple(): Promise<BaseVariableSchema[] | null>
|
|
60
|
+
{
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
abstract getOutputSchema(): Promise<BaseTypeSchema | null>;
|
|
64
|
+
abstract getState(): Promise<State>;
|
|
65
|
+
abstract getProps(): Promise<Props>;
|
|
66
|
+
abstract preHandle(): Promise<void>;
|
|
67
|
+
abstract handle(): Promise<Output>;
|
|
68
|
+
abstract postHandle(): Promise<void>;
|
|
69
|
+
|
|
70
|
+
async run()
|
|
71
|
+
{
|
|
72
|
+
let info = this.getInfo();
|
|
73
|
+
this.meta = new Meta(info, this.req);
|
|
74
|
+
// output
|
|
75
|
+
this.output = null;
|
|
76
|
+
this.result = null;
|
|
77
|
+
|
|
78
|
+
let showLog = async (log: () => void) =>
|
|
79
|
+
{
|
|
80
|
+
if (this.log.show)
|
|
81
|
+
if (!this.log.can.some((x) => !x()))
|
|
82
|
+
{
|
|
83
|
+
try
|
|
84
|
+
{
|
|
85
|
+
for (let i = 0; i < this.log.onBefore.length; i++)
|
|
86
|
+
await this.log.onBefore[i]();
|
|
87
|
+
} catch (error)
|
|
88
|
+
{
|
|
89
|
+
this.app.logger.onCatchFatal(error);
|
|
90
|
+
}
|
|
91
|
+
log();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let failed = false;
|
|
96
|
+
|
|
97
|
+
try
|
|
98
|
+
{
|
|
99
|
+
// meta
|
|
100
|
+
this.meta.onStart();
|
|
101
|
+
|
|
102
|
+
let accpet: string = new ObjectService(this.req.headers.accept).getString("");
|
|
103
|
+
let isJson = accpet.includes("application/json") || accpet.includes("*/*");
|
|
104
|
+
let isSchema = accpet.includes("application/schema");
|
|
105
|
+
if (isJson)
|
|
106
|
+
{
|
|
107
|
+
this.state = await this.getState();
|
|
108
|
+
this.props = await this.getProps();
|
|
109
|
+
|
|
110
|
+
// preHandle
|
|
111
|
+
await this.preHandle();
|
|
112
|
+
|
|
113
|
+
// check for anomaly
|
|
114
|
+
let anomaly: AnomalyDetector | null = this.getAnomaly();
|
|
115
|
+
if (anomaly != null)
|
|
116
|
+
if (anomaly.isAnomaly(this.meta.ip, this.meta.url))
|
|
117
|
+
ErrorOperation.throwHTTP(403, 'Suspicious activity detected.');
|
|
118
|
+
|
|
119
|
+
// check
|
|
120
|
+
JoiValidator.check(await this.getBodySchema(), this.req.body, "Body");
|
|
121
|
+
if (this.validate_query)
|
|
122
|
+
{
|
|
123
|
+
let query = this.req.query;
|
|
124
|
+
let schemas = await this.getQueriesSchemaSimple();
|
|
125
|
+
if (schemas == null)
|
|
126
|
+
schemas = await this.getQueriesSchema();
|
|
127
|
+
else
|
|
128
|
+
{
|
|
129
|
+
for (let key of Object.keys(query))
|
|
130
|
+
{
|
|
131
|
+
let element = query[key];
|
|
132
|
+
if (Array.isArray(element))
|
|
133
|
+
query[key] = element.join(",");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
JoiValidator.checkArray(schemas, query, "Query String");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// call controller
|
|
140
|
+
this.output = await this.handle();
|
|
141
|
+
|
|
142
|
+
// postHandle
|
|
143
|
+
await this.postHandle();
|
|
144
|
+
}
|
|
145
|
+
else if (isSchema)
|
|
146
|
+
this.result = await this.getSchema();
|
|
147
|
+
else
|
|
148
|
+
ErrorOperation.throwHTTP(400, "Invalid accept: " + accpet);
|
|
149
|
+
} catch (error)
|
|
150
|
+
{
|
|
151
|
+
failed = true;
|
|
152
|
+
let message: string;
|
|
153
|
+
if (error instanceof Error)
|
|
154
|
+
{
|
|
155
|
+
this.meta.error = error;
|
|
156
|
+
message = error.message;
|
|
157
|
+
}
|
|
158
|
+
else
|
|
159
|
+
message = error + "";
|
|
160
|
+
|
|
161
|
+
if (error instanceof HTTPError)
|
|
162
|
+
{
|
|
163
|
+
this.meta.code = error.code;
|
|
164
|
+
await showLog(() =>
|
|
165
|
+
{
|
|
166
|
+
this.app.logger.error(error.message, error.stack, JSON.stringify(this.meta, undefined, 4));
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
{
|
|
171
|
+
this.meta.code = 500;
|
|
172
|
+
if (error instanceof Error)
|
|
173
|
+
{
|
|
174
|
+
await showLog(() =>
|
|
175
|
+
{
|
|
176
|
+
this.app.logger.critical(error.message, error.stack, JSON.stringify(this.meta, undefined, 4));
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
this.meta.message = message;
|
|
181
|
+
if (error instanceof HTTPError)
|
|
182
|
+
this.result = this.meta.message;
|
|
183
|
+
else
|
|
184
|
+
this.result = "Sorry, internl server error.";
|
|
185
|
+
}
|
|
186
|
+
// result
|
|
187
|
+
if (!this.result)
|
|
188
|
+
{
|
|
189
|
+
if (this.output == null)
|
|
190
|
+
this.result = "Success";
|
|
191
|
+
else
|
|
192
|
+
this.result = this.output;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// finish
|
|
196
|
+
this.meta.onFinish();
|
|
197
|
+
|
|
198
|
+
if (!failed)
|
|
199
|
+
await showLog(() =>
|
|
200
|
+
{
|
|
201
|
+
this.app.logger.info(JSON.stringify(this.meta, undefined, 4));
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
if (this.meta.redirect_location)
|
|
205
|
+
return this.res.redirect(this.meta.code, this.meta.redirect_location);
|
|
206
|
+
return this.res.status(this.meta.code).send(this.result);
|
|
207
|
+
}
|
|
208
|
+
async getSchema(): Promise<ControllerSchema>
|
|
209
|
+
{
|
|
210
|
+
let info = this.getInfo();
|
|
211
|
+
let schema = new ControllerSchema(info.name, info.tag, info.method, info.path, info.summary);
|
|
212
|
+
schema.headers = await this.getHeadersSchema();
|
|
213
|
+
schema.parameters = await this.getParametersSchema();
|
|
214
|
+
schema.body = await this.getBodySchema();
|
|
215
|
+
schema.queries = await this.getQueriesSchema();
|
|
216
|
+
schema.output = await this.getOutputSchema();
|
|
217
|
+
schema.check();
|
|
218
|
+
return schema;
|
|
219
|
+
}
|
|
220
|
+
protected _setCookie(name: string, value: any, expires: Date | undefined = undefined)
|
|
221
|
+
{
|
|
222
|
+
let DOMAIN = new EnvService("DOMAIN").getString();
|
|
223
|
+
let domain = "." + DOMAIN;
|
|
224
|
+
this.res.cookie(name, value, { domain, path: "/", sameSite: "none", secure: true, expires });
|
|
225
|
+
}
|
|
226
226
|
}
|
package/src/BaseCron.ts
CHANGED
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
import { BaseDatabase } from './BaseDatabase';
|
|
2
|
-
import cron from 'node-cron';
|
|
3
|
-
import { BaseApplication } from './BaseApplication';
|
|
4
|
-
import { Timer } from './Timer';
|
|
5
|
-
|
|
6
|
-
export type BaseCronInfoType =
|
|
7
|
-
{
|
|
8
|
-
name: string,
|
|
9
|
-
expression?: string,
|
|
10
|
-
interval?: number,
|
|
11
|
-
runOnStart: boolean,
|
|
12
|
-
ignoreDatabase?: boolean
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export abstract class BaseCron<D extends { [name: string]: BaseDatabase }>
|
|
16
|
-
{
|
|
17
|
-
protected log: {
|
|
18
|
-
enabled: boolean;
|
|
19
|
-
onStarted: boolean;
|
|
20
|
-
onFinished: boolean;
|
|
21
|
-
} = {
|
|
22
|
-
enabled: true,
|
|
23
|
-
onStarted: true,
|
|
24
|
-
onFinished: true
|
|
25
|
-
};
|
|
26
|
-
protected app: BaseApplication<D>;
|
|
27
|
-
protected info!: BaseCronInfoType;
|
|
28
|
-
protected timer: Timer = new Timer();
|
|
29
|
-
|
|
30
|
-
constructor(app: BaseApplication<D>)
|
|
31
|
-
{
|
|
32
|
-
this.app = app;
|
|
33
|
-
this.run = this.run.bind(this);
|
|
34
|
-
this.loop = this.loop.bind(this);
|
|
35
|
-
this.handler = this.handler.bind(this);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
abstract getInfo(): BaseCronInfoType;
|
|
39
|
-
|
|
40
|
-
run()
|
|
41
|
-
{
|
|
42
|
-
this.info = this.getInfo();
|
|
43
|
-
|
|
44
|
-
if (this.info.expression && this.info.interval)
|
|
45
|
-
throw new Error("Both experssion and interval should not be provided in getInfo function");
|
|
46
|
-
else if (this.info.expression)
|
|
47
|
-
this.app.logger.info(`Cron ${this.info.name} was schedued for ${this.info.expression}.`);
|
|
48
|
-
else if (this.info.interval)
|
|
49
|
-
this.app.logger.info(`Cron ${this.info.name} was schedued for every ${this.info.interval}ms.`);
|
|
50
|
-
else
|
|
51
|
-
throw new Error("Either experssion or interval should be provided in getInfo function");
|
|
52
|
-
|
|
53
|
-
if (this.info.runOnStart)
|
|
54
|
-
this.handler();
|
|
55
|
-
if (this.info.expression)
|
|
56
|
-
cron.schedule(this.info.expression, this.handler);
|
|
57
|
-
else if (this.info.interval)
|
|
58
|
-
this.loop();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private async loop()
|
|
62
|
-
{
|
|
63
|
-
while (true)
|
|
64
|
-
{
|
|
65
|
-
await this.handler()
|
|
66
|
-
await new Promise(resolver =>
|
|
67
|
-
{
|
|
68
|
-
setTimeout(() =>
|
|
69
|
-
{
|
|
70
|
-
resolver({});
|
|
71
|
-
}, this.info.interval);
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
private async handler()
|
|
77
|
-
{
|
|
78
|
-
if (this.log.enabled)
|
|
79
|
-
if (this.log.onStarted)
|
|
80
|
-
this.app.logger.info(`Cron ${this.info.name} was started.`);
|
|
81
|
-
try
|
|
82
|
-
{
|
|
83
|
-
// preHandle
|
|
84
|
-
this.timer.onStart();
|
|
85
|
-
await this.preHandle();
|
|
86
|
-
// Handler
|
|
87
|
-
await this.handleOne();
|
|
88
|
-
// postHandle
|
|
89
|
-
await this.postHandle();
|
|
90
|
-
this.timer.onFinish();
|
|
91
|
-
|
|
92
|
-
if (this.log.enabled)
|
|
93
|
-
if (this.log.onFinished)
|
|
94
|
-
this.app.logger.info(`Cron ${this.info.name} was finished in ${this.timer.duration}ms.`);
|
|
95
|
-
}
|
|
96
|
-
catch (error: any)
|
|
97
|
-
{
|
|
98
|
-
this.app.logger.onCatchCritical(error);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
async preHandle(): Promise<void> { }
|
|
103
|
-
abstract handleOne(): Promise<void>;
|
|
104
|
-
async postHandle(): Promise<void> { }
|
|
1
|
+
import { BaseDatabase } from './BaseDatabase';
|
|
2
|
+
import cron from 'node-cron';
|
|
3
|
+
import { BaseApplication } from './BaseApplication';
|
|
4
|
+
import { Timer } from './Timer';
|
|
5
|
+
|
|
6
|
+
export type BaseCronInfoType =
|
|
7
|
+
{
|
|
8
|
+
name: string,
|
|
9
|
+
expression?: string,
|
|
10
|
+
interval?: number,
|
|
11
|
+
runOnStart: boolean,
|
|
12
|
+
ignoreDatabase?: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export abstract class BaseCron<D extends { [name: string]: BaseDatabase }>
|
|
16
|
+
{
|
|
17
|
+
protected log: {
|
|
18
|
+
enabled: boolean;
|
|
19
|
+
onStarted: boolean;
|
|
20
|
+
onFinished: boolean;
|
|
21
|
+
} = {
|
|
22
|
+
enabled: true,
|
|
23
|
+
onStarted: true,
|
|
24
|
+
onFinished: true
|
|
25
|
+
};
|
|
26
|
+
protected app: BaseApplication<D>;
|
|
27
|
+
protected info!: BaseCronInfoType;
|
|
28
|
+
protected timer: Timer = new Timer();
|
|
29
|
+
|
|
30
|
+
constructor(app: BaseApplication<D>)
|
|
31
|
+
{
|
|
32
|
+
this.app = app;
|
|
33
|
+
this.run = this.run.bind(this);
|
|
34
|
+
this.loop = this.loop.bind(this);
|
|
35
|
+
this.handler = this.handler.bind(this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
abstract getInfo(): BaseCronInfoType;
|
|
39
|
+
|
|
40
|
+
run()
|
|
41
|
+
{
|
|
42
|
+
this.info = this.getInfo();
|
|
43
|
+
|
|
44
|
+
if (this.info.expression && this.info.interval)
|
|
45
|
+
throw new Error("Both experssion and interval should not be provided in getInfo function");
|
|
46
|
+
else if (this.info.expression)
|
|
47
|
+
this.app.logger.info(`Cron ${this.info.name} was schedued for ${this.info.expression}.`);
|
|
48
|
+
else if (this.info.interval)
|
|
49
|
+
this.app.logger.info(`Cron ${this.info.name} was schedued for every ${this.info.interval}ms.`);
|
|
50
|
+
else
|
|
51
|
+
throw new Error("Either experssion or interval should be provided in getInfo function");
|
|
52
|
+
|
|
53
|
+
if (this.info.runOnStart)
|
|
54
|
+
this.handler();
|
|
55
|
+
if (this.info.expression)
|
|
56
|
+
cron.schedule(this.info.expression, this.handler);
|
|
57
|
+
else if (this.info.interval)
|
|
58
|
+
this.loop();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private async loop()
|
|
62
|
+
{
|
|
63
|
+
while (true)
|
|
64
|
+
{
|
|
65
|
+
await this.handler()
|
|
66
|
+
await new Promise(resolver =>
|
|
67
|
+
{
|
|
68
|
+
setTimeout(() =>
|
|
69
|
+
{
|
|
70
|
+
resolver({});
|
|
71
|
+
}, this.info.interval);
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async handler()
|
|
77
|
+
{
|
|
78
|
+
if (this.log.enabled)
|
|
79
|
+
if (this.log.onStarted)
|
|
80
|
+
this.app.logger.info(`Cron ${this.info.name} was started.`);
|
|
81
|
+
try
|
|
82
|
+
{
|
|
83
|
+
// preHandle
|
|
84
|
+
this.timer.onStart();
|
|
85
|
+
await this.preHandle();
|
|
86
|
+
// Handler
|
|
87
|
+
await this.handleOne();
|
|
88
|
+
// postHandle
|
|
89
|
+
await this.postHandle();
|
|
90
|
+
this.timer.onFinish();
|
|
91
|
+
|
|
92
|
+
if (this.log.enabled)
|
|
93
|
+
if (this.log.onFinished)
|
|
94
|
+
this.app.logger.info(`Cron ${this.info.name} was finished in ${this.timer.duration}ms.`);
|
|
95
|
+
}
|
|
96
|
+
catch (error: any)
|
|
97
|
+
{
|
|
98
|
+
this.app.logger.onCatchCritical(error);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
async preHandle(): Promise<void> { }
|
|
103
|
+
abstract handleOne(): Promise<void>;
|
|
104
|
+
async postHandle(): Promise<void> { }
|
|
105
105
|
}
|