namirasoft-core 1.4.12 → 1.4.13
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/EncryptionOperation.js +17 -7
- package/dist/EncryptionOperation.js.map +1 -1
- package/dist/HashOperation.js +17 -7
- package/dist/HashOperation.js.map +1 -1
- package/dist/PhoneOperation.js +17 -7
- package/dist/PhoneOperation.js.map +1 -1
- package/dist/SortItem.d.ts +24 -0
- package/dist/SortItem.js +75 -0
- package/dist/SortItem.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/BaseDatabaseRow.ts +6 -6
- package/src/BaseMetaColumn.ts +13 -13
- package/src/BaseMetaTable.ts +24 -24
- package/src/BaseServer.ts +107 -107
- package/src/CacheService.ts +57 -57
- package/src/ConsoleOperation.ts +68 -68
- package/src/ConvertService.ts +100 -100
- package/src/CookieService.ts +33 -33
- package/src/Countries.ts +486 -486
- package/src/Country.ts +21 -21
- package/src/CountryOperation.ts +98 -98
- package/src/EncodingOperation.ts +12 -12
- package/src/EncryptionOperation.ts +40 -40
- package/src/EnvService.ts +22 -22
- package/src/ErrorOperation.ts +13 -13
- package/src/FileOperation.ts +57 -57
- package/src/FilterItem.ts +128 -128
- package/src/FilterItemColumnType.ts +6 -6
- package/src/FilterItemOperator.ts +51 -51
- package/src/GeoOperation.ts +18 -18
- package/src/HTTPError.ts +8 -8
- package/src/HTTPMethod.ts +6 -6
- package/src/HashOperation.ts +24 -24
- package/src/IStorage.ts +5 -5
- package/src/IStorageCookie.ts +52 -52
- package/src/IStorageJsonFile.ts +45 -45
- package/src/IStorageLocal.ts +16 -16
- package/src/IStorageMemory.ts +17 -17
- package/src/NamingConvention.ts +107 -107
- package/src/ObjectService.ts +27 -27
- package/src/PackageService.ts +76 -76
- package/src/PhoneOperation.ts +8 -8
- package/src/PriceOperation.ts +18 -18
- package/src/RegexTemplate.ts +7 -7
- package/src/SearchOperation.ts +29 -29
- package/src/SortItem.ts +89 -0
- package/src/StringOperation.ts +18 -18
- package/src/TimeOperation.ts +262 -262
- package/src/URLOperation.ts +54 -54
- package/src/VersionOperation.ts +46 -46
- package/src/index.ts +40 -39
package/src/FilterItem.ts
CHANGED
|
@@ -1,129 +1,129 @@
|
|
|
1
|
-
import { BaseMetaColumn } from "./BaseMetaColumn";
|
|
2
|
-
import { BaseMetaTable } from "./BaseMetaTable";
|
|
3
|
-
import { EncodingOperation } from "./EncodingOperation";
|
|
4
|
-
import { ErrorOperation } from "./ErrorOperation";
|
|
5
|
-
import { FilterItemOperator } from "./FilterItemOperator";
|
|
6
|
-
|
|
7
|
-
export class FilterItem
|
|
8
|
-
{
|
|
9
|
-
public table: { name: string; text: string; };
|
|
10
|
-
public column: { name: string; text: string; };
|
|
11
|
-
public not: boolean;
|
|
12
|
-
public operator: FilterItemOperator;
|
|
13
|
-
public values: string[];
|
|
14
|
-
public static encode(filters: FilterItem[] | null): string
|
|
15
|
-
{
|
|
16
|
-
let value = this.stringify(filters);
|
|
17
|
-
return EncodingOperation.Base64Encode(value);
|
|
18
|
-
}
|
|
19
|
-
public static decode(value: string): FilterItem[] | null
|
|
20
|
-
{
|
|
21
|
-
let encoded = EncodingOperation.Base64Decode(value);
|
|
22
|
-
return this.parse(encoded);
|
|
23
|
-
}
|
|
24
|
-
public static stringify(filters: FilterItem[] | null): string
|
|
25
|
-
{
|
|
26
|
-
if (filters)
|
|
27
|
-
return filters.map(x => x.getCommand()).join(";");
|
|
28
|
-
return "";
|
|
29
|
-
}
|
|
30
|
-
public static parse(item: string | null): FilterItem[]
|
|
31
|
-
{
|
|
32
|
-
let ans: FilterItem[] = [];
|
|
33
|
-
if (item)
|
|
34
|
-
{
|
|
35
|
-
let index = 0;
|
|
36
|
-
let items = item.split(/(?<!\\);/);
|
|
37
|
-
let thereIsMore = (): boolean =>
|
|
38
|
-
{
|
|
39
|
-
return index < items.length;
|
|
40
|
-
};
|
|
41
|
-
let next = (name: string): string =>
|
|
42
|
-
{
|
|
43
|
-
if (thereIsMore())
|
|
44
|
-
return items[index++];
|
|
45
|
-
throw ErrorOperation.getHTTP(400, `Next value is required for '${name}'. Must be separated by ;`);
|
|
46
|
-
};
|
|
47
|
-
while (thereIsMore())
|
|
48
|
-
{
|
|
49
|
-
let value_table = next("Table");
|
|
50
|
-
let table = new BaseMetaTable(value_table, value_table);
|
|
51
|
-
|
|
52
|
-
let value_column = next("Column");
|
|
53
|
-
let column = new BaseMetaColumn(value_column, value_column, "", false)
|
|
54
|
-
|
|
55
|
-
let value_not = next("Not");
|
|
56
|
-
let not = value_not != "0"
|
|
57
|
-
if (value_not != "0" && value_not != "1")
|
|
58
|
-
ErrorOperation.throwHTTP(400, `Invalid value '${value_not}' for FilterItem.not, Only 0 and 1 are allowed.`);
|
|
59
|
-
|
|
60
|
-
let value_operator = next("Operator");
|
|
61
|
-
let operator = FilterItemOperator.getByName(value_operator);
|
|
62
|
-
|
|
63
|
-
let values: string[] = [];
|
|
64
|
-
for (let j = 0; j < operator.count; j++)
|
|
65
|
-
{
|
|
66
|
-
let value_value = next("Value Number " + (j + 1));
|
|
67
|
-
let value = value_value.replace(/\\;/gm, ";");
|
|
68
|
-
values.push(value);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
ans.push(new FilterItem(table, column, not, operator, ...values));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return ans;
|
|
75
|
-
}
|
|
76
|
-
public static test(filters: FilterItem[] | null, object: any): boolean
|
|
77
|
-
{
|
|
78
|
-
if (filters)
|
|
79
|
-
{
|
|
80
|
-
// todo
|
|
81
|
-
if (object)
|
|
82
|
-
return true;
|
|
83
|
-
return false;
|
|
84
|
-
// todo
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
constructor(table: { name: string; text: string; }, column: { name: string; text: string; }, not: boolean, operator: FilterItemOperator, ...values: string[])
|
|
89
|
-
{
|
|
90
|
-
this.table = table;
|
|
91
|
-
this.column = column;
|
|
92
|
-
this.not = not;
|
|
93
|
-
this.operator = operator;
|
|
94
|
-
this.values = values;
|
|
95
|
-
if (this.values.length != operator.count)
|
|
96
|
-
throw new Error(`Invalid count of values: ${this.values.length}.${operator.count} is required.`);
|
|
97
|
-
}
|
|
98
|
-
getCommand()
|
|
99
|
-
{
|
|
100
|
-
return [this.table.name, this.column.name, this.not ? "1" : "0", this.operator.name, ...this.values.map(x => (x ?? "").toString().replace(/;/gm, "\\;"))].join(";");
|
|
101
|
-
}
|
|
102
|
-
isAllColumns()
|
|
103
|
-
{
|
|
104
|
-
return this.column.name === "*";
|
|
105
|
-
}
|
|
106
|
-
toString()
|
|
107
|
-
{
|
|
108
|
-
let ans = [];
|
|
109
|
-
if (!this.isAllColumns())
|
|
110
|
-
{
|
|
111
|
-
let table;
|
|
112
|
-
let column;
|
|
113
|
-
if (this.table.text)
|
|
114
|
-
table = this.table.text;
|
|
115
|
-
else
|
|
116
|
-
table = this.table.name;
|
|
117
|
-
if (this.column.text)
|
|
118
|
-
column = this.column.text;
|
|
119
|
-
else
|
|
120
|
-
column = this.column.name;
|
|
121
|
-
ans.push([table, column].filter(x => x).join("."));
|
|
122
|
-
}
|
|
123
|
-
if (this.not)
|
|
124
|
-
ans.push("Not");
|
|
125
|
-
ans.push(this.operator.name);
|
|
126
|
-
ans.push(this.values.join(", "));
|
|
127
|
-
return ans.filter(x => x).join(" ");
|
|
128
|
-
}
|
|
1
|
+
import { BaseMetaColumn } from "./BaseMetaColumn";
|
|
2
|
+
import { BaseMetaTable } from "./BaseMetaTable";
|
|
3
|
+
import { EncodingOperation } from "./EncodingOperation";
|
|
4
|
+
import { ErrorOperation } from "./ErrorOperation";
|
|
5
|
+
import { FilterItemOperator } from "./FilterItemOperator";
|
|
6
|
+
|
|
7
|
+
export class FilterItem
|
|
8
|
+
{
|
|
9
|
+
public table: { name: string; text: string; };
|
|
10
|
+
public column: { name: string; text: string; };
|
|
11
|
+
public not: boolean;
|
|
12
|
+
public operator: FilterItemOperator;
|
|
13
|
+
public values: string[];
|
|
14
|
+
public static encode(filters: FilterItem[] | null): string
|
|
15
|
+
{
|
|
16
|
+
let value = this.stringify(filters);
|
|
17
|
+
return EncodingOperation.Base64Encode(value);
|
|
18
|
+
}
|
|
19
|
+
public static decode(value: string): FilterItem[] | null
|
|
20
|
+
{
|
|
21
|
+
let encoded = EncodingOperation.Base64Decode(value);
|
|
22
|
+
return this.parse(encoded);
|
|
23
|
+
}
|
|
24
|
+
public static stringify(filters: FilterItem[] | null): string
|
|
25
|
+
{
|
|
26
|
+
if (filters)
|
|
27
|
+
return filters.map(x => x.getCommand()).join(";");
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
public static parse(item: string | null): FilterItem[]
|
|
31
|
+
{
|
|
32
|
+
let ans: FilterItem[] = [];
|
|
33
|
+
if (item)
|
|
34
|
+
{
|
|
35
|
+
let index = 0;
|
|
36
|
+
let items = item.split(/(?<!\\);/);
|
|
37
|
+
let thereIsMore = (): boolean =>
|
|
38
|
+
{
|
|
39
|
+
return index < items.length;
|
|
40
|
+
};
|
|
41
|
+
let next = (name: string): string =>
|
|
42
|
+
{
|
|
43
|
+
if (thereIsMore())
|
|
44
|
+
return items[index++];
|
|
45
|
+
throw ErrorOperation.getHTTP(400, `Next value is required for '${name}'. Must be separated by ;`);
|
|
46
|
+
};
|
|
47
|
+
while (thereIsMore())
|
|
48
|
+
{
|
|
49
|
+
let value_table = next("Table");
|
|
50
|
+
let table = new BaseMetaTable(value_table, value_table);
|
|
51
|
+
|
|
52
|
+
let value_column = next("Column");
|
|
53
|
+
let column = new BaseMetaColumn(value_column, value_column, "", false)
|
|
54
|
+
|
|
55
|
+
let value_not = next("Not");
|
|
56
|
+
let not = value_not != "0"
|
|
57
|
+
if (value_not != "0" && value_not != "1")
|
|
58
|
+
ErrorOperation.throwHTTP(400, `Invalid value '${value_not}' for FilterItem.not, Only 0 and 1 are allowed.`);
|
|
59
|
+
|
|
60
|
+
let value_operator = next("Operator");
|
|
61
|
+
let operator = FilterItemOperator.getByName(value_operator);
|
|
62
|
+
|
|
63
|
+
let values: string[] = [];
|
|
64
|
+
for (let j = 0; j < operator.count; j++)
|
|
65
|
+
{
|
|
66
|
+
let value_value = next("Value Number " + (j + 1));
|
|
67
|
+
let value = value_value.replace(/\\;/gm, ";");
|
|
68
|
+
values.push(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ans.push(new FilterItem(table, column, not, operator, ...values));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return ans;
|
|
75
|
+
}
|
|
76
|
+
public static test(filters: FilterItem[] | null, object: any): boolean
|
|
77
|
+
{
|
|
78
|
+
if (filters)
|
|
79
|
+
{
|
|
80
|
+
// todo
|
|
81
|
+
if (object)
|
|
82
|
+
return true;
|
|
83
|
+
return false;
|
|
84
|
+
// todo
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
constructor(table: { name: string; text: string; }, column: { name: string; text: string; }, not: boolean, operator: FilterItemOperator, ...values: string[])
|
|
89
|
+
{
|
|
90
|
+
this.table = table;
|
|
91
|
+
this.column = column;
|
|
92
|
+
this.not = not;
|
|
93
|
+
this.operator = operator;
|
|
94
|
+
this.values = values;
|
|
95
|
+
if (this.values.length != operator.count)
|
|
96
|
+
throw new Error(`Invalid count of values: ${this.values.length}.${operator.count} is required.`);
|
|
97
|
+
}
|
|
98
|
+
getCommand()
|
|
99
|
+
{
|
|
100
|
+
return [this.table.name, this.column.name, this.not ? "1" : "0", this.operator.name, ...this.values.map(x => (x ?? "").toString().replace(/;/gm, "\\;"))].join(";");
|
|
101
|
+
}
|
|
102
|
+
isAllColumns()
|
|
103
|
+
{
|
|
104
|
+
return this.column.name === "*";
|
|
105
|
+
}
|
|
106
|
+
toString()
|
|
107
|
+
{
|
|
108
|
+
let ans = [];
|
|
109
|
+
if (!this.isAllColumns())
|
|
110
|
+
{
|
|
111
|
+
let table;
|
|
112
|
+
let column;
|
|
113
|
+
if (this.table.text)
|
|
114
|
+
table = this.table.text;
|
|
115
|
+
else
|
|
116
|
+
table = this.table.name;
|
|
117
|
+
if (this.column.text)
|
|
118
|
+
column = this.column.text;
|
|
119
|
+
else
|
|
120
|
+
column = this.column.name;
|
|
121
|
+
ans.push([table, column].filter(x => x).join("."));
|
|
122
|
+
}
|
|
123
|
+
if (this.not)
|
|
124
|
+
ans.push("Not");
|
|
125
|
+
ans.push(this.operator.name);
|
|
126
|
+
ans.push(this.values.join(", "));
|
|
127
|
+
return ans.filter(x => x).join(" ");
|
|
128
|
+
}
|
|
129
129
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export enum FilterItemColumnType
|
|
2
|
-
{
|
|
3
|
-
Any = "Any",
|
|
4
|
-
Boolean = "Boolean",
|
|
5
|
-
Number = "Number",
|
|
6
|
-
String = "String"
|
|
1
|
+
export enum FilterItemColumnType
|
|
2
|
+
{
|
|
3
|
+
Any = "Any",
|
|
4
|
+
Boolean = "Boolean",
|
|
5
|
+
Number = "Number",
|
|
6
|
+
String = "String"
|
|
7
7
|
};
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { ErrorOperation } from "./ErrorOperation";
|
|
2
|
-
import { FilterItemColumnType } from "./FilterItemColumnType";
|
|
3
|
-
|
|
4
|
-
export class FilterItemOperator
|
|
5
|
-
{
|
|
6
|
-
static all = {
|
|
7
|
-
equals: new FilterItemOperator("Equals", "=", 1, (_: FilterItemColumnType) => { return true; }),
|
|
8
|
-
contains: new FilterItemOperator("Contains", ":", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number || type == FilterItemColumnType.String; }),
|
|
9
|
-
regex: new FilterItemOperator("Regex", "*", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number || type == FilterItemColumnType.String; }),
|
|
10
|
-
empty: new FilterItemOperator("Empty", "?", 0, (type: FilterItemColumnType) => { return type != FilterItemColumnType.Any; }),
|
|
11
|
-
exists: new FilterItemOperator("Exists", "??", 0, (_: FilterItemColumnType, required: boolean) => { return !required; }),
|
|
12
|
-
startswith: new FilterItemOperator("Starts With", "(", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.String; }),
|
|
13
|
-
endswith: new FilterItemOperator("Ends With", ")", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.String; }),
|
|
14
|
-
lessthan: new FilterItemOperator("Less Than", "<", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; }),
|
|
15
|
-
lessthanequal: new FilterItemOperator("Less Than Equal", "<=", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; }),
|
|
16
|
-
morethan: new FilterItemOperator("More Than", ">", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; }),
|
|
17
|
-
morethanequal: new FilterItemOperator("More Than Equal", ">=", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; })
|
|
18
|
-
};
|
|
19
|
-
static getAll(): FilterItemOperator[]
|
|
20
|
-
{
|
|
21
|
-
return Object.keys(FilterItemOperator.all).map(name => (FilterItemOperator.all as any)[name]);
|
|
22
|
-
}
|
|
23
|
-
static getAllByType(type: FilterItemColumnType, required: boolean): FilterItemOperator[]
|
|
24
|
-
{
|
|
25
|
-
return FilterItemOperator.getAll().filter(f => f.accepts(type, required));
|
|
26
|
-
}
|
|
27
|
-
static getByName(name: string): FilterItemOperator
|
|
28
|
-
{
|
|
29
|
-
let found = (FilterItemOperator.all as any)[name.toLowerCase()];
|
|
30
|
-
if (!found)
|
|
31
|
-
ErrorOperation.throwHTTP(400, `Could not find operator by name: '${name}'. Valid values are ${this.getAll().map(x => x.name).join(",")}`);
|
|
32
|
-
return found;
|
|
33
|
-
}
|
|
34
|
-
static getBySign(sign: string): FilterItemOperator
|
|
35
|
-
{
|
|
36
|
-
let found = FilterItemOperator.getAll().filter(x => x.sign == sign);
|
|
37
|
-
if (found.length == 0)
|
|
38
|
-
ErrorOperation.throwHTTP(400, `Could not find operator by sign: '${sign}'. Valid values are ${this.getAll().map(x => x.sign).join(",")}`);
|
|
39
|
-
return found[0];
|
|
40
|
-
}
|
|
41
|
-
name: string;
|
|
42
|
-
sign: string;
|
|
43
|
-
count: number;
|
|
44
|
-
accepts: (type: FilterItemColumnType, required: boolean) => boolean;
|
|
45
|
-
constructor(name: string, sign: string, count: number, accepts: (type: FilterItemColumnType, required: boolean) => boolean)
|
|
46
|
-
{
|
|
47
|
-
this.name = name;
|
|
48
|
-
this.sign = sign;
|
|
49
|
-
this.count = count;
|
|
50
|
-
this.accepts = accepts;
|
|
51
|
-
}
|
|
1
|
+
import { ErrorOperation } from "./ErrorOperation";
|
|
2
|
+
import { FilterItemColumnType } from "./FilterItemColumnType";
|
|
3
|
+
|
|
4
|
+
export class FilterItemOperator
|
|
5
|
+
{
|
|
6
|
+
static all = {
|
|
7
|
+
equals: new FilterItemOperator("Equals", "=", 1, (_: FilterItemColumnType) => { return true; }),
|
|
8
|
+
contains: new FilterItemOperator("Contains", ":", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number || type == FilterItemColumnType.String; }),
|
|
9
|
+
regex: new FilterItemOperator("Regex", "*", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number || type == FilterItemColumnType.String; }),
|
|
10
|
+
empty: new FilterItemOperator("Empty", "?", 0, (type: FilterItemColumnType) => { return type != FilterItemColumnType.Any; }),
|
|
11
|
+
exists: new FilterItemOperator("Exists", "??", 0, (_: FilterItemColumnType, required: boolean) => { return !required; }),
|
|
12
|
+
startswith: new FilterItemOperator("Starts With", "(", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.String; }),
|
|
13
|
+
endswith: new FilterItemOperator("Ends With", ")", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.String; }),
|
|
14
|
+
lessthan: new FilterItemOperator("Less Than", "<", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; }),
|
|
15
|
+
lessthanequal: new FilterItemOperator("Less Than Equal", "<=", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; }),
|
|
16
|
+
morethan: new FilterItemOperator("More Than", ">", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; }),
|
|
17
|
+
morethanequal: new FilterItemOperator("More Than Equal", ">=", 1, (type: FilterItemColumnType) => { return type == FilterItemColumnType.Number; })
|
|
18
|
+
};
|
|
19
|
+
static getAll(): FilterItemOperator[]
|
|
20
|
+
{
|
|
21
|
+
return Object.keys(FilterItemOperator.all).map(name => (FilterItemOperator.all as any)[name]);
|
|
22
|
+
}
|
|
23
|
+
static getAllByType(type: FilterItemColumnType, required: boolean): FilterItemOperator[]
|
|
24
|
+
{
|
|
25
|
+
return FilterItemOperator.getAll().filter(f => f.accepts(type, required));
|
|
26
|
+
}
|
|
27
|
+
static getByName(name: string): FilterItemOperator
|
|
28
|
+
{
|
|
29
|
+
let found = (FilterItemOperator.all as any)[name.toLowerCase()];
|
|
30
|
+
if (!found)
|
|
31
|
+
ErrorOperation.throwHTTP(400, `Could not find operator by name: '${name}'. Valid values are ${this.getAll().map(x => x.name).join(",")}`);
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
static getBySign(sign: string): FilterItemOperator
|
|
35
|
+
{
|
|
36
|
+
let found = FilterItemOperator.getAll().filter(x => x.sign == sign);
|
|
37
|
+
if (found.length == 0)
|
|
38
|
+
ErrorOperation.throwHTTP(400, `Could not find operator by sign: '${sign}'. Valid values are ${this.getAll().map(x => x.sign).join(",")}`);
|
|
39
|
+
return found[0];
|
|
40
|
+
}
|
|
41
|
+
name: string;
|
|
42
|
+
sign: string;
|
|
43
|
+
count: number;
|
|
44
|
+
accepts: (type: FilterItemColumnType, required: boolean) => boolean;
|
|
45
|
+
constructor(name: string, sign: string, count: number, accepts: (type: FilterItemColumnType, required: boolean) => boolean)
|
|
46
|
+
{
|
|
47
|
+
this.name = name;
|
|
48
|
+
this.sign = sign;
|
|
49
|
+
this.count = count;
|
|
50
|
+
this.accepts = accepts;
|
|
51
|
+
}
|
|
52
52
|
}
|
package/src/GeoOperation.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export class GeoOperation
|
|
2
|
-
{
|
|
3
|
-
static distance(lat1: number, lng1: number, lat2: number, lng2: number)
|
|
4
|
-
{
|
|
5
|
-
function degreeToRadian(degree: number)
|
|
6
|
-
{
|
|
7
|
-
return degree * Math.PI / 180;
|
|
8
|
-
}
|
|
9
|
-
let R = 6371; // km
|
|
10
|
-
let dLat = degreeToRadian(lat2 - lat1);
|
|
11
|
-
let dLng = degreeToRadian(lng2 - lng1);
|
|
12
|
-
lat1 = degreeToRadian(lat1);
|
|
13
|
-
lat2 = degreeToRadian(lat2);
|
|
14
|
-
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
15
|
-
Math.sin(dLng / 2) * Math.sin(dLng / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
16
|
-
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
17
|
-
return R * c;
|
|
18
|
-
}
|
|
1
|
+
export class GeoOperation
|
|
2
|
+
{
|
|
3
|
+
static distance(lat1: number, lng1: number, lat2: number, lng2: number)
|
|
4
|
+
{
|
|
5
|
+
function degreeToRadian(degree: number)
|
|
6
|
+
{
|
|
7
|
+
return degree * Math.PI / 180;
|
|
8
|
+
}
|
|
9
|
+
let R = 6371; // km
|
|
10
|
+
let dLat = degreeToRadian(lat2 - lat1);
|
|
11
|
+
let dLng = degreeToRadian(lng2 - lng1);
|
|
12
|
+
lat1 = degreeToRadian(lat1);
|
|
13
|
+
lat2 = degreeToRadian(lat2);
|
|
14
|
+
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
15
|
+
Math.sin(dLng / 2) * Math.sin(dLng / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
16
|
+
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
17
|
+
return R * c;
|
|
18
|
+
}
|
|
19
19
|
}
|
package/src/HTTPError.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export class HTTPError extends Error
|
|
2
|
-
{
|
|
3
|
-
code: number;
|
|
4
|
-
constructor(code: number, message: string)
|
|
5
|
-
{
|
|
6
|
-
super(message);
|
|
7
|
-
this.code = code;
|
|
8
|
-
}
|
|
1
|
+
export class HTTPError extends Error
|
|
2
|
+
{
|
|
3
|
+
code: number;
|
|
4
|
+
constructor(code: number, message: string)
|
|
5
|
+
{
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
9
|
}
|
package/src/HTTPMethod.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export enum HTTPMethod
|
|
2
|
-
{
|
|
3
|
-
GET = "GET",
|
|
4
|
-
POST = "POST",
|
|
5
|
-
PUT = "PUT",
|
|
6
|
-
DELETE = "DELETE"
|
|
1
|
+
export enum HTTPMethod
|
|
2
|
+
{
|
|
3
|
+
GET = "GET",
|
|
4
|
+
POST = "POST",
|
|
5
|
+
PUT = "PUT",
|
|
6
|
+
DELETE = "DELETE"
|
|
7
7
|
}
|
package/src/HashOperation.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import * as crypto from 'node:crypto';
|
|
2
|
-
|
|
3
|
-
export class HashOperation
|
|
4
|
-
{
|
|
5
|
-
static SHA256<T>(message: T): string
|
|
6
|
-
{
|
|
7
|
-
const hash = crypto.createHash('sha256');
|
|
8
|
-
hash.update(JSON.stringify(message));
|
|
9
|
-
return hash.digest('hex');
|
|
10
|
-
}
|
|
11
|
-
static SHA256Secret<T>(secret: string, message: T): string
|
|
12
|
-
{
|
|
13
|
-
const hash = crypto.createHmac('sha256', secret);
|
|
14
|
-
hash.update(JSON.stringify(message));
|
|
15
|
-
return hash.digest('hex');
|
|
16
|
-
}
|
|
17
|
-
static isValidSHA256(values: any, signature: string)
|
|
18
|
-
{
|
|
19
|
-
return this.SHA256(values).toLowerCase() === signature.toLowerCase();
|
|
20
|
-
}
|
|
21
|
-
static isValidSHA256Secret(secret: string, values: any, signature: string)
|
|
22
|
-
{
|
|
23
|
-
return this.SHA256Secret(secret, values).toLowerCase() === signature.toLowerCase();
|
|
24
|
-
}
|
|
1
|
+
import * as crypto from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export class HashOperation
|
|
4
|
+
{
|
|
5
|
+
static SHA256<T>(message: T): string
|
|
6
|
+
{
|
|
7
|
+
const hash = crypto.createHash('sha256');
|
|
8
|
+
hash.update(JSON.stringify(message));
|
|
9
|
+
return hash.digest('hex');
|
|
10
|
+
}
|
|
11
|
+
static SHA256Secret<T>(secret: string, message: T): string
|
|
12
|
+
{
|
|
13
|
+
const hash = crypto.createHmac('sha256', secret);
|
|
14
|
+
hash.update(JSON.stringify(message));
|
|
15
|
+
return hash.digest('hex');
|
|
16
|
+
}
|
|
17
|
+
static isValidSHA256(values: any, signature: string)
|
|
18
|
+
{
|
|
19
|
+
return this.SHA256(values).toLowerCase() === signature.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
static isValidSHA256Secret(secret: string, values: any, signature: string)
|
|
22
|
+
{
|
|
23
|
+
return this.SHA256Secret(secret, values).toLowerCase() === signature.toLowerCase();
|
|
24
|
+
}
|
|
25
25
|
}
|
package/src/IStorage.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export abstract class IStorage
|
|
2
|
-
{
|
|
3
|
-
abstract get(name: string, defaultValue: string): string;
|
|
4
|
-
abstract set(name: string, value: string): void;
|
|
5
|
-
abstract del(name: string): void;
|
|
1
|
+
export abstract class IStorage
|
|
2
|
+
{
|
|
3
|
+
abstract get(name: string, defaultValue: string): string;
|
|
4
|
+
abstract set(name: string, value: string): void;
|
|
5
|
+
abstract del(name: string): void;
|
|
6
6
|
}
|
package/src/IStorageCookie.ts
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import { EncodingOperation } from "./EncodingOperation";
|
|
2
|
-
import { IStorage } from "./IStorage";
|
|
3
|
-
|
|
4
|
-
export class IStorageCookie extends IStorage
|
|
5
|
-
{
|
|
6
|
-
private fixed: { [name: string]: string };
|
|
7
|
-
private cookies?: string;
|
|
8
|
-
constructor(fixed: { [name: string]: string }, cookies?: string)
|
|
9
|
-
{
|
|
10
|
-
super();
|
|
11
|
-
this.fixed = fixed;
|
|
12
|
-
this.cookies = cookies;
|
|
13
|
-
}
|
|
14
|
-
private getAll(): { [name: string]: string }
|
|
15
|
-
{
|
|
16
|
-
let ans: { [name: string]: string } = {};
|
|
17
|
-
let items = (this.cookies ?? document.cookie).split(";");
|
|
18
|
-
for (let i = 0; i < items.length; i++)
|
|
19
|
-
{
|
|
20
|
-
const ops = items[i].split("=");
|
|
21
|
-
ans[ops[0].trim()] = ops[1]?.trim().replace(/\%3D/gm, "=");
|
|
22
|
-
}
|
|
23
|
-
return ans;
|
|
24
|
-
}
|
|
25
|
-
override get(name: string, defaultValue: string)
|
|
26
|
-
{
|
|
27
|
-
try
|
|
28
|
-
{
|
|
29
|
-
let all = this.getAll();
|
|
30
|
-
return EncodingOperation.Base64Decode(all[name]) ?? defaultValue;
|
|
31
|
-
} catch (error)
|
|
32
|
-
{
|
|
33
|
-
return defaultValue;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
override set(name: string, value: string)
|
|
37
|
-
{
|
|
38
|
-
try
|
|
39
|
-
{
|
|
40
|
-
let items = [];
|
|
41
|
-
for (let key of Object.keys(this.fixed))
|
|
42
|
-
items.push(`${key}=${this.fixed[key]}`);
|
|
43
|
-
items.push(`${name}=${EncodingOperation.Base64Encode(value)}`);
|
|
44
|
-
document.cookie = items.join(";");
|
|
45
|
-
} catch (error)
|
|
46
|
-
{
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
override del(name: string)
|
|
50
|
-
{
|
|
51
|
-
this.set(name, "");
|
|
52
|
-
}
|
|
1
|
+
import { EncodingOperation } from "./EncodingOperation";
|
|
2
|
+
import { IStorage } from "./IStorage";
|
|
3
|
+
|
|
4
|
+
export class IStorageCookie extends IStorage
|
|
5
|
+
{
|
|
6
|
+
private fixed: { [name: string]: string };
|
|
7
|
+
private cookies?: string;
|
|
8
|
+
constructor(fixed: { [name: string]: string }, cookies?: string)
|
|
9
|
+
{
|
|
10
|
+
super();
|
|
11
|
+
this.fixed = fixed;
|
|
12
|
+
this.cookies = cookies;
|
|
13
|
+
}
|
|
14
|
+
private getAll(): { [name: string]: string }
|
|
15
|
+
{
|
|
16
|
+
let ans: { [name: string]: string } = {};
|
|
17
|
+
let items = (this.cookies ?? document.cookie).split(";");
|
|
18
|
+
for (let i = 0; i < items.length; i++)
|
|
19
|
+
{
|
|
20
|
+
const ops = items[i].split("=");
|
|
21
|
+
ans[ops[0].trim()] = ops[1]?.trim().replace(/\%3D/gm, "=");
|
|
22
|
+
}
|
|
23
|
+
return ans;
|
|
24
|
+
}
|
|
25
|
+
override get(name: string, defaultValue: string)
|
|
26
|
+
{
|
|
27
|
+
try
|
|
28
|
+
{
|
|
29
|
+
let all = this.getAll();
|
|
30
|
+
return EncodingOperation.Base64Decode(all[name]) ?? defaultValue;
|
|
31
|
+
} catch (error)
|
|
32
|
+
{
|
|
33
|
+
return defaultValue;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
override set(name: string, value: string)
|
|
37
|
+
{
|
|
38
|
+
try
|
|
39
|
+
{
|
|
40
|
+
let items = [];
|
|
41
|
+
for (let key of Object.keys(this.fixed))
|
|
42
|
+
items.push(`${key}=${this.fixed[key]}`);
|
|
43
|
+
items.push(`${name}=${EncodingOperation.Base64Encode(value)}`);
|
|
44
|
+
document.cookie = items.join(";");
|
|
45
|
+
} catch (error)
|
|
46
|
+
{
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
override del(name: string)
|
|
50
|
+
{
|
|
51
|
+
this.set(name, "");
|
|
52
|
+
}
|
|
53
53
|
}
|