@alinex/datastore
Version:
Read, work and write data structures from and to differents locations and formats.
104 lines • 4.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.modified = exports.save = exports.load = void 0;
const debug_1 = require("debug");
const execa = require("execa");
const ssh2_1 = require("ssh2");
const debug = debug_1.default('datastore:protocol:shell');
const debugDetails = debug_1.default('datastore:details');
const load = (async function (parsedUri, options = {}) {
if (!parsedUri.hash)
return Promise.reject(`No command given in ${parsedUri.href}`);
let cmd = decodeURIComponent(parsedUri.hash.substr(1));
if (parsedUri.host) {
if (parsedUri.pathname)
cmd = `cd ${parsedUri.pathname} && ${cmd}`;
return new Promise((resolve, reject) => {
debug('loading from remote shell ssh://%s@%s:%s using %o', parsedUri.username, parsedUri.host, parsedUri.port, cmd);
const conn = new ssh2_1.Client();
conn.on('ready', () => {
conn.exec(cmd, (err, stream) => {
if (err) {
conn.end();
return reject(err);
}
const buf = [];
stream
.on('end', function () {
conn.end();
return resolve([Buffer.concat(buf), {}]);
})
.on('data', (data) => {
debugDetails('got data', data.toString());
buf.push(data);
});
});
}).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
});
});
}
debug('loading from shell using: %o', cmd);
const { stdout, ...meta } = await execa.command(cmd, {
cwd: parsedUri.host ? undefined : parsedUri.pathname || process.cwd(),
shell: true,
reject: !options.ignoreError
});
return [Buffer.from(stdout), meta];
});
exports.load = load;
const save = (async function (parsedUri, buffer, options) {
if (!parsedUri.hash)
return Promise.reject(`No command given in ${parsedUri.href}`);
debug(`storing to ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`);
let cmd = decodeURI(parsedUri.hash.substr(1));
if (parsedUri.host) {
if (parsedUri.pathname)
cmd = `cd ${parsedUri.pathname} && ${cmd}`;
return new Promise((resolve, reject) => {
debug('storing through remote shell ssh://%s@%s:%s using %o', parsedUri.username, parsedUri.host, parsedUri.port, cmd);
const conn = new ssh2_1.Client();
conn.on('ready', () => {
conn.exec(cmd, (err, stream) => {
if (err) {
conn.end();
return reject(err);
}
stream.on('end', function () {
conn.end();
return resolve();
});
});
}).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
});
});
}
debug(`storing through shell using: ${cmd}`);
await execa.command(cmd, {
input: buffer,
cwd: parsedUri.host ? undefined : parsedUri.pathname || process.cwd(),
shell: true
});
});
exports.save = save;
const modified = async function (parsedUri, options) {
if (!parsedUri.hash)
return Promise.reject(`No command given in ${parsedUri.href}`);
debug(`last modification check ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`);
return Promise.resolve(new Date());
};
exports.modified = modified;
//# sourceMappingURL=shell.js.map