Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
jed committed Jan 27, 2012
0 parents commit de0d945
Show file tree
Hide file tree
Showing 14 changed files with 511 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
credentials
node_modules
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
credentials
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- 0.6
20 changes: 20 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,20 @@
Copyright (c) 2011 Jed Schmidt, http://jed.is/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
@@ -0,0 +1,11 @@
dynamo (coming soon...)
=======================

[![Build Status](https://secure.travis-ci.org/jed/dynamo.png)](http://travis-ci.org/jed/dynamo)

Copyright
---------

Copyright (c) 2012 Jed Schmidt. See LICENSE.txt for details.

Send any questions or comments [here](http://twitter.com/jedschmidt).
34 changes: 34 additions & 0 deletions lib/Account.js
@@ -0,0 +1,34 @@
var crypto = require("crypto")
, Database = require("./Database")
, Session = require("./Session")

function Account(credentials) {
this.session = new Session(credentials)
this.database = new Database
this.database.account = this
}

Account.prototype.sign = function sign(request, cb) {
this.session.fetch(function(err, session) {
if (err) return cb(err)

var hash = crypto.createHash("sha256")

request.headers["x-amz-security-token"] = session.token

hash = hash.update(request.toString()).digest()

request.headers["x-amzn-authorization"] = "AWS3 " + [
"AWSAccessKeyId=" + session.credentials.accessKeyId,
"Algorithm=HmacSHA256",
"SignedHeaders=host;x-amz-date;x-amz-security-token;x-amz-target",
"Signature=" + session.credentials.sign(hash)
]

cb(null, request)
})
}

Account.Database = Database

module.exports = Account
16 changes: 16 additions & 0 deletions lib/Credentials.js
@@ -0,0 +1,16 @@
var crypto = require("crypto")

function Credentials(attrs) {
var secretAccessKey = attrs.secretAccessKey

this.accessKeyId = attrs.accessKeyId

this.sign = function(data) {
return crypto
.createHmac("sha256", secretAccessKey)
.update(data)
.digest("base64")
}
}

module.exports = Credentials
65 changes: 65 additions & 0 deletions lib/Database.js
@@ -0,0 +1,65 @@
var Request = require("./Request")
, log = console.log.bind(console)

function Database(){}

Database.prototype = {
listTables: function(options, cb) {
this.request("ListTables", options, cb)
},

createTable: function(options, cb) {
this.request("CreateTable", options, cb)
},

describeTable: function(options, cb) {
this.request("DescribeTable", options, cb)
},

updateTable: function(options, cb) {
this.request("UpdateTable", options, cb)
},

deleteTable: function(options, cb) {
this.request("DeleteTable", options, cb)
},

scan: function(options, cb) {
this.request("Scan", options, cb)
},

query: function(options, cb) {
this.request("Query", options, cb)
},

batchGetItems: function(options, cb) {
this.request("BatchGetItems", options, cb)
},

getItem: function(options, cb) {
this.request("GetItem", options, cb)
},

putItem: function(options, cb) {
this.request("PutItem", options, cb)
},

updateItem: function(options, cb) {
this.request("UpdateItem", options, cb)
},

deleteItem: function(options, cb) {
this.request("DeleteItem", options, cb)
},

request: function(target, data, cb) {
this.account.sign(
new Request(target, data),
function(err, request) {
err ? cb(err) : request.send(cb || log)
}
)
}
}

module.exports = Database
68 changes: 68 additions & 0 deletions lib/Request.js
@@ -0,0 +1,68 @@
var http = require("http")
, crypto = require("crypto")

function Request(target, data) {
var headers = this.headers = new Headers

this.json = JSON.stringify(data)

headers["x-amz-target"] = Request.prototype.target + target
headers["Host"] = this.host
headers["Content-Length"] = Buffer.byteLength(this.json)
}

Request.prototype = {
method: "POST",
host: "dynamodb.us-east-1.amazonaws.com",
pathname: "/",
target: "DynamoDB_20111205.",
data: {},

toString: function() {
return this.method +
"\n" + this.pathname +
"\n" +
"\n" + this.headers +
"\n" +
"\n" + this.json
},

send: function(cb) {
var request = http.request(this, function(res) {
var json = ""

res.on("data", function(chunk){ json += chunk })
res.on("end", function() {
var response = JSON.parse(json)

if (res.statusCode == 200) cb(null, response)

else cb(new Error(response.__type + ": " + response.message))
})
})

request.on("error", cb)

request.write(this.json)
request.end()
}
}

function Headers() {
this["x-amz-date"] = this["Date"] = (new Date).toUTCString()
this["Content-Type"] = Headers.prototype["Content-Type"]
}

Headers.prototype = {
"Content-Type": "application/x-amz-json-1.0",

toString: function() {
return "host:" + this["Host"] +
"\nx-amz-date:" + this["x-amz-date"] +
"\nx-amz-security-token:" + this["x-amz-security-token"] +
"\nx-amz-target:" + this["x-amz-target"]
}
}

Request.Headers = Headers
module.exports = Request
120 changes: 120 additions & 0 deletions lib/Session.js
@@ -0,0 +1,120 @@
var https = require("https")
, crypto = require("crypto")
, Credentials = require("./Credentials")

function Session(attrs) {
this.credentials = new Credentials(attrs)
this.listeners = []
}

Session.prototype = {
duration: 60 * 60 * 1000,
consumedCapacity: 0,

fetch: function(cb) {
if (this.expiration > new Date) return cb(null, this)

this.listeners.push(cb) > 1 || this.refresh()
},

refresh: function() {
var req = new Request

req.query.DurationSeconds = 0 | this.duration / 1000
req.query.AWSAccessKeyId = this.credentials.accessKeyId
req.query.Signature = this.credentials.sign(req.toString(), "sha256", "base64")

req.send(function(err, data) {
var listeners = this.listeners.splice(0)

if (!err) {
this.expiration = new Date(data.expiration)
this.credentials = new Credentials(data)
this.token = data.sessionToken
}

listeners.forEach(function(cb) {
cb(err, err ? null : this)
}, this)
}.bind(this))
}
}

function Request() {
this.query = new Query
}

Request.prototype = {
method: "GET",
host: "sts.amazonaws.com",
pathname: "/",

toString: function() {
return this.method +
"\n" + this.host +
"\n" + this.pathname +
"\n" + this.query.toString().slice(1)
},

send: function(cb) {
var signature = encodeURIComponent(this.query.Signature)
, query = this.query + "&Signature=" + signature
, path = Request.prototype.pathname + query
, options = { host: this.host, path: path }

https.get(options, function(res) {
var xml = ""

res.on("data", function(chunk){ xml += chunk })
res.on("end", function() {
var response = new Response(xml)

if (res.statusCode == 200) cb(null, response)

else cb(new Error(
response.type + "(" + response.code + ")\n\n" +
response.message
))
})
})
}
}

function Query() {
this.Timestamp = (new Date).toISOString().slice(0, 19) + "Z"
}

Query.prototype = {
Action : "GetSessionToken",
SignatureMethod : "HmacSHA256",
SignatureVersion : "2",
Version : "2011-06-15",

toString: function() {
return (
"?AWSAccessKeyId=" + this.AWSAccessKeyId +
"&Action=" + this.Action +
"&DurationSeconds=" + this.DurationSeconds +
"&SignatureMethod=" + this.SignatureMethod +
"&SignatureVersion=" + this.SignatureVersion +
"&Timestamp=" + encodeURIComponent(this.Timestamp) +
"&Version=" + this.Version
)
}
}

function Response(xml) {
var tag, key, regexp = /<(\w+)>(.*)</g

while (tag = regexp.exec(xml)) {
key = tag[1]
key = key.charAt(0).toLowerCase() + key.slice(1)
this[key] = tag[2]
}
}

Request.Query = Query
Session.Request = Request
Session.Response = Response
Session.Credentials = Credentials
module.exports = Session
9 changes: 9 additions & 0 deletions lib/index.js
@@ -0,0 +1,9 @@
var Account = require("./Account")
, Session = require("./Session")

exports.Account = Account
exports.Session = Session

exports.createClient = function(credentials) {
return new Account(credentials).database
}
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"author": "Jed Schmidt <tr@nslator.jp> (http://jed.is)",
"name": "dynamo",
"description": "DynamoDB client for node.js",
"version": "0.0.1",
"homepage": "https://github.com/jed/dynamo",
"repository": {
"type": "git",
"url": "git://github.com/jed/dynamo.git"
},
"main": "./lib",
"scripts": {
"test": "mocha"
},
"engines": {
"node": "~0.6.x"
},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"config": {
"credentials": "dynamo-credentials.herokuapp.com"
}
}

0 comments on commit de0d945

Please sign in to comment.