-
Notifications
You must be signed in to change notification settings - Fork 158
/
xhook.js
822 lines (752 loc) · 24.1 KB
/
xhook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//XHook - v1.6.2 - https://github.com/jpillora/xhook
//Jaime Pillora <dev@jpillora.com> - MIT Copyright 2023
var xhook = (function () {
'use strict';
const slice = (o, n) => Array.prototype.slice.call(o, n);
let result = null;
//find global object
if (
typeof WorkerGlobalScope !== "undefined" &&
self instanceof WorkerGlobalScope
) {
result = self;
} else if (typeof global !== "undefined") {
result = global;
} else if (window) {
result = window;
}
const windowRef = result;
const documentRef = result.document;
const UPLOAD_EVENTS = ["load", "loadend", "loadstart"];
const COMMON_EVENTS = ["progress", "abort", "error", "timeout"];
const depricatedProp = p =>
["returnValue", "totalSize", "position"].includes(p);
const mergeObjects = function (src, dst) {
for (let k in src) {
if (depricatedProp(k)) {
continue;
}
const v = src[k];
try {
dst[k] = v;
} catch (error) {}
}
return dst;
};
//proxy events from one emitter to another
const proxyEvents = function (events, src, dst) {
const p = event =>
function (e) {
const clone = {};
//copies event, with dst emitter inplace of src
for (let k in e) {
if (depricatedProp(k)) {
continue;
}
const val = e[k];
clone[k] = val === src ? dst : val;
}
//emits out the dst
return dst.dispatchEvent(event, clone);
};
//dont proxy manual events
for (let event of Array.from(events)) {
if (dst._has(event)) {
src[`on${event}`] = p(event);
}
}
};
//create fake event
const fakeEvent = function (type) {
if (documentRef && documentRef.createEventObject != null) {
const msieEventObject = documentRef.createEventObject();
msieEventObject.type = type;
return msieEventObject;
}
// on some platforms like android 4.1.2 and safari on windows, it appears
// that new Event is not allowed
try {
return new Event(type);
} catch (error) {
return { type };
}
};
//tiny event emitter
const EventEmitter = function (nodeStyle) {
//private
let events = {};
const listeners = event => events[event] || [];
//public
const emitter = {};
emitter.addEventListener = function (event, callback, i) {
events[event] = listeners(event);
if (events[event].indexOf(callback) >= 0) {
return;
}
i = i === undefined ? events[event].length : i;
events[event].splice(i, 0, callback);
};
emitter.removeEventListener = function (event, callback) {
//remove all
if (event === undefined) {
events = {};
return;
}
//remove all of type event
if (callback === undefined) {
events[event] = [];
}
//remove particular handler
const i = listeners(event).indexOf(callback);
if (i === -1) {
return;
}
listeners(event).splice(i, 1);
};
emitter.dispatchEvent = function () {
const args = slice(arguments);
const event = args.shift();
if (!nodeStyle) {
args[0] = mergeObjects(args[0], fakeEvent(event));
Object.defineProperty(args[0], "target", {
writable: false,
value: this,
});
}
const legacylistener = emitter[`on${event}`];
if (legacylistener) {
legacylistener.apply(emitter, args);
}
const iterable = listeners(event).concat(listeners("*"));
for (let i = 0; i < iterable.length; i++) {
const listener = iterable[i];
listener.apply(emitter, args);
}
};
emitter._has = event => !!(events[event] || emitter[`on${event}`]);
//add extra aliases
if (nodeStyle) {
emitter.listeners = event => slice(listeners(event));
emitter.on = emitter.addEventListener;
emitter.off = emitter.removeEventListener;
emitter.fire = emitter.dispatchEvent;
emitter.once = function (e, fn) {
var fire = function () {
emitter.off(e, fire);
return fn.apply(null, arguments);
};
return emitter.on(e, fire);
};
emitter.destroy = () => (events = {});
}
return emitter;
};
//helper
const CRLF = "\r\n";
const objectToString = function (headersObj) {
const entries = Object.entries(headersObj);
const headers = entries.map(([name, value]) => {
return `${name.toLowerCase()}: ${value}`;
});
return headers.join(CRLF);
};
const stringToObject = function (headersString, dest) {
const headers = headersString.split(CRLF);
if (dest == null) {
dest = {};
}
for (let header of headers) {
if (/([^:]+):\s*(.+)/.test(header)) {
const name = RegExp.$1 != null ? RegExp.$1.toLowerCase() : undefined;
const value = RegExp.$2;
if (dest[name] == null) {
dest[name] = value;
}
}
}
return dest;
};
const convert = function (headers, dest) {
switch (typeof headers) {
case "object": {
return objectToString(headers);
}
case "string": {
return stringToObject(headers, dest);
}
}
return [];
};
var headers = { convert };
//global set of hook functions,
//uses event emitter to store hooks
const hooks = EventEmitter(true);
const nullify = res => (res === undefined ? null : res);
//browser's XMLHttpRequest
const Native$1 = windowRef.XMLHttpRequest;
//xhook's XMLHttpRequest
const Xhook$1 = function () {
const ABORTED = -1;
const xhr = new Native$1();
//==========================
// Extra state
const request = {};
let status = null;
let hasError = undefined;
let transiting = undefined;
let response = undefined;
var currentState = 0;
//==========================
// Private API
//read results from real xhr into response
const readHead = function () {
// Accessing attributes on an aborted xhr object will
// throw an 'c00c023f error' in IE9 and lower, don't touch it.
response.status = status || xhr.status;
if (status !== ABORTED) {
response.statusText = xhr.statusText;
}
if (status !== ABORTED) {
const object = headers.convert(xhr.getAllResponseHeaders());
for (let key in object) {
const val = object[key];
if (!response.headers[key]) {
const name = key.toLowerCase();
response.headers[name] = val;
}
}
return;
}
};
const readBody = function () {
//https://xhr.spec.whatwg.org/
if (!xhr.responseType || xhr.responseType === "text") {
response.text = xhr.responseText;
response.data = xhr.responseText;
try {
response.xml = xhr.responseXML;
} catch (error) {}
// unable to set responseXML due to response type, we attempt to assign responseXML
// when the type is text even though it's against the spec due to several libraries
// and browser vendors who allow this behavior. causing these requests to fail when
// xhook is installed on a page.
} else if (xhr.responseType === "document") {
response.xml = xhr.responseXML;
response.data = xhr.responseXML;
} else {
response.data = xhr.response;
}
//new in some browsers
if ("responseURL" in xhr) {
response.finalUrl = xhr.responseURL;
}
};
//write response into facade xhr
const writeHead = function () {
facade.status = response.status;
facade.statusText = response.statusText;
};
const writeBody = function () {
if ("text" in response) {
facade.responseText = response.text;
}
if ("xml" in response) {
facade.responseXML = response.xml;
}
if ("data" in response) {
facade.response = response.data;
}
if ("finalUrl" in response) {
facade.responseURL = response.finalUrl;
}
};
const emitFinal = function () {
if (!hasError) {
facade.dispatchEvent("load", {});
}
facade.dispatchEvent("loadend", {});
if (hasError) {
facade.readyState = 0;
}
};
//ensure ready state 0 through 4 is handled
const emitReadyState = function (n) {
while (n > currentState && currentState < 4) {
facade.readyState = ++currentState;
// make fake events for libraries that actually check the type on
// the event object
if (currentState === 1) {
facade.dispatchEvent("loadstart", {});
}
if (currentState === 2) {
writeHead();
}
if (currentState === 4) {
writeHead();
writeBody();
}
facade.dispatchEvent("readystatechange", {});
//delay final events incase of error
if (currentState === 4) {
if (request.async === false) {
emitFinal();
} else {
setTimeout(emitFinal, 0);
}
}
}
};
//control facade ready state
const setReadyState = function (n) {
//emit events until readyState reaches 4
if (n !== 4) {
emitReadyState(n);
return;
}
//before emitting 4, run all 'after' hooks in sequence
const afterHooks = hooks.listeners("after");
var process = function () {
if (afterHooks.length > 0) {
//execute each 'before' hook one at a time
const hook = afterHooks.shift();
if (hook.length === 2) {
hook(request, response);
process();
} else if (hook.length === 3 && request.async) {
hook(request, response, process);
} else {
process();
}
} else {
//response ready for reading
emitReadyState(4);
}
return;
};
process();
};
//==========================
// Facade XHR
var facade = EventEmitter();
request.xhr = facade;
// Handle the underlying ready state
xhr.onreadystatechange = function (event) {
//pull status and headers
try {
if (xhr.readyState === 2) {
readHead();
}
} catch (error) {}
//pull response data
if (xhr.readyState === 4) {
transiting = false;
readHead();
readBody();
}
setReadyState(xhr.readyState);
};
//mark this xhr as errored
const hasErrorHandler = function () {
hasError = true;
};
facade.addEventListener("error", hasErrorHandler);
facade.addEventListener("timeout", hasErrorHandler);
facade.addEventListener("abort", hasErrorHandler);
// progress means we're current downloading...
facade.addEventListener("progress", function (event) {
if (currentState < 3) {
setReadyState(3);
} else if (xhr.readyState <= 3) {
//until ready (4), each progress event is followed by readystatechange...
facade.dispatchEvent("readystatechange", {}); //TODO fake an XHR event
}
});
// initialise 'withCredentials' on facade xhr in browsers with it
// or if explicitly told to do so
if ("withCredentials" in xhr) {
facade.withCredentials = false;
}
facade.status = 0;
// initialise all possible event handlers
for (let event of Array.from(COMMON_EVENTS.concat(UPLOAD_EVENTS))) {
facade[`on${event}`] = null;
}
facade.open = function (method, url, async, user, pass) {
// Initailize empty XHR facade
currentState = 0;
hasError = false;
transiting = false;
//reset request
request.headers = {};
request.headerNames = {};
request.status = 0;
request.method = method;
request.url = url;
request.async = async !== false;
request.user = user;
request.pass = pass;
//reset response
response = {};
response.headers = {};
// openned facade xhr (not real xhr)
setReadyState(1);
};
facade.send = function (body) {
//read xhr settings before hooking
let k, modk;
for (k of ["type", "timeout", "withCredentials"]) {
modk = k === "type" ? "responseType" : k;
if (modk in facade) {
request[k] = facade[modk];
}
}
request.body = body;
const send = function () {
//proxy all events from real xhr to facade
proxyEvents(COMMON_EVENTS, xhr, facade);
//proxy all upload events from the real to the upload facade
if (facade.upload) {
proxyEvents(
COMMON_EVENTS.concat(UPLOAD_EVENTS),
xhr.upload,
facade.upload
);
}
//prepare request all at once
transiting = true;
//perform open
xhr.open(
request.method,
request.url,
request.async,
request.user,
request.pass
);
//write xhr settings
for (k of ["type", "timeout", "withCredentials"]) {
modk = k === "type" ? "responseType" : k;
if (k in request) {
xhr[modk] = request[k];
}
}
//insert headers
for (let header in request.headers) {
const value = request.headers[header];
if (header) {
xhr.setRequestHeader(header, value);
}
}
//real send!
xhr.send(request.body);
};
const beforeHooks = hooks.listeners("before");
//process beforeHooks sequentially
var process = function () {
if (!beforeHooks.length) {
return send();
}
//go to next hook OR optionally provide response
const done = function (userResponse) {
//break chain - provide dummy response (readyState 4)
if (
typeof userResponse === "object" &&
(typeof userResponse.status === "number" ||
typeof response.status === "number")
) {
mergeObjects(userResponse, response);
if (!("data" in userResponse)) {
userResponse.data = userResponse.response || userResponse.text;
}
setReadyState(4);
return;
}
//continue processing until no beforeHooks left
process();
};
//specifically provide headers (readyState 2)
done.head = function (userResponse) {
mergeObjects(userResponse, response);
setReadyState(2);
};
//specifically provide partial text (responseText readyState 3)
done.progress = function (userResponse) {
mergeObjects(userResponse, response);
setReadyState(3);
};
const hook = beforeHooks.shift();
//async or sync?
if (hook.length === 1) {
done(hook(request));
} else if (hook.length === 2 && request.async) {
//async handlers must use an async xhr
hook(request, done);
} else {
//skip async hook on sync requests
done();
}
return;
};
//kick off
process();
};
facade.abort = function () {
status = ABORTED;
if (transiting) {
xhr.abort(); //this will emit an 'abort' for us
} else {
facade.dispatchEvent("abort", {});
}
};
facade.setRequestHeader = function (header, value) {
//the first header set is used for all future case-alternatives of 'name'
const lName = header != null ? header.toLowerCase() : undefined;
const name = (request.headerNames[lName] =
request.headerNames[lName] || header);
//append header to any previous values
if (request.headers[name]) {
value = request.headers[name] + ", " + value;
}
request.headers[name] = value;
};
facade.getResponseHeader = header =>
nullify(response.headers[header ? header.toLowerCase() : undefined]);
facade.getAllResponseHeaders = () =>
nullify(headers.convert(response.headers));
//proxy call only when supported
if (xhr.overrideMimeType) {
facade.overrideMimeType = function () {
xhr.overrideMimeType.apply(xhr, arguments);
};
}
//create emitter when supported
if (xhr.upload) {
let up = EventEmitter();
facade.upload = up;
request.upload = up;
}
facade.UNSENT = 0;
facade.OPENED = 1;
facade.HEADERS_RECEIVED = 2;
facade.LOADING = 3;
facade.DONE = 4;
// fill in default values for an empty XHR object according to the spec
facade.response = "";
facade.responseText = "";
facade.responseXML = null;
facade.readyState = 0;
facade.statusText = "";
return facade;
};
Xhook$1.UNSENT = 0;
Xhook$1.OPENED = 1;
Xhook$1.HEADERS_RECEIVED = 2;
Xhook$1.LOADING = 3;
Xhook$1.DONE = 4;
//patch interface
var XMLHttpRequest = {
patch() {
if (Native$1) {
windowRef.XMLHttpRequest = Xhook$1;
}
},
unpatch() {
if (Native$1) {
windowRef.XMLHttpRequest = Native$1;
}
},
Native: Native$1,
Xhook: Xhook$1,
};
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
//browser's fetch
const Native = windowRef.fetch;
function copyToObjFromRequest(req) {
const copyedKeys = [
"method",
"headers",
"body",
"mode",
"credentials",
"cache",
"redirect",
"referrer",
"referrerPolicy",
"integrity",
"keepalive",
"signal",
"url",
];
let copyedObj = {};
copyedKeys.forEach(key => (copyedObj[key] = req[key]));
return copyedObj;
}
function covertHeaderToPlainObj(headers) {
if (headers instanceof Headers) {
return covertTDAarryToObj([...headers.entries()]);
}
if (Array.isArray(headers)) {
return covertTDAarryToObj(headers);
}
return headers;
}
function covertTDAarryToObj(input) {
return input.reduce((prev, [key, value]) => {
prev[key] = value;
return prev;
}, {});
}
/**
* if fetch(hacked by Xhook) accept a Request as a first parameter, it will be destrcuted to a plain object.
* Finally the whole network request was convert to fectch(Request.url, other options)
*/
const Xhook = function (input, init = { headers: {} }) {
let options = Object.assign(Object.assign({}, init), { isFetch: true });
if (input instanceof Request) {
const requestObj = copyToObjFromRequest(input);
const prevHeaders = Object.assign(Object.assign({}, covertHeaderToPlainObj(requestObj.headers)), covertHeaderToPlainObj(options.headers));
options = Object.assign(Object.assign(Object.assign({}, requestObj), init), { headers: prevHeaders, acceptedRequest: true });
}
else {
options.url = input;
}
const beforeHooks = hooks.listeners("before");
const afterHooks = hooks.listeners("after");
return new Promise(function (resolve, reject) {
let fullfiled = resolve;
const processAfter = function (response) {
if (!afterHooks.length) {
return fullfiled(response);
}
const hook = afterHooks.shift();
if (hook.length === 2) {
hook(options, response);
return processAfter(response);
}
else if (hook.length === 3) {
return hook(options, response, processAfter);
}
else {
return processAfter(response);
}
};
const done = function (userResponse) {
if (userResponse !== undefined) {
const response = new Response(userResponse.body || userResponse.text, userResponse);
resolve(response);
processAfter(response);
return;
}
//continue processing until no hooks left
processBefore();
};
const processBefore = function () {
if (!beforeHooks.length) {
send();
return;
}
const hook = beforeHooks.shift();
if (hook.length === 1) {
return done(hook(options));
}
else if (hook.length === 2) {
return hook(options, done);
}
};
const send = () => __awaiter(this, void 0, void 0, function* () {
const { url, isFetch, acceptedRequest } = options, restInit = __rest(options, ["url", "isFetch", "acceptedRequest"]);
if (input instanceof Request && restInit.body instanceof ReadableStream) {
restInit.body = yield new Response(restInit.body).text();
}
return Native(url, restInit)
.then(response => processAfter(response))
.catch(function (err) {
fullfiled = reject;
processAfter(err);
return reject(err);
});
});
processBefore();
});
};
//patch interface
var fetch = {
patch() {
if (Native) {
windowRef.fetch = Xhook;
}
},
unpatch() {
if (Native) {
windowRef.fetch = Native;
}
},
Native,
Xhook,
};
//the global hooks event emitter is also the global xhook object
//(not the best decision in hindsight)
const xhook = hooks;
xhook.EventEmitter = EventEmitter;
//modify hooks
xhook.before = function (handler, i) {
if (handler.length < 1 || handler.length > 2) {
throw "invalid hook";
}
return xhook.on("before", handler, i);
};
xhook.after = function (handler, i) {
if (handler.length < 2 || handler.length > 3) {
throw "invalid hook";
}
return xhook.on("after", handler, i);
};
//globally enable/disable
xhook.enable = function () {
XMLHttpRequest.patch();
fetch.patch();
};
xhook.disable = function () {
XMLHttpRequest.unpatch();
fetch.unpatch();
};
//expose native objects
xhook.XMLHttpRequest = XMLHttpRequest.Native;
xhook.fetch = fetch.Native;
//expose helpers
xhook.headers = headers.convert;
//enable by default
xhook.enable();
return xhook;
})();
//# sourceMappingURL=xhook.js.map