+84
| /** | ||
| * Module dependencies | ||
| */ | ||
| var debug = require('debug')('jsonp'); | ||
| /** | ||
| * Module exports. | ||
| */ | ||
| module.exports = jsonp; | ||
| /** | ||
| * Callback index. | ||
| */ | ||
| var count = 0; | ||
| /** | ||
| * Noop function. | ||
| */ | ||
| function noop(){}; | ||
| /** | ||
| * JSONP handler | ||
| * | ||
| * Options: | ||
| * - param {String} qs parameter (`callback`) | ||
| * - timeout {Number} how long after a timeout error is emitted (`60000`) | ||
| * | ||
| * @param {String} url | ||
| * @param {Object|Function} optional options / callback | ||
| * @param {Function} optional callback | ||
| */ | ||
| function jsonp(url, opts, fn){ | ||
| if ('function' == typeof opts) { | ||
| fn = opts; | ||
| opts = {}; | ||
| } | ||
| var opts = opts || {}; | ||
| var param = opts.param || 'callback'; | ||
| var timeout = null != opts.timeout ? opts.timeout : 60000; | ||
| var enc = encodeURIComponent; | ||
| var target = document.getElementsByTagName('script')[0]; | ||
| var script; | ||
| var timer; | ||
| // generate a unique id for this request | ||
| var id = count++; | ||
| if (timeout) { | ||
| timer = setTimeout(function(){ | ||
| cleanup(); | ||
| fn && fn(new Error('Timeout')); | ||
| }, timeout); | ||
| } | ||
| function cleanup(){ | ||
| target.parentNode.removeChild(script); | ||
| window['__jp' + id] = noop; | ||
| } | ||
| window['__jp' + id] = function(data){ | ||
| debug('jsonp got', data); | ||
| if (timer) clearTimeout(timer); | ||
| cleanup(); | ||
| fn && fn(null, data); | ||
| }; | ||
| // add qs component | ||
| url += (~url.indexOf('?') ? '&' : '?') + param + '=' + enc('__jp' + id + ''); | ||
| url = url.replace('?&', '?'); | ||
| debug('jsonp req "%s"', url); | ||
| // create script | ||
| script = document.createElement('script'); | ||
| script.src = url; | ||
| target.parentNode.insertBefore(script, target); | ||
| }; |
+25
| # jsonp | ||
| A simple JSONP implementation. | ||
| ## API | ||
| ### jsonp(url, opts, fn) | ||
| - `url` (`String`) url to fetch | ||
| - `opts` (`Object`), optional | ||
| - `param` (`String`) name of the query string component to specify | ||
| the callback (defaults to `callback`) | ||
| - `timeout` (`Number`) how long after a timeout error is emitted. `0` to | ||
| disable (defaults to `60000`) | ||
| - `fn` callback | ||
| The callback is called with `err, data` parameters. | ||
| If it times out, the `err` will be an `Error` object whose `message` is | ||
| `Timeout`. | ||
| ## License | ||
| MIT |
+3
-3
@@ -5,8 +5,8 @@ { | ||
| "description": "A sane JSONP implementation.", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "dependencies": { | ||
| "visionmedia/debug": "*" | ||
| }, | ||
| "scripts": ["jsonp.js"], | ||
| "main": "jsonp.js" | ||
| "scripts": ["index.js"], | ||
| "main": "index.js" | ||
| } |
+7
-0
| 0.0.3 / 2013-02-03 | ||
| ================== | ||
| * use `index.js` | ||
| * go back to integer ids | ||
| * honor `param` option | ||
| 0.0.2 / 2013-02-03 | ||
@@ -3,0 +10,0 @@ ================== |
+1
-1
| { | ||
| "name": "jsonp", | ||
| "description": "A sane JSONP implementation.", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "dependencies": { | ||
@@ -6,0 +6,0 @@ "debug": "*" |
-87
| /** | ||
| * Module dependencies | ||
| */ | ||
| var debug = require('debug')('jsonp') | ||
| /** | ||
| * Module exports. | ||
| */ | ||
| module.exports = jsonp; | ||
| /** | ||
| * Callback index. | ||
| */ | ||
| var count = 0; | ||
| /** | ||
| * Noop function. | ||
| */ | ||
| function noop () {}; | ||
| /** | ||
| * JSONP handler | ||
| * | ||
| * Options: | ||
| * - param {String} qs parameter (`callback`) | ||
| * - timeout {Number} how long after a timeout error is emitted (`60000`) | ||
| * | ||
| * @param {String} url | ||
| * @param {Object|Function} optional options / callback | ||
| * @param {Function} optional callback | ||
| */ | ||
| function jsonp (url, opts, fn) { | ||
| if ('function' == typeof opts) { | ||
| fn = opts; | ||
| opts = {}; | ||
| } | ||
| var opts = opts || {} | ||
| , callback = opts.callback || 'callback' | ||
| , timeout = null != opts.timeout ? opts.timeout : 60000 | ||
| , enc = encodeURIComponent | ||
| , target = document.getElementsByTagName('script')[0] | ||
| , script | ||
| , timer | ||
| // generate a hash of the url | ||
| var id = 0 | ||
| for (var i = 0, l = url.length; i < l; i++) { | ||
| id += url.charCodeAt(i); | ||
| } | ||
| if (timeout) { | ||
| timer = setTimeout(function () { | ||
| cleanup(); | ||
| fn && fn(new Error('Timeout')); | ||
| }, timeout); | ||
| } | ||
| function cleanup () { | ||
| target.parentNode.removeChild(script); | ||
| window['__jp' + id] = noop; | ||
| } | ||
| window['__jp' + id] = function (data) { | ||
| debug('jsonp got', data); | ||
| if (timer) clearTimeout(timer); | ||
| cleanup(); | ||
| fn && fn(null, data); | ||
| }; | ||
| // add qs component | ||
| url += (~url.indexOf('?') ? '&' : '?') + 'callback=' + enc('__jp' + id + ''); | ||
| url = url.replace('?&', '?'); | ||
| debug('jsonp req "%s"', url); | ||
| // create script | ||
| script = document.createElement('script'); | ||
| script.src = url; | ||
| target.parentNode.insertBefore(script, target); | ||
| }; |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
2807
25.42%6
20%1
-50%26
Infinity%77
-3.75%