@mytmpvpn/mytmpvpn-common 1.1.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.
- package/README.md +47 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +5 -0
- package/dist/src/models/index.d.ts +2 -0
- package/dist/src/models/index.js +5 -0
- package/dist/src/models/peanuts.d.ts +12 -0
- package/dist/src/models/peanuts.js +8 -0
- package/dist/src/models/uservpn.d.ts +23 -0
- package/dist/src/models/uservpn.js +21 -0
- package/dist/src/models/vpn.d.ts +35 -0
- package/dist/src/models/vpn.js +80 -0
- package/dist/src/utils.d.ts +4 -0
- package/dist/src/utils.js +22 -0
- package/dist/test/models.test.d.ts +1 -0
- package/dist/test/models.test.js +13 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# mytmpvpn-common
|
|
2
|
+
|
|
3
|
+
Common library for the [`MyTmpVpn`](https://gitlab.com/mytmpvpn/) project.
|
|
4
|
+
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
Follow instructions from [`mytmpvpn-cli`](https://gitlab.com/mytmpvpn/mytmpvpn-cli)
|
|
8
|
+
|
|
9
|
+
## TODO: write down remaining sections
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Description
|
|
13
|
+
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
|
14
|
+
|
|
15
|
+
## Badges
|
|
16
|
+
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
|
17
|
+
|
|
18
|
+
## Visuals
|
|
19
|
+
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
|
26
|
+
|
|
27
|
+
## Support
|
|
28
|
+
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
|
29
|
+
|
|
30
|
+
## Roadmap
|
|
31
|
+
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
State if you are open to contributions and what your requirements are for accepting them.
|
|
35
|
+
|
|
36
|
+
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
|
37
|
+
|
|
38
|
+
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
|
39
|
+
|
|
40
|
+
## Authors and acknowledgment
|
|
41
|
+
Show your appreciation to those who have contributed to the project.
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
For open source projects, say how it is licensed.
|
|
45
|
+
|
|
46
|
+
## Project status
|
|
47
|
+
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
|
@@ -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,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
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mytmpvpn/mytmpvpn-common",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Common library for all MyTmpVpn related projects",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"/dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
"./*": "./dist/src/*.js"
|
|
11
|
+
},
|
|
12
|
+
"typesVersions": {
|
|
13
|
+
"*": {
|
|
14
|
+
"*": [
|
|
15
|
+
"dist/src/*"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc --build",
|
|
21
|
+
"watch": "tsc -w",
|
|
22
|
+
"test": "jest",
|
|
23
|
+
"link-deps": "echo \"No dependencies, noop; that's fine\"",
|
|
24
|
+
"clean": "rm -rf dist/"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "pierre.vigneras@gmail.com",
|
|
28
|
+
"license": "GNU AGPL",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/jest": "^27.5.2",
|
|
31
|
+
"jest": "^27.5.1",
|
|
32
|
+
"ts-jest": "^27.1.5",
|
|
33
|
+
"typescript": "^4.8.4"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"loglevel": "^1.8.1"
|
|
37
|
+
}
|
|
38
|
+
}
|