@digitraffic/common 2026.3.17-1 → 2026.3.26-1-beta

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 (238) hide show
  1. package/README.md +18 -1
  2. package/dist/__test__/asserter.d.ts +13 -0
  3. package/dist/__test__/asserter.js +39 -0
  4. package/dist/__test__/db-testutils.d.ts +3 -0
  5. package/dist/__test__/db-testutils.js +38 -0
  6. package/dist/__test__/dependencies.test.d.ts +1 -0
  7. package/dist/__test__/dependencies.test.js +21 -0
  8. package/dist/__test__/imports.test.d.ts +1 -0
  9. package/dist/__test__/imports.test.js +318 -0
  10. package/dist/__test__/infra/acl-builder.test.d.ts +1 -0
  11. package/dist/__test__/infra/acl-builder.test.js +72 -0
  12. package/dist/__test__/infra/api/handler-factory.test.d.ts +1 -0
  13. package/dist/__test__/infra/api/handler-factory.test.js +42 -0
  14. package/dist/__test__/infra/api/integration.test.d.ts +1 -0
  15. package/dist/__test__/infra/api/integration.test.js +162 -0
  16. package/dist/__test__/infra/api/response.test.d.ts +1 -0
  17. package/dist/__test__/infra/api/response.test.js +77 -0
  18. package/dist/__test__/infra/api/static-integration.test.d.ts +1 -0
  19. package/dist/__test__/infra/api/static-integration.test.js +35 -0
  20. package/dist/__test__/infra/documentation.test.d.ts +1 -0
  21. package/dist/__test__/infra/documentation.test.js +38 -0
  22. package/dist/__test__/infra/scheduler.test.d.ts +1 -0
  23. package/dist/__test__/infra/scheduler.test.js +23 -0
  24. package/dist/__test__/infra/security-rule.test.d.ts +1 -0
  25. package/dist/__test__/infra/security-rule.test.js +21 -0
  26. package/dist/__test__/infra/stack/rest-apis.test.d.ts +1 -0
  27. package/dist/__test__/infra/stack/rest-apis.test.js +47 -0
  28. package/dist/__test__/marine/id_utils.test.d.ts +1 -0
  29. package/dist/__test__/marine/id_utils.test.js +45 -0
  30. package/dist/__test__/mock-ky.d.ts +2 -0
  31. package/dist/__test__/mock-ky.js +15 -0
  32. package/dist/__test__/promise/promise.test.d.ts +1 -0
  33. package/dist/__test__/promise/promise.test.js +126 -0
  34. package/dist/__test__/runtime/dt-logger.test.d.ts +1 -0
  35. package/dist/__test__/runtime/dt-logger.test.js +193 -0
  36. package/dist/__test__/secrets/secret-holder.test.d.ts +1 -0
  37. package/dist/__test__/secrets/secret-holder.test.js +96 -0
  38. package/dist/__test__/secrets/secret.test.d.ts +1 -0
  39. package/dist/__test__/secrets/secret.test.js +57 -0
  40. package/dist/__test__/stack/dt-function.test.d.ts +1 -0
  41. package/dist/__test__/stack/dt-function.test.js +340 -0
  42. package/dist/__test__/stack/rest-apis.test.d.ts +1 -0
  43. package/dist/__test__/stack/rest-apis.test.js +45 -0
  44. package/dist/__test__/test/mock-ky.test.d.ts +1 -0
  45. package/dist/__test__/test/mock-ky.test.js +46 -0
  46. package/dist/__test__/testutils.d.ts +12 -0
  47. package/dist/__test__/testutils.js +32 -0
  48. package/dist/__test__/types/lambda-proxy-types.test.d.ts +8 -0
  49. package/dist/__test__/types/lambda-proxy-types.test.js +155 -0
  50. package/dist/__test__/types/lambda-response-builder.test.d.ts +1 -0
  51. package/dist/__test__/types/lambda-response-builder.test.js +81 -0
  52. package/dist/__test__/types/lambda-response.test.d.ts +9 -0
  53. package/dist/__test__/types/lambda-response.test.js +73 -0
  54. package/dist/__test__/utils/base64.test.d.ts +1 -0
  55. package/dist/__test__/utils/base64.test.js +38 -0
  56. package/dist/__test__/utils/date-utils.test.d.ts +1 -0
  57. package/dist/__test__/utils/date-utils.test.js +32 -0
  58. package/dist/__test__/utils/geometry.test.d.ts +1 -0
  59. package/dist/__test__/utils/geometry.test.js +25 -0
  60. package/dist/__test__/utils/lambda-proxy-event.test.d.ts +1 -0
  61. package/dist/__test__/utils/lambda-proxy-event.test.js +45 -0
  62. package/dist/__test__/utils/logging.test.d.ts +1 -0
  63. package/dist/__test__/utils/logging.test.js +75 -0
  64. package/dist/__test__/utils/stop-watch.test.d.ts +1 -0
  65. package/dist/__test__/utils/stop-watch.test.js +118 -0
  66. package/dist/__test__/utils/utils.test.d.ts +1 -0
  67. package/dist/__test__/utils/utils.test.js +48 -0
  68. package/dist/aws/infra/acl-builder.d.ts +53 -0
  69. package/dist/aws/infra/acl-builder.js +407 -0
  70. package/dist/aws/infra/api/handler-factory.d.ts +22 -0
  71. package/dist/aws/infra/api/handler-factory.js +68 -0
  72. package/dist/aws/infra/api/integration.d.ts +49 -0
  73. package/dist/aws/infra/api/integration.js +162 -0
  74. package/dist/aws/infra/api/response.d.ts +62 -0
  75. package/dist/aws/infra/api/response.js +132 -0
  76. package/dist/aws/infra/api/responses.d.ts +60 -0
  77. package/dist/aws/infra/api/responses.js +90 -0
  78. package/dist/aws/infra/api/static-integration.d.ts +16 -0
  79. package/dist/aws/infra/api/static-integration.js +76 -0
  80. package/dist/aws/infra/bucket-policy.d.ts +38 -0
  81. package/dist/aws/infra/bucket-policy.js +30 -0
  82. package/dist/aws/infra/canaries/canary-alarm.d.ts +6 -0
  83. package/dist/aws/infra/canaries/canary-alarm.js +20 -0
  84. package/dist/aws/infra/canaries/canary-keys.d.ts +3 -0
  85. package/dist/aws/infra/canaries/canary-keys.js +4 -0
  86. package/dist/aws/infra/canaries/canary-parameters.d.ts +19 -0
  87. package/dist/aws/infra/canaries/canary-parameters.js +2 -0
  88. package/dist/aws/infra/canaries/canary-role.d.ts +14 -0
  89. package/dist/aws/infra/canaries/canary-role.js +51 -0
  90. package/dist/aws/infra/canaries/canary.d.ts +8 -0
  91. package/dist/aws/infra/canaries/canary.js +26 -0
  92. package/dist/aws/infra/canaries/database-canary.d.ts +17 -0
  93. package/dist/aws/infra/canaries/database-canary.js +65 -0
  94. package/dist/aws/infra/canaries/database-checker.d.ts +33 -0
  95. package/dist/aws/infra/canaries/database-checker.js +119 -0
  96. package/dist/aws/infra/canaries/url-canary.d.ts +16 -0
  97. package/dist/aws/infra/canaries/url-canary.js +55 -0
  98. package/dist/aws/infra/canaries/url-checker.d.ts +45 -0
  99. package/dist/aws/infra/canaries/url-checker.js +256 -0
  100. package/dist/aws/infra/documentation.d.ts +56 -0
  101. package/dist/aws/infra/documentation.js +90 -0
  102. package/dist/aws/infra/import-util.d.ts +17 -0
  103. package/dist/aws/infra/import-util.js +41 -0
  104. package/dist/aws/infra/scheduler.d.ts +12 -0
  105. package/dist/aws/infra/scheduler.js +27 -0
  106. package/dist/aws/infra/security-rule.d.ts +12 -0
  107. package/dist/aws/infra/security-rule.js +35 -0
  108. package/dist/aws/infra/sqs-integration.d.ts +4 -0
  109. package/dist/aws/infra/sqs-integration.js +85 -0
  110. package/dist/aws/infra/sqs-queue.d.ts +19 -0
  111. package/dist/aws/infra/sqs-queue.js +145 -0
  112. package/dist/aws/infra/stack/dt-function-alarms.d.ts +29 -0
  113. package/dist/aws/infra/stack/dt-function-alarms.js +54 -0
  114. package/dist/aws/infra/stack/dt-function.d.ts +124 -0
  115. package/dist/aws/infra/stack/dt-function.js +315 -0
  116. package/dist/aws/infra/stack/lambda-configs.d.ts +44 -0
  117. package/dist/aws/infra/stack/lambda-configs.js +71 -0
  118. package/dist/aws/infra/stack/lambda-log-group.d.ts +15 -0
  119. package/dist/aws/infra/stack/lambda-log-group.js +24 -0
  120. package/dist/aws/infra/stack/monitoredfunction.d.ts +85 -0
  121. package/dist/aws/infra/stack/monitoredfunction.js +147 -0
  122. package/dist/aws/infra/stack/parameters.d.ts +40 -0
  123. package/dist/aws/infra/stack/parameters.js +50 -0
  124. package/dist/aws/infra/stack/rest-api.d.ts +74 -0
  125. package/dist/aws/infra/stack/rest-api.js +235 -0
  126. package/dist/aws/infra/stack/stack-checking-aspect.d.ts +20 -0
  127. package/dist/aws/infra/stack/stack-checking-aspect.js +183 -0
  128. package/dist/aws/infra/stack/stack.d.ts +56 -0
  129. package/dist/aws/infra/stack/stack.js +71 -0
  130. package/dist/aws/infra/stack/subscription.d.ts +17 -0
  131. package/dist/aws/infra/stack/subscription.js +37 -0
  132. package/dist/aws/infra/stacks/db-dns-stack.d.ts +13 -0
  133. package/dist/aws/infra/stacks/db-dns-stack.js +60 -0
  134. package/dist/aws/infra/stacks/db-proxy-stack.d.ts +24 -0
  135. package/dist/aws/infra/stacks/db-proxy-stack.js +74 -0
  136. package/dist/aws/infra/stacks/db-stack.d.ts +65 -0
  137. package/dist/aws/infra/stacks/db-stack.js +189 -0
  138. package/dist/aws/infra/stacks/intra-stack-configuration.d.ts +5 -0
  139. package/dist/aws/infra/stacks/intra-stack-configuration.js +2 -0
  140. package/dist/aws/infra/stacks/network-stack.d.ts +14 -0
  141. package/dist/aws/infra/stacks/network-stack.js +45 -0
  142. package/dist/aws/infra/usage-plans.d.ts +16 -0
  143. package/dist/aws/infra/usage-plans.js +38 -0
  144. package/dist/aws/runtime/apikey.d.ts +2 -0
  145. package/dist/aws/runtime/apikey.js +13 -0
  146. package/dist/aws/runtime/digitraffic-integration-response.d.ts +8 -0
  147. package/dist/aws/runtime/digitraffic-integration-response.js +25 -0
  148. package/dist/aws/runtime/dt-logger-default.d.ts +9 -0
  149. package/dist/aws/runtime/dt-logger-default.js +6 -0
  150. package/dist/aws/runtime/dt-logger.d.ts +117 -0
  151. package/dist/aws/runtime/dt-logger.js +159 -0
  152. package/dist/aws/runtime/environment.d.ts +5 -0
  153. package/dist/aws/runtime/environment.js +7 -0
  154. package/dist/aws/runtime/s3.d.ts +3 -0
  155. package/dist/aws/runtime/s3.js +21 -0
  156. package/dist/aws/runtime/secrets/dbsecret.d.ts +16 -0
  157. package/dist/aws/runtime/secrets/dbsecret.js +26 -0
  158. package/dist/aws/runtime/secrets/proxy-holder.d.ts +9 -0
  159. package/dist/aws/runtime/secrets/proxy-holder.js +25 -0
  160. package/dist/aws/runtime/secrets/rds-holder.d.ts +9 -0
  161. package/dist/aws/runtime/secrets/rds-holder.js +25 -0
  162. package/dist/aws/runtime/secrets/secret-holder.d.ts +30 -0
  163. package/dist/aws/runtime/secrets/secret-holder.js +81 -0
  164. package/dist/aws/runtime/secrets/secret.d.ts +8 -0
  165. package/dist/aws/runtime/secrets/secret.js +61 -0
  166. package/dist/aws/types/errors.d.ts +8 -0
  167. package/dist/aws/types/errors.js +13 -0
  168. package/dist/aws/types/lambda-proxy-types.d.ts +59 -0
  169. package/dist/aws/types/lambda-proxy-types.js +210 -0
  170. package/dist/aws/types/lambda-response.d.ts +89 -0
  171. package/dist/aws/types/lambda-response.js +204 -0
  172. package/dist/aws/types/mediatypes.d.ts +11 -0
  173. package/dist/aws/types/mediatypes.js +14 -0
  174. package/dist/aws/types/model-with-reference.d.ts +7 -0
  175. package/dist/aws/types/model-with-reference.js +2 -0
  176. package/dist/aws/types/tags.d.ts +2 -0
  177. package/dist/aws/types/tags.js +4 -0
  178. package/dist/database/database.d.ts +27 -0
  179. package/dist/database/database.js +95 -0
  180. package/dist/database/last-updated.d.ts +15 -0
  181. package/dist/database/last-updated.js +46 -0
  182. package/dist/database/models.d.ts +6 -0
  183. package/dist/database/models.js +2 -0
  184. package/dist/index.d.ts +2 -0
  185. package/dist/index.js +2 -0
  186. package/dist/marine/id_utils.d.ts +3 -0
  187. package/dist/marine/id_utils.js +36 -0
  188. package/dist/marine/rtz.d.ts +48 -0
  189. package/dist/marine/rtz.js +2 -0
  190. package/dist/types/async-timeout-error.d.ts +3 -0
  191. package/dist/types/async-timeout-error.js +6 -0
  192. package/dist/types/either.d.ts +9 -0
  193. package/dist/types/either.js +2 -0
  194. package/dist/types/geojson.d.ts +47 -0
  195. package/dist/types/geojson.js +51 -0
  196. package/dist/types/http-error.d.ts +4 -0
  197. package/dist/types/http-error.js +8 -0
  198. package/dist/types/input-error.d.ts +2 -0
  199. package/dist/types/input-error.js +3 -0
  200. package/dist/types/language.d.ts +5 -0
  201. package/dist/types/language.js +7 -0
  202. package/dist/types/nullable.d.ts +24 -0
  203. package/dist/types/nullable.js +2 -0
  204. package/dist/types/openapi-schema.d.ts +932 -0
  205. package/dist/types/openapi-schema.js +151 -0
  206. package/dist/types/traffictype.d.ts +11 -0
  207. package/dist/types/traffictype.js +13 -0
  208. package/dist/types/urn.d.ts +1 -0
  209. package/dist/types/urn.js +2 -0
  210. package/dist/types/util-types.d.ts +11 -0
  211. package/dist/types/util-types.js +2 -0
  212. package/dist/types/validator.d.ts +4 -0
  213. package/dist/types/validator.js +9 -0
  214. package/dist/utils/api-model.d.ts +51 -0
  215. package/dist/utils/api-model.js +118 -0
  216. package/dist/utils/base64.d.ts +34 -0
  217. package/dist/utils/base64.js +53 -0
  218. package/dist/utils/date-utils.d.ts +27 -0
  219. package/dist/utils/date-utils.js +45 -0
  220. package/dist/utils/geojson-types.d.ts +14 -0
  221. package/dist/utils/geojson-types.js +15 -0
  222. package/dist/utils/geometry.d.ts +44 -0
  223. package/dist/utils/geometry.js +154 -0
  224. package/dist/utils/lambda-proxy-event.d.ts +9 -0
  225. package/dist/utils/lambda-proxy-event.js +31 -0
  226. package/dist/utils/logging.d.ts +40 -0
  227. package/dist/utils/logging.js +88 -0
  228. package/dist/utils/retry.d.ts +33 -0
  229. package/dist/utils/retry.js +135 -0
  230. package/dist/utils/slack.d.ts +5 -0
  231. package/dist/utils/slack.js +24 -0
  232. package/dist/utils/stop-watch.d.ts +46 -0
  233. package/dist/utils/stop-watch.js +114 -0
  234. package/dist/utils/utils.d.ts +95 -0
  235. package/dist/utils/utils.js +178 -0
  236. package/dist/utils/zod-utils.d.ts +27 -0
  237. package/dist/utils/zod-utils.js +57 -0
  238. package/package.json +30 -28
@@ -0,0 +1,59 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
2
+ import { MediaType } from "./mediatypes.js";
3
+ export declare const MAX_LAMBDA_PAYLOAD_BYTES: number;
4
+ export declare const REDIRECTION_HTTP_STATUS_CODES: readonly [301, 302, 303, 307, 308];
5
+ export type RedirectionStatusCode = (typeof REDIRECTION_HTTP_STATUS_CODES)[number];
6
+ /**
7
+ * Used to build APIGatewayProxyResult response for Lambda proxy integration.
8
+ * Default status is 200, compressed = false and contentType is application/json.
9
+ */
10
+ export declare class LambdaProxyResponseBuilder {
11
+ body?: string;
12
+ isBase64Encoded: boolean;
13
+ etag?: string;
14
+ fileName?: string;
15
+ timestamp?: Date;
16
+ location?: string;
17
+ compressBody: boolean;
18
+ status: number;
19
+ contentType: string;
20
+ debug: boolean;
21
+ private sizeUncompressedBase64Bytes?;
22
+ private sizeCompressedBase64Bytes?;
23
+ static create(body?: object | string, isBase64Encoded?: boolean): LambdaProxyResponseBuilder;
24
+ withBody(body: object | string, isBase64Encoded?: boolean): this;
25
+ withFileName(fileName: string): LambdaProxyResponseBuilder;
26
+ withStatus(status: number): LambdaProxyResponseBuilder;
27
+ withTimestamp(timestamp: Date | string | undefined): LambdaProxyResponseBuilder;
28
+ withCompression(enabled?: boolean): LambdaProxyResponseBuilder;
29
+ withContentType(contentType: MediaType): LambdaProxyResponseBuilder;
30
+ withDebug(enabled?: boolean): LambdaProxyResponseBuilder;
31
+ withLocation(location: string): LambdaProxyResponseBuilder;
32
+ static internalError(error?: object | string): APIGatewayProxyResult;
33
+ static notImplemented(error?: object | string): APIGatewayProxyResult;
34
+ static badRequest(error?: object | string): APIGatewayProxyResult;
35
+ static notFound(error?: object | string): APIGatewayProxyResult;
36
+ static unauthorized(error?: object | string): APIGatewayProxyResult;
37
+ static redirect(path: string, status?: RedirectionStatusCode): APIGatewayProxyResult;
38
+ private withError;
39
+ build(): APIGatewayProxyResult;
40
+ /**
41
+ * Encodes body to base64 string that is compressed if compression was requested only if
42
+ * compressed value is smaller than uncompressed.
43
+ *
44
+ * @private
45
+ * @return Base64 encoded body string and compressed if compression was requested,
46
+ * and it's smaller than uncompressed value.
47
+ */
48
+ private encodeBody;
49
+ }
50
+ /**
51
+ * Subset of APIGatewayProxyEvent containing only query string parameters.
52
+ * Useful to be used as simple proxy lambda handler parameter when not full event:
53
+ * <code>import type { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";</code>
54
+ * is not needed.
55
+ *
56
+ * Use <code>import type { APIGatewayProxyResult } from "aws-lambda";</code>
57
+ * as return type for proxy lambda handlers.
58
+ */
59
+ export type APIGatewayProxyEventSubset = Pick<APIGatewayProxyEvent, "queryStringParameters" | "multiValueQueryStringParameters">;
@@ -0,0 +1,210 @@
1
+ import etag from "etag";
2
+ import { compressBuffer, decodeBase64ToUtf8 } from "../../utils/base64.js";
3
+ import { truncateMiddle } from "../../utils/logging.js";
4
+ import { StopWatch } from "../../utils/stop-watch.js";
5
+ import { logger } from "../runtime/dt-logger-default.js";
6
+ import { MediaType } from "./mediatypes.js";
7
+ export const MAX_LAMBDA_PAYLOAD_BYTES = 6 * 1024 * 1024; // 6 MiB
8
+ export const REDIRECTION_HTTP_STATUS_CODES = [301, 302, 303, 307, 308];
9
+ /**
10
+ * Used to build APIGatewayProxyResult response for Lambda proxy integration.
11
+ * Default status is 200, compressed = false and contentType is application/json.
12
+ */
13
+ export class LambdaProxyResponseBuilder {
14
+ body;
15
+ isBase64Encoded = false;
16
+ etag;
17
+ fileName;
18
+ timestamp;
19
+ location;
20
+ compressBody = false;
21
+ status = 200;
22
+ contentType = MediaType.APPLICATION_JSON;
23
+ debug = false;
24
+ sizeUncompressedBase64Bytes;
25
+ sizeCompressedBase64Bytes;
26
+ static create(body = "", isBase64Encoded = false) {
27
+ const builder = new LambdaProxyResponseBuilder();
28
+ if (body !== undefined) {
29
+ builder.withBody(body, isBase64Encoded);
30
+ }
31
+ return builder;
32
+ }
33
+ withBody(body, isBase64Encoded = false) {
34
+ if (isBase64Encoded && typeof body !== "string") {
35
+ throw new Error("Body must be a base64 encoded string when isBase64Encoded is true");
36
+ }
37
+ // Either body as is or stringify object
38
+ this.body = typeof body === "string" ? body : JSON.stringify(body);
39
+ this.isBase64Encoded = isBase64Encoded;
40
+ // create strong etag by default from original body
41
+ this.etag = this.isBase64Encoded
42
+ ? decodeBase64ToUtf8(this.body)
43
+ : etag(this.body);
44
+ return this;
45
+ }
46
+ withFileName(fileName) {
47
+ this.fileName = fileName;
48
+ return this;
49
+ }
50
+ withStatus(status) {
51
+ this.status = status;
52
+ return this;
53
+ }
54
+ withTimestamp(timestamp) {
55
+ if (timestamp !== undefined) {
56
+ if (typeof timestamp === "string") {
57
+ // Convert string to Date
58
+ this.timestamp = new Date(timestamp);
59
+ }
60
+ else {
61
+ this.timestamp = timestamp;
62
+ }
63
+ }
64
+ return this;
65
+ }
66
+ withCompression(enabled = true) {
67
+ if (this.isBase64Encoded && enabled) {
68
+ throw new Error("Body is already base64 encoded, it can't be compressed. Use with non encoded body with compression.");
69
+ }
70
+ this.compressBody = enabled;
71
+ return this;
72
+ }
73
+ withContentType(contentType) {
74
+ this.contentType = contentType;
75
+ return this;
76
+ }
77
+ withDebug(enabled = true) {
78
+ this.debug = enabled;
79
+ return this;
80
+ }
81
+ withLocation(location) {
82
+ this.location = location;
83
+ return this;
84
+ }
85
+ static internalError(error = "Internal Error") {
86
+ return LambdaProxyResponseBuilder.create().withError(error, 500).build();
87
+ }
88
+ static notImplemented(error = "Not Implemented") {
89
+ return LambdaProxyResponseBuilder.create().withError(error, 501).build();
90
+ }
91
+ static badRequest(error = "Bad Request") {
92
+ return LambdaProxyResponseBuilder.create().withError(error, 400).build();
93
+ }
94
+ static notFound(error = "Not Found") {
95
+ return LambdaProxyResponseBuilder.create().withError(error, 404).build();
96
+ }
97
+ static unauthorized(error = "Unauthorized") {
98
+ return LambdaProxyResponseBuilder.create().withError(error, 401).build();
99
+ }
100
+ static redirect(path, status = 302) {
101
+ return LambdaProxyResponseBuilder.create()
102
+ .withLocation(path)
103
+ .withStatus(status)
104
+ .build();
105
+ }
106
+ withError(error, status) {
107
+ return this.withStatus(status)
108
+ .withBody(error)
109
+ .withContentType(typeof error === "string"
110
+ ? MediaType.TEXT_PLAIN
111
+ : MediaType.APPLICATION_JSON);
112
+ }
113
+ build() {
114
+ const bodyNotRequired = [204, ...REDIRECTION_HTTP_STATUS_CODES].includes(this.status);
115
+ if (!this.body && !bodyNotRequired) {
116
+ throw new Error("Body is required for LambdaResponseBuilder");
117
+ }
118
+ // This needs to be called before building the response as this might modify compressBody value
119
+ const maybeEncodedBody = this.body ? this.encodeBody() : "";
120
+ const response = {
121
+ statusCode: this.status,
122
+ headers: {
123
+ "Content-Type": this.contentType,
124
+ ...(this.compressBody ? { "Content-Encoding": "gzip" } : {}),
125
+ ...(this.fileName
126
+ ? { "Content-Disposition": `attachment; filename="${this.fileName}"` }
127
+ : {}),
128
+ ...(this.location ? { Location: this.location } : {}),
129
+ ...(this.timestamp
130
+ ? { "Last-Modified": this.timestamp.toUTCString() }
131
+ : {}),
132
+ ...(this.etag ? { ETag: this.etag } : {}),
133
+ },
134
+ body: maybeEncodedBody,
135
+ isBase64Encoded: this.isBase64Encoded || this.compressBody,
136
+ };
137
+ // Count response size and return bad request if too large
138
+ const sw = StopWatch.createStarted("countResponseSize");
139
+ const responseJsonString = JSON.stringify(response);
140
+ const responseSize = Buffer.byteLength(responseJsonString);
141
+ sw.stop("countResponseSize");
142
+ if (this.debug) {
143
+ // Determine log level
144
+ const logLevel = responseSize > MAX_LAMBDA_PAYLOAD_BYTES ? "error" : "info";
145
+ logger[logLevel]({
146
+ method: "LambdaResponseBuilder.build",
147
+ message: "Built LambdaProxyResponse",
148
+ customResponse: truncateMiddle(responseJsonString, 1000),
149
+ customResponseSizeBytes: responseSize,
150
+ customTooLarge: responseSize > MAX_LAMBDA_PAYLOAD_BYTES,
151
+ });
152
+ }
153
+ // Lambda proxy integration max response size is 6 MiB
154
+ if (responseSize > MAX_LAMBDA_PAYLOAD_BYTES) {
155
+ return LambdaProxyResponseBuilder.badRequest("Response too large. Limit response size with parameters.");
156
+ }
157
+ return response;
158
+ }
159
+ /**
160
+ * Encodes body to base64 string that is compressed if compression was requested only if
161
+ * compressed value is smaller than uncompressed.
162
+ *
163
+ * @private
164
+ * @return Base64 encoded body string and compressed if compression was requested,
165
+ * and it's smaller than uncompressed value.
166
+ */
167
+ encodeBody() {
168
+ if (!this.body) {
169
+ throw new Error("Body is required for LambdaResponseBuilder");
170
+ }
171
+ if (this.isBase64Encoded || !this.compressBody) {
172
+ // Body is already base64 encoded, or we don't want to compress it -> return as is
173
+ return this.body;
174
+ }
175
+ const rawBuffer = Buffer.from(this.body, "utf8");
176
+ const sw = StopWatch.createStarted("base64");
177
+ const uncompressedBase64 = rawBuffer.toString("base64");
178
+ sw.stop("base64").start("compress");
179
+ const compressed = compressBuffer(rawBuffer);
180
+ const compressedBase64 = compressed.toString("base64");
181
+ sw.stop("compress");
182
+ this.sizeUncompressedBase64Bytes = Buffer.byteLength(uncompressedBase64);
183
+ this.sizeCompressedBase64Bytes = Buffer.byteLength(compressedBase64);
184
+ const compressionNeeded = this.sizeCompressedBase64Bytes < this.sizeUncompressedBase64Bytes;
185
+ if (this.debug) {
186
+ const compressionRatio = (this.sizeUncompressedBase64Bytes / this.sizeCompressedBase64Bytes).toFixed(1);
187
+ const spaceSavingRatio = (1.0 -
188
+ this.sizeCompressedBase64Bytes / this.sizeUncompressedBase64Bytes).toFixed(1);
189
+ logger.info({
190
+ method: "LambdaResponseBuilder.build",
191
+ message: "Compression ratio for LambdaResponse",
192
+ customCompressedBytes: this.sizeCompressedBase64Bytes,
193
+ customUncompressedBytes: this.sizeUncompressedBase64Bytes,
194
+ customCompressionRatio: compressionRatio,
195
+ customSpaceSavingRatio: spaceSavingRatio,
196
+ customCompressionEnabled: compressionNeeded,
197
+ customCompressionTookMs: sw.getDuration("compress"),
198
+ customBase64TookMs: sw.getDuration("base64"),
199
+ });
200
+ }
201
+ if (!compressionNeeded) {
202
+ // deactivate compression and return uncompressed encoded body
203
+ this.withCompression(false);
204
+ return this.body;
205
+ }
206
+ // Compression was better than uncompressed
207
+ return compressedBase64;
208
+ }
209
+ }
210
+ //# sourceMappingURL=lambda-proxy-types.js.map
@@ -0,0 +1,89 @@
1
+ export declare class LambdaResponse {
2
+ readonly status: number;
3
+ readonly body: string;
4
+ readonly fileName?: string;
5
+ readonly timestamp?: string;
6
+ readonly etag: string;
7
+ constructor(status: number, body: string, fileName?: string, timestamp?: Date, etagValue?: string);
8
+ /**
9
+ * @Deprecated Use LambdaResponseBuilder.create().withTimestamp(...).build() instead.
10
+ */
11
+ withTimestamp(timestamp: Date): LambdaResponse;
12
+ /**
13
+ * Create LambdaResponse for HTTP 200 from json.
14
+ * @Deprecated Use LambdaResponseBuilder.create().withBody(...).build() instead.
15
+ */
16
+ static okJson<T>(json: T, fileName?: string): LambdaResponse;
17
+ /**
18
+ * Create LambdaResponse for HTTP 200 from string.
19
+ * @Deprecated Use LambdaResponseBuilder.create().withBody(...).build() instead.
20
+ */
21
+ static ok(body: string, fileName?: string): LambdaResponse;
22
+ /**
23
+ * Create LambdaResponse for HTTP 200 from base64-encoded data.
24
+ * @Deprecated Use LambdaResponseBuilder.create().withBody(...).build() instead.
25
+ */
26
+ static okBinary(base64: string, fileName?: string): LambdaResponse;
27
+ /**
28
+ * Create LambdaResponse for HTTP 400
29
+ * @Deprecated Use LambdaResponseBuilder.badRequest instead.
30
+ */
31
+ static badRequest(error?: string): LambdaResponse;
32
+ /**
33
+ * Create LambdaResponse for HTTP 404
34
+ * @Deprecated Use LambdaResponseBuilder.notFound instead.
35
+ */
36
+ static notFound(error?: string): LambdaResponse;
37
+ /**
38
+ * Create LambdaResponse for HTTP 500
39
+ * @Deprecated Use LambdaResponseBuilder.internalError instead.
40
+ */
41
+ static internalError(error?: string): LambdaResponse;
42
+ /**
43
+ * Create LambdaResponse for HTTP 401
44
+ * @Deprecated Use LambdaResponseBuilder.unauthorized instead.
45
+ */
46
+ static unauthorized(error?: string): LambdaResponse;
47
+ /**
48
+ * Create LambdaResponse for HTTP 413
49
+ * @Deprecated Use LambdaResponseBuilder.badRequest instead.
50
+ */
51
+ static contentTooLarge(error?: string): LambdaResponse;
52
+ /**
53
+ * Create LambdaResponse for HTTP 501
54
+ * @Deprecated Use LambdaResponseBuilder.notImplemented instead.
55
+ */
56
+ static notImplemented(error?: string): LambdaResponse;
57
+ private static createForString;
58
+ private static createForBase64;
59
+ }
60
+ /**
61
+ * Default status is 200 and compressed = false.
62
+ */
63
+ export declare class LambdaResponseBuilder {
64
+ body?: string;
65
+ etag?: string;
66
+ fileName?: string;
67
+ timestamp?: Date;
68
+ status: number;
69
+ debug: boolean;
70
+ static create(body?: object | string): LambdaResponseBuilder;
71
+ withBody<T extends object | string>(body: T): this;
72
+ withFileName(fileName: string): LambdaResponseBuilder;
73
+ withStatus(status: number): LambdaResponseBuilder;
74
+ withTimestamp(timestamp: Date | string | undefined): LambdaResponseBuilder;
75
+ withDebug(enabled?: boolean): LambdaResponseBuilder;
76
+ static internalError(error?: string): LambdaResponse;
77
+ static notImplemented(error?: string): LambdaResponse;
78
+ static badRequest(error?: string): LambdaResponse;
79
+ static notFound(error?: string): LambdaResponse;
80
+ static unauthorized(error?: string): LambdaResponse;
81
+ private withError;
82
+ build(): LambdaResponse;
83
+ /**
84
+ * Encodes body to base64 string.
85
+ * @private
86
+ * @return Body as base64 encoded string.
87
+ */
88
+ private encodeBody;
89
+ }
@@ -0,0 +1,204 @@
1
+ import etag from "etag";
2
+ import { encodeUtf8ToBase64 } from "../../utils/base64.js";
3
+ import { truncateMiddle } from "../../utils/logging.js";
4
+ import { StopWatch } from "../../utils/stop-watch.js";
5
+ import { logger } from "../runtime/dt-logger-default.js";
6
+ import { MAX_LAMBDA_PAYLOAD_BYTES } from "./lambda-proxy-types.js";
7
+ export class LambdaResponse {
8
+ status;
9
+ body;
10
+ fileName;
11
+ timestamp;
12
+ etag;
13
+ constructor(status, body, fileName, timestamp, etagValue = etag(body)) {
14
+ this.status = status;
15
+ this.body = body;
16
+ this.fileName = fileName;
17
+ this.timestamp = timestamp?.toUTCString();
18
+ this.etag = etagValue; // create strong etag by default
19
+ }
20
+ /**
21
+ * @Deprecated Use LambdaResponseBuilder.create().withTimestamp(...).build() instead.
22
+ */
23
+ withTimestamp(timestamp) {
24
+ return new LambdaResponse(this.status, this.body, this.fileName, timestamp, this.etag);
25
+ }
26
+ /**
27
+ * Create LambdaResponse for HTTP 200 from json.
28
+ * @Deprecated Use LambdaResponseBuilder.create().withBody(...).build() instead.
29
+ */
30
+ static okJson(json, fileName) {
31
+ return LambdaResponse.ok(JSON.stringify(json), fileName);
32
+ }
33
+ /**
34
+ * Create LambdaResponse for HTTP 200 from string.
35
+ * @Deprecated Use LambdaResponseBuilder.create().withBody(...).build() instead.
36
+ */
37
+ static ok(body, fileName) {
38
+ return LambdaResponse.okBinary(toBase64(body), fileName);
39
+ }
40
+ /**
41
+ * Create LambdaResponse for HTTP 200 from base64-encoded data.
42
+ * @Deprecated Use LambdaResponseBuilder.create().withBody(...).build() instead.
43
+ */
44
+ static okBinary(base64, fileName) {
45
+ return LambdaResponse.createForBase64(200, base64, fileName);
46
+ }
47
+ /**
48
+ * Create LambdaResponse for HTTP 400
49
+ * @Deprecated Use LambdaResponseBuilder.badRequest instead.
50
+ */
51
+ static badRequest(error = "Bad Request") {
52
+ return LambdaResponse.createForString(400, error);
53
+ }
54
+ /**
55
+ * Create LambdaResponse for HTTP 404
56
+ * @Deprecated Use LambdaResponseBuilder.notFound instead.
57
+ */
58
+ static notFound(error = "Not Found") {
59
+ return LambdaResponse.createForString(404, error);
60
+ }
61
+ /**
62
+ * Create LambdaResponse for HTTP 500
63
+ * @Deprecated Use LambdaResponseBuilder.internalError instead.
64
+ */
65
+ static internalError(error = "Internal Error") {
66
+ return LambdaResponse.createForString(500, error);
67
+ }
68
+ /**
69
+ * Create LambdaResponse for HTTP 401
70
+ * @Deprecated Use LambdaResponseBuilder.unauthorized instead.
71
+ */
72
+ static unauthorized(error = "Unauthorized") {
73
+ return LambdaResponse.createForString(401, error);
74
+ }
75
+ /**
76
+ * Create LambdaResponse for HTTP 413
77
+ * @Deprecated Use LambdaResponseBuilder.badRequest instead.
78
+ */
79
+ static contentTooLarge(error = "Content Too Large") {
80
+ return LambdaResponse.createForString(413, error);
81
+ }
82
+ /**
83
+ * Create LambdaResponse for HTTP 501
84
+ * @Deprecated Use LambdaResponseBuilder.notImplemented instead.
85
+ */
86
+ static notImplemented(error = "Not Implemented") {
87
+ return LambdaResponse.createForString(501, error);
88
+ }
89
+ static createForString(status, body, fileName) {
90
+ return LambdaResponse.createForBase64(status, toBase64(body), fileName);
91
+ }
92
+ static createForBase64(status, base64Body, fileName) {
93
+ return new LambdaResponse(status, base64Body, fileName);
94
+ }
95
+ }
96
+ function toBase64(body) {
97
+ return Buffer.from(body).toString("base64");
98
+ }
99
+ /**
100
+ * Default status is 200 and compressed = false.
101
+ */
102
+ export class LambdaResponseBuilder {
103
+ body;
104
+ etag;
105
+ fileName;
106
+ timestamp;
107
+ status = 200;
108
+ debug = false;
109
+ static create(body) {
110
+ const builder = new LambdaResponseBuilder();
111
+ if (body !== undefined) {
112
+ builder.withBody(body);
113
+ }
114
+ return builder;
115
+ }
116
+ withBody(body) {
117
+ this.body = typeof body === "string" ? body : JSON.stringify(body);
118
+ // create strong etag by default from original body
119
+ this.etag = etag(this.body); // create strong etag by default from original body
120
+ return this;
121
+ }
122
+ withFileName(fileName) {
123
+ this.fileName = fileName;
124
+ return this;
125
+ }
126
+ withStatus(status) {
127
+ this.status = status;
128
+ return this;
129
+ }
130
+ withTimestamp(timestamp) {
131
+ if (timestamp !== undefined) {
132
+ if (typeof timestamp === "string") {
133
+ // Convert string to Date
134
+ this.timestamp = new Date(timestamp);
135
+ }
136
+ else {
137
+ this.timestamp = timestamp;
138
+ }
139
+ }
140
+ return this;
141
+ }
142
+ withDebug(enabled = true) {
143
+ this.debug = enabled;
144
+ return this;
145
+ }
146
+ static internalError(error = "Internal Error") {
147
+ return LambdaResponseBuilder.create().withError(error, 500).build();
148
+ }
149
+ static notImplemented(error = "Not Implemented") {
150
+ return LambdaResponseBuilder.create().withError(error, 501).build();
151
+ }
152
+ static badRequest(error = "Bad Request") {
153
+ return LambdaResponseBuilder.create().withError(error, 400).build();
154
+ }
155
+ static notFound(error = "Not Found") {
156
+ return LambdaResponseBuilder.create().withError(error, 404).build();
157
+ }
158
+ static unauthorized(error = "Unauthorized") {
159
+ return LambdaResponseBuilder.create().withError(error, 401).build();
160
+ }
161
+ withError(error, status) {
162
+ return this.withStatus(status).withBody(error);
163
+ }
164
+ build() {
165
+ if (!this.body) {
166
+ throw new Error("Body is required for LambdaResponseBuilder");
167
+ }
168
+ const encodedBody = this.encodeBody();
169
+ const response = new LambdaResponse(this.status, encodedBody, this.fileName, this.timestamp, this.etag);
170
+ // Count response size and return bad request if too large
171
+ const sw = StopWatch.createStarted("countResponseSize");
172
+ const responseJsonString = JSON.stringify(response);
173
+ const responseSize = Buffer.byteLength(responseJsonString);
174
+ sw.stop("countResponseSize");
175
+ if (this.debug) {
176
+ // Determine log level
177
+ const logLevel = responseSize > MAX_LAMBDA_PAYLOAD_BYTES ? "error" : "info";
178
+ logger[logLevel]({
179
+ method: "LambdaResponseBuilder.build",
180
+ message: "Built LambdaProxyResponse",
181
+ customResponse: truncateMiddle(responseJsonString, 1000),
182
+ customResponseSizeBytes: responseSize,
183
+ customTooLarge: responseSize > MAX_LAMBDA_PAYLOAD_BYTES,
184
+ });
185
+ }
186
+ // Lambda proxy integration max response size is 6 MiB
187
+ if (responseSize > MAX_LAMBDA_PAYLOAD_BYTES) {
188
+ return LambdaResponseBuilder.badRequest("Response too large. Limit response size with parameters.");
189
+ }
190
+ return response;
191
+ }
192
+ /**
193
+ * Encodes body to base64 string.
194
+ * @private
195
+ * @return Body as base64 encoded string.
196
+ */
197
+ encodeBody() {
198
+ if (!this.body) {
199
+ throw new Error("Body is required for LambdaResponseBuilder");
200
+ }
201
+ return encodeUtf8ToBase64(this.body);
202
+ }
203
+ }
204
+ //# sourceMappingURL=lambda-response.js.map
@@ -0,0 +1,11 @@
1
+ export declare enum MediaType {
2
+ APPLICATION_JSON = "application/json",
3
+ APPLICATION_XML = "application/xml",
4
+ APPLICATION_GEOJSON = "application/geo+json;charset=UTF-8",
5
+ IMAGE_SVG = "image/svg+xml",
6
+ IMAGE_JPEG = "image/jpeg",
7
+ TEXT_PLAIN = "text/plain",
8
+ TEXT_HTML = "text/html",
9
+ TEXT_CSV = "text/csv",
10
+ APPLICATION_JSON_UTF8 = "application/json;charset=UTF-8"
11
+ }
@@ -0,0 +1,14 @@
1
+ // charset=UTF-8 is deprecated but mobile applications sometimes needs it
2
+ export var MediaType;
3
+ (function (MediaType) {
4
+ MediaType["APPLICATION_JSON"] = "application/json";
5
+ MediaType["APPLICATION_XML"] = "application/xml";
6
+ MediaType["APPLICATION_GEOJSON"] = "application/geo+json;charset=UTF-8";
7
+ MediaType["IMAGE_SVG"] = "image/svg+xml";
8
+ MediaType["IMAGE_JPEG"] = "image/jpeg";
9
+ MediaType["TEXT_PLAIN"] = "text/plain";
10
+ MediaType["TEXT_HTML"] = "text/html";
11
+ MediaType["TEXT_CSV"] = "text/csv";
12
+ MediaType["APPLICATION_JSON_UTF8"] = "application/json;charset=UTF-8";
13
+ })(MediaType || (MediaType = {}));
14
+ //# sourceMappingURL=mediatypes.js.map
@@ -0,0 +1,7 @@
1
+ import type { Model } from "aws-cdk-lib/aws-apigateway";
2
+ /**
3
+ * Model object with a reference to an API Gateway model object.
4
+ */
5
+ export interface ModelWithReference extends Model {
6
+ modelReference: string;
7
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=model-with-reference.js.map
@@ -0,0 +1,2 @@
1
+ export declare const BETA_TAGS: string[];
2
+ export declare const DATA_V1_TAGS: string[];
@@ -0,0 +1,4 @@
1
+ // DEPRECATED! This must be replaced with new application specific tags
2
+ export const BETA_TAGS = ["Beta"];
3
+ export const DATA_V1_TAGS = ["Data v1"];
4
+ //# sourceMappingURL=tags.js.map
@@ -0,0 +1,27 @@
1
+ import { type IDatabase, type ITask } from "pg-promise";
2
+ export declare enum DatabaseEnvironmentKeys {
3
+ DB_USER = "DB_USER",
4
+ DB_PASS = "DB_PASS",
5
+ DB_URI = "DB_URI",
6
+ DB_RO_URI = "DB_RO_URI",
7
+ DB_APPLICATION = "DB_APPLICATION"
8
+ }
9
+ export type DTDatabase = IDatabase<unknown>;
10
+ export type DTTransaction = ITask<unknown>;
11
+ /**
12
+ * Creates a non-pooling database connection primarily used by Lambdas.
13
+ *
14
+ * Note! Using this method opens a new RDS connection on every invocation. It is advised to
15
+ * use RDS proxy to pool connections transparently.
16
+ * https://docs.amazonaws.cn/en_us/AmazonRDS/latest/AuroraUserGuide/rds-proxy.html
17
+ * @param username Username
18
+ * @param password Password
19
+ * @param applicationName name of application
20
+ * @param url Connection URL
21
+ * @param convertNullsToUndefined if true null values in query results will be converted to undefined, Default false.
22
+ * @param options pg-promise options
23
+ */
24
+ export declare function initDbConnection(username: string, password: string, applicationName: string, url: string, convertNullsToUndefined: boolean, options?: object): DTDatabase;
25
+ export declare function inTransaction<T>(fn: (db: DTTransaction) => Promise<T>, convertNullsToUndefined?: boolean): Promise<T>;
26
+ export declare function inDatabase<T>(fn: (db: DTDatabase) => Promise<T>, convertNullsToUndefined?: boolean): Promise<T>;
27
+ export declare function inDatabaseReadonly<T>(fn: (db: DTDatabase) => Promise<T>, convertNullsToUndefined?: boolean): Promise<T>;