@adaas/a-utils 0.0.2
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/.nvmrc +1 -0
- package/LICENSE +22 -0
- package/README.md +49 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/src/constants/errors.constants.d.ts +65 -0
- package/dist/src/constants/errors.constants.js +74 -0
- package/dist/src/constants/errors.constants.js.map +1 -0
- package/dist/src/global/ASEID.class.d.ts +77 -0
- package/dist/src/global/ASEID.class.js +129 -0
- package/dist/src/global/ASEID.class.js.map +1 -0
- package/dist/src/global/A_Deferred.class.d.ts +8 -0
- package/dist/src/global/A_Deferred.class.js +19 -0
- package/dist/src/global/A_Deferred.class.js.map +1 -0
- package/dist/src/global/A_Entity.class.d.ts +49 -0
- package/dist/src/global/A_Entity.class.js +94 -0
- package/dist/src/global/A_Entity.class.js.map +1 -0
- package/dist/src/global/A_Error.class.d.ts +13 -0
- package/dist/src/global/A_Error.class.js +54 -0
- package/dist/src/global/A_Error.class.js.map +1 -0
- package/dist/src/global/A_Polyfills.d.ts +21 -0
- package/dist/src/global/A_Polyfills.js +123 -0
- package/dist/src/global/A_Polyfills.js.map +1 -0
- package/dist/src/global/A_ScheduleObject.class.d.ts +9 -0
- package/dist/src/global/A_ScheduleObject.class.js +38 -0
- package/dist/src/global/A_ScheduleObject.class.js.map +1 -0
- package/dist/src/global/A_ServerError.class.d.ts +13 -0
- package/dist/src/global/A_ServerError.class.js +57 -0
- package/dist/src/global/A_ServerError.class.js.map +1 -0
- package/dist/src/helpers/A_Common.helper.d.ts +32 -0
- package/dist/src/helpers/A_Common.helper.js +184 -0
- package/dist/src/helpers/A_Common.helper.js.map +1 -0
- package/dist/src/helpers/A_Schedule.helper.d.ts +6 -0
- package/dist/src/helpers/A_Schedule.helper.js +21 -0
- package/dist/src/helpers/A_Schedule.helper.js.map +1 -0
- package/dist/src/types/ASEID.types.d.ts +65 -0
- package/dist/src/types/ASEID.types.js +5 -0
- package/dist/src/types/ASEID.types.js.map +1 -0
- package/dist/src/types/A_Common.types.d.ts +59 -0
- package/dist/src/types/A_Common.types.js +3 -0
- package/dist/src/types/A_Common.types.js.map +1 -0
- package/dist/src/types/A_Entity.types.d.ts +13 -0
- package/dist/src/types/A_Entity.types.js +4 -0
- package/dist/src/types/A_Entity.types.js.map +1 -0
- package/dist/src/types/A_Error.type.d.ts +7 -0
- package/dist/src/types/A_Error.type.js +3 -0
- package/dist/src/types/A_Error.type.js.map +1 -0
- package/dist/src/types/A_Error.types.d.ts +7 -0
- package/dist/src/types/A_Error.types.js +3 -0
- package/dist/src/types/A_Error.types.js.map +1 -0
- package/dist/src/types/A_ScheduleObject.types.d.ts +9 -0
- package/dist/src/types/A_ScheduleObject.types.js +3 -0
- package/dist/src/types/A_ScheduleObject.types.js.map +1 -0
- package/dist/src/types/A_ServerError.types.d.ts +4 -0
- package/dist/src/types/A_ServerError.types.js +3 -0
- package/dist/src/types/A_ServerError.types.js.map +1 -0
- package/docs/a-logo-docs.png +0 -0
- package/index.ts +64 -0
- package/jest.config.ts +19 -0
- package/package.json +73 -0
- package/src/constants/errors.constants.ts +78 -0
- package/src/global/ASEID.class.ts +208 -0
- package/src/global/A_Deferred.class.ts +20 -0
- package/src/global/A_Entity.class.ts +134 -0
- package/src/global/A_Error.class.ts +75 -0
- package/src/global/A_Polyfills.ts +112 -0
- package/src/global/A_ScheduleObject.class.ts +53 -0
- package/src/global/A_ServerError.class.ts +70 -0
- package/src/helpers/A_Common.helper.ts +229 -0
- package/src/helpers/A_Schedule.helper.ts +25 -0
- package/src/types/ASEID.types.ts +86 -0
- package/src/types/A_Common.types.ts +111 -0
- package/src/types/A_Entity.types.ts +17 -0
- package/src/types/A_Error.types.ts +10 -0
- package/src/types/A_ScheduleObject.types.ts +9 -0
- package/src/types/A_ServerError.types.ts +7 -0
- package/tests/default.test.ts +160 -0
- package/tests/polyfill.test.ts +37 -0
- package/tsconfig.json +58 -0
- package/tslint.json +98 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { A_CONSTANTS__DEFAULT_ERRORS } from "../constants/errors.constants";
|
|
2
|
+
import { A_CommonHelper } from "../helpers/A_Common.helper";
|
|
3
|
+
import { A_TYPES__ASEID_Constructor, A_TYPES__ASEID_JSON } from "../types/ASEID.types";
|
|
4
|
+
import { A_Error } from "./A_Error.class";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A - ADAAS
|
|
9
|
+
* S - System
|
|
10
|
+
* E - Entity
|
|
11
|
+
* I - Identifier
|
|
12
|
+
* D - iDentifier
|
|
13
|
+
*
|
|
14
|
+
*
|
|
15
|
+
* adaas-sso@scope:usr:0000000001
|
|
16
|
+
*
|
|
17
|
+
* APP_NAMESPACE + @ + SCOPE + : ENTITY_NAME + : + ID + @ + VERSION
|
|
18
|
+
*/
|
|
19
|
+
export class ASEID {
|
|
20
|
+
|
|
21
|
+
static regexp: RegExp = new RegExp(`^[a-z|A-Z|0-9]+@[a-z|A-Z|0-9|-]+:[a-z|A-Z]+:[a-z|A-Z|0-9|-]+(@v[0-9]+|@lts)?$`)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Namespace for the ASEID
|
|
26
|
+
* Generally it is the application name or code, should correspond to the namespace of the application
|
|
27
|
+
* Could be ID or ASEID
|
|
28
|
+
*/
|
|
29
|
+
private _namespace!: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Entity Scope the primary location of the resource
|
|
33
|
+
* Organization, or organization Unit
|
|
34
|
+
* Could be ID or ASEID
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
private _scope!: string
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Entity Type the type of the resource
|
|
41
|
+
*/
|
|
42
|
+
private _entity!: string
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Entity ID the unique identifier of the resource
|
|
46
|
+
*/
|
|
47
|
+
private _id!: string
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Version of the entity (optional)
|
|
52
|
+
*/
|
|
53
|
+
private _version?: string
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Shard of the entity (optional)
|
|
57
|
+
*/
|
|
58
|
+
private _shard?: string
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
constructor(
|
|
62
|
+
aseid: string
|
|
63
|
+
)
|
|
64
|
+
constructor(
|
|
65
|
+
props: A_TYPES__ASEID_Constructor
|
|
66
|
+
)
|
|
67
|
+
constructor(param1: string | A_TYPES__ASEID_Constructor) {
|
|
68
|
+
|
|
69
|
+
if (typeof param1 === 'string' && !ASEID.isASEID(param1)) {
|
|
70
|
+
throw new A_Error(A_CONSTANTS__DEFAULT_ERRORS.INVALID_ASEID)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const aseidString = typeof param1 === 'string' ? param1 : ASEID.generateASEID(param1);
|
|
74
|
+
|
|
75
|
+
const { namespace, scope, entity, id, version, shard } = ASEID.parseASEID(aseidString);
|
|
76
|
+
|
|
77
|
+
this._namespace = namespace;
|
|
78
|
+
this._scope = scope;
|
|
79
|
+
this._entity = entity;
|
|
80
|
+
this._id = id;
|
|
81
|
+
this._version = version;
|
|
82
|
+
this._shard = shard;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
get namespace(): string {
|
|
87
|
+
return this._namespace;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get scope(): string {
|
|
91
|
+
return this._scope;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
get entity(): string {
|
|
95
|
+
return this._entity;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get id(): string {
|
|
99
|
+
return this._id;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get version(): string | undefined {
|
|
103
|
+
return this._version;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get shard(): string | undefined {
|
|
107
|
+
return this._shard;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Tests if the identity string is an ASEID
|
|
113
|
+
*
|
|
114
|
+
* @param identity
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
static isASEID(identity: string): boolean {
|
|
118
|
+
return this.regexp.test(
|
|
119
|
+
identity
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Generate an ASEID from a namespace, entity, and id
|
|
125
|
+
*
|
|
126
|
+
* @param props
|
|
127
|
+
* @returns
|
|
128
|
+
*/
|
|
129
|
+
static generateASEID(
|
|
130
|
+
props: A_TYPES__ASEID_Constructor
|
|
131
|
+
): string {
|
|
132
|
+
|
|
133
|
+
const namespace = props.namespace
|
|
134
|
+
? this.isASEID(props.namespace)
|
|
135
|
+
? this.parseASEID(props.namespace).id
|
|
136
|
+
: props.namespace
|
|
137
|
+
: process.env.A_NAMESPACE;
|
|
138
|
+
|
|
139
|
+
const scope = typeof props.scope === 'number'
|
|
140
|
+
? A_CommonHelper.formatWithLeadingZeros(props.scope) :
|
|
141
|
+
this.isASEID(props.scope)
|
|
142
|
+
? this.parseASEID(props.scope).id
|
|
143
|
+
: props.scope;
|
|
144
|
+
|
|
145
|
+
const entity = props.entity;
|
|
146
|
+
|
|
147
|
+
const id = typeof props.id === 'number'
|
|
148
|
+
? A_CommonHelper.formatWithLeadingZeros(props.id)
|
|
149
|
+
: props.id;
|
|
150
|
+
|
|
151
|
+
const version = props.version;
|
|
152
|
+
|
|
153
|
+
const shard = !props.shard ? undefined : props.shard;
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
return `${namespace}@${scope}:${entity}:${shard ? (shard + '--' + id) : id}${version ? ('@' + version) : ''}`
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Parse ASEID into its components
|
|
162
|
+
*
|
|
163
|
+
*
|
|
164
|
+
* @param identity
|
|
165
|
+
* @returns
|
|
166
|
+
*/
|
|
167
|
+
static parseASEID(identity: string): A_TYPES__ASEID_JSON {
|
|
168
|
+
|
|
169
|
+
const [namespace, body, version] = identity.split('@');
|
|
170
|
+
const [scope, entity, idCandidate] = body.split(':');
|
|
171
|
+
|
|
172
|
+
const shard = idCandidate.includes('--') ? idCandidate.split('--')[0] : undefined;
|
|
173
|
+
const id = idCandidate.includes('--') ? idCandidate.split('--')[1] : idCandidate;
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
namespace,
|
|
177
|
+
scope: scope,
|
|
178
|
+
entity,
|
|
179
|
+
id: id,
|
|
180
|
+
version: version ? version : undefined,
|
|
181
|
+
shard
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
toString(): string {
|
|
187
|
+
return ASEID.generateASEID({
|
|
188
|
+
namespace: this._namespace,
|
|
189
|
+
scope: this._scope,
|
|
190
|
+
entity: this._entity,
|
|
191
|
+
id: this._id,
|
|
192
|
+
version: this._version,
|
|
193
|
+
shard: this._shard
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
toJSON(): A_TYPES__ASEID_JSON {
|
|
199
|
+
return {
|
|
200
|
+
namespace: this._namespace,
|
|
201
|
+
scope: this._scope,
|
|
202
|
+
entity: this._entity,
|
|
203
|
+
id: this._id,
|
|
204
|
+
version: this._version,
|
|
205
|
+
shard: this._shard
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export class A_Deferred<T> {
|
|
2
|
+
public promise: Promise<T>;
|
|
3
|
+
private resolveFn!: (value: T | PromiseLike<T>) => void;
|
|
4
|
+
private rejectFn!: (reason?: any) => void;
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
this.promise = new Promise<T>((resolve, reject) => {
|
|
8
|
+
this.resolveFn = resolve;
|
|
9
|
+
this.rejectFn = reject;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
resolve(value: T | PromiseLike<T>): void {
|
|
14
|
+
this.resolveFn(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
reject(reason?: any): void {
|
|
18
|
+
this.rejectFn(reason);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { A_CONSTANTS__DEFAULT_ERRORS } from "../constants/errors.constants";
|
|
2
|
+
import {
|
|
3
|
+
A_TYPES__AEntity_JSON,
|
|
4
|
+
A_TYPES__IAEntity
|
|
5
|
+
} from "../types/A_Entity.types";
|
|
6
|
+
import { A_Error } from "./A_Error.class";
|
|
7
|
+
import { ASEID } from "./ASEID.class";
|
|
8
|
+
|
|
9
|
+
export class A_Entity<
|
|
10
|
+
_ConstructorType = any,
|
|
11
|
+
_SerializedType extends A_TYPES__AEntity_JSON = A_TYPES__AEntity_JSON,
|
|
12
|
+
>
|
|
13
|
+
implements A_TYPES__IAEntity {
|
|
14
|
+
|
|
15
|
+
aseid!: ASEID;
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
aseid: string
|
|
20
|
+
)
|
|
21
|
+
constructor(
|
|
22
|
+
aseid: ASEID
|
|
23
|
+
)
|
|
24
|
+
constructor(
|
|
25
|
+
serialized: _SerializedType
|
|
26
|
+
)
|
|
27
|
+
constructor(
|
|
28
|
+
newEntity: _ConstructorType
|
|
29
|
+
)
|
|
30
|
+
constructor(props: string | ASEID | _SerializedType | _ConstructorType) {
|
|
31
|
+
|
|
32
|
+
switch (true) {
|
|
33
|
+
case (typeof props === 'string' && ASEID.isASEID(props)):
|
|
34
|
+
this.aseid = new ASEID(props);
|
|
35
|
+
break;
|
|
36
|
+
case (props instanceof ASEID):
|
|
37
|
+
this.aseid = props;
|
|
38
|
+
break;
|
|
39
|
+
case (typeof props === 'object' && (props as any).aseid):
|
|
40
|
+
this.fromSerialized(props as _SerializedType);
|
|
41
|
+
break;
|
|
42
|
+
|
|
43
|
+
case (typeof props === 'object'):
|
|
44
|
+
this.fromNewEntity(props as _ConstructorType);
|
|
45
|
+
break
|
|
46
|
+
|
|
47
|
+
default:
|
|
48
|
+
throw new A_Error(A_CONSTANTS__DEFAULT_ERRORS.INCORRECT_A_ENTITY_CONSTRUCTOR);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ====================================================================
|
|
54
|
+
// ================== DUPLICATED ASEID Getters ========================
|
|
55
|
+
// ====================================================================
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Extracts the ID from the ASEID
|
|
59
|
+
* ID is the unique identifier of the entity
|
|
60
|
+
*/
|
|
61
|
+
get id(): string | number {
|
|
62
|
+
return this.aseid.id;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Extracts the namespace from the ASEID
|
|
67
|
+
* namespace is an application specific identifier from where the entity is coming from
|
|
68
|
+
*/
|
|
69
|
+
get namespace(): string {
|
|
70
|
+
return this.aseid.namespace
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Extracts the scope from the ASEID
|
|
75
|
+
* scope is the scope of the entity from Application Namespace
|
|
76
|
+
*/
|
|
77
|
+
get scope(): string {
|
|
78
|
+
return this.aseid.scope;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Extracts the entity from the ASEID
|
|
83
|
+
* entity is the name of the entity from Application Namespace
|
|
84
|
+
*/
|
|
85
|
+
get entity(): string {
|
|
86
|
+
return this.aseid.entity;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Extracts the version from the ASEID
|
|
91
|
+
* version is the version of the entity
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
get version(): string | undefined {
|
|
95
|
+
return this.aseid.version;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Extracts the shard from the ASEID
|
|
100
|
+
* shard is the shard of the entity
|
|
101
|
+
*/
|
|
102
|
+
get shard(): string | undefined {
|
|
103
|
+
return this.aseid.shard;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
protected fromNewEntity(newEntity: _ConstructorType): void {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protected fromSerialized(serialized: _SerializedType): void {
|
|
112
|
+
this.aseid = new ASEID((serialized).aseid);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Converts the entity to a JSON object
|
|
119
|
+
*
|
|
120
|
+
*
|
|
121
|
+
* @returns
|
|
122
|
+
*/
|
|
123
|
+
toJSON(): _SerializedType {
|
|
124
|
+
return {
|
|
125
|
+
aseid: this.aseid.toString()
|
|
126
|
+
} as _SerializedType;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
toString(): string {
|
|
132
|
+
return this.aseid.toString();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { A_TYPES__ServerError } from '../types/A_ServerError.types';
|
|
2
|
+
import { AxiosError } from 'axios';
|
|
3
|
+
import { A_TYPES__Error } from '../types/A_Error.types';
|
|
4
|
+
import { A_CONSTANTS__ERROR_CODES } from '../constants/errors.constants';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class A_Error extends Error {
|
|
8
|
+
|
|
9
|
+
code!: string;
|
|
10
|
+
description!: string;
|
|
11
|
+
originalError?: Error | any
|
|
12
|
+
link?: string;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
params: A_TYPES__Error | Error | AxiosError | any
|
|
17
|
+
) {
|
|
18
|
+
super(params?.message || 'Oops... Something went wrong');
|
|
19
|
+
this.identifyErrorType(params);
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
get id(): string | undefined {
|
|
25
|
+
return this.code.split('@')[1]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
protected identifyErrorType(error: Error | AxiosError | A_TYPES__Error) {
|
|
30
|
+
|
|
31
|
+
if ((error as A_TYPES__ServerError).code &&
|
|
32
|
+
(error as A_TYPES__ServerError).description &&
|
|
33
|
+
(error as A_TYPES__ServerError).serverCode) {
|
|
34
|
+
|
|
35
|
+
const target = error as A_TYPES__ServerError;
|
|
36
|
+
|
|
37
|
+
this.message = target.message;
|
|
38
|
+
this.code = target.code;
|
|
39
|
+
this.description = target.description;
|
|
40
|
+
this.originalError = target.originalError;
|
|
41
|
+
this.link = target.link;
|
|
42
|
+
}
|
|
43
|
+
else if (error instanceof Error) {
|
|
44
|
+
this.message = error.message;
|
|
45
|
+
this.code = A_CONSTANTS__ERROR_CODES.UNEXPECTED_ERROR;
|
|
46
|
+
this.description = 'If you see this error please let us know.';
|
|
47
|
+
this.originalError = error;
|
|
48
|
+
this.link = 'https://support.adaas.org/error/' + this.id;
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
} else if (error instanceof AxiosError) {
|
|
52
|
+
this.message = error.response?.data.message || error.message;
|
|
53
|
+
this.code = error.response?.data.code || A_CONSTANTS__ERROR_CODES.UNEXPECTED_ERROR;
|
|
54
|
+
this.description = error.response?.data.description || 'If you see this error please let us know.';
|
|
55
|
+
this.originalError = error.response;
|
|
56
|
+
this.link = 'https://support.adaas.org/error/' + this.id;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
get compilingData(): A_TYPES__Error {
|
|
62
|
+
return {
|
|
63
|
+
message: this.message,
|
|
64
|
+
code: this.code,
|
|
65
|
+
description: this.description,
|
|
66
|
+
originalError: this.originalError
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
toJSON(): A_TYPES__Error {
|
|
71
|
+
return this.compilingData;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
interface Ifspolyfill {
|
|
2
|
+
readFileSync: (path: string, encoding: string) => string;
|
|
3
|
+
existsSync: (path: string) => boolean;
|
|
4
|
+
createReadStream: (path: string, options?: BufferEncoding) => any
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface ICryptoInterface {
|
|
8
|
+
createTextHash(text: string, algorithm: string): Promise<string>
|
|
9
|
+
createFileHash(filePath: string, algorithm: string): Promise<string>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class A_PolyfillsClass {
|
|
14
|
+
|
|
15
|
+
private _fs!: Ifspolyfill;
|
|
16
|
+
private _crypto!: ICryptoInterface;
|
|
17
|
+
|
|
18
|
+
// eslint-disable-next-line no-use-before-define
|
|
19
|
+
private fsName = 'fs'
|
|
20
|
+
private cryptoName = 'crypto'
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async fs() {
|
|
24
|
+
if (!this._fs) {
|
|
25
|
+
await this.init();
|
|
26
|
+
}
|
|
27
|
+
return this._fs;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async crypto() {
|
|
31
|
+
if (!this._crypto) {
|
|
32
|
+
await this.init();
|
|
33
|
+
}
|
|
34
|
+
return this._crypto;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
get env(): 'server' | 'browser' {
|
|
39
|
+
let testEnvironment: 'server' | 'browser' = 'browser';
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
testEnvironment = window.location ? 'browser' : 'server';
|
|
43
|
+
|
|
44
|
+
} catch (error) {
|
|
45
|
+
|
|
46
|
+
testEnvironment = 'server';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return testEnvironment;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
private async init() {
|
|
54
|
+
try {
|
|
55
|
+
if (this.env === 'server') {
|
|
56
|
+
// eslint-disable-next-line no-use-before-define
|
|
57
|
+
this._fs = await import('' + this.fsName) as Ifspolyfill;
|
|
58
|
+
// eslint-disable-next-line no-use-before-define
|
|
59
|
+
this._crypto = {
|
|
60
|
+
createTextHash: () => Promise.resolve(''),
|
|
61
|
+
createFileHash: (filePath: string, algorithm: string = 'sha384') => new Promise(async (resolve, reject) => {
|
|
62
|
+
try {
|
|
63
|
+
const crypto = await import('' + this.cryptoName);
|
|
64
|
+
|
|
65
|
+
const hash = crypto.createHash(algorithm);
|
|
66
|
+
const fileStream = this._fs.createReadStream(filePath);
|
|
67
|
+
|
|
68
|
+
fileStream.on('data', (data) => hash.update(data));
|
|
69
|
+
fileStream.on('end', () => resolve(`${algorithm}-${hash.digest('base64')}`));
|
|
70
|
+
fileStream.on('error', (err) => reject(err));
|
|
71
|
+
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return reject(error);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
throw new Error('Not Server Environment');
|
|
81
|
+
}
|
|
82
|
+
} catch (error) {
|
|
83
|
+
|
|
84
|
+
this._fs = {
|
|
85
|
+
readFileSync: (path: string, encoding: string) => '',
|
|
86
|
+
existsSync: (path: string) => false,
|
|
87
|
+
createReadStream: (path: string) => ''
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
this._crypto = {
|
|
91
|
+
createFileHash: () => Promise.resolve(''),
|
|
92
|
+
createTextHash: (text: string, algorithm: string = 'SHA-384') => new Promise<string>(async (resolve, reject) => {
|
|
93
|
+
try {
|
|
94
|
+
const encoder = new TextEncoder();
|
|
95
|
+
const data = encoder.encode(text);
|
|
96
|
+
const hashBuffer = await crypto.subtle.digest(algorithm, data);
|
|
97
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
98
|
+
const hashBase64 = btoa(String.fromCharCode(...hashArray.map(byte => String.fromCharCode(byte) as any)));
|
|
99
|
+
return resolve(`${algorithm}-${hashBase64}`);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
return reject(error);
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
export const A_Polyfills = new A_PolyfillsClass();
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { A_TYPES__ScheduleObjectConfig } from "../types/A_ScheduleObject.types";
|
|
2
|
+
import { A_Deferred } from "./A_Deferred.class";
|
|
3
|
+
import { A_Error } from "./A_Error.class";
|
|
4
|
+
|
|
5
|
+
export class A_ScheduleObject<T> {
|
|
6
|
+
|
|
7
|
+
private timeout!: NodeJS.Timeout;
|
|
8
|
+
private deferred!: A_Deferred<T>;
|
|
9
|
+
|
|
10
|
+
private config: A_TYPES__ScheduleObjectConfig = {
|
|
11
|
+
/**
|
|
12
|
+
* If the timeout is cleared, should the promise resolve or reject?
|
|
13
|
+
* BY Default it rejects
|
|
14
|
+
*
|
|
15
|
+
* !!!NOTE: If the property is set to true, the promise will resolve with undefined
|
|
16
|
+
*/
|
|
17
|
+
resolveOnClear: false
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
ms: number,
|
|
22
|
+
action: () => Promise<T>,
|
|
23
|
+
config?: A_TYPES__ScheduleObjectConfig
|
|
24
|
+
) {
|
|
25
|
+
if (config)
|
|
26
|
+
this.config = { ...this.config, ...config };
|
|
27
|
+
|
|
28
|
+
this.deferred = new A_Deferred<T>();
|
|
29
|
+
|
|
30
|
+
this.timeout = setTimeout(
|
|
31
|
+
() => action()
|
|
32
|
+
.then((...args) => this.deferred.resolve(...args))
|
|
33
|
+
.catch((...args) => this.deferred.reject(...args)),
|
|
34
|
+
ms
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get promise(): Promise<T> {
|
|
39
|
+
return this.deferred.promise;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
clear(): void {
|
|
43
|
+
if (this.timeout) {
|
|
44
|
+
clearTimeout(this.timeout);
|
|
45
|
+
|
|
46
|
+
if (this.config.resolveOnClear)
|
|
47
|
+
this.deferred.resolve(undefined as T);
|
|
48
|
+
else
|
|
49
|
+
this.deferred.reject(new A_Error("Timeout Cleared"));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { A_TYPES__ServerError } from '../types/A_ServerError.types';
|
|
2
|
+
import { AxiosError } from 'axios';
|
|
3
|
+
import { A_Error } from './A_Error.class';
|
|
4
|
+
import { A_CONSTANTS__ERROR_CODES } from '../constants/errors.constants';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class A_ServerError extends A_Error {
|
|
8
|
+
|
|
9
|
+
code!: string;
|
|
10
|
+
description!: string;
|
|
11
|
+
serverCode: number = 500;
|
|
12
|
+
originalError: Error | any
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
constructor(params: A_TYPES__ServerError | Error | AxiosError | any) {
|
|
16
|
+
super(params);
|
|
17
|
+
this.identifyErrorType(params);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
protected identifyErrorType(error: Error | AxiosError | A_TYPES__ServerError) {
|
|
22
|
+
|
|
23
|
+
if ((error as A_TYPES__ServerError).code &&
|
|
24
|
+
(error as A_TYPES__ServerError).description &&
|
|
25
|
+
(error as A_TYPES__ServerError).serverCode) {
|
|
26
|
+
|
|
27
|
+
const target = error as A_TYPES__ServerError;
|
|
28
|
+
|
|
29
|
+
this.message = target.message;
|
|
30
|
+
this.code = target.code;
|
|
31
|
+
this.description = target.description;
|
|
32
|
+
this.serverCode = target.serverCode;
|
|
33
|
+
this.originalError = target.originalError;
|
|
34
|
+
this.link = target.link;
|
|
35
|
+
}
|
|
36
|
+
else if (error instanceof Error) {
|
|
37
|
+
this.message = error.message;
|
|
38
|
+
this.code = A_CONSTANTS__ERROR_CODES.UNEXPECTED_ERROR;
|
|
39
|
+
this.description = 'If you see this error please let us know.';
|
|
40
|
+
this.serverCode = 500;
|
|
41
|
+
this.originalError = error;
|
|
42
|
+
this.link = 'https://support.adaas.org/error/' + this.id;
|
|
43
|
+
|
|
44
|
+
} else if (error instanceof AxiosError) {
|
|
45
|
+
this.message = error.response?.data.message || error.message;
|
|
46
|
+
this.code = error.response?.data.code || A_CONSTANTS__ERROR_CODES.UNEXPECTED_ERROR;
|
|
47
|
+
this.description = error.response?.data.description || 'If you see this error please let us know.';
|
|
48
|
+
this.serverCode = error.response?.status || 500;
|
|
49
|
+
this.originalError = error.response;
|
|
50
|
+
this.link = 'https://support.adaas.org/error/' + this.id;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
get compilingData(): A_TYPES__ServerError {
|
|
56
|
+
return {
|
|
57
|
+
message: this.message,
|
|
58
|
+
code: this.code,
|
|
59
|
+
description: this.description,
|
|
60
|
+
serverCode: this.serverCode,
|
|
61
|
+
originalError: this.originalError
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
toJSON(): A_TYPES__ServerError {
|
|
66
|
+
return this.compilingData;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|