app.js
function(srcStream, targetUrl, opts, callback) {
    var listenerErrorIndicator = { errored: false };
    if (typeof callback === 'undefined' && typeof opts === 'function') {
      callback = opts;
      opts = {};
    }
    validateArgs(targetUrl, opts, callback);
    opts = Object.assign({}, defaults(), opts);

    const ee = new events.EventEmitter();

    // Set up the DB client
    const restoreDB = request.client(targetUrl, opts);

    // Validate the DB exists, before proceeding to restore
    proceedIfDbValid(restoreDB, function(err) {
      if (err) {
        if (err.name === 'DatabaseNotFound') {
          err.message = `${err.message} Create the target database before restoring.`;
        }
        // Didn't exist, or another fatal error, exit
        callback(err);
        return;
      }

      restoreInternal(
        restoreDB,
        opts,
        srcStream,
        ee,
        function(err, writer) {
          if (err) {
            callback(err, null);
            return;
          }
          if (writer != null) {
            addEventListener(listenerErrorIndicator, writer, 'restored', function(obj) {
              debug(' restored ', obj.total);
              ee.emit('restored', { documents: obj.documents, total: obj.total });
            });
            addEventListener(listenerErrorIndicator, writer, 'error', function(err) {
              debug('Error ' + JSON.stringify(err));
              // Only call destroy if it is available on the stream
              if (srcStream.destroy && srcStream.destroy instanceof Function) {
                srcStream.destroy();
              }
              callback(err);
            });
            addEventListener(listenerErrorIndicator, writer, 'finished', function(obj) {
              debug('restore complete');
              ee.emit('finished', { total: obj.total });
              callback(null, obj);
            });
          }
        }
      );
    });
    return ee;
  }
function(srcStream, targetUrl, opts, callback) {
    var listenerErrorIndicator = { errored: false };
    if (typeof callback === 'undefined' && typeof opts === 'function') {
      callback = opts;
      opts = {};
    }
    validateArgs(targetUrl, opts, callback);
    opts = Object.assign({}, defaults(), opts);

    const ee = new events.EventEmitter();

    // Set up the DB client
    const restoreDB = request.client(targetUrl, opts);

    // Validate the DB exists, before proceeding to restore
    proceedIfDbValid(restoreDB, function(err) {
      if (err) {
        if (err.name === 'DatabaseNotFound') {
          err.message = `${err.message} Create the target database before restoring.`;
        }
        // Didn't exist, or another fatal error, exit
        callback(err);
        return;
      }

      restoreInternal(
        restoreDB,
        opts,
        srcStream,
        ee,
        function(err, writer) {
          if (err) {
            callback(err, null);
            return;
          }
          if (writer != null) {
            addEventListener(listenerErrorIndicator, writer, 'restored', function(obj) {
              debug(' restored ', obj.total);
              ee.emit('restored', { documents: obj.documents, total: obj.total });
            });
            addEventListener(listenerErrorIndicator, writer, 'error', function(err) {
              debug('Error ' + JSON.stringify(err));
              // Only call destroy if it is available on the stream
              if (srcStream.destroy && srcStream.destroy instanceof Function) {
                srcStream.destroy();
              }
              callback(err);
            });
            addEventListener(listenerErrorIndicator, writer, 'finished', function(obj) {
              debug('restore complete');
              ee.emit('finished', { total: obj.total });
              callback(null, obj);
            });
          }
        }
      );
    });
    return ee;
  }
function ( srcStream , targetUrl , opts , callback ) { var listenerErrorIndicator = { errored : false } ; if ( typeof callback === 'undefined' && typeof opts === 'function' ) { callback = opts ; opts = { } ; } validateArgs ( targetUrl , opts , callback ) ; opts = Object . assign ( { } , defaults ( ) , opts ) ; const ee = new events . EventEmitter ( ) ; // Set up the DB client const restoreDB = request . client ( targetUrl , opts ) ; // Validate the DB exists, before proceeding to restore proceedIfDbValid ( restoreDB , function ( err ) { if ( err ) { if ( err . name === 'DatabaseNotFound' ) { err . message = ` ${ err . message } ` ; } // Didn't exist, or another fatal error, exit callback ( err ) ; return ; } restoreInternal ( restoreDB , opts , srcStream , ee , function ( err , writer ) { if ( err ) { callback ( err , null ) ; return ; } if ( writer != null ) { addEventListener ( listenerErrorIndicator , writer , 'restored' , function ( obj ) { debug ( ' restored ' , obj . total ) ; ee . emit ( 'restored' , { documents : obj . documents , total : obj . total } ) ; } ) ; addEventListener ( listenerErrorIndicator , writer , 'error' , function ( err ) { debug ( 'Error ' + JSON . stringify ( err ) ) ; // Only call destroy if it is available on the stream if ( srcStream . destroy && srcStream . destroy instanceof Function ) { srcStream . destroy ( ) ; } callback ( err ) ; } ) ; addEventListener ( listenerErrorIndicator , writer , 'finished' , function ( obj ) { debug ( 'restore complete' ) ; ee . emit ( 'finished' , { total : obj . total } ) ; callback ( null , obj ) ; } ) ; } } ) ; } ) ; return ee ; }
Restore a backup from a stream.

@param {stream.Readable} srcStream - Stream containing backed up data.
@param {string} targetUrl - Target database.
@param {object} opts - Restore options.
@param {number} opts.parallelism - Number of parallel HTTP requests to use. Default 5.
@param {number} opts.bufferSize - Number of documents per batch request. Default 500.
@param {number} opts.requestTimeout - Milliseconds to wait before retrying a HTTP request. Default 120000.
@param {string} opts.iamApiKey - IAM API key to use to access Cloudant database.
@param {backupRestoreCallback} callback - Called on completion.
Restore a backup from a stream .