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