-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ryanp
committed
Apr 1, 2015
1 parent
5adb305
commit 4897f79
Showing
5 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
# dynamodb-local | ||
dynamodb-local | ||
|
||
A wrapper for AWS DynamoDB Local, intended for use in testcases. | ||
|
||
# Usage | ||
|
||
```javascript | ||
var DynamoDbLocal = require('dynamodb-local'); | ||
|
||
DynamoDbLocal.launch(8000); | ||
DynamoDbLocal.relaunch(8000); | ||
``` | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
"use strict"; | ||
|
||
var os = require('os'), | ||
spawn = require('child_process').spawn, | ||
fs = require('fs'), | ||
http = require('http'), | ||
tar = require('tar'), | ||
zlib = require('zlib'), | ||
path = require('path'), | ||
mkdirp = require('mkdirp'); | ||
|
||
const JARNAME = 'DynamoDBLocal.jar'; | ||
|
||
var tmpDynamoLocalDirDest = os.tmpdir() + 'dynamodb-local', | ||
runningProcesses = {}, | ||
DynamoDbLocal = { | ||
/** | ||
* | ||
* @param port | ||
* @param dbPath if omitted will use in memory | ||
* @param additionalArgs | ||
* @returns {Promise.<ChildProcess>} | ||
*/ | ||
launch: function (port, dbPath, additionalArgs) { | ||
if (!dbPath) { | ||
dbPath = '-inMemory'; | ||
} | ||
else { | ||
dbPath = '-dbPath ' + dbPath; | ||
} | ||
|
||
if (!additionalArgs) { | ||
additionalArgs = []; | ||
} | ||
else if (Array.isArray(additionalArgs)) { | ||
additionalArgs = [additionalArgs]; | ||
} | ||
|
||
return installDynamoDbLocal() | ||
.then(function () { | ||
let args = [ | ||
'-Djava.library.path=./DynamoDBLocal_lib -jar ' + JARNAME, | ||
'--port', port | ||
]; | ||
args.concat(additionalArgs); | ||
|
||
let child = spawn('java', args, {cwd: 'test', env: process.env}); | ||
|
||
runningProcesses[port] = child; | ||
|
||
return child; | ||
}); | ||
}, | ||
stop: function (port) { | ||
if (runningProcesses[port]) { | ||
runningProcesses[port].kill(); | ||
} | ||
}, | ||
relaunch: function (port, db) { | ||
this.stop(port); | ||
this.launch(port, db); | ||
} | ||
}; | ||
|
||
module.exports = DynamoDbLocal; | ||
|
||
function installDynamoDbLocal() { | ||
console.log("Checking for ", tmpDynamoLocalDirDest); | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
let jarPath = tmpDynamoLocalDirDest + '/' + JARNAME, | ||
stats = fs.lstatSync(tmpDynamoLocalDirDest); | ||
|
||
if (stats.isDirectory() && fs.existsSync(tmpDynamoLocalDirDest + '/' + jarPath)) { | ||
resolve(); | ||
} | ||
} | ||
catch (e) { | ||
} | ||
|
||
http | ||
.get('http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz', function (response) { | ||
if (302 != response.statusCode) { | ||
reject("Error getting DyanmoDb local latest tar.gz location: " + response.statusCode); | ||
} | ||
|
||
http | ||
.get(response.headers['location'], function (redirectResponse) { | ||
if (200 != redirectResponse.statusCode) { | ||
reject("Error getting DyanmoDb local latest tar.gz location " + response.headers['location'] + ": " + redirectResponse.statusCode); | ||
} | ||
redirectResponse | ||
.pipe(zlib.Unzip()) | ||
.pipe(tar.Parse()) | ||
.on('entry', function (entry) { | ||
var fullpath = path.join(tmpDynamoLocalDirDest, entry.path); | ||
if ('Directory' == entry.type) { | ||
fs.mkdirSync(fullpath); | ||
} | ||
else { | ||
mkdirp(path.dirname(fullpath), function (err) { | ||
if (err) reject(err); | ||
entry.pipe(fs.createWriteStream(fullpath)); | ||
}); | ||
} | ||
}) | ||
.on('finish', function () { | ||
resolve(); | ||
}) | ||
.on('error', function (err) { | ||
reject(err); | ||
}); | ||
}) | ||
.on('error', function (e) { | ||
reject(e.message); | ||
}); | ||
}) | ||
.on('error', function (e) { | ||
reject(e.message); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "dynamodb-local", | ||
"description": "A wrapper for AWS DynamoDB Local, intended for use in testcases", | ||
"version": "0.0.4", | ||
"homepage": "https://github.com/doapp-ryanp/dynamodb-local", | ||
"author": { | ||
"name": "Ryan Pendergast (DoApp, Inc)", | ||
"email": "[email protected]" | ||
}, | ||
"main": "./index.js", | ||
"engines": { | ||
"iojs": ">= 1.6.3", | ||
"npm": ">= 2.7.4" | ||
}, | ||
"dependencies": { | ||
"mkdirp": "^0.5.0", | ||
"tar": "^2.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
var DynamoDbLocal = require('./index'); | ||
|
||
DynamoDbLocal.launch(8000) | ||
.then(function (ChildProcess) { | ||
console.log("PID: ", ChildProcess.pid); | ||
console.log("finished"); | ||
}); |