@digitraffic/common 2022.12.1-1 → 2022.12.22-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 (45) hide show
  1. package/dist/aws/infra/api/handler-factory.d.ts +9 -7
  2. package/dist/aws/infra/api/handler-factory.js +18 -12
  3. package/dist/aws/infra/api/integration.d.ts +2 -2
  4. package/dist/aws/infra/api/responses.js +4 -4
  5. package/dist/aws/infra/canaries/canary-parameters.d.ts +2 -2
  6. package/dist/aws/infra/canaries/canary-role.d.ts +1 -1
  7. package/dist/aws/infra/canaries/canary-role.js +3 -5
  8. package/dist/aws/infra/documentation.d.ts +1 -1
  9. package/dist/aws/infra/documentation.js +3 -3
  10. package/dist/aws/infra/stack/monitoredfunction.d.ts +4 -4
  11. package/dist/aws/infra/stack/monitoredfunction.js +2 -2
  12. package/dist/aws/infra/stack/stack-checking-aspect.js +6 -8
  13. package/dist/aws/runtime/dt-logger.d.ts +28 -0
  14. package/dist/aws/runtime/dt-logger.js +41 -0
  15. package/dist/aws/runtime/secrets/dbsecret.d.ts +4 -4
  16. package/dist/aws/runtime/secrets/dbsecret.js +1 -1
  17. package/dist/aws/types/proxytypes.d.ts +4 -4
  18. package/dist/database/cached.d.ts +5 -0
  19. package/dist/database/cached.js +10 -4
  20. package/dist/database/last-updated.js +9 -7
  21. package/dist/marine/rtz.d.ts +20 -20
  22. package/dist/test/asserter.d.ts +6 -4
  23. package/dist/test/asserter.js +1 -1
  24. package/dist/test/httpserver.d.ts +1 -3
  25. package/dist/test/httpserver.js +10 -4
  26. package/dist/types/either.d.ts +4 -4
  27. package/package.json +1 -1
  28. package/src/aws/infra/api/handler-factory.ts +35 -21
  29. package/src/aws/infra/api/integration.ts +2 -2
  30. package/src/aws/infra/api/responses.ts +6 -4
  31. package/src/aws/infra/canaries/canary-parameters.ts +3 -3
  32. package/src/aws/infra/canaries/canary-role.ts +20 -10
  33. package/src/aws/infra/documentation.ts +42 -24
  34. package/src/aws/infra/stack/monitoredfunction.ts +6 -6
  35. package/src/aws/infra/stack/stack-checking-aspect.ts +15 -15
  36. package/src/aws/infra/stacks/db-stack.ts +2 -1
  37. package/src/aws/runtime/dt-logger.ts +61 -0
  38. package/src/aws/runtime/secrets/dbsecret.ts +5 -5
  39. package/src/aws/types/proxytypes.ts +14 -14
  40. package/src/database/cached.ts +32 -13
  41. package/src/database/last-updated.ts +75 -31
  42. package/src/marine/rtz.ts +29 -29
  43. package/src/test/asserter.ts +21 -11
  44. package/src/test/httpserver.ts +17 -8
  45. package/src/types/either.ts +8 -2
@@ -3,11 +3,11 @@
3
3
  *
4
4
  * Not fully described, extend if necessary.
5
5
  */
6
- export type ProxyLambdaResponse = {
7
- readonly statusCode: number
8
- readonly body: string
9
- readonly headers?: Record<string, string>
10
- readonly multiValueHeaders?: Record<string, string[]>
6
+ export interface ProxyLambdaResponse {
7
+ readonly statusCode: number;
8
+ readonly body: string;
9
+ readonly headers?: Record<string, string>;
10
+ readonly multiValueHeaders?: Record<string, string[]>;
11
11
  }
12
12
 
13
13
  /**
@@ -15,13 +15,13 @@ export type ProxyLambdaResponse = {
15
15
  *
16
16
  * Not fully described, extend if necessary.
17
17
  */
18
- export type ProxyLambdaRequest = {
19
- readonly resource: string
20
- readonly path: string
21
- readonly httpMethod: string
22
- readonly headers: Record<string, string>
23
- readonly multiValueHeaders: Record<string, string[]>
24
- readonly queryStringParameters: Record<string, string>
25
- readonly multiValueQueryStringParameters: Record<string, string[]>
26
- readonly body?: string
18
+ export interface ProxyLambdaRequest {
19
+ readonly resource: string;
20
+ readonly path: string;
21
+ readonly httpMethod: string;
22
+ readonly headers: Record<string, string>;
23
+ readonly multiValueHeaders: Record<string, string[]>;
24
+ readonly queryStringParameters: Record<string, string>;
25
+ readonly multiValueQueryStringParameters: Record<string, string[]>;
26
+ readonly body?: string;
27
27
  }
@@ -1,35 +1,54 @@
1
- import {PreparedStatement} from "pg-promise";
2
- import {DTDatabase, DTTransaction} from "./database";
1
+ import { PreparedStatement } from "pg-promise";
2
+ import { DTDatabase, DTTransaction } from "./database";
3
3
 
4
- const SQL_UPDATE_CACHE_VALUE =
5
- `insert into cached_json(cache_id, content, last_updated)
4
+ export interface CachedValue<T> {
5
+ content: T;
6
+ last_updated: Date;
7
+ }
8
+
9
+ const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_updated)
6
10
  values ($1, $2, now())
7
11
  on conflict(cache_id) do
8
12
  update set content = $2, last_updated = now()`;
9
13
 
10
- const SQL_GET_CACHE_VALUE =
11
- `select content, last_updated from cached_json
14
+ const SQL_GET_CACHE_VALUE = `select content, last_updated from cached_json
12
15
  where cache_id = $1`;
13
16
 
14
17
  const PS_UPDATE_CACHE_VALUE = new PreparedStatement({
15
- name: 'update-cache-value',
18
+ name: "update-cache-value",
16
19
  text: SQL_UPDATE_CACHE_VALUE,
17
20
  });
18
21
 
19
22
  const PS_GET_CACHE_VALUE = new PreparedStatement({
20
- name: 'get-cache-value',
23
+ name: "get-cache-value",
21
24
  text: SQL_GET_CACHE_VALUE,
22
25
  });
23
26
 
24
27
  export enum JSON_CACHE_KEY {
25
- NAUTICAL_WARNINGS_ACTIVE = 'nautical-warnings-active',
26
- NAUTICAL_WARNINGS_ARCHIVED = 'nautical-warnings-archived'
28
+ NAUTICAL_WARNINGS_ACTIVE = "nautical-warnings-active",
29
+ NAUTICAL_WARNINGS_ARCHIVED = "nautical-warnings-archived",
27
30
  }
28
31
 
29
- export function updateCachedJson<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY, value: T): Promise<null> {
32
+ export function updateCachedJson<T>(
33
+ db: DTDatabase | DTTransaction,
34
+ cacheKey: JSON_CACHE_KEY,
35
+ value: T
36
+ ): Promise<null> {
30
37
  return db.none(PS_UPDATE_CACHE_VALUE, [cacheKey, value]);
31
38
  }
32
39
 
33
- export function getJsonFromCache<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY): Promise<T | null> {
34
- return db.oneOrNone(PS_GET_CACHE_VALUE, [cacheKey]).then(value => value?.content ?? null);
40
+ export function getJsonFromCache<T>(
41
+ db: DTDatabase | DTTransaction,
42
+ cacheKey: JSON_CACHE_KEY
43
+ ): Promise<T | null> {
44
+ return db
45
+ .oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey])
46
+ .then((value) => value?.content ?? null);
47
+ }
48
+
49
+ export function getFromCache<T>(
50
+ db: DTDatabase | DTTransaction,
51
+ cacheKey: JSON_CACHE_KEY
52
+ ): Promise<CachedValue<T> | null> {
53
+ return db.oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey]);
35
54
  }
@@ -1,59 +1,103 @@
1
- import {DTDatabase, DTTransaction} from "./database";
1
+ import { DTDatabase, DTTransaction } from "./database";
2
2
 
3
3
  export enum DataType {
4
- VS_DATEX2="VS_DATEX2",
5
- COUNTING_SITES_DATA="COUNTING_SITES_DATA",
6
- COUNTING_SITES_METADATA="COUNTING_SITES_METADATA",
7
- COUNTING_SITES_METADATA_CHECK="COUNTING_SITES_METADATA_CHECK",
8
- MAINTENANCE_TRACKING_DATA_CHECKED="MAINTENANCE_TRACKING_DATA_CHECKED",
9
- PERMIT_DATA="PERMIT_DATA",
10
- PERMIT_DATA_CHECK="PERMIT_DATA_CHECK",
4
+ VS_DATEX2 = "VS_DATEX2",
5
+ COUNTING_SITES_DATA = "COUNTING_SITES_DATA",
6
+ COUNTING_SITES_METADATA = "COUNTING_SITES_METADATA",
7
+ COUNTING_SITES_METADATA_CHECK = "COUNTING_SITES_METADATA_CHECK",
8
+ MAINTENANCE_TRACKING_DATA_CHECKED = "MAINTENANCE_TRACKING_DATA_CHECKED",
9
+ PERMIT_DATA = "PERMIT_DATA",
10
+ PERMIT_DATA_CHECK = "PERMIT_DATA_CHECK",
11
11
  }
12
12
 
13
- const UNSET_SUBTYPE = '-';
13
+ const UNSET_SUBTYPE = "-";
14
14
 
15
15
  type UpdatedTimestamp = {
16
- updated: Date
16
+ updated: Date;
17
17
  } | null;
18
18
 
19
- export function getLastUpdated(db: DTDatabase, datatype: DataType): Promise<Date | null> {
20
- return db.oneOrNone("select updated from data_updated where data_type=$(datatype) and subtype=$(subtype)", {
21
- datatype: datatype, subtype: UNSET_SUBTYPE,
22
- }, (x: UpdatedTimestamp) => x?.updated || null);
19
+ export function getLastUpdated(
20
+ db: DTDatabase,
21
+ datatype: DataType
22
+ ): Promise<Date | null> {
23
+ return db.oneOrNone(
24
+ "select updated from data_updated where data_type=$(datatype) and subtype=$(subtype)",
25
+ {
26
+ datatype: datatype,
27
+ subtype: UNSET_SUBTYPE,
28
+ },
29
+ (x: UpdatedTimestamp) => x?.updated ?? null
30
+ );
23
31
  }
24
32
 
25
- export function getLastUpdatedWithSubtype(db: DTDatabase, datatype: DataType, subtype: string): Promise<Date | null> {
26
- return db.oneOrNone("SELECT updated FROM data_updated WHERE data_type=$(datatype) AND subtype=$(subtype)", {
27
- datatype: datatype, subtype: subtype,
28
- }, (x: UpdatedTimestamp) => x?.updated || null);
33
+ export function getLastUpdatedWithSubtype(
34
+ db: DTDatabase,
35
+ datatype: DataType,
36
+ subtype: string
37
+ ): Promise<Date | null> {
38
+ return db.oneOrNone(
39
+ "SELECT updated FROM data_updated WHERE data_type=$(datatype) AND subtype=$(subtype)",
40
+ {
41
+ datatype: datatype,
42
+ subtype: subtype,
43
+ },
44
+ (x: UpdatedTimestamp) => x?.updated ?? null
45
+ );
29
46
  }
30
47
 
31
- export function updateLastUpdated(db: DTDatabase | DTTransaction, datatype: DataType, updated: Date): Promise<null> {
32
- return db.none(`insert into data_updated(id, data_type, updated)
48
+ export function updateLastUpdated(
49
+ db: DTDatabase | DTTransaction,
50
+ datatype: DataType,
51
+ updated: Date
52
+ ): Promise<null> {
53
+ return db.none(
54
+ `insert into data_updated(id, data_type, updated)
33
55
  values(nextval('seq_data_updated'), $(datatype), $(updated))
34
56
  on conflict (data_type, subtype)
35
57
  do update set updated = $(updated)`,
36
- { updated, datatype });
58
+ { updated, datatype }
59
+ );
37
60
  }
38
61
 
39
- export function updateLastUpdatedWithSubtype(db: DTDatabase | DTTransaction, datatype: DataType, subtype: string, updated: Date): Promise<null> {
40
- return db.none(`insert into data_updated(id, data_type, subtype, updated)
62
+ export function updateLastUpdatedWithSubtype(
63
+ db: DTDatabase | DTTransaction,
64
+ datatype: DataType,
65
+ subtype: string,
66
+ updated: Date
67
+ ): Promise<null> {
68
+ return db.none(
69
+ `insert into data_updated(id, data_type, subtype, updated)
41
70
  values(nextval('seq_data_updated'), $(datatype), $(subtype), $(updated))
42
71
  on conflict (data_type, subtype)
43
72
  do update set updated = $(updated)`,
44
- { updated, subtype, datatype });
73
+ { updated, subtype, datatype }
74
+ );
45
75
  }
46
76
 
47
- export function getUpdatedTimestamp(db: DTDatabase, datatype: string): Promise<Date | null> {
48
- return db.oneOrNone("select updated_time as updated from updated_timestamp where updated_name=$(datatype)", {
49
- datatype: datatype,
50
- }, (x: UpdatedTimestamp) => x?.updated || null);
77
+ export function getUpdatedTimestamp(
78
+ db: DTDatabase,
79
+ datatype: string
80
+ ): Promise<Date | null> {
81
+ return db.oneOrNone(
82
+ "select updated_time as updated from updated_timestamp where updated_name=$(datatype)",
83
+ {
84
+ datatype: datatype,
85
+ },
86
+ (x: UpdatedTimestamp) => x?.updated ?? null
87
+ );
51
88
  }
52
89
 
53
- export function updateUpdatedTimestamp(db: DTDatabase | DTTransaction, datatype: string, date: Date, by = ''): Promise<null> {
54
- return db.none(`insert into updated_timestamp(updated_name, updated_time, updated_by)
90
+ export function updateUpdatedTimestamp(
91
+ db: DTDatabase | DTTransaction,
92
+ datatype: string,
93
+ date: Date,
94
+ by = ""
95
+ ): Promise<null> {
96
+ return db.none(
97
+ `insert into updated_timestamp(updated_name, updated_time, updated_by)
55
98
  values($(datatype), $(date), $(by))
56
99
  on conflict (updated_name)
57
100
  do update set updated_time = $(date), updated_by = $(by)`,
58
- { date, datatype, by });
101
+ { date, datatype, by }
102
+ );
59
103
  }
package/src/marine/rtz.ts CHANGED
@@ -1,57 +1,57 @@
1
- export type RtzPositionCoordinate = {
1
+ export interface RtzPositionCoordinate {
2
2
  readonly $: {
3
- readonly lat: number
4
- readonly lon: number
5
- }
3
+ readonly lat: number;
4
+ readonly lon: number;
5
+ };
6
6
  }
7
7
 
8
- export type RtzWaypointPosition = {
9
- readonly position: RtzPositionCoordinate[]
8
+ export interface RtzWaypointPosition {
9
+ readonly position: RtzPositionCoordinate[];
10
10
  }
11
11
 
12
- export type RtzWaypoint = {
13
- readonly waypoint: RtzWaypointPosition[]
12
+ export interface RtzWaypoint {
13
+ readonly waypoint: RtzWaypointPosition[];
14
14
  }
15
15
 
16
- export type RtzScheduleElement = {
16
+ export interface RtzScheduleElement {
17
17
  readonly $: {
18
18
  /**
19
19
  * Date
20
20
  */
21
- readonly etd?: string
21
+ readonly etd?: string;
22
22
  /**
23
23
  * Date
24
24
  */
25
- readonly eta?: string
26
- },
25
+ readonly eta?: string;
26
+ };
27
27
  }
28
28
 
29
- export type RtzSchedule = {
30
- readonly scheduleElement: RtzScheduleElement[]
29
+ export interface RtzSchedule {
30
+ readonly scheduleElement: RtzScheduleElement[];
31
31
  }
32
32
 
33
- export type RtzScheduleWrapper = {
34
- readonly manual?: RtzSchedule[]
35
- readonly calculated?: RtzSchedule[]
33
+ export interface RtzScheduleWrapper {
34
+ readonly manual?: RtzSchedule[];
35
+ readonly calculated?: RtzSchedule[];
36
36
  }
37
37
 
38
- export type RtzSchedules = {
39
- readonly schedule: RtzScheduleWrapper[]
38
+ export interface RtzSchedules {
39
+ readonly schedule: RtzScheduleWrapper[];
40
40
  }
41
41
 
42
- export type RtzRouteInfo = {
42
+ export interface RtzRouteInfo {
43
43
  readonly $: {
44
- readonly vesselMMSI: string
45
- readonly vesselIMO: string
46
- }
44
+ readonly vesselMMSI: string;
45
+ readonly vesselIMO: string;
46
+ };
47
47
  }
48
48
 
49
- export type RtzRoute = {
50
- readonly routeInfo: RtzRouteInfo[]
51
- readonly waypoints: RtzWaypoint[]
52
- readonly schedules: RtzSchedules[]
49
+ export interface RtzRoute {
50
+ readonly routeInfo: RtzRouteInfo[];
51
+ readonly waypoints: RtzWaypoint[];
52
+ readonly schedules: RtzSchedules[];
53
53
  }
54
54
 
55
- export type RtzVoyagePlan = {
56
- readonly route: RtzRoute
55
+ export interface RtzVoyagePlan {
56
+ readonly route: RtzRoute;
57
57
  }
@@ -2,47 +2,57 @@
2
2
  * A simple asserter-class for writing canaries without dependency to testing-libraries.
3
3
  */
4
4
 
5
+ type AssertedValue = string | number;
6
+
5
7
  export abstract class Asserter {
6
- static assertEquals<T>(value: T, expected: T) {
8
+ static assertEquals(value: AssertedValue, expected: AssertedValue) {
7
9
  if (value != expected) {
8
- throw new Error(`Given value ${value} was not expected ${expected}`);
10
+ throw new Error(
11
+ `Given value ${value} was not expected ${expected}`
12
+ );
9
13
  }
10
14
  }
11
15
 
12
- static assertTrue<T>(value: T) {
16
+ static assertTrue(value: boolean) {
13
17
  if (!value) {
14
- throw new Error(`Given value ${value} was not true`);
18
+ throw new Error(`Given value was not true`);
15
19
  }
16
20
  }
17
21
 
18
- static assertLength<T>(data: T[], expected: number) {
22
+ static assertLength<T>(data: T[] | undefined, expected: number) {
19
23
  if (!data) {
20
24
  throw new Error("Given array was not defined");
21
25
  }
22
26
 
23
27
  if (data.length != expected) {
24
- throw new Error(`Given array length ${data.length} was not expected ${expected}`);
28
+ throw new Error(
29
+ `Given array length ${data.length} was not expected ${expected}`
30
+ );
25
31
  }
26
32
  }
27
33
 
28
- static assertLengthGreaterThan<T>(data: T[], expected: number) {
34
+ static assertLengthGreaterThan<T>(data: T[] | undefined, expected: number) {
29
35
  if (!data) {
30
36
  throw new Error("Given array was not defined");
31
37
  }
32
38
 
33
39
  if (data.length <= expected) {
34
- throw new Error(`Given array length ${data.length} was not greater than ${expected}`);
40
+ throw new Error(
41
+ `Given array length ${data.length} was not greater than ${expected}`
42
+ );
35
43
  }
36
44
  }
37
45
 
38
46
  static assertGreaterThan(value: number, expected: number) {
39
47
  if (value <= expected) {
40
- throw new Error(`Value ${value} was expected to be greater than ${expected}`);
48
+ throw new Error(
49
+ `Value ${value} was expected to be greater than ${expected}`
50
+ );
41
51
  }
42
52
  }
43
53
 
44
54
  static assertToBeCloseTo(value: number, expected: number, delta: number) {
45
- expect(expected-value).toBeGreaterThanOrEqual(-1 * delta);
46
- expect(expected-value).toBeLessThanOrEqual(delta);
55
+ expect(expected - value).toBeGreaterThanOrEqual(-1 * delta);
56
+ expect(expected - value).toBeLessThanOrEqual(delta);
47
57
  }
48
58
  }
@@ -1,4 +1,5 @@
1
1
  import { Server, createServer } from "http";
2
+ import { parse } from "url";
2
3
 
3
4
  export const ERROR_NO_MATCH = "NO MATCH";
4
5
  export const ERRORCODE_NOT_FOUND = 404;
@@ -36,11 +37,18 @@ export class TestHttpServer {
36
37
  this.debuglog(`Starting test server on port ${port}`);
37
38
  this.server = createServer((req, res) => {
38
39
  this.debuglog("Mapped urls: ");
39
-
40
40
  Object.keys(props).forEach((k) => this.debuglog(k));
41
- this.debuglog("Received request to url " + req.url + "..");
42
- // eslint-disable-next-line @typescript-eslint/no-var-requires
43
- const path = require("url").parse(req.url).pathname;
41
+
42
+ if (!req.url) {
43
+ throw new Error("Missing request url!");
44
+ }
45
+
46
+ this.debuglog(`Received request to url ${req.url} ..`);
47
+ const path = parse(req.url).pathname;
48
+
49
+ if (!path) {
50
+ throw new Error("Missing path from request!");
51
+ }
44
52
 
45
53
  let dataStr = "";
46
54
  req.on("data", (chunk) => {
@@ -64,7 +72,7 @@ export class TestHttpServer {
64
72
  res.end(props[path](req.url, dataStr));
65
73
  });
66
74
  } else {
67
- this.debuglog("..no match for %" + path);
75
+ this.debuglog(`..no match for ${path}`);
68
76
  req.on("end", () => {
69
77
  // assume sent data is in JSON format
70
78
  this.messageStack[this.messageStack.length] =
@@ -91,6 +99,7 @@ export class TestHttpServer {
91
99
  }
92
100
  }
93
101
 
94
- export interface ListenProperties {
95
- [key: string]: (url?: string, data?: string) => string;
96
- }
102
+ export type ListenProperties = Record<
103
+ string,
104
+ (url?: string, data?: string) => string
105
+ >;
@@ -1,3 +1,9 @@
1
- export type EitherOk<T> = { result: "ok"; value: T };
2
- export type EitherError = { result: "error"; message: string };
1
+ export interface EitherOk<T> {
2
+ result: "ok";
3
+ value: T;
4
+ }
5
+ export interface EitherError {
6
+ result: "error";
7
+ message: string;
8
+ }
3
9
  export type Either<T> = EitherOk<T> | EitherError;