@digitraffic/common 2022.10.25-1 → 2022.10.31-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.
Files changed (283) hide show
  1. package/.editorconfig +9 -0
  2. package/.eslintignore +4 -0
  3. package/.eslintrc.json +27 -0
  4. package/.github/CODEOWNERS +2 -0
  5. package/.github/workflows/build.yml +36 -0
  6. package/.github/workflows/eslint.yml +38 -0
  7. package/.github/workflows/mirror.yml +15 -0
  8. package/.gitignore +29 -0
  9. package/.husky/pre-commit +4 -0
  10. package/.prettierrc.json +10 -0
  11. package/dist/aws/infra/api/integration.js +52 -0
  12. package/dist/aws/infra/api/response.js +61 -0
  13. package/dist/aws/infra/api/responses.js +82 -0
  14. package/dist/aws/infra/api/static-integration.js +54 -0
  15. package/dist/aws/infra/canaries/canary-alarm.js +26 -0
  16. package/dist/aws/infra/canaries/canary-keys.js +7 -0
  17. package/dist/aws/infra/canaries/canary-parameters.js +3 -0
  18. package/dist/aws/infra/canaries/canary-role.js +46 -0
  19. package/dist/aws/infra/canaries/canary.js +32 -0
  20. package/dist/aws/infra/canaries/database-canary.js +70 -0
  21. package/dist/aws/infra/canaries/database-checker.js +103 -0
  22. package/dist/aws/infra/canaries/url-canary.js +47 -0
  23. package/dist/aws/infra/canaries/url-checker.js +252 -0
  24. package/dist/aws/infra/documentation.js +95 -0
  25. package/dist/aws/infra/scheduler.js +31 -0
  26. package/dist/aws/infra/security-rule.js +39 -0
  27. package/dist/aws/infra/sqs-integration.js +93 -0
  28. package/dist/aws/infra/sqs-queue.js +130 -0
  29. package/dist/aws/infra/stack/lambda-configs.js +105 -0
  30. package/dist/aws/infra/stack/monitoredfunction.js +143 -0
  31. package/dist/aws/infra/stack/rest_apis.js +185 -0
  32. package/dist/aws/infra/stack/stack-checking-aspect.js +174 -0
  33. package/dist/aws/infra/stack/stack.js +67 -0
  34. package/dist/aws/infra/stack/subscription.js +42 -0
  35. package/dist/aws/infra/usage-plans.js +42 -0
  36. package/dist/aws/runtime/apikey.js +13 -0
  37. package/dist/aws/runtime/digitraffic-integration-response.js +26 -0
  38. package/dist/aws/runtime/environment.js +12 -0
  39. package/dist/aws/runtime/messaging.js +31 -0
  40. package/dist/aws/runtime/s3.js +30 -0
  41. package/dist/aws/runtime/secrets/dbsecret.js +96 -0
  42. package/dist/aws/runtime/secrets/proxy-holder.js +27 -0
  43. package/dist/aws/runtime/secrets/rds-holder.js +27 -0
  44. package/dist/aws/runtime/secrets/secret-holder.js +76 -0
  45. package/dist/aws/runtime/secrets/secret.js +43 -0
  46. package/dist/aws/types/errors.js +16 -0
  47. package/dist/aws/types/lambda-response.js +33 -0
  48. package/dist/aws/types/mediatypes.js +16 -0
  49. package/dist/aws/types/model-with-reference.js +3 -0
  50. package/dist/aws/types/proxytypes.js +3 -0
  51. package/dist/aws/types/tags.js +7 -0
  52. package/dist/database/cached.js +32 -0
  53. package/dist/database/database.js +70 -0
  54. package/dist/database/last-updated.js +54 -0
  55. package/dist/database/models.js +3 -0
  56. package/dist/marine/id_utils.js +33 -0
  57. package/dist/marine/rtz.js +3 -0
  58. package/dist/test/asserter.js +45 -0
  59. package/dist/test/db-testutils.js +31 -0
  60. package/dist/test/httpserver.js +74 -0
  61. package/dist/test/secret.js +25 -0
  62. package/dist/test/secrets-manager.js +59 -0
  63. package/dist/test/testutils.js +44 -0
  64. package/dist/types/either.js +3 -0
  65. package/dist/types/input-error.js +7 -0
  66. package/dist/types/language.js +10 -0
  67. package/dist/types/traffictype.js +13 -0
  68. package/dist/types/validator.js +14 -0
  69. package/dist/utils/api-model.js +129 -0
  70. package/dist/utils/base64.js +21 -0
  71. package/dist/utils/date-utils.js +34 -0
  72. package/dist/utils/geojson-types.js +18 -0
  73. package/dist/utils/geometry.js +164 -0
  74. package/dist/utils/retry.js +50 -0
  75. package/dist/utils/slack.js +25 -0
  76. package/dist/utils/utils.js +75 -0
  77. package/jest.config.js +15 -0
  78. package/package.json +15 -13
  79. package/src/@types/geojson-validation/index.d.ts +4 -0
  80. package/src/aws/infra/api/integration.ts +73 -0
  81. package/src/aws/infra/api/response.ts +67 -0
  82. package/src/aws/infra/api/responses.ts +124 -0
  83. package/src/aws/infra/api/static-integration.ts +62 -0
  84. package/src/aws/infra/canaries/canary-alarm.ts +31 -0
  85. package/src/aws/infra/canaries/canary-keys.ts +3 -0
  86. package/{aws/infra/canaries/canary-parameters.d.ts → src/aws/infra/canaries/canary-parameters.ts} +7 -6
  87. package/src/aws/infra/canaries/canary-role.ts +47 -0
  88. package/src/aws/infra/canaries/canary.ts +46 -0
  89. package/src/aws/infra/canaries/database-canary.ts +98 -0
  90. package/src/aws/infra/canaries/database-checker.ts +155 -0
  91. package/src/aws/infra/canaries/url-canary.ts +74 -0
  92. package/src/aws/infra/canaries/url-checker.ts +366 -0
  93. package/src/aws/infra/documentation.ts +124 -0
  94. package/src/aws/infra/scheduler.ts +59 -0
  95. package/src/aws/infra/security-rule.ts +38 -0
  96. package/src/aws/infra/sqs-integration.ts +102 -0
  97. package/src/aws/infra/sqs-queue.ts +148 -0
  98. package/src/aws/infra/stack/lambda-configs.ts +207 -0
  99. package/src/aws/infra/stack/monitoredfunction.ts +342 -0
  100. package/src/aws/infra/stack/rest_apis.ts +223 -0
  101. package/src/aws/infra/stack/stack-checking-aspect.ts +279 -0
  102. package/src/aws/infra/stack/stack.ts +145 -0
  103. package/src/aws/infra/stack/subscription.ts +58 -0
  104. package/src/aws/infra/usage-plans.ts +41 -0
  105. package/src/aws/runtime/apikey.ts +9 -0
  106. package/src/aws/runtime/digitraffic-integration-response.ts +28 -0
  107. package/src/aws/runtime/environment.ts +9 -0
  108. package/src/aws/runtime/messaging.ts +26 -0
  109. package/src/aws/runtime/s3.ts +44 -0
  110. package/src/aws/runtime/secrets/dbsecret.ts +116 -0
  111. package/src/aws/runtime/secrets/proxy-holder.ts +37 -0
  112. package/src/aws/runtime/secrets/rds-holder.ts +33 -0
  113. package/src/aws/runtime/secrets/secret-holder.ts +116 -0
  114. package/src/aws/runtime/secrets/secret.ts +50 -0
  115. package/src/aws/types/errors.ts +14 -0
  116. package/src/aws/types/lambda-response.ts +43 -0
  117. package/{aws/types/mediatypes.d.ts → src/aws/types/mediatypes.ts} +4 -3
  118. package/{aws/types/model-with-reference.d.ts → src/aws/types/model-with-reference.ts} +2 -1
  119. package/src/aws/types/proxytypes.ts +27 -0
  120. package/src/aws/types/tags.ts +3 -0
  121. package/src/database/cached.ts +35 -0
  122. package/src/database/database.ts +96 -0
  123. package/src/database/last-updated.ts +59 -0
  124. package/{database/models.d.ts → src/database/models.ts} +1 -0
  125. package/src/marine/id_utils.ts +30 -0
  126. package/src/marine/rtz.ts +57 -0
  127. package/src/test/asserter.ts +48 -0
  128. package/src/test/db-testutils.ts +44 -0
  129. package/src/test/httpserver.ts +96 -0
  130. package/src/test/secret.ts +23 -0
  131. package/src/test/secrets-manager.ts +34 -0
  132. package/src/test/testutils.ts +39 -0
  133. package/src/types/either.ts +3 -0
  134. package/src/types/input-error.ts +2 -0
  135. package/src/types/language.ts +3 -0
  136. package/src/types/traffictype.ts +8 -0
  137. package/src/types/validator.ts +10 -0
  138. package/src/utils/api-model.ts +133 -0
  139. package/src/utils/base64.ts +16 -0
  140. package/src/utils/date-utils.ts +30 -0
  141. package/src/utils/geojson-types.ts +22 -0
  142. package/src/utils/geometry.ts +164 -0
  143. package/src/utils/retry.ts +49 -0
  144. package/src/utils/slack.ts +22 -0
  145. package/src/utils/utils.ts +105 -0
  146. package/test/marine/id_utils.test.ts +57 -0
  147. package/test/promise/promise.test.ts +143 -0
  148. package/test/secrets/dbsecret.test.ts +59 -0
  149. package/test/secrets/secret-holder.test.ts +143 -0
  150. package/test/secrets/secret.test.ts +49 -0
  151. package/test/test/httpserver.test.ts +128 -0
  152. package/test/utils/date-utils.test.ts +28 -0
  153. package/test/utils/geometry.test.ts +29 -0
  154. package/test/utils/utils.test.ts +64 -0
  155. package/tsconfig.eslint.json +4 -0
  156. package/tsconfig.json +22 -0
  157. package/yarn.lock +4060 -0
  158. package/aws/infra/api/integration.d.ts +0 -21
  159. package/aws/infra/api/integration.js +0 -52
  160. package/aws/infra/api/response.d.ts +0 -22
  161. package/aws/infra/api/response.js +0 -61
  162. package/aws/infra/api/responses.d.ts +0 -39
  163. package/aws/infra/api/responses.js +0 -79
  164. package/aws/infra/api/static-integration.d.ts +0 -15
  165. package/aws/infra/api/static-integration.js +0 -54
  166. package/aws/infra/canaries/canary-alarm.d.ts +0 -6
  167. package/aws/infra/canaries/canary-alarm.js +0 -26
  168. package/aws/infra/canaries/canary-parameters.js +0 -3
  169. package/aws/infra/canaries/canary-role.d.ts +0 -6
  170. package/aws/infra/canaries/canary-role.js +0 -46
  171. package/aws/infra/canaries/canary.d.ts +0 -8
  172. package/aws/infra/canaries/canary.js +0 -32
  173. package/aws/infra/canaries/database-canary.d.ts +0 -18
  174. package/aws/infra/canaries/database-canary.js +0 -55
  175. package/aws/infra/canaries/database-checker.d.ts +0 -21
  176. package/aws/infra/canaries/database-checker.js +0 -109
  177. package/aws/infra/canaries/url-canary.d.ts +0 -19
  178. package/aws/infra/canaries/url-canary.js +0 -46
  179. package/aws/infra/canaries/url-checker.d.ts +0 -46
  180. package/aws/infra/canaries/url-checker.js +0 -238
  181. package/aws/infra/documentation.d.ts +0 -56
  182. package/aws/infra/documentation.js +0 -95
  183. package/aws/infra/scheduler.d.ts +0 -12
  184. package/aws/infra/scheduler.js +0 -31
  185. package/aws/infra/security-rule.d.ts +0 -12
  186. package/aws/infra/security-rule.js +0 -39
  187. package/aws/infra/sqs-integration.d.ts +0 -7
  188. package/aws/infra/sqs-integration.js +0 -93
  189. package/aws/infra/sqs-queue.d.ts +0 -16
  190. package/aws/infra/sqs-queue.js +0 -130
  191. package/aws/infra/stack/lambda-configs.d.ts +0 -72
  192. package/aws/infra/stack/lambda-configs.js +0 -93
  193. package/aws/infra/stack/monitoredfunction.d.ts +0 -84
  194. package/aws/infra/stack/monitoredfunction.js +0 -135
  195. package/aws/infra/stack/rest_apis.d.ts +0 -41
  196. package/aws/infra/stack/rest_apis.js +0 -185
  197. package/aws/infra/stack/stack-checking-aspect.d.ts +0 -21
  198. package/aws/infra/stack/stack-checking-aspect.js +0 -174
  199. package/aws/infra/stack/stack.d.ts +0 -44
  200. package/aws/infra/stack/stack.js +0 -60
  201. package/aws/infra/stack/subscription.d.ts +0 -17
  202. package/aws/infra/stack/subscription.js +0 -41
  203. package/aws/infra/usage-plans.d.ts +0 -15
  204. package/aws/infra/usage-plans.js +0 -42
  205. package/aws/runtime/apikey.d.ts +0 -2
  206. package/aws/runtime/apikey.js +0 -13
  207. package/aws/runtime/digitraffic-integration-response.d.ts +0 -8
  208. package/aws/runtime/digitraffic-integration-response.js +0 -26
  209. package/aws/runtime/environment.d.ts +0 -1
  210. package/aws/runtime/environment.js +0 -12
  211. package/aws/runtime/messaging.d.ts +0 -10
  212. package/aws/runtime/messaging.js +0 -31
  213. package/aws/runtime/s3.d.ts +0 -2
  214. package/aws/runtime/s3.js +0 -30
  215. package/aws/runtime/secrets/dbsecret.d.ts +0 -54
  216. package/aws/runtime/secrets/dbsecret.js +0 -96
  217. package/aws/runtime/secrets/proxy-holder.d.ts +0 -9
  218. package/aws/runtime/secrets/proxy-holder.js +0 -26
  219. package/aws/runtime/secrets/rds-holder.d.ts +0 -9
  220. package/aws/runtime/secrets/rds-holder.js +0 -26
  221. package/aws/runtime/secrets/secret-holder.d.ts +0 -26
  222. package/aws/runtime/secrets/secret-holder.js +0 -73
  223. package/aws/runtime/secrets/secret.d.ts +0 -8
  224. package/aws/runtime/secrets/secret.js +0 -43
  225. package/aws/types/errors.d.ts +0 -4
  226. package/aws/types/errors.js +0 -9
  227. package/aws/types/lambda-response.d.ts +0 -12
  228. package/aws/types/lambda-response.js +0 -28
  229. package/aws/types/mediatypes.js +0 -15
  230. package/aws/types/model-with-reference.js +0 -3
  231. package/aws/types/proxytypes.d.ts +0 -26
  232. package/aws/types/proxytypes.js +0 -3
  233. package/aws/types/tags.d.ts +0 -2
  234. package/aws/types/tags.js +0 -7
  235. package/database/cached.d.ts +0 -7
  236. package/database/cached.js +0 -32
  237. package/database/database.d.ts +0 -19
  238. package/database/database.js +0 -62
  239. package/database/last-updated.d.ts +0 -16
  240. package/database/last-updated.js +0 -54
  241. package/database/models.js +0 -3
  242. package/index.d.ts +0 -1
  243. package/index.js +0 -18
  244. package/marine/id_utils.d.ts +0 -3
  245. package/marine/id_utils.js +0 -33
  246. package/marine/rtz.d.ts +0 -48
  247. package/marine/rtz.js +0 -3
  248. package/test/asserter.d.ts +0 -11
  249. package/test/asserter.js +0 -45
  250. package/test/db-testutils.d.ts +0 -2
  251. package/test/db-testutils.js +0 -31
  252. package/test/httpserver.d.ts +0 -18
  253. package/test/httpserver.js +0 -67
  254. package/test/secret.d.ts +0 -3
  255. package/test/secret.js +0 -25
  256. package/test/secrets-manager.d.ts +0 -9
  257. package/test/secrets-manager.js +0 -59
  258. package/test/testutils.d.ts +0 -12
  259. package/test/testutils.js +0 -44
  260. package/types/input-error.d.ts +0 -2
  261. package/types/input-error.js +0 -7
  262. package/types/language.d.ts +0 -5
  263. package/types/language.js +0 -10
  264. package/types/traffictype.d.ts +0 -8
  265. package/types/traffictype.js +0 -13
  266. package/types/validator.d.ts +0 -4
  267. package/types/validator.js +0 -14
  268. package/utils/api-model.d.ts +0 -87
  269. package/utils/api-model.js +0 -129
  270. package/utils/base64.d.ts +0 -12
  271. package/utils/base64.js +0 -21
  272. package/utils/date-utils.d.ts +0 -17
  273. package/utils/date-utils.js +0 -34
  274. package/utils/geojson-types.d.ts +0 -14
  275. package/utils/geojson-types.js +0 -18
  276. package/utils/geometry.d.ts +0 -36
  277. package/utils/geometry.js +0 -140
  278. package/utils/retry.d.ts +0 -13
  279. package/utils/retry.js +0 -50
  280. package/utils/slack.d.ts +0 -5
  281. package/utils/slack.js +0 -25
  282. package/utils/utils.d.ts +0 -30
  283. package/utils/utils.js +0 -64
@@ -0,0 +1,143 @@
1
+ import { mockSecret, stubSecretsManager } from "../../src/test/secrets-manager";
2
+
3
+ import * as sinon from "sinon";
4
+
5
+ const SECRET_WITH_PREFIX = {
6
+ "prefix.value": "value",
7
+ "prefix.name": "name",
8
+ "wrong.value": "value",
9
+ username: "DB_USER",
10
+ };
11
+ const SECRET_EMPTY = {};
12
+
13
+ const stubSM = stubSecretsManager();
14
+
15
+ import { SecretHolder } from "../../src/aws/runtime/secrets/secret-holder";
16
+ import { DatabaseEnvironmentKeys } from "../../src/aws/runtime/secrets/dbsecret";
17
+
18
+ describe("SecretHolder - tests", () => {
19
+ beforeEach(() => {
20
+ process.env["SECRET_ID"] = "test-id";
21
+ });
22
+
23
+ afterEach(() => {
24
+ sinon.restore();
25
+ sinon.reset();
26
+ delete process.env[DatabaseEnvironmentKeys.DB_USER];
27
+ });
28
+
29
+ test("get - no secret", async () => {
30
+ mockSecret(null);
31
+
32
+ const holder = SecretHolder.create();
33
+ await expect(async () => {
34
+ await holder.get();
35
+ }).rejects.toThrowError("No secret found!");
36
+ });
37
+
38
+ test("get - empty secret", async () => {
39
+ mockSecret(SECRET_EMPTY);
40
+
41
+ const holder = SecretHolder.create();
42
+ const secret = await holder.get();
43
+
44
+ expect(secret).toEqual(SECRET_EMPTY);
45
+ });
46
+
47
+ test("get - no prefix", async () => {
48
+ mockSecret(SECRET_WITH_PREFIX);
49
+
50
+ const holder = SecretHolder.create();
51
+ const secret = await holder.get();
52
+
53
+ expect(secret).toEqual(SECRET_WITH_PREFIX);
54
+ });
55
+
56
+ test("get - check keys - not found", async () => {
57
+ mockSecret(SECRET_WITH_PREFIX);
58
+
59
+ const holder = SecretHolder.create("", ["not_found"]);
60
+ await expect(async () => {
61
+ await holder.get();
62
+ }).rejects.toThrow();
63
+ });
64
+
65
+ test("get - check keys - found", async () => {
66
+ mockSecret(SECRET_WITH_PREFIX);
67
+
68
+ const holder = SecretHolder.create("", ["prefix.value", "username"]);
69
+
70
+ await holder.get();
71
+ });
72
+
73
+ test("setDatabaseCredentials - no prefix", async () => {
74
+ mockSecret(SECRET_WITH_PREFIX);
75
+
76
+ const holder = SecretHolder.create();
77
+ expect(process.env[DatabaseEnvironmentKeys.DB_USER]).toBeUndefined();
78
+
79
+ await holder.setDatabaseCredentials();
80
+ expect(process.env[DatabaseEnvironmentKeys.DB_USER]).toEqual(
81
+ SECRET_WITH_PREFIX.username
82
+ );
83
+ });
84
+
85
+ test("setDatabaseCredentials - with prefix", async () => {
86
+ mockSecret(SECRET_WITH_PREFIX);
87
+
88
+ const holder = SecretHolder.create("prefix");
89
+ expect(process.env[DatabaseEnvironmentKeys.DB_USER]).toBeUndefined();
90
+
91
+ await holder.setDatabaseCredentials();
92
+ expect(process.env[DatabaseEnvironmentKeys.DB_USER]).toEqual(
93
+ SECRET_WITH_PREFIX.username
94
+ );
95
+ });
96
+
97
+ test("getSecret - with prefix", async () => {
98
+ mockSecret(SECRET_WITH_PREFIX);
99
+
100
+ const holder = SecretHolder.create("prefix");
101
+ const secret = await holder.get();
102
+
103
+ expect(secret).toEqual({
104
+ value: "value",
105
+ name: "name",
106
+ });
107
+ });
108
+
109
+ test("get - ttl - do not fetch", async () => {
110
+ mockSecret(SECRET_WITH_PREFIX);
111
+
112
+ const holder = SecretHolder.create();
113
+
114
+ const callCount = stubSM.callCount;
115
+
116
+ await holder.get();
117
+ expect(stubSM.callCount).toEqual(callCount + 1);
118
+
119
+ // gets cached secret
120
+ await holder.get();
121
+ expect(stubSM.callCount).toEqual(callCount + 1);
122
+ });
123
+
124
+ test("get - ttl - fetch", async () => {
125
+ mockSecret(SECRET_WITH_PREFIX);
126
+
127
+ const holder = new SecretHolder("", "", [], {
128
+ ttl: 1,
129
+ });
130
+
131
+ const callCount = stubSM.callCount;
132
+
133
+ await holder.get();
134
+ expect(stubSM.callCount).toEqual(callCount + 1);
135
+
136
+ // cache expires, fetches secret again
137
+ const start = Date.now();
138
+ while (Date.now() < start + 2000);
139
+
140
+ await holder.get();
141
+ expect(stubSM.callCount).toEqual(callCount + 2);
142
+ });
143
+ });
@@ -0,0 +1,49 @@
1
+ import {mockSecret, stubSecretsManager} from "../../src/test/secrets-manager";
2
+
3
+ import * as sinon from 'sinon';
4
+
5
+ const SECRET_ID = "test_secret";
6
+ const SECRET_WITH_PREFIX = {
7
+ "prefix.value" : "value",
8
+ "prefix.name" : "name",
9
+ "wrong.value" : "value",
10
+ };
11
+ const SECRET_EMPTY = {};
12
+
13
+ stubSecretsManager();
14
+
15
+ import {getSecret} from "../../src/aws/runtime/secrets/secret";
16
+
17
+ describe('secret - test', () => {
18
+ afterEach(() => {
19
+ sinon.restore();
20
+ });
21
+
22
+ test('getSecret - no secret', async () => {
23
+ mockSecret(null);
24
+ await expect(async () => {
25
+ await getSecret(SECRET_ID, '');
26
+ }).rejects.toThrowError("No secret found!");
27
+ });
28
+
29
+ test('getSecret - empty secret', async () => {
30
+ mockSecret(SECRET_EMPTY);
31
+ const secret = await getSecret(SECRET_ID, '');
32
+ expect(secret).toEqual(SECRET_EMPTY);
33
+ });
34
+
35
+ test('getSecret - no prefix', async () => {
36
+ mockSecret(SECRET_WITH_PREFIX);
37
+ const secret = await getSecret(SECRET_ID, '');
38
+ expect(secret).toEqual(SECRET_WITH_PREFIX);
39
+ });
40
+
41
+ test('getSecret - with prefix', async () => {
42
+ mockSecret(SECRET_WITH_PREFIX);
43
+ const secret = await getSecret(SECRET_ID, 'prefix');
44
+ expect(secret).toEqual({
45
+ value: "value",
46
+ name: "name",
47
+ });
48
+ });
49
+ });
@@ -0,0 +1,128 @@
1
+ import {
2
+ TestHttpServer,
3
+ ListenProperties,
4
+ ERROR_NO_MATCH,
5
+ ERRORCODE_NOT_FOUND,
6
+ } from "../../src/test/httpserver";
7
+ import { IncomingMessage } from "http";
8
+ import http = require("http");
9
+
10
+ const DEFAULT_PATH = "/";
11
+ const PORT = 8091;
12
+
13
+ const DEFAULT_PROPS = {
14
+ "/": () => "",
15
+ };
16
+
17
+ describe("TestHttpServer - test", () => {
18
+ async function withServer(
19
+ fn: (server: TestHttpServer) => Promise<void>,
20
+ props: ListenProperties = DEFAULT_PROPS,
21
+ statusCode = 200
22
+ ) {
23
+ const server = new TestHttpServer();
24
+
25
+ server.listen(PORT, props, false, statusCode);
26
+
27
+ try {
28
+ await fn(server);
29
+ } finally {
30
+ server.close();
31
+ }
32
+ }
33
+
34
+ function sendGetRequest(path = DEFAULT_PATH): Promise<IncomingMessage> {
35
+ return sendRequest("GET", path);
36
+ }
37
+
38
+ function sendPostRequest(
39
+ path = DEFAULT_PATH,
40
+ body: string
41
+ ): Promise<IncomingMessage> {
42
+ return sendRequest("POST", path, body);
43
+ }
44
+
45
+ function sendRequest(
46
+ method: string,
47
+ path: string,
48
+ body?: string
49
+ ): Promise<IncomingMessage> {
50
+ return new Promise((resolve, reject) => {
51
+ const request = http.request(
52
+ {
53
+ path,
54
+ port: PORT,
55
+ method,
56
+ },
57
+ (response: IncomingMessage) => {
58
+ response.on("data", () => {
59
+ // do nothing
60
+ });
61
+
62
+ //the whole response has been received, so we just print it out here
63
+ response.on("end", () => {
64
+ resolve(response);
65
+ });
66
+
67
+ response.on("error", (error: Error) => {
68
+ reject(error);
69
+ });
70
+ }
71
+ );
72
+
73
+ if (method === "POST") {
74
+ request.write(body);
75
+ }
76
+ request.end();
77
+ });
78
+ }
79
+
80
+ test("no calls", () => {
81
+ return withServer(async (server: TestHttpServer) => {
82
+ await expect(server.getCallCount()).toEqual(0);
83
+ });
84
+ });
85
+
86
+ test("one get", async () => {
87
+ await withServer(async (server: TestHttpServer) => {
88
+ await sendGetRequest();
89
+
90
+ expect(server.getCallCount()).toEqual(1);
91
+ });
92
+ });
93
+
94
+ test("one get - no MATCH", async () => {
95
+ await withServer(async (server: TestHttpServer) => {
96
+ const response = await sendGetRequest("/no-match");
97
+
98
+ expect(server.getCallCount()).toEqual(1);
99
+ expect(server.getRequestBody(0)).toEqual(ERROR_NO_MATCH);
100
+ expect(response.statusCode).toEqual(ERRORCODE_NOT_FOUND);
101
+ });
102
+ });
103
+
104
+ test("get - error 405", async () => {
105
+ const ERROR_CODE = 405;
106
+
107
+ await withServer(
108
+ async (server: TestHttpServer) => {
109
+ const response = await sendGetRequest();
110
+
111
+ expect(server.getCallCount()).toEqual(1);
112
+ expect(response.statusCode).toEqual(ERROR_CODE);
113
+ },
114
+ DEFAULT_PROPS,
115
+ ERROR_CODE
116
+ );
117
+ });
118
+
119
+ test("one post", async () => {
120
+ await withServer(async (server: TestHttpServer) => {
121
+ const testBody = "Testing123!";
122
+ await sendPostRequest(DEFAULT_PATH, testBody);
123
+
124
+ expect(server.getCallCount()).toEqual(1);
125
+ expect(server.getRequestBody(0)).toEqual(testBody);
126
+ });
127
+ });
128
+ });
@@ -0,0 +1,28 @@
1
+ import moment from "moment";
2
+ import * as CommonDateUtils from "../../src/utils/date-utils";
3
+
4
+ const ISO = "2022-01-02T01:02:03.004Z";
5
+
6
+ describe('CommonDateUtilsTest', () => {
7
+
8
+ test('dateFromIsoString', () => {
9
+ const parsed = CommonDateUtils.dateFromIsoString(ISO);
10
+ expect(parsed.toISOString()).toEqual(ISO);
11
+ });
12
+
13
+ test('dateFromIsoString fails', () => {
14
+ expect(() => CommonDateUtils.dateFromIsoString(ISO + "foobar")).toThrowError();
15
+ });
16
+
17
+ test('countDiffMs', () => {
18
+ const start = new Date();
19
+ const end = moment(start).add(1234, 'milliseconds').toDate();
20
+ expect (CommonDateUtils.countDiffMs(start, end)).toEqual(1234);
21
+ });
22
+
23
+ test('countDiffMs', () => {
24
+ const start = new Date();
25
+ const end = moment(start).add(1234, 'seconds').toDate();
26
+ expect (CommonDateUtils.countDiffInSeconds(start, end)).toEqual(1234);
27
+ });
28
+ });
@@ -0,0 +1,29 @@
1
+ import {Asserter} from "../../src/test/asserter";
2
+ import * as Geometry from '../../src/utils/geometry';
3
+
4
+ const TAMPERE_WGS84_X = 23.761290078;
5
+ const TAMPERE_WGS84_Y = 61.497742570;
6
+
7
+ const KUOPIO_WGS84_X = 27.688935;
8
+ const KUOPIO_WGS84_Y = 62.892983;
9
+ const TAMPERE_KUOPIO_DISTANCE_KM = 255.8;
10
+
11
+ describe('CommonGeometryTest', () => {
12
+
13
+ test('distanceBetweenWGS84PointsInKm', () => {
14
+ Asserter.assertToBeCloseTo(Geometry.distanceBetweenPositionsInKm([TAMPERE_WGS84_X, TAMPERE_WGS84_Y], [KUOPIO_WGS84_X, KUOPIO_WGS84_Y]),TAMPERE_KUOPIO_DISTANCE_KM, 0.5);
15
+ console.info(Geometry.distanceBetweenPositionsInKm([TAMPERE_WGS84_X, TAMPERE_WGS84_Y], [KUOPIO_WGS84_X, KUOPIO_WGS84_Y]));
16
+ });
17
+
18
+ test('distanceBetweenWGS84PointsInKm', () => {
19
+ Asserter.assertToBeCloseTo(Geometry.distanceBetweenPositionsInM([TAMPERE_WGS84_X, TAMPERE_WGS84_Y], [KUOPIO_WGS84_X, KUOPIO_WGS84_Y]),TAMPERE_KUOPIO_DISTANCE_KM*1000, 500);
20
+ console.info(Geometry.distanceBetweenPositionsInM([TAMPERE_WGS84_X, TAMPERE_WGS84_Y], [KUOPIO_WGS84_X, KUOPIO_WGS84_Y]));
21
+ });
22
+
23
+ test('areDistinctPositions', () => {
24
+ expect(Geometry.areDistinctPositions([1,2],[1,2])).toBe(false);
25
+ expect(Geometry.areDistinctPositions([1.1,2.2],[1.1,2.2])).toBe(false);
26
+ expect(Geometry.areDistinctPositions([1,2.1],[1,2])).toBe(true);
27
+ expect(Geometry.areDistinctPositions([1,2],[1,2.000000000000001])).toBe(true);
28
+ });
29
+ });
@@ -0,0 +1,64 @@
1
+ import * as ArrayUtils from "../../src/utils/utils";
2
+
3
+ describe("ArrayUtils", () => {
4
+ test("bothArraysHasSameValues", () => {
5
+ expect(ArrayUtils.bothArraysHasSameValues([], [])).toEqual(true);
6
+ expect(ArrayUtils.bothArraysHasSameValues(["a"], ["a"])).toEqual(true);
7
+ expect(ArrayUtils.bothArraysHasSameValues(["a"], ["a", "a"])).toEqual(
8
+ true
9
+ );
10
+ expect(
11
+ ArrayUtils.bothArraysHasSameValues(["a", "a"], ["a", "a"])
12
+ ).toEqual(true);
13
+
14
+ expect(ArrayUtils.bothArraysHasSameValues(null, null)).toEqual(true);
15
+ expect(
16
+ ArrayUtils.bothArraysHasSameValues(undefined, undefined)
17
+ ).toEqual(true);
18
+ expect(ArrayUtils.bothArraysHasSameValues(null, undefined)).toEqual(
19
+ true
20
+ );
21
+ expect(ArrayUtils.bothArraysHasSameValues(["a"], undefined)).toEqual(
22
+ false
23
+ );
24
+ expect(ArrayUtils.bothArraysHasSameValues(["a"], null)).toEqual(false);
25
+ /* eslint-enable */
26
+ expect(
27
+ ArrayUtils.bothArraysHasSameValues(["a", "b"], ["a", "a"])
28
+ ).toEqual(false);
29
+ expect(
30
+ ArrayUtils.bothArraysHasSameValues(["a", "a", "a"], ["a", "b", "c"])
31
+ ).toEqual(false);
32
+
33
+ const o1 = { a: 1, b: 2 };
34
+ const o2 = { a: 1, b: 2 };
35
+ // Objects are references to same
36
+ expect(ArrayUtils.bothArraysHasSameValues([o1], [o1])).toEqual(true);
37
+ // Object's are not the same but the contents are the same
38
+ expect(ArrayUtils.bothArraysHasSameValues([o1], [o2])).toEqual(false);
39
+ });
40
+
41
+ test("getFirst - empty throws", () => {
42
+ expect(() => {
43
+ ArrayUtils.getFirst([]);
44
+ }).toThrow();
45
+ });
46
+
47
+ test("getFirst - two objects", () => {
48
+ expect(ArrayUtils.getFirst([1, 2])).toEqual(1);
49
+ });
50
+
51
+ test("getFirst - two objects with sort function", () => {
52
+ expect(ArrayUtils.getFirst([1, 2], (a) => -a)).toEqual(2);
53
+ });
54
+
55
+ test("getLast - empty throws", () => {
56
+ expect(() => {
57
+ ArrayUtils.getLast([]);
58
+ }).toThrow();
59
+ });
60
+
61
+ test("getLast - two objects", () => {
62
+ expect(ArrayUtils.getLast([1, 2])).toEqual(2);
63
+ });
64
+ });
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": ["src/**/*.ts", "test/**/*.ts"],
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "commonjs",
5
+ "lib": ["es2020"],
6
+ "outDir": "dist",
7
+ "declaration": true,
8
+ "sourceMap": true,
9
+ "moduleResolution": "node",
10
+ "strict": true,
11
+ "exactOptionalPropertyTypes": false,
12
+ "esModuleInterop": true,
13
+ "forceConsistentCasingInFileNames": true,
14
+ "skipLibCheck": true,
15
+
16
+ "noImplicitReturns": true,
17
+ "noFallthroughCasesInSwitch": true,
18
+ "experimentalDecorators": true
19
+ },
20
+ "include": ["src/**/*.ts"],
21
+ "$schema": "https://json.schemastore.org/tsconfig"
22
+ }