@awsless/awsless 0.0.49 → 0.0.51

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
@@ -841,20 +841,29 @@ var rollupBundle = ({ format: format2 = "esm", minify = true, handler = "index.d
841
841
  const result = await bundle.generate({
842
842
  format: format2,
843
843
  sourcemap: "hidden",
844
- exports: "auto"
844
+ exports: "auto",
845
+ manualChunks: {}
845
846
  });
846
- const output = result.output[0];
847
- const code = Buffer.from(output.code, "utf8");
848
- const map = output.map ? Buffer.from(output.map.toString(), "utf8") : void 0;
849
- const hash = createHash("sha1").update(code).digest("hex");
850
- return {
851
- handler,
852
- hash,
853
- files: [{
854
- name: format2 === "esm" ? "index.mjs" : "index.js",
847
+ const hash = createHash("sha1");
848
+ const files = [];
849
+ for (const item of result.output) {
850
+ if (item.type !== "chunk") {
851
+ continue;
852
+ }
853
+ const name = item.isEntry ? format2 === "esm" ? "index.mjs" : "index.js" : item.fileName;
854
+ const code = Buffer.from(item.code, "utf8");
855
+ const map = item.map ? Buffer.from(item.map.toString(), "utf8") : void 0;
856
+ hash.update(code);
857
+ files.push({
858
+ name,
855
859
  code,
856
860
  map
857
- }]
861
+ });
862
+ }
863
+ return {
864
+ handler,
865
+ hash: hash.digest("hex"),
866
+ files
858
867
  };
859
868
  };
860
869
  };
@@ -1240,9 +1249,13 @@ var FunctionSchema = z6.union([
1240
1249
  /** The file path of the function code. */
1241
1250
  file: LocalFileSchema,
1242
1251
  /** The name of the exported method within your code that Lambda calls to run your function.
1243
- * @default 'default'
1252
+ * @default 'index.default'
1244
1253
  */
1245
1254
  handler: z6.string().optional(),
1255
+ /** Minify the function code.
1256
+ * @default true
1257
+ */
1258
+ minify: z6.boolean().optional(),
1246
1259
  /** Put the function inside your global VPC.
1247
1260
  * @default false
1248
1261
  */
@@ -1307,7 +1320,11 @@ var schema = z6.object({
1307
1320
  /** The name of the exported method within your code that Lambda calls to run your function.
1308
1321
  * @default 'default'
1309
1322
  */
1310
- handler: z6.string().default("default"),
1323
+ handler: z6.string().default("index.default"),
1324
+ /** Minify the function code.
1325
+ * @default true
1326
+ */
1327
+ minify: z6.boolean().default(true),
1311
1328
  /** Put the function inside your global VPC.
1312
1329
  * @default false
1313
1330
  */
@@ -1432,7 +1449,10 @@ var toLambdaFunction = (ctx, id, fileOrProps) => {
1432
1449
  const props = typeof fileOrProps === "string" ? { ...config.defaults?.function, file: fileOrProps } : { ...config.defaults?.function, ...fileOrProps };
1433
1450
  const lambda = new Function(id, {
1434
1451
  name: `${config.name}-${stack.name}-${id}`,
1435
- code: Code.fromFile(id, props.file, rollupBundle({ handler: `index.${props.handler}` })),
1452
+ code: Code.fromFile(id, props.file, rollupBundle({
1453
+ handler: props.handler,
1454
+ minify: props.minify
1455
+ })),
1436
1456
  ...props,
1437
1457
  vpc: void 0
1438
1458
  });
@@ -4592,6 +4612,12 @@ var Distribution = class extends Resource {
4592
4612
  Quantity: this.props.originGroups?.length ?? 0,
4593
4613
  Items: this.props.originGroups?.map((originGroup) => originGroup.toJSON()) ?? []
4594
4614
  },
4615
+ CustomErrorResponses: this.props.customErrorResponses?.map((item) => ({
4616
+ ErrorCode: item.errorCode,
4617
+ ...this.attr("ErrorCachingMinTTL", item.cacheMinTTL?.toSeconds()),
4618
+ ...this.attr("ResponseCode", item.responseCode),
4619
+ ...this.attr("ResponsePagePath", item.responsePath)
4620
+ })) ?? [],
4595
4621
  DefaultCacheBehavior: {
4596
4622
  TargetOriginId: this.props.targetOriginId,
4597
4623
  ViewerProtocolPolicy: this.props.viewerProtocol ?? "redirect-to-https",
@@ -4952,6 +4978,35 @@ var ResponseHeadersPolicy = class extends Resource {
4952
4978
  };
4953
4979
 
4954
4980
  // src/plugins/site.ts
4981
+ var ErrorResponseSchema = z24.union([
4982
+ z24.string(),
4983
+ z24.object({
4984
+ /** The path to the custom error page that you want to return to the viewer when your origin returns the HTTP status code specified.
4985
+ * @example ```
4986
+ * "/404.html"
4987
+ * ```
4988
+ *
4989
+ * We recommend that you store custom error pages in an Amazon S3 bucket.
4990
+ * If you store custom error pages on an HTTP server and the server starts to return 5xx errors,
4991
+ * CloudFront can't get the files that you want to return to viewers because the origin server is unavailable.
4992
+ */
4993
+ path: z24.string(),
4994
+ /** The HTTP status code that you want CloudFront to return to the viewer along with the custom error page.
4995
+ * 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:
4996
+ * - 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.
4997
+ * If you substitute 200, the response typically won't be intercepted.
4998
+ * - 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.
4999
+ * - You might want to return a 200 status code (OK) and static website so your customers don't know that your website is down.
5000
+ */
5001
+ statusCode: z24.number().int().positive().optional(),
5002
+ /** The minimum amount of time, that you want to cache the error response.
5003
+ * 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.
5004
+ * @example
5005
+ * "1 day"
5006
+ */
5007
+ minTTL: DurationSchema.optional()
5008
+ })
5009
+ ]).optional();
4955
5010
  var sitePlugin = definePlugin({
4956
5011
  name: "site",
4957
5012
  schema: z24.object({
@@ -4961,12 +5016,13 @@ var sitePlugin = definePlugin({
4961
5016
  * {
4962
5017
  * sites: {
4963
5018
  * SITE_NAME: {
4964
- * static: 'dist/client'
4965
- * ssr: 'dist/server/index.js'
5019
+ * domain: 'example.com',
5020
+ * static: 'dist/client',
5021
+ * ssr: 'dist/server/index.js',
4966
5022
  * }
4967
5023
  * }
4968
5024
  * }
4969
- * */
5025
+ */
4970
5026
  sites: z24.record(
4971
5027
  ResourceIdSchema,
4972
5028
  z24.object({
@@ -4977,6 +5033,31 @@ var sitePlugin = definePlugin({
4977
5033
  static: LocalDirectorySchema.optional(),
4978
5034
  /** Specifies the ssr file. */
4979
5035
  ssr: FunctionSchema.optional(),
5036
+ /** Customize the error responses for specific HTTP status codes. */
5037
+ errors: z24.object({
5038
+ /** Customize a `400 Bad Request` response */
5039
+ 400: ErrorResponseSchema,
5040
+ /** Customize a `403 Forbidden` response. */
5041
+ 403: ErrorResponseSchema,
5042
+ /** Customize a `404 Not Found` response. */
5043
+ 404: ErrorResponseSchema,
5044
+ /** Customize a `405 Method Not Allowed` response. */
5045
+ 405: ErrorResponseSchema,
5046
+ /** Customize a `414 Request-URI Too Long` response. */
5047
+ 414: ErrorResponseSchema,
5048
+ /** Customize a `416 Range Not Satisfiable` response. */
5049
+ 416: ErrorResponseSchema,
5050
+ /** Customize a `500 Internal Server Error` response. */
5051
+ 500: ErrorResponseSchema,
5052
+ /** Customize a `501 Not Implemented` response. */
5053
+ 501: ErrorResponseSchema,
5054
+ /** Customize a `502 Bad Gateway` response. */
5055
+ 502: ErrorResponseSchema,
5056
+ /** Customize a `503 Service Unavailable` response. */
5057
+ 503: ErrorResponseSchema,
5058
+ /** Customize a `504 Gateway Timeout` response. */
5059
+ 504: ErrorResponseSchema
5060
+ }).optional(),
4980
5061
  /** Define the cors headers. */
4981
5062
  cors: z24.object({
4982
5063
  override: z24.boolean().default(false),
@@ -5148,7 +5229,22 @@ var sitePlugin = definePlugin({
5148
5229
  targetOriginId: props.ssr && props.static ? "group" : props.ssr ? "lambda" : "bucket",
5149
5230
  originRequestPolicyId: originRequest.id,
5150
5231
  cachePolicyId: cache.id,
5151
- responseHeadersPolicyId: responseHeaders.id
5232
+ responseHeadersPolicyId: responseHeaders.id,
5233
+ customErrorResponses: Object.entries(props.errors ?? {}).map(([errorCode, item]) => {
5234
+ if (typeof item === "string") {
5235
+ return {
5236
+ errorCode,
5237
+ responsePath: item,
5238
+ responseCode: Number(errorCode)
5239
+ };
5240
+ }
5241
+ return {
5242
+ errorCode,
5243
+ cacheMinTTL: item.minTTL,
5244
+ responsePath: item.path,
5245
+ responseCode: item.statusCode ?? Number(errorCode)
5246
+ };
5247
+ })
5152
5248
  }).dependsOn(originRequest, responseHeaders, cache, ...deps);
5153
5249
  if (props.static) {
5154
5250
  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 };