@mytmpvpn/mytmpvpn-common 1.2.0 → 1.2.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.
@@ -0,0 +1,2 @@
1
+ export * as utils from './utils';
2
+ export * as models from './models';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.models = exports.utils = void 0;
4
+ exports.utils = require("./utils");
5
+ exports.models = require("./models");
@@ -0,0 +1,2 @@
1
+ export * as vpn from './vpn';
2
+ export * as uservpn from './uservpn';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uservpn = exports.vpn = void 0;
4
+ exports.vpn = require("./vpn");
5
+ exports.uservpn = require("./uservpn");
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ declare const PEANUTS_CONFIG: {
3
+ min: number;
4
+ max: number;
5
+ };
6
+ interface PeanutsPack {
7
+ name: string;
8
+ url: URL;
9
+ peanuts: number;
10
+ price: number;
11
+ }
12
+ export { PEANUTS_CONFIG, PeanutsPack };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PEANUTS_CONFIG = void 0;
4
+ const PEANUTS_CONFIG = {
5
+ min: 5,
6
+ max: 100,
7
+ };
8
+ exports.PEANUTS_CONFIG = PEANUTS_CONFIG;
@@ -0,0 +1,23 @@
1
+ import * as vpn from './vpn';
2
+ interface UserVpn {
3
+ userId: string;
4
+ version: number;
5
+ vpn: vpn.Vpn;
6
+ }
7
+ type newUserVpnParams = {
8
+ userId: string;
9
+ region: string;
10
+ version?: number;
11
+ config?: any;
12
+ state?: any;
13
+ };
14
+ type getUserVpnFromParams = {
15
+ userId: string;
16
+ vpnId: string;
17
+ version?: number;
18
+ config?: any;
19
+ state?: any;
20
+ };
21
+ declare function newUserVpn({ userId, region, version, ...rest }: newUserVpnParams): UserVpn;
22
+ declare function getUserVpnFrom({ userId, vpnId, version, ...rest }: getUserVpnFromParams): UserVpn;
23
+ export { UserVpn, newUserVpn, getUserVpnFrom };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUserVpnFrom = exports.newUserVpn = void 0;
4
+ const vpn = require("./vpn");
5
+ const INITIAL_VERSION = 0;
6
+ function newUserVpn({ userId, region, version = INITIAL_VERSION, ...rest }) {
7
+ return {
8
+ userId: userId,
9
+ version: version,
10
+ vpn: vpn.newVpn(region, rest.config, rest.state)
11
+ };
12
+ }
13
+ exports.newUserVpn = newUserVpn;
14
+ function getUserVpnFrom({ userId, vpnId, version = INITIAL_VERSION, ...rest }) {
15
+ return {
16
+ userId: userId,
17
+ version: version,
18
+ vpn: vpn.getVpnFrom(vpnId, rest.state, rest.config)
19
+ };
20
+ }
21
+ exports.getUserVpnFrom = getUserVpnFrom;
@@ -0,0 +1,35 @@
1
+ declare enum VpnState {
2
+ Failed = "Failed",
3
+ Creating = "Creating",
4
+ Created = "Created",
5
+ Provisioning = "Provisioning",
6
+ Paused = "Paused",
7
+ Running = "Running",
8
+ Deprovisioning = "Deprovisioning",
9
+ Deleted = "Deleted"
10
+ }
11
+ declare function rank(state: VpnState): number;
12
+ declare enum VpnType {
13
+ OpenVpn = "openvpn",
14
+ WireGuard = "wireguard"
15
+ }
16
+ interface VpnConfig {
17
+ type: VpnType;
18
+ maxPeanuts: number;
19
+ }
20
+ interface VpnMetrics {
21
+ duration: number;
22
+ bytes: number;
23
+ peanuts: number;
24
+ }
25
+ interface Vpn {
26
+ vpnId: string;
27
+ createdAt: Date;
28
+ region: string;
29
+ config: VpnConfig;
30
+ state: VpnState;
31
+ }
32
+ declare function getVpnConfigTypes(): VpnType[];
33
+ declare function newVpn(region: string, config: VpnConfig, state: VpnState): Vpn;
34
+ declare function getVpnFrom(vpnId: string, state: VpnState, config: VpnConfig): Vpn;
35
+ export { VpnState, VpnMetrics, VpnType, VpnConfig, Vpn, rank, newVpn, getVpnFrom, getVpnConfigTypes };
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getVpnConfigTypes = exports.getVpnFrom = exports.newVpn = exports.rank = exports.VpnType = exports.VpnState = void 0;
4
+ var VpnState;
5
+ (function (VpnState) {
6
+ VpnState["Failed"] = "Failed";
7
+ VpnState["Creating"] = "Creating";
8
+ VpnState["Created"] = "Created";
9
+ VpnState["Provisioning"] = "Provisioning";
10
+ VpnState["Paused"] = "Paused";
11
+ VpnState["Running"] = "Running";
12
+ VpnState["Deprovisioning"] = "Deprovisioning";
13
+ VpnState["Deleted"] = "Deleted";
14
+ })(VpnState || (VpnState = {}));
15
+ exports.VpnState = VpnState;
16
+ const vpnStateRank = Object.values(VpnState);
17
+ function rank(state) {
18
+ return vpnStateRank.indexOf(state);
19
+ }
20
+ exports.rank = rank;
21
+ var VpnType;
22
+ (function (VpnType) {
23
+ VpnType["OpenVpn"] = "openvpn";
24
+ VpnType["WireGuard"] = "wireguard";
25
+ })(VpnType || (VpnType = {}));
26
+ exports.VpnType = VpnType;
27
+ function getVpnConfigTypes() {
28
+ return Object.values(VpnType).filter((item) => {
29
+ return isNaN(Number(item));
30
+ });
31
+ }
32
+ exports.getVpnConfigTypes = getVpnConfigTypes;
33
+ function newVpn(region, config, state) {
34
+ const createdAt = new Date();
35
+ // Replace ':' in the id as it's forbiden in DDB
36
+ const vpnId = `${dateToId(createdAt)}@${region}`;
37
+ return {
38
+ vpnId: vpnId,
39
+ createdAt: createdAt,
40
+ region: region,
41
+ config: config,
42
+ state: state,
43
+ };
44
+ }
45
+ exports.newVpn = newVpn;
46
+ function getVpnFrom(vpnId, state, config) {
47
+ const tokens = vpnId.split('@');
48
+ if (tokens.length !== 2) {
49
+ throw new Error(`Bad vpnId format: ${vpnId}`);
50
+ }
51
+ const createdAt = idToDate(tokens[0]);
52
+ const region = tokens[1];
53
+ return {
54
+ vpnId: vpnId,
55
+ createdAt: createdAt,
56
+ region: region,
57
+ config: config,
58
+ state: state,
59
+ };
60
+ }
61
+ exports.getVpnFrom = getVpnFrom;
62
+ // Using a date from an id is convenient for debugging.
63
+ // We can't use Date.toISOString() because some characters are forbidden
64
+ // E.g: SFn refuse the ':'
65
+ // So we need convertor back and forth
66
+ function dateToId(date) {
67
+ // YYYY-MM-DDTHH:mm:ss.uuuZ -> YYYYMMDDHHmmssuuu
68
+ return date.toISOString().replace(/[^\d]/g, '');
69
+ }
70
+ function idToDate(id) {
71
+ // YYYYMMDDHHmmssuuu -> YYYY-MM-DDTHH:mm:ss.uuuZ
72
+ const year = id.substring(0, 4);
73
+ const month = id.substring(4, 6);
74
+ const day = id.substring(6, 8);
75
+ const hour = id.substring(8, 10);
76
+ const minute = id.substring(10, 12);
77
+ const second = id.substring(12, 14);
78
+ const millis = id.substring(14);
79
+ return new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}.${millis}Z`);
80
+ }
@@ -0,0 +1,4 @@
1
+ declare function getFromEnvOrThrow(env: string): string;
2
+ declare function choose(choices: any[]): any;
3
+ declare function sleep(millis: number): Promise<unknown>;
4
+ export { getFromEnvOrThrow, choose, sleep };
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sleep = exports.choose = exports.getFromEnvOrThrow = void 0;
4
+ const log = require("loglevel");
5
+ function getFromEnvOrThrow(env) {
6
+ const result = process.env[env];
7
+ if (!result) {
8
+ throw new Error(`${env} unset in environment!`);
9
+ }
10
+ log.debug(`Using ${env} = ${result}`);
11
+ return result;
12
+ }
13
+ exports.getFromEnvOrThrow = getFromEnvOrThrow;
14
+ function choose(choices) {
15
+ var index = Math.floor(Math.random() * choices.length);
16
+ return choices[index];
17
+ }
18
+ exports.choose = choose;
19
+ function sleep(millis) {
20
+ return new Promise(resolve => setTimeout(resolve, millis));
21
+ }
22
+ exports.sleep = sleep;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vpn_1 = require("../src/models/vpn");
4
+ describe('Testing VpnState', () => {
5
+ it("should check that all VpnState are ordered properly", async () => {
6
+ expect((0, vpn_1.rank)(vpn_1.VpnState.Creating) <= (0, vpn_1.rank)(vpn_1.VpnState.Created)).toBe(true);
7
+ expect((0, vpn_1.rank)(vpn_1.VpnState.Created) <= (0, vpn_1.rank)(vpn_1.VpnState.Provisioning)).toBe(true);
8
+ expect((0, vpn_1.rank)(vpn_1.VpnState.Provisioning) <= (0, vpn_1.rank)(vpn_1.VpnState.Running)).toBe(true);
9
+ expect((0, vpn_1.rank)(vpn_1.VpnState.Paused) <= (0, vpn_1.rank)(vpn_1.VpnState.Running)).toBe(true);
10
+ expect((0, vpn_1.rank)(vpn_1.VpnState.Running) <= (0, vpn_1.rank)(vpn_1.VpnState.Deprovisioning)).toBe(true);
11
+ expect((0, vpn_1.rank)(vpn_1.VpnState.Deprovisioning) <= (0, vpn_1.rank)(vpn_1.VpnState.Deleted)).toBe(true);
12
+ });
13
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mytmpvpn/mytmpvpn-common",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Common library for all MyTmpVpn related projects",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [