Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-jsonp

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-jsonp - npm Package Compare versions

Comparing version 1.1.2 to 1.1.3

10

CHANGELOG.md

@@ -0,1 +1,11 @@

<a name="1.1.2"></a>
## [1.1.2](https://github.com/lbwa/jsonp/compare/v1.1.1...v1.1.2) (2018-08-17)
### Bug Fixes
* **core:** remove unnecessary code for reduce code weight ([90bb90a](https://github.com/lbwa/jsonp/commit/90bb90a))
<a name="1.1.1"></a>

@@ -2,0 +12,0 @@ ## [1.1.1](https://github.com/lbwa/jsonp/compare/v1.1.0...v1.1.1) (2018-08-17)

40

dist/better-jsonp.js
/*!
* better-jsonp v1.1.2 Copyrights (c) 2018 Bowen (lbwa)
* better-jsonp v1.1.3 Copyrights (c) 2020 Bowen (lbwa)
* Released under the MIT License.

@@ -12,5 +12,2 @@ */

function noop() {}
function euc(value) {
return encodeURIComponent(value);
}

@@ -33,6 +30,2 @@ var PREFIX = 'callback';

});
this.initState({
timeout: timeout,
jsonpCallback: jsonpCallback
});
this.encodeURL({

@@ -43,2 +36,6 @@ url: url,

});
this.initState({
timeout: timeout,
jsonpCallback: jsonpCallback
});
this.insert(this._url);

@@ -53,3 +50,3 @@ }

// This checking only works in CDN installing, not as a dependency using
if (jsonpCallback === 'jsonp') throw new Error('Don\'t name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value');
if (jsonpCallback === 'jsonp') throw new Error("Don't name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value");
};

@@ -81,3 +78,8 @@ Jsonp.prototype.initState = function (_a) {

_this.cleanScript();
reject(new Error("Countdown has been clear! JSONP request unsuccessfully due to 404/500"));
// clear timer
if (_this._timer) {
window.clearTimeout(_this._timer);
_this._timer = null;
}
reject(new Error("[SCRIPT ONERROR]: JSONP request unsuccessfully due to 404/500"));
};

@@ -93,9 +95,11 @@ window[_this._jsonpCallback] = function (data) {

var _this = this;
// It can be disable when param timeout equal falsy value (0, null etc.)
if (timeout) {
this._timer = window.setTimeout(function () {
var id = window.setTimeout(function () {
window[_this._jsonpCallback] = noop;
_this.cleanScript();
_this._timer = null;
_this.cleanScript();
throw new Error('JSONP request unsuccessfully (eg.timeout or wrong url).');
throw new Error('[TIMEOUT]: JSONP request unsuccessfully (eg.timeout or wrong url).');
}, timeout);
this._timer = id;
}

@@ -109,3 +113,3 @@ };

// eg. ?callback=...
var id = euc(this._jsonpCallback);
var id = encodeURIComponent(this._jsonpCallback);
url += "" + (url.indexOf('?') < 0 ? '?' : '&') + callbackParams + "=" + id;

@@ -116,3 +120,3 @@ // add other parameters to url ending excluding callback name parameter

var value = urlParams[key] !== undefined ? urlParams[key] : '';
url += "&" + key + "=" + euc(value);
url += "&" + key + "=" + encodeURIComponent(value);
});

@@ -128,9 +132,7 @@ // converted request url

Jsonp.prototype.cleanScript = function () {
if (this._script.parentNode) {
this._reference.parentNode.removeChild(this._script);
if (this._script && this._script.parentNode) {
this._script.parentNode.removeChild(this._script);
this._script = null;
}
// reset response handler
window[this._jsonpCallback] = noop;
if (this._timer) window.clearTimeout(this._timer);
};

@@ -137,0 +139,0 @@ return Jsonp;

/*!
* better-jsonp v1.1.2 Copyrights (c) 2018 Bowen (lbwa)
* better-jsonp v1.1.3 Copyrights (c) 2020 Bowen (lbwa)
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.jsonp=t()}(this,function(){"use strict";function e(){}function t(e){return encodeURIComponent(e)}var n="callback",r=function(){function r(e){var t=e.url,r=e.timeout,o=void 0===r?6e3:r,i=e.jsonpCallback,c=void 0===i?""+n+Date.now():i,s=e.callbackParams,a=void 0===s?"jsonpCallback":s,l=e.urlParams,u=void 0===l?{}:l;this.checkOptions({url:t,jsonpCallback:c}),this.initState({timeout:o,jsonpCallback:c}),this.encodeURL({url:t,callbackParams:a,urlParams:u}),this.insert(this._url)}return r.prototype.checkOptions=function(e){var t=e.url,n=e.jsonpCallback;if(!t)throw new Error("Please check your request url.");if("jsonp"===n)throw new Error("Don't name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value")},r.prototype.initState=function(e){var t=e.timeout,n=e.jsonpCallback;this.createScript(),this._jsonpCallback=n,this.handler=this.createHandler(),this.createTimer(t)},r.prototype.createScript=function(){this._reference=document.getElementsByTagName("script")[0]||document.body.lastElementChild,this._script=document.createElement("script")},r.prototype.createHandler=function(){var e=this;return new Promise(function(t,n){e._script.onerror=function(){e.cleanScript(),n(new Error("Countdown has been clear! JSONP request unsuccessfully due to 404/500"))},window[e._jsonpCallback]=function(n){e.cleanScript(),t(n)}})},r.prototype.createTimer=function(t){var n=this;t&&(this._timer=window.setTimeout(function(){throw window[n._jsonpCallback]=e,n._timer=null,n.cleanScript(),new Error("JSONP request unsuccessfully (eg.timeout or wrong url).")},t))},r.prototype.encodeURL=function(e){var n=e.url,r=e.callbackParams,o=e.urlParams,i=t(this._jsonpCallback);n+=(n.indexOf("?")<0?"?":"&")+r+"="+i,Object.keys(o).forEach(function(e){var r=void 0!==o[e]?o[e]:"";n+="&"+e+"="+t(r)}),this._url=n},r.prototype.insert=function(e){this._script.src=e,this._reference.parentNode.insertBefore(this._script,this._reference)},r.prototype.cleanScript=function(){this._script.parentNode&&(this._reference.parentNode.removeChild(this._script),this._script=null),window[this._jsonpCallback]=e,this._timer&&window.clearTimeout(this._timer)},r}();return function(e){return new r(e).handler}});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.jsonp=e()}(this,function(){"use strict";function t(){}var e="callback",n=function(){function n(t){var n=t.url,r=t.timeout,o=void 0===r?6e3:r,i=t.jsonpCallback,c=void 0===i?""+e+Date.now():i,s=t.callbackParams,a=void 0===s?"jsonpCallback":s,l=t.urlParams,u=void 0===l?{}:l;this.checkOptions({url:n,jsonpCallback:c}),this.encodeURL({url:n,callbackParams:a,urlParams:u}),this.initState({timeout:o,jsonpCallback:c}),this.insert(this._url)}return n.prototype.checkOptions=function(t){var e=t.url,n=t.jsonpCallback;if(!e)throw new Error("Please check your request url.");if("jsonp"===n)throw new Error("Don't name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value")},n.prototype.initState=function(t){var e=t.timeout,n=t.jsonpCallback;this.createScript(),this._jsonpCallback=n,this.handler=this.createHandler(),this.createTimer(e)},n.prototype.createScript=function(){this._reference=document.getElementsByTagName("script")[0]||document.body.lastElementChild,this._script=document.createElement("script")},n.prototype.createHandler=function(){var t=this;return new Promise(function(e,n){t._script.onerror=function(){t.cleanScript(),t._timer&&(window.clearTimeout(t._timer),t._timer=null),n(new Error("[SCRIPT ONERROR]: JSONP request unsuccessfully due to 404/500"))},window[t._jsonpCallback]=function(n){t.cleanScript(),e(n)}})},n.prototype.createTimer=function(e){var n=this;if(e){var r=window.setTimeout(function(){throw window[n._jsonpCallback]=t,n.cleanScript(),n._timer=null,new Error("[TIMEOUT]: JSONP request unsuccessfully (eg.timeout or wrong url).")},e);this._timer=r}},n.prototype.encodeURL=function(t){var e=t.url,n=t.callbackParams,r=t.urlParams,o=encodeURIComponent(this._jsonpCallback);e+=(e.indexOf("?")<0?"?":"&")+n+"="+o,Object.keys(r).forEach(function(t){var n=void 0!==r[t]?r[t]:"";e+="&"+t+"="+encodeURIComponent(n)}),this._url=e},n.prototype.insert=function(t){this._script.src=t,this._reference.parentNode.insertBefore(this._script,this._reference)},n.prototype.cleanScript=function(){this._script&&this._script.parentNode&&(this._script.parentNode.removeChild(this._script),this._script=null),window[this._jsonpCallback]=t},n}();return function(t){return new n(t).handler}});
import { options } from '../utils/types'
import { noop, euc } from '../utils/index'
import { noop } from '../utils/index'

@@ -13,5 +13,5 @@ const PREFIX = 'callback'

private _script: HTMLScriptElement | null // trigger element
private _timer: number // timer ID
private _timer: number
constructor ({
constructor({
url,

@@ -27,6 +27,2 @@ timeout = 6000,

})
this.initState({
timeout,
jsonpCallback
})
this.encodeURL({

@@ -37,9 +33,10 @@ url,

})
this.initState({
timeout,
jsonpCallback
})
this.insert(this._url)
}
checkOptions ({
url,
jsonpCallback
}: options) {
checkOptions({ url, jsonpCallback }: options) {
if (!url) throw new Error('Please check your request url.')

@@ -51,6 +48,9 @@

// This checking only works in CDN installing, not as a dependency using
if (jsonpCallback === 'jsonp') throw new Error('Don\'t name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value')
if (jsonpCallback === 'jsonp')
throw new Error(
"Don't name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value"
)
}
initState ({
initState({
timeout,

@@ -74,5 +74,6 @@ jsonpCallback

createScript () {
this._reference = document.getElementsByTagName('script')[0]
|| document.body.lastElementChild
createScript() {
this._reference =
document.getElementsByTagName('script')[0] ||
document.body.lastElementChild
this._script = document.createElement('script')

@@ -85,11 +86,19 @@ }

*/
createHandler () {
return new Promise((resolve, reject) => {
createHandler() {
return new Promise<object>((resolve, reject) => {
// handle 404/500 in response
this._script.onerror = () => {
this.cleanScript()
reject(new Error(`Countdown has been clear! JSONP request unsuccessfully due to 404/500`))
// clear timer
if (this._timer) {
window.clearTimeout(this._timer)
this._timer = null
}
reject(
new Error(
`[SCRIPT ONERROR]: JSONP request unsuccessfully due to 404/500`
)
)
}
(<any>window)[this._jsonpCallback] = (data: object) => {
;(window as any)[this._jsonpCallback] = (data: object) => {
this.cleanScript()

@@ -102,21 +111,21 @@ resolve(data)

// create a request timer for limiting request period
createTimer (timeout: options['timeout']) {
createTimer(timeout: options['timeout']) {
// It can be disable when param timeout equal falsy value (0, null etc.)
if (timeout) {
this._timer = window.setTimeout(() => {
(<any>window)[this._jsonpCallback] = noop
const id = window.setTimeout(() => {
;(window as any)[this._jsonpCallback] = noop
this.cleanScript()
this._timer = null
this.cleanScript()
throw new Error('JSONP request unsuccessfully (eg.timeout or wrong url).')
throw new Error(
'[TIMEOUT]: JSONP request unsuccessfully (eg.timeout or wrong url).'
)
}, timeout)
this._timer = id
}
}
encodeURL ({
url,
callbackParams,
urlParams
}: options) {
encodeURL({ url, callbackParams, urlParams }: options) {
// name of query parameter to specify the callback name
// eg. ?callback=...
const id = euc(this._jsonpCallback)
const id = encodeURIComponent(this._jsonpCallback)
url += `${url.indexOf('?') < 0 ? '?' : '&'}${callbackParams}=${id}`

@@ -128,3 +137,3 @@

const value = urlParams[key] !== undefined ? urlParams[key] : ''
url += `&${key}=${euc(value)}`
url += `&${key}=${encodeURIComponent(value)}`
})

@@ -137,3 +146,3 @@

// activate JSONP
insert (url: string) {
insert(url: string) {
this._script.src = url

@@ -143,5 +152,5 @@ this._reference.parentNode.insertBefore(this._script, this._reference)

cleanScript () {
if (this._script.parentNode) {
this._reference.parentNode.removeChild(this._script)
cleanScript() {
if (this._script && this._script.parentNode) {
this._script.parentNode.removeChild(this._script)
this._script = null

@@ -151,5 +160,4 @@ }

// reset response handler
(<any>window)[this._jsonpCallback] = noop
if (this._timer) window.clearTimeout(this._timer)
;(window as any)[this._jsonpCallback] = noop
}
}
{
"name": "better-jsonp",
"version": "1.1.2",
"version": "1.1.3",
"description": "A simple JSONP implementation",

@@ -14,3 +14,3 @@ "main": "dist/better-jsonp.min.js",

"ts": "npx tsc",
"dev": "node samples/server.js",
"dev": "node docs/server.js",
"build": "yarn run cleaner && yarn run build:prod && yarn run build:dev",

@@ -20,6 +20,6 @@ "build:prod": "npx rollup -c scripts/rollup.config.js --environment TARGET:production",

"test": "echo 'Under construction'",
"lint": "npx eslint --fix --ext .js lib/ samples/ scripts/",
"lint": "npx eslint --fix --ext .js lib/ docs/ scripts/",
"release": "sh scripts/release.sh",
"cleaner": "rm -vrf dist/*",
"commit": "yarn run lint && yarn run build && git add . && git-cz",
"commit": "yarn run lint && git add . && git-cz",
"changelog": "npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0"

@@ -26,0 +26,0 @@ },

@@ -5,2 +5,10 @@ # Better JSONP [![npm](https://img.shields.io/npm/v/better-jsonp.svg)](https://www.npmjs.com/package/better-jsonp) [![Build Status](https://travis-ci.org/lbwa/jsonp.svg?branch=master)](https://travis-ci.org/lbwa/jsonp) [![npm](https://img.shields.io/npm/dt/better-jsonp.svg)](https://github.com/lbwa/jsonp)

- [See demo page]
- [Play on the playground]
[See demo page]:https://lbwa.github.io/jsonp/
[Play on the playground]:https://jsfiddle.net/608oagxd/8/
## Features

@@ -65,4 +73,4 @@

url: 'http://localhost',
// global function named `${jsonpCallback}` will be invoked when JSONP response
jsonpCallback: 'jsonpCallback', // any different name from request module
// global function named `callback` will be invoked when JSONP response
jsonpCallback: 'callback', // any different name from request module
timeout: 5000,

@@ -83,4 +91,6 @@ // eg. ?customCallbackParams=...

- You can also invoke function named `jsonp` directly in global environment if you have installed package from CDN.
> For more customization capability, This package wouldn't convert `jsonpCallback` to a new name to prevent unexpected reset.
- You can also invoke function named `jsonp` directly in global environment if you have installed package from CDN.([Play on the playground])
```js

@@ -100,3 +110,3 @@ jsonp({

| `url` | `String` | true | JSONP request address |
| `timeout` | `Number` | false, default : `6000` | how long after timeout error is emitted. `0` to disable |
| `timeout` | `Number` | false, default : `6000` milliseconds | how long after timeout error is emitted. `0` to disable |
| `jsonpCallback` | `String` | false, default : `'callback'+Date.now()` | global callback function name which is used to handle JSONP response. |

@@ -106,3 +116,3 @@ | `callbackParams` | `String` | false, default: `jsonpCallback` | name of query parameter to specify the callback name |

## Notice ⚠️
## Notice

@@ -109,0 +119,0 @@ - `Uncaught SyntaxError: Unexpected token :`error

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc