namirasoft-node 1.4.103 → 1.4.104
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/BaseApplication.d.ts +2 -1
- package/dist/BaseApplication.js +8 -7
- package/dist/BaseApplication.js.map +1 -1
- package/dist/BaseController.d.ts +2 -1
- package/dist/BaseController.js +1 -0
- package/dist/BaseController.js.map +1 -1
- package/package.json +43 -43
- package/src/AnomalyDetector.ts +84 -84
- package/src/BaseApplication.ts +443 -440
- package/src/BaseApplicationLink.ts +6 -6
- package/src/BaseController.ts +226 -225
- package/src/BaseCron.ts +104 -104
- package/src/BaseDatabase.ts +45 -45
- package/src/BaseEmailService.ts +38 -38
- package/src/BaseFilterItemBuilder.ts +191 -191
- package/src/BaseFilterItemBuilderDatabase.ts +45 -45
- package/src/BaseFilterItemBuilderObject.ts +95 -95
- 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
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
|
}
|
package/src/BaseDatabase.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import * as express from "express";
|
|
2
|
-
import { ErrorOperation, SortItem } from "namirasoft-core";
|
|
3
|
-
|
|
4
|
-
export abstract class BaseDatabase
|
|
5
|
-
{
|
|
6
|
-
private tables: { [name: string]: any } = {};
|
|
7
|
-
public req?: express.Request;
|
|
8
|
-
public res?: express.Response;
|
|
9
|
-
abstract init(): Promise<void>;
|
|
10
|
-
abstract connect(): Promise<void>;
|
|
11
|
-
abstract sync(force: boolean): Promise<void>;
|
|
12
|
-
async seedBefore(): Promise<void>
|
|
13
|
-
{
|
|
14
|
-
}
|
|
15
|
-
async seedAfter(): Promise<void>
|
|
16
|
-
{
|
|
17
|
-
}
|
|
18
|
-
addTable(name: string, table: any)
|
|
19
|
-
{
|
|
20
|
-
this.tables[name] = table;
|
|
21
|
-
}
|
|
22
|
-
getTable<T>(name: string): T
|
|
23
|
-
{
|
|
24
|
-
let ans = this.tables[name] as T;
|
|
25
|
-
if (!ans)
|
|
26
|
-
ErrorOperation.throwHTTP(404, `Table '${name}' not found`);
|
|
27
|
-
return ans;
|
|
28
|
-
}
|
|
29
|
-
paginate(page_number: number, page_size: number, page_size_default?: number): { offset: number, limit: number }
|
|
30
|
-
{
|
|
31
|
-
// page_number
|
|
32
|
-
if (isNaN(page_number))
|
|
33
|
-
page_number = 1;
|
|
34
|
-
// page_size
|
|
35
|
-
if (isNaN(page_size))
|
|
36
|
-
if (page_size_default)
|
|
37
|
-
page_size = page_size_default;
|
|
38
|
-
if (isNaN(page_size))
|
|
39
|
-
page_size = 25;
|
|
40
|
-
//
|
|
41
|
-
let offset = (page_number - 1) * page_size;
|
|
42
|
-
let limit = page_size;
|
|
43
|
-
return { offset, limit };
|
|
44
|
-
}
|
|
45
|
-
public abstract getSortOptions(sorts?: SortItem[] | undefined): any;
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { ErrorOperation, SortItem } from "namirasoft-core";
|
|
3
|
+
|
|
4
|
+
export abstract class BaseDatabase
|
|
5
|
+
{
|
|
6
|
+
private tables: { [name: string]: any } = {};
|
|
7
|
+
public req?: express.Request;
|
|
8
|
+
public res?: express.Response;
|
|
9
|
+
abstract init(): Promise<void>;
|
|
10
|
+
abstract connect(): Promise<void>;
|
|
11
|
+
abstract sync(force: boolean): Promise<void>;
|
|
12
|
+
async seedBefore(): Promise<void>
|
|
13
|
+
{
|
|
14
|
+
}
|
|
15
|
+
async seedAfter(): Promise<void>
|
|
16
|
+
{
|
|
17
|
+
}
|
|
18
|
+
addTable(name: string, table: any)
|
|
19
|
+
{
|
|
20
|
+
this.tables[name] = table;
|
|
21
|
+
}
|
|
22
|
+
getTable<T>(name: string): T
|
|
23
|
+
{
|
|
24
|
+
let ans = this.tables[name] as T;
|
|
25
|
+
if (!ans)
|
|
26
|
+
ErrorOperation.throwHTTP(404, `Table '${name}' not found`);
|
|
27
|
+
return ans;
|
|
28
|
+
}
|
|
29
|
+
paginate(page_number: number, page_size: number, page_size_default?: number): { offset: number, limit: number }
|
|
30
|
+
{
|
|
31
|
+
// page_number
|
|
32
|
+
if (isNaN(page_number))
|
|
33
|
+
page_number = 1;
|
|
34
|
+
// page_size
|
|
35
|
+
if (isNaN(page_size))
|
|
36
|
+
if (page_size_default)
|
|
37
|
+
page_size = page_size_default;
|
|
38
|
+
if (isNaN(page_size))
|
|
39
|
+
page_size = 25;
|
|
40
|
+
//
|
|
41
|
+
let offset = (page_number - 1) * page_size;
|
|
42
|
+
let limit = page_size;
|
|
43
|
+
return { offset, limit };
|
|
44
|
+
}
|
|
45
|
+
public abstract getSortOptions(sorts?: SortItem[] | undefined): any;
|
|
46
46
|
}
|
package/src/BaseEmailService.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import nodemailer from 'nodemailer';
|
|
2
|
-
import Mail, { AttachmentLike } from "nodemailer/lib/mailer";
|
|
3
|
-
import { Readable } from "stream";
|
|
4
|
-
|
|
5
|
-
export abstract class BaseEmailService
|
|
6
|
-
{
|
|
7
|
-
username: string;
|
|
8
|
-
constructor(username: string)
|
|
9
|
-
{
|
|
10
|
-
this.username = username;
|
|
11
|
-
}
|
|
12
|
-
protected abstract getTransform(): any;
|
|
13
|
-
send(from: string | null, to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void)
|
|
14
|
-
{
|
|
15
|
-
let transform = this.getTransform();
|
|
16
|
-
let transporter = nodemailer.createTransport(transform);
|
|
17
|
-
|
|
18
|
-
let mailOptions: Mail.Options = {
|
|
19
|
-
from: from ?? this.username,
|
|
20
|
-
to,
|
|
21
|
-
subject,
|
|
22
|
-
text,
|
|
23
|
-
html
|
|
24
|
-
};
|
|
25
|
-
if (html)
|
|
26
|
-
mailOptions.html = html;
|
|
27
|
-
|
|
28
|
-
transporter.sendMail(mailOptions, function (error, info)
|
|
29
|
-
{
|
|
30
|
-
if (callback)
|
|
31
|
-
callback(error, info);
|
|
32
|
-
else
|
|
33
|
-
{
|
|
34
|
-
if (error)
|
|
35
|
-
console.log(error);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
1
|
+
import nodemailer from 'nodemailer';
|
|
2
|
+
import Mail, { AttachmentLike } from "nodemailer/lib/mailer";
|
|
3
|
+
import { Readable } from "stream";
|
|
4
|
+
|
|
5
|
+
export abstract class BaseEmailService
|
|
6
|
+
{
|
|
7
|
+
username: string;
|
|
8
|
+
constructor(username: string)
|
|
9
|
+
{
|
|
10
|
+
this.username = username;
|
|
11
|
+
}
|
|
12
|
+
protected abstract getTransform(): any;
|
|
13
|
+
send(from: string | null, to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void)
|
|
14
|
+
{
|
|
15
|
+
let transform = this.getTransform();
|
|
16
|
+
let transporter = nodemailer.createTransport(transform);
|
|
17
|
+
|
|
18
|
+
let mailOptions: Mail.Options = {
|
|
19
|
+
from: from ?? this.username,
|
|
20
|
+
to,
|
|
21
|
+
subject,
|
|
22
|
+
text,
|
|
23
|
+
html
|
|
24
|
+
};
|
|
25
|
+
if (html)
|
|
26
|
+
mailOptions.html = html;
|
|
27
|
+
|
|
28
|
+
transporter.sendMail(mailOptions, function (error, info)
|
|
29
|
+
{
|
|
30
|
+
if (callback)
|
|
31
|
+
callback(error, info);
|
|
32
|
+
else
|
|
33
|
+
{
|
|
34
|
+
if (error)
|
|
35
|
+
console.log(error);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
39
|
}
|