Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ludicrous
Path: blob/main/corrosion/lib/browser/http.js
1223 views
1
function createHttpRewriter(ctx = {}) {
2
return function rewriteHttp() {
3
if (ctx.window.Request) {
4
const requestURL = Object.getOwnPropertyDescriptor(ctx.window.Request.prototype, 'url');
5
ctx.window.Request = new Proxy(ctx.window.Request, {
6
construct(target, args) {
7
if (args[0]) args[0] = ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], })
8
return Reflect.construct(target, args);
9
},
10
});
11
Object.defineProperty(ctx.window.Request.prototype, 'url', {
12
get: new Proxy(requestURL.get, {
13
apply: (target, that, args) => {
14
var url = Reflect.apply(target, that, args);
15
return url ? ctx.url.unwrap(url, ctx.meta) : url;
16
},
17
}),
18
});
19
};
20
if (ctx.window.Response) {
21
const responseURL = Object.getOwnPropertyDescriptor(ctx.window.Response.prototype, 'url');
22
Object.defineProperty(ctx.window.Response.prototype, 'url', {
23
get: new Proxy(responseURL.get, {
24
apply: (target, that, args) => {
25
var url = Reflect.apply(target, that, args);
26
return url ? ctx.url.unwrap(url, ctx.meta) : url;
27
},
28
}),
29
});
30
};
31
if (ctx.window.open) {
32
ctx.window.open = new Proxy(ctx.window.open, {
33
apply: (target, that, args) => {
34
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
35
return Reflect.apply(target, that, args)
36
},
37
});
38
};
39
if (ctx.window.fetch) {
40
ctx.window.fetch = new Proxy(ctx.window.fetch, {
41
apply: (target, that, args) => {
42
if (args[0] instanceof ctx.window.Request) return Reflect.apply(target, that, args);
43
if (args[0]) args[0] = ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], });
44
return Reflect.apply(target, that, args);
45
},
46
});
47
};
48
if (ctx.window.Navigator && ctx.window.Navigator.prototype.sendBeacon) {
49
ctx.window.Navigator.prototype.sendBeacon = new Proxy(ctx.window.Navigator.prototype.sendBeacon, {
50
apply: (target, that, args) => {
51
if (args[0]) ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], });
52
return Reflect.apply(target, that, args);
53
},
54
});
55
};
56
if (ctx.window.XMLHttpRequest) {
57
const responseURL = Object.getOwnPropertyDescriptor(ctx.window.XMLHttpRequest.prototype, 'responseURL');
58
ctx.window.XMLHttpRequest.prototype.open = new Proxy(ctx.window.XMLHttpRequest.prototype.open, {
59
apply: (target, that, args) => {
60
if (args[1]) args[1] = ctx.url.wrap(args[1], { ...ctx.meta, flags: ['xhr'], });
61
return Reflect.apply(target, that, args);
62
},
63
});
64
Object.defineProperty(ctx.window.XMLHttpRequest.prototype, 'responseURL', {
65
get: new Proxy(responseURL.get, {
66
apply: (target, that, args) => {
67
const url = Reflect.apply(target, that, args);
68
return url ? ctx.url.unwrap(url, ctx.meta) : url;
69
},
70
}),
71
});
72
};
73
if (ctx.window.postMessage) {
74
ctx.window.postMessage = new Proxy(ctx.window.postMessage, {
75
apply: (target, that, args) => {
76
if (!ctx.serviceWorker && args[1]) args[1] = ctx.meta.origin;
77
return Reflect.apply(target, that, args);
78
},
79
});
80
};
81
if (ctx.window.WebSocket && ctx.config.ws) {
82
ctx.window.WebSocket = new Proxy(ctx.window.WebSocket, {
83
construct: (target, args) => {
84
if (args[0]) args[0] = ctx.url.wrap(args[0].toString().replace('ws', 'http'), ctx.meta).replace('http', 'ws') + '?origin=' + ctx.location.origin;
85
return Reflect.construct(target, args);
86
},
87
});
88
};
89
};
90
};
91
92
module.exports = createHttpRewriter;
93