Skip to content

Commit

Permalink
Fix region being used as options object, closes #14
Browse files Browse the repository at this point in the history
Thanks to @shiroh for noticing this and providing a PR!
  • Loading branch information
mhart committed May 31, 2015
1 parent e03a7cb commit 6c99e35
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ Returns a readable and writable Node.js stream for the given Kinesis stream

`options` include:

- `region`: a string, or object with AWS credentials, host, port, etc (`us-east-1` by default)
- `region`: a string, or (deprecated) object with AWS credentials, host, port, etc (resolved from env or file by default)
- `credentials`: an object with `accessKeyId`/`secretAccessKey` properties (resolved from env, file or IAM by default)
- `shards`: an array of shard IDs, or shard objects. If not provided, these will be fetched and cached.
- `oldest`: if truthy, then will start at the oldest records (using `TRIM_HORIZON`) instead of the latest
- `writeConcurrency`: how many parallel writes to allow (`1` by default)
- `cacheSize`: number of PartitionKey-to-SequenceNumber mappings to cache (`1000` by default)
- `agent`: HTTP agent used (uses Node.js defaults otherwise)
- `timeout`: HTTP request timeout (uses Node.js defaults otherwise)
- `initialRetryMs`: first pause before retrying under the default policy (`50` by default)
- `maxRetries`: max number of retries under the default policy (`10` by default)
Expand All @@ -96,6 +98,14 @@ Makes a generic Kinesis request with the given action (eg, `ListStreams`) and da

`options` include:

- `region`: a string, or object with AWS credentials, host, port, etc (`us-east-1` by default)


- `region`: a string, or (deprecated) object with AWS credentials, host, port, etc (resolved from env or file by default)
- `credentials`: an object with `accessKeyId`/`secretAccessKey` properties (resolved from env, file or IAM by default)
- `agent`: HTTP agent used (uses Node.js defaults otherwise)
- `timeout`: HTTP request timeout (uses Node.js defaults otherwise)
- `initialRetryMs`: first pause before retrying under the default policy (`50` by default)
- `maxRetries`: max number of retries under the default policy (`10` by default)
- `errorCodes`: array of Node.js error codes to retry on (`['EADDRINFO',
'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'EMFILE']` by default)
- `errorNames`: array of Kinesis exceptions to retry on
(`['ProvisionedThroughputExceededException', 'ThrottlingException']` by default)
- `retryPolicy`: a function to implement a retry policy different from the default one
42 changes: 22 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ KinesisStream.prototype._write = function(data, encoding, cb) {
self.sequenceCache.set(data.PartitionKey, sequenceNumber)

self.resolveShards(function(err, shards) {
if (err) {
self.emit('putRecord')
return self.emit('error', err)
}
for (var i = 0; i < shards.length; i++) {
if (shards[i].id != responseData.ShardId) continue

Expand Down Expand Up @@ -259,6 +263,8 @@ function request(action, data, options, cb) {

cb = once(cb)

options = resolveOptions(options)

function loadCreds(cb) {
var needRegion = !options.region
var needCreds = !options.credentials || !options.credentials.accessKeyId || !options.credentials.secretAccessKey
Expand Down Expand Up @@ -286,7 +292,8 @@ function request(action, data, options, cb) {
}
}

options = resolveOptions(options)
if (!options.region) options.region = (options.host || '').split('.', 2)[1] || 'us-east-1'
if (!options.host) options.host = 'kinesis.' + options.region + '.amazonaws.com'

var httpOptions = {},
body = JSON.stringify(data),
Expand Down Expand Up @@ -323,7 +330,7 @@ function request(action, data, options, cb) {
res.setEncoding('utf8')

res.on('error', cb)
res.on('data', function(chunk){ json += chunk })
res.on('data', function(chunk) { json += chunk })
res.on('end', function() {
var response, parseError

Expand Down Expand Up @@ -410,27 +417,22 @@ function resolveOptions(options) {
return clone
}, {})

if (typeof region === 'object') {
options.host = region.host
options.port = region.port
options.region = region.region
options.version = region.version
options.agent = region.agent
options.https = region.https
options.credentials = region.credentials
} else {
if (/^[a-z]{2}\-[a-z]+\-\d$/.test(region))
options.region = region
else if (!options.host)
// Backwards compatibility for when 1st param was host
options.host = region
if (typeof region === 'object' && region != null) {
options.host = options.host || region.host
options.port = options.port || region.port
options.region = options.region || region.region
options.version = options.version || region.version
options.agent = options.agent || region.agent
options.https = options.https || region.https
options.credentials = options.credentials || region.credentials
} else if (/^[a-z]{2}\-[a-z]+\-\d$/.test(region)) {
options.region = region
} else if (!options.host) {
// Backwards compatibility for when 1st param was host
options.host = region
}
if (!options.region) options.region = (options.host || '').split('.', 2)[1] || 'us-east-1'
if (!options.host) options.host = 'kinesis.' + options.region + '.amazonaws.com'
if (!options.version) options.version = '20131202'

if (!options.credentials) options.credentials = options.credentials

return options
}

Expand Down

0 comments on commit 6c99e35

Please sign in to comment.