This repository was archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathjsonp.js
130 lines (112 loc) · 3.59 KB
/
jsonp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2012-2016 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
'use strict';
var UrlBuilder, responsePromise, client;
UrlBuilder = require('../UrlBuilder');
responsePromise = require('../util/responsePromise');
client = require('../client');
// consider abstracting this into a util module
function clearProperty(scope, propertyName) {
try {
delete scope[propertyName];
}
catch (e) {
// IE doesn't like to delete properties on the window object
if (propertyName in scope) {
scope[propertyName] = void 0;
}
}
}
function cleanupScriptNode(response) {
try {
if (response.raw && response.raw.parentNode) {
response.raw.parentNode.removeChild(response.raw);
}
} catch (e) {
// ignore
}
}
function registerCallback(prefix, resolve, response, name) {
if (!name) {
do {
name = prefix + Math.floor(new Date().getTime() * Math.random());
}
while (name in window);
}
window[name] = function jsonpCallback(data) {
response.entity = data;
clearProperty(window, name);
cleanupScriptNode(response);
if (!response.request.canceled) {
resolve(response);
}
};
return name;
}
/**
* Executes the request as JSONP.
*
* @param {string} request.path the URL to load
* @param {Object} [request.params] parameters to bind to the path
* @param {string} [request.callback.param='callback'] the parameter name for
* which the callback function name is the value
* @param {string} [request.callback.prefix='jsonp'] prefix for the callback
* function, as the callback is attached to the window object, a unique,
* unobtrusive prefix is desired
* @param {string} [request.callback.name=<generated>] pins the name of the
* callback function, useful for cases where the server doesn't allow
* custom callback names. Generally not recommended.
*
* @returns {Promise<Response>}
*/
module.exports = client(function jsonp(request) {
return responsePromise.promise(function (resolve, reject) {
var callbackName, callbackParams, script, firstScript, response;
request = typeof request === 'string' ? { path: request } : request || {};
response = { request: request };
if (request.canceled) {
response.error = 'precanceled';
reject(response);
return;
}
request.callback = request.callback || {};
callbackName = registerCallback(request.callback.prefix || 'jsonp', resolve, response, request.callback.name);
callbackParams = {};
callbackParams[request.callback.param || 'callback'] = callbackName;
request.canceled = false;
request.cancel = function cancel() {
request.canceled = true;
cleanupScriptNode(response);
reject(response);
};
script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = response.url = new UrlBuilder(request.path, callbackParams).build();
function handlePossibleError() {
if (typeof window[callbackName] === 'function') {
response.error = 'loaderror';
clearProperty(window, callbackName);
cleanupScriptNode(response);
reject(response);
}
}
script.onerror = function () {
handlePossibleError();
};
script.onload = script.onreadystatechange = function (e) {
// script tag load callbacks are completely non-standard
// handle case where onreadystatechange is fired for an error instead of onerror
if ((e && (e.type === 'load' || e.type === 'error')) || script.readyState === 'loaded') {
handlePossibleError();
}
};
response.raw = script;
firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
});
});