UNPKG

gpg-ts

Version:

GPG encryption and decryption in node.js by way of the gpg command-line tool. Now with TypeScript.

133 lines 4.94 kB
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.spawnStreamingGPG = exports.spawnGPG = void 0; const child_process_1 = require("child_process"); const fs_1 = require("fs"); const _ = __importStar(require("lodash")); const globalArgs = ['--batch']; /** * Wrapper around spawning GPG. Handles stdout, stderr, and default args. * * @param {String} input Input string. Piped to stdin. * @param {Array} defaultArgs Default arguments for this task. * @param {Array} args Arguments to pass to GPG when spawned. * @param {Function} cb Callback. Last parameter of the callback is stderr. */ function spawnGPG(input, defaultArgs, args, cb) { cb = _.once(cb); const gpgArgs = (args || []).concat(defaultArgs); let buffers = []; let buffersLength = 0; let error = ''; const gpg = spawnIt(gpgArgs, cb); gpg.stdout.on('data', function (buf) { buffers.push(buf); buffersLength += buf.length; }); gpg.stderr.on('data', function (buf) { error += buf.toString('utf8'); }); gpg.on('close', function (code) { const msg = Buffer.concat(buffers, buffersLength); if (code !== 0) { // If error is empty, we probably redirected stderr to stdout (for verifySignature, import, etc) return cb(new Error(error || msg.toString('utf-8')), msg, error); } cb(null, msg, error); }); gpg.stdin.end(input); } exports.spawnGPG = spawnGPG; /** * Similar to spawnGPG, but sets up a read/write pipe to/from a stream. * * @param {Object} options Options. Should have source and dest strings or streams. * @param {Array} args GPG args. * @param {Function} cb Callback */ function spawnStreamingGPG(options, args, cb) { cb = _.once(cb); options = options || {}; if (typeof options.source !== 'string' && !isStream(options.source)) { return cb(new Error('Missing \'source\' option (string or stream)'), null); } else if (typeof options.dest !== 'string' && !isStream(options.dest)) { return cb(new Error('Missing \'dest\' option (string or stream)'), null); } let sourceStream; if (!isReadable(options.source)) { // This will throw if the file doesn't exist try { sourceStream = fs_1.createReadStream(options.source); } catch (e) { return cb(new Error(options.source + ' does not exist. Error: ' + e.message), null); } } else { sourceStream = options.source; } let destStream; if (!isWritable(options.dest)) { try { destStream = fs_1.createWriteStream(options.dest); } catch (e) { return cb(new Error('Error opening ' + options.dest + '. Error: ' + e.message), null); } } else { destStream = options.dest; } // Go for it const gpg = spawnIt(args, cb); if (!isStream(options.dest)) { gpg.on('close', code => { cb(null, null); }); } else { cb(null, destStream); } // Pipe input file into gpg stdin; gpg stdout into output file.. sourceStream.pipe(gpg.stdin); gpg.stdout.pipe(destStream); } exports.spawnStreamingGPG = spawnStreamingGPG; // Wrapper around spawn. Catches error events and passed global args. function spawnIt(args, fn) { const gpg = child_process_1.spawn('gpg', globalArgs.concat(args || [])); gpg.on('error', err => { fn(err, null, null); }); return gpg; } // Check if input is stream with duck typing function isStream(stream) { return stream != null && typeof stream === 'object' && typeof stream.pipe === 'function'; } function isReadable(stream) { return stream != null && typeof stream === 'object' && typeof stream.pipe === 'function' && typeof stream.read === 'function'; } function isWritable(stream) { return stream != null && typeof stream === 'object' && typeof stream.pipe === 'function' && typeof stream.write === 'function'; } //# sourceMappingURL=spawnGPG.js.map