@pellegrims/nx-remotecache-s3
Version:
Remote caching for @nrwl/nx using S3 storage
50 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupS3TaskRunner = void 0;
const tslib_1 = require("tslib");
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 = (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
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: (filename) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
try {
const result = yield 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: (filename) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const result = yield 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: Object.assign(Object.assign({}, (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