carlin 1.49.21 → 2.1.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.
- package/dist/index.mjs +301 -36
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -589,7 +589,7 @@ const copyRoot404To404Index = async ({ bucket }) => {
|
|
|
589
589
|
throw error;
|
|
590
590
|
}
|
|
591
591
|
};
|
|
592
|
-
const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory }) => {
|
|
592
|
+
const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory, uploadSourceMaps = false }) => {
|
|
593
593
|
log.info(logPrefix$18, `Uploading directory ${directory}/ to ${bucket}/${bucketKey}...`);
|
|
594
594
|
const allFiles = await getAllFilesInsideADirectory({ directory });
|
|
595
595
|
/**
|
|
@@ -597,12 +597,24 @@ const uploadDirectoryToS3 = async ({ bucket, bucketKey = "", directory }) => {
|
|
|
597
597
|
* error. Discovered at #16 https://github.com/ttoss/carlin/issues/16.
|
|
598
598
|
*/
|
|
599
599
|
if (allFiles.length === 0) throw new Error(`Directory ${directory}/ has no files.`);
|
|
600
|
-
|
|
600
|
+
/**
|
|
601
|
+
* Source maps expose the application's original source, and the bucket this
|
|
602
|
+
* uploads to is public, so they are excluded unless explicitly requested.
|
|
603
|
+
*
|
|
604
|
+
* This runs AFTER the empty-directory check on purpose: that error means
|
|
605
|
+
* "the build folder is probably wrong", and a directory containing only
|
|
606
|
+
* source maps must not borrow it to say something else.
|
|
607
|
+
*/
|
|
608
|
+
const files = uploadSourceMaps ? allFiles : allFiles.filter((file) => {
|
|
609
|
+
return !file.endsWith(".map");
|
|
610
|
+
});
|
|
611
|
+
if (files.length !== allFiles.length) log.info(logPrefix$18, `Skipping ${allFiles.length - files.length} source map file(s). Pass --upload-source-maps to include them.`);
|
|
612
|
+
const numberOfGroups = Math.ceil(files.length / 63);
|
|
601
613
|
/**
|
|
602
614
|
* Divide all files and create "numberOfGroups" groups of files whose max
|
|
603
615
|
* length is GROUP_MAX_LENGTH.
|
|
604
616
|
*/
|
|
605
|
-
const aoaOfFiles =
|
|
617
|
+
const aoaOfFiles = files.reduce((acc, file, index) => {
|
|
606
618
|
const groupIndex = index % numberOfGroups;
|
|
607
619
|
if (!acc[groupIndex]) acc[groupIndex] = [];
|
|
608
620
|
acc[index % numberOfGroups].push(file);
|
|
@@ -689,6 +701,16 @@ const emptyS3Directory = async ({ bucket, directory = "" }) => {
|
|
|
689
701
|
* Delete old S3 files based on retention period.
|
|
690
702
|
* Files older than the specified number of days will be deleted.
|
|
691
703
|
*/
|
|
704
|
+
/**
|
|
705
|
+
* These three exemptions are pre-existing: the function is unchanged by the
|
|
706
|
+
* commit that added them, and it violates the same rules on `main`. They only
|
|
707
|
+
* surface now because lint-staged lints whole files that appear in the diff,
|
|
708
|
+
* and an unrelated change elsewhere in this file put it there.
|
|
709
|
+
*
|
|
710
|
+
* Do not read this as a licence to grow the function. Tracked for a proper
|
|
711
|
+
* refactor — deletion logic should not be restructured inside a PR about
|
|
712
|
+
* something else.
|
|
713
|
+
*/
|
|
692
714
|
const deleteOldS3Files = async ({ bucket, continuationToken, directory = "", retentionDays, totalDeleted = 0 }) => {
|
|
693
715
|
if (!continuationToken) log.info(logPrefix$18, `Deleting files older than ${retentionDays} days from ${bucket}/${directory}...`);
|
|
694
716
|
try {
|
|
@@ -3298,28 +3320,6 @@ const reportToGitHubPR = async () => {
|
|
|
3298
3320
|
log.info(logPrefix$6, existingComment ? "PR comment updated." : "PR comment created.");
|
|
3299
3321
|
};
|
|
3300
3322
|
//#endregion
|
|
3301
|
-
//#region src/deploy/staticApp/findDefaultBuildFolder.ts
|
|
3302
|
-
/**
|
|
3303
|
-
* Fixes #20 https://github.com/ttoss/carlin/issues/20
|
|
3304
|
-
*/
|
|
3305
|
-
const defaultBuildFolders = [
|
|
3306
|
-
"build",
|
|
3307
|
-
"out",
|
|
3308
|
-
"storybook-static",
|
|
3309
|
-
"dist"
|
|
3310
|
-
];
|
|
3311
|
-
const findDefaultBuildFolder = async () => {
|
|
3312
|
-
return (await Promise.all(defaultBuildFolders.map(async (directory) => {
|
|
3313
|
-
return {
|
|
3314
|
-
directory,
|
|
3315
|
-
isValid: (await getAllFilesInsideADirectory({ directory })).length !== 0
|
|
3316
|
-
};
|
|
3317
|
-
}))).reduce((acc, cur) => {
|
|
3318
|
-
if (cur.isValid) return cur.directory;
|
|
3319
|
-
return acc;
|
|
3320
|
-
}, "");
|
|
3321
|
-
};
|
|
3322
|
-
//#endregion
|
|
3323
3323
|
//#region src/deploy/staticApp/getStaticAppBucket.ts
|
|
3324
3324
|
const STATIC_APP_BUCKET_LOGICAL_ID$1 = "StaticBucket";
|
|
3325
3325
|
const getStaticAppBucket = async ({ stackName }) => {
|
|
@@ -3370,11 +3370,186 @@ const invalidateCloudFront = async ({ outputs }) => {
|
|
|
3370
3370
|
} else log.info(logPrefix$5, `Cannot invalidate because distribution does not exist.`);
|
|
3371
3371
|
};
|
|
3372
3372
|
//#endregion
|
|
3373
|
+
//#region src/deploy/staticApp/responseHeaders.ts
|
|
3374
|
+
/**
|
|
3375
|
+
* The header names emitted by the `SecurityHeadersConfig` of the baseline
|
|
3376
|
+
* policy, mapped to the config key that emits them. Each key is optional in
|
|
3377
|
+
* `SecurityHeadersConfig`, so a user-defined header of the same name replaces
|
|
3378
|
+
* the baseline one by removing the key.
|
|
3379
|
+
*/
|
|
3380
|
+
const SECURITY_HEADERS_CONFIG_KEYS = {
|
|
3381
|
+
"content-security-policy": "ContentSecurityPolicy",
|
|
3382
|
+
"referrer-policy": "ReferrerPolicy",
|
|
3383
|
+
"strict-transport-security": "StrictTransportSecurity",
|
|
3384
|
+
"x-content-type-options": "ContentTypeOptions",
|
|
3385
|
+
"x-frame-options": "FrameOptions",
|
|
3386
|
+
"x-xss-protection": "XSSProtection"
|
|
3387
|
+
};
|
|
3388
|
+
/**
|
|
3389
|
+
* The header names emitted by the `CorsConfig` of the baseline policy.
|
|
3390
|
+
* `CorsConfig` requires its fields as a unit, so a single header cannot be
|
|
3391
|
+
* replaced without inventing semantics for the others. Defining one of these
|
|
3392
|
+
* is an error instead.
|
|
3393
|
+
*/
|
|
3394
|
+
const CORS_CONFIG_HEADERS = [
|
|
3395
|
+
"access-control-allow-credentials",
|
|
3396
|
+
"access-control-allow-headers",
|
|
3397
|
+
"access-control-allow-methods",
|
|
3398
|
+
"access-control-allow-origin",
|
|
3399
|
+
"access-control-expose-headers",
|
|
3400
|
+
"access-control-max-age"
|
|
3401
|
+
];
|
|
3402
|
+
/**
|
|
3403
|
+
* https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
|
|
3404
|
+
*/
|
|
3405
|
+
const HEADER_NAME_REGEX = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
3406
|
+
/**
|
|
3407
|
+
* Configuration of the managed policy
|
|
3408
|
+
* `CORS-with-preflight-and-SecurityHeadersPolicy`
|
|
3409
|
+
* (`eaab4381-ed33-4a86-88ca-d9558dc6cd63`), which every static app deploy has
|
|
3410
|
+
* used since CloudFront support was added.
|
|
3411
|
+
*
|
|
3412
|
+
* A custom policy replaces the managed one instead of extending it, so the
|
|
3413
|
+
* managed settings are reproduced here to keep deploys that define response
|
|
3414
|
+
* headers equivalent to the ones that don't.
|
|
3415
|
+
*
|
|
3416
|
+
* Transcribed 2026-08-22 from
|
|
3417
|
+
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-response-headers-policies.html
|
|
3418
|
+
*
|
|
3419
|
+
* `AccessControlAllowHeaders` is required by CloudFormation but isn't listed
|
|
3420
|
+
* on that page. It mirrors the `AllowedHeaders` of the bucket CORS rules.
|
|
3421
|
+
*
|
|
3422
|
+
* Note the `Override` flags are not uniform: `X-Content-Type-Options` is the
|
|
3423
|
+
* only header the managed policy overrides the origin for.
|
|
3424
|
+
*/
|
|
3425
|
+
const BASELINE_RESPONSE_HEADERS_CONFIG = {
|
|
3426
|
+
CorsConfig: {
|
|
3427
|
+
AccessControlAllowCredentials: false,
|
|
3428
|
+
AccessControlAllowHeaders: { Items: ["*"] },
|
|
3429
|
+
AccessControlAllowMethods: { Items: [
|
|
3430
|
+
"DELETE",
|
|
3431
|
+
"GET",
|
|
3432
|
+
"HEAD",
|
|
3433
|
+
"OPTIONS",
|
|
3434
|
+
"PATCH",
|
|
3435
|
+
"POST",
|
|
3436
|
+
"PUT"
|
|
3437
|
+
] },
|
|
3438
|
+
AccessControlAllowOrigins: { Items: ["*"] },
|
|
3439
|
+
AccessControlExposeHeaders: { Items: ["*"] },
|
|
3440
|
+
OriginOverride: false
|
|
3441
|
+
},
|
|
3442
|
+
SecurityHeadersConfig: {
|
|
3443
|
+
ReferrerPolicy: {
|
|
3444
|
+
Override: false,
|
|
3445
|
+
ReferrerPolicy: "strict-origin-when-cross-origin"
|
|
3446
|
+
},
|
|
3447
|
+
StrictTransportSecurity: {
|
|
3448
|
+
AccessControlMaxAgeSec: 31536e3,
|
|
3449
|
+
IncludeSubdomains: false,
|
|
3450
|
+
Override: false,
|
|
3451
|
+
Preload: false
|
|
3452
|
+
},
|
|
3453
|
+
ContentTypeOptions: { Override: true },
|
|
3454
|
+
FrameOptions: {
|
|
3455
|
+
FrameOption: "SAMEORIGIN",
|
|
3456
|
+
Override: false
|
|
3457
|
+
},
|
|
3458
|
+
XSSProtection: {
|
|
3459
|
+
ModeBlock: true,
|
|
3460
|
+
Override: false,
|
|
3461
|
+
Protection: true
|
|
3462
|
+
}
|
|
3463
|
+
}
|
|
3464
|
+
};
|
|
3465
|
+
const validateResponseHeaders = (responseHeaders) => {
|
|
3466
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3467
|
+
for (const { header } of responseHeaders) {
|
|
3468
|
+
if (!header || !HEADER_NAME_REGEX.test(header)) throw new Error(`"${header}" is not a valid HTTP header name for the response-headers option.`);
|
|
3469
|
+
const headerLowerCase = header.toLowerCase();
|
|
3470
|
+
if (seen.has(headerLowerCase)) throw new Error(`The response header "${header}" was defined more than once. Header names are case-insensitive.`);
|
|
3471
|
+
seen.add(headerLowerCase);
|
|
3472
|
+
if (CORS_CONFIG_HEADERS.includes(headerLowerCase)) throw new Error(`The response header "${header}" is part of the CORS configuration and cannot be defined by the response-headers option. Use the response-headers-policy option to take full control of the policy.`);
|
|
3473
|
+
}
|
|
3474
|
+
};
|
|
3475
|
+
/**
|
|
3476
|
+
* Normalizes the `response-headers` option into a list of headers.
|
|
3477
|
+
*
|
|
3478
|
+
* Accepts the object form, which is the one to prefer:
|
|
3479
|
+
*
|
|
3480
|
+
* ```ts
|
|
3481
|
+
* {
|
|
3482
|
+
* 'content-security-policy': "default-src 'self'",
|
|
3483
|
+
* }
|
|
3484
|
+
* ```
|
|
3485
|
+
*
|
|
3486
|
+
* And the array form, needed only to set `override` per header:
|
|
3487
|
+
*
|
|
3488
|
+
* ```ts
|
|
3489
|
+
* [
|
|
3490
|
+
* { header: 'content-security-policy', value: "default-src 'self'", override: false },
|
|
3491
|
+
* ]
|
|
3492
|
+
* ```
|
|
3493
|
+
*/
|
|
3494
|
+
const parseResponseHeaders = (arg) => {
|
|
3495
|
+
if (!arg) return [];
|
|
3496
|
+
const responseHeaders = Array.isArray(arg) ? arg.map(({ header, value, override = true }) => {
|
|
3497
|
+
return {
|
|
3498
|
+
header,
|
|
3499
|
+
value,
|
|
3500
|
+
override
|
|
3501
|
+
};
|
|
3502
|
+
}) : Object.entries(arg).map(([header, value]) => {
|
|
3503
|
+
return {
|
|
3504
|
+
header,
|
|
3505
|
+
value: String(value),
|
|
3506
|
+
override: true
|
|
3507
|
+
};
|
|
3508
|
+
});
|
|
3509
|
+
validateResponseHeaders(responseHeaders);
|
|
3510
|
+
return responseHeaders;
|
|
3511
|
+
};
|
|
3512
|
+
/**
|
|
3513
|
+
* Builds the `ResponseHeadersPolicyConfig` of the policy created when the
|
|
3514
|
+
* response-headers option is defined: the baseline managed policy settings
|
|
3515
|
+
* with the user-defined headers layered on top.
|
|
3516
|
+
*/
|
|
3517
|
+
const getResponseHeadersPolicyConfig = ({ responseHeaders }) => {
|
|
3518
|
+
const securityHeadersConfig = { ...BASELINE_RESPONSE_HEADERS_CONFIG.SecurityHeadersConfig };
|
|
3519
|
+
/**
|
|
3520
|
+
* A user-defined header replaces the baseline one instead of being emitted
|
|
3521
|
+
* alongside it.
|
|
3522
|
+
*/
|
|
3523
|
+
for (const { header } of responseHeaders) {
|
|
3524
|
+
const securityHeadersConfigKey = SECURITY_HEADERS_CONFIG_KEYS[header.toLowerCase()];
|
|
3525
|
+
if (securityHeadersConfigKey) delete securityHeadersConfig[securityHeadersConfigKey];
|
|
3526
|
+
}
|
|
3527
|
+
return {
|
|
3528
|
+
Comment: { "Fn::Sub": ["Response headers policy for ${Project} project.", { Project: { Ref: "Project" } }] },
|
|
3529
|
+
CorsConfig: BASELINE_RESPONSE_HEADERS_CONFIG.CorsConfig,
|
|
3530
|
+
CustomHeadersConfig: { Items: responseHeaders.map(({ header, value, override }) => {
|
|
3531
|
+
return {
|
|
3532
|
+
Header: header,
|
|
3533
|
+
Override: override,
|
|
3534
|
+
Value: value
|
|
3535
|
+
};
|
|
3536
|
+
}) },
|
|
3537
|
+
/**
|
|
3538
|
+
* Response headers policy names must be unique per AWS account. Static app
|
|
3539
|
+
* deploys are always in us-east-1, so the stack name cannot collide with a
|
|
3540
|
+
* same-named stack in another region.
|
|
3541
|
+
*/
|
|
3542
|
+
Name: { Ref: "AWS::StackName" },
|
|
3543
|
+
...Object.keys(securityHeadersConfig).length > 0 ? { SecurityHeadersConfig: securityHeadersConfig } : {}
|
|
3544
|
+
};
|
|
3545
|
+
};
|
|
3546
|
+
//#endregion
|
|
3373
3547
|
//#region src/deploy/staticApp/staticApp.template.ts
|
|
3374
3548
|
const PACKAGE_VERSION = getPackageVersion();
|
|
3375
3549
|
const STATIC_APP_BUCKET_LOGICAL_ID = "StaticBucket";
|
|
3376
3550
|
const CLOUDFRONT_DISTRIBUTION_LOGICAL_ID = "CloudFrontDistribution";
|
|
3377
3551
|
const CLOUDFRONT_ORIGIN_ACCESS_CONTROL_LOGICAL_ID = "OriginAccessControl";
|
|
3552
|
+
const CLOUDFRONT_RESPONSE_HEADERS_POLICY_LOGICAL_ID = "ResponseHeadersPolicy";
|
|
3378
3553
|
const ROUTE_53_RECORD_SET_GROUP_LOGICAL_ID = "Route53RecordSetGroup";
|
|
3379
3554
|
const ERROR_DOCUMENT = "404/index.html";
|
|
3380
3555
|
/**
|
|
@@ -3393,8 +3568,31 @@ const ORIGIN_REQUEST_POLICY_ID = "88a5eaf4-2fd4-4709-b370-b4c650ea3fcf";
|
|
|
3393
3568
|
* Name: CORS-with-preflight-and-SecurityHeadersPolicy
|
|
3394
3569
|
* ID: eaab4381-ed33-4a86-88ca-d9558dc6cd63
|
|
3395
3570
|
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-response-headers-policies.html
|
|
3571
|
+
*
|
|
3572
|
+
* Used when neither the `responseHeaders` nor the `responseHeadersPolicy`
|
|
3573
|
+
* option is defined. Its settings are reproduced in
|
|
3574
|
+
* `BASELINE_RESPONSE_HEADERS_CONFIG` for the policy created by the
|
|
3575
|
+
* `responseHeaders` option.
|
|
3396
3576
|
*/
|
|
3397
3577
|
const ORIGIN_RESPONSE_POLICY_ID = "eaab4381-ed33-4a86-88ca-d9558dc6cd63";
|
|
3578
|
+
/**
|
|
3579
|
+
* https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-responseheaderspolicy.html
|
|
3580
|
+
*/
|
|
3581
|
+
const RESPONSE_HEADERS_POLICY_ID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
3582
|
+
/**
|
|
3583
|
+
* The distribution takes a single response headers policy id, so the three
|
|
3584
|
+
* sources are mutually exclusive:
|
|
3585
|
+
*
|
|
3586
|
+
* 1. `responseHeadersPolicy`: an existing policy id, or the name of an
|
|
3587
|
+
* exported value whose value is a policy id.
|
|
3588
|
+
* 1. `responseHeaders`: the policy created by this template.
|
|
3589
|
+
* 1. Neither: the managed policy used by every deploy so far.
|
|
3590
|
+
*/
|
|
3591
|
+
const getResponseHeadersPolicyId = ({ responseHeaders = [], responseHeadersPolicy }) => {
|
|
3592
|
+
if (responseHeadersPolicy) return RESPONSE_HEADERS_POLICY_ID_REGEX.test(responseHeadersPolicy) ? responseHeadersPolicy : { "Fn::ImportValue": responseHeadersPolicy };
|
|
3593
|
+
if (responseHeaders.length > 0) return { "Fn::GetAtt": [CLOUDFRONT_RESPONSE_HEADERS_POLICY_LOGICAL_ID, "Id"] };
|
|
3594
|
+
return ORIGIN_RESPONSE_POLICY_ID;
|
|
3595
|
+
};
|
|
3398
3596
|
const getBucketStaticWebsiteTemplate = ({ spa }) => {
|
|
3399
3597
|
return {
|
|
3400
3598
|
AWSTemplateFormatVersion: "2010-09-09",
|
|
@@ -3439,7 +3637,7 @@ const getBucketStaticWebsiteTemplate = ({ spa }) => {
|
|
|
3439
3637
|
} }
|
|
3440
3638
|
};
|
|
3441
3639
|
};
|
|
3442
|
-
const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, spa, hostedZoneName }) => {
|
|
3640
|
+
const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, responseHeaders = [], responseHeadersPolicy, spa, hostedZoneName }) => {
|
|
3443
3641
|
const template = {
|
|
3444
3642
|
AWSTemplateFormatVersion: "2010-09-09",
|
|
3445
3643
|
Resources: {
|
|
@@ -3517,7 +3715,10 @@ const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, spa, hosted
|
|
|
3517
3715
|
* https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-defaultcachebehavior.html#cfn-cloudfront-distribution-defaultcachebehavior-cachepolicyid
|
|
3518
3716
|
*/
|
|
3519
3717
|
CachePolicyId: CACHE_POLICY_ID,
|
|
3520
|
-
ResponseHeadersPolicyId:
|
|
3718
|
+
ResponseHeadersPolicyId: getResponseHeadersPolicyId({
|
|
3719
|
+
responseHeaders,
|
|
3720
|
+
responseHeadersPolicy
|
|
3721
|
+
}),
|
|
3521
3722
|
TargetOriginId: { Ref: STATIC_APP_BUCKET_LOGICAL_ID },
|
|
3522
3723
|
ViewerProtocolPolicy: "redirect-to-https"
|
|
3523
3724
|
},
|
|
@@ -3544,7 +3745,11 @@ const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, spa, hosted
|
|
|
3544
3745
|
SigningBehavior: "always",
|
|
3545
3746
|
SigningProtocol: "sigv4"
|
|
3546
3747
|
} }
|
|
3547
|
-
}
|
|
3748
|
+
},
|
|
3749
|
+
...responseHeaders.length > 0 ? { [CLOUDFRONT_RESPONSE_HEADERS_POLICY_LOGICAL_ID]: {
|
|
3750
|
+
Type: "AWS::CloudFront::ResponseHeadersPolicy",
|
|
3751
|
+
Properties: { ResponseHeadersPolicyConfig: getResponseHeadersPolicyConfig({ responseHeaders }) }
|
|
3752
|
+
} } : {}
|
|
3548
3753
|
};
|
|
3549
3754
|
if (acm) {
|
|
3550
3755
|
const acmCertificateArn = /^arn:aws:acm:[-a-z0-9]+:\d{12}:certificate\/[-a-z0-9]+$/.test(acm) ? acm : { "Fn::ImportValue": acm };
|
|
@@ -3637,12 +3842,14 @@ const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, spa, hosted
|
|
|
3637
3842
|
};
|
|
3638
3843
|
return template;
|
|
3639
3844
|
};
|
|
3640
|
-
const getStaticAppTemplate = ({ acm, aliases, appendIndexHtml, cloudfront, spa, hostedZoneName, region }) => {
|
|
3845
|
+
const getStaticAppTemplate = ({ acm, aliases, appendIndexHtml, cloudfront, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region }) => {
|
|
3641
3846
|
if (cloudfront) return getCloudFrontTemplate({
|
|
3642
3847
|
acm,
|
|
3643
3848
|
aliases,
|
|
3644
3849
|
appendIndexHtml,
|
|
3645
3850
|
cloudfront,
|
|
3851
|
+
responseHeaders,
|
|
3852
|
+
responseHeadersPolicy,
|
|
3646
3853
|
spa,
|
|
3647
3854
|
hostedZoneName,
|
|
3648
3855
|
region
|
|
@@ -3650,8 +3857,30 @@ const getStaticAppTemplate = ({ acm, aliases, appendIndexHtml, cloudfront, spa,
|
|
|
3650
3857
|
return getBucketStaticWebsiteTemplate({ spa });
|
|
3651
3858
|
};
|
|
3652
3859
|
//#endregion
|
|
3860
|
+
//#region src/deploy/staticApp/findDefaultBuildFolder.ts
|
|
3861
|
+
/**
|
|
3862
|
+
* Fixes #20 https://github.com/ttoss/carlin/issues/20
|
|
3863
|
+
*/
|
|
3864
|
+
const defaultBuildFolders = [
|
|
3865
|
+
"build",
|
|
3866
|
+
"out",
|
|
3867
|
+
"storybook-static",
|
|
3868
|
+
"dist"
|
|
3869
|
+
];
|
|
3870
|
+
const findDefaultBuildFolder = async () => {
|
|
3871
|
+
return (await Promise.all(defaultBuildFolders.map(async (directory) => {
|
|
3872
|
+
return {
|
|
3873
|
+
directory,
|
|
3874
|
+
isValid: (await getAllFilesInsideADirectory({ directory })).length !== 0
|
|
3875
|
+
};
|
|
3876
|
+
}))).reduce((acc, cur) => {
|
|
3877
|
+
if (cur.isValid) return cur.directory;
|
|
3878
|
+
return acc;
|
|
3879
|
+
}, "");
|
|
3880
|
+
};
|
|
3881
|
+
//#endregion
|
|
3653
3882
|
//#region src/deploy/staticApp/uploadBuiltAppToS3.ts
|
|
3654
|
-
const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket }) => {
|
|
3883
|
+
const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket, uploadSourceMaps }) => {
|
|
3655
3884
|
/**
|
|
3656
3885
|
* Only empty directory if the number of the files inside $directory.
|
|
3657
3886
|
* If the number of files is zero, uploadDirectoryToS3 will thrown.
|
|
@@ -3663,7 +3892,8 @@ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket }) => {
|
|
|
3663
3892
|
});
|
|
3664
3893
|
await uploadDirectoryToS3({
|
|
3665
3894
|
bucket,
|
|
3666
|
-
directory
|
|
3895
|
+
directory,
|
|
3896
|
+
uploadSourceMaps
|
|
3667
3897
|
});
|
|
3668
3898
|
return;
|
|
3669
3899
|
}
|
|
@@ -3675,7 +3905,8 @@ const uploadBuiltAppToS3 = async ({ buildFolder: directory, bucket }) => {
|
|
|
3675
3905
|
});
|
|
3676
3906
|
await uploadDirectoryToS3({
|
|
3677
3907
|
bucket,
|
|
3678
|
-
directory: defaultDirectory
|
|
3908
|
+
directory: defaultDirectory,
|
|
3909
|
+
uploadSourceMaps
|
|
3679
3910
|
});
|
|
3680
3911
|
await copyRoot404To404Index({ bucket });
|
|
3681
3912
|
return;
|
|
@@ -3693,7 +3924,7 @@ const logPrefix$4 = "static-app";
|
|
|
3693
3924
|
* 1. Upload static files to the host bucket S3.
|
|
3694
3925
|
* 1. Remove old deployment versions. Keep only the 3 most recent ones.
|
|
3695
3926
|
*/
|
|
3696
|
-
const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, cloudfront, spa, hostedZoneName, region, skipUpload }) => {
|
|
3927
|
+
const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, cloudfront, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region, skipUpload, uploadSourceMaps }) => {
|
|
3697
3928
|
try {
|
|
3698
3929
|
const { stackName } = await handleDeployInitialization({ logPrefix: logPrefix$4 });
|
|
3699
3930
|
const params = { StackName: stackName };
|
|
@@ -3702,6 +3933,8 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
|
|
|
3702
3933
|
aliases,
|
|
3703
3934
|
appendIndexHtml,
|
|
3704
3935
|
cloudfront,
|
|
3936
|
+
responseHeaders,
|
|
3937
|
+
responseHeadersPolicy,
|
|
3705
3938
|
spa,
|
|
3706
3939
|
hostedZoneName,
|
|
3707
3940
|
region
|
|
@@ -3715,7 +3948,8 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
|
|
|
3715
3948
|
if (!skipUpload) await uploadBuiltAppToS3({
|
|
3716
3949
|
buildFolder,
|
|
3717
3950
|
bucket,
|
|
3718
|
-
cloudfront
|
|
3951
|
+
cloudfront,
|
|
3952
|
+
uploadSourceMaps
|
|
3719
3953
|
});
|
|
3720
3954
|
const { Outputs } = await deploy({
|
|
3721
3955
|
params,
|
|
@@ -3736,7 +3970,8 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
|
|
|
3736
3970
|
await uploadBuiltAppToS3({
|
|
3737
3971
|
buildFolder,
|
|
3738
3972
|
bucket: newBucket,
|
|
3739
|
-
cloudfront
|
|
3973
|
+
cloudfront,
|
|
3974
|
+
uploadSourceMaps
|
|
3740
3975
|
});
|
|
3741
3976
|
}
|
|
3742
3977
|
} catch (error) {
|
|
@@ -3790,6 +4025,15 @@ const options$4 = {
|
|
|
3790
4025
|
hidden: true,
|
|
3791
4026
|
type: "string"
|
|
3792
4027
|
},
|
|
4028
|
+
"response-headers": {
|
|
4029
|
+
coerce: parseResponseHeaders,
|
|
4030
|
+
default: [],
|
|
4031
|
+
describe: "Headers added by CloudFront to every response sent to viewers. Pass an object whose keys are header names and values are header values."
|
|
4032
|
+
},
|
|
4033
|
+
"response-headers-policy": {
|
|
4034
|
+
describe: "The id of an existing CloudFront response headers policy, or the name of the exported variable whose value is the id, that will be associated to the distribution instead of the default one.",
|
|
4035
|
+
type: "string"
|
|
4036
|
+
},
|
|
3793
4037
|
"skip-upload": {
|
|
3794
4038
|
default: false,
|
|
3795
4039
|
describe: "Skip files upload to S3. Useful when wanting update only CloudFormation.",
|
|
@@ -3800,13 +4044,34 @@ const options$4 = {
|
|
|
3800
4044
|
describe: "This option enables CloudFront to serve a single page application (SPA).",
|
|
3801
4045
|
require: false,
|
|
3802
4046
|
type: "boolean"
|
|
4047
|
+
},
|
|
4048
|
+
/**
|
|
4049
|
+
* Source maps contain the application's original source, and the bucket this
|
|
4050
|
+
* command uploads to is public, so publishing them is a source disclosure.
|
|
4051
|
+
* The default is therefore to exclude them, and including them must be an
|
|
4052
|
+
* explicit, deliberate choice.
|
|
4053
|
+
*/
|
|
4054
|
+
"upload-source-maps": {
|
|
4055
|
+
default: false,
|
|
4056
|
+
describe: "Upload source map (`.map`) files to S3. They are excluded by default because the bucket is public and source maps expose the application source.",
|
|
4057
|
+
require: false,
|
|
4058
|
+
type: "boolean"
|
|
3803
4059
|
}
|
|
3804
4060
|
};
|
|
3805
4061
|
const deployStaticAppCommand = {
|
|
3806
4062
|
command: "static-app",
|
|
3807
4063
|
describe: "Deploy static app.",
|
|
3808
4064
|
builder: (yargs) => {
|
|
3809
|
-
return yargs.options(addGroupToOptions(options$4, "Deploy Static App Options")).
|
|
4065
|
+
return yargs.options(addGroupToOptions(options$4, "Deploy Static App Options")).check(({ cloudfront, responseHeaders, responseHeadersPolicy }) => {
|
|
4066
|
+
const hasResponseHeaders = responseHeaders?.length > 0;
|
|
4067
|
+
if (hasResponseHeaders && responseHeadersPolicy) throw new Error("The response-headers and response-headers-policy options are mutually exclusive. The distribution takes a single response headers policy.");
|
|
4068
|
+
/**
|
|
4069
|
+
* Response headers are applied by CloudFront, so a bucket only
|
|
4070
|
+
* deploy has nothing to attach them to.
|
|
4071
|
+
*/
|
|
4072
|
+
if (!cloudfront && (hasResponseHeaders || responseHeadersPolicy)) throw new Error(`The ${hasResponseHeaders ? "response-headers" : "response-headers-policy"} option requires the cloudfront option.`);
|
|
4073
|
+
return true;
|
|
4074
|
+
}).middleware(() => {
|
|
3810
4075
|
AWS.config.region = CLOUDFRONT_REGION;
|
|
3811
4076
|
});
|
|
3812
4077
|
},
|