UNPKG

@cumulus/aws-client

Version:
140 lines 4.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.streamToString = exports.throttleOnce = exports.getLocalstackAwsClientOptions = exports.localStackAwsClientOptions = exports.getLocalstackEndpoint = exports.inTestMode = void 0; const errors_1 = require("@cumulus/errors"); const utils_1 = require("./utils"); const inTestMode = () => process.env.NODE_ENV === 'test'; exports.inTestMode = inTestMode; // From https://github.com/localstack/localstack/blob/master/README.md const localStackPorts = { APIGatewayClient: 4566, CloudFormation: 4566, CloudWatchEvents: 4566, DynamoDB: 4566, DynamoDBClient: 4566, DynamoDBStreamsClient: 4566, EC2: 4566, ECS: 4566, ElasticsearchService: 4566, firehose: 4566, iam: 4566, Kinesis: 4566, KMS: 4566, Lambda: 4566, redshift: 4566, route53: 4566, S3: 4566, SecretsManager: 4566, ses: 4566, SFN: 4566, SNS: 4566, SQS: 4566, ssm: 4566, STS: 4566, }; /** * Test if a given AWS service is supported by LocalStack. * * @param {Function} serviceIdentifier - an AWS service object constructor function * @returns {boolean} true or false depending on whether the service is * supported by LocalStack * * @private */ function localstackSupportedService(serviceIdentifier) { return Object.keys(localStackPorts).includes(serviceIdentifier); } /** * Returns the proper endpoint for a given aws service * * @param {string} identifier - service name * @returns {string} the localstack endpoint * * @private */ function getLocalstackEndpoint(identifier) { const key = `LOCAL_${identifier.toUpperCase()}_HOST`; if (process.env[key]) { return `http://${process.env[key]}:${localStackPorts[identifier]}`; } return `http://${process.env.LOCALSTACK_HOST}:${localStackPorts[identifier]}`; } exports.getLocalstackEndpoint = getLocalstackEndpoint; /** * Create an AWS service object that talks to LocalStack. * * This function expects that the LOCALSTACK_HOST environment variable will be set. * * @param {Function} Service - an AWS service object constructor function * @param {Object} options - options to pass to the service object constructor function * @returns {Object} an AWS service object * * @private */ function localStackAwsClientOptions(Service, options = {}) { if (!process.env.LOCALSTACK_HOST) { throw new Error('The LOCALSTACK_HOST environment variable is not set.'); } const serviceIdentifier = (0, utils_1.getServiceIdentifer)(Service); const localStackOptions = { region: 'us-east-1', endpoint: getLocalstackEndpoint(serviceIdentifier), ...options, credentials: { accessKeyId: 'my-access-key-id', secretAccessKey: 'my-secret-access-key', ...options.credentials, }, }; if (serviceIdentifier.toLowerCase() === 's3') localStackOptions.forcePathStyle = true; return localStackOptions; } exports.localStackAwsClientOptions = localStackAwsClientOptions; /** * Create an AWS service object that does not actually talk to AWS. * * @param {Function} Service - an AWS service object constructor function * @param {Object} options - options to pass to the service object constructor function * @returns {Object} an AWS service object * * @private */ function getLocalstackAwsClientOptions(Service, options = {}) { const serviceIdentifier = (0, utils_1.getServiceIdentifer)(Service); if (localstackSupportedService(serviceIdentifier)) { return localStackAwsClientOptions(Service, options); } return {}; } exports.getLocalstackAwsClientOptions = getLocalstackAwsClientOptions; /** * Return a function that throws a ThrottlingException the first time it is called, then returns as * normal any other times. * * @param {Function} fn * @returns {Function} * * @private */ const throttleOnce = (fn) => { let throttleNextCall = true; return (...args) => { if (throttleNextCall) { throttleNextCall = false; throw new errors_1.ThrottlingException(); } return fn(...args); }; }; exports.throttleOnce = throttleOnce; const streamToString = (stream) => { let result = ''; // eslint-disable-next-line no-return-assign stream.on('data', (chunk) => result += chunk.toString()); return new Promise((resolve) => { stream.on('end', () => resolve(result)); }); }; exports.streamToString = streamToString; //# sourceMappingURL=test-utils.js.map