UNPKG

@uizard/nx-fast-s3-cache

Version:

Nx s3 cache using GNU tar, pigz and multipart s3 uploads/downloads

40 lines (39 loc) 1.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRemoteCacheRetrieve = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = require("path"); const get_file_name_from_hash_1 = require("./get-file-name-from-hash"); const COMMIT_FILE_EXTENSION = ".commit"; const COMMIT_FILE_CONTENT = "true"; const writeCommitFile = (destination) => { const commitFilePath = destination + COMMIT_FILE_EXTENSION; return fs_1.default.writeFileSync(commitFilePath, COMMIT_FILE_CONTENT); }; const createRemoteCacheRetrieve = (safeImplementation) => async (hash, cacheDirectory) => { const implementation = await safeImplementation; if (!implementation) { return false; } const { fileExists, retrieveFile, writeOnly } = implementation; if (writeOnly) { // cannot retrieve from write-only cache return false; } const file = (0, get_file_name_from_hash_1.getFileNameFromHash)(hash); const fileExistsRes = await fileExists(file); if (!(fileExistsRes === null || fileExistsRes === void 0 ? void 0 : fileExistsRes.result)) { return false; } const result = await retrieveFile(file, (0, path_1.join)(cacheDirectory, hash)); const destination = result === null || result === void 0 ? void 0 : result.path; if (!destination) { return false; } await writeCommitFile(destination); return true; }; exports.createRemoteCacheRetrieve = createRemoteCacheRetrieve;