Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

jsonp

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonp - npm Package Compare versions

Comparing version
0.0.2
to
0.0.3
+84
index.js
/**
* 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);
};
# 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"
}
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 @@ ==================

{
"name": "jsonp",
"description": "A sane JSONP implementation.",
"version": "0.0.2",
"version": "0.0.3",
"dependencies": {

@@ -6,0 +6,0 @@ "debug": "*"

/**
* 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);
};