-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit 4f59db0
Showing
4 changed files
with
293 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Tiago Quelhas | ||
|
||
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. |
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,21 @@ | ||
# corsRequest.js | ||
|
||
A minimal, dependency-free, cross-browser CORS HTTP client. | ||
|
||
Uses XMLHttpRequest2 with a fallback to XDomainRequest. | ||
|
||
### corsRequest(method, url, [data], [done]) | ||
### corsRequest.get(url, [data], [done]) | ||
### corsRequest.post(url, [data], [done]) | ||
|
||
Performs an HTTP GET or POST request to the specified URL. | ||
|
||
The *data* argument populates the request body. If it is an object, it is sent as JSON. If it is a string, it is sent as plain text. Otherwise, an empty request body is sent. | ||
|
||
If supplied, the *done* callback must have signature (err, res). The request is unsuccessful if the browser reports an error or timeout. When the request is successful, the following are set: | ||
|
||
* *res.status*: the response status code (this is always 200 when XDomainRequest is used). | ||
* *res.text*: the raw response body. | ||
* *res.json*: the response body parsed as JSON, if it is valid JSON. | ||
|
||
All functions return an object with properties *method*, *url*, *data* and *req* (the underlying request object). Properties *err* and *res* are set when the request completes. Call *abort()* on the object to abort the request. |
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,32 @@ | ||
{ | ||
"name": "corsRequest", | ||
"version": "0.0.1", | ||
"authors": [ | ||
"Tiago Quelhas <[email protected]>" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/tjgq/cors-request.git" | ||
}, | ||
"description": "A minimal, dependency-free, cross-browser CORS HTTP client", | ||
"main": "corsRequest.js", | ||
"moduleType": [ | ||
"amd", | ||
"globals", | ||
"node" | ||
], | ||
"keywords": [ | ||
"HTTP", | ||
"CORS", | ||
"XMLHttpRequest", | ||
"XDomainRequest" | ||
], | ||
"license": "MIT", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
] | ||
} |
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,219 @@ | ||
(function (root, factory) { | ||
if (typeof exports === 'object') { | ||
// CommonJS | ||
module.exports = factory(); | ||
} else if (typeof define === 'function' && define.amd) { | ||
// AMD | ||
define(['corsRequest'], function() { | ||
return (root.corsRequest = factory()); | ||
}); | ||
} else { | ||
// Global variable | ||
root.corsRequest = factory(); | ||
} | ||
}(this, function() { | ||
|
||
// Determine the underlying implementation to be used. | ||
var useXhr = 'withCredentials' in new XMLHttpRequest(); | ||
var useXdr = !useXhr && typeof XDomainRequest !== 'undefined'; | ||
var Request; | ||
if (useXhr) { | ||
Request = XMLHttpRequest; | ||
} else if (useXdr) { | ||
Request = XDomainRequest; | ||
} else { | ||
throw new Error('corsRequest requires either XMLHttpRequest2 or XDomainRequest'); | ||
} | ||
|
||
|
||
// A function that does nothing. | ||
var noop = function() {}; | ||
|
||
|
||
// Return a function that only calls fn once. | ||
function once(fn) { | ||
fn.called = false; | ||
return function() { | ||
if (!fn.called) { | ||
fn.called = true; | ||
return fn.apply(null, arguments); | ||
} | ||
}; | ||
} | ||
|
||
|
||
// Curry fn on the remaining arguments. | ||
function curry(fn) { | ||
var curriedArgs = [].slice.call(arguments, 1); | ||
return function() { | ||
var remainingArgs = [].slice.call(arguments); | ||
return fn.apply(null, curriedArgs.concat(remainingArgs)); | ||
}; | ||
} | ||
|
||
|
||
// Encode object as JSON, returning undefined if unsuccessful. | ||
function toJSON(obj) { | ||
try { | ||
return JSON.stringify(obj, null, 0); | ||
} catch (e) { | ||
return; | ||
} | ||
} | ||
|
||
|
||
// Parse string as JSON, returning undefined if unsuccessful. | ||
function fromJSON(str) { | ||
try { | ||
return JSON.parse(str); | ||
} catch (e) { | ||
return; | ||
} | ||
} | ||
|
||
|
||
// Return whether o is a non-null object. | ||
function isObject(obj) { | ||
return typeof obj === 'object' && obj !== null; | ||
} | ||
|
||
|
||
// Encode data to JSON representation or string, according to its type. | ||
function encode(data) { | ||
if (isObject(data)) { | ||
return toJSON(data); | ||
} else { | ||
return data.toString(); | ||
} | ||
} | ||
|
||
|
||
// Perform and store state for an HTTP request. | ||
function CorsRequest(method, url, data, done) { | ||
|
||
var self = this; | ||
|
||
done = once(done); | ||
|
||
function error(msg) { | ||
return new Error('corsRequest: ' + method + ' ' + url + ': ' + msg); | ||
} | ||
|
||
this.method = method; | ||
this.url = url; | ||
this.data = data; | ||
|
||
this.req = new Request(); | ||
|
||
this.req.open(this.method, this.url); | ||
|
||
this.req.ontimeout = function() { | ||
self.err = error('timeout'); | ||
done(self.err); | ||
}; | ||
|
||
this.req.onerror = function() { | ||
self.err = error('error'); | ||
done(self.err); | ||
}; | ||
|
||
this.req.onload = function() { | ||
if (useXhr && self.req.readyState !== 4) { | ||
// XMLHttpRequest not yet complete. | ||
return; | ||
} | ||
if (useXhr && self.req.status === 0) { | ||
// XMLHttpRequest aborted, e.g. CORS error or connection reset. | ||
self.err = error('incomplete'); | ||
done(self.err); | ||
return; | ||
} | ||
// Request complete. | ||
self.err = null; | ||
self.res = { | ||
status: useXhr ? self.req.status : 200, | ||
text: self.req.responseText, | ||
json: fromJSON(self.req.responseText) | ||
}; | ||
done(self.err, self.res); | ||
}; | ||
|
||
// Setting this handler seems to increase the probability | ||
// of the request being sucessful on old IE versions. | ||
if (useXdr) { | ||
this.req.onprogress = noop; | ||
} | ||
|
||
this.abort = function() { | ||
if (self.err === void 0) { | ||
self.req.abort(); | ||
self.err = error('aborted'); | ||
done(self.err); | ||
} | ||
}; | ||
|
||
if (this.data !== void 0) { | ||
var payload = encode(data); | ||
} | ||
|
||
// Calling send() right away sometimes causes the request | ||
// to fail on old IE versions. | ||
setTimeout(function() { | ||
if (self.err === void 0) { | ||
self.req.send(payload || null); | ||
} | ||
}, 0); | ||
} | ||
|
||
|
||
// Instantiate CorsRequest with default arguments. | ||
function corsRequest() { | ||
|
||
var method, url, data, done; | ||
|
||
if (arguments.length < 2) { | ||
throw new Error('corsRequest: missing argument'); | ||
} | ||
|
||
// corsRequest(method, url, ...) | ||
method = arguments[0].toUpperCase(); | ||
url = arguments[1]; | ||
|
||
if (method !== 'GET' && method !== 'POST') { | ||
throw new Error('corsRequest: bad method'); | ||
} | ||
|
||
if (arguments.length === 3) { | ||
if (typeof arguments[2] === 'function') { | ||
// corsRequest(method, url, done) | ||
done = arguments[2]; | ||
} else { | ||
// corsRequest(method, url, data) | ||
data = arguments[2]; | ||
} | ||
} | ||
|
||
if (arguments.length > 3) { | ||
// corsRequest(method, url, data, done) | ||
data = arguments[2]; | ||
done = arguments[3]; | ||
} | ||
|
||
return new CorsRequest(method, url, data, done || noop); | ||
} | ||
|
||
|
||
// Report implementation in use. | ||
corsRequest.usingXhr = useXhr; | ||
corsRequest.usingXdr = useXdr; | ||
|
||
|
||
// Utility functions for individual methods. | ||
corsRequest.get = curry(corsRequest, 'GET'); | ||
corsRequest.post = curry(corsRequest, 'POST'); | ||
|
||
|
||
// Export public interface. | ||
return corsRequest; | ||
|
||
})); |