@mytmpvpn/mytmpvpn-common 5.0.0 → 6.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/dist/src/errors.d.ts +8 -2
- package/dist/src/errors.js +21 -5
- package/dist/src/models/peanuts.d.ts +2 -7
- package/dist/src/models/peanuts.js +1 -6
- package/dist/src/models/uservpn.d.ts +6 -7
- package/dist/src/models/vpn.d.ts +11 -3
- package/dist/src/models/vpn.js +29 -33
- package/dist/src/utils.d.ts +3 -4
- package/dist/test/models.test.js +193 -27
- package/package.json +5 -2
package/dist/src/errors.d.ts
CHANGED
|
@@ -8,11 +8,17 @@ export declare class InvalidVpnConfigError extends MyTmpVpnError {
|
|
|
8
8
|
constructor(msg: string);
|
|
9
9
|
}
|
|
10
10
|
export declare class MinPeanutsError extends InvalidVpnConfigError {
|
|
11
|
-
constructor(given: number, balance: number);
|
|
11
|
+
constructor(min: number, given: number, balance: number);
|
|
12
12
|
}
|
|
13
13
|
export declare class MaxPeanutsError extends InvalidVpnConfigError {
|
|
14
|
-
constructor(given: number, balance: number);
|
|
14
|
+
constructor(max: number, given: number, balance: number);
|
|
15
15
|
}
|
|
16
16
|
export declare class NotEnoughPeanutsError extends InvalidVpnConfigError {
|
|
17
17
|
constructor(given: number, balance: number);
|
|
18
18
|
}
|
|
19
|
+
export declare class MinDeleteAfterError extends InvalidVpnConfigError {
|
|
20
|
+
constructor(min: number, given: number);
|
|
21
|
+
}
|
|
22
|
+
export declare class MaxDeleteAfterError extends InvalidVpnConfigError {
|
|
23
|
+
constructor(max: number, given: number);
|
|
24
|
+
}
|
package/dist/src/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NotEnoughPeanutsError = exports.MaxPeanutsError = exports.MinPeanutsError = exports.InvalidVpnConfigError = exports.InvalidUserConfigError = exports.MyTmpVpnError = void 0;
|
|
3
|
+
exports.MaxDeleteAfterError = exports.MinDeleteAfterError = exports.NotEnoughPeanutsError = exports.MaxPeanutsError = exports.MinPeanutsError = exports.InvalidVpnConfigError = exports.InvalidUserConfigError = exports.MyTmpVpnError = void 0;
|
|
4
4
|
const peanuts_1 = require("./models/peanuts");
|
|
5
5
|
class MyTmpVpnError extends Error {
|
|
6
6
|
constructor(msg) {
|
|
@@ -27,16 +27,16 @@ class InvalidVpnConfigError extends MyTmpVpnError {
|
|
|
27
27
|
}
|
|
28
28
|
exports.InvalidVpnConfigError = InvalidVpnConfigError;
|
|
29
29
|
class MinPeanutsError extends InvalidVpnConfigError {
|
|
30
|
-
constructor(given, balance) {
|
|
31
|
-
super(`Minimum number of peanuts should be ${
|
|
30
|
+
constructor(min, given, balance) {
|
|
31
|
+
super(`Minimum number of peanuts should be ${min}, you specified: ${(0, peanuts_1.peanutsToClient)(given)} and you have ${(0, peanuts_1.peanutsToClient)(balance)}.`);
|
|
32
32
|
// Set the prototype explicitly.
|
|
33
33
|
Object.setPrototypeOf(this, MinPeanutsError.prototype);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
exports.MinPeanutsError = MinPeanutsError;
|
|
37
37
|
class MaxPeanutsError extends InvalidVpnConfigError {
|
|
38
|
-
constructor(given, balance) {
|
|
39
|
-
super(`Maximum number of peanuts should be ${
|
|
38
|
+
constructor(max, given, balance) {
|
|
39
|
+
super(`Maximum number of peanuts should be ${max}, you specified: ${(0, peanuts_1.peanutsToClient)(given)} and you have ${(0, peanuts_1.peanutsToClient)(balance)}.`);
|
|
40
40
|
// Set the prototype explicitly.
|
|
41
41
|
Object.setPrototypeOf(this, MaxPeanutsError.prototype);
|
|
42
42
|
}
|
|
@@ -50,3 +50,19 @@ class NotEnoughPeanutsError extends InvalidVpnConfigError {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
exports.NotEnoughPeanutsError = NotEnoughPeanutsError;
|
|
53
|
+
class MinDeleteAfterError extends InvalidVpnConfigError {
|
|
54
|
+
constructor(min, given) {
|
|
55
|
+
super(`deleteAfter must be greater than ${min}, you specified: ${given}.`);
|
|
56
|
+
// Set the prototype explicitly.
|
|
57
|
+
Object.setPrototypeOf(this, MinDeleteAfterError.prototype);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.MinDeleteAfterError = MinDeleteAfterError;
|
|
61
|
+
class MaxDeleteAfterError extends InvalidVpnConfigError {
|
|
62
|
+
constructor(max, given) {
|
|
63
|
+
super(`deleteAfter must be lesser than ${max}, you specified: ${given}.`);
|
|
64
|
+
// Set the prototype explicitly.
|
|
65
|
+
Object.setPrototypeOf(this, MaxDeleteAfterError.prototype);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.MaxDeleteAfterError = MaxDeleteAfterError;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
min: number;
|
|
4
|
-
max: number;
|
|
5
|
-
};
|
|
6
|
-
interface PeanutsPack {
|
|
2
|
+
export interface PeanutsPack {
|
|
7
3
|
name: string;
|
|
8
4
|
url: URL;
|
|
9
5
|
peanuts: number;
|
|
10
6
|
price: number;
|
|
11
7
|
description: string[];
|
|
12
8
|
}
|
|
13
|
-
declare function peanutsToClient(peanuts: number): number;
|
|
14
|
-
export { PEANUTS_CONFIG, PeanutsPack, peanutsToClient };
|
|
9
|
+
export declare function peanutsToClient(peanuts: number): number;
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.peanutsToClient =
|
|
4
|
-
const PEANUTS_CONFIG = {
|
|
5
|
-
min: 1,
|
|
6
|
-
max: 1000
|
|
7
|
-
};
|
|
8
|
-
exports.PEANUTS_CONFIG = PEANUTS_CONFIG;
|
|
3
|
+
exports.peanutsToClient = void 0;
|
|
9
4
|
function peanutsToClient(peanuts) {
|
|
10
5
|
return Number.parseFloat(peanuts.toFixed(2));
|
|
11
6
|
}
|
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import * as vpn from './vpn';
|
|
2
|
-
interface UserVpn {
|
|
2
|
+
export interface UserVpn {
|
|
3
3
|
userId: string;
|
|
4
4
|
version: number;
|
|
5
5
|
vpn: vpn.Vpn;
|
|
6
6
|
}
|
|
7
|
-
interface newUserVpnParams {
|
|
7
|
+
export interface newUserVpnParams {
|
|
8
8
|
userId: string;
|
|
9
9
|
region: string;
|
|
10
10
|
version?: number;
|
|
11
11
|
config: vpn.VpnConfig;
|
|
12
12
|
state: vpn.VpnState;
|
|
13
13
|
}
|
|
14
|
-
interface getUserVpnFromParams {
|
|
14
|
+
export interface getUserVpnFromParams {
|
|
15
15
|
userId: string;
|
|
16
16
|
vpnId: string;
|
|
17
17
|
version?: number;
|
|
18
18
|
config: vpn.VpnConfig;
|
|
19
19
|
state: vpn.VpnState;
|
|
20
20
|
}
|
|
21
|
-
declare function newUserVpn({ userId, region, version, ...rest }: newUserVpnParams): UserVpn;
|
|
22
|
-
declare function getUserVpnFrom({ userId, vpnId, version, ...rest }: getUserVpnFromParams): UserVpn;
|
|
23
|
-
declare function jsonToUserVpn(jsonString: any): UserVpn;
|
|
24
|
-
export { type UserVpn, newUserVpn, getUserVpnFrom, jsonToUserVpn };
|
|
21
|
+
export declare function newUserVpn({ userId, region, version, ...rest }: newUserVpnParams): UserVpn;
|
|
22
|
+
export declare function getUserVpnFrom({ userId, vpnId, version, ...rest }: getUserVpnFromParams): UserVpn;
|
|
23
|
+
export declare function jsonToUserVpn(jsonString: any): UserVpn;
|
package/dist/src/models/vpn.d.ts
CHANGED
|
@@ -14,8 +14,16 @@ export declare function fromRank(rank: number): VpnState;
|
|
|
14
14
|
export declare enum VpnType {
|
|
15
15
|
WireGuard = "wireguard"
|
|
16
16
|
}
|
|
17
|
-
export
|
|
18
|
-
|
|
17
|
+
export interface VpnConfigLimits {
|
|
18
|
+
deleteAfterFieldMinValue: number;
|
|
19
|
+
deleteAfterFieldMaxValue: number;
|
|
20
|
+
maxPeanutsFieldMinValue: number;
|
|
21
|
+
maxPeanutsFieldMaxValue: number;
|
|
22
|
+
}
|
|
23
|
+
export interface ValidDeleteAfterConfig {
|
|
24
|
+
min: number;
|
|
25
|
+
max: number;
|
|
26
|
+
}
|
|
19
27
|
export interface VpnConfig {
|
|
20
28
|
type: VpnType;
|
|
21
29
|
maxPeanuts: number;
|
|
@@ -36,7 +44,7 @@ export interface Vpn {
|
|
|
36
44
|
export declare function getVpnConfigTypes(): VpnType[];
|
|
37
45
|
export declare function vpnAgainstQuotaPredicate(uservpn: UserVpn): boolean;
|
|
38
46
|
export declare function validateVpnNbAgainstQuota(vpnNb: number, quota: number): number;
|
|
39
|
-
export declare function checkValidConfig(userId: string, currentBalance: number, vpnConfig: any): VpnConfig;
|
|
47
|
+
export declare function checkValidConfig(userId: string, currentBalance: number, vpnConfigLimits: VpnConfigLimits, vpnConfig: any): VpnConfig;
|
|
40
48
|
export declare function newVpn(region: string, config: VpnConfig, state: VpnState): Vpn;
|
|
41
49
|
export declare function getVpnFrom(vpnId: string, state: VpnState, config: VpnConfig): Vpn;
|
|
42
50
|
export declare function vpnIdToWgFileName(vpnId: string): string;
|
package/dist/src/models/vpn.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.metricsToClient = exports.vpnToClient = exports.vpnIdToWgFileName = exports.getVpnFrom = exports.newVpn = exports.checkValidConfig = exports.validateVpnNbAgainstQuota = exports.vpnAgainstQuotaPredicate = exports.getVpnConfigTypes = exports.
|
|
3
|
+
exports.metricsToClient = exports.vpnToClient = exports.vpnIdToWgFileName = exports.getVpnFrom = exports.newVpn = exports.checkValidConfig = exports.validateVpnNbAgainstQuota = exports.vpnAgainstQuotaPredicate = exports.getVpnConfigTypes = exports.VpnType = exports.fromRank = exports.toRank = exports.VpnState = void 0;
|
|
4
4
|
const errors_1 = require("../errors");
|
|
5
5
|
const peanuts_1 = require("./peanuts");
|
|
6
6
|
var VpnState;
|
|
@@ -27,8 +27,6 @@ var VpnType;
|
|
|
27
27
|
(function (VpnType) {
|
|
28
28
|
VpnType["WireGuard"] = "wireguard";
|
|
29
29
|
})(VpnType = exports.VpnType || (exports.VpnType = {}));
|
|
30
|
-
exports.MIN_DELETE_AFTER = 5 * 60; // Minimum is 5 minutes
|
|
31
|
-
exports.MAX_DELETE_AFTER = 24 * 60 * 60; // Maximum is 24 hours
|
|
32
30
|
function getVpnConfigTypes() {
|
|
33
31
|
return Object.values(VpnType).filter((item) => {
|
|
34
32
|
return isNaN(Number(item));
|
|
@@ -42,37 +40,31 @@ function validateVpnId(vpnId) {
|
|
|
42
40
|
}
|
|
43
41
|
return vpnId;
|
|
44
42
|
}
|
|
45
|
-
function validateMaxPeanuts(peanuts, currentBalance) {
|
|
43
|
+
function validateMaxPeanuts(peanuts, currentBalance, vpnConfigLimits) {
|
|
46
44
|
let result = peanuts;
|
|
47
45
|
if (peanuts <= 0) {
|
|
48
|
-
result = Math.min(currentBalance,
|
|
46
|
+
result = Math.min(currentBalance, vpnConfigLimits.maxPeanutsFieldMaxValue);
|
|
49
47
|
}
|
|
50
48
|
if (result > currentBalance) {
|
|
51
49
|
throw new errors_1.NotEnoughPeanutsError(result, currentBalance);
|
|
52
50
|
}
|
|
53
|
-
if (result <
|
|
54
|
-
throw new errors_1.MinPeanutsError(result, currentBalance);
|
|
51
|
+
if (result < vpnConfigLimits.maxPeanutsFieldMinValue) {
|
|
52
|
+
throw new errors_1.MinPeanutsError(vpnConfigLimits.maxPeanutsFieldMinValue, result, currentBalance);
|
|
55
53
|
}
|
|
56
|
-
if (result >
|
|
57
|
-
throw new errors_1.MaxPeanutsError(result, currentBalance);
|
|
54
|
+
if (result > vpnConfigLimits.maxPeanutsFieldMaxValue) {
|
|
55
|
+
throw new errors_1.MaxPeanutsError(vpnConfigLimits.maxPeanutsFieldMaxValue, result, currentBalance);
|
|
58
56
|
}
|
|
59
57
|
return result;
|
|
60
58
|
}
|
|
61
|
-
function validateDeleteAfter(deleteAfter) {
|
|
59
|
+
function validateDeleteAfter(deleteAfter, vpnConfigLimits) {
|
|
62
60
|
if (deleteAfter === undefined) {
|
|
63
|
-
return
|
|
61
|
+
return vpnConfigLimits.deleteAfterFieldMaxValue;
|
|
64
62
|
}
|
|
65
|
-
if (
|
|
66
|
-
|
|
63
|
+
if (deleteAfter < vpnConfigLimits.deleteAfterFieldMinValue) {
|
|
64
|
+
throw new errors_1.MinDeleteAfterError(vpnConfigLimits.deleteAfterFieldMinValue, deleteAfter);
|
|
67
65
|
}
|
|
68
|
-
if (
|
|
69
|
-
throw new errors_1.
|
|
70
|
-
}
|
|
71
|
-
if (deleteAfter < exports.MIN_DELETE_AFTER) {
|
|
72
|
-
throw new errors_1.InvalidVpnConfigError(`deleteAfter must be greater than ${exports.MIN_DELETE_AFTER}: ${deleteAfter}`);
|
|
73
|
-
}
|
|
74
|
-
if (deleteAfter > exports.MAX_DELETE_AFTER) {
|
|
75
|
-
throw new errors_1.InvalidVpnConfigError(`deleteAfter must be lesser than ${exports.MAX_DELETE_AFTER}: ${deleteAfter}`);
|
|
66
|
+
if (deleteAfter > vpnConfigLimits.deleteAfterFieldMaxValue) {
|
|
67
|
+
throw new errors_1.MaxDeleteAfterError(vpnConfigLimits.deleteAfterFieldMaxValue, deleteAfter);
|
|
76
68
|
}
|
|
77
69
|
return deleteAfter;
|
|
78
70
|
}
|
|
@@ -93,10 +85,10 @@ function validateVpnNbAgainstQuota(vpnNb, quota) {
|
|
|
93
85
|
}
|
|
94
86
|
exports.validateVpnNbAgainstQuota = validateVpnNbAgainstQuota;
|
|
95
87
|
// vpnConfig is of type any here because it could come from user input such as json
|
|
96
|
-
function checkValidConfig(userId, currentBalance, vpnConfig) {
|
|
97
|
-
const maxPeanuts = checkValidMaxPeanuts(userId, currentBalance, vpnConfig.maxPeanuts);
|
|
88
|
+
function checkValidConfig(userId, currentBalance, vpnConfigLimits, vpnConfig) {
|
|
89
|
+
const maxPeanuts = checkValidMaxPeanuts(userId, currentBalance, vpnConfig.maxPeanuts, vpnConfigLimits);
|
|
98
90
|
const type = checkValidVpnType(vpnConfig.type);
|
|
99
|
-
const deleteAfter = checkValidDeleteAfter(vpnConfig.deleteAfter);
|
|
91
|
+
const deleteAfter = checkValidDeleteAfter(vpnConfig.deleteAfter, vpnConfigLimits);
|
|
100
92
|
const result = {
|
|
101
93
|
maxPeanuts,
|
|
102
94
|
type,
|
|
@@ -105,13 +97,13 @@ function checkValidConfig(userId, currentBalance, vpnConfig) {
|
|
|
105
97
|
return result;
|
|
106
98
|
}
|
|
107
99
|
exports.checkValidConfig = checkValidConfig;
|
|
108
|
-
function checkValidMaxPeanuts(userId, currentBalance, maxPeanuts) {
|
|
109
|
-
const peanuts = Number.parseFloat(maxPeanuts);
|
|
110
|
-
if (Number.isNaN(peanuts)) {
|
|
100
|
+
function checkValidMaxPeanuts(userId, currentBalance, maxPeanuts, vpnConfigLimits) {
|
|
101
|
+
const peanuts = maxPeanuts ? Number.parseFloat(maxPeanuts) : -1;
|
|
102
|
+
if (Number.isNaN(peanuts) || !Number.isFinite(peanuts)) {
|
|
111
103
|
throw new errors_1.InvalidVpnConfigError(`Config maxPeanuts is invalid: ${maxPeanuts}`);
|
|
112
104
|
}
|
|
113
|
-
console.
|
|
114
|
-
return validateMaxPeanuts(peanuts, currentBalance);
|
|
105
|
+
console.debug(`Validating peanuts for ${userId}, peanuts: ${peanuts}, currentBalance: ${currentBalance}`);
|
|
106
|
+
return validateMaxPeanuts(peanuts, currentBalance, vpnConfigLimits);
|
|
115
107
|
}
|
|
116
108
|
function checkValidVpnType(jsonType) {
|
|
117
109
|
if (!jsonType) {
|
|
@@ -123,9 +115,15 @@ function checkValidVpnType(jsonType) {
|
|
|
123
115
|
}
|
|
124
116
|
return type;
|
|
125
117
|
}
|
|
126
|
-
function checkValidDeleteAfter(jsonDeleteAfter) {
|
|
118
|
+
function checkValidDeleteAfter(jsonDeleteAfter, vpnConfigLimits) {
|
|
119
|
+
if (jsonDeleteAfter === undefined) {
|
|
120
|
+
return validateDeleteAfter(undefined, vpnConfigLimits);
|
|
121
|
+
}
|
|
127
122
|
const deleteAfter = Number.parseInt(jsonDeleteAfter);
|
|
128
|
-
|
|
123
|
+
if (Number.isNaN(deleteAfter)) {
|
|
124
|
+
throw new errors_1.InvalidVpnConfigError(`Config deleteAfter is invalid: ${jsonDeleteAfter}`);
|
|
125
|
+
}
|
|
126
|
+
return validateDeleteAfter(deleteAfter, vpnConfigLimits);
|
|
129
127
|
}
|
|
130
128
|
function newVpn(region, config, state) {
|
|
131
129
|
// e.g: ap-northeast-3
|
|
@@ -136,7 +134,6 @@ function newVpn(region, config, state) {
|
|
|
136
134
|
const vpnId = `${dateToId(createdAt)}@${region}`;
|
|
137
135
|
// Assert we will be able to deserialize later on...
|
|
138
136
|
validateVpnId(vpnId);
|
|
139
|
-
validateDeleteAfter(config.deleteAfter);
|
|
140
137
|
return {
|
|
141
138
|
vpnId,
|
|
142
139
|
createdAt,
|
|
@@ -148,7 +145,6 @@ function newVpn(region, config, state) {
|
|
|
148
145
|
exports.newVpn = newVpn;
|
|
149
146
|
function getVpnFrom(vpnId, state, config) {
|
|
150
147
|
validateVpnId(vpnId);
|
|
151
|
-
validateDeleteAfter(config.deleteAfter);
|
|
152
148
|
const tokens = vpnId.split('@');
|
|
153
149
|
if (tokens.length !== 2) {
|
|
154
150
|
throw new errors_1.MyTmpVpnError(`Incorrect vpnId: ${vpnId}`);
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
declare function getFromEnvOrThrow(env: string): string;
|
|
2
|
-
declare function choose(choices: any[]): any;
|
|
3
|
-
declare function sleep(millis: number): Promise<void>;
|
|
4
|
-
export { getFromEnvOrThrow, choose, sleep };
|
|
1
|
+
export declare function getFromEnvOrThrow(env: string): string;
|
|
2
|
+
export declare function choose(choices: any[]): any;
|
|
3
|
+
export declare function sleep(millis: number): Promise<void>;
|
package/dist/test/models.test.js
CHANGED
|
@@ -36,16 +36,15 @@ describe('Testing Vpn constructors', () => {
|
|
|
36
36
|
expect(vpn.vpnId).toBeDefined();
|
|
37
37
|
});
|
|
38
38
|
it('Should create vpn with optional fields set', async () => {
|
|
39
|
-
const deleteAfter = vpn_1.MIN_DELETE_AFTER;
|
|
40
39
|
const vpn = (0, vpn_1.newVpn)('az-test-7', {
|
|
41
40
|
maxPeanuts: -1,
|
|
42
41
|
type: vpn_1.VpnType.WireGuard,
|
|
43
|
-
deleteAfter:
|
|
42
|
+
deleteAfter: 37,
|
|
44
43
|
}, vpn_1.VpnState.Created);
|
|
45
44
|
expect(vpn.createdAt).toBeDefined();
|
|
46
45
|
expect(vpn.region).toEqual('az-test-7');
|
|
47
46
|
expect(vpn.state).toEqual(vpn_1.VpnState.Created);
|
|
48
|
-
expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter:
|
|
47
|
+
expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: 37 });
|
|
49
48
|
expect(vpn.vpnId).toBeDefined();
|
|
50
49
|
});
|
|
51
50
|
it('Should create vpn with deleteAfter set to NaN', async () => {
|
|
@@ -62,12 +61,11 @@ describe('Testing Vpn constructors', () => {
|
|
|
62
61
|
expect(vpn.vpnId).toBeDefined();
|
|
63
62
|
});
|
|
64
63
|
it('Should deserialize a vpn with all mandatory fields set', async () => {
|
|
65
|
-
const
|
|
66
|
-
const vpn = (0, vpn_1.getVpnFrom)('20030902012345678@az-test-7', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: deleteAfter });
|
|
64
|
+
const vpn = (0, vpn_1.getVpnFrom)('20030902012345678@az-test-7', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: 37 });
|
|
67
65
|
expect(vpn.createdAt).toEqual(new Date('2003-09-02T01:23:45.678Z'));
|
|
68
66
|
expect(vpn.region).toEqual('az-test-7');
|
|
69
67
|
expect(vpn.state).toEqual(vpn_1.VpnState.Created);
|
|
70
|
-
expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter:
|
|
68
|
+
expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: 37 });
|
|
71
69
|
expect(vpn.vpnId).toBeDefined();
|
|
72
70
|
});
|
|
73
71
|
it('Should deserialize a vpn with deleteAfter set to NaN', async () => {
|
|
@@ -87,7 +85,7 @@ describe('Testing Vpn constructors', () => {
|
|
|
87
85
|
config: {
|
|
88
86
|
maxPeanuts: -1,
|
|
89
87
|
type: vpn_1.VpnType.WireGuard,
|
|
90
|
-
deleteAfter:
|
|
88
|
+
deleteAfter: 37
|
|
91
89
|
},
|
|
92
90
|
state: vpn_1.VpnState.Created,
|
|
93
91
|
});
|
|
@@ -107,26 +105,6 @@ describe('Testing Vpn constructors', () => {
|
|
|
107
105
|
expect(() => (0, vpn_1.getVpnFrom)('20030902012345678@test', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }))
|
|
108
106
|
.toThrow(new errors_1.MyTmpVpnError("Incorrect vpnId: \'20030902012345678@test\'"));
|
|
109
107
|
});
|
|
110
|
-
it('Should shout when deleteAfter is lower than MIN', async () => {
|
|
111
|
-
const wrongDeleteAfter = vpn_1.MIN_DELETE_AFTER - 1;
|
|
112
|
-
expect(() => (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: wrongDeleteAfter }, vpn_1.VpnState.Created))
|
|
113
|
-
.toThrow(new errors_1.MyTmpVpnError(`deleteAfter must be greater than ${vpn_1.MIN_DELETE_AFTER}: ${wrongDeleteAfter}`));
|
|
114
|
-
});
|
|
115
|
-
it('Should shout when deleteAfter is greater than MAX', async () => {
|
|
116
|
-
const wrongDeleteAfter = vpn_1.MAX_DELETE_AFTER + 1;
|
|
117
|
-
expect(() => (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: wrongDeleteAfter }, vpn_1.VpnState.Created))
|
|
118
|
-
.toThrow(new errors_1.MyTmpVpnError(`deleteAfter must be lesser than ${vpn_1.MAX_DELETE_AFTER}: ${wrongDeleteAfter}`));
|
|
119
|
-
});
|
|
120
|
-
it('Should shout when deleteAfter is positive infinity', async () => {
|
|
121
|
-
const wrongdeleteAfter = Number.POSITIVE_INFINITY;
|
|
122
|
-
expect(() => (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: wrongdeleteAfter }, vpn_1.VpnState.Created))
|
|
123
|
-
.toThrow(new errors_1.MyTmpVpnError(`deleteAfter must be finite: ${wrongdeleteAfter}`));
|
|
124
|
-
});
|
|
125
|
-
it('Should shout when deleteAfter is negative infinity', async () => {
|
|
126
|
-
const wrongdeleteAfter = Number.NEGATIVE_INFINITY;
|
|
127
|
-
expect(() => (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, deleteAfter: wrongdeleteAfter }, vpn_1.VpnState.Created))
|
|
128
|
-
.toThrow(new errors_1.MyTmpVpnError(`deleteAfter must be finite: ${wrongdeleteAfter}`));
|
|
129
|
-
});
|
|
130
108
|
});
|
|
131
109
|
describe('Testing vpnId to Wireguard file name functions ', () => {
|
|
132
110
|
it('Should shout when vpnId is empty', async () => {
|
|
@@ -142,3 +120,191 @@ describe('Testing vpnId to Wireguard file name functions ', () => {
|
|
|
142
120
|
.toThrow(new errors_1.MyTmpVpnError("Incorrect vpnId: \'20030902012345678@test\'"));
|
|
143
121
|
});
|
|
144
122
|
});
|
|
123
|
+
describe('Testing vpn config', () => {
|
|
124
|
+
it('Should set maxPeanuts to the validPeanutsConfig maximum when set to negative', async () => {
|
|
125
|
+
const validatedConfig = (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
126
|
+
maxPeanutsFieldMinValue: 0,
|
|
127
|
+
maxPeanutsFieldMaxValue: 1,
|
|
128
|
+
deleteAfterFieldMinValue: 1,
|
|
129
|
+
deleteAfterFieldMaxValue: 3,
|
|
130
|
+
}, {
|
|
131
|
+
maxPeanuts: -1,
|
|
132
|
+
type: vpn_1.VpnType.WireGuard,
|
|
133
|
+
deleteAfter: 1
|
|
134
|
+
});
|
|
135
|
+
expect(validatedConfig).toEqual({
|
|
136
|
+
maxPeanuts: 1,
|
|
137
|
+
type: vpn_1.VpnType.WireGuard,
|
|
138
|
+
deleteAfter: 1
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
it('Should set maxPeanuts to the validPeanutsConfig maximum when null', async () => {
|
|
142
|
+
const validatedConfig = (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
143
|
+
maxPeanutsFieldMinValue: 0,
|
|
144
|
+
maxPeanutsFieldMaxValue: 1,
|
|
145
|
+
deleteAfterFieldMinValue: 1,
|
|
146
|
+
deleteAfterFieldMaxValue: 3,
|
|
147
|
+
}, {
|
|
148
|
+
maxPeanuts: null,
|
|
149
|
+
type: vpn_1.VpnType.WireGuard,
|
|
150
|
+
deleteAfter: 1
|
|
151
|
+
});
|
|
152
|
+
expect(validatedConfig).toEqual({
|
|
153
|
+
maxPeanuts: 1,
|
|
154
|
+
type: vpn_1.VpnType.WireGuard,
|
|
155
|
+
deleteAfter: 1
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
it('Should set maxPeanuts to the validPeanutsConfig maximum when undefined', async () => {
|
|
159
|
+
const validatedConfig = (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
160
|
+
maxPeanutsFieldMinValue: 0,
|
|
161
|
+
maxPeanutsFieldMaxValue: 1,
|
|
162
|
+
deleteAfterFieldMinValue: 1,
|
|
163
|
+
deleteAfterFieldMaxValue: 3,
|
|
164
|
+
}, {
|
|
165
|
+
maxPeanuts: undefined,
|
|
166
|
+
type: vpn_1.VpnType.WireGuard,
|
|
167
|
+
deleteAfter: 1
|
|
168
|
+
});
|
|
169
|
+
expect(validatedConfig).toEqual({
|
|
170
|
+
maxPeanuts: 1,
|
|
171
|
+
type: vpn_1.VpnType.WireGuard,
|
|
172
|
+
deleteAfter: 1
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
it('Should shout when maxPeanuts is Infinity', async () => {
|
|
176
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
177
|
+
maxPeanutsFieldMinValue: 2,
|
|
178
|
+
maxPeanutsFieldMaxValue: 3,
|
|
179
|
+
deleteAfterFieldMinValue: 1,
|
|
180
|
+
deleteAfterFieldMaxValue: 3,
|
|
181
|
+
}, {
|
|
182
|
+
maxPeanuts: Number.POSITIVE_INFINITY,
|
|
183
|
+
type: vpn_1.VpnType.WireGuard,
|
|
184
|
+
deleteAfter: 1
|
|
185
|
+
})).toThrow(new errors_1.InvalidVpnConfigError('Config maxPeanuts is invalid: Infinity'));
|
|
186
|
+
});
|
|
187
|
+
it('Should shout when maxPeanuts is -Infinity', async () => {
|
|
188
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
189
|
+
maxPeanutsFieldMinValue: 2,
|
|
190
|
+
maxPeanutsFieldMaxValue: 3,
|
|
191
|
+
deleteAfterFieldMinValue: 1,
|
|
192
|
+
deleteAfterFieldMaxValue: 3,
|
|
193
|
+
}, {
|
|
194
|
+
maxPeanuts: Number.NEGATIVE_INFINITY,
|
|
195
|
+
type: vpn_1.VpnType.WireGuard,
|
|
196
|
+
deleteAfter: 1
|
|
197
|
+
})).toThrow(new errors_1.InvalidVpnConfigError('Config maxPeanuts is invalid: -Infinity'));
|
|
198
|
+
});
|
|
199
|
+
it('Should shout when maxPeanuts is not a number', async () => {
|
|
200
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
201
|
+
maxPeanutsFieldMinValue: 2,
|
|
202
|
+
maxPeanutsFieldMaxValue: 3,
|
|
203
|
+
deleteAfterFieldMinValue: 1,
|
|
204
|
+
deleteAfterFieldMaxValue: 3,
|
|
205
|
+
}, {
|
|
206
|
+
maxPeanuts: 'Not a number',
|
|
207
|
+
type: vpn_1.VpnType.WireGuard,
|
|
208
|
+
deleteAfter: 1
|
|
209
|
+
})).toThrow(new errors_1.InvalidVpnConfigError('Config maxPeanuts is invalid: Not a number'));
|
|
210
|
+
});
|
|
211
|
+
it('Should shout when maxPeanuts is below the validPeanutsConfig minimum', async () => {
|
|
212
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
213
|
+
maxPeanutsFieldMinValue: 2,
|
|
214
|
+
maxPeanutsFieldMaxValue: 3,
|
|
215
|
+
deleteAfterFieldMinValue: 1,
|
|
216
|
+
deleteAfterFieldMaxValue: 3,
|
|
217
|
+
}, {
|
|
218
|
+
maxPeanuts: 1,
|
|
219
|
+
type: vpn_1.VpnType.WireGuard,
|
|
220
|
+
deleteAfter: 1
|
|
221
|
+
})).toThrow(new errors_1.MinPeanutsError(2, 1, 2));
|
|
222
|
+
});
|
|
223
|
+
it('Should shout when maxPeanuts is above the validPeanutsConfig maximum', async () => {
|
|
224
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 10, {
|
|
225
|
+
maxPeanutsFieldMinValue: 2,
|
|
226
|
+
maxPeanutsFieldMaxValue: 3,
|
|
227
|
+
deleteAfterFieldMinValue: 1,
|
|
228
|
+
deleteAfterFieldMaxValue: 3,
|
|
229
|
+
}, {
|
|
230
|
+
maxPeanuts: 5,
|
|
231
|
+
type: vpn_1.VpnType.WireGuard,
|
|
232
|
+
deleteAfter: 1
|
|
233
|
+
})).toThrow(new errors_1.MaxPeanutsError(3, 5, 10));
|
|
234
|
+
});
|
|
235
|
+
it('Should shout when deleteAfter is below the validPeanutsConfig minimum', async () => {
|
|
236
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
237
|
+
maxPeanutsFieldMinValue: 1,
|
|
238
|
+
maxPeanutsFieldMaxValue: 3,
|
|
239
|
+
deleteAfterFieldMinValue: 3,
|
|
240
|
+
deleteAfterFieldMaxValue: 5,
|
|
241
|
+
}, {
|
|
242
|
+
maxPeanuts: 1,
|
|
243
|
+
type: vpn_1.VpnType.WireGuard,
|
|
244
|
+
deleteAfter: 1
|
|
245
|
+
})).toThrow(new errors_1.MinDeleteAfterError(3, 1));
|
|
246
|
+
});
|
|
247
|
+
it('Should shout when deleteAfter is greater the validPeanutsConfig maximum', async () => {
|
|
248
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
249
|
+
maxPeanutsFieldMinValue: 1,
|
|
250
|
+
maxPeanutsFieldMaxValue: 3,
|
|
251
|
+
deleteAfterFieldMinValue: 3,
|
|
252
|
+
deleteAfterFieldMaxValue: 5,
|
|
253
|
+
}, {
|
|
254
|
+
maxPeanuts: 1,
|
|
255
|
+
type: vpn_1.VpnType.WireGuard,
|
|
256
|
+
deleteAfter: 7
|
|
257
|
+
})).toThrow(new errors_1.MaxDeleteAfterError(5, 7));
|
|
258
|
+
});
|
|
259
|
+
it('Should shout when deleteAfter is positive infinity', async () => {
|
|
260
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
261
|
+
maxPeanutsFieldMinValue: 1,
|
|
262
|
+
maxPeanutsFieldMaxValue: 3,
|
|
263
|
+
deleteAfterFieldMinValue: 3,
|
|
264
|
+
deleteAfterFieldMaxValue: 5,
|
|
265
|
+
}, {
|
|
266
|
+
maxPeanuts: 1,
|
|
267
|
+
type: vpn_1.VpnType.WireGuard,
|
|
268
|
+
deleteAfter: Number.POSITIVE_INFINITY
|
|
269
|
+
})).toThrow(new errors_1.InvalidVpnConfigError('Config deleteAfter is invalid: Infinity'));
|
|
270
|
+
});
|
|
271
|
+
it('Should shout when deleteAfter is negative infinity', async () => {
|
|
272
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
273
|
+
maxPeanutsFieldMinValue: 1,
|
|
274
|
+
maxPeanutsFieldMaxValue: 3,
|
|
275
|
+
deleteAfterFieldMinValue: 3,
|
|
276
|
+
deleteAfterFieldMaxValue: 5,
|
|
277
|
+
}, {
|
|
278
|
+
maxPeanuts: 1,
|
|
279
|
+
type: vpn_1.VpnType.WireGuard,
|
|
280
|
+
deleteAfter: Number.NEGATIVE_INFINITY
|
|
281
|
+
})).toThrow(new errors_1.InvalidVpnConfigError('Config deleteAfter is invalid: -Infinity'));
|
|
282
|
+
});
|
|
283
|
+
it('Should shout when deleteAfter is not a number', async () => {
|
|
284
|
+
expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
|
|
285
|
+
maxPeanutsFieldMinValue: 1,
|
|
286
|
+
maxPeanutsFieldMaxValue: 3,
|
|
287
|
+
deleteAfterFieldMinValue: 3,
|
|
288
|
+
deleteAfterFieldMaxValue: 5,
|
|
289
|
+
}, {
|
|
290
|
+
maxPeanuts: 1,
|
|
291
|
+
type: vpn_1.VpnType.WireGuard,
|
|
292
|
+
deleteAfter: 'Not a number'
|
|
293
|
+
})).toThrow(new errors_1.InvalidVpnConfigError('Config deleteAfter is invalid: Not a number'));
|
|
294
|
+
});
|
|
295
|
+
it('Should not shout when deleteAfter is undefined', async () => {
|
|
296
|
+
expect((0, vpn_1.checkValidConfig)('userId', 2, {
|
|
297
|
+
maxPeanutsFieldMinValue: 1,
|
|
298
|
+
maxPeanutsFieldMaxValue: 3,
|
|
299
|
+
deleteAfterFieldMinValue: 3,
|
|
300
|
+
deleteAfterFieldMaxValue: 5,
|
|
301
|
+
}, {
|
|
302
|
+
maxPeanuts: 1,
|
|
303
|
+
type: vpn_1.VpnType.WireGuard,
|
|
304
|
+
})).toEqual({
|
|
305
|
+
maxPeanuts: 1,
|
|
306
|
+
type: vpn_1.VpnType.WireGuard,
|
|
307
|
+
deleteAfter: 5
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mytmpvpn/mytmpvpn-common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.2",
|
|
4
4
|
"description": "Common library for all MyTmpVpn related projects",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -23,7 +23,10 @@
|
|
|
23
23
|
"test": "jest",
|
|
24
24
|
"link-deps": "echo \"No dependencies, noop; that's fine\"",
|
|
25
25
|
"clean": "rm -rf dist/",
|
|
26
|
-
"upgrade": "ncu --target semver --upgrade"
|
|
26
|
+
"upgrade": "ncu --target semver --upgrade",
|
|
27
|
+
"prepublishOnly": "npm ci && npm run build && npm run test",
|
|
28
|
+
"version": "git add -A",
|
|
29
|
+
"postversion": "git push && git push --tags && npm publish"
|
|
27
30
|
},
|
|
28
31
|
"keywords": [],
|
|
29
32
|
"author": "pierre.vigneras@gmail.com",
|