UNPKG

@pellegrims/nx-remotecache-s3

Version:
63 lines 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildS3Client = exports.getProxyConfig = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); const util_1 = require("./util"); const credential_provider_node_1 = require("@aws-sdk/credential-provider-node"); const client_sts_1 = require("@aws-sdk/client-sts"); const node_http_handler_1 = require("@smithy/node-http-handler"); const hpagent_1 = require("hpagent"); const ENV_ENDPOINT = 'NXCACHE_S3_ENDPOINT'; const ENV_PROFILE = 'NXCACHE_S3_PROFILE'; const ENV_FORCE_PATH_STYLE = 'NXCACHE_S3_FORCE_PATH_STYLE'; const ENV_REGION = 'NXCACHE_S3_REGION'; const ENV_ACCESS_KEY_ID = 'NXCACHE_S3_ACCESS_KEY_ID'; const ENV_SECRET_ACCESS_KEY = 'NXCACHE_S3_SECRET_ACCESS_KEY'; const DEFAULT_S3_ENDPOINT = 'https://s3.amazonaws.com'; function getHttpAgent() { return new hpagent_1.HttpsProxyAgent({ proxy: (0, util_1.getHttpProxy)() }); } function getHttpsAgent() { return new hpagent_1.HttpsProxyAgent({ proxy: (0, util_1.getHttpsProxy)() }); } const getProxyConfig = (s3Endpoint = DEFAULT_S3_ENDPOINT) => ({ ...(!(0, util_1.matchesNoProxy)(s3Endpoint, (0, util_1.getNoProxy)()) && ((0, util_1.getHttpProxy)() || (0, util_1.getHttpsProxy)()) && { requestHandler: new node_http_handler_1.NodeHttpHandler({ ...((0, util_1.getHttpProxy)() && { httpAgent: getHttpAgent() }), ...((0, util_1.getHttpsProxy)() && { httpsAgent: getHttpsAgent() }), }), }), }); exports.getProxyConfig = getProxyConfig; const buildS3Client = (options) => { var _a, _b, _c, _d; const provider = getCredentialsProvider(options); return new client_s3_1.S3({ endpoint: (_a = (0, util_1.getEnv)(ENV_ENDPOINT)) !== null && _a !== void 0 ? _a : options.endpoint, region: (_b = (0, util_1.getEnv)(ENV_REGION)) !== null && _b !== void 0 ? _b : options.region, credentials: provider, forcePathStyle: (0, util_1.getEnv)(ENV_FORCE_PATH_STYLE) === 'true' || options.forcePathStyle, ...(0, exports.getProxyConfig)((_d = (_c = (0, util_1.getEnv)(ENV_ENDPOINT)) !== null && _c !== void 0 ? _c : options.endpoint) !== null && _d !== void 0 ? _d : DEFAULT_S3_ENDPOINT), }); }; exports.buildS3Client = buildS3Client; const getCredentialsProvider = (options) => { var _a; const awsAccessKeyIdOverride = (0, util_1.getEnv)(ENV_ACCESS_KEY_ID); const awsSecretAccessKeyOverride = (0, util_1.getEnv)(ENV_SECRET_ACCESS_KEY); if ((awsAccessKeyIdOverride === null || awsAccessKeyIdOverride === void 0 ? void 0 : awsAccessKeyIdOverride.length) && (awsSecretAccessKeyOverride === null || awsSecretAccessKeyOverride === void 0 ? void 0 : awsSecretAccessKeyOverride.length)) { return { accessKeyId: awsAccessKeyIdOverride, secretAccessKey: awsSecretAccessKeyOverride, }; } else { return (0, credential_provider_node_1.defaultProvider)({ profile: (_a = (0, util_1.getEnv)(ENV_PROFILE)) !== null && _a !== void 0 ? _a : options.profile, roleAssumerWithWebIdentity: (0, client_sts_1.getDefaultRoleAssumerWithWebIdentity)(), roleAssumer: (0, client_sts_1.getDefaultRoleAssumer)(), }); } }; //# sourceMappingURL=s3-client.js.map