Skip to content

Commit

Permalink
Merge pull request #12 from ByteCommander/master
Browse files Browse the repository at this point in the history
Update broken download URL (move to https), add installer configuration method
  • Loading branch information
doapp-ryanp committed Mar 22, 2017
2 parents 08d3ab9 + a5c0b7f commit 892dc75
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 41 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
# dynamodb-local

A wrapper for AWS DynamoDB Local, intended for use in testcases. Will automatically download the files needed to run DynamoDb Local.
A wrapper for AWS DynamoDB Local, intended for use in testcases.
Will automatically download the files needed to run DynamoDb Local.

You can optionally override the download URL from where it fetches the installation archive
as well as the target directory to which it will install the binaries (default is your system's temp folder).


# Usage

Install the module as development dependency by running

`npm install dynamodb-local --save-dev`

Then in node:
Then in node, write your test script like this:

```javascript
var DynamoDbLocal = require('dynamodb-local');
var dynamoLocalPort = 8000;

DynamoDbLocal.launch(dynamoLocalPort, null, ['-sharedDb']); //if you want to share with Javascript Shell
//Do your tests
DynamoDbLocal.stop(8000);
DynamoDbLocal.launch(dynamoLocalPort, null, ['-sharedDb']) //if you want to share with Javascript Shell
.then(function () {

// do your tests

DynamoDbLocal.stop(dynamoLocalPort);
});
```

See [AWS DynamoDB Docs](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html) for more info on how to interact with DynamoDB Local.
Another example which also shows how to override the installer configuration can be found in
[examples/simple.js](examples/simple.js).

See [AWS DynamoDB Docs](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html)
for more info on how to interact with DynamoDB Local.
16 changes: 11 additions & 5 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

var DynamoDbLocal = require('../index');

// optional config customization - default is your OS' temp directory and an Amazon server from US West
DynamoDbLocal.configureInstaller({
installPath: './dynamodblocal-bin',
dowloadUrl: 'https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/dynamodb_local_latest.tar.gz'
});

DynamoDbLocal.launch(8000)
.then(function (ChildProcess) {
console.log("PID created: ", ChildProcess.pid);
console.log('PID created: ', ChildProcess.pid);

//do your tests
//do your tests

DynamoDbLocal.stop(8000);
console.log('finished');
});
DynamoDbLocal.stop(8000);
console.log('finished');
});
75 changes: 46 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
'use strict';

var os = require('os'),
spawn = require('child_process').spawn,
fs = require('fs'),
http = require('http'),
https = require('https'),
tar = require('tar'),
zlib = require('zlib'),
path = require('path'),
Expand All @@ -12,8 +12,12 @@ var os = require('os'),

var JARNAME = 'DynamoDBLocal.jar';

var tmpDynamoLocalDirDest = path.join(os.tmpdir(), 'dynamodb-local'),
runningProcesses = {},
var Config = {
installPath: path.join(os.tmpdir(), 'dynamodb-local'),
downloadUrl: 'https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz'
};

var runningProcesses = {},
DynamoDbLocal = {
/**
*
Expand Down Expand Up @@ -60,17 +64,17 @@ var tmpDynamoLocalDirDest = path.join(os.tmpdir(), 'dynamodb-local'),
args = args.concat(additionalArgs);

var child = spawn('java', args, {
cwd: tmpDynamoLocalDirDest,
cwd: Config.installPath,
env: process.env,
stdio: ['pipe', 'pipe', process.stderr]
});

if (!child.pid) throw new Error("Unable to launch DynamoDBLocal process");
if (!child.pid) throw new Error('Unable to launch DynamoDBLocal process');

child
.on('error', function (err) {
if (verbose) console.log("local DynamoDB start error", err);
throw new Error("Local DynamoDB failed to start. ");
if (verbose) console.log('local DynamoDB start error', err);
throw new Error('Local DynamoDB failed to start. ');
})
.on('close', function (code) {
if (code !== null && code !== 0) {
Expand All @@ -80,7 +84,10 @@ var tmpDynamoLocalDirDest = path.join(os.tmpdir(), 'dynamodb-local'),

runningProcesses[port] = child;

if (verbose) console.log("DynamoDbLocal(" + child.pid + ") started on port", port, "via java", args.join(' '), "from CWD", tmpDynamoLocalDirDest);
if (verbose) {
console.log('DynamoDbLocal(', child.pid, ') started on port', port,
'via java', args.join(' '), 'from CWD', Config.installPath);
}

return child;
});
Expand All @@ -94,46 +101,56 @@ var tmpDynamoLocalDirDest = path.join(os.tmpdir(), 'dynamodb-local'),
relaunch: function (port, db) {
this.stop(port);
this.launch(port, db);
},
configureInstaller: function (conf) {
if (conf.installPath) {
Config.installPath = conf.installPath;
}
if (conf.downloadUrl) {
Config.downloadUrl = conf.downloadUrl;
}
}
};

module.exports = DynamoDbLocal;

function installDynamoDbLocal() {
console.log("Checking for ", tmpDynamoLocalDirDest);
console.log('Checking for DynamoDB-Local in ', Config.installPath);
var deferred = Q.defer();

try {
if (fs.existsSync(path.join(tmpDynamoLocalDirDest, JARNAME))) {
if (fs.existsSync(path.join(Config.installPath, JARNAME))) {
return Q.fcall(function () {
return true;
});
}
} catch (e) {
}

console.log("DynamoDb Local not installed. Installing...");
console.log('DynamoDb Local not installed. Installing...');

if (!fs.existsSync(tmpDynamoLocalDirDest))
fs.mkdirSync(tmpDynamoLocalDirDest);
if (!fs.existsSync(Config.installPath))
fs.mkdirSync(Config.installPath);

http.get('http://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz', function (redirectResponse) {
if (200 != redirectResponse.statusCode) {
deferred.reject(new Error("Error getting DynamoDb local latest tar.gz location " + response.headers['location'] + ": " + redirectResponse.statusCode));
}
redirectResponse
.pipe(zlib.Unzip())
.pipe(tar.Extract({path: tmpDynamoLocalDirDest}))
.on('end', function () {
deferred.resolve();
https.get(Config.downloadUrl,
function (redirectResponse) {
if (200 != redirectResponse.statusCode) {
deferred.reject(new Error('Error getting DynamoDb local latest tar.gz location ' +
response.headers['location'] + ': ' + redirectResponse.statusCode));
}
redirectResponse
.pipe(zlib.Unzip())
.pipe(tar.Extract({path: Config.installPath}))
.on('end', function () {
deferred.resolve();
})
.on('error', function (err) {
deferred.reject(err);
});
})
.on('error', function (err) {
deferred.reject(err);
.on('error', function (e) {
deferred.reject(e);
});
})
.on('error', function (e) {
deferred.reject(e);
});

return deferred.promise;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "dynamodb-local",
"description": "A wrapper for AWS DynamoDB Local, intended for use in testcases",
"version": "0.0.14",
"version": "0.0.15",
"license": "MIT",
"repository": "doapp-ryanp/dynamodb-local",
"author": {
"name": "Ryan Pendergast",
Expand Down

0 comments on commit 892dc75

Please sign in to comment.