@mytmpvpn/mytmpvpn-common 6.0.2 → 6.1.1

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);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
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
+ const loglevel_1 = require("loglevel");
6
7
  var VpnState;
7
8
  (function (VpnState) {
8
9
  VpnState["Failed"] = "Failed";
@@ -41,20 +42,25 @@ function validateVpnId(vpnId) {
41
42
  return vpnId;
42
43
  }
43
44
  function validateMaxPeanuts(peanuts, currentBalance, vpnConfigLimits) {
44
- 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
+ }
45
49
  if (peanuts <= 0) {
46
- 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;
47
53
  }
48
- if (result > currentBalance) {
49
- throw new errors_1.NotEnoughPeanutsError(result, currentBalance);
54
+ if (peanuts > currentBalance) {
55
+ throw new errors_1.NotEnoughPeanutsError(peanuts, currentBalance);
50
56
  }
51
- if (result < vpnConfigLimits.maxPeanutsFieldMinValue) {
52
- throw new errors_1.MinPeanutsError(vpnConfigLimits.maxPeanutsFieldMinValue, result, currentBalance);
57
+ if (peanuts < vpnConfigLimits.maxPeanutsFieldMinValue) {
58
+ throw new errors_1.MinPeanutsError(vpnConfigLimits.maxPeanutsFieldMinValue, peanuts, currentBalance);
53
59
  }
54
- if (result > vpnConfigLimits.maxPeanutsFieldMaxValue) {
55
- throw new errors_1.MaxPeanutsError(vpnConfigLimits.maxPeanutsFieldMaxValue, result, currentBalance);
60
+ if (peanuts > vpnConfigLimits.maxPeanutsFieldMaxValue) {
61
+ throw new errors_1.MaxPeanutsError(vpnConfigLimits.maxPeanutsFieldMaxValue, peanuts, currentBalance);
56
62
  }
57
- return result;
63
+ return peanuts;
58
64
  }
59
65
  function validateDeleteAfter(deleteAfter, vpnConfigLimits) {
60
66
  if (deleteAfter === undefined) {
@@ -102,7 +108,7 @@ function checkValidMaxPeanuts(userId, currentBalance, maxPeanuts, vpnConfigLimit
102
108
  if (Number.isNaN(peanuts) || !Number.isFinite(peanuts)) {
103
109
  throw new errors_1.InvalidVpnConfigError(`Config maxPeanuts is invalid: ${maxPeanuts}`);
104
110
  }
105
- console.debug(`Validating peanuts for ${userId}, peanuts: ${peanuts}, currentBalance: ${currentBalance}`);
111
+ loglevel_1.default.debug(`Validating peanuts for ${userId}, peanuts: ${peanuts}, currentBalance: ${currentBalance}`);
106
112
  return validateMaxPeanuts(peanuts, currentBalance, vpnConfigLimits);
107
113
  }
108
114
  function checkValidVpnType(jsonType) {
@@ -234,7 +240,7 @@ function vpnIdToWgFileName(vpnId) {
234
240
  const number = seps[2];
235
241
  // Let's make sure the end file is of 15 char max
236
242
  const result = `${ddhhmmss}${ccld}${location}${number}`.substring(0, 15);
237
- console.log(`Transforming ${vpnId} to ${result}`);
243
+ loglevel_1.default.info(`Transforming ${vpnId} to ${result}`);
238
244
  return result;
239
245
  }
240
246
  exports.vpnIdToWgFileName = vpnIdToWgFileName;
@@ -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.0.2",
3
+ "version": "6.1.1",
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",