@mytmpvpn/mytmpvpn-common 6.1.0 → 6.2.0

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.
@@ -4,6 +4,9 @@ export declare class MyTmpVpnError extends Error {
4
4
  export declare class InvalidUserConfigError extends MyTmpVpnError {
5
5
  constructor(msg: string);
6
6
  }
7
+ export declare class NegativeBalanceError extends MyTmpVpnError {
8
+ constructor(balance: number);
9
+ }
7
10
  export declare class InvalidVpnConfigError extends MyTmpVpnError {
8
11
  constructor(msg: string);
9
12
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MaxDeleteAfterError = exports.MinDeleteAfterError = 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.NegativeBalanceError = exports.InvalidUserConfigError = exports.MyTmpVpnError = void 0;
4
4
  const peanuts_1 = require("./models/peanuts");
5
5
  class MyTmpVpnError extends Error {
6
6
  constructor(msg) {
@@ -18,6 +18,14 @@ class InvalidUserConfigError extends MyTmpVpnError {
18
18
  }
19
19
  }
20
20
  exports.InvalidUserConfigError = InvalidUserConfigError;
21
+ class NegativeBalanceError extends MyTmpVpnError {
22
+ constructor(balance) {
23
+ super(`Negative peanuts balance: ${(0, peanuts_1.peanutsToClient)(balance)}.`);
24
+ // Set the prototype explicitly.
25
+ Object.setPrototypeOf(this, NotEnoughPeanutsError.prototype);
26
+ }
27
+ }
28
+ exports.NegativeBalanceError = NegativeBalanceError;
21
29
  class InvalidVpnConfigError extends MyTmpVpnError {
22
30
  constructor(msg) {
23
31
  super(msg);
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.peanutsToClient = void 0;
4
4
  function peanutsToClient(peanuts) {
5
- return Number.parseFloat(peanuts.toFixed(2));
5
+ return peanuts ? Number.parseFloat(peanuts.toFixed(2)) : 0;
6
6
  }
7
7
  exports.peanutsToClient = peanutsToClient;
@@ -50,3 +50,8 @@ export declare function getVpnFrom(vpnId: string, state: VpnState, config: VpnCo
50
50
  export declare function vpnIdToWgFileName(vpnId: string): string;
51
51
  export declare function vpnToClient(vpn: Vpn): Vpn;
52
52
  export declare function metricsToClient(metrics: VpnMetrics | undefined): VpnMetrics;
53
+ /**
54
+ * Extract the creation date from a VPN ID
55
+ * VPN ID format: {dateToId(createdAt)}@{region}
56
+ */
57
+ export declare function getCreationDateFromVpnId(vpnId: string): Date;
@@ -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.VpnType = exports.fromRank = exports.toRank = exports.VpnState = void 0;
3
+ exports.getCreationDateFromVpnId = 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
  const loglevel_1 = require("loglevel");
@@ -42,20 +42,25 @@ function validateVpnId(vpnId) {
42
42
  return vpnId;
43
43
  }
44
44
  function validateMaxPeanuts(peanuts, currentBalance, vpnConfigLimits) {
45
- let result = peanuts;
45
+ // No matter what, if currentBalance is negative, we fail
46
+ if (currentBalance <= 0) {
47
+ throw new errors_1.NegativeBalanceError(currentBalance);
48
+ }
46
49
  if (peanuts <= 0) {
47
- result = Math.min(currentBalance, vpnConfigLimits.maxPeanutsFieldMaxValue);
50
+ // result = Math.min(currentBalance, vpnConfigLimits.maxPeanutsFieldMaxValue)
51
+ // This is always safe as it means run as long as there are enough peanuts
52
+ return -1;
48
53
  }
49
- if (result > currentBalance) {
50
- throw new errors_1.NotEnoughPeanutsError(result, currentBalance);
54
+ if (peanuts > currentBalance) {
55
+ throw new errors_1.NotEnoughPeanutsError(peanuts, currentBalance);
51
56
  }
52
- if (result < vpnConfigLimits.maxPeanutsFieldMinValue) {
53
- throw new errors_1.MinPeanutsError(vpnConfigLimits.maxPeanutsFieldMinValue, result, currentBalance);
57
+ if (peanuts < vpnConfigLimits.maxPeanutsFieldMinValue) {
58
+ throw new errors_1.MinPeanutsError(vpnConfigLimits.maxPeanutsFieldMinValue, peanuts, currentBalance);
54
59
  }
55
- if (result > vpnConfigLimits.maxPeanutsFieldMaxValue) {
56
- throw new errors_1.MaxPeanutsError(vpnConfigLimits.maxPeanutsFieldMaxValue, result, currentBalance);
60
+ if (peanuts > vpnConfigLimits.maxPeanutsFieldMaxValue) {
61
+ throw new errors_1.MaxPeanutsError(vpnConfigLimits.maxPeanutsFieldMaxValue, peanuts, currentBalance);
57
62
  }
58
- return result;
63
+ return peanuts;
59
64
  }
60
65
  function validateDeleteAfter(deleteAfter, vpnConfigLimits) {
61
66
  if (deleteAfter === undefined) {
@@ -271,3 +276,16 @@ function metricsToClient(metrics) {
271
276
  };
272
277
  }
273
278
  exports.metricsToClient = metricsToClient;
279
+ /**
280
+ * Extract the creation date from a VPN ID
281
+ * VPN ID format: {dateToId(createdAt)}@{region}
282
+ */
283
+ function getCreationDateFromVpnId(vpnId) {
284
+ validateVpnId(vpnId);
285
+ const tokens = vpnId.split('@');
286
+ if (tokens.length !== 2) {
287
+ throw new errors_1.MyTmpVpnError(`Invalid VPN ID format: ${vpnId}`);
288
+ }
289
+ return idToDate(tokens[0]);
290
+ }
291
+ exports.getCreationDateFromVpnId = getCreationDateFromVpnId;
@@ -121,7 +121,7 @@ describe('Testing vpnId to Wireguard file name functions ', () => {
121
121
  });
122
122
  });
123
123
  describe('Testing vpn config', () => {
124
- it('Should set maxPeanuts to the validPeanutsConfig maximum when set to negative', async () => {
124
+ it('Should keep maxPeanuts to -1 when specified', async () => {
125
125
  const validatedConfig = (0, vpn_1.checkValidConfig)('userId', 2, {
126
126
  maxPeanutsFieldMinValue: 0,
127
127
  maxPeanutsFieldMaxValue: 1,
@@ -133,12 +133,12 @@ describe('Testing vpn config', () => {
133
133
  deleteAfter: 1
134
134
  });
135
135
  expect(validatedConfig).toEqual({
136
- maxPeanuts: 1,
136
+ maxPeanuts: -1,
137
137
  type: vpn_1.VpnType.WireGuard,
138
138
  deleteAfter: 1
139
139
  });
140
140
  });
141
- it('Should set maxPeanuts to the validPeanutsConfig maximum when null', async () => {
141
+ it('Should set maxPeanuts to -1 when null', async () => {
142
142
  const validatedConfig = (0, vpn_1.checkValidConfig)('userId', 2, {
143
143
  maxPeanutsFieldMinValue: 0,
144
144
  maxPeanutsFieldMaxValue: 1,
@@ -150,12 +150,12 @@ describe('Testing vpn config', () => {
150
150
  deleteAfter: 1
151
151
  });
152
152
  expect(validatedConfig).toEqual({
153
- maxPeanuts: 1,
153
+ maxPeanuts: -1,
154
154
  type: vpn_1.VpnType.WireGuard,
155
155
  deleteAfter: 1
156
156
  });
157
157
  });
158
- it('Should set maxPeanuts to the validPeanutsConfig maximum when undefined', async () => {
158
+ it('Should set maxPeanuts to -1 when undefined', async () => {
159
159
  const validatedConfig = (0, vpn_1.checkValidConfig)('userId', 2, {
160
160
  maxPeanutsFieldMinValue: 0,
161
161
  maxPeanutsFieldMaxValue: 1,
@@ -167,11 +167,35 @@ describe('Testing vpn config', () => {
167
167
  deleteAfter: 1
168
168
  });
169
169
  expect(validatedConfig).toEqual({
170
- maxPeanuts: 1,
170
+ maxPeanuts: -1,
171
171
  type: vpn_1.VpnType.WireGuard,
172
172
  deleteAfter: 1
173
173
  });
174
174
  });
175
+ it('Should shout when currentBalance is negative', async () => {
176
+ expect(() => (0, vpn_1.checkValidConfig)('userId', -1, {
177
+ maxPeanutsFieldMinValue: 2,
178
+ maxPeanutsFieldMaxValue: 3,
179
+ deleteAfterFieldMinValue: 1,
180
+ deleteAfterFieldMaxValue: 3,
181
+ }, {
182
+ maxPeanuts: -1,
183
+ type: vpn_1.VpnType.WireGuard,
184
+ deleteAfter: 1
185
+ })).toThrow(new errors_1.NegativeBalanceError(-1));
186
+ });
187
+ it('Should shout when currentBalance is zero', async () => {
188
+ expect(() => (0, vpn_1.checkValidConfig)('userId', 0, {
189
+ maxPeanutsFieldMinValue: 2,
190
+ maxPeanutsFieldMaxValue: 3,
191
+ deleteAfterFieldMinValue: 1,
192
+ deleteAfterFieldMaxValue: 3,
193
+ }, {
194
+ maxPeanuts: -1,
195
+ type: vpn_1.VpnType.WireGuard,
196
+ deleteAfter: 1
197
+ })).toThrow(new errors_1.NegativeBalanceError(0));
198
+ });
175
199
  it('Should shout when maxPeanuts is Infinity', async () => {
176
200
  expect(() => (0, vpn_1.checkValidConfig)('userId', 2, {
177
201
  maxPeanutsFieldMinValue: 2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mytmpvpn/mytmpvpn-common",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "Common library for all MyTmpVpn related projects",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -36,7 +36,7 @@
36
36
  "@typescript-eslint/eslint-plugin": "^5.62.0",
37
37
  "eslint": "^8.57.1",
38
38
  "eslint-config-standard-with-typescript": "^37.0.0",
39
- "eslint-plugin-import": "^2.31.0",
39
+ "eslint-plugin-import": "^2.32.0",
40
40
  "eslint-plugin-n": "^16.6.2",
41
41
  "eslint-plugin-promise": "^6.6.0",
42
42
  "jest": "^27.5.1",