Skip to content

Commit

Permalink
Merge pull request #241 from galacean/feat/support-request-stats
Browse files Browse the repository at this point in the history
Fix Cancel request auto hook
  • Loading branch information
JujieX authored Dec 14, 2023
2 parents 23f1438 + e7f541e commit 6f4df38
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions packages/stats/src/hooks/RequestHook.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@

let requestSize = 0;

let originalSend = XMLHttpRequest.prototype.send;
function hookRequest() {

const cacheMap = new Map<string, number>();
function addRequestSize(url: string, size: number) {
if (cacheMap.get(url) == undefined) {
cacheMap.set(url, size);
console.log(`request(${size}): ${url}`);
requestSize += size;
let originalSend = XMLHttpRequest.prototype.send;

const cacheMap = new Map<string, number>();
function addRequestSize(url: string, size: number) {
if (cacheMap.get(url) == undefined) {
cacheMap.set(url, size);
console.log(`request(${size}): ${url}`);
requestSize += size;
}
}

XMLHttpRequest.prototype.send = function (body) {
this.addEventListener(
"load",
function () {
let size = 0;
if (this.responseType === "" || this.responseType === "text") {
size = new Blob([JSON.stringify(this.responseText)]).size;
} else if (this.response instanceof Blob) {
size = this.response.size;
} else if (this.response instanceof ArrayBuffer) {
size = this.response.byteLength;
} else if (this.responseType === "json") {
size = new Blob([JSON.stringify(this.response)]).size;
}

addRequestSize((this as XMLHttpRequest).responseURL, size);
},
false
);

originalSend.call(this, body);

var originalImageSrc = Object.getOwnPropertyDescriptor(
Image.prototype,
"src"
).set;

this.originalImageSrc = originalImageSrc;

Object.defineProperty(Image.prototype, "src", {
set: function (value) {
fetch(value).then((response) => {
if (response.ok) {
response.blob().then((blob) => {
addRequestSize((this as XMLHttpRequest).responseURL, blob.size);
});
}
});
originalImageSrc.call(this, value);
},
});
};
}

XMLHttpRequest.prototype.send = function (body) {
this.addEventListener(
"load",
function () {
let size = 0;
if (this.responseType === "" || this.responseType === "text") {
size = new Blob([JSON.stringify(this.responseText)]).size;
} else if (this.response instanceof Blob) {
size = this.response.size;
} else if (this.response instanceof ArrayBuffer) {
size = this.response.byteLength;
} else if (this.responseType === "json") {
size = new Blob([JSON.stringify(this.response)]).size;
}

addRequestSize((this as XMLHttpRequest).responseURL, size);
},
false
);

originalSend.call(this, body);

var originalImageSrc = Object.getOwnPropertyDescriptor(
Image.prototype,
"src"
).set;

this.originalImageSrc = originalImageSrc;

Object.defineProperty(Image.prototype, "src", {
set: function (value) {
fetch(value).then((response) => {
if (response.ok) {
response.blob().then((blob) => {
addRequestSize((this as XMLHttpRequest).responseURL, blob.size);
});
}
});
originalImageSrc.call(this, value);
},
});
};

export class RequestHook {
private _originalSend;
Expand All @@ -64,6 +69,7 @@ export class RequestHook {

constructor() {
this._hooked = true;
hookRequest();
}

public reset(): void {
Expand Down

0 comments on commit 6f4df38

Please sign in to comment.