@digitraffic/common 2024.1.24-3 → 2024.1.30-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.
- package/dist/__test__/api/handler-factory.test.d.mts +1 -0
- package/dist/__test__/api/handler-factory.test.mjs +43 -0
- package/dist/__test__/api/response.test.d.mts +1 -0
- package/dist/__test__/api/response.test.mjs +86 -0
- package/dist/__test__/imports.test.d.mts +1 -0
- package/dist/__test__/imports.test.mjs +332 -0
- package/dist/__test__/marine/id_utils.test.d.mts +1 -0
- package/dist/__test__/marine/id_utils.test.mjs +44 -0
- package/dist/__test__/promise/promise.test.d.mts +1 -0
- package/dist/__test__/promise/promise.test.mjs +130 -0
- package/dist/__test__/runtime/dt-logger.test.d.mts +1 -0
- package/dist/__test__/runtime/dt-logger.test.mjs +108 -0
- package/dist/__test__/secrets/secret-holder.test.d.mts +1 -0
- package/dist/__test__/secrets/secret-holder.test.mjs +89 -0
- package/dist/__test__/secrets/secret.test.d.mts +1 -0
- package/dist/__test__/secrets/secret.test.mjs +42 -0
- package/dist/__test__/test/httpserver.test.d.mts +1 -0
- package/dist/__test__/test/httpserver.test.mjs +154 -0
- package/dist/__test__/types/lambda-response.test.d.mts +1 -0
- package/dist/__test__/types/lambda-response.test.mjs +58 -0
- package/dist/__test__/utils/date-utils.test.d.mts +1 -0
- package/dist/__test__/utils/date-utils.test.mjs +27 -0
- package/dist/__test__/utils/geometry.test.d.mts +1 -0
- package/dist/__test__/utils/geometry.test.mjs +24 -0
- package/dist/__test__/utils/logging.test.d.mts +1 -0
- package/dist/__test__/utils/logging.test.mjs +78 -0
- package/dist/__test__/utils/utils.test.d.mts +1 -0
- package/dist/__test__/utils/utils.test.mjs +43 -0
- package/dist/aws/infra/api/handler-factory.mjs +4 -0
- package/dist/aws/infra/api/integration.d.mts +2 -2
- package/dist/aws/infra/api/integration.mjs +4 -1
- package/dist/aws/infra/api/response.d.mts +1 -1
- package/dist/aws/infra/api/responses.d.mts +1 -1
- package/dist/aws/infra/api/responses.mjs +2 -0
- package/dist/aws/infra/api/static-integration.mjs +1 -1
- package/dist/aws/infra/canaries/canary-alarm.d.mts +1 -1
- package/dist/aws/infra/canaries/canary-alarm.mjs +2 -0
- package/dist/aws/infra/canaries/canary-parameters.mjs +1 -1
- package/dist/aws/infra/canaries/canary-role.mjs +1 -0
- package/dist/aws/infra/canaries/canary.d.mts +2 -2
- package/dist/aws/infra/canaries/canary.mjs +2 -0
- package/dist/aws/infra/canaries/database-canary.d.mts +2 -2
- package/dist/aws/infra/canaries/database-canary.mjs +2 -0
- package/dist/aws/infra/canaries/database-checker.d.mts +1 -1
- package/dist/aws/infra/canaries/database-checker.mjs +7 -1
- package/dist/aws/infra/canaries/url-canary.d.mts +2 -2
- package/dist/aws/infra/canaries/url-canary.mjs +3 -0
- package/dist/aws/infra/canaries/url-checker.d.mts +1 -1
- package/dist/aws/infra/canaries/url-checker.mjs +4 -1
- package/dist/aws/infra/documentation.mjs +5 -1
- package/dist/aws/infra/import-util.d.mts +1 -1
- package/dist/aws/infra/import-util.mjs +4 -3
- package/dist/aws/infra/scheduler.mjs +2 -0
- package/dist/aws/infra/security-rule.d.mts +1 -1
- package/dist/aws/infra/security-rule.mjs +1 -0
- package/dist/aws/infra/sqs-integration.d.mts +1 -1
- package/dist/aws/infra/sqs-integration.mjs +3 -1
- package/dist/aws/infra/sqs-queue.d.mts +1 -1
- package/dist/aws/infra/sqs-queue.mjs +2 -1
- package/dist/aws/infra/stack/lambda-configs.d.mts +4 -4
- package/dist/aws/infra/stack/lambda-configs.mjs +4 -2
- package/dist/aws/infra/stack/monitoredfunction.d.mts +3 -3
- package/dist/aws/infra/stack/monitoredfunction.mjs +20 -16
- package/dist/aws/infra/stack/parameters.mjs +1 -0
- package/dist/aws/infra/stack/rest_apis.d.mts +2 -2
- package/dist/aws/infra/stack/rest_apis.mjs +6 -1
- package/dist/aws/infra/stack/stack-checking-aspect.d.mts +2 -2
- package/dist/aws/infra/stack/stack-checking-aspect.mjs +6 -1
- package/dist/aws/infra/stack/stack.d.mts +5 -5
- package/dist/aws/infra/stack/stack.mjs +9 -0
- package/dist/aws/infra/stack/subscription.mjs +4 -0
- package/dist/aws/infra/stacks/db-dns-stack.d.mts +1 -1
- package/dist/aws/infra/stacks/db-dns-stack.mjs +1 -0
- package/dist/aws/infra/stacks/db-proxy-stack.d.mts +3 -3
- package/dist/aws/infra/stacks/db-proxy-stack.mjs +4 -2
- package/dist/aws/infra/stacks/db-stack.d.mts +3 -3
- package/dist/aws/infra/stacks/db-stack.mjs +11 -7
- package/dist/aws/infra/stacks/intra-stack-configuration.d.mts +1 -1
- package/dist/aws/infra/stacks/network-stack.d.mts +2 -2
- package/dist/aws/infra/stacks/network-stack.mjs +8 -0
- package/dist/aws/infra/usage-plans.d.mts +1 -1
- package/dist/aws/infra/usage-plans.mjs +1 -0
- package/dist/aws/runtime/apikey.d.mts +2 -2
- package/dist/aws/runtime/apikey.mjs +2 -1
- package/dist/aws/runtime/digitraffic-integration-response.d.mts +1 -1
- package/dist/aws/runtime/dt-logger.mjs +6 -2
- package/dist/aws/runtime/messaging.d.mts +2 -2
- package/dist/aws/runtime/s3.d.mts +2 -2
- package/dist/aws/runtime/s3.mjs +2 -1
- package/dist/aws/runtime/secrets/dbsecret.d.mts +1 -1
- package/dist/aws/runtime/secrets/proxy-holder.mjs +1 -0
- package/dist/aws/runtime/secrets/rds-holder.mjs +1 -0
- package/dist/aws/runtime/secrets/secret-holder.d.mts +1 -1
- package/dist/aws/runtime/secrets/secret-holder.mjs +6 -1
- package/dist/aws/runtime/secrets/secret.mjs +4 -2
- package/dist/aws/types/errors.mjs +1 -0
- package/dist/aws/types/lambda-response.mjs +5 -0
- package/dist/aws/types/model-with-reference.mjs +1 -1
- package/dist/database/cached.d.mts +1 -1
- package/dist/database/database.mjs +1 -0
- package/dist/database/last-updated.d.mts +1 -1
- package/dist/test/db-testutils.d.mts +1 -1
- package/dist/test/db-testutils.mjs +1 -1
- package/dist/test/httpserver.mjs +7 -3
- package/dist/test/secrets-manager.d.mts +1 -1
- package/dist/test/secrets-manager.mjs +1 -1
- package/dist/test/testutils.mjs +1 -1
- package/dist/types/http-error.mjs +1 -0
- package/dist/types/nullable.d.mts +1 -1
- package/dist/utils/api-model.d.mts +2 -2
- package/dist/utils/api-model.mjs +1 -1
- package/dist/utils/geojson-types.d.mts +1 -1
- package/dist/utils/geojson-types.mjs +4 -2
- package/dist/utils/geometry.d.mts +1 -1
- package/dist/utils/geometry.mjs +3 -0
- package/dist/utils/retry.d.mts +2 -2
- package/dist/utils/retry.mjs +2 -2
- package/dist/utils/slack.mjs +1 -0
- package/dist/utils/utils.d.mts +2 -2
- package/package.json +11 -6
- package/src/@types/geojson-validation/index.d.mts +0 -4
- package/src/aws/infra/api/handler-factory.mts +0 -86
- package/src/aws/infra/api/integration.mts +0 -147
- package/src/aws/infra/api/response.mts +0 -165
- package/src/aws/infra/api/responses.mts +0 -127
- package/src/aws/infra/api/static-integration.mts +0 -108
- package/src/aws/infra/canaries/Synthetics.d.mts +0 -21
- package/src/aws/infra/canaries/canary-alarm.mts +0 -33
- package/src/aws/infra/canaries/canary-keys.mts +0 -3
- package/src/aws/infra/canaries/canary-parameters.mts +0 -19
- package/src/aws/infra/canaries/canary-role.mts +0 -73
- package/src/aws/infra/canaries/canary.mts +0 -44
- package/src/aws/infra/canaries/database-canary.mts +0 -98
- package/src/aws/infra/canaries/database-checker.mts +0 -163
- package/src/aws/infra/canaries/url-canary.mts +0 -98
- package/src/aws/infra/canaries/url-checker.mts +0 -388
- package/src/aws/infra/documentation.mts +0 -142
- package/src/aws/infra/import-util.mts +0 -57
- package/src/aws/infra/scheduler.mts +0 -59
- package/src/aws/infra/security-rule.mts +0 -38
- package/src/aws/infra/sqs-integration.mts +0 -106
- package/src/aws/infra/sqs-queue.mts +0 -162
- package/src/aws/infra/stack/lambda-configs.mts +0 -135
- package/src/aws/infra/stack/monitoredfunction.mts +0 -352
- package/src/aws/infra/stack/parameters.mts +0 -74
- package/src/aws/infra/stack/rest_apis.mts +0 -322
- package/src/aws/infra/stack/stack-checking-aspect.mts +0 -233
- package/src/aws/infra/stack/stack.mts +0 -144
- package/src/aws/infra/stack/subscription.mts +0 -58
- package/src/aws/infra/stacks/db-dns-stack.mts +0 -77
- package/src/aws/infra/stacks/db-proxy-stack.mts +0 -134
- package/src/aws/infra/stacks/db-stack.mts +0 -292
- package/src/aws/infra/stacks/intra-stack-configuration.mts +0 -6
- package/src/aws/infra/stacks/network-stack.mts +0 -76
- package/src/aws/infra/usage-plans.mts +0 -50
- package/src/aws/runtime/apikey.mts +0 -9
- package/src/aws/runtime/digitraffic-integration-response.mts +0 -35
- package/src/aws/runtime/dt-logger-default.mts +0 -11
- package/src/aws/runtime/dt-logger.mts +0 -184
- package/src/aws/runtime/environment.mts +0 -22
- package/src/aws/runtime/messaging.mts +0 -26
- package/src/aws/runtime/s3.mts +0 -44
- package/src/aws/runtime/secrets/dbsecret.mts +0 -31
- package/src/aws/runtime/secrets/node-ttl.d.mts +0 -12
- package/src/aws/runtime/secrets/proxy-holder.mts +0 -34
- package/src/aws/runtime/secrets/rds-holder.mts +0 -34
- package/src/aws/runtime/secrets/secret-holder.mts +0 -106
- package/src/aws/runtime/secrets/secret.mts +0 -58
- package/src/aws/types/errors.mts +0 -14
- package/src/aws/types/lambda-response.mts +0 -100
- package/src/aws/types/mediatypes.mts +0 -12
- package/src/aws/types/model-with-reference.mts +0 -8
- package/src/aws/types/proxytypes.mts +0 -27
- package/src/aws/types/tags.mts +0 -3
- package/src/database/cached.mts +0 -64
- package/src/database/database.mts +0 -107
- package/src/database/last-updated.mts +0 -103
- package/src/database/models.mts +0 -7
- package/src/index.mts +0 -2
- package/src/marine/id_utils.mts +0 -30
- package/src/marine/rtz.mts +0 -57
- package/src/test/asserter.mts +0 -58
- package/src/test/db-testutils.mts +0 -52
- package/src/test/httpserver.mts +0 -111
- package/src/test/secrets-manager.mts +0 -37
- package/src/test/testutils.mts +0 -39
- package/src/types/async-timeout-error.mts +0 -5
- package/src/types/aws-env.mts +0 -3
- package/src/types/either.mts +0 -9
- package/src/types/http-error.mts +0 -8
- package/src/types/input-error.mts +0 -2
- package/src/types/language.mts +0 -3
- package/src/types/nullable.mts +0 -21
- package/src/types/traffictype.mts +0 -8
- package/src/types/urn.mts +0 -1
- package/src/types/util-types.mts +0 -10
- package/src/types/validator.mts +0 -10
- package/src/utils/api-model.mts +0 -133
- package/src/utils/base64.mts +0 -16
- package/src/utils/date-utils.mts +0 -53
- package/src/utils/geojson-types.mts +0 -22
- package/src/utils/geometry.mts +0 -171
- package/src/utils/logging.mts +0 -75
- package/src/utils/retry.mts +0 -200
- package/src/utils/slack.mts +0 -26
- package/src/utils/utils.mts +0 -184
@@ -1,100 +0,0 @@
|
|
1
|
-
import etag from "etag";
|
2
|
-
|
3
|
-
export class LambdaResponse {
|
4
|
-
readonly status: number;
|
5
|
-
readonly body: string;
|
6
|
-
readonly fileName?: string;
|
7
|
-
readonly timestamp?: string;
|
8
|
-
readonly etag: string;
|
9
|
-
|
10
|
-
constructor(
|
11
|
-
status: number,
|
12
|
-
body: string,
|
13
|
-
fileName?: string,
|
14
|
-
timestamp?: Date
|
15
|
-
) {
|
16
|
-
this.status = status;
|
17
|
-
this.body = body;
|
18
|
-
this.fileName = fileName;
|
19
|
-
this.timestamp = timestamp?.toUTCString();
|
20
|
-
this.etag = etag(body); // create strong etag by default
|
21
|
-
}
|
22
|
-
|
23
|
-
withTimestamp(timestamp: Date) {
|
24
|
-
return new LambdaResponse(
|
25
|
-
this.status,
|
26
|
-
this.body,
|
27
|
-
this.fileName,
|
28
|
-
timestamp
|
29
|
-
);
|
30
|
-
}
|
31
|
-
|
32
|
-
/**
|
33
|
-
* Create LambdaResponse for HTTP 200 from json.
|
34
|
-
*/
|
35
|
-
static okJson<T>(json: T, fileName?: string) {
|
36
|
-
return this.ok(JSON.stringify(json), fileName);
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Create LambdaResponse for HTTP 200 from string.
|
41
|
-
*/
|
42
|
-
static ok(body: string, fileName?: string) {
|
43
|
-
return this.okBinary(toBase64(body), fileName);
|
44
|
-
}
|
45
|
-
|
46
|
-
/**
|
47
|
-
* Create LambdaResponse for HTTP 200 from base64-encoded data.
|
48
|
-
*/
|
49
|
-
static okBinary(base64: string, fileName?: string) {
|
50
|
-
return this.createForBase64(200, base64, fileName);
|
51
|
-
}
|
52
|
-
|
53
|
-
/**
|
54
|
-
* Create LambdaResponse for HTTP 400
|
55
|
-
*/
|
56
|
-
static badRequest(body: string) {
|
57
|
-
return this.createForString(400, body);
|
58
|
-
}
|
59
|
-
|
60
|
-
/**
|
61
|
-
* Create LambdaResponse for HTTP 404
|
62
|
-
*/
|
63
|
-
static notFound() {
|
64
|
-
return this.createForString(404, "Not found");
|
65
|
-
}
|
66
|
-
|
67
|
-
/**
|
68
|
-
* Create LambdaResponse for HTTP 500
|
69
|
-
*/
|
70
|
-
static internalError() {
|
71
|
-
return this.createForString(500, "Internal error");
|
72
|
-
}
|
73
|
-
|
74
|
-
/**
|
75
|
-
* Create LambdaResponse for HTTP 501
|
76
|
-
*/
|
77
|
-
static notImplemented() {
|
78
|
-
return this.createForString(501, "Not implemented");
|
79
|
-
}
|
80
|
-
|
81
|
-
private static createForString(
|
82
|
-
status: number,
|
83
|
-
body: string,
|
84
|
-
fileName?: string
|
85
|
-
): LambdaResponse {
|
86
|
-
return this.createForBase64(status, toBase64(body), fileName);
|
87
|
-
}
|
88
|
-
|
89
|
-
private static createForBase64(
|
90
|
-
status: number,
|
91
|
-
base64Body: string,
|
92
|
-
fileName?: string
|
93
|
-
): LambdaResponse {
|
94
|
-
return new LambdaResponse(status, base64Body, fileName);
|
95
|
-
}
|
96
|
-
}
|
97
|
-
|
98
|
-
function toBase64(body: string) {
|
99
|
-
return Buffer.from(body).toString("base64");
|
100
|
-
}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
// charset=UTF-8 is deprecated but mobile applications sometimes needs it
|
2
|
-
export enum MediaType {
|
3
|
-
APPLICATION_JSON = "application/json",
|
4
|
-
APPLICATION_XML = "application/xml",
|
5
|
-
APPLICATION_GEOJSON = "application/geo+json;charset=UTF-8",
|
6
|
-
IMAGE_SVG = "image/svg+xml",
|
7
|
-
IMAGE_JPEG = "image/jpeg",
|
8
|
-
TEXT_PLAIN = "text/plain",
|
9
|
-
TEXT_HTML = "text/html",
|
10
|
-
TEXT_CSV = "text/csv",
|
11
|
-
APPLICATION_JSON_UTF8 = "application/json;charset=UTF-8",
|
12
|
-
}
|
@@ -1,27 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
|
3
|
-
*
|
4
|
-
* Not fully described, extend if necessary.
|
5
|
-
*/
|
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
|
-
}
|
12
|
-
|
13
|
-
/**
|
14
|
-
* https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
|
15
|
-
*
|
16
|
-
* Not fully described, extend if necessary.
|
17
|
-
*/
|
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
|
-
}
|
package/src/aws/types/tags.mts
DELETED
package/src/database/cached.mts
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
import pg from "pg-promise";
|
2
|
-
import { DTDatabase, DTTransaction } from "./database.mjs";
|
3
|
-
|
4
|
-
const { PreparedStatement } = pg;
|
5
|
-
|
6
|
-
export interface CachedValue<T> {
|
7
|
-
content: T;
|
8
|
-
last_updated: Date;
|
9
|
-
modified: Date;
|
10
|
-
}
|
11
|
-
|
12
|
-
const PS_UPDATE_CACHE_VALUE = new PreparedStatement({
|
13
|
-
name: "update-cache-value",
|
14
|
-
text: `insert into cached_json(cache_id, content, last_updated)
|
15
|
-
values ($1, $2, $3)
|
16
|
-
on conflict(cache_id) do
|
17
|
-
update set content = $2, last_updated = $3`,
|
18
|
-
});
|
19
|
-
|
20
|
-
const PS_GET_CACHE_VALUE = new PreparedStatement({
|
21
|
-
name: "get-cache-value",
|
22
|
-
text: "select content, last_updated, modified from cached_json where cache_id = $1",
|
23
|
-
});
|
24
|
-
|
25
|
-
export enum JSON_CACHE_KEY {
|
26
|
-
NAUTICAL_WARNINGS_ACTIVE = "nautical-warnings-active",
|
27
|
-
NAUTICAL_WARNINGS_ARCHIVED = "nautical-warnings-archived",
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
*
|
32
|
-
* @param db
|
33
|
-
* @param cacheKey
|
34
|
-
* @param value
|
35
|
-
* @param lastUpdated time when data was created or updated
|
36
|
-
*/
|
37
|
-
export async function updateCachedJson<T>(
|
38
|
-
db: DTDatabase | DTTransaction,
|
39
|
-
cacheKey: JSON_CACHE_KEY,
|
40
|
-
value: T,
|
41
|
-
lastUpdated: Date
|
42
|
-
): Promise<void> {
|
43
|
-
await db.none(PS_UPDATE_CACHE_VALUE, [cacheKey, value, lastUpdated]);
|
44
|
-
}
|
45
|
-
|
46
|
-
export function getJsonFromCache<T>(
|
47
|
-
db: DTDatabase | DTTransaction,
|
48
|
-
cacheKey: JSON_CACHE_KEY
|
49
|
-
): Promise<T | undefined> {
|
50
|
-
return db
|
51
|
-
.oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey])
|
52
|
-
.then((value) => value?.content ?? undefined);
|
53
|
-
}
|
54
|
-
|
55
|
-
export async function getFromCache<T>(
|
56
|
-
db: DTDatabase | DTTransaction,
|
57
|
-
cacheKey: JSON_CACHE_KEY
|
58
|
-
): Promise<CachedValue<T> | undefined> {
|
59
|
-
return db
|
60
|
-
.oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey])
|
61
|
-
.then((result) => {
|
62
|
-
return result ?? undefined;
|
63
|
-
});
|
64
|
-
}
|
@@ -1,107 +0,0 @@
|
|
1
|
-
import { type IDatabase, type ITask } from "pg-promise";
|
2
|
-
import { getEnvVariable, getEnvVariableOrElse } from "../utils/utils.mjs";
|
3
|
-
import { logger } from "../aws/runtime/dt-logger-default.mjs";
|
4
|
-
import { logException } from "../utils/logging.mjs";
|
5
|
-
|
6
|
-
export enum DatabaseEnvironmentKeys {
|
7
|
-
DB_USER = "DB_USER",
|
8
|
-
DB_PASS = "DB_PASS",
|
9
|
-
DB_URI = "DB_URI",
|
10
|
-
DB_RO_URI = "DB_RO_URI",
|
11
|
-
DB_APPLICATION = "DB_APPLICATION",
|
12
|
-
}
|
13
|
-
import pgpImport from "pg-promise";
|
14
|
-
const pgp = pgpImport();
|
15
|
-
|
16
|
-
// convert numeric types to number instead of string
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
18
|
-
pgp.pg.types.setTypeParser(pgp.pg.types.builtins.INT8, (value: string) => {
|
19
|
-
return parseInt(value);
|
20
|
-
});
|
21
|
-
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
23
|
-
pgp.pg.types.setTypeParser(pgp.pg.types.builtins.FLOAT8, (value: string) => {
|
24
|
-
return parseFloat(value);
|
25
|
-
});
|
26
|
-
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
28
|
-
pgp.pg.types.setTypeParser(pgp.pg.types.builtins.NUMERIC, (value: string) => {
|
29
|
-
return parseFloat(value);
|
30
|
-
});
|
31
|
-
|
32
|
-
export type DTDatabase = IDatabase<unknown>;
|
33
|
-
|
34
|
-
export type DTTransaction = ITask<unknown>;
|
35
|
-
|
36
|
-
/**
|
37
|
-
* Creates a non-pooling database connection primarily used by Lambdas.
|
38
|
-
*
|
39
|
-
* Note! Using this method opens a new RDS connection on every invocation. It is advised to
|
40
|
-
* use RDS proxy to pool connections transparently.
|
41
|
-
* https://docs.amazonaws.cn/en_us/AmazonRDS/latest/AuroraUserGuide/rds-proxy.html
|
42
|
-
* @param username Username
|
43
|
-
* @param password Password
|
44
|
-
* @param applicationName name of application
|
45
|
-
* @param url Connection URL
|
46
|
-
* @param options pg-promise options
|
47
|
-
*/
|
48
|
-
export function initDbConnection(
|
49
|
-
username: string,
|
50
|
-
password: string,
|
51
|
-
applicationName: string,
|
52
|
-
url: string,
|
53
|
-
options?: object
|
54
|
-
): DTDatabase {
|
55
|
-
const finalUrl = `postgresql://${username}:${password}@${url}?application_name=${applicationName}`;
|
56
|
-
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
58
|
-
return pgp(finalUrl, options);
|
59
|
-
}
|
60
|
-
|
61
|
-
export function inTransaction<T>(
|
62
|
-
fn: (db: DTTransaction) => Promise<T>
|
63
|
-
): Promise<T> {
|
64
|
-
return inDatabase((db) => db.tx((t: DTTransaction) => fn(t)));
|
65
|
-
}
|
66
|
-
|
67
|
-
export function inDatabase<T>(fn: (db: DTDatabase) => Promise<T>): Promise<T> {
|
68
|
-
return doInDatabase(false, fn);
|
69
|
-
}
|
70
|
-
|
71
|
-
export function inDatabaseReadonly<T>(
|
72
|
-
fn: (db: DTDatabase) => Promise<T>
|
73
|
-
): Promise<T> {
|
74
|
-
return doInDatabase(true, fn);
|
75
|
-
}
|
76
|
-
|
77
|
-
async function doInDatabase<T>(
|
78
|
-
readonly: boolean,
|
79
|
-
fn: (db: DTDatabase) => Promise<T>
|
80
|
-
): Promise<T> {
|
81
|
-
const db_application = getEnvVariableOrElse(
|
82
|
-
DatabaseEnvironmentKeys.DB_APPLICATION,
|
83
|
-
"unknown-cdk-application"
|
84
|
-
);
|
85
|
-
const db_uri = readonly
|
86
|
-
? getEnvVariable(DatabaseEnvironmentKeys.DB_RO_URI)
|
87
|
-
: getEnvVariable(DatabaseEnvironmentKeys.DB_URI);
|
88
|
-
|
89
|
-
const db = initDbConnection(
|
90
|
-
getEnvVariable(DatabaseEnvironmentKeys.DB_USER),
|
91
|
-
getEnvVariable(DatabaseEnvironmentKeys.DB_PASS),
|
92
|
-
db_application,
|
93
|
-
db_uri
|
94
|
-
);
|
95
|
-
try {
|
96
|
-
// deallocate all prepared statements to allow for connection pooling
|
97
|
-
// DISCARD instead of DEALLOCATE as it didn't always clean all prepared statements
|
98
|
-
await db.none("DISCARD ALL");
|
99
|
-
return await fn(db);
|
100
|
-
} catch (e) {
|
101
|
-
logException(logger, e);
|
102
|
-
|
103
|
-
throw e;
|
104
|
-
} finally {
|
105
|
-
await db.$pool.end();
|
106
|
-
}
|
107
|
-
}
|
@@ -1,103 +0,0 @@
|
|
1
|
-
import { DTDatabase, DTTransaction } from "./database.mjs";
|
2
|
-
|
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",
|
11
|
-
}
|
12
|
-
|
13
|
-
const UNSET_SUBTYPE = "-";
|
14
|
-
|
15
|
-
type UpdatedTimestamp = {
|
16
|
-
updated: Date;
|
17
|
-
} | null;
|
18
|
-
|
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
|
-
);
|
31
|
-
}
|
32
|
-
|
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
|
-
);
|
46
|
-
}
|
47
|
-
|
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)
|
55
|
-
values(nextval('seq_data_updated'), $(datatype), $(updated))
|
56
|
-
on conflict (data_type, subtype)
|
57
|
-
do update set updated = $(updated)`,
|
58
|
-
{ updated, datatype }
|
59
|
-
);
|
60
|
-
}
|
61
|
-
|
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)
|
70
|
-
values(nextval('seq_data_updated'), $(datatype), $(subtype), $(updated))
|
71
|
-
on conflict (data_type, subtype)
|
72
|
-
do update set updated = $(updated)`,
|
73
|
-
{ updated, subtype, datatype }
|
74
|
-
);
|
75
|
-
}
|
76
|
-
|
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
|
-
);
|
88
|
-
}
|
89
|
-
|
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)
|
98
|
-
values($(datatype), $(date), $(by))
|
99
|
-
on conflict (updated_name)
|
100
|
-
do update set updated_time = $(date), updated_by = $(by)`,
|
101
|
-
{ date, datatype, by }
|
102
|
-
);
|
103
|
-
}
|
package/src/database/models.mts
DELETED
package/src/index.mts
DELETED
package/src/marine/id_utils.mts
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
export function isValidIMO(imo: number): boolean {
|
2
|
-
return imo >= 1000000 && imo <= 9999999 && imoChecksumIsValid(imo);
|
3
|
-
}
|
4
|
-
|
5
|
-
function imoChecksumIsValid(imo: number): boolean {
|
6
|
-
const imoStr = imo.toString();
|
7
|
-
const imoDigit1 = Number(imoStr[0]);
|
8
|
-
const imoDigit2 = Number(imoStr[1]);
|
9
|
-
const imoDigit3 = Number(imoStr[2]);
|
10
|
-
const imoDigit4 = Number(imoStr[3]);
|
11
|
-
const imoDigit5 = Number(imoStr[4]);
|
12
|
-
const imoDigit6 = Number(imoStr[5]);
|
13
|
-
const checkDigit = Number(imoStr[6]);
|
14
|
-
const checkCalculation = Number(((imoDigit1 * 7) + (imoDigit2 * 6) + (imoDigit3 * 5) + (imoDigit4 * 4) + (imoDigit5 * 3) + (imoDigit6 * 2)));
|
15
|
-
const checkResult = checkCalculation % 10 === checkDigit;
|
16
|
-
if (!checkResult) {
|
17
|
-
console.warn('method=imoChecksumIsValid IMO checksum failed %d', imo);
|
18
|
-
}
|
19
|
-
return checkResult;
|
20
|
-
}
|
21
|
-
|
22
|
-
export function isValidMMSI(mmsi: number): boolean {
|
23
|
-
return mmsi >= 100000000 && mmsi <= 999999999;
|
24
|
-
}
|
25
|
-
|
26
|
-
const LocodePattern = /^FI[A-Z]{3}$/i;
|
27
|
-
|
28
|
-
export function isValidLOCODE(locode: string): boolean {
|
29
|
-
return LocodePattern.test(locode);
|
30
|
-
}
|
package/src/marine/rtz.mts
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
export interface RtzPositionCoordinate {
|
2
|
-
readonly $: {
|
3
|
-
readonly lat: number;
|
4
|
-
readonly lon: number;
|
5
|
-
};
|
6
|
-
}
|
7
|
-
|
8
|
-
export interface RtzWaypointPosition {
|
9
|
-
readonly position: RtzPositionCoordinate[];
|
10
|
-
}
|
11
|
-
|
12
|
-
export interface RtzWaypoint {
|
13
|
-
readonly waypoint: RtzWaypointPosition[];
|
14
|
-
}
|
15
|
-
|
16
|
-
export interface RtzScheduleElement {
|
17
|
-
readonly $: {
|
18
|
-
/**
|
19
|
-
* Date
|
20
|
-
*/
|
21
|
-
readonly etd?: string;
|
22
|
-
/**
|
23
|
-
* Date
|
24
|
-
*/
|
25
|
-
readonly eta?: string;
|
26
|
-
};
|
27
|
-
}
|
28
|
-
|
29
|
-
export interface RtzSchedule {
|
30
|
-
readonly scheduleElement: RtzScheduleElement[];
|
31
|
-
}
|
32
|
-
|
33
|
-
export interface RtzScheduleWrapper {
|
34
|
-
readonly manual?: RtzSchedule[];
|
35
|
-
readonly calculated?: RtzSchedule[];
|
36
|
-
}
|
37
|
-
|
38
|
-
export interface RtzSchedules {
|
39
|
-
readonly schedule: RtzScheduleWrapper[];
|
40
|
-
}
|
41
|
-
|
42
|
-
export interface RtzRouteInfo {
|
43
|
-
readonly $: {
|
44
|
-
readonly vesselMMSI: string;
|
45
|
-
readonly vesselIMO: string;
|
46
|
-
};
|
47
|
-
}
|
48
|
-
|
49
|
-
export interface RtzRoute {
|
50
|
-
readonly routeInfo: RtzRouteInfo[];
|
51
|
-
readonly waypoints: RtzWaypoint[];
|
52
|
-
readonly schedules: RtzSchedules[];
|
53
|
-
}
|
54
|
-
|
55
|
-
export interface RtzVoyagePlan {
|
56
|
-
readonly route: RtzRoute;
|
57
|
-
}
|
package/src/test/asserter.mts
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* A simple asserter-class for writing canaries without dependency to testing-libraries.
|
3
|
-
*/
|
4
|
-
|
5
|
-
type AssertedValue = string | number;
|
6
|
-
|
7
|
-
export abstract class Asserter {
|
8
|
-
static assertEquals(value: AssertedValue, expected: AssertedValue) {
|
9
|
-
if (value != expected) {
|
10
|
-
throw new Error(
|
11
|
-
`Given value ${value} was not expected ${expected}`
|
12
|
-
);
|
13
|
-
}
|
14
|
-
}
|
15
|
-
|
16
|
-
static assertTrue(value: boolean) {
|
17
|
-
if (!value) {
|
18
|
-
throw new Error(`Given value was not true`);
|
19
|
-
}
|
20
|
-
}
|
21
|
-
|
22
|
-
static assertLength<T>(data: T[] | undefined, expected: number) {
|
23
|
-
if (!data) {
|
24
|
-
throw new Error("Given array was not defined");
|
25
|
-
}
|
26
|
-
|
27
|
-
if (data.length != expected) {
|
28
|
-
throw new Error(
|
29
|
-
`Given array length ${data.length} was not expected ${expected}`
|
30
|
-
);
|
31
|
-
}
|
32
|
-
}
|
33
|
-
|
34
|
-
static assertLengthGreaterThan<T>(data: T[] | undefined, expected: number) {
|
35
|
-
if (!data) {
|
36
|
-
throw new Error("Given array was not defined");
|
37
|
-
}
|
38
|
-
|
39
|
-
if (data.length <= expected) {
|
40
|
-
throw new Error(
|
41
|
-
`Given array length ${data.length} was not greater than ${expected}`
|
42
|
-
);
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
static assertGreaterThan(value: number, expected: number) {
|
47
|
-
if (value <= expected) {
|
48
|
-
throw new Error(
|
49
|
-
`Value ${value} was expected to be greater than ${expected}`
|
50
|
-
);
|
51
|
-
}
|
52
|
-
}
|
53
|
-
|
54
|
-
static assertToBeCloseTo(value: number, expected: number, delta: number) {
|
55
|
-
expect(expected - value).toBeGreaterThanOrEqual(-1 * delta);
|
56
|
-
expect(expected - value).toBeLessThanOrEqual(delta);
|
57
|
-
}
|
58
|
-
}
|
@@ -1,52 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
DatabaseEnvironmentKeys,
|
3
|
-
DTDatabase,
|
4
|
-
initDbConnection,
|
5
|
-
} from "../database/database.mjs";
|
6
|
-
import { Countable } from "../database/models.mjs";
|
7
|
-
|
8
|
-
export async function assertCount(db: DTDatabase, sql: string, count: number) {
|
9
|
-
await db.one(sql).then((x: Countable) => expect(x.count).toEqual(count));
|
10
|
-
}
|
11
|
-
|
12
|
-
export function dbTestBase(
|
13
|
-
fn: (db: DTDatabase) => void,
|
14
|
-
truncateFn: (db: DTDatabase) => Promise<void>,
|
15
|
-
dbUser: string,
|
16
|
-
dbPass: string,
|
17
|
-
dbUri: string
|
18
|
-
): () => void {
|
19
|
-
const theDbUri = process.env.DB_URI ?? dbUri;
|
20
|
-
console.log(`Test database URI: ${theDbUri}`);
|
21
|
-
|
22
|
-
return () => {
|
23
|
-
const db: DTDatabase = initDbConnection(
|
24
|
-
dbUser,
|
25
|
-
dbPass,
|
26
|
-
"test",
|
27
|
-
theDbUri,
|
28
|
-
{
|
29
|
-
noWarnings: true, // ignore duplicate connection warning for tests
|
30
|
-
}
|
31
|
-
);
|
32
|
-
|
33
|
-
beforeAll(async () => {
|
34
|
-
process.env[DatabaseEnvironmentKeys.DB_USER] = dbUser;
|
35
|
-
process.env[DatabaseEnvironmentKeys.DB_PASS] = dbPass;
|
36
|
-
process.env[DatabaseEnvironmentKeys.DB_URI] = theDbUri;
|
37
|
-
process.env[DatabaseEnvironmentKeys.DB_RO_URI] = theDbUri;
|
38
|
-
await truncateFn(db);
|
39
|
-
});
|
40
|
-
|
41
|
-
afterAll(async () => {
|
42
|
-
await truncateFn(db);
|
43
|
-
await db.$pool.end();
|
44
|
-
});
|
45
|
-
|
46
|
-
beforeEach(async () => {
|
47
|
-
await truncateFn(db);
|
48
|
-
});
|
49
|
-
|
50
|
-
fn(db);
|
51
|
-
};
|
52
|
-
}
|