@alicloud/fetcher-jsonp 1.5.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/const/index.js +0 -1
- package/build/cjs/index.js +0 -3
- package/build/cjs/util/clear-callback-fn.js +0 -2
- package/build/cjs/util/create-error.js +0 -1
- package/build/cjs/util/generate-callback-name.js +0 -1
- package/build/cjs/util/jsonp.js +23 -35
- package/build/es/const/index.js +0 -1
- package/build/es/util/clear-callback-fn.js +0 -1
- package/build/es/util/jsonp.js +24 -30
- package/package.json +7 -7
package/build/cjs/const/index.js
CHANGED
package/build/cjs/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
@@ -17,7 +16,5 @@ Object.defineProperty(exports, "default", {
|
|
|
17
16
|
return _jsonp.default;
|
|
18
17
|
}
|
|
19
18
|
});
|
|
20
|
-
|
|
21
19
|
var _jsonp = _interopRequireDefault(require("./util/jsonp"));
|
|
22
|
-
|
|
23
20
|
var _const = require("./const");
|
|
@@ -4,13 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = clearCallbackFn;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* 清除全局的方法
|
|
10
9
|
*/
|
|
11
10
|
function clearCallbackFn(callbackName) {
|
|
12
11
|
var win = window;
|
|
13
|
-
|
|
14
12
|
try {
|
|
15
13
|
// IE8 throws exception when try to delete a property on window http://stackoverflow.com/a/1824228/751089
|
|
16
14
|
delete win[callbackName];
|
package/build/cjs/util/jsonp.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = jsonp;
|
|
9
|
-
|
|
10
8
|
var _const = require("../const");
|
|
11
|
-
|
|
12
9
|
var _createError = _interopRequireDefault(require("./create-error"));
|
|
13
|
-
|
|
14
10
|
var _generateCallbackName = _interopRequireDefault(require("./generate-callback-name"));
|
|
15
|
-
|
|
16
11
|
var _clearCallbackFn = _interopRequireDefault(require("./clear-callback-fn"));
|
|
17
|
-
|
|
18
12
|
/**
|
|
19
13
|
* 一个「纯」的 Promise 封装的 JSONP
|
|
20
14
|
*
|
|
@@ -24,58 +18,55 @@ function jsonp() {
|
|
|
24
18
|
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
25
19
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
26
20
|
var _options$timeout = options.timeout,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
timeout = _options$timeout === void 0 ? 5000 : _options$timeout,
|
|
22
|
+
charset = options.charset,
|
|
23
|
+
_options$jsonpCallbac = options.jsonpCallback,
|
|
24
|
+
jsonpCallback = _options$jsonpCallbac === void 0 ? 'callback' : _options$jsonpCallbac,
|
|
25
|
+
_options$jsonpCallbac2 = options.jsonpCallbackFunction,
|
|
26
|
+
jsonpCallbackFunction = _options$jsonpCallbac2 === void 0 ? (0, _generateCallbackName.default)() : _options$jsonpCallbac2,
|
|
27
|
+
signal = options.signal;
|
|
34
28
|
var scriptElement = document.createElement('script');
|
|
35
29
|
var timeoutId;
|
|
36
30
|
scriptElement.src = "".concat(url).concat(url.indexOf('?') < 0 ? '?' : '&').concat(jsonpCallback, "=").concat(jsonpCallbackFunction);
|
|
37
|
-
|
|
38
31
|
if (charset) {
|
|
39
32
|
scriptElement.setAttribute('charset', charset);
|
|
40
33
|
}
|
|
34
|
+
(document.head || document.getElementsByTagName('head')[0]).appendChild(scriptElement);
|
|
41
35
|
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
// 清除全局污染
|
|
44
37
|
function cleanup() {
|
|
45
|
-
(0, _clearCallbackFn.default)(jsonpCallbackFunction);
|
|
38
|
+
(0, _clearCallbackFn.default)(jsonpCallbackFunction);
|
|
46
39
|
|
|
40
|
+
// 清除 script DOM
|
|
47
41
|
if (scriptElement.parentNode) {
|
|
48
42
|
scriptElement.parentNode.removeChild(scriptElement);
|
|
49
|
-
}
|
|
50
|
-
|
|
43
|
+
}
|
|
51
44
|
|
|
45
|
+
// 清除 timeout
|
|
52
46
|
if (timeoutId) {
|
|
53
47
|
window.clearTimeout(timeoutId);
|
|
54
48
|
timeoutId = undefined;
|
|
55
49
|
}
|
|
56
50
|
}
|
|
51
|
+
|
|
57
52
|
/**
|
|
58
53
|
* 当 abort 或 timeout 的时候,执行 cleanup,虽然 DOM 被移除了,但 JS 的加载还在继续,超时后还有可能加载完毕,
|
|
59
54
|
* 需要避免这种情况下抛错
|
|
60
55
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
56
|
function cleanupPrematurely() {
|
|
64
57
|
cleanup();
|
|
65
|
-
|
|
66
58
|
window[jsonpCallbackFunction] = function () {
|
|
67
59
|
return (0, _clearCallbackFn.default)(jsonpCallbackFunction);
|
|
68
60
|
};
|
|
69
61
|
}
|
|
70
|
-
|
|
71
62
|
return new Promise(function (resolve, reject) {
|
|
72
|
-
var returned = false;
|
|
63
|
+
var returned = false;
|
|
73
64
|
|
|
65
|
+
// 异步事件 1:回调
|
|
74
66
|
window[jsonpCallbackFunction] = function (result) {
|
|
75
67
|
if (returned) {
|
|
76
68
|
return;
|
|
77
69
|
}
|
|
78
|
-
|
|
79
70
|
returned = true;
|
|
80
71
|
resolve({
|
|
81
72
|
ok: true,
|
|
@@ -85,41 +76,38 @@ function jsonp() {
|
|
|
85
76
|
}
|
|
86
77
|
});
|
|
87
78
|
cleanup();
|
|
88
|
-
};
|
|
89
|
-
|
|
79
|
+
};
|
|
90
80
|
|
|
81
|
+
// 异步事件 2:超时
|
|
91
82
|
if (timeout > 0) {
|
|
92
83
|
timeoutId = window.setTimeout(function () {
|
|
93
84
|
if (returned) {
|
|
94
85
|
return;
|
|
95
86
|
}
|
|
96
|
-
|
|
97
87
|
returned = true;
|
|
98
88
|
reject((0, _createError.default)(_const.EJsonpError.TIMEOUT, "JSONP timeout after ".concat(timeout, "ms, url = ").concat(url)));
|
|
99
89
|
cleanupPrematurely();
|
|
100
90
|
}, timeout);
|
|
101
|
-
}
|
|
102
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
|
103
|
-
|
|
91
|
+
}
|
|
104
92
|
|
|
93
|
+
// 异步事件 3:abort,监听 signal 的 abort(和 fetch 一样)
|
|
94
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
|
105
95
|
if (signal) {
|
|
106
96
|
signal.addEventListener('abort', function () {
|
|
107
97
|
if (returned) {
|
|
108
98
|
return;
|
|
109
99
|
}
|
|
110
|
-
|
|
111
100
|
returned = true;
|
|
112
101
|
reject((0, _createError.default)('AbortError', "The JSONP was aborted, url = ".concat(url)));
|
|
113
102
|
cleanupPrematurely();
|
|
114
103
|
});
|
|
115
|
-
}
|
|
116
|
-
|
|
104
|
+
}
|
|
117
105
|
|
|
106
|
+
// 异步事件 3:404/500 等
|
|
118
107
|
scriptElement.onerror = function () {
|
|
119
108
|
if (returned) {
|
|
120
109
|
return;
|
|
121
110
|
}
|
|
122
|
-
|
|
123
111
|
returned = true;
|
|
124
112
|
reject((0, _createError.default)(_const.EJsonpError.NETWORK, "JSONP failed, url = ".concat(url)));
|
|
125
113
|
cleanup();
|
package/build/es/const/index.js
CHANGED
package/build/es/util/jsonp.js
CHANGED
|
@@ -2,68 +2,65 @@ import { EJsonpError } from '../const';
|
|
|
2
2
|
import createError from './create-error';
|
|
3
3
|
import generateCallbackName from './generate-callback-name';
|
|
4
4
|
import clearCallbackFn from './clear-callback-fn';
|
|
5
|
+
|
|
5
6
|
/**
|
|
6
7
|
* 一个「纯」的 Promise 封装的 JSONP
|
|
7
8
|
*
|
|
8
9
|
* 参考 https://github.com/camsong/fetch-jsonp
|
|
9
10
|
*/
|
|
10
|
-
|
|
11
11
|
export default function jsonp() {
|
|
12
12
|
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
13
13
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
14
14
|
var _options$timeout = options.timeout,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
timeout = _options$timeout === void 0 ? 5000 : _options$timeout,
|
|
16
|
+
charset = options.charset,
|
|
17
|
+
_options$jsonpCallbac = options.jsonpCallback,
|
|
18
|
+
jsonpCallback = _options$jsonpCallbac === void 0 ? 'callback' : _options$jsonpCallbac,
|
|
19
|
+
_options$jsonpCallbac2 = options.jsonpCallbackFunction,
|
|
20
|
+
jsonpCallbackFunction = _options$jsonpCallbac2 === void 0 ? generateCallbackName() : _options$jsonpCallbac2,
|
|
21
|
+
signal = options.signal;
|
|
22
22
|
var scriptElement = document.createElement('script');
|
|
23
23
|
var timeoutId;
|
|
24
24
|
scriptElement.src = "".concat(url).concat(url.indexOf('?') < 0 ? '?' : '&').concat(jsonpCallback, "=").concat(jsonpCallbackFunction);
|
|
25
|
-
|
|
26
25
|
if (charset) {
|
|
27
26
|
scriptElement.setAttribute('charset', charset);
|
|
28
27
|
}
|
|
28
|
+
(document.head || document.getElementsByTagName('head')[0]).appendChild(scriptElement);
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
// 清除全局污染
|
|
32
31
|
function cleanup() {
|
|
33
|
-
clearCallbackFn(jsonpCallbackFunction);
|
|
32
|
+
clearCallbackFn(jsonpCallbackFunction);
|
|
34
33
|
|
|
34
|
+
// 清除 script DOM
|
|
35
35
|
if (scriptElement.parentNode) {
|
|
36
36
|
scriptElement.parentNode.removeChild(scriptElement);
|
|
37
|
-
}
|
|
38
|
-
|
|
37
|
+
}
|
|
39
38
|
|
|
39
|
+
// 清除 timeout
|
|
40
40
|
if (timeoutId) {
|
|
41
41
|
window.clearTimeout(timeoutId);
|
|
42
42
|
timeoutId = undefined;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
+
|
|
45
46
|
/**
|
|
46
47
|
* 当 abort 或 timeout 的时候,执行 cleanup,虽然 DOM 被移除了,但 JS 的加载还在继续,超时后还有可能加载完毕,
|
|
47
48
|
* 需要避免这种情况下抛错
|
|
48
49
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
50
|
function cleanupPrematurely() {
|
|
52
51
|
cleanup();
|
|
53
|
-
|
|
54
52
|
window[jsonpCallbackFunction] = function () {
|
|
55
53
|
return clearCallbackFn(jsonpCallbackFunction);
|
|
56
54
|
};
|
|
57
55
|
}
|
|
58
|
-
|
|
59
56
|
return new Promise(function (resolve, reject) {
|
|
60
|
-
var returned = false;
|
|
57
|
+
var returned = false;
|
|
61
58
|
|
|
59
|
+
// 异步事件 1:回调
|
|
62
60
|
window[jsonpCallbackFunction] = function (result) {
|
|
63
61
|
if (returned) {
|
|
64
62
|
return;
|
|
65
63
|
}
|
|
66
|
-
|
|
67
64
|
returned = true;
|
|
68
65
|
resolve({
|
|
69
66
|
ok: true,
|
|
@@ -73,41 +70,38 @@ export default function jsonp() {
|
|
|
73
70
|
}
|
|
74
71
|
});
|
|
75
72
|
cleanup();
|
|
76
|
-
};
|
|
77
|
-
|
|
73
|
+
};
|
|
78
74
|
|
|
75
|
+
// 异步事件 2:超时
|
|
79
76
|
if (timeout > 0) {
|
|
80
77
|
timeoutId = window.setTimeout(function () {
|
|
81
78
|
if (returned) {
|
|
82
79
|
return;
|
|
83
80
|
}
|
|
84
|
-
|
|
85
81
|
returned = true;
|
|
86
82
|
reject(createError(EJsonpError.TIMEOUT, "JSONP timeout after ".concat(timeout, "ms, url = ").concat(url)));
|
|
87
83
|
cleanupPrematurely();
|
|
88
84
|
}, timeout);
|
|
89
|
-
}
|
|
90
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
|
91
|
-
|
|
85
|
+
}
|
|
92
86
|
|
|
87
|
+
// 异步事件 3:abort,监听 signal 的 abort(和 fetch 一样)
|
|
88
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
|
93
89
|
if (signal) {
|
|
94
90
|
signal.addEventListener('abort', function () {
|
|
95
91
|
if (returned) {
|
|
96
92
|
return;
|
|
97
93
|
}
|
|
98
|
-
|
|
99
94
|
returned = true;
|
|
100
95
|
reject(createError('AbortError', "The JSONP was aborted, url = ".concat(url)));
|
|
101
96
|
cleanupPrematurely();
|
|
102
97
|
});
|
|
103
|
-
}
|
|
104
|
-
|
|
98
|
+
}
|
|
105
99
|
|
|
100
|
+
// 异步事件 3:404/500 等
|
|
106
101
|
scriptElement.onerror = function () {
|
|
107
102
|
if (returned) {
|
|
108
103
|
return;
|
|
109
104
|
}
|
|
110
|
-
|
|
111
105
|
returned = true;
|
|
112
106
|
reject(createError(EJsonpError.NETWORK, "JSONP failed, url = ".concat(url)));
|
|
113
107
|
cleanup();
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alicloud/fetcher-jsonp",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "fetcher 的底层 jsonp 实现,可独立使用",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "build/cjs/index.js",
|
|
8
8
|
"module": "build/es/index.js",
|
|
9
9
|
"types": "build/types/index.d.ts",
|
|
10
|
-
"homepage": "https://github.com/aliyun/alibabacloud-console-base/tree/master/packages/fetcher-jsonp",
|
|
10
|
+
"homepage": "https://github.com/aliyun/alibabacloud-console-base/tree/master/packages-fetcher/fetcher-jsonp",
|
|
11
11
|
"author": {
|
|
12
12
|
"name": "Jianchun Wang",
|
|
13
13
|
"email": "justnewbee@gmail.com"
|
|
@@ -25,14 +25,14 @@
|
|
|
25
25
|
"promise"
|
|
26
26
|
],
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@alicloud/console-base-demo-helper-theme-switcher": "^1.1.
|
|
28
|
+
"@alicloud/console-base-demo-helper-theme-switcher": "^1.1.2",
|
|
29
29
|
"@alicloud/console-toolkit-cli": "^1.2.30",
|
|
30
30
|
"@alicloud/console-toolkit-preset-component": "^1.2.61",
|
|
31
|
-
"@alicloud/demo-rc-elements": "^1.10.
|
|
32
|
-
"@alicloud/fetcher-demo-helpers": "^1.4.
|
|
31
|
+
"@alicloud/demo-rc-elements": "^1.10.8",
|
|
32
|
+
"@alicloud/fetcher-demo-helpers": "^1.4.2",
|
|
33
33
|
"@types/react": "^17.0.48",
|
|
34
34
|
"react": "^17.0.2",
|
|
35
|
-
"typescript": "^4.
|
|
35
|
+
"typescript": "^4.8.4"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"start": "breezr start-storybook",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"clean": "rm -rf build",
|
|
46
46
|
"prepublishOnly": "yarn clean && yarn build"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "4b5eefd0215b1f8028fce664d64484ceeb703ef1"
|
|
49
49
|
}
|