@awsless/awsless 0.0.49 → 0.0.50

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/bin.js CHANGED
@@ -1240,9 +1240,13 @@ var FunctionSchema = z6.union([
1240
1240
  /** The file path of the function code. */
1241
1241
  file: LocalFileSchema,
1242
1242
  /** The name of the exported method within your code that Lambda calls to run your function.
1243
- * @default 'default'
1243
+ * @default 'index.default'
1244
1244
  */
1245
1245
  handler: z6.string().optional(),
1246
+ /** Minify the function code.
1247
+ * @default true
1248
+ */
1249
+ minify: z6.boolean().optional(),
1246
1250
  /** Put the function inside your global VPC.
1247
1251
  * @default false
1248
1252
  */
@@ -1307,7 +1311,11 @@ var schema = z6.object({
1307
1311
  /** The name of the exported method within your code that Lambda calls to run your function.
1308
1312
  * @default 'default'
1309
1313
  */
1310
- handler: z6.string().default("default"),
1314
+ handler: z6.string().default("index.default"),
1315
+ /** Minify the function code.
1316
+ * @default true
1317
+ */
1318
+ minify: z6.boolean().default(true),
1311
1319
  /** Put the function inside your global VPC.
1312
1320
  * @default false
1313
1321
  */
@@ -1432,7 +1440,10 @@ var toLambdaFunction = (ctx, id, fileOrProps) => {
1432
1440
  const props = typeof fileOrProps === "string" ? { ...config.defaults?.function, file: fileOrProps } : { ...config.defaults?.function, ...fileOrProps };
1433
1441
  const lambda = new Function(id, {
1434
1442
  name: `${config.name}-${stack.name}-${id}`,
1435
- code: Code.fromFile(id, props.file, rollupBundle({ handler: `index.${props.handler}` })),
1443
+ code: Code.fromFile(id, props.file, rollupBundle({
1444
+ handler: props.handler,
1445
+ minify: props.minify
1446
+ })),
1436
1447
  ...props,
1437
1448
  vpc: void 0
1438
1449
  });
@@ -4592,6 +4603,12 @@ var Distribution = class extends Resource {
4592
4603
  Quantity: this.props.originGroups?.length ?? 0,
4593
4604
  Items: this.props.originGroups?.map((originGroup) => originGroup.toJSON()) ?? []
4594
4605
  },
4606
+ CustomErrorResponses: this.props.customErrorResponses?.map((item) => ({
4607
+ ErrorCode: item.errorCode,
4608
+ ...this.attr("ErrorCachingMinTTL", item.cacheMinTTL?.toSeconds()),
4609
+ ...this.attr("ResponseCode", item.responseCode),
4610
+ ...this.attr("ResponsePagePath", item.responsePath)
4611
+ })) ?? [],
4595
4612
  DefaultCacheBehavior: {
4596
4613
  TargetOriginId: this.props.targetOriginId,
4597
4614
  ViewerProtocolPolicy: this.props.viewerProtocol ?? "redirect-to-https",
@@ -4952,6 +4969,35 @@ var ResponseHeadersPolicy = class extends Resource {
4952
4969
  };
4953
4970
 
4954
4971
  // src/plugins/site.ts
4972
+ var ErrorResponseSchema = z24.union([
4973
+ z24.string(),
4974
+ z24.object({
4975
+ /** The path to the custom error page that you want to return to the viewer when your origin returns the HTTP status code specified.
4976
+ * @example ```
4977
+ * "/404.html"
4978
+ * ```
4979
+ *
4980
+ * We recommend that you store custom error pages in an Amazon S3 bucket.
4981
+ * If you store custom error pages on an HTTP server and the server starts to return 5xx errors,
4982
+ * CloudFront can't get the files that you want to return to viewers because the origin server is unavailable.
4983
+ */
4984
+ path: z24.string(),
4985
+ /** The HTTP status code that you want CloudFront to return to the viewer along with the custom error page.
4986
+ * There are a variety of reasons that you might want CloudFront to return a status code different from the status code that your origin returned to CloudFront, for example:
4987
+ * - Some Internet devices (some firewalls and corporate proxies, for example) intercept HTTP 4xx and 5xx and prevent the response from being returned to the viewer.
4988
+ * If you substitute 200, the response typically won't be intercepted.
4989
+ * - If you don't care about distinguishing among different client errors or server errors, you can specify 400 or 500 as the ResponseCode for all 4xx or 5xx errors.
4990
+ * - You might want to return a 200 status code (OK) and static website so your customers don't know that your website is down.
4991
+ */
4992
+ statusCode: z24.number().int().positive().optional(),
4993
+ /** The minimum amount of time, that you want to cache the error response.
4994
+ * When this time period has elapsed, CloudFront queries your origin to see whether the problem that caused the error has been resolved and the requested object is now available.
4995
+ * @example
4996
+ * "1 day"
4997
+ */
4998
+ minTTL: DurationSchema.optional()
4999
+ })
5000
+ ]).optional();
4955
5001
  var sitePlugin = definePlugin({
4956
5002
  name: "site",
4957
5003
  schema: z24.object({
@@ -4961,12 +5007,13 @@ var sitePlugin = definePlugin({
4961
5007
  * {
4962
5008
  * sites: {
4963
5009
  * SITE_NAME: {
4964
- * static: 'dist/client'
4965
- * ssr: 'dist/server/index.js'
5010
+ * domain: 'example.com',
5011
+ * static: 'dist/client',
5012
+ * ssr: 'dist/server/index.js',
4966
5013
  * }
4967
5014
  * }
4968
5015
  * }
4969
- * */
5016
+ */
4970
5017
  sites: z24.record(
4971
5018
  ResourceIdSchema,
4972
5019
  z24.object({
@@ -4977,6 +5024,31 @@ var sitePlugin = definePlugin({
4977
5024
  static: LocalDirectorySchema.optional(),
4978
5025
  /** Specifies the ssr file. */
4979
5026
  ssr: FunctionSchema.optional(),
5027
+ /** Customize the error responses for specific HTTP status codes. */
5028
+ errors: z24.object({
5029
+ /** Customize a `400 Bad Request` response */
5030
+ 400: ErrorResponseSchema,
5031
+ /** Customize a `403 Forbidden` response. */
5032
+ 403: ErrorResponseSchema,
5033
+ /** Customize a `404 Not Found` response. */
5034
+ 404: ErrorResponseSchema,
5035
+ /** Customize a `405 Method Not Allowed` response. */
5036
+ 405: ErrorResponseSchema,
5037
+ /** Customize a `414 Request-URI Too Long` response. */
5038
+ 414: ErrorResponseSchema,
5039
+ /** Customize a `416 Range Not Satisfiable` response. */
5040
+ 416: ErrorResponseSchema,
5041
+ /** Customize a `500 Internal Server Error` response. */
5042
+ 500: ErrorResponseSchema,
5043
+ /** Customize a `501 Not Implemented` response. */
5044
+ 501: ErrorResponseSchema,
5045
+ /** Customize a `502 Bad Gateway` response. */
5046
+ 502: ErrorResponseSchema,
5047
+ /** Customize a `503 Service Unavailable` response. */
5048
+ 503: ErrorResponseSchema,
5049
+ /** Customize a `504 Gateway Timeout` response. */
5050
+ 504: ErrorResponseSchema
5051
+ }).optional(),
4980
5052
  /** Define the cors headers. */
4981
5053
  cors: z24.object({
4982
5054
  override: z24.boolean().default(false),
@@ -5148,7 +5220,22 @@ var sitePlugin = definePlugin({
5148
5220
  targetOriginId: props.ssr && props.static ? "group" : props.ssr ? "lambda" : "bucket",
5149
5221
  originRequestPolicyId: originRequest.id,
5150
5222
  cachePolicyId: cache.id,
5151
- responseHeadersPolicyId: responseHeaders.id
5223
+ responseHeadersPolicyId: responseHeaders.id,
5224
+ customErrorResponses: Object.entries(props.errors ?? {}).map(([errorCode, item]) => {
5225
+ if (typeof item === "string") {
5226
+ return {
5227
+ errorCode,
5228
+ responsePath: item,
5229
+ responseCode: Number(errorCode)
5230
+ };
5231
+ }
5232
+ return {
5233
+ errorCode,
5234
+ cacheMinTTL: item.minTTL,
5235
+ responsePath: item.path,
5236
+ responseCode: item.statusCode ?? Number(errorCode)
5237
+ };
5238
+ })
5152
5239
  }).dependsOn(originRequest, responseHeaders, cache, ...deps);
5153
5240
  if (props.static) {
5154
5241
  const bucketPolicy = new BucketPolicy(`site-${id}`, {
@@ -0,0 +1,78 @@
1
+ import { CloudFrontClient, CreateInvalidationCommand, waitUntilInvalidationCompleted } from '@aws-sdk/client-cloudfront';
2
+
3
+ const send = async (event, id, status, data, reason = '')=>{
4
+ const body = JSON.stringify({
5
+ Status: status,
6
+ Reason: reason,
7
+ PhysicalResourceId: id,
8
+ StackId: event.StackId,
9
+ RequestId: event.RequestId,
10
+ LogicalResourceId: event.LogicalResourceId,
11
+ NoEcho: false,
12
+ Data: data
13
+ });
14
+ // @ts-ignore
15
+ await fetch(event.ResponseURL, {
16
+ method: 'PUT',
17
+ port: 443,
18
+ body,
19
+ headers: {
20
+ 'content-type': '',
21
+ 'content-length': Buffer.from(body).byteLength
22
+ }
23
+ });
24
+ };
25
+
26
+ const client = new CloudFrontClient({});
27
+ const handler = async (event)=>{
28
+ const type = event.RequestType;
29
+ const { distributionId, paths, waitForInvalidation } = event.ResourceProperties;
30
+ console.log('Type', type);
31
+ console.log('DistributionId', distributionId);
32
+ console.log('Paths', paths);
33
+ console.log('WaitForInvalidation', waitForInvalidation);
34
+ try {
35
+ if (type === 'Update') {
36
+ const id = await invalidateCache(distributionId, paths);
37
+ if (waitForInvalidation) {
38
+ await wait(distributionId, id);
39
+ }
40
+ }
41
+ await send(event, distributionId, 'SUCCESS');
42
+ } catch (error) {
43
+ if (error instanceof Error) {
44
+ await send(event, distributionId, 'FAILED', {}, error.message);
45
+ } else {
46
+ await send(event, distributionId, 'FAILED', {}, 'Unknown error');
47
+ }
48
+ console.error(error);
49
+ }
50
+ };
51
+ const invalidateCache = async (distributionId, paths)=>{
52
+ const result = await client.send(new CreateInvalidationCommand({
53
+ DistributionId: distributionId,
54
+ InvalidationBatch: {
55
+ CallerReference: Date.now().toString(),
56
+ Paths: {
57
+ Quantity: paths.length,
58
+ Items: paths
59
+ }
60
+ }
61
+ }));
62
+ return result.Invalidation.Id;
63
+ };
64
+ const wait = async (distributionId, id)=>{
65
+ try {
66
+ await waitUntilInvalidationCompleted({
67
+ client,
68
+ maxWaitTime: 600
69
+ }, {
70
+ DistributionId: distributionId,
71
+ Id: id
72
+ });
73
+ } catch (error) {
74
+ console.error(error);
75
+ }
76
+ };
77
+
78
+ export { handler };