@digitraffic/common 2024.1.24-3 → 2024.3.11-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 +86 -0
- package/dist/__test__/secrets/secret.test.d.mts +1 -0
- package/dist/__test__/secrets/secret.test.mjs +38 -0
- package/dist/__test__/test/httpserver.test.d.mts +1 -0
- package/dist/__test__/test/httpserver.test.mjs +154 -0
- package/dist/__test__/test/mock-ky.test.d.mts +1 -0
- package/dist/__test__/test/mock-ky.test.mjs +46 -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 +23 -18
- 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 -2
- 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/messaging.mjs +5 -4
- package/dist/aws/runtime/s3.d.mts +4 -2
- package/dist/aws/runtime/s3.mjs +15 -10
- 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 +5 -6
- 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/mock-ky.d.mts +2 -0
- package/dist/test/mock-ky.mjs +15 -0
- package/dist/test/secrets-manager.d.mts +3 -2
- package/dist/test/secrets-manager.mjs +14 -16
- 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/logging.mjs +2 -2
- package/dist/utils/retry.d.mts +2 -2
- package/dist/utils/retry.mjs +2 -2
- package/dist/utils/slack.mjs +4 -3
- package/dist/utils/utils.d.mts +2 -2
- package/package.json +25 -15
- 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
package/src/test/httpserver.mts
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
import { Server, createServer } from "http";
|
2
|
-
import { parse } from "url";
|
3
|
-
|
4
|
-
export const ERROR_NO_MATCH = "NO MATCH";
|
5
|
-
export const ERRORCODE_NOT_FOUND = 404;
|
6
|
-
|
7
|
-
/**
|
8
|
-
* A mock HTTP server created for testing connections from a Lambda to an outside integration
|
9
|
-
*/
|
10
|
-
export class TestHttpServer {
|
11
|
-
private server?: Server;
|
12
|
-
private debug: boolean;
|
13
|
-
|
14
|
-
private messageStack: string[];
|
15
|
-
|
16
|
-
constructor() {
|
17
|
-
this.debug = false;
|
18
|
-
this.messageStack = [];
|
19
|
-
}
|
20
|
-
|
21
|
-
getCallCount(): number {
|
22
|
-
return this.messageStack.length;
|
23
|
-
}
|
24
|
-
|
25
|
-
getRequestBody(callNumber: number): string {
|
26
|
-
return this.messageStack[callNumber];
|
27
|
-
}
|
28
|
-
|
29
|
-
listen(
|
30
|
-
port: number,
|
31
|
-
props: ListenProperties,
|
32
|
-
debug = false,
|
33
|
-
statusCode = 200,
|
34
|
-
) {
|
35
|
-
this.debug = debug;
|
36
|
-
this.messageStack = [];
|
37
|
-
this.debuglog(`Starting test server on port ${port}`);
|
38
|
-
this.server = createServer((req, res) => {
|
39
|
-
this.debuglog("Mapped urls: ");
|
40
|
-
Object.keys(props).forEach((k) => this.debuglog(k));
|
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
|
-
}
|
52
|
-
|
53
|
-
let dataStr = "";
|
54
|
-
req.on("data", (chunk) => {
|
55
|
-
if (chunk) {
|
56
|
-
dataStr += chunk;
|
57
|
-
}
|
58
|
-
});
|
59
|
-
|
60
|
-
if (path in props) {
|
61
|
-
this.debuglog("..url matched");
|
62
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
63
|
-
res.setHeader(
|
64
|
-
"Access-Control-Allow-Headers",
|
65
|
-
"Authorization,X-User-Id,X-Auth-Token",
|
66
|
-
);
|
67
|
-
res.writeHead(statusCode);
|
68
|
-
|
69
|
-
req.on("end", () => {
|
70
|
-
// assume sent data is in JSON format
|
71
|
-
this.messageStack[this.messageStack.length] = dataStr;
|
72
|
-
res.end(props[path](req.url, dataStr));
|
73
|
-
});
|
74
|
-
} else {
|
75
|
-
this.debuglog(`..no match for ${path}`);
|
76
|
-
req.on("end", () => {
|
77
|
-
// assume sent data is in JSON format
|
78
|
-
this.messageStack[this.messageStack.length] =
|
79
|
-
ERROR_NO_MATCH;
|
80
|
-
res.writeHead(ERRORCODE_NOT_FOUND);
|
81
|
-
res.end(ERROR_NO_MATCH);
|
82
|
-
});
|
83
|
-
}
|
84
|
-
});
|
85
|
-
this.server.listen(port);
|
86
|
-
}
|
87
|
-
|
88
|
-
close(): Promise<boolean> {
|
89
|
-
return new Promise((resolve, reject) => {
|
90
|
-
this.debuglog("Closing test server");
|
91
|
-
if (this.server !== undefined) {
|
92
|
-
this.server.close((error) =>
|
93
|
-
error != null ? reject(false) : resolve(true),
|
94
|
-
);
|
95
|
-
} else {
|
96
|
-
resolve(true);
|
97
|
-
}
|
98
|
-
});
|
99
|
-
}
|
100
|
-
|
101
|
-
private debuglog(str: string) {
|
102
|
-
if (this.debug) {
|
103
|
-
console.debug(str);
|
104
|
-
}
|
105
|
-
}
|
106
|
-
}
|
107
|
-
|
108
|
-
export type ListenProperties = Record<
|
109
|
-
string,
|
110
|
-
(url?: string, data?: string) => string
|
111
|
-
>;
|
@@ -1,37 +0,0 @@
|
|
1
|
-
import AWS from "aws-sdk";
|
2
|
-
import * as sinon from "sinon";
|
3
|
-
import { EnvKeys } from "../aws/runtime/environment.mjs";
|
4
|
-
import { setEnvVariable } from "../utils/utils.mjs";
|
5
|
-
|
6
|
-
setEnvVariable(EnvKeys.AWS_REGION, "eu-west-1");
|
7
|
-
const secretValue = sinon.stub();
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Stub Secrets Manager for tests. You must call this
|
11
|
-
* before you instantiate Secrets Manager(this might happen when you import the function that uses Secrets Manager).
|
12
|
-
*
|
13
|
-
* To mock the actual secret, call mockSecret()
|
14
|
-
*/
|
15
|
-
export function stubSecretsManager() {
|
16
|
-
const smStub = {
|
17
|
-
getSecretValue: secretValue,
|
18
|
-
};
|
19
|
-
|
20
|
-
sinon.stub(AWS, "SecretsManager").returns(smStub);
|
21
|
-
|
22
|
-
return smStub.getSecretValue;
|
23
|
-
}
|
24
|
-
|
25
|
-
export function mockSecret<Secret>(secret: Secret) {
|
26
|
-
if (!secret) {
|
27
|
-
secretValue.returns({
|
28
|
-
promise: sinon.stub().returns({}),
|
29
|
-
});
|
30
|
-
} else {
|
31
|
-
secretValue.returns({
|
32
|
-
promise: sinon.stub().returns({
|
33
|
-
SecretString: JSON.stringify(secret),
|
34
|
-
}),
|
35
|
-
});
|
36
|
-
}
|
37
|
-
}
|
package/src/test/testutils.mts
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
export function randomString() : string {
|
2
|
-
return Math.random().toString(36).substring(2);
|
3
|
-
}
|
4
|
-
|
5
|
-
export function getRandomNumber(min: number , max: number) : number {
|
6
|
-
return Math.random() * (max - min) + min;
|
7
|
-
}
|
8
|
-
|
9
|
-
export function getRandomNumberAsString(min: number , max: number) : string {
|
10
|
-
return getRandomNumber(min, max).toString();
|
11
|
-
}
|
12
|
-
|
13
|
-
export function getRandomInteger(min: number , max: number) : number {
|
14
|
-
return Math.round(getRandomNumber(min, max));
|
15
|
-
}
|
16
|
-
|
17
|
-
export function getRandomIntegerAsString(min: number , max: number) : string {
|
18
|
-
return Math.round(getRandomInteger(min, max)).toString();
|
19
|
-
}
|
20
|
-
|
21
|
-
export function getRandomBigInt(min: number , max: number) : bigint {
|
22
|
-
return BigInt(getRandomInteger(min, max));
|
23
|
-
}
|
24
|
-
|
25
|
-
export function randomBoolean(): boolean {
|
26
|
-
return Math.random() < 0.5;
|
27
|
-
}
|
28
|
-
|
29
|
-
/**
|
30
|
-
* Returns a new copy of an array, shuffled using Math.random()
|
31
|
-
* @param array Array
|
32
|
-
*/
|
33
|
-
export function shuffle<T>(array: T[]): T[] {
|
34
|
-
// pretty fast way to copy an array, not necessarily the fastest
|
35
|
-
const newArray = array.slice(0);
|
36
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
37
|
-
newArray.sort((x) => 0.5 - Math.random());
|
38
|
-
return newArray;
|
39
|
-
}
|
package/src/types/aws-env.mts
DELETED
package/src/types/either.mts
DELETED
package/src/types/http-error.mts
DELETED
package/src/types/language.mts
DELETED
package/src/types/nullable.mts
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
import { ValueOf } from "./util-types.mjs";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Adds `null` as an accepted type to all properties in given type.
|
5
|
-
*/
|
6
|
-
export type Nullable<Obj> = { [Key in keyof Obj]: Obj[Key] | null };
|
7
|
-
|
8
|
-
type RequiredKeys<Obj> = ValueOf<{
|
9
|
-
[Key in keyof Obj]-?: object extends { [K in Key]: Obj[Key] } ? never : Key;
|
10
|
-
}>;
|
11
|
-
type OptionalKeys<Obj> = ValueOf<{
|
12
|
-
[Key in keyof Obj]-?: object extends { [K in Key]: Obj[Key] } ? Key : never;
|
13
|
-
}>;
|
14
|
-
type RequiredProperties<Obj> = Pick<Obj, RequiredKeys<Obj>>;
|
15
|
-
type OptionalProperties<Obj> = Pick<Obj, OptionalKeys<Obj>>;
|
16
|
-
|
17
|
-
/**
|
18
|
-
* Adds `null` as an accepted type to all optional properties in given type. Required properties remain unchanged.
|
19
|
-
*/
|
20
|
-
export type NullableOptional<Obj> = RequiredProperties<Obj> &
|
21
|
-
Nullable<OptionalProperties<Obj>>;
|
package/src/types/urn.mts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export type URN<Namespace extends string, NamespaceSpecificString extends string = ""> = `urn:${Namespace}:${NamespaceSpecificString}`;
|
package/src/types/util-types.mts
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
export type ValueOf<Obj> = Obj[keyof Obj];
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Type-level conversion from readonly object to mutable object. Use with caution!
|
5
|
-
*
|
6
|
-
* Only converts in the type-level. Caution must be used, as the typescript cannot enforce readonly-ness at
|
7
|
-
* runtime. This is only useful for type-checking and especially in cases where 3rd party code requires
|
8
|
-
* mutable even though it is not needed.
|
9
|
-
*/
|
10
|
-
export type Writable<T> = { -readonly [P in keyof T]: T[P] };
|
package/src/types/validator.mts
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
export const MIN_YEAR = 1900;
|
2
|
-
export const MAX_YEAR = 2100;
|
3
|
-
|
4
|
-
export function validateYear(year: number, minYear = MIN_YEAR, maxYear = MAX_YEAR) {
|
5
|
-
return year >= minYear && year <= maxYear;
|
6
|
-
}
|
7
|
-
|
8
|
-
export function validateMonth(month: number) {
|
9
|
-
return month > 0 && month < 13;
|
10
|
-
}
|
package/src/utils/api-model.mts
DELETED
@@ -1,133 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
RestApi,
|
3
|
-
Model,
|
4
|
-
JsonSchema,
|
5
|
-
RequestValidator,
|
6
|
-
JsonSchemaType,
|
7
|
-
JsonSchemaVersion,
|
8
|
-
} from 'aws-cdk-lib/aws-apigateway';
|
9
|
-
import {ModelWithReference} from "../aws/types/model-with-reference.mjs";
|
10
|
-
|
11
|
-
/**
|
12
|
-
* Get a reference to an OpenAPI model object in a REST API.
|
13
|
-
* Can be used to supply a reference to properties of a GeoJSON feature.
|
14
|
-
* @param modelId Id of the referenced object
|
15
|
-
* @param restApiId Id of the REST API
|
16
|
-
*/
|
17
|
-
export function getModelReference(modelId: string, restApiId: string) {
|
18
|
-
return `https://apigateway.amazonaws.com/restapis/${restApiId}/models/${modelId}`;
|
19
|
-
}
|
20
|
-
|
21
|
-
/**
|
22
|
-
* Adds a request validator to a REST API to enforce request parameters/body requirements.
|
23
|
-
* https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html
|
24
|
-
* @param api REST API
|
25
|
-
*/
|
26
|
-
export function addDefaultValidator(api: RestApi): RequestValidator {
|
27
|
-
return api.addRequestValidator('DefaultValidator', {
|
28
|
-
validateRequestParameters: true,
|
29
|
-
validateRequestBody: true,
|
30
|
-
});
|
31
|
-
}
|
32
|
-
|
33
|
-
/**
|
34
|
-
* Adds a JSON Schema model to an API Gateway API. Can be used later to generate OpenAPI specifications.
|
35
|
-
* This method adds a schema for _a single object._
|
36
|
-
* @param name Name of the model
|
37
|
-
* @param api REST API
|
38
|
-
* @param schema JSON Schema
|
39
|
-
* @return ModelWithReference A model object with a reference to an API Gateway model object.
|
40
|
-
*/
|
41
|
-
export function addServiceModel(modelName: string, api: RestApi, schema: JsonSchema): ModelWithReference {
|
42
|
-
const mwr = api.addModel(modelName, {
|
43
|
-
contentType: 'application/json',
|
44
|
-
modelName,
|
45
|
-
schema,
|
46
|
-
}) as ModelWithReference;
|
47
|
-
mwr.modelReference = getModelReference(mwr.modelId, api.restApiId);
|
48
|
-
return mwr;
|
49
|
-
}
|
50
|
-
|
51
|
-
/**
|
52
|
-
* Add an empty JSON schema model to an API Gateway API.
|
53
|
-
* Usable for example if the incoming data is in XML format but some template transformation is necessary.
|
54
|
-
* @param modelName
|
55
|
-
* @param api
|
56
|
-
* @param contentType
|
57
|
-
*/
|
58
|
-
export function addSimpleServiceModel(modelName: string, api: RestApi, contentType = 'application/xml'): Model {
|
59
|
-
return api.addModel(modelName, {
|
60
|
-
contentType,
|
61
|
-
modelName,
|
62
|
-
schema: {},
|
63
|
-
});
|
64
|
-
}
|
65
|
-
|
66
|
-
/**
|
67
|
-
* Adds a JSON Schema model to an API Gateway API. Can be used later to generate OpenAPI specifications.
|
68
|
-
* This method adds a schema for _an array._
|
69
|
-
* @param model
|
70
|
-
* @param api
|
71
|
-
*/
|
72
|
-
export function createArraySchema(model: Model, api: RestApi): JsonSchema {
|
73
|
-
return {
|
74
|
-
type: JsonSchemaType.ARRAY,
|
75
|
-
items: {
|
76
|
-
ref: getModelReference(model.modelId, api.restApiId),
|
77
|
-
},
|
78
|
-
};
|
79
|
-
}
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Creates a JSON Schema for a GeoJSON Feature. Can be used to generate OpenAPI descriptions.
|
83
|
-
* @param modelReference Reference to a model object
|
84
|
-
*/
|
85
|
-
export function featureSchema(modelReference: string) {
|
86
|
-
return {
|
87
|
-
schema: JsonSchemaVersion.DRAFT4,
|
88
|
-
type: JsonSchemaType.OBJECT,
|
89
|
-
description: 'GeoJson Feature',
|
90
|
-
required: ['type', 'properties', 'geometry'],
|
91
|
-
properties: {
|
92
|
-
type: {
|
93
|
-
type: JsonSchemaType.STRING,
|
94
|
-
description: 'Feature',
|
95
|
-
enum: ['Feature'],
|
96
|
-
},
|
97
|
-
properties: {
|
98
|
-
ref: modelReference,
|
99
|
-
},
|
100
|
-
geometry: {
|
101
|
-
type: JsonSchemaType.OBJECT,
|
102
|
-
description: 'GeoJSON geometry',
|
103
|
-
},
|
104
|
-
},
|
105
|
-
};
|
106
|
-
}
|
107
|
-
|
108
|
-
/**
|
109
|
-
*
|
110
|
-
* Creates a JSON Schema for a GeoJSON Feature Collection. Can be used to generate OpenAPI descriptions.
|
111
|
-
* @param modelReference Reference to a model object, in this case Features.
|
112
|
-
*/
|
113
|
-
export function geojsonSchema(modelReference: string) {
|
114
|
-
return {
|
115
|
-
schema: JsonSchemaVersion.DRAFT4,
|
116
|
-
type: JsonSchemaType.OBJECT,
|
117
|
-
description: 'GeoJson FeatureCollection',
|
118
|
-
required: ['type', 'features'],
|
119
|
-
properties: {
|
120
|
-
type: {
|
121
|
-
type: JsonSchemaType.STRING,
|
122
|
-
description: 'FeatureCollection',
|
123
|
-
enum: ['FeatureCollection'],
|
124
|
-
},
|
125
|
-
features: {
|
126
|
-
type: JsonSchemaType.ARRAY,
|
127
|
-
items: {
|
128
|
-
ref: modelReference,
|
129
|
-
},
|
130
|
-
},
|
131
|
-
},
|
132
|
-
};
|
133
|
-
}
|
package/src/utils/base64.mts
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Decode given string from base64 to ascii
|
3
|
-
* @param str string
|
4
|
-
*/
|
5
|
-
export function decodeBase64ToAscii(str: string) {
|
6
|
-
return decodeBase64(str, 'ascii');
|
7
|
-
}
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Decode given string from base64 to given encoding
|
11
|
-
* @param str
|
12
|
-
* @param encoding
|
13
|
-
*/
|
14
|
-
export function decodeBase64(str: string, encoding: BufferEncoding) {
|
15
|
-
return Buffer.from(str, 'base64').toString(encoding);
|
16
|
-
}
|
package/src/utils/date-utils.mts
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
import { formatInTimeZone } from "date-fns-tz";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Constant for the 1970-01-01T00:00:00Z epoch Date.
|
5
|
-
*/
|
6
|
-
export const EPOCH = new Date(Date.UTC(1970, 0, 1));
|
7
|
-
|
8
|
-
export const UTC = "UTC";
|
9
|
-
|
10
|
-
export const MYSQL_DATETIME_FORMAT = "yyyy-MM-dd HH:mm";
|
11
|
-
|
12
|
-
/**
|
13
|
-
* Counts difference in milliseconds between dates.
|
14
|
-
* @param start
|
15
|
-
* @param end
|
16
|
-
*/
|
17
|
-
export function countDiffMs(start: Date, end: Date): number {
|
18
|
-
return end.getTime() - start.getTime();
|
19
|
-
}
|
20
|
-
|
21
|
-
/**
|
22
|
-
* Counts difference in seconds between dates.
|
23
|
-
* @param start
|
24
|
-
* @param end
|
25
|
-
*/
|
26
|
-
export function countDiffInSeconds(start: Date, end: Date): number {
|
27
|
-
return countDiffMs(start, end) / 1000;
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
* Converts ISO 8601 date-time -string to Date object
|
32
|
-
* @param isoString to convert
|
33
|
-
*/
|
34
|
-
export function dateFromIsoString(isoString: string): Date {
|
35
|
-
const parsed = new Date(isoString);
|
36
|
-
if (!isValidDate(parsed)) {
|
37
|
-
throw new Error(
|
38
|
-
`Could not parse iso date-time string: "${isoString}" to date`
|
39
|
-
);
|
40
|
-
}
|
41
|
-
return parsed;
|
42
|
-
}
|
43
|
-
|
44
|
-
function isValidDate(d: unknown) {
|
45
|
-
return d instanceof Date && !isNaN(d.getTime());
|
46
|
-
}
|
47
|
-
|
48
|
-
/**
|
49
|
-
* Formats a date in UTC in the given format, regardless of system time zone
|
50
|
-
*/
|
51
|
-
export function dateToUTCString(date: Date, format: string): string {
|
52
|
-
return formatInTimeZone(date, UTC, format);
|
53
|
-
}
|
@@ -1,22 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* GeoJSON types
|
3
|
-
*/
|
4
|
-
import {LineString, Point, Position} from "geojson";
|
5
|
-
|
6
|
-
export class GeoJsonPoint implements Point {
|
7
|
-
readonly type = "Point";
|
8
|
-
readonly coordinates: Position;
|
9
|
-
|
10
|
-
constructor(coordinates: Position) {
|
11
|
-
this.coordinates = coordinates;
|
12
|
-
}
|
13
|
-
}
|
14
|
-
|
15
|
-
export class GeoJsonLineString implements LineString {
|
16
|
-
readonly type = "LineString";
|
17
|
-
readonly coordinates: Position[];
|
18
|
-
|
19
|
-
constructor(coordinates: Position[]) {
|
20
|
-
this.coordinates = coordinates;
|
21
|
-
}
|
22
|
-
}
|
package/src/utils/geometry.mts
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* GeoJSON functions and tools
|
3
|
-
*/
|
4
|
-
import { Feature, FeatureCollection, Geometry, Position } from "geojson";
|
5
|
-
import * as geoJsonValidator from "geojson-validation";
|
6
|
-
import { logger } from "../aws/runtime/dt-logger-default.mjs";
|
7
|
-
|
8
|
-
export const SRID_WGS84 = 4326;
|
9
|
-
|
10
|
-
/**
|
11
|
-
* Creates WKT geometry from GeoJSON geometry
|
12
|
-
* @param geometry GeoJson geometry to convert to WKT
|
13
|
-
*/
|
14
|
-
export function createGeometry(geometry: Geometry): string {
|
15
|
-
if (geometry.type === "LineString") {
|
16
|
-
const coordinates = linestring(geometry.coordinates);
|
17
|
-
|
18
|
-
return `LINESTRING(${coordinates})`;
|
19
|
-
} else if (geometry.type === "Point") {
|
20
|
-
const coordinates = coordinatePair(geometry.coordinates);
|
21
|
-
|
22
|
-
return `POINT(${coordinates})`;
|
23
|
-
} else if (geometry.type === "Polygon") {
|
24
|
-
const coordinates = polygon(geometry.coordinates);
|
25
|
-
|
26
|
-
return `POLYGON(${coordinates})`;
|
27
|
-
} else if (geometry.type === "MultiPolygon") {
|
28
|
-
const coordinates = multiPolygon(geometry.coordinates);
|
29
|
-
|
30
|
-
return `MULTIPOLYGON(${coordinates})`;
|
31
|
-
}
|
32
|
-
|
33
|
-
logger.error({
|
34
|
-
method: "Geometry.createGeometry",
|
35
|
-
message: "Unsupported locationType " + geometry.type,
|
36
|
-
});
|
37
|
-
|
38
|
-
return "POLYGON EMPTY";
|
39
|
-
}
|
40
|
-
|
41
|
-
function linestring(coordinates: Position[]): string {
|
42
|
-
return coordinates.map((c: Position) => coordinatePair(c)).join(",");
|
43
|
-
}
|
44
|
-
|
45
|
-
function polygon(coordinates: Position[][]): string {
|
46
|
-
const list = coordinates.map((c: Position[]) => linestring(c)).join(",");
|
47
|
-
return `(${list})`;
|
48
|
-
}
|
49
|
-
|
50
|
-
function multiPolygon(coordinates: Position[][][]): string {
|
51
|
-
const list = coordinates.map((c: Position[][]) => polygon(c)).join(",");
|
52
|
-
return `(${list})`;
|
53
|
-
}
|
54
|
-
|
55
|
-
function coordinatePair(coordinate: Position): string {
|
56
|
-
return `${coordinate[0]} ${coordinate[1]}`;
|
57
|
-
}
|
58
|
-
|
59
|
-
/**
|
60
|
-
* Create a GeoJSON FeatureCollection from a list of GeoJSON features with a 'last updated' property
|
61
|
-
* @param features List of Features
|
62
|
-
* @param lastUpdated Last updated date
|
63
|
-
*/
|
64
|
-
export function createFeatureCollection(
|
65
|
-
features: Feature[],
|
66
|
-
lastUpdated: Date | null
|
67
|
-
): FeatureCollection {
|
68
|
-
return {
|
69
|
-
type: "FeatureCollection",
|
70
|
-
lastUpdated: lastUpdated,
|
71
|
-
features: features,
|
72
|
-
} as FeatureCollection;
|
73
|
-
}
|
74
|
-
|
75
|
-
export function isValidGeoJson<T>(json: T): boolean {
|
76
|
-
// Tests complain about this method returning type string[] which is obviously wrong. Therefore, this casting.
|
77
|
-
return geoJsonValidator.valid(json) as unknown as boolean;
|
78
|
-
}
|
79
|
-
|
80
|
-
export function isFeatureCollection<T>(json: T): boolean {
|
81
|
-
// Tests complain about this method returning type string[] which is obviously wrong. Therefore, this casting.
|
82
|
-
return geoJsonValidator.isFeatureCollection(json) as unknown as boolean;
|
83
|
-
}
|
84
|
-
|
85
|
-
const DEGREES_TO_RADIANS = 0.017453292519943295; // = Math.PI / 180
|
86
|
-
const EARTH_RADIUS_KM = 6371;
|
87
|
-
|
88
|
-
/**
|
89
|
-
* Returns the distance between this and given GeoJSON point in kilometers. Doesn't take in account altitude.
|
90
|
-
* Based on the following Stack Overflow question:
|
91
|
-
* http://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula,
|
92
|
-
* which is based on https://en.wikipedia.org/wiki/Haversine_formula (error rate: ~0.55%).
|
93
|
-
*/
|
94
|
-
function distanceBetweenWGS84PointsInKm(
|
95
|
-
fromXLon: number,
|
96
|
-
fromYLat: number,
|
97
|
-
toXLon: number,
|
98
|
-
toYLat: number
|
99
|
-
): number {
|
100
|
-
const diffLat = toRadians(toYLat - fromYLat);
|
101
|
-
const diffLon = toRadians(toXLon - fromXLon);
|
102
|
-
|
103
|
-
const a =
|
104
|
-
Math.sin(diffLat / 2) * Math.sin(diffLat / 2) +
|
105
|
-
Math.cos(toRadians(fromYLat)) *
|
106
|
-
Math.cos(toRadians(toYLat)) *
|
107
|
-
Math.sin(diffLon / 2) *
|
108
|
-
Math.sin(diffLon / 2);
|
109
|
-
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
110
|
-
return EARTH_RADIUS_KM * c;
|
111
|
-
}
|
112
|
-
|
113
|
-
/**
|
114
|
-
* Calculates distance between two GeoJSON points (WGS84)
|
115
|
-
* @param pos1
|
116
|
-
* @param pos2
|
117
|
-
*/
|
118
|
-
export function distanceBetweenPositionsInKm(pos1: Position, pos2: Position) {
|
119
|
-
return distanceBetweenWGS84PointsInKm(pos1[0], pos1[1], pos2[0], pos2[1]);
|
120
|
-
}
|
121
|
-
|
122
|
-
export function areDistinctPositions(previous: Position, next: Position) {
|
123
|
-
return previous[0] !== next[0] || previous[1] !== next[1];
|
124
|
-
}
|
125
|
-
|
126
|
-
/**
|
127
|
-
* Calculates distance between two GeoJSON points (WGS84)
|
128
|
-
* @param pos1
|
129
|
-
* @param pos2
|
130
|
-
*/
|
131
|
-
export function distanceBetweenPositionsInM(pos1: Position, pos2: Position) {
|
132
|
-
return distanceBetweenPositionsInKm(pos1, pos2) * 1000; // km -> m
|
133
|
-
}
|
134
|
-
|
135
|
-
export function createGmlLineString(geometry: Geometry, srsName = "EPSG:4326") {
|
136
|
-
const posList = createPosList(geometry);
|
137
|
-
|
138
|
-
return {
|
139
|
-
srsName,
|
140
|
-
posList,
|
141
|
-
};
|
142
|
-
}
|
143
|
-
|
144
|
-
function createPosList(geometry: Geometry) {
|
145
|
-
if (geometry.type === "Point") {
|
146
|
-
return positionToList(geometry.coordinates);
|
147
|
-
} else if (geometry.type === "LineString") {
|
148
|
-
return lineStringToList(geometry.coordinates);
|
149
|
-
} else if (geometry.type === "Polygon") {
|
150
|
-
return polygonToList(geometry.coordinates);
|
151
|
-
}
|
152
|
-
|
153
|
-
throw new Error("unknown geometry type " + JSON.stringify(geometry));
|
154
|
-
}
|
155
|
-
|
156
|
-
function polygonToList(positions: Position[][], precision = 8) {
|
157
|
-
return positions.map((p) => lineStringToList(p, precision)).join(" ");
|
158
|
-
}
|
159
|
-
|
160
|
-
function lineStringToList(positions: Position[], precision = 8) {
|
161
|
-
return positions.map((p) => positionToList(p, precision)).join(" ");
|
162
|
-
}
|
163
|
-
|
164
|
-
export function positionToList(position: Position, precision = 8) {
|
165
|
-
return position.map((n) => n.toPrecision(precision)).join(" ");
|
166
|
-
}
|
167
|
-
|
168
|
-
// Converts numeric degrees to radians
|
169
|
-
function toRadians(angdeg: number) {
|
170
|
-
return angdeg * DEGREES_TO_RADIANS;
|
171
|
-
}
|