ajax-hooker 1.3.0 → 1.3.1

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/dist/cjs/index.js CHANGED
@@ -60,6 +60,7 @@ class XhrInterceptor {
60
60
  DONE: this.nativeXhr.DONE
61
61
  };
62
62
  xhrResponseEvents=[ "readystatechange", "load", "loadend" ];
63
+ xhrAssignedMethodOverrides=new WeakMap;
63
64
  xhrInstanceAttr=[ "response", "responseText", "responseXML", "status", "statusText" ];
64
65
  xhrInstanceAttrHandler=this.xhrInstanceAttr.reduce((acc, attr) => {
65
66
  acc[attr] = function(target) {
@@ -224,11 +225,24 @@ class XhrInterceptor {
224
225
  };
225
226
  return toSortedString(a) === toSortedString(b);
226
227
  }
228
+ getAssignedMethodOverride(target, attr) {
229
+ return this.xhrAssignedMethodOverrides.get(target)?.[attr];
230
+ }
231
+ setAssignedMethodOverride(target, attr, value) {
232
+ let targetOverrides = this.xhrAssignedMethodOverrides.get(target);
233
+ if (!targetOverrides) {
234
+ targetOverrides = {};
235
+ this.xhrAssignedMethodOverrides.set(target, targetOverrides);
236
+ }
237
+ targetOverrides[attr] = value;
238
+ }
227
239
  getCaptureOption(options) {
228
240
  if (typeof options === "boolean") return options;
229
241
  return !!options?.capture;
230
242
  }
231
243
  getAttrHandler(target, attr, receiver) {
244
+ const assignedMethodOverride = this.getAssignedMethodOverride(target, attr);
245
+ if (assignedMethodOverride !== void 0) return assignedMethodOverride;
232
246
  if (this.xhrInstanceAttr.includes(attr)) return this.xhrInstanceAttrHandler[attr](target);
233
247
  if (this.xhrMethodsHandler[attr]) return this.xhrMethodsHandler[attr](this, target, receiver);
234
248
  return null;
@@ -243,6 +257,7 @@ class XhrInterceptor {
243
257
  return self.getAttrHandler(target, prop, receiver) ?? getProxyValue(target, prop);
244
258
  },
245
259
  set(target, prop, value, receiver) {
260
+ if (self.xhrMethodsHandler[prop]) self.setAssignedMethodOverride(target, prop, value);
246
261
  if (typeof value === "function" && prop.startsWith("on")) {
247
262
  const isResponseEvent = self.xhrResponseEvents.includes(prop.replace(/^on/, ""));
248
263
  const fn = async function(...args) {
package/dist/esm/index.js CHANGED
@@ -54,6 +54,7 @@ class XhrInterceptor {
54
54
  DONE: this.nativeXhr.DONE
55
55
  };
56
56
  xhrResponseEvents=[ "readystatechange", "load", "loadend" ];
57
+ xhrAssignedMethodOverrides=new WeakMap;
57
58
  xhrInstanceAttr=[ "response", "responseText", "responseXML", "status", "statusText" ];
58
59
  xhrInstanceAttrHandler=this.xhrInstanceAttr.reduce((acc, attr) => {
59
60
  acc[attr] = function(target) {
@@ -218,11 +219,24 @@ class XhrInterceptor {
218
219
  };
219
220
  return toSortedString(a) === toSortedString(b);
220
221
  }
222
+ getAssignedMethodOverride(target, attr) {
223
+ return this.xhrAssignedMethodOverrides.get(target)?.[attr];
224
+ }
225
+ setAssignedMethodOverride(target, attr, value) {
226
+ let targetOverrides = this.xhrAssignedMethodOverrides.get(target);
227
+ if (!targetOverrides) {
228
+ targetOverrides = {};
229
+ this.xhrAssignedMethodOverrides.set(target, targetOverrides);
230
+ }
231
+ targetOverrides[attr] = value;
232
+ }
221
233
  getCaptureOption(options) {
222
234
  if (typeof options === "boolean") return options;
223
235
  return !!options?.capture;
224
236
  }
225
237
  getAttrHandler(target, attr, receiver) {
238
+ const assignedMethodOverride = this.getAssignedMethodOverride(target, attr);
239
+ if (assignedMethodOverride !== void 0) return assignedMethodOverride;
226
240
  if (this.xhrInstanceAttr.includes(attr)) return this.xhrInstanceAttrHandler[attr](target);
227
241
  if (this.xhrMethodsHandler[attr]) return this.xhrMethodsHandler[attr](this, target, receiver);
228
242
  return null;
@@ -237,6 +251,7 @@ class XhrInterceptor {
237
251
  return self.getAttrHandler(target, prop, receiver) ?? getProxyValue(target, prop);
238
252
  },
239
253
  set(target, prop, value, receiver) {
254
+ if (self.xhrMethodsHandler[prop]) self.setAssignedMethodOverride(target, prop, value);
240
255
  if (typeof value === "function" && prop.startsWith("on")) {
241
256
  const isResponseEvent = self.xhrResponseEvents.includes(prop.replace(/^on/, ""));
242
257
  const fn = async function(...args) {
@@ -48,6 +48,7 @@ var AjaxHooker = function(exports) {
48
48
  DONE: this.nativeXhr.DONE
49
49
  };
50
50
  xhrResponseEvents=[ "readystatechange", "load", "loadend" ];
51
+ xhrAssignedMethodOverrides=new WeakMap;
51
52
  xhrInstanceAttr=[ "response", "responseText", "responseXML", "status", "statusText" ];
52
53
  xhrInstanceAttrHandler=this.xhrInstanceAttr.reduce((acc, attr) => {
53
54
  acc[attr] = function(target) {
@@ -212,11 +213,24 @@ var AjaxHooker = function(exports) {
212
213
  };
213
214
  return toSortedString(a) === toSortedString(b);
214
215
  }
216
+ getAssignedMethodOverride(target, attr) {
217
+ return this.xhrAssignedMethodOverrides.get(target)?.[attr];
218
+ }
219
+ setAssignedMethodOverride(target, attr, value) {
220
+ let targetOverrides = this.xhrAssignedMethodOverrides.get(target);
221
+ if (!targetOverrides) {
222
+ targetOverrides = {};
223
+ this.xhrAssignedMethodOverrides.set(target, targetOverrides);
224
+ }
225
+ targetOverrides[attr] = value;
226
+ }
215
227
  getCaptureOption(options) {
216
228
  if (typeof options === "boolean") return options;
217
229
  return !!options?.capture;
218
230
  }
219
231
  getAttrHandler(target, attr, receiver) {
232
+ const assignedMethodOverride = this.getAssignedMethodOverride(target, attr);
233
+ if (assignedMethodOverride !== void 0) return assignedMethodOverride;
220
234
  if (this.xhrInstanceAttr.includes(attr)) return this.xhrInstanceAttrHandler[attr](target);
221
235
  if (this.xhrMethodsHandler[attr]) return this.xhrMethodsHandler[attr](this, target, receiver);
222
236
  return null;
@@ -231,6 +245,7 @@ var AjaxHooker = function(exports) {
231
245
  return self.getAttrHandler(target, prop, receiver) ?? getProxyValue(target, prop);
232
246
  },
233
247
  set(target, prop, value, receiver) {
248
+ if (self.xhrMethodsHandler[prop]) self.setAssignedMethodOverride(target, prop, value);
234
249
  if (typeof value === "function" && prop.startsWith("on")) {
235
250
  const isResponseEvent = self.xhrResponseEvents.includes(prop.replace(/^on/, ""));
236
251
  const fn = async function(...args) {
@@ -13,6 +13,7 @@ export declare class XhrInterceptor {
13
13
  hooks: HookFunction[];
14
14
  private readonly xhrContructorKeys;
15
15
  private xhrResponseEvents;
16
+ private xhrAssignedMethodOverrides;
16
17
  private xhrInstanceAttr;
17
18
  private xhrInstanceAttrHandler;
18
19
  private xhrMethodsHandler;
@@ -22,6 +23,8 @@ export declare class XhrInterceptor {
22
23
  private parseHeaders;
23
24
  private responseProcessor;
24
25
  private headersEqual;
26
+ private getAssignedMethodOverride;
27
+ private setAssignedMethodOverride;
25
28
  private getCaptureOption;
26
29
  private getAttrHandler;
27
30
  private _generateProxyXMLHttpRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ajax-hooker",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Browser AJAX interceptor for XMLHttpRequest and fetch with unified hooks, request/response mutation, and streaming response support.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",