UNPKG

@pellegrims/nx-remotecache-s3

Version:
51 lines 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupS3TaskRunner = void 0; const lib_storage_1 = require("@aws-sdk/lib-storage"); const nx_remotecache_custom_1 = require("nx-remotecache-custom"); const s3_client_1 = require("./s3-client"); const util_1 = require("./util"); const ENV_BUCKET = 'NXCACHE_S3_BUCKET'; const ENV_PREFIX = 'NXCACHE_S3_PREFIX'; const setupS3TaskRunner = async (options) => { var _a, _b, _c; (0, nx_remotecache_custom_1.initEnv)(options); const s3Storage = (0, s3_client_1.buildS3Client)(options); const bucket = (_a = (0, util_1.getEnv)(ENV_BUCKET)) !== null && _a !== void 0 ? _a : options.bucket; const prefix = (_c = (_b = (0, util_1.getEnv)(ENV_PREFIX)) !== null && _b !== void 0 ? _b : options.prefix) !== null && _c !== void 0 ? _c : ''; return { name: 'S3', fileExists: async (filename) => { try { const result = await s3Storage.headObject((0, util_1.buildCommonCommandInput)(bucket, prefix, filename)); return !!result; } catch (error) { if (error.name === '403' || error.name === 'NotFound') { return false; } else { throw error; } } }, retrieveFile: async (filename) => { const result = await s3Storage.getObject((0, util_1.buildCommonCommandInput)(bucket, prefix, filename)); return result.Body; }, storeFile: (filename, stream) => { const upload = new lib_storage_1.Upload({ client: s3Storage, params: { ...(0, util_1.buildCommonCommandInput)(bucket, prefix, filename), // eslint-disable-next-line @typescript-eslint/naming-convention Body: stream, }, }); return upload.done(); }, }; }; exports.setupS3TaskRunner = setupS3TaskRunner; //# sourceMappingURL=setup-s3-task-runner.js.map