sst 2.30.4 → 2.31.0

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.
@@ -2,7 +2,7 @@ import { Construct } from "constructs";
2
2
  import { Bucket, BucketProps, IBucket } from "aws-cdk-lib/aws-s3";
3
3
  import { IDistribution } from "aws-cdk-lib/aws-cloudfront";
4
4
  import { DistributionDomainProps } from "./Distribution.js";
5
- import { BaseSiteFileOptions, BaseSiteFileOptionsFilter, BaseSiteReplaceProps, BaseSiteCdkDistributionProps, BaseSiteFileOptionsDeprecated } from "./BaseSite.js";
5
+ import { BaseSiteFileOptions, BaseSiteReplaceProps, BaseSiteCdkDistributionProps } from "./BaseSite.js";
6
6
  import { SSTConstruct } from "./Construct.js";
7
7
  import { FunctionBindingProps } from "./util/functionBinding.js";
8
8
  export interface StaticSiteProps {
@@ -67,33 +67,6 @@ export interface StaticSiteProps {
67
67
  * ```
68
68
  */
69
69
  buildOutput?: string;
70
- /**
71
- * Pass in a list of file options to configure cache control for different files. Behind the scenes, the `StaticSite` construct uses a combination of the `s3 cp` and `s3 sync` commands to upload the website content to the S3 bucket. An `s3 cp` command is run for each file option block, and the options are passed in as the command options.
72
- * @default No cache control for HTML files, and a 1 year cache control for JS/CSS files.
73
- * ```js
74
- * [
75
- * {
76
- * filters: [{ exclude: "*" }, { include: "*.html" }],
77
- * cacheControl: "max-age=0,no-cache,no-store,must-revalidate",
78
- * },
79
- * {
80
- * filters: [{ exclude: "*" }, { include: "*.js" }, { include: "*.css" }],
81
- * cacheControl: "max-age=31536000,public,immutable",
82
- * },
83
- * ]
84
- * ```
85
- * @example
86
- * ```js
87
- * new StaticSite(stack, "Site", {
88
- * buildOutput: "dist",
89
- * fileOptions: [{
90
- * filters: [{ exclude: "*" }, { include: "*.js" }],
91
- * cacheControl: "max-age=31536000,public,immutable",
92
- * }]
93
- * });
94
- * ```
95
- */
96
- fileOptions?: StaticSiteFileOptions[] | StaticSiteFileOptionsDeprecated[];
97
70
  /**
98
71
  * Pass in a list of placeholder values to be replaced in the website content. For example, the follow configuration:
99
72
  *
@@ -169,6 +142,50 @@ export interface StaticSiteProps {
169
142
  * ```
170
143
  */
171
144
  purgeFiles?: boolean;
145
+ assets?: {
146
+ /**
147
+ * Character encoding for text based assets uploaded to S3 (ex: html, css, js, etc.). If "none" is specified, no charset will be returned in header.
148
+ * @default utf-8
149
+ * @example
150
+ * ```js
151
+ * assets: {
152
+ * textEncoding: "iso-8859-1"
153
+ * }
154
+ * ```
155
+ */
156
+ textEncoding?: "utf-8" | "iso-8859-1" | "windows-1252" | "ascii" | "none";
157
+ /**
158
+ * Pass in a list of file options to configure cache control for different files. Behind the scenes, the `StaticSite` construct uses a combination of the `s3 cp` and `s3 sync` commands to upload the website content to the S3 bucket. An `s3 cp` command is run for each file option block, and the options are passed in as the command options.
159
+ * @default No cache control for HTML files, and a 1 year cache control for JS/CSS files.
160
+ * ```js
161
+ * assets: {
162
+ * fileOptions: [
163
+ * {
164
+ * files: "**",
165
+ * cacheControl: "max-age=0,no-cache,no-store,must-revalidate",
166
+ * },
167
+ * {
168
+ * files: "**\/*.{js,css}",
169
+ * cacheControl: "max-age=31536000,public,immutable",
170
+ * },
171
+ * ],
172
+ * }
173
+ * ```
174
+ * @example
175
+ * ```js
176
+ * assets: {
177
+ * fileOptions: [
178
+ * {
179
+ * files: "**\/*.zip",
180
+ * cacheControl: "private,no-cache,no-store,must-revalidate",
181
+ * contentType: "application/zip",
182
+ * },
183
+ * ],
184
+ * }
185
+ * ```
186
+ */
187
+ fileOptions?: StaticSiteFileOptions[];
188
+ };
172
189
  dev?: {
173
190
  /**
174
191
  * When running `sst dev, site is not deployed. This is to ensure `sst dev` can start up quickly.
@@ -263,12 +280,7 @@ export interface StaticSiteProps {
263
280
  }
264
281
  export interface StaticSiteDomainProps extends DistributionDomainProps {
265
282
  }
266
- export interface StaticSiteFileOptionsFilter extends BaseSiteFileOptionsFilter {
267
- }
268
283
  export interface StaticSiteFileOptions extends BaseSiteFileOptions {
269
- filters: StaticSiteFileOptionsFilter[];
270
- }
271
- export interface StaticSiteFileOptionsDeprecated extends BaseSiteFileOptionsDeprecated {
272
284
  }
273
285
  export interface StaticSiteReplaceProps extends BaseSiteReplaceProps {
274
286
  }
@@ -327,6 +339,7 @@ export declare class StaticSite extends Construct implements SSTConstruct {
327
339
  };
328
340
  /** @internal */
329
341
  getFunctionBinding(): FunctionBindingProps;
342
+ private validateDeprecatedFileOptions;
330
343
  private generateViteTypes;
331
344
  private buildApp;
332
345
  private createS3Assets;
@@ -4,13 +4,11 @@ import fs from "fs";
4
4
  import crypto from "crypto";
5
5
  import { execSync } from "child_process";
6
6
  import { Construct } from "constructs";
7
- import { Token, Duration, RemovalPolicy, CustomResource, } from "aws-cdk-lib/core";
7
+ import { Token, RemovalPolicy, CustomResource } from "aws-cdk-lib/core";
8
8
  import { BlockPublicAccess, Bucket, } from "aws-cdk-lib/aws-s3";
9
9
  import { Asset } from "aws-cdk-lib/aws-s3-assets";
10
- import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
11
10
  import { Function as CfFunction, FunctionCode as CfFunctionCode, FunctionEventType as CfFunctionEventType, ViewerProtocolPolicy, } from "aws-cdk-lib/aws-cloudfront";
12
11
  import { S3Origin } from "aws-cdk-lib/aws-cloudfront-origins";
13
- import { AwsCliLayer } from "aws-cdk-lib/lambda-layer-awscli";
14
12
  import { Stack } from "./Stack.js";
15
13
  import { Distribution } from "./Distribution.js";
16
14
  import { getBuildCmdEnvironment, buildErrorResponsesFor404ErrorPage, buildErrorResponsesForRedirectToIndex, } from "./BaseSite.js";
@@ -20,6 +18,8 @@ import { getParameterPath, } from "./util/functionBinding.js";
20
18
  import { gray } from "colorette";
21
19
  import { useProject } from "../project.js";
22
20
  import { createAppContext } from "./context.js";
21
+ import { Effect, Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
22
+ import { VisibleError } from "../error.js";
23
23
  const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
24
24
  /**
25
25
  * The `StaticSite` construct is a higher level CDK construct that makes it easy to create a static website.
@@ -54,6 +54,7 @@ export class StaticSite extends Construct {
54
54
  };
55
55
  this.doNotDeploy =
56
56
  !stack.isActive || (app.mode === "dev" && !this.props.dev?.deploy);
57
+ this.validateDeprecatedFileOptions();
57
58
  this.generateViteTypes();
58
59
  useSites().add(stack.stackName, id, this.props);
59
60
  if (this.doNotDeploy) {
@@ -68,10 +69,9 @@ export class StaticSite extends Construct {
68
69
  // Build app
69
70
  this.buildApp();
70
71
  // Create S3 Deployment
71
- const cliLayer = new AwsCliLayer(this, "AwsCliLayer");
72
72
  const assets = this.createS3Assets();
73
73
  const filenamesAsset = this.bundleFilenamesAsset();
74
- const s3deployCR = this.createS3Deployment(cliLayer, assets, filenamesAsset);
74
+ const s3deployCR = this.createS3Deployment(assets, filenamesAsset);
75
75
  this.distribution.node.addDependency(s3deployCR);
76
76
  // Invalidate CloudFront
77
77
  this.distribution.createInvalidation(this.generateInvalidationId(assets));
@@ -153,6 +153,12 @@ export class StaticSite extends Construct {
153
153
  },
154
154
  };
155
155
  }
156
+ validateDeprecatedFileOptions() {
157
+ // @ts-expect-error
158
+ if (this.props.fileOptions) {
159
+ throw new VisibleError(`In the "${this.node.id}" construct, the "fileOptions" property has been replaced by "assets.fileOptions". More details on upgrading - https://docs.sst.dev/upgrade-guide#upgrade-to-v2310`);
160
+ }
161
+ }
156
162
  generateViteTypes() {
157
163
  const { path: sitePath, environment } = this.props;
158
164
  // Build the path
@@ -295,81 +301,58 @@ interface ImportMeta {
295
301
  });
296
302
  }
297
303
  }
298
- createS3Deployment(cliLayer, assets, filenamesAsset) {
299
- const fileOptions = this.props.fileOptions ?? [
300
- {
301
- filters: [{ exclude: "*" }, { include: "*.html" }],
302
- cacheControl: "max-age=0,no-cache,no-store,must-revalidate",
303
- },
304
- {
305
- filters: [{ exclude: "*" }, { include: "*.js" }, { include: "*.css" }],
306
- cacheControl: "max-age=31536000,public,immutable",
307
- },
308
- ];
309
- // Create a Lambda function that will be doing the uploading
310
- const uploader = new Function(this, "S3Uploader", {
311
- code: Code.fromAsset(path.join(__dirname, "../support/base-site-custom-resource")),
312
- layers: [cliLayer],
313
- runtime: Runtime.PYTHON_3_11,
314
- handler: "s3-upload.handler",
315
- timeout: Duration.minutes(15),
316
- memorySize: 1024,
317
- });
318
- this.bucket.grantReadWrite(uploader);
319
- assets.forEach((asset) => asset.grantRead(uploader));
320
- // Create the custom resource function
321
- const handler = new Function(this, "S3Handler", {
322
- code: Code.fromAsset(path.join(__dirname, "../support/base-site-custom-resource")),
323
- layers: [cliLayer],
324
- runtime: Runtime.PYTHON_3_11,
325
- handler: "s3-handler.handler",
326
- timeout: Duration.minutes(15),
327
- memorySize: 1024,
328
- environment: {
329
- UPLOADER_FUNCTION_NAME: uploader.functionName,
330
- },
304
+ createS3Deployment(assets, filenamesAsset) {
305
+ const stack = Stack.of(this);
306
+ const policy = new Policy(this, "S3UploaderPolicy", {
307
+ statements: [
308
+ new PolicyStatement({
309
+ effect: Effect.ALLOW,
310
+ actions: ["lambda:InvokeFunction"],
311
+ resources: [stack.customResourceHandler.functionArn],
312
+ }),
313
+ new PolicyStatement({
314
+ effect: Effect.ALLOW,
315
+ actions: ["s3:ListBucket", "s3:PutObject", "s3:DeleteObject"],
316
+ resources: [this.bucket.bucketArn, `${this.bucket.bucketArn}/*`],
317
+ }),
318
+ new PolicyStatement({
319
+ effect: Effect.ALLOW,
320
+ actions: ["s3:GetObject"],
321
+ resources: [`${assets[0].bucket.bucketArn}/*`],
322
+ }),
323
+ ],
331
324
  });
332
- this.bucket.grantReadWrite(handler);
333
- filenamesAsset?.grantRead(handler);
334
- uploader.grantInvoke(handler);
335
- // Create custom resource
336
- return new CustomResource(this, "S3Deployment", {
337
- serviceToken: handler.functionArn,
338
- resourceType: "Custom::SSTBucketDeployment",
325
+ stack.customResourceHandler.role?.attachInlinePolicy(policy);
326
+ const resource = new CustomResource(this, "S3Uploader", {
327
+ serviceToken: stack.customResourceHandler.functionArn,
328
+ resourceType: "Custom::S3Uploader",
339
329
  properties: {
340
- Sources: assets.map((asset) => ({
341
- BucketName: asset.s3BucketName,
342
- ObjectKey: asset.s3ObjectKey,
330
+ sources: assets.map((asset) => ({
331
+ bucketName: asset.s3BucketName,
332
+ objectKey: asset.s3ObjectKey,
343
333
  })),
344
- DestinationBucketName: this.bucket.bucketName,
345
- Filenames: filenamesAsset && {
346
- BucketName: filenamesAsset.s3BucketName,
347
- ObjectKey: filenamesAsset.s3ObjectKey,
334
+ destinationBucketName: this.bucket.bucketName,
335
+ filenames: filenamesAsset && {
336
+ bucketName: filenamesAsset.s3BucketName,
337
+ objectKey: filenamesAsset.s3ObjectKey,
348
338
  },
349
- FileOptions: (fileOptions || []).map((o) => {
350
- const { filters, cacheControl, contentType, contentEncoding } = o;
351
- const { include, exclude } = o;
352
- return [
353
- ...(typeof exclude === "string" ? [exclude] : exclude ?? [])
354
- .map((entry) => ["--exclude", entry])
355
- .flat(2),
356
- ...(typeof include === "string" ? [include] : include ?? [])
357
- .map((entry) => ["--include", entry])
358
- .flat(2),
359
- ...(filters || [])
360
- .map((filter) => Object.entries(filter).map(([key, value]) => [
361
- `--${key}`,
362
- value,
363
- ]))
364
- .flat(2),
365
- cacheControl ? ["--cache-control", cacheControl] : [],
366
- contentType ? ["--content-type", contentType] : [],
367
- contentEncoding ? ["--content-encoding", contentEncoding] : [],
368
- ].flat();
369
- }),
370
- ReplaceValues: this.getS3ContentReplaceValues(),
339
+ textEncoding: this.props.assets?.textEncoding ?? "utf-8",
340
+ fileOptions: [
341
+ {
342
+ files: "**",
343
+ cacheControl: "max-age=0,no-cache,no-store,must-revalidate",
344
+ },
345
+ {
346
+ files: ["**/*.js", "**/*.css"],
347
+ cacheControl: "max-age=31536000,public,immutable",
348
+ },
349
+ ...(this.props.assets?.fileOptions || []),
350
+ ],
351
+ replaceValues: this.getS3ContentReplaceValues(),
371
352
  },
372
353
  });
354
+ resource.node.addDependency(policy);
355
+ return resource;
373
356
  }
374
357
  /////////////////////
375
358
  // CloudFront Distribution
@@ -6,7 +6,7 @@ import { execSync } from "child_process";
6
6
  import { Construct } from "constructs";
7
7
  import { Token, Duration, RemovalPolicy, CustomResource, } from "aws-cdk-lib/core";
8
8
  import * as s3 from "aws-cdk-lib/aws-s3";
9
- import { Role, Effect, PolicyStatement, CompositePrincipal, ServicePrincipal, ManagedPolicy, } from "aws-cdk-lib/aws-iam";
9
+ import { Role, Effect, PolicyStatement, CompositePrincipal, ServicePrincipal, ManagedPolicy, Policy, } from "aws-cdk-lib/aws-iam";
10
10
  import * as sqs from "aws-cdk-lib/aws-sqs";
11
11
  import * as logs from "aws-cdk-lib/aws-logs";
12
12
  import * as lambda from "aws-cdk-lib/aws-lambda";
@@ -518,81 +518,67 @@ export class NextjsSite extends Construct {
518
518
  }
519
519
  }
520
520
  createS3Deployment() {
521
- // Create a Lambda function that will be doing the uploading
522
- const uploader = new lambda.Function(this, "S3Uploader", {
523
- code: lambda.Code.fromAsset(path.join(__dirname, "../../support/base-site-custom-resource")),
524
- layers: [this.awsCliLayer],
525
- runtime: lambda.Runtime.PYTHON_3_7,
526
- handler: "s3-upload.handler",
527
- timeout: Duration.minutes(15),
528
- memorySize: 1024,
529
- });
530
- this.cdk.bucket.grantReadWrite(uploader);
531
- this.assets.forEach((asset) => asset.grantRead(uploader));
532
- // Create the custom resource function
533
- const handler = new lambda.Function(this, "S3Handler", {
534
- code: lambda.Code.fromAsset(path.join(__dirname, "../../support/base-site-custom-resource")),
535
- layers: [this.awsCliLayer],
536
- runtime: lambda.Runtime.PYTHON_3_7,
537
- handler: "s3-handler.handler",
538
- timeout: Duration.minutes(15),
539
- memorySize: 1024,
540
- environment: {
541
- UPLOADER_FUNCTION_NAME: uploader.functionName,
542
- },
521
+ const stack = Stack.of(this);
522
+ const policy = new Policy(this, "S3UploaderPolicy", {
523
+ statements: [
524
+ new PolicyStatement({
525
+ effect: Effect.ALLOW,
526
+ actions: ["lambda:InvokeFunction"],
527
+ resources: [stack.customResourceHandler.functionArn],
528
+ }),
529
+ new PolicyStatement({
530
+ effect: Effect.ALLOW,
531
+ actions: ["s3:ListBucket", "s3:PutObject", "s3:DeleteObject"],
532
+ resources: [
533
+ this.cdk.bucket.bucketArn,
534
+ `${this.cdk.bucket.bucketArn}/*`,
535
+ ],
536
+ }),
537
+ new PolicyStatement({
538
+ effect: Effect.ALLOW,
539
+ actions: ["s3:GetObject"],
540
+ resources: [`${this.assets[0].bucket.bucketArn}/*`],
541
+ }),
542
+ ],
543
543
  });
544
- this.cdk.bucket.grantReadWrite(handler);
545
- uploader.grantInvoke(handler);
546
- // Create custom resource
547
- const fileOptions = [
548
- {
549
- exclude: "*",
550
- include: "public/*",
551
- cacheControl: "public,max-age=31536000,must-revalidate",
552
- },
553
- {
554
- exclude: "*",
555
- include: "static/*",
556
- cacheControl: "public,max-age=31536000,must-revalidate",
557
- },
558
- {
559
- exclude: "*",
560
- include: "static-pages/*",
561
- cacheControl: "public,max-age=0,s-maxage=2678400,must-revalidate",
562
- },
563
- {
564
- exclude: "*",
565
- include: "_next/data/*",
566
- cacheControl: "public,max-age=0,s-maxage=2678400,must-revalidate",
567
- },
568
- {
569
- exclude: "*",
570
- include: "_next/static/*",
571
- cacheControl: "public,max-age=31536000,immutable",
572
- },
573
- ];
574
- return new CustomResource(this, "S3Deployment", {
575
- serviceToken: handler.functionArn,
576
- resourceType: "Custom::SSTBucketDeployment",
544
+ stack.customResourceHandler.role?.attachInlinePolicy(policy);
545
+ const resource = new CustomResource(this, "S3Uploader", {
546
+ serviceToken: stack.customResourceHandler.functionArn,
547
+ resourceType: "Custom::S3Uploader",
577
548
  properties: {
578
- Sources: this.assets.map((asset) => ({
579
- BucketName: asset.s3BucketName,
580
- ObjectKey: asset.s3ObjectKey,
549
+ sources: this.assets.map((asset) => ({
550
+ bucketName: asset.s3BucketName,
551
+ objectKey: asset.s3ObjectKey,
581
552
  })),
582
- DestinationBucketName: this.cdk.bucket.bucketName,
583
- FileOptions: (fileOptions || []).map(({ exclude, include, cacheControl }) => {
584
- return [
585
- "--exclude",
586
- exclude,
587
- "--include",
588
- include,
589
- "--cache-control",
590
- cacheControl,
591
- ];
592
- }),
593
- ReplaceValues: this.getS3ContentReplaceValues(),
553
+ destinationBucketName: this.cdk.bucket.bucketName,
554
+ textEncoding: "UTF-8",
555
+ fileOptions: [
556
+ {
557
+ files: "/public/**",
558
+ cacheControl: "public,max-age=31536000,must-revalidate",
559
+ },
560
+ {
561
+ files: "/static/**",
562
+ cacheControl: "public,max-age=31536000,must-revalidate",
563
+ },
564
+ {
565
+ files: "/static-pages/**",
566
+ cacheControl: "public,max-age=0,s-maxage=2678400,must-revalidate",
567
+ },
568
+ {
569
+ files: "/_next/data/**",
570
+ cacheControl: "public,max-age=0,s-maxage=2678400,must-revalidate",
571
+ },
572
+ {
573
+ files: "/_next/static/**",
574
+ cacheControl: "public,max-age=31536000,immutable",
575
+ },
576
+ ],
577
+ replaceValues: this.getS3ContentReplaceValues(),
594
578
  },
595
579
  });
580
+ resource.node.addDependency(policy);
581
+ return resource;
596
582
  }
597
583
  /////////////////////
598
584
  // Build App
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "sideEffects": false,
3
3
  "name": "sst",
4
- "version": "2.30.4",
4
+ "version": "2.31.0",
5
5
  "bin": {
6
6
  "sst": "cli/sst.js"
7
7
  },
@@ -73,7 +73,7 @@
73
73
  "express": "^4.18.2",
74
74
  "fast-jwt": "^3.1.1",
75
75
  "get-port": "^6.1.2",
76
- "glob": "^8.0.3",
76
+ "glob": "^10.0.0",
77
77
  "graphql": "*",
78
78
  "graphql-yoga": "^3.9.0",
79
79
  "immer": "9",
@@ -107,20 +107,21 @@
107
107
  "@tsconfig/node16": "^1.0.3",
108
108
  "@tsconfig/node18": "^18.2.2",
109
109
  "@types/adm-zip": "^0.5.0",
110
+ "@types/async": "^3.2.22",
110
111
  "@types/aws-iot-device-sdk": "^2.2.4",
111
112
  "@types/aws-lambda": "^8.10.108",
112
113
  "@types/babel__core": "^7.1.20",
113
114
  "@types/babel__generator": "^7.6.4",
114
115
  "@types/cross-spawn": "^6.0.2",
115
116
  "@types/express": "^4.17.14",
116
- "@types/glob": "^8.0.0",
117
117
  "@types/node": "^18.11.9",
118
118
  "@types/react": "^17.0.33",
119
119
  "@types/uuid": "^8.3.4",
120
120
  "@types/ws": "^8.5.3",
121
121
  "@types/yargs": "^17.0.13",
122
122
  "archiver": "^5.3.1",
123
- "astro-sst": "2.30.4",
123
+ "astro-sst": "2.31.0",
124
+ "async": "^3.2.4",
124
125
  "tsx": "^3.12.1",
125
126
  "typescript": "^5.2.2",
126
127
  "vitest": "^0.33.0"