-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds web-standalone for running restfox without needing electro…
…n or the browser extensions to bypass cors and non https requests - this works by proxying all requests through the server (closes #18)
- Loading branch information
1 parent
fc93c04
commit 1c5f09b
Showing
9 changed files
with
1,287 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
public/ |
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,66 @@ | ||
import express from 'express' | ||
import fetch from 'node-fetch' | ||
|
||
const app = express() | ||
|
||
const port = process.env.PORT || 4004 | ||
|
||
app.use(express.static('public')) | ||
|
||
app.post('/proxy', async(req, res) => { | ||
const url = req.headers['x-proxy-req-url'] | ||
const method = req.headers['x-proxy-req-method'] | ||
const headers = {} | ||
const body = req.body | ||
|
||
Object.keys(req.headers).forEach(header => { | ||
if(header.startsWith('x-proxy-req-header-')) { | ||
headers[header.replace('x-proxy-req-header-', '')] = req.headers[header] | ||
} | ||
}) | ||
|
||
try { | ||
const startTime = new Date() | ||
|
||
const response = await fetch(url, { | ||
method, | ||
headers, | ||
body: method !== 'GET' ? body : undefined | ||
}) | ||
|
||
const endTime = new Date() | ||
|
||
const status = response.status | ||
const statusText = response.statusText | ||
const responseHeaders = [...response.headers.entries()] | ||
|
||
const responseBlob = await response.blob() | ||
const mimeType = responseBlob.type | ||
const buffer = await responseBlob.arrayBuffer() | ||
|
||
const timeTaken = endTime - startTime | ||
|
||
const responseToSend = { | ||
status, | ||
statusText, | ||
headers: responseHeaders, | ||
mimeType, | ||
buffer: Array.from(new Uint8Array(buffer)), | ||
timeTaken | ||
} | ||
|
||
res.send({ | ||
event: 'response', | ||
eventData: responseToSend | ||
}) | ||
} catch(e) { | ||
res.send({ | ||
event: 'responseError', | ||
eventData: e.message | ||
}) | ||
} | ||
}) | ||
|
||
app.listen(port, () => { | ||
console.log(`Restfox running on port http://localhost:${port}`) | ||
}) |
Oops, something went wrong.