UNPKG

@staticcms/proxy-server-lite

Version:
128 lines 6.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerMiddleware = exports.getSchema = exports.localFsMiddleware = void 0; const path_1 = __importDefault(require("path")); const joi_1 = require("../joi"); const customValidators_1 = require("../joi/customValidators"); const fs_1 = require("../utils/fs"); const entries_1 = require("../utils/entries"); function localFsMiddleware({ repoPath, logger }) { return async function (req, res) { try { const { body } = req; switch (body.action) { case 'info': { res.json({ repo: path_1.default.basename(repoPath), type: 'local_fs', }); break; } case 'entriesByFolder': { const payload = body.params; const { folder, extension, depth } = payload; const entries = await (0, fs_1.listRepoFiles)(repoPath, folder, extension, depth).then(items => (0, entries_1.entriesFromFiles)(repoPath, items.map(item => ({ path: item.file })))); res.json(entries); break; } case 'entriesByFiles': { const payload = body.params; const entries = await (0, entries_1.entriesFromFiles)(repoPath, payload.files); res.json(entries); break; } case 'getEntry': { const payload = body.params; const [entry] = await (0, entries_1.entriesFromFiles)(repoPath, [{ path: payload.path }]); res.json(entry); break; } case 'persistEntry': { const { entry, dataFiles = [entry], assets, } = body.params; await Promise.all(dataFiles.map(dataFile => (0, fs_1.writeFile)(path_1.default.join(repoPath, dataFile.path), dataFile.raw))); // save assets await Promise.all(assets.map(a => (0, fs_1.writeFile)(path_1.default.join(repoPath, a.path), Buffer.from(a.content, a.encoding)))); if (dataFiles.every(dataFile => dataFile.newPath)) { dataFiles.forEach(async (dataFile) => { await (0, fs_1.move)(path_1.default.join(repoPath, dataFile.path), path_1.default.join(repoPath, dataFile.newPath)); }); } res.json({ message: 'entry persisted' }); break; } case 'getMedia': { const { mediaFolder, publicFolder = mediaFolder } = body.params; const fsItems = await (0, fs_1.listRepoFiles)(repoPath, mediaFolder, '', 1); res.json(fsItems.map(item => ({ path: item.file.replace(/\\/g, '/'), url: item.file.replace(/\\/g, '/').replace(mediaFolder.replace(/^\//g, ''), publicFolder), isDirectory: item.isDirectory, }))); break; } case 'getMediaFile': { const { path } = body.params; const mediaFile = await (0, entries_1.readMediaFile)(repoPath, path); res.json(mediaFile); break; } case 'persistMedia': { const { asset } = body.params; await (0, fs_1.writeFile)(path_1.default.join(repoPath, asset.path), Buffer.from(asset.content, asset.encoding)); const file = await (0, entries_1.readMediaFile)(repoPath, asset.path); res.json(file); break; } case 'deleteFile': { const { path: filePath } = body.params; await (0, fs_1.deleteFile)(repoPath, filePath); res.json({ message: `deleted file ${filePath}` }); break; } case 'deleteFiles': { const { paths } = body.params; await Promise.all(paths.map(filePath => (0, fs_1.deleteFile)(repoPath, filePath))); res.json({ message: `deleted files ${paths.join(', ')}` }); break; } case 'getDeployPreview': { res.json(null); break; } default: { const message = `Unknown action ${body.action}`; res.status(422).json({ error: message }); break; } } } catch (e) { if (e instanceof Error) { logger.error(`Error handling ${JSON.stringify(req.body)}: ${e.message}`); if (e.message.startsWith('ENOENT: no such file or directory')) { res.status(404).json({ error: 'Not found' }); return; } } res.status(500).json({ error: 'Unknown error' }); } }; } exports.localFsMiddleware = localFsMiddleware; function getSchema({ repoPath }) { const schema = (0, joi_1.defaultSchema)({ path: (0, customValidators_1.pathTraversal)(repoPath) }); return schema; } exports.getSchema = getSchema; async function registerMiddleware(app, options) { const { logger } = options; const repoPath = path_1.default.resolve(process.env.GIT_REPO_DIRECTORY || process.cwd()); app.post('/api/v1', (0, joi_1.joi)(getSchema({ repoPath }))); app.post('/api/v1', localFsMiddleware({ repoPath, logger })); logger.info(`Static CMS File System Proxy Server configured with ${repoPath}`); } exports.registerMiddleware = registerMiddleware; //# sourceMappingURL=index.js.map