namirasoft-node 1.4.131 → 1.4.133

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/src/Meta.ts CHANGED
@@ -1,37 +1,37 @@
1
- import * as express from 'express';
2
- import { IncomingHttpHeaders } from "http";
3
- import { IPOperation } from './IPOperation';
4
- import { HTTPMethod } from 'namirasoft-core';
5
- import { Timer } from './Timer';
6
-
7
- export class Meta
8
- {
9
- info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string };
10
- ip: string;
11
- method: string;
12
- url: string;
13
- headers?: IncomingHttpHeaders;
14
- body?: any;
15
- timer: Timer = new Timer();
16
- code: number = 200;
17
- message: string = "Success";
18
- error: Error | null = null;
19
- redirect_location: string | null = null;
20
- constructor(info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string }, req: express.Request)
21
- {
22
- this.info = info;
23
- this.ip = IPOperation.getIP(req);
24
- this.method = req.method;
25
- this.url = req.originalUrl;
26
- this.headers = req.headers;
27
- this.body = req.body;
28
- }
29
- onStart()
30
- {
31
- this.timer.onStart();
32
- }
33
- onFinish()
34
- {
35
- this.timer.onFinish();
36
- }
1
+ import * as express from 'express';
2
+ import { IncomingHttpHeaders } from "http";
3
+ import { IPOperation } from './IPOperation';
4
+ import { HTTPMethod } from 'namirasoft-core';
5
+ import { Timer } from './Timer';
6
+
7
+ export class Meta
8
+ {
9
+ info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string };
10
+ ip: string;
11
+ method: string;
12
+ url: string;
13
+ headers?: IncomingHttpHeaders;
14
+ body?: any;
15
+ timer: Timer = new Timer();
16
+ code: number = 200;
17
+ message: string = "Success";
18
+ error: Error | null = null;
19
+ redirect_location: string | null = null;
20
+ constructor(info: { name: string, tag: string, method: HTTPMethod, path: string, summary: string }, req: express.Request)
21
+ {
22
+ this.info = info;
23
+ this.ip = IPOperation.getIP(req);
24
+ this.method = req.method;
25
+ this.url = req.originalUrl;
26
+ this.headers = req.headers;
27
+ this.body = req.body;
28
+ }
29
+ onStart()
30
+ {
31
+ this.timer.onStart();
32
+ }
33
+ onFinish()
34
+ {
35
+ this.timer.onFinish();
36
+ }
37
37
  }
@@ -1,95 +1,95 @@
1
- import { ErrorOperation, TimeOperation } from "namirasoft-core";
2
-
3
- export class OTPOperation
4
- {
5
- public static OTP_MAX_WAIT_TIME: number = 1440; // minutes
6
- public static OTP_EXPIRE_TIME: number = 5; // minutes
7
- public static INCREASE_POWER_BASE: number = 2;
8
-
9
- public static OTP_REQ_MAX_TRY_COUNT: number = 5;
10
- public static OTP_VER_MAX_TRY_COUNT: number = 3;
11
-
12
- static generate(length: number = 6, digit: number = 3)
13
- {
14
- if (!length)
15
- length = 6;
16
- if (!digit)
17
- digit = 3;
18
- let dig = [];
19
- for (let i = 0; i < digit; i++)
20
- dig[i] = parseInt((Math.random() * 9 + 1) + "");
21
- let ans = '';
22
- for (let i = 0; i < length; i++)
23
- ans = ans + '' + dig[parseInt((Math.random() * dig.length) + "")];
24
- return ans;
25
- }
26
- static getWaitTime(x_otp_req_tried_count: number): number
27
- {
28
- let wait_time = 0;
29
- let extra_attempt = x_otp_req_tried_count - OTPOperation.OTP_REQ_MAX_TRY_COUNT;
30
- if (extra_attempt > 0)
31
- wait_time = Math.min(Math.pow(OTPOperation.INCREASE_POWER_BASE, extra_attempt), OTPOperation.OTP_MAX_WAIT_TIME) * 60;
32
- return parseInt(wait_time + "");
33
- }
34
- static async onSafeRequest<T>(x_otp: string | null, x_otp_time: Date | string | null, x_otp_req_tried_count: number,
35
- handler: (row: {
36
- x_otp: string,
37
- x_otp_time: Date,
38
- x_otp_req_tried_count: number,
39
- x_otp_ver_tried_count: number
40
- }) => Promise<T>): Promise<T>
41
- {
42
- // check wait time
43
- let wait_time = OTPOperation.getWaitTime(x_otp_req_tried_count);
44
- let wait_date = TimeOperation.secondsAgo(wait_time, new Date());
45
- if (x_otp_time != null)
46
- x_otp_time = new Date(x_otp_time);
47
- let next_time = TimeOperation.diffInSecond(x_otp_time ?? new Date("0001-01-01"), wait_date, false);
48
- if (next_time > 0)
49
- {
50
- let mins = Math.max(1, Math.round(next_time / 60));
51
- ErrorOperation.throwHTTP(429, 'You have sent too many request, please try again in ' + mins + ' minute' + (mins > 1 ? "s" : "") + '.')
52
- }
53
-
54
- let x_otp_ver_tried_count = 0;
55
- if (!x_otp)
56
- x_otp_req_tried_count = 0;
57
- x_otp = OTPOperation.generate(6, 3);
58
- x_otp_time = new Date();
59
- x_otp_req_tried_count += 1;
60
-
61
- return await handler({ x_otp, x_otp_time, x_otp_req_tried_count, x_otp_ver_tried_count });
62
- }
63
- static async onSafeVerify<T>(x_otp: string | null, x_otp_time: Date | string | null, x_otp_ver_tried_count: number, code: string,
64
- handler: (row: {
65
- x_otp: string | null,
66
- x_otp_time: Date | null,
67
- x_verified: boolean,
68
- }) => Promise<T>,
69
- errorHandler: () => Promise<void>): Promise<T>
70
- {
71
- if (!x_otp || !x_otp_time)
72
- throw ErrorOperation.getHTTP(410, "The OTP has not been generated yet. Please request first.");
73
-
74
- if (x_otp_time != null)
75
- x_otp_time = new Date(x_otp_time);
76
-
77
- if (x_otp_time < TimeOperation.minutesAgo(OTPOperation.OTP_EXPIRE_TIME, new Date()))
78
- ErrorOperation.throwHTTP(410, "The OTP code has been expired. Please request again.");
79
-
80
- if (x_otp_ver_tried_count > OTPOperation.OTP_VER_MAX_TRY_COUNT)
81
- ErrorOperation.throwHTTP(429, "The number of allowed attempts has been exceeded. Please try again later.");
82
-
83
- if (x_otp !== code)
84
- {
85
- await errorHandler();
86
- ErrorOperation.throwHTTP(401, "Wrong code.");
87
- }
88
-
89
- return await handler({
90
- x_otp: null,
91
- x_otp_time: null,
92
- x_verified: true,
93
- });
94
- }
1
+ import { ErrorOperation, TimeOperation } from "namirasoft-core";
2
+
3
+ export class OTPOperation
4
+ {
5
+ public static OTP_MAX_WAIT_TIME: number = 1440; // minutes
6
+ public static OTP_EXPIRE_TIME: number = 5; // minutes
7
+ public static INCREASE_POWER_BASE: number = 2;
8
+
9
+ public static OTP_REQ_MAX_TRY_COUNT: number = 5;
10
+ public static OTP_VER_MAX_TRY_COUNT: number = 3;
11
+
12
+ static generate(length: number = 6, digit: number = 3)
13
+ {
14
+ if (!length)
15
+ length = 6;
16
+ if (!digit)
17
+ digit = 3;
18
+ let dig = [];
19
+ for (let i = 0; i < digit; i++)
20
+ dig[i] = parseInt((Math.random() * 9 + 1) + "");
21
+ let ans = '';
22
+ for (let i = 0; i < length; i++)
23
+ ans = ans + '' + dig[parseInt((Math.random() * dig.length) + "")];
24
+ return ans;
25
+ }
26
+ static getWaitTime(x_otp_req_tried_count: number): number
27
+ {
28
+ let wait_time = 0;
29
+ let extra_attempt = x_otp_req_tried_count - OTPOperation.OTP_REQ_MAX_TRY_COUNT;
30
+ if (extra_attempt > 0)
31
+ wait_time = Math.min(Math.pow(OTPOperation.INCREASE_POWER_BASE, extra_attempt), OTPOperation.OTP_MAX_WAIT_TIME) * 60;
32
+ return parseInt(wait_time + "");
33
+ }
34
+ static async onSafeRequest<T>(x_otp: string | null, x_otp_time: Date | string | null, x_otp_req_tried_count: number,
35
+ handler: (row: {
36
+ x_otp: string,
37
+ x_otp_time: Date,
38
+ x_otp_req_tried_count: number,
39
+ x_otp_ver_tried_count: number
40
+ }) => Promise<T>): Promise<T>
41
+ {
42
+ // check wait time
43
+ let wait_time = OTPOperation.getWaitTime(x_otp_req_tried_count);
44
+ let wait_date = TimeOperation.secondsAgo(wait_time, new Date());
45
+ if (x_otp_time != null)
46
+ x_otp_time = new Date(x_otp_time);
47
+ let next_time = TimeOperation.diffInSecond(x_otp_time ?? new Date("0001-01-01"), wait_date, false);
48
+ if (next_time > 0)
49
+ {
50
+ let mins = Math.max(1, Math.round(next_time / 60));
51
+ ErrorOperation.throwHTTP(429, 'You have sent too many request, please try again in ' + mins + ' minute' + (mins > 1 ? "s" : "") + '.')
52
+ }
53
+
54
+ let x_otp_ver_tried_count = 0;
55
+ if (!x_otp)
56
+ x_otp_req_tried_count = 0;
57
+ x_otp = OTPOperation.generate(6, 3);
58
+ x_otp_time = new Date();
59
+ x_otp_req_tried_count += 1;
60
+
61
+ return await handler({ x_otp, x_otp_time, x_otp_req_tried_count, x_otp_ver_tried_count });
62
+ }
63
+ static async onSafeVerify<T>(x_otp: string | null, x_otp_time: Date | string | null, x_otp_ver_tried_count: number, code: string,
64
+ handler: (row: {
65
+ x_otp: string | null,
66
+ x_otp_time: Date | null,
67
+ x_verified: boolean,
68
+ }) => Promise<T>,
69
+ errorHandler: () => Promise<void>): Promise<T>
70
+ {
71
+ if (!x_otp || !x_otp_time)
72
+ throw ErrorOperation.getHTTP(410, "The OTP has not been generated yet. Please request first.");
73
+
74
+ if (x_otp_time != null)
75
+ x_otp_time = new Date(x_otp_time);
76
+
77
+ if (x_otp_time < TimeOperation.minutesAgo(OTPOperation.OTP_EXPIRE_TIME, new Date()))
78
+ ErrorOperation.throwHTTP(410, "The OTP code has been expired. Please request again.");
79
+
80
+ if (x_otp_ver_tried_count > OTPOperation.OTP_VER_MAX_TRY_COUNT)
81
+ ErrorOperation.throwHTTP(429, "The number of allowed attempts has been exceeded. Please try again later.");
82
+
83
+ if (x_otp !== code)
84
+ {
85
+ await errorHandler();
86
+ ErrorOperation.throwHTTP(401, "Wrong code.");
87
+ }
88
+
89
+ return await handler({
90
+ x_otp: null,
91
+ x_otp_time: null,
92
+ x_verified: true,
93
+ });
94
+ }
95
95
  }
@@ -1,28 +1,28 @@
1
- import * as express from 'express';
2
- import { ConvertService, ObjectService } from 'namirasoft-core';
3
-
4
- export class RequestHeaderService extends ConvertService
5
- {
6
- private req: express.Request;
7
- private name: string;
8
- constructor(req: express.Request, name: string, mandatory: boolean = false)
9
- {
10
- super(mandatory);
11
- this.req = req;
12
- this.name = name;
13
- }
14
- override getNullString()
15
- {
16
- if (this.req)
17
- if (this.req.headers)
18
- {
19
- let item = this.req.headers[this.name];
20
- return new ObjectService(item).getNullString();
21
- }
22
- return null;
23
- }
24
- protected override onMandatoryError(): void
25
- {
26
- throw new Error(`Request Header value was not provided: ${this.name}`);
27
- }
1
+ import * as express from 'express';
2
+ import { ConvertService, ObjectService } from 'namirasoft-core';
3
+
4
+ export class RequestHeaderService extends ConvertService
5
+ {
6
+ private req: express.Request;
7
+ private name: string;
8
+ constructor(req: express.Request, name: string, mandatory: boolean = false)
9
+ {
10
+ super(mandatory);
11
+ this.req = req;
12
+ this.name = name;
13
+ }
14
+ override getNullString()
15
+ {
16
+ if (this.req)
17
+ if (this.req.headers)
18
+ {
19
+ let item = this.req.headers[this.name];
20
+ return new ObjectService(item).getNullString();
21
+ }
22
+ return null;
23
+ }
24
+ protected override onMandatoryError(): void
25
+ {
26
+ throw new Error(`Request Header value was not provided: ${this.name}`);
27
+ }
28
28
  }
@@ -1,27 +1,27 @@
1
- import { BaseEmailService } from './BaseEmailService';
2
-
3
- export class SMTPService extends BaseEmailService
4
- {
5
- host: string;
6
- port: number;
7
- password: string;
8
- constructor(host: string, port: number, username: string, password: string)
9
- {
10
- super(username);
11
- this.host = host;
12
- this.port = port;
13
- this.password = password;
14
- }
15
- protected override getTransform(): any
16
- {
17
- return {
18
- host: this.host,
19
- port: this.port,
20
- secure: true,
21
- auth: {
22
- user: this.username,
23
- pass: this.password
24
- }
25
- };
26
- }
1
+ import { BaseEmailService } from './BaseEmailService';
2
+
3
+ export class SMTPService extends BaseEmailService
4
+ {
5
+ host: string;
6
+ port: number;
7
+ password: string;
8
+ constructor(host: string, port: number, username: string, password: string)
9
+ {
10
+ super(username);
11
+ this.host = host;
12
+ this.port = port;
13
+ this.password = password;
14
+ }
15
+ protected override getTransform(): any
16
+ {
17
+ return {
18
+ host: this.host,
19
+ port: this.port,
20
+ secure: true,
21
+ auth: {
22
+ user: this.username,
23
+ pass: this.password
24
+ }
25
+ };
26
+ }
27
27
  }
@@ -1,24 +1,24 @@
1
- import * as express from 'express';
2
- import { ErrorOperation, HashOperation } from "namirasoft-core";
3
- import { RequestHeaderService } from './RequestHeaderService';
4
-
5
- export class ServerToServerOperation
6
- {
7
- static isValid(sign_key: string, data: any, req: express.Request, sign_header: string): boolean
8
- {
9
- let signature = new RequestHeaderService(req, sign_header).getString();
10
- return HashOperation.isValidSHA256Secret(sign_key, data, signature);
11
- }
12
- static check(sign_key: string, data: any, req: express.Request, sign_header: string): void
13
- {
14
- if (!sign_key)
15
- ErrorOperation.throwHTTP(401, "Invlid signature - No sign key.");
16
- if (!sign_header)
17
- ErrorOperation.throwHTTP(401, "Invlid signature - No sign header name.");
18
- let signature = new RequestHeaderService(req, sign_header).getString();
19
- if (!signature)
20
- ErrorOperation.throwHTTP(401, "Invlid signature - No signature.");
21
- if (!this.isValid(sign_key, data, req, sign_header))
22
- ErrorOperation.throwHTTP(401, "Invlid signature.");
23
- }
1
+ import * as express from 'express';
2
+ import { ErrorOperation, HashOperation } from "namirasoft-core";
3
+ import { RequestHeaderService } from './RequestHeaderService';
4
+
5
+ export class ServerToServerOperation
6
+ {
7
+ static isValid(sign_key: string, data: any, req: express.Request, sign_header: string): boolean
8
+ {
9
+ let signature = new RequestHeaderService(req, sign_header).getString();
10
+ return HashOperation.isValidSHA256Secret(sign_key, data, signature);
11
+ }
12
+ static check(sign_key: string, data: any, req: express.Request, sign_header: string): void
13
+ {
14
+ if (!sign_key)
15
+ ErrorOperation.throwHTTP(401, "Invlid signature - No sign key.");
16
+ if (!sign_header)
17
+ ErrorOperation.throwHTTP(401, "Invlid signature - No sign header name.");
18
+ let signature = new RequestHeaderService(req, sign_header).getString();
19
+ if (!signature)
20
+ ErrorOperation.throwHTTP(401, "Invlid signature - No signature.");
21
+ if (!this.isValid(sign_key, data, req, sign_header))
22
+ ErrorOperation.throwHTTP(401, "Invlid signature.");
23
+ }
24
24
  }
package/src/Timer.ts CHANGED
@@ -1,18 +1,18 @@
1
- export class Timer
2
- {
3
- public start_time: Date | null = null;
4
- public end_time: Date | null = null;
5
- public duration: number | null = null;
6
- constructor()
7
- {
8
- }
9
- onStart()
10
- {
11
- this.start_time = new Date();
12
- }
13
- onFinish()
14
- {
15
- this.end_time = new Date();
16
- this.duration = (this.end_time?.getTime() ?? 0) - (this.start_time?.getTime() ?? 0);
17
- }
1
+ export class Timer
2
+ {
3
+ public start_time: Date | null = null;
4
+ public end_time: Date | null = null;
5
+ public duration: number | null = null;
6
+ constructor()
7
+ {
8
+ }
9
+ onStart()
10
+ {
11
+ this.start_time = new Date();
12
+ }
13
+ onFinish()
14
+ {
15
+ this.end_time = new Date();
16
+ this.duration = (this.end_time?.getTime() ?? 0) - (this.start_time?.getTime() ?? 0);
17
+ }
18
18
  }
package/src/Validator.ts CHANGED
@@ -1,16 +1,16 @@
1
- import { ErrorOperation } from "namirasoft-core";
2
-
3
- export class Validator
4
- {
5
- static checkDuplicated<T>(name: string, values: T[]): void
6
- {
7
- const seen = new Set<T>();
8
-
9
- for (const value of values)
10
- {
11
- if (seen.has(value))
12
- ErrorOperation.throwHTTP(400, `Duplicated value '${value}' was found in ${name}`);
13
- seen.add(value);
14
- }
15
- }
1
+ import { ErrorOperation } from "namirasoft-core";
2
+
3
+ export class Validator
4
+ {
5
+ static checkDuplicated<T>(name: string, values: T[]): void
6
+ {
7
+ const seen = new Set<T>();
8
+
9
+ for (const value of values)
10
+ {
11
+ if (seen.has(value))
12
+ ErrorOperation.throwHTTP(400, `Duplicated value '${value}' was found in ${name}`);
13
+ seen.add(value);
14
+ }
15
+ }
16
16
  }
package/src/index.ts CHANGED
@@ -1,24 +1,24 @@
1
- export * from "./AnomalyDetector";
2
- export * from "./BaseApplication";
3
- export * from "./BaseApplicationLink";
4
- export * from "./BaseController";
5
- export * from "./BaseCron";
6
- export * from "./BaseDatabase";
7
- export * from "./BaseEmailService";
8
- export * from "./BaseFilterItemBuilder";
9
- export * from "./BaseFilterItemBuilderDatabase";
10
- export * from "./BaseFilterItemBuilderObject";
11
- export * from "./BaseTable";
12
- export * from "./BaseTableColumnOptions";
13
- export * from "./CommandOperation";
14
- export * from "./EncryptionOperation";
15
- export * from "./GmailService";
16
- export * from "./IDatabase";
17
- export * from "./IPOperation";
18
- export * from "./Meta";
19
- export * from "./OTPOperation";
20
- export * from "./RequestHeaderService";
21
- export * from "./ServerToServerOperation";
22
- export * from "./SMTPService";
23
- export * from "./Timer";
1
+ export * from "./AnomalyDetector";
2
+ export * from "./BaseApplication";
3
+ export * from "./BaseApplicationLink";
4
+ export * from "./BaseController";
5
+ export * from "./BaseCron";
6
+ export * from "./BaseDatabase";
7
+ export * from "./BaseEmailService";
8
+ export * from "./BaseFilterItemBuilder";
9
+ export * from "./BaseFilterItemBuilderDatabase";
10
+ export * from "./BaseFilterItemBuilderObject";
11
+ export * from "./BaseTable";
12
+ export * from "./BaseTableColumnOptions";
13
+ export * from "./CommandOperation";
14
+ export * from "./EncryptionOperation";
15
+ export * from "./GmailService";
16
+ export * from "./IDatabase";
17
+ export * from "./IPOperation";
18
+ export * from "./Meta";
19
+ export * from "./OTPOperation";
20
+ export * from "./RequestHeaderService";
21
+ export * from "./ServerToServerOperation";
22
+ export * from "./SMTPService";
23
+ export * from "./Timer";
24
24
  export * from "./Validator";