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 logger_1 = require("../../logger"); const logger = logger_1.Logger.build('Endpoints.CacheRestore'); exports.default = models_1.EndpointFactory .build() .io({ path: 'cache:restore:stream' }) .compile(async ({ incoming, reply, server }) => { const chunks = []; if (!incoming.data?.stream) { return reply.validationError(['Wrong input data format']); } incoming.data.stream.on('data', (chunk) => chunks.push(chunk.toString())); await new Promise((resolve) => incoming.data.stream.once('finish', resolve)); const unziped = await (0, node_gzip_1.ungzip)(Buffer.from(chunks.join(''), '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.parameters.ttl ?? 60 * 60; const backup = JSON.parse(unziped.toString('utf8')); if (server.databases.redis) { await Promise.all(backup.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.stream.endpoint.js.map