UNPKG

@alinex/datastore

Version:

Read, work and write data structures from and to differents locations and formats.

37 lines 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@alinex/core"); const protocols = ['file', 'sftp', 'ftp', 'ftps', 'http', 'https', 'postgres', 'shell']; const alias = { https: 'http', ftps: 'ftp', ssh: 'shell' }; const handler = function (parsedUri, options) { if (!parsedUri.protocol) return Promise.reject(new Error(`Missing protocol in ${parsedUri.href}`)); let prot = parsedUri.protocol.replace(/:$/, '').toLowerCase(); if (alias[prot]) prot = alias[prot]; if (!prot) { return Promise.reject(new core_1.ExitError(`Could not detect protocol, specify in url.`, 'Possible protocols are: ' + protocols.join(', '))); } return Promise.resolve().then(() => require(`./${prot}`)); }; const load = async function (parsedUri, options = {}) { return handler(parsedUri).then(mod => { const start = Date.now(); return mod.load(parsedUri, options).then((res) => { res[1].time = Date.now() - start; return [res[0], res[1]]; }); }); }; const save = async function (parsedUri, buffer, options) { return handler(parsedUri).then(mod => mod.save(parsedUri, buffer, options)); }; const modified = async function (parsedUri, options) { return handler(parsedUri).then(mod => mod.modified(parsedUri, options)); }; exports.default = { load, save, modified }; //# sourceMappingURL=index.js.map