UNPKG

@goldstack/template-s3

Version:

Building blocks for linking a package to an AWS S3 bucket.

116 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isMocked = exports.getMockedS3 = void 0; exports.getLocalBucketName = getLocalBucketName; exports.getLocalBucketUrl = getLocalBucketUrl; exports.connect = connect; exports.resetMockS3 = resetMockS3; exports.resetMocksIfRequired = resetMocksIfRequired; const client_s3_1 = require("@aws-sdk/client-s3"); const infra_aws_1 = require("@goldstack/infra-aws"); const utils_esbuild_1 = require("@goldstack/utils-esbuild"); const utils_log_1 = require("@goldstack/utils-log"); const utils_package_config_embedded_1 = require("@goldstack/utils-package-config-embedded"); let s3MockUsed = false; /** * Gets the package configuration and deployment for S3 operations */ const getPackageConfigAndDeployment = (goldstackConfig, packageSchema, deploymentName) => { const packageConfig = new utils_package_config_embedded_1.EmbeddedPackageConfig({ goldstackJson: goldstackConfig, packageSchema, }); deploymentName = deploymentName || process.env.GOLDSTACK_DEPLOYMENT; if (!deploymentName) { throw new Error('GOLDSTACK_DEPLOYMENT environment variable is not set.'); } if (deploymentName === 'local') { return { packageConfig, deployment: { name: 'local', awsRegion: 'us-east-1', awsUser: process.env.AWS_USER || 'local', configuration: { bucketName: goldstackConfig.name, }, }, }; } const deployment = packageConfig.getDeployment(deploymentName); if (!deployment) { throw new Error(`Cannot find deployment ${deploymentName}.`); } return { packageConfig, deployment }; }; /** * Gets a mocked S3 client for local development */ const getMockedS3 = (goldstackConfig, bucket) => { const bucketName = bucket || getLocalBucketName(goldstackConfig); (0, utils_log_1.debug)(`Creating mock S3 client for bucket: ${bucketName}`); const createS3Client = require((0, utils_esbuild_1.excludeInBundle)('mock-aws-s3-v3')).createS3Client; const client = createS3Client({ localDirectory: 'goldstackLocal/s3', bucket: bucketName, }); client._goldstackIsMocked = true; s3MockUsed = true; (0, utils_log_1.debug)(`Mock S3 client created for bucket: ${bucketName}`); return client; }; exports.getMockedS3 = getMockedS3; /** * Gets the local bucket name for a given configuration */ function getLocalBucketName(goldstackConfig) { return `local-${goldstackConfig.name}`; } /** * Gets the local bucket URL for a given configuration */ function getLocalBucketUrl(goldstackConfig) { return `http://localhost:4566/${getLocalBucketName(goldstackConfig)}`; } /** * Connects to S3, using a mocked client for local deployment or a real S3 client for other environments */ async function connect(goldstackConfig, packageSchema, bucket) { const { deployment } = getPackageConfigAndDeployment(goldstackConfig, packageSchema); if (deployment.name === 'local') { (0, utils_log_1.warn)(`Initializing mocked S3 for ${goldstackConfig.name}. Consider using getMockedS3() directly if you need bucket-specific configuration.`); return (0, exports.getMockedS3)(goldstackConfig, bucket); } const awsUser = process.env.AWS_ACCESS_KEY_ID ? undefined : await (0, infra_aws_1.getAWSUser)(deployment.awsUser); return new client_s3_1.S3Client({ credentials: awsUser, region: deployment.awsRegion, }); } const isMocked = (client) => { return client._goldstackIsMocked === true; }; exports.isMocked = isMocked; /** * Resets all mocked S3 state. Should be called in afterAll to prevent * state leakage between test suites and to allow Jest to exit cleanly. */ function resetMockS3() { (0, utils_log_1.debug)('resetMockS3 called - resetting all mock S3 state'); const MockS3 = require((0, utils_esbuild_1.excludeInBundle)('mock-aws-s3-v3')); MockS3.resetMocks(); s3MockUsed = false; (0, utils_log_1.debug)('resetMockS3 complete'); } function resetMocksIfRequired(deploymentName, goldstackConfig) { if (s3MockUsed) { (0, utils_log_1.warn)('Initialising a real S3 bucket after a mocked one had been created. All mocks are reset.', { deploymentName, package: goldstackConfig.name, }); // only require this for local testing const MockS3 = require((0, utils_esbuild_1.excludeInBundle)('mock-aws-s3-v3')); MockS3.resetMocks(); } } //# sourceMappingURL=connectLocal.js.map