UNPKG

@n1k1t/mock-server

Version:

The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations

34 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const node_gzip_1 = require("node-gzip"); const models_1 = require("../models"); const utils_1 = require("../../utils"); const logger_1 = require("../../logger"); const logger = logger_1.Logger.build('Endpoints.CacheRestore'); exports.default = models_1.EndpointFactory .build() .http({ method: 'POST', path: '/cache/restore' }) .io({ path: 'cache:restore' }) .compile(async ({ incoming, reply, server }) => { const unziped = await (0, node_gzip_1.ungzip)(Buffer.from(incoming.data.backup, 'base64')).catch((error) => { logger.error('Got error while cache unzip', error?.stack ?? error); return null; }); if (!unziped) { return reply.internalError('Cannot unzip backup payload'); } const ttl = incoming.data.ttl ?? 60 * 60; const backup = (0, utils_1.parseJsonSafe)(unziped.toString('utf8')); if (backup.status === 'ERROR') { logger.error('Got error while parsing cache', backup.error?.stack ?? backup.error); return reply.internalError('Cannot parse cache'); } if (server.databases.redis) { await Promise.all(backup.result.redis.map(([key, value]) => server.databases.redis.setex(key, ttl, value).catch((error) => { logger.error(`Got error while inserting key [${key}]`, error?.stack ?? error); return null; }))); } reply.ok(null); }); //# sourceMappingURL=cache.restore.endpoint.js.map