From e0e647d419215b6a7e562dd64bdbfcd58443952d Mon Sep 17 00:00:00 2001 From: timestopper Date: Mon, 10 Dec 2018 15:15:12 +0200 Subject: [PATCH 1/2] Fix issue 84 https://github.com/markitx/dynamo-backup-to-s3/issues/84 TypeError: Cannot read property push of undefined on dynamo-restore-from-s3 --- lib/dynamo-restore.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/dynamo-restore.js b/lib/dynamo-restore.js index b341fb8..66abc3d 100755 --- a/lib/dynamo-restore.js +++ b/lib/dynamo-restore.js @@ -25,9 +25,12 @@ function DynamoRestore(options) { options.awsSecret = options.awsSecret || process.env.AWS_SECRET_ACCESS_KEY; options.awsRegion = options.awsRegion || process.env.AWS_DEFAULT_REGION || 'ap-southeast-2'; + // AWS.config.update({ + // accessKeyId: options.awsKey, + // secretAccessKey: options.awsSecret, + // region: options.awsRegion + // }); AWS.config.update({ - accessKeyId: options.awsKey, - secretAccessKey: options.awsSecret, region: options.awsRegion }); @@ -107,7 +110,8 @@ DynamoRestore.prototype._checkTableExists = function(error, data) { if (this.options.overwrite) { this.emit('warning', util.format('WARN: table [%s] will be overwritten.', this.options.table)); setTimeout(dynamodb.describeTable.bind(dynamodb, { TableName: this.options.table }, this._checkTableReady.bind(this)), 1000); - } else { + } + else { this.emit('error', 'Fatal Error. The destination table already exists! Exiting process..'); } return; @@ -145,10 +149,15 @@ DynamoRestore.prototype._startDownload = function() { .on('line', this._processLine.bind(this)) .on('close', (function() { this.emit('finish-download'); - this.batches.push({ - items: this.requestItems.splice(0, DYNAMO_CHUNK_SIZE), - attempts: 0 - }); + // if (!this.batches) { + // this.batches = [] + // } + if (this.requestItems && this.requestItems.length) { + this.batches.push({ + items: this.requestItems.splice(0, DYNAMO_CHUNK_SIZE), + attempts: 0 + }); + } this._finishBatches(); }).bind(this)); this.readline.meta = meta; @@ -281,7 +290,7 @@ DynamoRestore.prototype._checkTableReady = function(error, data) { // All ready, now we can start inserting records this.tableready = true; this.readline.resume(); - while (this.batches.length) { this._sendBatch(); } + while (this.batches && this.batches.length) { this._sendBatch(); } } else { setTimeout(dynamodb.describeTable.bind(dynamodb, { TableName: data.Table.TableName }, this._checkTableReady.bind(this)), 1000); } @@ -344,8 +353,8 @@ DynamoRestore.prototype._sendBatch = function() { }; DynamoRestore.prototype._finishBatches = function() { - if (!this.batches.length) { - if (!this.requests.length) { + if (!this.batches || !this.batches.length) { + if (!this.requests || !this.requests.length) { // Finish only if there is nothing left to wait for. this.emit('finish'); return; @@ -360,4 +369,4 @@ DynamoRestore.prototype._finishBatches = function() { setTimeout(this._finishBatches.bind(this), 200); }; -module.exports = DynamoRestore; \ No newline at end of file +module.exports = DynamoRestore; From f7965882b41bb4eec784cfbd52b6bbcf4e11bf09 Mon Sep 17 00:00:00 2001 From: timestopper Date: Mon, 10 Dec 2018 15:27:06 +0200 Subject: [PATCH 2/2] Update dynamo-restore.js --- lib/dynamo-restore.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/dynamo-restore.js b/lib/dynamo-restore.js index 66abc3d..a1119d1 100755 --- a/lib/dynamo-restore.js +++ b/lib/dynamo-restore.js @@ -24,15 +24,16 @@ function DynamoRestore(options) { options.awsKey = options.awsKey || process.env.AWS_ACCESS_KEY_ID; options.awsSecret = options.awsSecret || process.env.AWS_SECRET_ACCESS_KEY; options.awsRegion = options.awsRegion || process.env.AWS_DEFAULT_REGION || 'ap-southeast-2'; - - // AWS.config.update({ - // accessKeyId: options.awsKey, - // secretAccessKey: options.awsSecret, - // region: options.awsRegion - // }); - AWS.config.update({ + var awsConfigData = { region: options.awsRegion - }); + } + + if ( options.awsSecret && options.awsRegion ) { + awsConfigData.accessKeyId = options.awsKey; + awsConfigData.secretAccessKey = options.awsSecret; + } + + AWS.config.update(awsConfigData); this.options = options; this.dynamodb = new AWS.DynamoDB();