UNPKG

@studion/infra-code-blocks

Version:
88 lines (87 loc) 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.S3CacheStrategy = void 0; const aws = require("@pulumi/aws"); const pulumi = require("@pulumi/pulumi"); class S3CacheStrategy extends pulumi.ComponentResource { name; pathPattern; config; cachePolicy; responseHeadersPolicy; constructor(name, args, opts = {}) { super('studion:cloudfront:S3CacheStrategy', name, {}, opts); this.name = name; const { pathPattern, bucket, cacheTtl } = args; this.pathPattern = pathPattern; this.cachePolicy = this.createCachePolicy(cacheTtl); this.responseHeadersPolicy = this.createResponseHeadersPolicy(); this.config = { targetOriginId: pulumi.output(bucket).apply(b => b.arn), viewerProtocolPolicy: 'redirect-to-https', allowedMethods: ['GET', 'HEAD'], cachedMethods: ['GET', 'HEAD'], compress: true, cachePolicyId: this.cachePolicy.id, responseHeadersPolicyId: this.responseHeadersPolicy.id, }; this.registerOutputs(); } createCachePolicy(ttl) { const enableEncoding = pulumi.output(ttl).apply(val => val !== 0); return new aws.cloudfront.CachePolicy(`${this.name}-cache-policy`, { defaultTtl: ttl ?? 86400, // default to 1 day minTtl: ttl ?? 60, // default to 1 minute maxTtl: ttl ?? 31536000, // default to 1 year parametersInCacheKeyAndForwardedToOrigin: { cookiesConfig: { cookieBehavior: 'none', }, headersConfig: { headerBehavior: 'none', }, queryStringsConfig: { queryStringBehavior: 'none', }, enableAcceptEncodingGzip: enableEncoding, enableAcceptEncodingBrotli: enableEncoding, }, }, { parent: this }); } createResponseHeadersPolicy() { return new aws.cloudfront.ResponseHeadersPolicy(`${this.name}-res-headers-policy`, { customHeadersConfig: { items: [ { header: 'Cache-Control', value: 'no-cache', override: false, }, ], }, securityHeadersConfig: { contentTypeOptions: { override: true, }, frameOptions: { frameOption: 'DENY', override: false, }, // instruct browsers to only use HTTPS strictTransportSecurity: { accessControlMaxAgeSec: 31536000, // 1 year includeSubdomains: true, preload: true, override: true, }, }, }, { parent: this }); } getPathConfig() { return { pathPattern: this.pathPattern, ...this.config, }; } } exports.S3CacheStrategy = S3CacheStrategy;