-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from borodean/promise
Promise support
- Loading branch information
Showing
10 changed files
with
219 additions
and
48 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
*.log | ||
*.sublime-* | ||
borodean-jsonp-*.tgz | ||
dist/jsonp.min.js | ||
dist/jsonp.min.js.map | ||
dist/*.min.js | ||
dist/*.min.js.map | ||
node_modules |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.npmignore | ||
.travis.yml | ||
borodean-jsonp-*.tgz | ||
dist/*-*.*.*.min.* | ||
karma.conf.js | ||
rollup.config.js | ||
test.js | ||
test |
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
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
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
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 |
---|---|---|
@@ -1,19 +1,22 @@ | ||
{ | ||
"name": "@borodean/jsonp", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "The smallest possible JSONP implementation", | ||
"license": "MIT", | ||
"author": "Vadym Borodin <[email protected]> http://borodean.com", | ||
"main": "callback.js", | ||
"repository": "borodean/jsonp", | ||
"scripts": { | ||
"build": "rollup -co dist/jsonp.min.js", | ||
"lint": "xo --env=browser --env=mocha --global expect --global sinon --space", | ||
"build": "rollup -c", | ||
"lint": "xo --env=browser --env=mocha --global expect --global sinon --no-esnext --space", | ||
"test": "karma start", | ||
"test-local": "karma start --local" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^11.0.2", | ||
"browserify": "^16.5.0", | ||
"chai": "*", | ||
"core-js": "^3.6.4", | ||
"karma": "^4.4.1", | ||
"karma-browserify": "^7.0.0", | ||
"karma-chai": "^0.1.0", | ||
|
@@ -22,12 +25,11 @@ | |
"karma-sinon": "^1.0.5", | ||
"lodash": "^4.17.15", | ||
"mocha": "^4.1.0", | ||
"rollup": "^0.41.4", | ||
"rollup-plugin-commonjs": "^7.0.0", | ||
"rollup-plugin-filesize": "^1.0.1", | ||
"rollup-plugin-uglify": "^1.0.1", | ||
"sinon": "^1.17.7", | ||
"rollup": "^1.31.0", | ||
"rollup-plugin-filesize": "^6.2.1", | ||
"rollup-plugin-uglify": "^6.0.4", | ||
"sinon": "^7.5.0", | ||
"watchify": "^3.11.1", | ||
"xo": "^0.17.1" | ||
"xo": "^0.26.0" | ||
} | ||
} |
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,26 @@ | ||
var count = 0; | ||
|
||
module.exports = function (url, options) { | ||
options = options || {}; | ||
|
||
var object = options.object || window; | ||
var key = options.key || 'j' + count++; | ||
var parameter = 'parameter' in options ? options.parameter : 'callback'; | ||
|
||
var script = document.createElement('script'); | ||
script.src = parameter ? (url + (~url.indexOf('?') ? '&' : '?') + parameter + '=' + key) : url; // eslint-disable-line no-implicit-coercion | ||
|
||
return new Promise(function (resolve, reject) { | ||
script.onerror = function () { // eslint-disable-line unicorn/prefer-add-event-listener | ||
delete object[key]; | ||
reject(new Error()); | ||
}; | ||
|
||
object[key] = function (response) { | ||
delete object[key]; | ||
resolve(response); | ||
}; | ||
|
||
document.head.removeChild(document.head.appendChild(script)); // eslint-disable-line unicorn/prefer-node-append | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,19 +1,40 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
module.exports = { | ||
entry: 'index.js', | ||
format: 'iife', | ||
moduleName: 'jsonp', | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import filesize from 'rollup-plugin-filesize'; | ||
import {uglify} from 'rollup-plugin-uglify'; | ||
|
||
import {version} from './package.json'; | ||
|
||
const name = 'jsonp'; | ||
|
||
const createInput = (input, outputBasename) => ({ | ||
input, | ||
output: [ | ||
createOutput(`dist/${outputBasename}.min.js`), | ||
createOutput(`dist/${outputBasename}-${version}.min.js`) | ||
], | ||
plugins: [ | ||
require('rollup-plugin-commonjs')(), | ||
require('rollup-plugin-filesize')(), | ||
require('rollup-plugin-uglify')({ | ||
commonjs(), | ||
filesize(), | ||
uglify({ | ||
compress: { | ||
collapse_vars: true, | ||
unsafe: true | ||
}, | ||
mangle: true | ||
}) | ||
], | ||
sourceMap: true | ||
}; | ||
] | ||
}); | ||
|
||
const createOutput = file => ({ | ||
file, | ||
format: 'iife', | ||
name, | ||
sourcemap: true | ||
}); | ||
|
||
export default [ | ||
createInput('callback.js', name), | ||
createInput('promise.js', `${name}-promise`) | ||
]; |
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
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,89 @@ | ||
/* eslint-disable import/no-unassigned-import */ | ||
/* eslint-disable no-unused-expressions */ | ||
/* eslint-disable promise/prefer-await-to-then */ | ||
|
||
require('core-js/features/promise'); | ||
var _ = require('lodash'); | ||
|
||
var jsonp = require('../promise'); | ||
|
||
describe('jsonp/promise', function () { | ||
this.timeout(20000); | ||
|
||
beforeEach(function () { | ||
sinon.spy(document.head, 'appendChild'); | ||
window.foo = {}; | ||
}); | ||
|
||
afterEach(function () { | ||
document.head.appendChild.restore(); | ||
delete window.foo; | ||
}); | ||
|
||
it('injects a script', function () { | ||
var promise = jsonp('https://jsfiddle.net/echo/jsonp'); | ||
expect(document.head.appendChild.lastCall.args[0].src).to.equal('https://jsfiddle.net/echo/jsonp?callback=j0'); | ||
return promise; | ||
}); | ||
|
||
it('respects query parameters', function () { | ||
var promise = jsonp('https://jsfiddle.net/echo/jsonp?foo=bar'); | ||
expect(document.head.appendChild.lastCall.args[0].src).to.equal('https://jsfiddle.net/echo/jsonp?foo=bar&callback=j1'); | ||
return promise; | ||
}); | ||
|
||
it('handles simultaneous requests', function () { | ||
return Promise.all([ | ||
jsonp('https://jsfiddle.net/echo/jsonp?foo=bar&delay=1'), | ||
jsonp('https://jsfiddle.net/echo/jsonp?baz=qux') | ||
]).then(function (data) { | ||
expect(data[0]).to.deep.equal({foo: 'bar'}); | ||
expect(data[1]).to.deep.equal({baz: 'qux'}); | ||
}); | ||
}); | ||
|
||
it('retrieves data and cleans up', function () { | ||
var promise = jsonp('https://jsfiddle.net/echo/jsonp?foo=bar'); | ||
return promise.then(function (data) { | ||
expect(data).to.deep.equal({foo: 'bar'}); | ||
expect(Object.keys(window).some(RegExp.prototype.test.bind(/^j\d+/))).to.be.false; | ||
expect(document.querySelectorAll('script[src*="jsfiddle.net"]')).to.have.lengthOf(0); | ||
}); | ||
}); | ||
|
||
it('fails and cleans up', function () { | ||
var promise = jsonp('https://httpbin.org/status/400'); | ||
return promise.then(expect.fail, function (err) { | ||
expect(err).to.be.an('error'); | ||
expect(Object.keys(window).some(RegExp.prototype.test.bind(/^j\d+/))).to.be.false; | ||
expect(document.querySelectorAll('script[src*="jsfiddle.net"]')).to.have.lengthOf(0); | ||
}); | ||
}); | ||
|
||
it('sets a custom callback query parameter', function () { | ||
var promise = jsonp('https://www.reddit.com/api/info.json', {parameter: 'jsonp'}); | ||
expect(document.head.appendChild.lastCall.args[0].src).to.equal('https://www.reddit.com/api/info.json?jsonp=j6'); | ||
return promise; | ||
}); | ||
|
||
it('disables the callback query parameter', function () { | ||
var promise = jsonp('https://httpbin.org/status/400', {parameter: ''}); | ||
expect(document.head.appendChild.lastCall.args[0].src).to.equal('https://httpbin.org/status/400'); | ||
return promise.then(expect.fail, _.noop); | ||
}); | ||
|
||
it('retrieves data via a custom callback name', function () { | ||
var promise = jsonp('https://jsfiddle.net/echo/jsonp?foo=bar', {key: 'foo'}); | ||
return promise.then(function (data) { | ||
expect(data).to.deep.equal({foo: 'bar'}); | ||
expect(document.head.appendChild.lastCall.args[0].src).to.equal('https://jsfiddle.net/echo/jsonp?foo=bar&callback=foo'); | ||
}); | ||
}); | ||
|
||
it('retrieves data via a custom callback object', function () { | ||
var promise = jsonp('https://jsfiddle.net/echo/jsonp?foo=bar&callback=foo.bar', {object: window.foo, key: 'bar', parameter: ''}); | ||
return promise.then(function (data) { | ||
expect(data).to.deep.equal({foo: 'bar'}); | ||
}); | ||
}); | ||
}); |