-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2083 from quadratichq/ayush/1899
feat: request proxying for JS & fix auth header override for both JS and Python requests
- Loading branch information
Showing
11 changed files
with
202 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...t/src/app/web-workers/javascriptWebWorker/worker/javascript/getJavascriptFetchOverride.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export function getJavascriptFetchOverride(proxyUrl: string, jwt: string) { | ||
return ` | ||
self['fetch'] = new Proxy(fetch, { | ||
apply: function (target, thisArg, args) { | ||
const [url, config] = args; | ||
const newConfig = config || {}; | ||
const headers = newConfig.headers || {}; | ||
const newHeaders = {}; | ||
// Prefix all original request headers with X-Proxy- | ||
for (const key in headers) { | ||
newHeaders[\`X-Proxy-\${key}\`] = headers[key]; | ||
} | ||
// Set the original request URL on X-Proxy-Url header | ||
newHeaders['X-Proxy-Url'] = url.toString(); | ||
// Set the authorization header for the proxy server | ||
newHeaders['Authorization'] = 'Bearer ${jwt}'; | ||
newConfig.headers = newHeaders; | ||
return target.call(thisArg, '${proxyUrl}', newConfig); | ||
}, | ||
});\n | ||
`; | ||
} |
50 changes: 50 additions & 0 deletions
50
...ent/src/app/web-workers/javascriptWebWorker/worker/javascript/getJavascriptXHROverride.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export function getJavascriptXHROverride(proxyUrl: string, jwt: string) { | ||
return ` | ||
self['XMLHttpRequest'] = new Proxy(XMLHttpRequest, { | ||
construct: function (target, args) { | ||
const xhr = new target(); | ||
xhr.open = new Proxy(xhr.open, { | ||
apply: function (target, thisArg, args) { | ||
Object.defineProperty(xhr, '__url', { value: args[1].toString(), writable: true }); | ||
args[1] = '${proxyUrl}'; | ||
return target.apply(thisArg, args); | ||
}, | ||
}); | ||
xhr.setRequestHeader = new Proxy(xhr.setRequestHeader, { | ||
apply: function (target, thisArg, args) { | ||
// apply quadratic-authorization header as the only authorization header | ||
// this is required for authentication with the proxy server | ||
if (args[0] === 'Quadratic-Authorization') { | ||
args[0] = 'Authorization'; | ||
} else { | ||
// apply all headers on the original request prefixed with X-Proxy- | ||
args[0] = \`X-Proxy-\${args[0]}\`; | ||
} | ||
return target.apply(thisArg, args); | ||
}, | ||
}); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === XMLHttpRequest.OPENED) { | ||
// this applies the quadratic-authorization header as the only authorization header | ||
// this is required for authentication with the proxy server | ||
xhr.setRequestHeader('Quadratic-Authorization', 'Bearer ${jwt}'); | ||
// this applies the original request URL as the x-proxy-url header | ||
// this will get prefixed with X-Proxy due to above setRequestHeader override | ||
xhr.setRequestHeader('Url', xhr.__url); | ||
} | ||
// After completion of XHR request | ||
if (xhr.readyState === 4) { | ||
if (xhr.status === 401) { | ||
} | ||
} | ||
}; | ||
return xhr; | ||
}, | ||
});\n | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.