-
Notifications
You must be signed in to change notification settings - Fork 94
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 #459 from andywang425:dev
7.1.1 close #457
- Loading branch information
Showing
41 changed files
with
1,788 additions
and
706 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,127 @@ | ||
interface XMLHttpRequestProxy extends XMLHttpRequest { | ||
responseText: string | ||
readyState: number | ||
response: any | ||
responseURL: string | ||
responseXML: Document | null | ||
status: number | ||
statusText: string | ||
xhr: OriginXMLHttpRequest | ||
} | ||
|
||
interface OriginXMLHttpRequest extends XMLHttpRequest { | ||
getProxy(): XMLHttpRequestProxy | ||
} | ||
|
||
interface AttrGetterAndSetter<T = any> { | ||
getter?: (value: T, xhr: OriginXMLHttpRequest) => T | ||
setter?: (value: T, xhr: OriginXMLHttpRequest) => T | ||
} | ||
|
||
interface XhrRequestConfig { | ||
method: string | ||
url: string | ||
headers: any | ||
body: any | ||
async: boolean | ||
user: string | ||
password: string | ||
withCredentials: boolean | ||
xhr: OriginXMLHttpRequest | ||
} | ||
|
||
interface XhrResponse { | ||
config: XhrRequestConfig | ||
headers: any | ||
response: any | ||
status: number | ||
statusText?: string | ||
} | ||
|
||
type XhrErrorType = 'error' | 'timeout' | 'abort' | ||
|
||
interface XhrError { | ||
config: XhrRequestConfig | ||
type: XhrErrorType | ||
} | ||
|
||
interface Hooks { | ||
onreadystatechange?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
onabort?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
onerror?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
onload?: ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | null | ||
onloadend?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
onloadstart?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
onprogress?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
ontimeout?: | ||
| ((this: XMLHttpRequestProxy, xhr: OriginXMLHttpRequest, ev: ProgressEvent) => any) | ||
| null | ||
abort?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
getAllResponseHeaders?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
getResponseHeader?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
open?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
overrideMimeType?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
send?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
setRequestHeader?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
addEventListener?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
removeEventListener?: (args: Array<any>, xhr: OriginXMLHttpRequest) => any | ||
|
||
response?: AttrGetterAndSetter | ||
responseText?: AttrGetterAndSetter<string> | ||
readyState?: AttrGetterAndSetter<number> | ||
responseType?: AttrGetterAndSetter<XMLHttpRequestResponseType> | ||
responseURL?: AttrGetterAndSetter<string> | ||
responseXML?: AttrGetterAndSetter<Document | null> | ||
status?: AttrGetterAndSetter<number> | ||
statusText?: AttrGetterAndSetter<string> | ||
timeout?: AttrGetterAndSetter<number> | ||
upload?: AttrGetterAndSetter<XMLHttpRequestUpload> | ||
withCredentials?: AttrGetterAndSetter<boolean> | ||
} | ||
|
||
interface XhrHandler { | ||
resolve(response: XhrResponse): void | ||
|
||
reject(err: XhrError): void | ||
} | ||
|
||
interface XhrRequestHandler extends XhrHandler { | ||
next(config: XhrRequestConfig): void | ||
} | ||
|
||
interface XhrResponseHandler extends XhrHandler { | ||
next(response: XhrResponse): void | ||
} | ||
|
||
interface XhrErrorHandler extends XhrHandler { | ||
next(error: XhrError): void | ||
} | ||
|
||
interface Proxy { | ||
onRequest?: (config: XhrRequestConfig, handler: XhrRequestHandler) => void | ||
onResponse?: (response: XhrResponse, handler: XhrResponseHandler) => void | ||
onError?: (err: XhrError, handler: XhrErrorHandler) => void | ||
} | ||
|
||
export function proxy( | ||
proxy: Proxy, | ||
win?: Window | ||
): { originXhr: XMLHttpRequest; unProxy: () => void } | ||
|
||
export function unProxy(win?: Window) | ||
|
||
export function hook(hooks: Hooks, win?: Window): { originXhr: XMLHttpRequest; unHook: () => void } | ||
|
||
export function unHook(win?: Window) |
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,10 @@ | ||
/* | ||
* author: wendux | ||
* email: 824783146@qq.com | ||
* source code: https://github.com/wendux/Ajax-hook | ||
**/ | ||
export { hook } from './src/xhr-hook' | ||
export { proxy } from './src/xhr-proxy' | ||
|
||
// 因为原版的ajax-hook存在一个bug,https://github.com/wendux/ajax-hook/pull/122 | ||
// 所以暂时先使用 https://github.com/cyfung1031 修改后的版本 |
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,131 @@ | ||
/* | ||
* author: wendux | ||
* email: 824783146@qq.com | ||
* source code: https://github.com/wendux/Ajax-hook | ||
*/ | ||
|
||
export var events = ['load', 'loadend', 'timeout', 'error', 'readystatechange', 'abort'] | ||
|
||
var OriginXhr = '__origin_xhr' | ||
|
||
export function configEvent(event, xhrProxy) { | ||
var e = {} | ||
for (var attr in event) e[attr] = event[attr] | ||
// xhrProxy instead | ||
e.target = e.currentTarget = xhrProxy | ||
return e | ||
} | ||
|
||
export function hook(proxy, win) { | ||
win = win || window | ||
var originXhr = win.XMLHttpRequest | ||
|
||
var HookXMLHttpRequest = function () { | ||
// We shouldn't hookAjax XMLHttpRequest.prototype because we can't | ||
// guarantee that all attributes are on the prototype。 | ||
// Instead, hooking XMLHttpRequest instance can avoid this problem. | ||
|
||
var xhr = new originXhr() | ||
|
||
// Generate all callbacks(eg. onload) are enumerable (not undefined). | ||
for (var i = 0; i < events.length; ++i) { | ||
var key = 'on' + events[i] | ||
if (xhr[key] === undefined) xhr[key] = null | ||
} | ||
|
||
for (var attr in xhr) { | ||
var type = '' | ||
try { | ||
type = typeof xhr[attr] // May cause exception on some browser | ||
} catch (e) {} | ||
if (type === 'function') { | ||
// hookAjax methods of xhr, such as `open`、`send` ... | ||
this[attr] = hookFunction(attr) | ||
} else if (attr !== OriginXhr) { | ||
Object.defineProperty(this, attr, { | ||
get: getterFactory(attr), | ||
set: setterFactory(attr), | ||
enumerable: true | ||
}) | ||
} | ||
} | ||
var that = this | ||
xhr.getProxy = function () { | ||
return that | ||
} | ||
this[OriginXhr] = xhr | ||
} | ||
|
||
HookXMLHttpRequest.prototype = originXhr.prototype | ||
HookXMLHttpRequest.prototype.constructor = HookXMLHttpRequest | ||
|
||
win.XMLHttpRequest = HookXMLHttpRequest | ||
|
||
Object.assign(win.XMLHttpRequest, { | ||
UNSENT: 0, | ||
OPENED: 1, | ||
HEADERS_RECEIVED: 2, | ||
LOADING: 3, | ||
DONE: 4 | ||
}) | ||
|
||
// Generate getter for attributes of xhr | ||
function getterFactory(attr) { | ||
return function () { | ||
var v = this.hasOwnProperty(attr + '_') ? this[attr + '_'] : this[OriginXhr][attr] | ||
var attrGetterHook = (proxy[attr] || {})['getter'] | ||
return (attrGetterHook && attrGetterHook(v, this)) || v | ||
} | ||
} | ||
|
||
// Generate setter for attributes of xhr; by this we have an opportunity | ||
// to hookAjax event callbacks (eg: `onload`) of xhr; | ||
function setterFactory(attr) { | ||
return function (v) { | ||
var xhr = this[OriginXhr] | ||
var that = this | ||
var hook = proxy[attr] | ||
// hookAjax event callbacks such as `onload`、`onreadystatechange`... | ||
if (attr.substring(0, 2) === 'on') { | ||
that[attr + '_'] = v | ||
xhr[attr] = function (e) { | ||
e = configEvent(e, that) | ||
var ret = proxy[attr] && proxy[attr].call(that, xhr, e) | ||
ret || v.call(that, e) | ||
} | ||
} else { | ||
//If the attribute isn't writable, generate proxy attribute | ||
var attrSetterHook = (hook || {})['setter'] | ||
v = (attrSetterHook && attrSetterHook(v, that)) || v | ||
this[attr + '_'] = v | ||
try { | ||
// Not all attributes of xhr are writable(setter may undefined). | ||
xhr[attr] = v | ||
} catch (e) {} | ||
} | ||
} | ||
} | ||
|
||
// Hook methods of xhr. | ||
function hookFunction(fun) { | ||
return function () { | ||
var args = [].slice.call(arguments) | ||
if (proxy[fun]) { | ||
var ret = proxy[fun].call(this, args, this[OriginXhr]) | ||
// If the proxy return value exists, return it directly, | ||
// otherwise call the function of xhr. | ||
if (ret) return ret | ||
} | ||
return this[OriginXhr][fun].apply(this[OriginXhr], args) | ||
} | ||
} | ||
|
||
function unHook() { | ||
win.XMLHttpRequest = originXhr | ||
HookXMLHttpRequest.prototype.constructor = originXhr | ||
originXhr = undefined | ||
} | ||
|
||
// Return the real XMLHttpRequest and unHook func | ||
return { originXhr, unHook } | ||
} |
Oops, something went wrong.