@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.
Files changed (212) hide show
  1. package/dist/__test__/api/handler-factory.test.d.mts +1 -0
  2. package/dist/__test__/api/handler-factory.test.mjs +43 -0
  3. package/dist/__test__/api/response.test.d.mts +1 -0
  4. package/dist/__test__/api/response.test.mjs +86 -0
  5. package/dist/__test__/imports.test.d.mts +1 -0
  6. package/dist/__test__/imports.test.mjs +332 -0
  7. package/dist/__test__/marine/id_utils.test.d.mts +1 -0
  8. package/dist/__test__/marine/id_utils.test.mjs +44 -0
  9. package/dist/__test__/promise/promise.test.d.mts +1 -0
  10. package/dist/__test__/promise/promise.test.mjs +130 -0
  11. package/dist/__test__/runtime/dt-logger.test.d.mts +1 -0
  12. package/dist/__test__/runtime/dt-logger.test.mjs +108 -0
  13. package/dist/__test__/secrets/secret-holder.test.d.mts +1 -0
  14. package/dist/__test__/secrets/secret-holder.test.mjs +86 -0
  15. package/dist/__test__/secrets/secret.test.d.mts +1 -0
  16. package/dist/__test__/secrets/secret.test.mjs +38 -0
  17. package/dist/__test__/test/httpserver.test.d.mts +1 -0
  18. package/dist/__test__/test/httpserver.test.mjs +154 -0
  19. package/dist/__test__/test/mock-ky.test.d.mts +1 -0
  20. package/dist/__test__/test/mock-ky.test.mjs +46 -0
  21. package/dist/__test__/types/lambda-response.test.d.mts +1 -0
  22. package/dist/__test__/types/lambda-response.test.mjs +58 -0
  23. package/dist/__test__/utils/date-utils.test.d.mts +1 -0
  24. package/dist/__test__/utils/date-utils.test.mjs +27 -0
  25. package/dist/__test__/utils/geometry.test.d.mts +1 -0
  26. package/dist/__test__/utils/geometry.test.mjs +24 -0
  27. package/dist/__test__/utils/logging.test.d.mts +1 -0
  28. package/dist/__test__/utils/logging.test.mjs +78 -0
  29. package/dist/__test__/utils/utils.test.d.mts +1 -0
  30. package/dist/__test__/utils/utils.test.mjs +43 -0
  31. package/dist/aws/infra/api/handler-factory.mjs +4 -0
  32. package/dist/aws/infra/api/integration.d.mts +2 -2
  33. package/dist/aws/infra/api/integration.mjs +4 -1
  34. package/dist/aws/infra/api/response.d.mts +1 -1
  35. package/dist/aws/infra/api/responses.d.mts +1 -1
  36. package/dist/aws/infra/api/responses.mjs +2 -0
  37. package/dist/aws/infra/api/static-integration.mjs +1 -1
  38. package/dist/aws/infra/canaries/canary-alarm.d.mts +1 -1
  39. package/dist/aws/infra/canaries/canary-alarm.mjs +2 -0
  40. package/dist/aws/infra/canaries/canary-parameters.mjs +1 -1
  41. package/dist/aws/infra/canaries/canary-role.mjs +1 -0
  42. package/dist/aws/infra/canaries/canary.d.mts +2 -2
  43. package/dist/aws/infra/canaries/canary.mjs +2 -0
  44. package/dist/aws/infra/canaries/database-canary.d.mts +2 -2
  45. package/dist/aws/infra/canaries/database-canary.mjs +2 -0
  46. package/dist/aws/infra/canaries/database-checker.d.mts +1 -1
  47. package/dist/aws/infra/canaries/database-checker.mjs +7 -1
  48. package/dist/aws/infra/canaries/url-canary.d.mts +2 -2
  49. package/dist/aws/infra/canaries/url-canary.mjs +3 -0
  50. package/dist/aws/infra/canaries/url-checker.d.mts +1 -1
  51. package/dist/aws/infra/canaries/url-checker.mjs +4 -1
  52. package/dist/aws/infra/documentation.mjs +5 -1
  53. package/dist/aws/infra/import-util.d.mts +1 -1
  54. package/dist/aws/infra/import-util.mjs +4 -3
  55. package/dist/aws/infra/scheduler.mjs +2 -0
  56. package/dist/aws/infra/security-rule.d.mts +1 -1
  57. package/dist/aws/infra/security-rule.mjs +1 -0
  58. package/dist/aws/infra/sqs-integration.d.mts +1 -1
  59. package/dist/aws/infra/sqs-integration.mjs +3 -1
  60. package/dist/aws/infra/sqs-queue.d.mts +1 -1
  61. package/dist/aws/infra/sqs-queue.mjs +2 -1
  62. package/dist/aws/infra/stack/lambda-configs.d.mts +4 -4
  63. package/dist/aws/infra/stack/lambda-configs.mjs +4 -2
  64. package/dist/aws/infra/stack/monitoredfunction.d.mts +3 -3
  65. package/dist/aws/infra/stack/monitoredfunction.mjs +23 -18
  66. package/dist/aws/infra/stack/parameters.mjs +1 -0
  67. package/dist/aws/infra/stack/rest_apis.d.mts +2 -2
  68. package/dist/aws/infra/stack/rest_apis.mjs +6 -1
  69. package/dist/aws/infra/stack/stack-checking-aspect.d.mts +2 -2
  70. package/dist/aws/infra/stack/stack-checking-aspect.mjs +6 -1
  71. package/dist/aws/infra/stack/stack.d.mts +5 -5
  72. package/dist/aws/infra/stack/stack.mjs +9 -0
  73. package/dist/aws/infra/stack/subscription.mjs +4 -0
  74. package/dist/aws/infra/stacks/db-dns-stack.d.mts +1 -1
  75. package/dist/aws/infra/stacks/db-dns-stack.mjs +1 -0
  76. package/dist/aws/infra/stacks/db-proxy-stack.d.mts +3 -3
  77. package/dist/aws/infra/stacks/db-proxy-stack.mjs +4 -2
  78. package/dist/aws/infra/stacks/db-stack.d.mts +3 -3
  79. package/dist/aws/infra/stacks/db-stack.mjs +11 -7
  80. package/dist/aws/infra/stacks/intra-stack-configuration.d.mts +1 -1
  81. package/dist/aws/infra/stacks/network-stack.d.mts +2 -2
  82. package/dist/aws/infra/stacks/network-stack.mjs +8 -0
  83. package/dist/aws/infra/usage-plans.d.mts +1 -1
  84. package/dist/aws/infra/usage-plans.mjs +1 -0
  85. package/dist/aws/runtime/apikey.d.mts +2 -2
  86. package/dist/aws/runtime/apikey.mjs +2 -2
  87. package/dist/aws/runtime/digitraffic-integration-response.d.mts +1 -1
  88. package/dist/aws/runtime/dt-logger.mjs +6 -2
  89. package/dist/aws/runtime/messaging.d.mts +2 -2
  90. package/dist/aws/runtime/messaging.mjs +5 -4
  91. package/dist/aws/runtime/s3.d.mts +4 -2
  92. package/dist/aws/runtime/s3.mjs +15 -10
  93. package/dist/aws/runtime/secrets/dbsecret.d.mts +1 -1
  94. package/dist/aws/runtime/secrets/proxy-holder.mjs +1 -0
  95. package/dist/aws/runtime/secrets/rds-holder.mjs +1 -0
  96. package/dist/aws/runtime/secrets/secret-holder.d.mts +1 -1
  97. package/dist/aws/runtime/secrets/secret-holder.mjs +6 -1
  98. package/dist/aws/runtime/secrets/secret.mjs +5 -6
  99. package/dist/aws/types/errors.mjs +1 -0
  100. package/dist/aws/types/lambda-response.mjs +5 -0
  101. package/dist/aws/types/model-with-reference.mjs +1 -1
  102. package/dist/database/cached.d.mts +1 -1
  103. package/dist/database/database.mjs +1 -0
  104. package/dist/database/last-updated.d.mts +1 -1
  105. package/dist/test/db-testutils.d.mts +1 -1
  106. package/dist/test/db-testutils.mjs +1 -1
  107. package/dist/test/httpserver.mjs +7 -3
  108. package/dist/test/mock-ky.d.mts +2 -0
  109. package/dist/test/mock-ky.mjs +15 -0
  110. package/dist/test/secrets-manager.d.mts +3 -2
  111. package/dist/test/secrets-manager.mjs +14 -16
  112. package/dist/test/testutils.mjs +1 -1
  113. package/dist/types/http-error.mjs +1 -0
  114. package/dist/types/nullable.d.mts +1 -1
  115. package/dist/utils/api-model.d.mts +2 -2
  116. package/dist/utils/api-model.mjs +1 -1
  117. package/dist/utils/geojson-types.d.mts +1 -1
  118. package/dist/utils/geojson-types.mjs +4 -2
  119. package/dist/utils/geometry.d.mts +1 -1
  120. package/dist/utils/geometry.mjs +3 -0
  121. package/dist/utils/logging.mjs +2 -2
  122. package/dist/utils/retry.d.mts +2 -2
  123. package/dist/utils/retry.mjs +2 -2
  124. package/dist/utils/slack.mjs +4 -3
  125. package/dist/utils/utils.d.mts +2 -2
  126. package/package.json +25 -15
  127. package/src/@types/geojson-validation/index.d.mts +0 -4
  128. package/src/aws/infra/api/handler-factory.mts +0 -86
  129. package/src/aws/infra/api/integration.mts +0 -147
  130. package/src/aws/infra/api/response.mts +0 -165
  131. package/src/aws/infra/api/responses.mts +0 -127
  132. package/src/aws/infra/api/static-integration.mts +0 -108
  133. package/src/aws/infra/canaries/Synthetics.d.mts +0 -21
  134. package/src/aws/infra/canaries/canary-alarm.mts +0 -33
  135. package/src/aws/infra/canaries/canary-keys.mts +0 -3
  136. package/src/aws/infra/canaries/canary-parameters.mts +0 -19
  137. package/src/aws/infra/canaries/canary-role.mts +0 -73
  138. package/src/aws/infra/canaries/canary.mts +0 -44
  139. package/src/aws/infra/canaries/database-canary.mts +0 -98
  140. package/src/aws/infra/canaries/database-checker.mts +0 -163
  141. package/src/aws/infra/canaries/url-canary.mts +0 -98
  142. package/src/aws/infra/canaries/url-checker.mts +0 -388
  143. package/src/aws/infra/documentation.mts +0 -142
  144. package/src/aws/infra/import-util.mts +0 -57
  145. package/src/aws/infra/scheduler.mts +0 -59
  146. package/src/aws/infra/security-rule.mts +0 -38
  147. package/src/aws/infra/sqs-integration.mts +0 -106
  148. package/src/aws/infra/sqs-queue.mts +0 -162
  149. package/src/aws/infra/stack/lambda-configs.mts +0 -135
  150. package/src/aws/infra/stack/monitoredfunction.mts +0 -352
  151. package/src/aws/infra/stack/parameters.mts +0 -74
  152. package/src/aws/infra/stack/rest_apis.mts +0 -322
  153. package/src/aws/infra/stack/stack-checking-aspect.mts +0 -233
  154. package/src/aws/infra/stack/stack.mts +0 -144
  155. package/src/aws/infra/stack/subscription.mts +0 -58
  156. package/src/aws/infra/stacks/db-dns-stack.mts +0 -77
  157. package/src/aws/infra/stacks/db-proxy-stack.mts +0 -134
  158. package/src/aws/infra/stacks/db-stack.mts +0 -292
  159. package/src/aws/infra/stacks/intra-stack-configuration.mts +0 -6
  160. package/src/aws/infra/stacks/network-stack.mts +0 -76
  161. package/src/aws/infra/usage-plans.mts +0 -50
  162. package/src/aws/runtime/apikey.mts +0 -9
  163. package/src/aws/runtime/digitraffic-integration-response.mts +0 -35
  164. package/src/aws/runtime/dt-logger-default.mts +0 -11
  165. package/src/aws/runtime/dt-logger.mts +0 -184
  166. package/src/aws/runtime/environment.mts +0 -22
  167. package/src/aws/runtime/messaging.mts +0 -26
  168. package/src/aws/runtime/s3.mts +0 -44
  169. package/src/aws/runtime/secrets/dbsecret.mts +0 -31
  170. package/src/aws/runtime/secrets/node-ttl.d.mts +0 -12
  171. package/src/aws/runtime/secrets/proxy-holder.mts +0 -34
  172. package/src/aws/runtime/secrets/rds-holder.mts +0 -34
  173. package/src/aws/runtime/secrets/secret-holder.mts +0 -106
  174. package/src/aws/runtime/secrets/secret.mts +0 -58
  175. package/src/aws/types/errors.mts +0 -14
  176. package/src/aws/types/lambda-response.mts +0 -100
  177. package/src/aws/types/mediatypes.mts +0 -12
  178. package/src/aws/types/model-with-reference.mts +0 -8
  179. package/src/aws/types/proxytypes.mts +0 -27
  180. package/src/aws/types/tags.mts +0 -3
  181. package/src/database/cached.mts +0 -64
  182. package/src/database/database.mts +0 -107
  183. package/src/database/last-updated.mts +0 -103
  184. package/src/database/models.mts +0 -7
  185. package/src/index.mts +0 -2
  186. package/src/marine/id_utils.mts +0 -30
  187. package/src/marine/rtz.mts +0 -57
  188. package/src/test/asserter.mts +0 -58
  189. package/src/test/db-testutils.mts +0 -52
  190. package/src/test/httpserver.mts +0 -111
  191. package/src/test/secrets-manager.mts +0 -37
  192. package/src/test/testutils.mts +0 -39
  193. package/src/types/async-timeout-error.mts +0 -5
  194. package/src/types/aws-env.mts +0 -3
  195. package/src/types/either.mts +0 -9
  196. package/src/types/http-error.mts +0 -8
  197. package/src/types/input-error.mts +0 -2
  198. package/src/types/language.mts +0 -3
  199. package/src/types/nullable.mts +0 -21
  200. package/src/types/traffictype.mts +0 -8
  201. package/src/types/urn.mts +0 -1
  202. package/src/types/util-types.mts +0 -10
  203. package/src/types/validator.mts +0 -10
  204. package/src/utils/api-model.mts +0 -133
  205. package/src/utils/base64.mts +0 -16
  206. package/src/utils/date-utils.mts +0 -53
  207. package/src/utils/geojson-types.mts +0 -22
  208. package/src/utils/geometry.mts +0 -171
  209. package/src/utils/logging.mts +0 -75
  210. package/src/utils/retry.mts +0 -200
  211. package/src/utils/slack.mts +0 -26
  212. package/src/utils/utils.mts +0 -184
@@ -1,98 +0,0 @@
1
- import { Role } from "aws-cdk-lib/aws-iam";
2
- import { ISecret } from "aws-cdk-lib/aws-secretsmanager";
3
- import { CfnCanary } from "aws-cdk-lib/aws-synthetics";
4
- import { LambdaEnvironment } from "../stack/lambda-configs.mjs";
5
- import { DigitrafficRestApi } from "../stack/rest_apis.mjs";
6
- import { DigitrafficStack } from "../stack/stack.mjs";
7
- import { DigitrafficCanary } from "./canary.mjs";
8
- import { ENV_API_KEY, ENV_HOSTNAME, ENV_SECRET } from "./canary-keys.mjs";
9
- import { CanaryParameters } from "./canary-parameters.mjs";
10
-
11
- export interface UrlCanaryParameters extends CanaryParameters {
12
- readonly hostname: string;
13
- readonly apiKeyId: string;
14
- readonly inVpc?: boolean;
15
- }
16
-
17
- export class UrlCanary extends DigitrafficCanary {
18
- constructor(
19
- stack: DigitrafficStack,
20
- role: Role,
21
- params: UrlCanaryParameters,
22
- secret?: ISecret
23
- ) {
24
- const canaryName = `${params.name}-url`;
25
- const environmentVariables: LambdaEnvironment = {};
26
- environmentVariables[ENV_HOSTNAME] = params.hostname;
27
-
28
- if (params.secret) {
29
- environmentVariables[ENV_SECRET] = params.secret;
30
- }
31
-
32
- if (params.apiKeyId) {
33
- environmentVariables[ENV_API_KEY] = params.apiKeyId;
34
- }
35
-
36
- if (secret) {
37
- secret.grantRead(role);
38
- }
39
-
40
- // the handler code is defined at the actual project using this
41
- super(stack, canaryName, role, params, environmentVariables);
42
-
43
- if (params.inVpc && this.node.defaultChild instanceof CfnCanary) {
44
- const subnetIds =
45
- stack.vpc === undefined
46
- ? []
47
- : stack.vpc.privateSubnets.map((subnet) => subnet.subnetId);
48
-
49
- const securityGroupIds =
50
- stack.lambdaDbSg === undefined
51
- ? []
52
- : [stack.lambdaDbSg.securityGroupId];
53
-
54
- this.node.defaultChild.vpcConfig = {
55
- vpcId: stack.vpc?.vpcId,
56
- securityGroupIds,
57
- subnetIds,
58
- };
59
- }
60
- }
61
-
62
- static create(
63
- stack: DigitrafficStack,
64
- role: Role,
65
- publicApi: DigitrafficRestApi,
66
- params: Partial<UrlCanaryParameters>,
67
- secret?: ISecret
68
- ): UrlCanary {
69
- return new UrlCanary(
70
- stack,
71
- role,
72
- {
73
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
74
- handler: `${params.name!}.handler`,
75
- hostname: publicApi.hostname(),
76
- apiKeyId: this.getApiKey(publicApi),
77
- ...params,
78
- } as UrlCanaryParameters,
79
- secret
80
- );
81
- }
82
-
83
- static getApiKey(publicApi: DigitrafficRestApi): string | undefined {
84
- const apiKeys = publicApi.apiKeyIds;
85
-
86
- if (apiKeys.length > 1) {
87
- console.info("rest api has more than one api key");
88
- }
89
-
90
- if (apiKeys.length === 0) {
91
- console.info("rest api has no api keys");
92
- return undefined;
93
- }
94
-
95
- // always use first api key
96
- return publicApi.apiKeyIds[0];
97
- }
98
- }
@@ -1,388 +0,0 @@
1
- import { IncomingMessage, RequestOptions } from "http";
2
- import { Asserter } from "../../../test/asserter.mjs";
3
-
4
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
5
- import synthetics from "Synthetics";
6
- import zlib from "zlib";
7
- import { MediaType } from "../../types/mediatypes.mjs";
8
- import { getApiKeyFromAPIGateway } from "../../runtime/apikey.mjs";
9
- import { FeatureCollection } from "geojson";
10
- import { isValidGeoJson } from "../../../utils/geometry.mjs";
11
- import { getEnvVariable } from "../../../utils/utils.mjs";
12
- import { ENV_API_KEY, ENV_HOSTNAME } from "./canary-keys.mjs";
13
- import { logger } from "../../runtime/dt-logger-default.mjs";
14
- import { logException } from "../../../utils/logging.mjs";
15
-
16
- export const API_KEY_HEADER = "x-api-key";
17
-
18
- const baseHeaders = {
19
- "Digitraffic-User": "internal-digitraffic-canary",
20
- "Accept-Encoding": "gzip",
21
- Accept: "*/*",
22
- } as Record<string, string>;
23
-
24
- type CheckerFunction = (Res: IncomingMessage) => Promise<void>;
25
- type JsonCheckerFunction<T> = (
26
- json: T,
27
- body: string,
28
- message: IncomingMessage
29
- ) => Promise<void>;
30
-
31
- export class UrlChecker {
32
- private readonly requestOptions: RequestOptions;
33
-
34
- constructor(hostname: string, apiKey?: string) {
35
- const headers = { ...baseHeaders };
36
-
37
- if (apiKey) {
38
- headers[API_KEY_HEADER] = apiKey;
39
- }
40
-
41
- this.requestOptions = {
42
- hostname,
43
- method: "GET",
44
- protocol: "https:",
45
- headers,
46
- };
47
-
48
- synthetics.getConfiguration().disableRequestMetrics();
49
-
50
- synthetics
51
- .getConfiguration()
52
- .withIncludeRequestBody(false)
53
- .withIncludeRequestHeaders(false)
54
- .withIncludeResponseBody(false)
55
- .withIncludeResponseHeaders(false)
56
- .withFailedCanaryMetric(true);
57
- }
58
-
59
- static create(hostname: string, apiKeyId: string): Promise<UrlChecker> {
60
- return getApiKeyFromAPIGateway(apiKeyId).then((apiKey) => {
61
- return new UrlChecker(hostname, apiKey.value);
62
- });
63
- }
64
-
65
- static createV2(): Promise<UrlChecker> {
66
- return this.create(
67
- getEnvVariable(ENV_HOSTNAME),
68
- getEnvVariable(ENV_API_KEY)
69
- );
70
- }
71
-
72
- expectStatus<T>(
73
- statusCode: number,
74
- url: string,
75
- callback: JsonCheckerFunction<T>
76
- ): Promise<void> {
77
- const requestOptions = {
78
- ...this.requestOptions,
79
- ...{
80
- path: url,
81
- },
82
- };
83
-
84
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
85
- return synthetics.executeHttpStep(
86
- `Verify ${statusCode} for ${url.replace(/auth=.*/, "")}`,
87
- requestOptions,
88
- callback
89
- );
90
- }
91
-
92
- expect200<T>(
93
- url: string,
94
- ...callbacks: JsonCheckerFunction<T>[]
95
- ): Promise<void> {
96
- const callback = async (
97
- json: T,
98
- body: string,
99
- res: IncomingMessage
100
- ) => {
101
- await Promise.allSettled(callbacks.map((c) => c(json, body, res)));
102
- };
103
-
104
- return this.expectStatus(200, url, callback);
105
- }
106
-
107
- expect404(url: string): Promise<void> {
108
- const requestOptions = {
109
- ...this.requestOptions,
110
- ...{
111
- path: url,
112
- },
113
- };
114
-
115
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
116
- return synthetics.executeHttpStep(
117
- `Verify 404 for ${url}`,
118
- requestOptions,
119
- validateStatusCodeAndContentType(404, MediaType.TEXT_PLAIN)
120
- );
121
- }
122
-
123
- expect400(url: string): Promise<void> {
124
- const requestOptions = {
125
- ...this.requestOptions,
126
- ...{
127
- path: url,
128
- },
129
- };
130
-
131
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
132
- return synthetics.executeHttpStep(
133
- `Verify 400 for ${url}`,
134
- requestOptions,
135
- validateStatusCodeAndContentType(400, MediaType.TEXT_PLAIN)
136
- );
137
- }
138
-
139
- expect403WithoutApiKey(url: string, mediaType?: MediaType): Promise<void> {
140
- if (
141
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
142
- !this.requestOptions.headers ||
143
- !this.requestOptions.headers[API_KEY_HEADER]
144
- ) {
145
- logger.error({
146
- method: "UrlChecker.expect403WithoutApiKey",
147
- message: "No Api-key defined",
148
- });
149
- }
150
-
151
- const requestOptions = {
152
- ...this.requestOptions,
153
- ...{
154
- path: url,
155
- headers: baseHeaders,
156
- },
157
- };
158
-
159
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
160
- return synthetics.executeHttpStep(
161
- `Verify 403 for ${url}`,
162
- requestOptions,
163
- validateStatusCodeAndContentType(
164
- 403,
165
- mediaType ?? MediaType.APPLICATION_JSON
166
- )
167
- );
168
- }
169
-
170
- done(): string {
171
- return "Canary successful";
172
- }
173
- }
174
-
175
- async function getResponseBody(response: IncomingMessage): Promise<string> {
176
- const body = await getBodyFromResponse(response);
177
-
178
- if (response.headers["content-encoding"] === "gzip") {
179
- try {
180
- return zlib.gunzipSync(body).toString();
181
- } catch (e) {
182
- logException(logger, e);
183
- }
184
- }
185
-
186
- return body.toString();
187
- }
188
-
189
- function getBodyFromResponse(response: IncomingMessage): Promise<string> {
190
- return new Promise((resolve: (value: string) => void) => {
191
- const buffers: Buffer[] = [];
192
-
193
- response.on("data", (data: Buffer) => {
194
- buffers.push(data);
195
- });
196
-
197
- response.on("end", () => {
198
- resolve(Buffer.concat(buffers).toString());
199
- });
200
- });
201
- }
202
-
203
- /**
204
- * Returns function, that validates that the status code and content-type from response are the given values
205
- * @param statusCode
206
- * @param contentType
207
- */
208
- function validateStatusCodeAndContentType(
209
- statusCode: number,
210
- contentType: MediaType
211
- ): (Res: IncomingMessage) => Promise<void> {
212
- return (res: IncomingMessage) => {
213
- return new Promise((resolve) => {
214
- if (res.statusCode !== statusCode) {
215
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
216
- throw new Error(`${res.statusCode!} ${res.statusMessage!}`);
217
- }
218
-
219
- if (res.headers["content-type"] !== contentType) {
220
- throw new Error(
221
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
222
- `Wrong content-type ${res.headers["content-type"]!}`
223
- );
224
- }
225
-
226
- resolve();
227
- });
228
- };
229
- }
230
-
231
- // DEPRECATED
232
- export class ResponseChecker {
233
- private readonly contentType;
234
- private checkCors = true;
235
-
236
- constructor(contentType: string) {
237
- this.contentType = contentType;
238
- }
239
-
240
- static forJson(): ResponseChecker {
241
- return new ResponseChecker(MediaType.APPLICATION_JSON);
242
- }
243
-
244
- static forCSV(): ResponseChecker {
245
- return new ResponseChecker(MediaType.TEXT_CSV);
246
- }
247
-
248
- static forGeojson(): ResponseChecker {
249
- return new ResponseChecker(MediaType.APPLICATION_GEOJSON);
250
- }
251
-
252
- static forJpeg(): ResponseChecker {
253
- return new ResponseChecker(MediaType.IMAGE_JPEG);
254
- }
255
-
256
- check(): CheckerFunction {
257
- return this.responseChecker(() => {
258
- // no need to do anything
259
- });
260
- }
261
-
262
- checkJson<T>(
263
- fn: (json: T, body: string, res: IncomingMessage) => void
264
- ): CheckerFunction {
265
- return this.responseChecker((body: string, res: IncomingMessage) => {
266
- fn(JSON.parse(body) as unknown as T, body, res);
267
- });
268
- }
269
-
270
- responseChecker(
271
- fn: (body: string, res: IncomingMessage) => void
272
- ): CheckerFunction {
273
- return async (res: IncomingMessage): Promise<void> => {
274
- if (!res.statusCode) {
275
- throw new Error("statusCode missing");
276
- }
277
-
278
- if (res.statusCode < 200 || res.statusCode > 299) {
279
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
280
- throw new Error(`${res.statusCode} ${res.statusMessage!}`);
281
- }
282
-
283
- if (this.checkCors && !res.headers["access-control-allow-origin"]) {
284
- throw new Error("CORS missing");
285
- }
286
-
287
- if (res.headers["content-type"] !== this.contentType) {
288
- throw new Error(
289
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
290
- `Wrong content-type ${res.headers["content-type"]!}`
291
- );
292
- }
293
-
294
- const body = await getResponseBody(res);
295
-
296
- fn(body, res);
297
- };
298
- }
299
- }
300
-
301
- export class ContentChecker {
302
- static checkJson<T>(
303
- fn: (json: T, body: string, res: IncomingMessage) => void
304
- ): CheckerFunction {
305
- return async (res: IncomingMessage): Promise<void> => {
306
- const body = await getResponseBody(res);
307
-
308
- fn(JSON.parse(body) as unknown as T, body, res);
309
- };
310
- }
311
-
312
- static checkResponse(
313
- fn: (body: string, res: IncomingMessage) => void
314
- ): CheckerFunction {
315
- return async (res: IncomingMessage): Promise<void> => {
316
- const body = await getResponseBody(res);
317
-
318
- fn(body, res);
319
- };
320
- }
321
- }
322
-
323
- export class ContentTypeChecker {
324
- static checkContentType(contentType: MediaType): CheckerFunction {
325
- return (res: IncomingMessage) => {
326
- if (!res.statusCode) {
327
- throw new Error("statusCode missing");
328
- }
329
-
330
- if (res.statusCode < 200 || res.statusCode > 299) {
331
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
332
- throw new Error(`${res.statusCode} ${res.statusMessage!}`);
333
- }
334
-
335
- if (!res.headers["access-control-allow-origin"]) {
336
- throw new Error("CORS missing");
337
- }
338
-
339
- if (res.headers["content-type"] !== contentType) {
340
- throw new Error(
341
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
342
- `Wrong content-type ${res.headers["content-type"]!}`
343
- );
344
- }
345
-
346
- return Promise.resolve();
347
- };
348
- }
349
- }
350
-
351
- export class GeoJsonChecker {
352
- static validFeatureCollection(
353
- fn?: (json: FeatureCollection) => void
354
- ): CheckerFunction {
355
- return ResponseChecker.forGeojson().checkJson(
356
- (json: FeatureCollection) => {
357
- Asserter.assertEquals(json.type, "FeatureCollection");
358
- Asserter.assertTrue(isValidGeoJson(json));
359
-
360
- if (fn) {
361
- fn(json);
362
- }
363
- }
364
- );
365
- }
366
- }
367
-
368
- export class HeaderChecker {
369
- static checkHeaderExists(headerName: string): CheckerFunction {
370
- return (res: IncomingMessage) => {
371
- if (!res.headers[headerName]) {
372
- throw new Error("Missing header: " + headerName);
373
- }
374
-
375
- return Promise.resolve();
376
- };
377
- }
378
-
379
- static checkHeaderMissing(headerName: string): CheckerFunction {
380
- return (res: IncomingMessage) => {
381
- if (res.headers[headerName]) {
382
- throw new Error("Header should not exist: " + headerName);
383
- }
384
-
385
- return Promise.resolve();
386
- };
387
- }
388
- }
@@ -1,142 +0,0 @@
1
- import { Construct } from "constructs";
2
- import { CfnDocumentationPart, Resource } from "aws-cdk-lib/aws-apigateway";
3
-
4
- // Documentation parts are objects that describe an API Gateway API or parts of an API
5
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-documenting-api.html
6
-
7
- /**
8
- * Add description to a query parameter
9
- * @param name query parameter name
10
- * @param description query parameter description
11
- * @param resource REST API resource
12
- * @param stack CloudFormation stack
13
- *
14
- * @deprecated Use DigitrafficRestApi.documentResource
15
- */
16
- export function addQueryParameterDescription(
17
- name: string,
18
- description: string,
19
- resource: Resource,
20
- stack: Construct
21
- ) {
22
- new CfnDocumentationPart(stack, `${name}Documentation`, {
23
- restApiId: resource.api.restApiId,
24
- location: {
25
- type: "QUERY_PARAMETER",
26
- name,
27
- path: resource.path,
28
- },
29
- properties: JSON.stringify({ description }),
30
- });
31
- }
32
-
33
- /**
34
- * Add a documentation part to a method
35
- * @param methodDescription
36
- * @param documentationProperties
37
- * @param resource REST API resource
38
- * @param stack CloudFormation stack
39
- */
40
- export function addDocumentation(
41
- methodDescription: string,
42
- documentationProperties: object,
43
- resource: Resource,
44
- stack: Construct
45
- ) {
46
- new CfnDocumentationPart(stack, `${methodDescription}Documentation`, {
47
- restApiId: resource.api.restApiId,
48
- location: {
49
- type: "METHOD",
50
- path: resource.path,
51
- },
52
- properties: JSON.stringify(documentationProperties),
53
- });
54
- }
55
-
56
- /**
57
- * Adds OpenAPI tags to an API method
58
- * @param methodDescription Description of API method
59
- * @param tags OpenAPI tags
60
- * @param resource REST API resource
61
- * @param stack CloudFormation stack
62
- */
63
- export function addTags(
64
- methodDescription: string,
65
- tags: string[],
66
- resource: Resource,
67
- stack: Construct
68
- ) {
69
- addDocumentation(methodDescription, { tags }, resource, stack);
70
- }
71
-
72
- /**
73
- * Adds OpenAPI tags and a method summary to an API method
74
- *
75
- * @deprecated Use DigitrafficRestApi.documentResource
76
- *
77
- * @param methodDescription Description of API method
78
- * @param tags OpenAPI tags
79
- * @param summary OpenAPI summary
80
- * @param resource REST API resource
81
- * @param stack CloudFormation stack
82
- */
83
- export function addTagsAndSummary(
84
- methodDescription: string,
85
- tags: string[],
86
- summary: string,
87
- resource: Resource,
88
- stack: Construct
89
- ) {
90
- addDocumentation(methodDescription, { tags, summary }, resource, stack);
91
- }
92
-
93
- export interface DocumentationProperties {
94
- description?: string;
95
- tags?: string[];
96
- summary?: string;
97
- deprecated?: boolean;
98
- }
99
-
100
- export class DocumentationPart {
101
- readonly parameterName: string;
102
- readonly type: string;
103
- readonly documentationProperties: DocumentationProperties;
104
-
105
- private constructor(
106
- parameterName: string,
107
- documentationProperties: DocumentationProperties,
108
- type: string
109
- ) {
110
- this.parameterName = parameterName;
111
- this.documentationProperties = documentationProperties;
112
- this.type = type;
113
- }
114
-
115
- deprecated(note: string): this {
116
- // deprecated is not supported ATM: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html
117
- this.documentationProperties.deprecated = true;
118
- this.documentationProperties.summary += ". " + note;
119
-
120
- return this;
121
- }
122
-
123
- static queryParameter(parameterName: string, description: string) {
124
- return new DocumentationPart(
125
- parameterName,
126
- { description },
127
- "QUERY_PARAMETER"
128
- );
129
- }
130
-
131
- static pathParameter(parameterName: string, description: string) {
132
- return new DocumentationPart(
133
- parameterName,
134
- { description },
135
- "PATH_PARAMETER"
136
- );
137
- }
138
-
139
- static method(tags: string[], name: string, summary: string) {
140
- return new DocumentationPart(name, { tags, summary }, "METHOD");
141
- }
142
- }
@@ -1,57 +0,0 @@
1
- import { IVpc, Vpc } from "aws-cdk-lib/aws-ec2";
2
- import { CfnOutput, Fn, Stack } from "aws-cdk-lib";
3
- import { Construct } from "constructs";
4
-
5
- export class OldStackImports {
6
- public static AURORAINSTANCE_SG_IMPORT_NAME = "AuroraSG";
7
- public static RDSPROXY_SG_IMPORT_NAME = "RDSProxySG";
8
- }
9
-
10
- /**
11
- * Import VPC from other stack outputs
12
- */
13
- export function importVpc(scope: Construct, environmentName: string): IVpc {
14
- const vpcId = importValue(environmentName, "VPCID");
15
- const privateSubnetIds = [
16
- importValue(environmentName, "digitrafficprivateASubnet"),
17
- importValue(environmentName, "digitrafficprivateBSubnet"),
18
- ];
19
- const availabilityZones = ["euw1-az1", "euw1-az2"];
20
-
21
- // VPC reference construction requires vpcId and availability zones
22
- // private subnets are used in Lambda configuration
23
- return Vpc.fromVpcAttributes(scope, "vpc", {
24
- vpcId,
25
- privateSubnetIds,
26
- availabilityZones,
27
- });
28
- }
29
-
30
- /**
31
- * Import value from other stack output. Stack outputs are named with
32
- * digitraffic-${environmentName}-${name} pattern and this function takes care of it
33
- */
34
- export function importValue(environmentName: string, name: string): string {
35
- return Fn.importValue(outputName(environmentName, name));
36
- }
37
-
38
- /**
39
- * Export value as stack output. Use same naming pattern as importValue.
40
- */
41
- export function exportValue(
42
- stack: Stack,
43
- environmentName: string,
44
- name: string,
45
- value: string
46
- ) {
47
- const exportName = outputName(environmentName, name);
48
-
49
- new CfnOutput(stack, exportName, {
50
- exportName,
51
- value,
52
- });
53
- }
54
-
55
- export function outputName(environmentName: string, name: string): string {
56
- return `digitraffic-${environmentName}-${name}`;
57
- }