@mytmpvpn/mytmpvpn-common 2.1.1 → 2.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.
@@ -15,6 +15,7 @@ declare enum VpnType {
15
15
  interface VpnConfig {
16
16
  type: VpnType;
17
17
  maxPeanuts: number;
18
+ terminationDate?: Date;
18
19
  }
19
20
  interface VpnMetrics {
20
21
  duration: number;
@@ -35,6 +35,12 @@ function throwIfNotValidVpnId(vpnId) {
35
35
  throw new Error(`Incorrect vpnId: \'${vpnId}\'`);
36
36
  }
37
37
  }
38
+ function throwIfNotValidTerminationDate(terminationDate) {
39
+ // Make sure the given date is not in the past
40
+ if (terminationDate.getTime() < new Date().getTime()) {
41
+ throw new Error(`TerminationDate must be in the future: \'${terminationDate}\'`);
42
+ }
43
+ }
38
44
  function newVpn(region, config, state) {
39
45
  // e.g: ap-northeast-3
40
46
  if (!/[a-z]{2}-[a-z]+-[1-9]+/.test(region)) {
@@ -44,6 +50,9 @@ function newVpn(region, config, state) {
44
50
  const vpnId = `${dateToId(createdAt)}@${region}`;
45
51
  // Assert we will be able to deserialize later on...
46
52
  throwIfNotValidVpnId(vpnId);
53
+ if (config.terminationDate) {
54
+ throwIfNotValidTerminationDate(config.terminationDate);
55
+ }
47
56
  return {
48
57
  vpnId,
49
58
  createdAt,
@@ -20,7 +20,7 @@ describe('Testing Vpn constructors', () => {
20
20
  expect(() => (0, vpn_1.newVpn)('foo', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }, vpn_1.VpnState.Created))
21
21
  .toThrow(new Error("Incorrect region: \'foo\'"));
22
22
  });
23
- it('Should create vpn with all fields set', async () => {
23
+ it('Should create vpn with all mandatory fields set', async () => {
24
24
  const vpn = (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }, vpn_1.VpnState.Created);
25
25
  expect(vpn.createdAt).toBeDefined();
26
26
  expect(vpn.region).toEqual('az-test-7');
@@ -28,12 +28,26 @@ describe('Testing Vpn constructors', () => {
28
28
  expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard });
29
29
  expect(vpn.vpnId).toBeDefined();
30
30
  });
31
- it('Should deserialize a vpn with all fields set', async () => {
32
- const vpn = (0, vpn_1.getVpnFrom)('20030902012345678@az-test-7', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard });
31
+ it('Should create vpn with optional fields set', async () => {
32
+ const terminationDate = new Date(new Date().getTime() + 3600 * 1000);
33
+ const vpn = (0, vpn_1.newVpn)('az-test-7', {
34
+ maxPeanuts: -1,
35
+ type: vpn_1.VpnType.WireGuard,
36
+ terminationDate
37
+ }, vpn_1.VpnState.Created);
38
+ expect(vpn.createdAt).toBeDefined();
39
+ expect(vpn.region).toEqual('az-test-7');
40
+ expect(vpn.state).toEqual(vpn_1.VpnState.Created);
41
+ expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, terminationDate });
42
+ expect(vpn.vpnId).toBeDefined();
43
+ });
44
+ it('Should deserialize a vpn with all mandatory fields set', async () => {
45
+ const terminationDate = new Date(new Date().getTime() + 3600 * 1000);
46
+ const vpn = (0, vpn_1.getVpnFrom)('20030902012345678@az-test-7', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, terminationDate });
33
47
  expect(vpn.createdAt).toEqual(new Date('2003-09-02T01:23:45.678Z'));
34
48
  expect(vpn.region).toEqual('az-test-7');
35
49
  expect(vpn.state).toEqual(vpn_1.VpnState.Created);
36
- expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard });
50
+ expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, terminationDate });
37
51
  expect(vpn.vpnId).toBeDefined();
38
52
  });
39
53
  it('Should shout when vpnId is empty', async () => {
@@ -48,6 +62,11 @@ describe('Testing Vpn constructors', () => {
48
62
  expect(() => (0, vpn_1.getVpnFrom)('20030902012345678@test', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }))
49
63
  .toThrow(new Error("Incorrect vpnId: \'20030902012345678@test\'"));
50
64
  });
65
+ it('Should shout when termination is in the past', async () => {
66
+ const wrongTerminationDate = new Date(new Date().getTime() - 3600 * 1000);
67
+ expect(() => (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard, terminationDate: wrongTerminationDate }, vpn_1.VpnState.Created))
68
+ .toThrow(new Error(`TerminationDate must be in the future: \'${wrongTerminationDate}\'`));
69
+ });
51
70
  });
52
71
  describe('Testing vpnId to Wireguard file name functions ', () => {
53
72
  it('Should shout when vpnId is empty', async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mytmpvpn/mytmpvpn-common",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Common library for all MyTmpVpn related projects",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [