gitlab-acebase
Version:
AceBase realtime database server (webserver endpoint to allow remote connections)
55 lines • 2.56 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addRoute = void 0;
const error_1 = require("../shared/error");
const addRoute = (env) => {
env.router.post(`/import/${env.db.name}/*`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
// Import API
const path = req.path.slice(env.db.name.length + 9);
const access = yield env.rules.isOperationAllowed(req.user, path, 'import', { context: req.context });
if (!access.allow) {
return (0, error_1.sendUnauthorizedError)(res, access.code, access.message);
}
const format = req.query.format || 'json';
const suppress_events = req.query.suppress_events === '1';
req.pause(); // Switch to non-flowing mode so we can use .read() upon request
let eof = false;
req.once('end', () => { eof = true; });
const read = (length) => __awaiter(void 0, void 0, void 0, function* () {
let chunk = req.read();
if (chunk === null && !eof) {
yield new Promise(resolve => req.once('readable', resolve));
chunk = req.read();
}
// env.debug.verbose(`Received chunk: `, chunk);
return chunk;
});
const ref = env.db.ref(path);
try {
yield ref.import(read, { format, suppress_events });
res.send({ success: true });
}
catch (err) {
env.debug.error(`Error importing data for path "/${path}": `, err);
if (!res.headersSent) {
res.statusCode = 500;
res.send({ success: false, reason: err.message });
}
}
finally {
res.end();
}
}));
};
exports.addRoute = addRoute;
exports.default = exports.addRoute;
//# sourceMappingURL=data-import.js.map