UNPKG

@alinex/datastore

Version:

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

73 lines 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.modified = exports.save = exports.load = void 0; const path_1 = require("path"); const debug_1 = require("debug"); const sftpClient = require("ssh2-sftp-client"); const debug = debug_1.default('datastore:protocol:sftp'); const debugDetails = debug_1.default('datastore:details'); const load = function (parsedUri, options) { if (!parsedUri.pathname) return Promise.reject(`No pathname given in ${parsedUri.href}`); debug(`loading ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`); const meta = { path: parsedUri.pathname }; let sftp = new sftpClient(); return sftp .connect({ host: parsedUri.host, port: parsedUri.port ? Number(parsedUri.port) : undefined, username: parsedUri.username, password: parsedUri.password, privateKey: options ? options.privateKey : undefined, passphrase: options ? options.passphrase : undefined, debug: debugDetails.enabled ? debugDetails : undefined }) .then(() => sftp.stat(parsedUri.pathname)) .then(stat => (meta.stats = stat)) .then(() => sftp.get(parsedUri.pathname)) .then(data => { sftp.end(); return [data, meta]; }); }; exports.load = load; const save = (function (parsedUri, buffer, options) { if (!parsedUri.pathname) return Promise.reject(`No pathname given in ${parsedUri.href}`); debug(`storing to ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`); let sftp = new sftpClient(); return sftp .connect({ host: parsedUri.host, port: parsedUri.port ? Number(parsedUri.port) : undefined, username: parsedUri.username, password: parsedUri.password, privateKey: options ? options.privateKey : undefined, passphrase: options ? options.passphrase : undefined, debug: debugDetails.enabled ? debugDetails : undefined }) .then(() => sftp.put(buffer, parsedUri.pathname)); }); exports.save = save; const modified = async function (parsedUri, options) { if (!parsedUri.pathname) return Promise.reject(`No pathname given in ${parsedUri.href}`); debug(`last modification check ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`); let sftp = new sftpClient(); return sftp .connect({ host: parsedUri.host, port: parsedUri.port ? Number(parsedUri.port) : undefined, username: parsedUri.username, password: parsedUri.password, privateKey: options ? options.privateKey : undefined, passphrase: options ? options.passphrase : undefined }) .then(() => sftp.list(path_1.dirname(parsedUri.pathname))) .then(stats => { const f = path_1.basename(parsedUri.pathname); return new Date(stats.filter(stat => stat.name == f)[0].modifyTime); }); }; exports.modified = modified; //# sourceMappingURL=sftp.js.map