promised-io
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -63,3 +63,3 @@ /** | ||
if (request.queryString) { | ||
request.path += "?"+request.queryString; | ||
request.path += "?"+request.queryString; | ||
} | ||
@@ -66,0 +66,0 @@ var timedOut; |
@@ -31,3 +31,3 @@ /* | ||
timeouts = {}, | ||
timer, | ||
timer, | ||
queue; | ||
@@ -69,3 +69,3 @@ | ||
delay = Math.floor(delay); | ||
if(repeat){ | ||
@@ -77,3 +77,3 @@ timer.schedule(task, delay, delay); | ||
} | ||
return id; | ||
@@ -80,0 +80,0 @@ } |
@@ -21,5 +21,5 @@ var File = require("fs"), | ||
}catch(e){ | ||
var deferred = defer(); | ||
deferred.reject(e); | ||
return deferred.promise; | ||
var deferred = defer(); | ||
deferred.reject(e); | ||
return deferred.promise; | ||
} | ||
@@ -26,0 +26,0 @@ }; |
@@ -16,5 +16,5 @@ /** | ||
promised = true; | ||
if (request.jsgi && "async" in request.jsgi) promised = request.jsgi.async; | ||
for (var header in this.headers) { | ||
@@ -36,3 +36,3 @@ var value = this.headers[header]; | ||
if (typeof writer !== "undefined") writer.close(); | ||
try { | ||
@@ -45,3 +45,3 @@ connection.connect(); | ||
} | ||
var status = Number(connection.getResponseCode()), | ||
@@ -67,3 +67,3 @@ headers = {}; | ||
} | ||
var reader = new java.io.BufferedReader(new java.io.InputStreamReader(is)), | ||
@@ -88,2 +88,2 @@ builder = new java.lang.StringBuilder(), | ||
} | ||
}; | ||
}; |
32
fs.js
@@ -7,3 +7,3 @@ /** | ||
var fs = require("./engines/rhino/fs"); | ||
// for rhino | ||
@@ -21,10 +21,12 @@ for(var i in fs){ | ||
convertNodeAsyncFunction = require("./promise").convertNodeAsyncFunction; | ||
// convert all the non-sync functions | ||
// convert all the non-sync functions that have a sync counterpart | ||
for (var i in fs) { | ||
if (i.match(/Sync$/) || i.match(/watch/)) { | ||
exports[i] = fs[i]; | ||
if ((i + 'Sync') in fs) { | ||
// async | ||
exports[i] = convertNodeAsyncFunction(fs[i], i === "readFile"); | ||
} | ||
else{ | ||
exports[i] = convertNodeAsyncFunction(fs[i], i === "readFile"); | ||
// sync, or something that we can't auto-convert | ||
exports[i] = fs[i]; | ||
} | ||
@@ -45,3 +47,3 @@ } | ||
fs.read(fd, buffer, 0, 4096, null, readResponse); | ||
} | ||
} | ||
function readResponse(err, bytesRead){ | ||
@@ -85,3 +87,3 @@ if(err){ | ||
readAndSend(); | ||
return deferred.promise; | ||
return deferred.promise; | ||
}, | ||
@@ -111,7 +113,7 @@ length: 0 | ||
File.prototype = LazyArray.prototype; | ||
var nodeRead = exports.read; | ||
var nodeRead = exports.read; | ||
exports.read = function(path, options){ | ||
if(path instanceof File){ | ||
var args = arguments; | ||
var args = arguments; | ||
return when(path.fd, function(fd){ | ||
@@ -126,7 +128,7 @@ args[0] = fd; | ||
var nodeWrite = exports.write; | ||
var nodeWrite = exports.write; | ||
exports.write = function(path, contents, options, encoding){ | ||
if(path instanceof File){ | ||
var id = Math.random(); | ||
var args = arguments; | ||
var args = arguments; | ||
return when(path.fd, function(fd){ | ||
@@ -143,6 +145,6 @@ args[0] = fd; | ||
}; | ||
var nodeClose = exports.close; | ||
var nodeClose = exports.close; | ||
exports.close = function(file){ | ||
if(file instanceof File){ | ||
var args = arguments; | ||
var args = arguments; | ||
return when(file.fd, function(fd){ | ||
@@ -149,0 +151,0 @@ args[0] = fd; |
@@ -11,3 +11,3 @@ /** | ||
request; | ||
if(typeof XMLHttpRequest === "undefined"){ | ||
@@ -23,4 +23,4 @@ request = require("./engines/node/http-client").request; | ||
xhr = new XMLHttpRequest(); | ||
xhr.open(request.method || "GET", | ||
request.url || // allow request.url to shortcut creating a URL from all the various parts | ||
xhr.open(request.method || "GET", | ||
request.url || // allow request.url to shortcut creating a URL from all the various parts | ||
(scheme + "://" + serverName + ":" + serverPort + request.pathInfo + (request.queryString ? '?' + request.queryString : '')), true); | ||
@@ -103,3 +103,3 @@ for(var i in request.headers){ | ||
var domainToCookies = {}; | ||
return function(req) { | ||
@@ -109,3 +109,3 @@ var | ||
parseUri = require("./util/uri").parseUri; | ||
if (req.url) { | ||
@@ -118,3 +118,3 @@ var url = parseUri(req.url); | ||
} | ||
if (req.hostname && domainToCookies[req.hostname]) { | ||
@@ -126,3 +126,3 @@ var cookieString = ""; | ||
} | ||
return when(nextApp(req), function(response) { | ||
@@ -132,3 +132,3 @@ var cookies; | ||
var path, domain = req.hostname + (req.port ? ":"+req.port : ""); | ||
if (Array.isArray(response.headers["set-cookie"])) { | ||
@@ -161,3 +161,3 @@ cookies = []; | ||
} | ||
if (cookies) { | ||
@@ -167,3 +167,3 @@ domainToCookies[req.hostname] = cookies; | ||
} | ||
return response; | ||
@@ -190,3 +190,3 @@ }); | ||
} | ||
var finalRequest = this.request; | ||
@@ -199,2 +199,2 @@ this.request = function(options) { | ||
return request; | ||
}); | ||
}); |
@@ -15,3 +15,3 @@ ({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory.apply(this, deps.map(function(id){return require(id)}))}}). | ||
var exports = LazyArray; | ||
exports.LazyArray = LazyArray; | ||
exports.LazyArray = LazyArray; | ||
exports.first = function(array){ | ||
@@ -39,3 +39,3 @@ return exports.get(array, 0); | ||
var testProto2 = {a:"b"}; | ||
testProto.__proto__ = testProto2; | ||
testProto.__proto__ = testProto2; | ||
var mutableProto = testProto.a == "b"; | ||
@@ -42,0 +42,0 @@ function SomeWrapper(hasSomeAndLength){ |
32
oauth.js
@@ -41,7 +41,7 @@ var parseUrl = require("url").parse; | ||
this.headers = headers || Client.Headers; | ||
if(this.signatureMethod != "PLAINTEXT" && this.signatureMethod != "HMAC-SHA1"){ | ||
throw new Error("Unsupported signature method: " + this.signatureMethod); | ||
} | ||
// We don't store the secrets on the instance itself, that way it can | ||
@@ -55,3 +55,3 @@ // be passed to other actors without leaking | ||
} | ||
var key = secret + "&" + tokenSecret; | ||
@@ -80,7 +80,7 @@ if(signatureMethod == "PLAINTEXT"){ | ||
var chars = Client.NonceChars.split(""); | ||
return function nonceGenerator(){ | ||
return nonce.map(getRandomChar).join(""); | ||
}; | ||
function getRandomChar(){ | ||
@@ -126,3 +126,3 @@ return chars[Math.floor(Math.random() * chars.length)]; | ||
} | ||
// Normalize the request. `engines/node/http-client.request` is | ||
@@ -153,3 +153,3 @@ // quite flexible, but this should do it. | ||
request.headers.host = request.headers.host || request.hostname + (request.port ? ":" + request.port : ""); | ||
// Parse all request parameters into a flattened array of parameter pairs. | ||
@@ -181,3 +181,3 @@ // Note that this array contains munged parameter names. | ||
}, ""); | ||
// Depending on the request content type, look for request parameters in the body | ||
@@ -191,3 +191,3 @@ var waitForBody = false; | ||
} | ||
// If we're a POST or PUT and are not sending any content, or are sending urlencoded content, | ||
@@ -204,3 +204,3 @@ // add the `request.request` to the request body. If we are sending non-urlencoded content through | ||
} | ||
// Sign the request and then actually make it. | ||
@@ -281,14 +281,14 @@ return whenPromise(waitForBody, function(){ | ||
var baseUri = this._normalizeUrl(request); | ||
// Register OAuth parameters and add to the request parameters | ||
// Additional parameters can be specified via the `request.oauthParams` object | ||
var oauthParams = this._collectOAuthParams(request, requestParams); | ||
// Generate parameter string | ||
var params = this._normalizeParams(requestParams); | ||
// Sign the base string | ||
var baseString = this._createSignatureBase(request.method, baseUri, params); | ||
oauthParams.oauth_signature = this._createSignature(baseString); | ||
// Add Authorization header | ||
@@ -298,3 +298,3 @@ request.headers.authorization = "OAuth " + Object.keys(oauthParams).map(function(name){ | ||
}).join(","); | ||
// Now the request object can be used to make a signed request | ||
@@ -309,3 +309,3 @@ return request; | ||
} | ||
return this.request({ | ||
@@ -312,0 +312,0 @@ method: "POST", |
/** | ||
* AOP style event handling, for listening for method calls. Very similar to dojo.connect | ||
* AOP style event handling, for listening for method calls. Very similar to dojo.connect | ||
@@ -92,4 +92,4 @@ /* Add a listener for the execution of the given function slot on the given object. | ||
} | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; |
{ | ||
"name": "promised-io", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"author": { | ||
@@ -40,5 +40,8 @@ "name": "Kris Zyp" | ||
"devDependencies":{ | ||
"patr": ">0.2.6", | ||
"vows": ">=0.5.10" | ||
"patr": ">=0.2.6", | ||
"promises-aplus-tests": "*" | ||
}, | ||
"scripts": { | ||
"test": "node tests/" | ||
} | ||
} |
@@ -10,3 +10,3 @@ ({define:typeof define!="undefined"?define:function(factory){factory(require,exports)}}). | ||
exports.args = process.argv; | ||
exports.env = process.env; | ||
exports.env = process.env; | ||
exports.print = console.log; | ||
@@ -24,2 +24,2 @@ exports.dir = console.dir; | ||
} | ||
}); | ||
}); |
@@ -138,4 +138,4 @@ (function(define){ | ||
exports.Promise = exports.Deferred = exports.defer = defer; | ||
function defer(){ | ||
return new Deferred(); | ||
function defer(canceller){ | ||
return new Deferred(canceller); | ||
} | ||
@@ -643,3 +643,42 @@ | ||
function isGeneratorFunction(obj){ | ||
return obj && obj.constructor && 'GeneratorFunction' == obj.constructor.name; | ||
} | ||
/** | ||
* Promise-based coroutine trampoline | ||
* Adapted from https://github.com/deanlandolt/copromise/blob/master/copromise.js | ||
*/ | ||
function run(coroutine){ | ||
var deferred = defer(); | ||
(function next(value, exception) { | ||
var result; | ||
try { | ||
result = exception ? coroutine.throw(value) : coroutine.next(value); | ||
} | ||
catch (error) { | ||
return deferred.reject(error); | ||
} | ||
if (result.done) return deferred.resolve(result.value); | ||
exports.when(result.value, next, function(error) { | ||
next(error, true); | ||
}); | ||
})(); | ||
return deferred.promise; | ||
}; | ||
/** | ||
* Creates a task from a coroutine, provided as generator. The `yield` function can be provided | ||
* a promise (or any value) to wait on, and the value will be provided when the promise resolves. | ||
* @param coroutine generator or generator function to treat as a coroutine | ||
* @return promise for the return value from the coroutine | ||
*/ | ||
exports.spawn = function(coroutine){ | ||
if (isGeneratorFunction(coroutine)) { | ||
coroutine = coroutine(); | ||
} | ||
return run(coroutine); | ||
} | ||
/** | ||
* Converts a Node async function to a promise returning function | ||
@@ -646,0 +685,0 @@ * @param func node compatible async function which takes a callback as its last argument |
@@ -128,2 +128,2 @@ // Query String Utilities | ||
return typeof(thing) === "string"; | ||
}; | ||
}; |
Promised-IO is a cross-platform package for asynchronous promise-based IO. Promises | ||
provide a simple robust mechanism asynchronicity with separation of concerns by encapsulating | ||
provide a simple robust mechanism for asynchronicity with separation of concerns by encapsulating | ||
eventual completion of an operation with side effect free callback registration | ||
separate from call invocation. Promised-IO provides cross-platform | ||
separate from call invocation. Promised-IO provides cross-platform | ||
file, HTTP, and system interaction with promises for asynchronous operations. | ||
Promised-IO also utilizes "lazy arrays" for progressively completed | ||
Promised-IO also utilizes "lazy arrays" for progressively completed | ||
actions or for streaming of data. Lazy arrays provide all the standard iterative Array methods for | ||
@@ -22,3 +22,3 @@ receiving callbacks as actions are completed. Lazy arrays are utilized | ||
with promises. The promise API used by promised-io is the [Promises/A](http://wiki.commonjs.org/wiki/Promises/A) | ||
proposal used by Dojo, jQuery, and other toolkits. Within promised-io, a promise is | ||
proposal used by Dojo, jQuery, and other toolkits. Within promised-io, a promise is | ||
defined as any object that implements the Promises/A API, that is they provide a | ||
@@ -34,7 +34,7 @@ then() method that can take a callback. The then() methods definition is: | ||
when = require("promised-io/promise"); | ||
when = require("promised-io/promise").when; | ||
when(promiseOrValue, fulfilledHandler, errorHandler); | ||
You can pass a promise to the when() function and the fulfillment and error handlers will be registered for it's | ||
completion *or* you can pass a regular value, and the fulfillment handler will be | ||
completion *or* you can pass a regular value, and the fulfillment handler will be | ||
immediately be called. The when function is a staple of working with promises because | ||
@@ -51,3 +51,3 @@ it allows you to write code that normalizes interaction with synchronous values and asynchronous promises. | ||
The Deferred constructor is the primary mechanism for creating new promises. The Deferred | ||
object is a form of a promise that with an interface for fulfilling or rejecting the promise. | ||
object is a form of a promise with an interface for fulfilling or rejecting the promise. | ||
A Deferred object is a means for a producer to resolve a promise and it also provides | ||
@@ -62,4 +62,4 @@ a promise for consumers that are listening for the resolution of the promise. The basic | ||
setTimeout(function(){ | ||
// fulfill the deferred/promise, all listeners to the promise will be notified, and | ||
// provided the value as the value of the promise | ||
// fulfill the deferred/promise, all listeners to the promise will be notified, and | ||
// provided the value as the value of the promise | ||
deferred.resolve(value); | ||
@@ -71,4 +71,4 @@ }, ms); | ||
The Deferred can optional take a canceler function. This function will cause resulting | ||
promises to have a cancel() method, and if the cancel() method is called, the | ||
The Deferred can optionally take a canceler function. This function will cause resulting | ||
promises to have a cancel() method, and if the cancel() method is called, the | ||
Deferred will be canceled and the canceler function will be called. | ||
@@ -94,3 +94,3 @@ | ||
This is the promise object associated with the Deferred instance. The promise object | ||
This is the promise object associated with the Deferred instance. The promise object | ||
will not have any of the Deferred's fulfill or reject methods, and only provides an interface | ||
@@ -104,3 +104,3 @@ for listening. This can be safely provided to consumers without any chance of being modified. | ||
This will cancel the Deferred. | ||
## currentContext | ||
@@ -111,7 +111,7 @@ | ||
asynchronous actions, without having to actually pass the state to each function in | ||
the asynchronous chain. A common examples of such contextual state would be tracking | ||
the current transaction, or the currently logged in user. Such state information could be | ||
the asynchronous chain. Common examples of such contextual state would be tracking | ||
the current transaction or the currently logged in user. Such state information could be | ||
stored in a singleton (a module property or a global variable), but with asynchronous | ||
actions being interleaved, this is unsuitable for tracking state across asynchronous continuations | ||
of an action. | ||
of an action. | ||
@@ -121,3 +121,3 @@ The promised-io package's promise module provides a facility for tracking state across | ||
and whatever value that was in the variable at the time a promise was created | ||
will be restored when that promise is fulfilled (or rejected). | ||
will be restored when that promise is fulfilled (or rejected). | ||
@@ -132,3 +132,3 @@ ## all | ||
for the completion ("join") of all the actions. For example: | ||
group = all(promise1, promise2, promise3); | ||
@@ -147,4 +147,4 @@ group.then(function(array){ | ||
arguments, and first() will return a new promise that represents the completed value when the first promise | ||
is fulfilled. This allows you to run multiple asynchronous actions get the first result. For example: | ||
is fulfilled. This allows you to run multiple asynchronous actions and get the first result. For example: | ||
response = first(requestToMainSite, requestToMirrorSite1, requestToMirrorSite2); | ||
@@ -166,3 +166,3 @@ response.then(function(response){ | ||
resultPromise = require("promised-io/promise").whenPromise(valueOrPromise, fulfillmentHandler, errorHandler); | ||
resultPromise = require("promised-io/promise").whenPromise(valueOrPromise, fulfillmentHandler, errorHandler); | ||
@@ -179,6 +179,6 @@ The whenPromise() function behaves exactly like when() except that whenPromise | ||
# fs | ||
This module provides promise-based access to the filesystem. The API of the fs module | ||
basically follows the [Node File System module API](http://nodejs.org/api/fs.html). | ||
Each of the asynchronous functions in the Node's FS API is reflected with a corresponding | ||
Each of the asynchronous functions in the Node's FS API is reflected with a corresponding | ||
function in the fs module that returns a promise (instead of requiring a callback argument in the initial call). | ||
@@ -188,7 +188,16 @@ For example, where Node has fs.rename(path1, path2, [callback]), with promised-io | ||
var fs = require("promised-io/fs").fs; | ||
var fs = require("promised-io/fs"); | ||
fs.rename(path1, path2).then(function(){ | ||
// finished renaming | ||
}); | ||
}); | ||
Any callback arguments will be the same minus the error argument: | ||
var fs = require("promised-io/fs"); | ||
fs.readdir(path).then(function(files){ | ||
// use the result | ||
}, function(error) { | ||
// Handle errors here instead | ||
}); | ||
One function that does differ from NodeJS's fs module is the open() function. | ||
@@ -215,5 +224,5 @@ | ||
The file object is also a lazy array, which means you can read from the file using | ||
The file object is also a lazy array, which means you can read from the file using | ||
standard array methods. To asynchronously read the contents of a file, you can do: | ||
file.forEach(function(chunk){ | ||
@@ -231,3 +240,3 @@ // called for each chunk of the file until the end of the file is reached. | ||
Typically you don't need to directly use this module, rather other IO modules like the | ||
Typically you don't need to directly use this module, rather other IO modules like the | ||
file system (fs) and HTTP (http-client) modules provide lazy arrays that you can interact | ||
@@ -249,4 +258,4 @@ with. For example, we could search through a file for the string "lazy" and stop reading | ||
the returned lazy array. This means that in order for this function to be excuted, the | ||
resulting array should have a forEach (or any of the other standard methods) called on | ||
it to trigger the request for data. These methods follow this behavior: | ||
resulting array should have a forEach (or any of the other standard methods) called on | ||
it to trigger the request for data. These methods follow this behavior: | ||
@@ -283,3 +292,3 @@ * filter | ||
first = require("promised-io/lazy-array").first(lazyArray); | ||
This function returns the first element in a lazy array. | ||
@@ -290,3 +299,3 @@ | ||
last = require("promised-io/lazy-array").last(lazyArray); | ||
This function returns the last element in a lazy array. | ||
@@ -297,5 +306,5 @@ | ||
item = require("promised-io/lazy-array").get(index); | ||
This function returns the an element by index from a lazy array. | ||
This function returns an element by index from a lazy array. | ||
# http-client | ||
@@ -307,2 +316,2 @@ | ||
AFL or BSD license. The Persevere project is administered under the Dojo foundation, | ||
and all contributions require a Dojo CLA. | ||
and all contributions require a Dojo CLA. |
@@ -1,104 +0,114 @@ | ||
var vows = require("vows"), | ||
assert = require("assert"), | ||
oauth = require("../oauth"), | ||
querystring = require("../util/querystring"); | ||
var assert = require("assert"); | ||
var oauth = require("../oauth"); | ||
var querystring = require("../util/querystring"); | ||
vows.describe("OAuth").addBatch({ | ||
"When generating the signature base string described in <http://oauth.net/core/1.0/#sig_base_example>": { | ||
topic: new oauth.Client, | ||
"We get the expected result string": function(client){ | ||
var result = client._createSignatureBase("GET", "http://photos.example.net/photos", | ||
"file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original"); | ||
assert.equal(result, "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"); | ||
} | ||
exports.testGeneratingSignatureBaseString = function(){ | ||
// base string described in <http://oauth.net/core/1.0/#sig_base_example> | ||
var client = new oauth.Client; | ||
var result = client._createSignatureBase("GET", "http://photos.example.net/photos", | ||
"file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original"); | ||
assert.equal(result, "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"); | ||
}; | ||
exports.testNormalizingUrl = function(){ | ||
var client = new oauth.Client; | ||
// default ports should be stripped | ||
assert.equal(client._normalizeUrl({ protocol: "https:", hostname: "somehost.com", port: "443", pathname: "/foo/bar" }), "https://somehost.com/foo/bar"); | ||
assert.equal(client._normalizeUrl({ protocol: "http:", hostname: "somehost.com", port: "80", pathname: "/foo/bar" }), "http://somehost.com/foo/bar"); | ||
// should leave non-default ports from URLs for use in signature generation | ||
assert.equal(client._normalizeUrl({ protocol: "https:", hostname: "somehost.com", port: "446", pathname: "/foo/bar" }), "https://somehost.com:446/foo/bar"); | ||
assert.equal(client._normalizeUrl({ protocol: "http:", hostname: "somehost.com", port: "81", pathname: "/foo/bar" }), "http://somehost.com:81/foo/bar"); | ||
}; | ||
exports.testNormalizingRequestParams = function(){ | ||
var client = new oauth.Client; | ||
// ordered by name | ||
assert.equal(client._normalizeParams(["z", "a", "a", "b", "1", "c"]), "1=c&a=b&z=a"); | ||
// if two parameter names are the same then order by the value | ||
assert.equal(client._normalizeParams(["z", "b", "z", "a", "1", "c"]), "1=c&z=a&z=b"); | ||
// resulting parameters should be encoded and ordered as per <http://tools.ietf.org/html/rfc5849#section-3.1> (3.4.1.3.2) | ||
var requestParams = []; | ||
querystring.parseToArray(requestParams, querystring.stringify({ | ||
"b5" : "=%3D", | ||
"c@": "", | ||
"a2": "r b", | ||
"oauth_consumer_key": "9djdj82h48djs9d2", | ||
"oauth_token":"kkk9d7dh3k39sjv7", | ||
"oauth_signature_method": "HMAC-SHA1", | ||
"oauth_timestamp": "137131201", | ||
"oauth_nonce": "7d8f3e4a", | ||
"c2" : "" | ||
})); | ||
querystring.addToArray(requestParams, "a3", "a"); | ||
querystring.addToArray(requestParams, "a3", "2 q"); | ||
assert.equal(client._normalizeParams(requestParams), "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"); | ||
}; | ||
function generateClient() { | ||
var client = new oauth.Client("consumerkey", "consumersecret", null, null, null, "1.0", null, function(){ return "ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp"; }); | ||
oauth.Client.getTimestamp = function(){ return "1272399856"; }; | ||
return client; | ||
} | ||
exports.testSigningUrl = { | ||
"test without token": function(){ | ||
var client = generateClient(); | ||
// provide a valid signature when no token is present | ||
var requestParams = ["bar", "foo"]; | ||
var oauthParams = client._collectOAuthParams({}, requestParams); | ||
var params = client._normalizeParams(requestParams); | ||
var baseString = client._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params); | ||
var signature = client._createSignature(baseString); | ||
assert.equal(signature, "7ytO8vPSLut2GzHjU9pn1SV9xjc="); | ||
}, | ||
"When normalising a URL": { | ||
topic: new oauth.Client, | ||
"Default ports should be stripped": function(client){ | ||
assert.equal(client._normalizeUrl({ protocol: "https:", hostname: "somehost.com", port: "443", pathname: "/foo/bar" }), "https://somehost.com/foo/bar"); | ||
assert.equal(client._normalizeUrl({ protocol: "http:", hostname: "somehost.com", port: "80", pathname: "/foo/bar" }), "http://somehost.com/foo/bar"); | ||
}, | ||
"should leave in non-default ports from URLs for use in signature generation": function(client){ | ||
assert.equal(client._normalizeUrl({ protocol: "https:", hostname: "somehost.com", port: "446", pathname: "/foo/bar" }), "https://somehost.com:446/foo/bar"); | ||
assert.equal(client._normalizeUrl({ protocol: "http:", hostname: "somehost.com", port: "81", pathname: "/foo/bar" }), "http://somehost.com:81/foo/bar"); | ||
} | ||
"test with token": function(){ | ||
var client = generateClient(); | ||
// provide a valid signature when a token is present | ||
var bound = client.bind("token", ""); | ||
var requestParams = ["bar", "foo"]; | ||
var oauthParams = bound._collectOAuthParams({}, requestParams); | ||
var params = bound._normalizeParams(requestParams); | ||
var baseString = bound._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params); | ||
var signature = bound._createSignature(baseString); | ||
assert.equal(oauthParams.oauth_token, "token"); | ||
assert.equal(signature, "9LwCuCWw5sURtpMroIolU3YwsdI="); | ||
}, | ||
"When normalizing the request parameters": { | ||
topic: new oauth.Client, | ||
"Order them by name": function(client){ | ||
assert.equal(client._normalizeParams(["z", "a", "a", "b", "1", "c"]), "1=c&a=b&z=a"); | ||
}, | ||
"If two parameter names are the same then order by the value": function(client){ | ||
assert.equal(client._normalizeParams(["z", "b", "z", "a", "1", "c"]), "1=c&z=a&z=b"); | ||
}, | ||
"the resulting parameters should be encoded and ordered as per <http://tools.ietf.org/html/rfc5849#section-3.1> (3.4.1.3.2)": function(client){ | ||
var requestParams = []; | ||
querystring.parseToArray(requestParams, querystring.stringify({ | ||
"b5" : "=%3D", | ||
"c@": "", | ||
"a2": "r b", | ||
"oauth_consumer_key": "9djdj82h48djs9d2", | ||
"oauth_token":"kkk9d7dh3k39sjv7", | ||
"oauth_signature_method": "HMAC-SHA1", | ||
"oauth_timestamp": "137131201", | ||
"oauth_nonce": "7d8f3e4a", | ||
"c2" : "" | ||
})); | ||
querystring.addToArray(requestParams, "a3", "a"); | ||
querystring.addToArray(requestParams, "a3", "2 q"); | ||
assert.equal(client._normalizeParams(requestParams), "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"); | ||
} | ||
}, | ||
"When signing a URL": { | ||
topic: function(){ | ||
var client = new oauth.Client("consumerkey", "consumersecret", null, null, null, "1.0", null, function(){ return "ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp"; }); | ||
oauth.Client.getTimestamp = function(){ return "1272399856"; }; | ||
return client; | ||
}, | ||
"Provide a valid signature when no token is present": function(client){ | ||
var requestParams = ["bar", "foo"]; | ||
var oauthParams = client._collectOAuthParams({}, requestParams); | ||
var params = client._normalizeParams(requestParams); | ||
var baseString = client._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params); | ||
var signature = client._createSignature(baseString); | ||
assert.equal(signature, "7ytO8vPSLut2GzHjU9pn1SV9xjc="); | ||
}, | ||
"Provide a valid signature when a token is present": function(client){ | ||
var bound = client.bind("token", ""); | ||
var requestParams = ["bar", "foo"]; | ||
var oauthParams = bound._collectOAuthParams({}, requestParams); | ||
var params = bound._normalizeParams(requestParams); | ||
var baseString = bound._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params); | ||
var signature = bound._createSignature(baseString); | ||
assert.equal(oauthParams.oauth_token, "token"); | ||
assert.equal(signature, "9LwCuCWw5sURtpMroIolU3YwsdI="); | ||
}, | ||
"Provide a valid signature when a token and a token secret are present": function(client){ | ||
var bound = client.bind("token", "tokensecret"); | ||
var requestParams = ["bar", "foo"]; | ||
var oauthParams = bound._collectOAuthParams({}, requestParams); | ||
var params = bound._normalizeParams(requestParams); | ||
var baseString = bound._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params); | ||
var signature = bound._createSignature(baseString); | ||
assert.equal(signature, "zeOR0Wsm6EG6XSg0Vw/sbpoSib8="); | ||
} | ||
}, | ||
"When building the OAuth Authorization header": { | ||
topic: function(){ | ||
var client = new oauth.Client("consumerkey", "consumersecret", null, null, null, "1.0", null, function(){ return "ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp"; }); | ||
oauth.Client.getTimestamp = function(){ return "1272399856"; }; | ||
return client; | ||
}, | ||
"All provided OAuth arguments should be concatenated correctly" : function(client){ | ||
var request = client._signRequest({ | ||
method: "GET", | ||
protocol: "http:", | ||
hostname: "somehost.com", | ||
port: "3323", | ||
pathname: "/foo/poop", | ||
headers: {} | ||
}, ["bar", "foo"]); | ||
assert.equal(request.headers.authorization, 'OAuth oauth_consumer_key="consumerkey",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1272399856",oauth_nonce="ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp",oauth_version="1.0",oauth_signature="7ytO8vPSLut2GzHjU9pn1SV9xjc%3D"'); | ||
} | ||
"test with token and secret": function(){ | ||
var client = generateClient(); | ||
// provide a valid signature when a token and a token secret are present | ||
var bound = client.bind("token", "tokensecret"); | ||
var requestParams = ["bar", "foo"]; | ||
var oauthParams = bound._collectOAuthParams({}, requestParams); | ||
var params = bound._normalizeParams(requestParams); | ||
var baseString = bound._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params); | ||
var signature = bound._createSignature(baseString); | ||
assert.equal(signature, "zeOR0Wsm6EG6XSg0Vw/sbpoSib8="); | ||
} | ||
})["export"](module); | ||
}; | ||
exports.testBuildingAuthHeader = function(){ | ||
var client = generateClient(); | ||
// all provided OAuth arguments should be concatenated correctly | ||
var request = client._signRequest({ | ||
method: "GET", | ||
protocol: "http:", | ||
hostname: "somehost.com", | ||
port: "3323", | ||
pathname: "/foo/poop", | ||
headers: {} | ||
}, ["bar", "foo"]); | ||
assert.equal(request.headers.authorization, 'OAuth oauth_consumer_key="consumerkey",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1272399856",oauth_nonce="ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp",oauth_version="1.0",oauth_signature="7ytO8vPSLut2GzHjU9pn1SV9xjc%3D"'); | ||
}; | ||
if (require.main === module) | ||
require("patr/runner").run(exports); |
@@ -137,3 +137,3 @@ var assert = require("assert"), | ||
if (require.main === module) | ||
require("patr/runner").run(exports); | ||
require("patr/runner").run(exports); | ||
@@ -1,30 +0,26 @@ | ||
var vows = require("vows"), | ||
assert = require("assert"), | ||
querystring = require("../util/querystring"); | ||
var assert = require("assert"); | ||
var querystring = require("../util/querystring"); | ||
vows.describe("util/querystring").addBatch({ | ||
"When munging array parameters": { | ||
topic: ["bar", "baz"], | ||
"Each array value should have its own parameter name": function(arr){ | ||
var requestParams = []; | ||
querystring.addToArray(requestParams, "foo", arr); | ||
assert.deepEqual(requestParams, ["foo[]", "bar", "foo[]", "baz"]); | ||
} | ||
}, | ||
"When munging object parameters": { | ||
topic: { "key": "value" }, | ||
"Each property value should have its own parameter name": function(obj){ | ||
var requestParams = []; | ||
querystring.addToArray(requestParams, "obj", obj); | ||
assert.deepEqual(requestParams, ["obj[key]", "value"]); | ||
} | ||
}, | ||
"When parsing a munged query string": { | ||
topic: "foo[]=bar&foo[]=baz&obj[key]=value", | ||
"Arrays and objects should retain their own parameter names": function(qs){ | ||
var requestParams = []; | ||
querystring.parseToArray(requestParams, qs); | ||
assert.deepEqual(requestParams, ["foo[]", "bar", "foo[]", "baz", "obj[key]", "value"]); | ||
} | ||
} | ||
})["export"](module); | ||
exports.testMungingArrayParams = function(){ | ||
var arr = ["bar", "baz"]; | ||
var requestParams = []; | ||
querystring.addToArray(requestParams, "foo", arr); | ||
assert.deepEqual(requestParams, ["foo[]", "bar", "foo[]", "baz"]); | ||
}; | ||
exports.testMungingObjectParams = function(){ | ||
var obj = { "key": "value" }; | ||
var requestParams = []; | ||
querystring.addToArray(requestParams, "obj", obj); | ||
assert.deepEqual(requestParams, ["obj[key]", "value"]); | ||
}; | ||
exports.testParsingMungedQuerystring = function(){ | ||
var qs = "foo=bar&foo=baz&obj[key]=value"; | ||
var requestParams = []; | ||
querystring.parseToArray(requestParams, qs); | ||
assert.deepEqual(requestParams, ["foo[]", "bar", "foo[]", "baz", "obj[key]", "value"]); | ||
}; | ||
if (require.main === module) | ||
require("patr/runner").run(exports); |
@@ -21,3 +21,3 @@ var querystring = require("querystring"); | ||
} | ||
switch(type(value)){ | ||
@@ -24,0 +24,0 @@ case "[object String]": |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93216
27
2515
297
4