New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

xhook

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xhook - npm Package Compare versions

Comparing version

to
1.5.4

LICENSE

56

dist/xhook.js

@@ -1,2 +0,2 @@

//XHook - v1.5.3 - https://github.com/jpillora/xhook
//XHook - v1.5.4 - https://github.com/jpillora/xhook
//Jaime Pillora <dev@jpillora.com> - MIT Copyright 2022

@@ -260,2 +260,6 @@ var xhook = (function () {

//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
const nullify = res => (res === undefined ? null : res);

@@ -395,7 +399,7 @@

//before emitting 4, run all 'after' hooks in sequence
const hooks = xhook.listeners("after");
const afterHooks = hooks.listeners("after");
var process = function() {
if (hooks.length > 0) {
if (afterHooks.length > 0) {
//execute each 'before' hook one at a time
const hook = hooks.shift();
const hook = afterHooks.shift();
if (hook.length === 2) {

@@ -460,3 +464,3 @@ hook(request, response);

// or if explicitly told to do so
if ("withCredentials" in xhr || xhook.addWithCredentials) {
if ("withCredentials" in xhr) {
facade.withCredentials = false;

@@ -549,6 +553,6 @@ }

const hooks = xhook.listeners("before");
//process hooks sequentially
const beforeHooks = hooks.listeners("before");
//process beforeHooks sequentially
var process = function() {
if (!hooks.length) {
if (!beforeHooks.length) {
return send();

@@ -571,3 +575,3 @@ }

}
//continue processing until no hooks left
//continue processing until no beforeHooks left
process();

@@ -586,3 +590,3 @@ };

const hook = hooks.shift();
const hook = beforeHooks.shift();
//async or sync?

@@ -682,6 +686,2 @@ if (hook.length === 1) {

//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
//browser's fetch

@@ -803,20 +803,20 @@ const Native = windowRef.fetch;

//(not the best decision in hindsight)
const xhook$1 = hooks;
xhook$1.EventEmitter = EventEmitter;
const xhook = hooks;
xhook.EventEmitter = EventEmitter;
//modify hooks
xhook$1.before = function(handler, i) {
xhook.before = function(handler, i) {
if (handler.length < 1 || handler.length > 2) {
throw "invalid hook";
}
return xhook$1.on("before", handler, i);
return xhook.on("before", handler, i);
};
xhook$1.after = function(handler, i) {
xhook.after = function(handler, i) {
if (handler.length < 2 || handler.length > 3) {
throw "invalid hook";
}
return xhook$1.on("after", handler, i);
return xhook.on("after", handler, i);
};
//globally enable/disable
xhook$1.enable = function() {
xhook.enable = function() {
XMLHttpRequest.patch();

@@ -826,3 +826,3 @@ fetch.patch();

};
xhook$1.disable = function() {
xhook.disable = function() {
XMLHttpRequest.unpatch();

@@ -833,15 +833,15 @@ fetch.unpatch();

//expose native objects
xhook$1.XMLHttpRequest = XMLHttpRequest.Native;
xhook$1.fetch = fetch.Native;
xhook$1.FormData = FormData.Native;
xhook.XMLHttpRequest = XMLHttpRequest.Native;
xhook.fetch = fetch.Native;
xhook.FormData = FormData.Native;
//expose helpers
xhook$1.headers = headers.convert;
xhook.headers = headers.convert;
//enable by default
xhook$1.enable();
xhook.enable();
return xhook$1;
return xhook;
})();
//# sourceMappingURL=xhook.js.map

@@ -1,4 +0,4 @@

//XHook - v1.5.3 - https://github.com/jpillora/xhook
//XHook - v1.5.4 - https://github.com/jpillora/xhook
//Jaime Pillora <dev@jpillora.com> - MIT Copyright 2022
var xhook=function(){"use strict";Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(let t=0;t<this.length;t++){if(this[t]===e)return t}return-1});const e=(e,t)=>Array.prototype.slice.call(e,t);let t=null;"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?t=self:"undefined"!=typeof global?t=global:window&&(t=window);const n="undefined"!=typeof navigator&&navigator.useragent?navigator.userAgent:"";let o=null;(/msie (\d+)/.test(n.toLowerCase())||/trident\/.*; rv:(\d+)/.test(n.toLowerCase()))&&(o=parseInt(RegExp.$1,10));const r=t,s=t.document,a=["load","loadend","loadstart"],i=["progress","abort","error","timeout"],c=e=>["returnValue","totalSize","position"].includes(e),u=function(e,t){for(let n in e){if(c(n))continue;const o=e[n];try{t[n]=o}catch(e){}}return t},d=function(e,t,n){const o=e=>function(o){const r={};for(let e in o){if(c(e))continue;const s=o[e];r[e]=s===t?n:s}return n.dispatchEvent(e,r)};for(let r of Array.from(e))n._has(r)&&(t[`on${r}`]=o(r))},l=function(e){if(s&&null!=s.createEventObject){const t=s.createEventObject();return t.type=e,t}try{return new Event(e)}catch(t){return{type:e}}},f=function(t){let n={};const o=e=>n[e]||[],r={addEventListener:function(e,t,r){n[e]=o(e),n[e].indexOf(t)>=0||(r=void 0===r?n[e].length:r,n[e].splice(r,0,t))},removeEventListener:function(e,t){if(void 0===e)return void(n={});void 0===t&&(n[e]=[]);const r=o(e).indexOf(t);-1!==r&&o(e).splice(r,1)},dispatchEvent:function(){const n=e(arguments),s=n.shift();t||(n[0]=u(n[0],l(s)));const a=r[`on${s}`];a&&a.apply(r,n);const i=o(s).concat(o("*"));for(let e=0;e<i.length;e++){i[e].apply(r,n)}},_has:e=>!(!n[e]&&!r[`on${e}`])};return t&&(r.listeners=t=>e(o(t)),r.on=r.addEventListener,r.off=r.removeEventListener,r.fire=r.dispatchEvent,r.once=function(e,t){var n=function(){return r.off(e,n),t.apply(null,arguments)};return r.on(e,n)},r.destroy=()=>n={}),r};var p=function(e,t){let n;switch(null==t&&(t={}),typeof e){case"object":var o=[];for(let t in e){const r=e[t];n=t.toLowerCase(),o.push(`${n}:\t${r}`)}return o.join("\n")+"\n";case"string":o=e.split("\n");for(let e of Array.from(o))if(/([^:]+):\s*(.+)/.test(e)){n=null!=RegExp.$1?RegExp.$1.toLowerCase():void 0;const e=RegExp.$2;null==t[n]&&(t[n]=e)}return t}return[]},h=r.FormData;const y=function(t){this.fd=t?new h(t):new h,this.form=t;const n=[];Object.defineProperty(this,"entries",{get:()=>(t?e(t.querySelectorAll("input,select")).filter((e=>!["checkbox","radio"].includes(e.type)||e.checked)).map((e=>[e.name,"file"===e.type?e.files:e.value])):[]).concat(n)}),this.append=function(){const t=e(arguments);return n.push(t),this.fd.append.apply(this.fd,t)}.bind(this)};var v={patch(){h&&(r.FormData=y)},unpatch(){h&&(r.FormData=h)},Native:h,Xhook:y};const E=e=>void 0===e?null:e,g=r.XMLHttpRequest,m=function(){const e=new g,t={};let n,r,s,c=null;var l=0;const h=function(){if(s.status=c||e.status,-1===c&&o<10||(s.statusText=e.statusText),-1===c);else{const t=p(e.getAllResponseHeaders());for(let e in t){const n=t[e];if(!s.headers[e]){const t=e.toLowerCase();s.headers[t]=n}}}},y=function(){L.status=s.status,L.statusText=s.statusText},m=function(){n||L.dispatchEvent("load",{}),L.dispatchEvent("loadend",{}),n&&(L.readyState=0)},b=function(e){for(;e>l&&l<4;)L.readyState=++l,1===l&&L.dispatchEvent("loadstart",{}),2===l&&y(),4===l&&(y(),"text"in s&&(L.responseText=s.text),"xml"in s&&(L.responseXML=s.xml),"data"in s&&(L.response=s.data),"finalUrl"in s&&(L.responseURL=s.finalUrl)),L.dispatchEvent("readystatechange",{}),4===l&&(!1===t.async?m():setTimeout(m,0))},x=function(e){if(4!==e)return void b(e);const n=xhook.listeners("after");var o=function(){if(n.length>0){const e=n.shift();2===e.length?(e(t,s),o()):3===e.length&&t.async?e(t,s,o):o()}else b(4)};o()};var L=f();t.xhr=L,e.onreadystatechange=function(t){try{2===e.readyState&&h()}catch(e){}4===e.readyState&&(r=!1,h(),function(){if(e.responseType&&"text"!==e.responseType)"document"===e.responseType?(s.xml=e.responseXML,s.data=e.responseXML):s.data=e.response;else{s.text=e.responseText,s.data=e.responseText;try{s.xml=e.responseXML}catch(e){}}"responseURL"in e&&(s.finalUrl=e.responseURL)}()),x(e.readyState)};const w=function(){n=!0};L.addEventListener("error",w),L.addEventListener("timeout",w),L.addEventListener("abort",w),L.addEventListener("progress",(function(t){l<3?x(3):e.readyState<=3&&L.dispatchEvent("readystatechange",{})})),("withCredentials"in e||xhook.addWithCredentials)&&(L.withCredentials=!1),L.status=0;for(let e of Array.from(i.concat(a)))L[`on${e}`]=null;if(L.open=function(e,o,a,i,c){l=0,n=!1,r=!1,t.headers={},t.headerNames={},t.status=0,t.method=e,t.url=o,t.async=!1!==a,t.user=i,t.pass=c,s={},s.headers={},x(1)},L.send=function(n){let o,c;for(o of["type","timeout","withCredentials"])c="type"===o?"responseType":o,c in L&&(t[o]=L[c]);t.body=n;const l=xhook.listeners("before");var f=function(){if(!l.length)return function(){for(o of(d(i,e,L),L.upload&&d(i.concat(a),e.upload,L.upload),r=!0,e.open(t.method,t.url,t.async,t.user,t.pass),["type","timeout","withCredentials"]))c="type"===o?"responseType":o,o in t&&(e[c]=t[o]);for(let n in t.headers){const o=t.headers[n];n&&e.setRequestHeader(n,o)}t.body instanceof v.Xhook&&(t.body=t.body.fd),e.send(t.body)}();const n=function(e){if("object"==typeof e&&("number"==typeof e.status||"number"==typeof s.status))return u(e,s),"data"in e||(e.data=e.response||e.text),void x(4);f()};n.head=function(e){u(e,s),x(2)},n.progress=function(e){u(e,s),x(3)};const p=l.shift();1===p.length?n(p(t)):2===p.length&&t.async?p(t,n):n()};f()},L.abort=function(){c=-1,r?e.abort():L.dispatchEvent("abort",{})},L.setRequestHeader=function(e,n){const o=null!=e?e.toLowerCase():void 0,r=t.headerNames[o]=t.headerNames[o]||e;t.headers[r]&&(n=t.headers[r]+", "+n),t.headers[r]=n},L.getResponseHeader=e=>E(s.headers[e?e.toLowerCase():void 0]),L.getAllResponseHeaders=()=>E(p(s.headers)),e.overrideMimeType&&(L.overrideMimeType=function(){e.overrideMimeType.apply(e,arguments)}),e.upload){let e=f();L.upload=e,t.upload=e}return L.UNSENT=0,L.OPENED=1,L.HEADERS_RECEIVED=2,L.LOADING=3,L.DONE=4,L.response="",L.responseText="",L.responseXML=null,L.readyState=0,L.statusText="",L};m.UNSENT=0,m.OPENED=1,m.HEADERS_RECEIVED=2,m.LOADING=3,m.DONE=4;var b={patch(){g&&(r.XMLHttpRequest=m)},unpatch(){g&&(r.XMLHttpRequest=g)},Native:g,Xhook:m};const x=f(!0),L=r.fetch,w=function(e,t){null==t&&(t={headers:{}});let n=null;e instanceof Request?n=e:t.url=e;const o=x.listeners("before"),r=x.listeners("after");return new Promise((function(e,s){let a=e;const i=function(){return t.body instanceof v.Xhook&&(t.body=t.body.fd),t.headers&&(t.headers=new Headers(t.headers)),n||(n=new Request(t.url,t)),u(t,n)};var c=function(e){if(!r.length)return a(e);const t=r.shift();return 2===t.length?(t(i(),e),c(e)):3===t.length?t(i(),e,c):c(e)};const d=function(t){if(void 0!==t){const n=new Response(t.body||t.text,t);return e(n),void c(n)}l()};var l=function(){if(!o.length)return void f();const e=o.shift();return 1===e.length?d(e(t)):2===e.length?e(i(),d):void 0},f=()=>L(i()).then((e=>c(e))).catch((function(e){return a=s,c(e),s(e)}));l()}))};var R={patch(){L&&(r.fetch=w)},unpatch(){L&&(r.fetch=L)},Native:L,Xhook:w};const T=x;return T.EventEmitter=f,T.before=function(e,t){if(e.length<1||e.length>2)throw"invalid hook";return T.on("before",e,t)},T.after=function(e,t){if(e.length<2||e.length>3)throw"invalid hook";return T.on("after",e,t)},T.enable=function(){b.patch(),R.patch(),v.patch()},T.disable=function(){b.unpatch(),R.unpatch(),v.unpatch()},T.XMLHttpRequest=b.Native,T.fetch=R.Native,T.FormData=v.Native,T.headers=p,T.enable(),T}();
var xhook=function(){"use strict";Array.prototype.indexOf||(Array.prototype.indexOf=function(e){for(let t=0;t<this.length;t++){if(this[t]===e)return t}return-1});const e=(e,t)=>Array.prototype.slice.call(e,t);let t=null;"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?t=self:"undefined"!=typeof global?t=global:window&&(t=window);const n="undefined"!=typeof navigator&&navigator.useragent?navigator.userAgent:"";let o=null;(/msie (\d+)/.test(n.toLowerCase())||/trident\/.*; rv:(\d+)/.test(n.toLowerCase()))&&(o=parseInt(RegExp.$1,10));const r=t,s=t.document,a=["load","loadend","loadstart"],i=["progress","abort","error","timeout"],c=e=>["returnValue","totalSize","position"].includes(e),u=function(e,t){for(let n in e){if(c(n))continue;const o=e[n];try{t[n]=o}catch(e){}}return t},d=function(e,t,n){const o=e=>function(o){const r={};for(let e in o){if(c(e))continue;const s=o[e];r[e]=s===t?n:s}return n.dispatchEvent(e,r)};for(let r of Array.from(e))n._has(r)&&(t[`on${r}`]=o(r))},l=function(e){if(s&&null!=s.createEventObject){const t=s.createEventObject();return t.type=e,t}try{return new Event(e)}catch(t){return{type:e}}},f=function(t){let n={};const o=e=>n[e]||[],r={addEventListener:function(e,t,r){n[e]=o(e),n[e].indexOf(t)>=0||(r=void 0===r?n[e].length:r,n[e].splice(r,0,t))},removeEventListener:function(e,t){if(void 0===e)return void(n={});void 0===t&&(n[e]=[]);const r=o(e).indexOf(t);-1!==r&&o(e).splice(r,1)},dispatchEvent:function(){const n=e(arguments),s=n.shift();t||(n[0]=u(n[0],l(s)));const a=r[`on${s}`];a&&a.apply(r,n);const i=o(s).concat(o("*"));for(let e=0;e<i.length;e++){i[e].apply(r,n)}},_has:e=>!(!n[e]&&!r[`on${e}`])};return t&&(r.listeners=t=>e(o(t)),r.on=r.addEventListener,r.off=r.removeEventListener,r.fire=r.dispatchEvent,r.once=function(e,t){var n=function(){return r.off(e,n),t.apply(null,arguments)};return r.on(e,n)},r.destroy=()=>n={}),r};var p=function(e,t){let n;switch(null==t&&(t={}),typeof e){case"object":var o=[];for(let t in e){const r=e[t];n=t.toLowerCase(),o.push(`${n}:\t${r}`)}return o.join("\n")+"\n";case"string":o=e.split("\n");for(let e of Array.from(o))if(/([^:]+):\s*(.+)/.test(e)){n=null!=RegExp.$1?RegExp.$1.toLowerCase():void 0;const e=RegExp.$2;null==t[n]&&(t[n]=e)}return t}return[]},h=r.FormData;const y=function(t){this.fd=t?new h(t):new h,this.form=t;const n=[];Object.defineProperty(this,"entries",{get:()=>(t?e(t.querySelectorAll("input,select")).filter((e=>!["checkbox","radio"].includes(e.type)||e.checked)).map((e=>[e.name,"file"===e.type?e.files:e.value])):[]).concat(n)}),this.append=function(){const t=e(arguments);return n.push(t),this.fd.append.apply(this.fd,t)}.bind(this)};var v={patch(){h&&(r.FormData=y)},unpatch(){h&&(r.FormData=h)},Native:h,Xhook:y};const E=f(!0),g=e=>void 0===e?null:e,m=r.XMLHttpRequest,b=function(){const e=new m,t={};let n,r,s,c=null;var l=0;const h=function(){if(s.status=c||e.status,-1===c&&o<10||(s.statusText=e.statusText),-1===c);else{const t=p(e.getAllResponseHeaders());for(let e in t){const n=t[e];if(!s.headers[e]){const t=e.toLowerCase();s.headers[t]=n}}}},y=function(){w.status=s.status,w.statusText=s.statusText},b=function(){n||w.dispatchEvent("load",{}),w.dispatchEvent("loadend",{}),n&&(w.readyState=0)},x=function(e){for(;e>l&&l<4;)w.readyState=++l,1===l&&w.dispatchEvent("loadstart",{}),2===l&&y(),4===l&&(y(),"text"in s&&(w.responseText=s.text),"xml"in s&&(w.responseXML=s.xml),"data"in s&&(w.response=s.data),"finalUrl"in s&&(w.responseURL=s.finalUrl)),w.dispatchEvent("readystatechange",{}),4===l&&(!1===t.async?b():setTimeout(b,0))},L=function(e){if(4!==e)return void x(e);const n=E.listeners("after");var o=function(){if(n.length>0){const e=n.shift();2===e.length?(e(t,s),o()):3===e.length&&t.async?e(t,s,o):o()}else x(4)};o()};var w=f();t.xhr=w,e.onreadystatechange=function(t){try{2===e.readyState&&h()}catch(e){}4===e.readyState&&(r=!1,h(),function(){if(e.responseType&&"text"!==e.responseType)"document"===e.responseType?(s.xml=e.responseXML,s.data=e.responseXML):s.data=e.response;else{s.text=e.responseText,s.data=e.responseText;try{s.xml=e.responseXML}catch(e){}}"responseURL"in e&&(s.finalUrl=e.responseURL)}()),L(e.readyState)};const R=function(){n=!0};w.addEventListener("error",R),w.addEventListener("timeout",R),w.addEventListener("abort",R),w.addEventListener("progress",(function(t){l<3?L(3):e.readyState<=3&&w.dispatchEvent("readystatechange",{})})),"withCredentials"in e&&(w.withCredentials=!1),w.status=0;for(let e of Array.from(i.concat(a)))w[`on${e}`]=null;if(w.open=function(e,o,a,i,c){l=0,n=!1,r=!1,t.headers={},t.headerNames={},t.status=0,t.method=e,t.url=o,t.async=!1!==a,t.user=i,t.pass=c,s={},s.headers={},L(1)},w.send=function(n){let o,c;for(o of["type","timeout","withCredentials"])c="type"===o?"responseType":o,c in w&&(t[o]=w[c]);t.body=n;const l=E.listeners("before");var f=function(){if(!l.length)return function(){for(o of(d(i,e,w),w.upload&&d(i.concat(a),e.upload,w.upload),r=!0,e.open(t.method,t.url,t.async,t.user,t.pass),["type","timeout","withCredentials"]))c="type"===o?"responseType":o,o in t&&(e[c]=t[o]);for(let n in t.headers){const o=t.headers[n];n&&e.setRequestHeader(n,o)}t.body instanceof v.Xhook&&(t.body=t.body.fd),e.send(t.body)}();const n=function(e){if("object"==typeof e&&("number"==typeof e.status||"number"==typeof s.status))return u(e,s),"data"in e||(e.data=e.response||e.text),void L(4);f()};n.head=function(e){u(e,s),L(2)},n.progress=function(e){u(e,s),L(3)};const p=l.shift();1===p.length?n(p(t)):2===p.length&&t.async?p(t,n):n()};f()},w.abort=function(){c=-1,r?e.abort():w.dispatchEvent("abort",{})},w.setRequestHeader=function(e,n){const o=null!=e?e.toLowerCase():void 0,r=t.headerNames[o]=t.headerNames[o]||e;t.headers[r]&&(n=t.headers[r]+", "+n),t.headers[r]=n},w.getResponseHeader=e=>g(s.headers[e?e.toLowerCase():void 0]),w.getAllResponseHeaders=()=>g(p(s.headers)),e.overrideMimeType&&(w.overrideMimeType=function(){e.overrideMimeType.apply(e,arguments)}),e.upload){let e=f();w.upload=e,t.upload=e}return w.UNSENT=0,w.OPENED=1,w.HEADERS_RECEIVED=2,w.LOADING=3,w.DONE=4,w.response="",w.responseText="",w.responseXML=null,w.readyState=0,w.statusText="",w};b.UNSENT=0,b.OPENED=1,b.HEADERS_RECEIVED=2,b.LOADING=3,b.DONE=4;var x={patch(){m&&(r.XMLHttpRequest=b)},unpatch(){m&&(r.XMLHttpRequest=m)},Native:m,Xhook:b};const L=r.fetch,w=function(e,t){null==t&&(t={headers:{}});let n=null;e instanceof Request?n=e:t.url=e;const o=E.listeners("before"),r=E.listeners("after");return new Promise((function(e,s){let a=e;const i=function(){return t.body instanceof v.Xhook&&(t.body=t.body.fd),t.headers&&(t.headers=new Headers(t.headers)),n||(n=new Request(t.url,t)),u(t,n)};var c=function(e){if(!r.length)return a(e);const t=r.shift();return 2===t.length?(t(i(),e),c(e)):3===t.length?t(i(),e,c):c(e)};const d=function(t){if(void 0!==t){const n=new Response(t.body||t.text,t);return e(n),void c(n)}l()};var l=function(){if(!o.length)return void f();const e=o.shift();return 1===e.length?d(e(t)):2===e.length?e(i(),d):void 0},f=()=>L(i()).then((e=>c(e))).catch((function(e){return a=s,c(e),s(e)}));l()}))};var R={patch(){L&&(r.fetch=w)},unpatch(){L&&(r.fetch=L)},Native:L,Xhook:w};const T=E;return T.EventEmitter=f,T.before=function(e,t){if(e.length<1||e.length>2)throw"invalid hook";return T.on("before",e,t)},T.after=function(e,t){if(e.length<2||e.length>3)throw"invalid hook";return T.on("after",e,t)},T.enable=function(){x.patch(),R.patch(),v.patch()},T.disable=function(){x.unpatch(),R.unpatch(),v.unpatch()},T.XMLHttpRequest=x.Native,T.fetch=R.Native,T.FormData=v.Native,T.headers=p,T.enable(),T}();
//# sourceMappingURL=xhook.min.js.map

@@ -255,2 +255,6 @@ //if required, add 'indexOf' method to Array

//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
const nullify = res => (res === undefined ? null : res);

@@ -390,7 +394,7 @@

//before emitting 4, run all 'after' hooks in sequence
const hooks = xhook.listeners("after");
const afterHooks = hooks.listeners("after");
var process = function() {
if (hooks.length > 0) {
if (afterHooks.length > 0) {
//execute each 'before' hook one at a time
const hook = hooks.shift();
const hook = afterHooks.shift();
if (hook.length === 2) {

@@ -455,3 +459,3 @@ hook(request, response);

// or if explicitly told to do so
if ("withCredentials" in xhr || xhook.addWithCredentials) {
if ("withCredentials" in xhr) {
facade.withCredentials = false;

@@ -544,6 +548,6 @@ }

const hooks = xhook.listeners("before");
//process hooks sequentially
const beforeHooks = hooks.listeners("before");
//process beforeHooks sequentially
var process = function() {
if (!hooks.length) {
if (!beforeHooks.length) {
return send();

@@ -566,3 +570,3 @@ }

}
//continue processing until no hooks left
//continue processing until no beforeHooks left
process();

@@ -581,3 +585,3 @@ };

const hook = hooks.shift();
const hook = beforeHooks.shift();
//async or sync?

@@ -677,6 +681,2 @@ if (hook.length === 1) {

//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
//browser's fetch

@@ -798,20 +798,20 @@ const Native = windowRef.fetch;

//(not the best decision in hindsight)
const xhook$1 = hooks;
xhook$1.EventEmitter = EventEmitter;
const xhook = hooks;
xhook.EventEmitter = EventEmitter;
//modify hooks
xhook$1.before = function(handler, i) {
xhook.before = function(handler, i) {
if (handler.length < 1 || handler.length > 2) {
throw "invalid hook";
}
return xhook$1.on("before", handler, i);
return xhook.on("before", handler, i);
};
xhook$1.after = function(handler, i) {
xhook.after = function(handler, i) {
if (handler.length < 2 || handler.length > 3) {
throw "invalid hook";
}
return xhook$1.on("after", handler, i);
return xhook.on("after", handler, i);
};
//globally enable/disable
xhook$1.enable = function() {
xhook.enable = function() {
XMLHttpRequest.patch();

@@ -821,3 +821,3 @@ fetch.patch();

};
xhook$1.disable = function() {
xhook.disable = function() {
XMLHttpRequest.unpatch();

@@ -828,12 +828,12 @@ fetch.unpatch();

//expose native objects
xhook$1.XMLHttpRequest = XMLHttpRequest.Native;
xhook$1.fetch = fetch.Native;
xhook$1.FormData = FormData.Native;
xhook.XMLHttpRequest = XMLHttpRequest.Native;
xhook.fetch = fetch.Native;
xhook.FormData = FormData.Native;
//expose helpers
xhook$1.headers = headers.convert;
xhook.headers = headers.convert;
//enable by default
xhook$1.enable();
xhook.enable();
export { xhook$1 as default };
export { xhook as default };

@@ -257,2 +257,6 @@ 'use strict';

//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
const nullify = res => (res === undefined ? null : res);

@@ -392,7 +396,7 @@

//before emitting 4, run all 'after' hooks in sequence
const hooks = xhook.listeners("after");
const afterHooks = hooks.listeners("after");
var process = function() {
if (hooks.length > 0) {
if (afterHooks.length > 0) {
//execute each 'before' hook one at a time
const hook = hooks.shift();
const hook = afterHooks.shift();
if (hook.length === 2) {

@@ -457,3 +461,3 @@ hook(request, response);

// or if explicitly told to do so
if ("withCredentials" in xhr || xhook.addWithCredentials) {
if ("withCredentials" in xhr) {
facade.withCredentials = false;

@@ -546,6 +550,6 @@ }

const hooks = xhook.listeners("before");
//process hooks sequentially
const beforeHooks = hooks.listeners("before");
//process beforeHooks sequentially
var process = function() {
if (!hooks.length) {
if (!beforeHooks.length) {
return send();

@@ -568,3 +572,3 @@ }

}
//continue processing until no hooks left
//continue processing until no beforeHooks left
process();

@@ -583,3 +587,3 @@ };

const hook = hooks.shift();
const hook = beforeHooks.shift();
//async or sync?

@@ -679,6 +683,2 @@ if (hook.length === 1) {

//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
//browser's fetch

@@ -800,20 +800,20 @@ const Native = windowRef.fetch;

//(not the best decision in hindsight)
const xhook$1 = hooks;
xhook$1.EventEmitter = EventEmitter;
const xhook = hooks;
xhook.EventEmitter = EventEmitter;
//modify hooks
xhook$1.before = function(handler, i) {
xhook.before = function(handler, i) {
if (handler.length < 1 || handler.length > 2) {
throw "invalid hook";
}
return xhook$1.on("before", handler, i);
return xhook.on("before", handler, i);
};
xhook$1.after = function(handler, i) {
xhook.after = function(handler, i) {
if (handler.length < 2 || handler.length > 3) {
throw "invalid hook";
}
return xhook$1.on("after", handler, i);
return xhook.on("after", handler, i);
};
//globally enable/disable
xhook$1.enable = function() {
xhook.enable = function() {
XMLHttpRequest.patch();

@@ -823,3 +823,3 @@ fetch.patch();

};
xhook$1.disable = function() {
xhook.disable = function() {
XMLHttpRequest.unpatch();

@@ -830,12 +830,12 @@ fetch.unpatch();

//expose native objects
xhook$1.XMLHttpRequest = XMLHttpRequest.Native;
xhook$1.fetch = fetch.Native;
xhook$1.FormData = FormData.Native;
xhook.XMLHttpRequest = XMLHttpRequest.Native;
xhook.fetch = fetch.Native;
xhook.FormData = FormData.Native;
//expose helpers
xhook$1.headers = headers.convert;
xhook.headers = headers.convert;
//enable by default
xhook$1.enable();
xhook.enable();
module.exports = xhook$1;
module.exports = xhook;
{
"name": "xhook",
"version": "1.5.3",
"version": "1.5.4",
"description": "Easily intercept and modify XHR request and response",

@@ -5,0 +5,0 @@ "main": "lib/main.js",

@@ -29,30 +29,6 @@ # XHook

## Future Features
* Add BrowserSwarm or TestlingCI automated cross-browser tests
*Tip: See [CONTRIBUTING.md](CONTRIBUTING.md) for steps on how to contribute* :wink:
## Example
Here, we're converting vowels to **z**'s in all requests to 'example.txt':
``` javascript
//modify 'responseText' of 'example2.txt'
xhook.after(function(request, response) {
if(request.url.match(/example\.txt$/))
response.text = response.text.replace(/[aeiou]/g,'z');
});
```
## Browser Support
Tested in IE8+, Chrome, Firefox, Safari
Support Modern Browser.
#### *Run test suite here: http://jpillora.com/xhook/test*
<!--
[![browser support](https://ci.testling.com/jpillora/xhook.png)](https://ci.testling.com/jpillora/xhook)
-->
## Demos

@@ -66,6 +42,13 @@

Using `script` link to load xhook and use it, like so:
``` html
<script src="//unpkg.com/xhook@latest/dist/xhook.min.js"></script>
<script>
xhook.after(function(request, response) {
if(request.url.match(/example\.txt$/))
response.text = response.text.replace(/[aeiou]/g,'z');
});
</script>
```
npm install xhook
```
* Development [xhook.js](https://jpillora.com/xhook/dist/xhook.js)

@@ -75,6 +58,18 @@ * Production [xhook.min.js](https://jpillora.com/xhook/dist/xhook.min.js)

``` html
<script src="//unpkg.com/xhook@latest/dist/xhook.min.js"></script>
```
We can also install xhook via npm.
```bash
npm install xhook
```
Then use ESM syntax to load xhook.
```js
import xhook from 'xhook';
//modify 'responseText' of 'example2.txt'
xhook.after(function(request, response) {
if(request.url.match(/example\.txt$/))
response.text = response.text.replace(/[aeiou]/g,'z');
});
```
## API

@@ -125,3 +120,3 @@

### Overview
## Overview

@@ -134,3 +129,3 @@ <img src="https://docs.google.com/drawings/d/1PTxHDqdW9iNqagDwtaO0ggXZkJp7ILiRDVWAMHInFGQ/pub?w=498&amp;h=235">

### Reference
## Reference

@@ -143,3 +138,3 @@ https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

### Issues
## Issues

@@ -156,29 +151,4 @@ * XHook does **not** attempt to resolve any browser compatibility issues. Libraries like jQuery

Contributors:
## License
* Jaime Pillora @jpillora <dev@jpillora.com>
* Daniel Gasienica <daniel@gasienica.ch>
* Maayan Glikser <maayan@glikm.com>
* @TrickyPi
#### MIT License
Copyright © 2022 Jaime Pillora <dev@jpillora.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[MIT](LICENSE) License Copyright © 2022 Jaime Pillora dev@jpillora.com

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet