UNPKG

@staticcms/proxy-server-lite

Version:
48 lines 1.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readMediaFile = exports.entriesFromFiles = void 0; const crypto_1 = __importDefault(require("crypto")); const path_1 = __importDefault(require("path")); const fs_1 = require("fs"); function sha256(buffer) { return crypto_1.default.createHash('sha256').update(buffer).digest('hex'); } // normalize windows os path format function normalizePath(path) { return path.replace(/\\/g, '/'); } async function entriesFromFiles(repoPath, files) { return Promise.all(files.map(async (file) => { try { const content = await fs_1.promises.readFile(path_1.default.join(repoPath, file.path)); return { data: content.toString(), file: { path: normalizePath(file.path), label: file.label, id: sha256(content) }, }; } catch (e) { return { data: null, file: { path: normalizePath(file.path), label: file.label, id: null }, }; } })); } exports.entriesFromFiles = entriesFromFiles; async function readMediaFile(repoPath, file) { const encoding = 'base64'; const buffer = await fs_1.promises.readFile(path_1.default.join(repoPath, file)); const id = sha256(buffer); return { id, content: buffer.toString(encoding), encoding, path: normalizePath(file), name: path_1.default.basename(file), }; } exports.readMediaFile = readMediaFile; //# sourceMappingURL=entries.js.map