Skip to content

Instantly share code, notes, and snippets.

@burtonator
Created September 15, 2020 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burtonator/96794ce550d4b679200017e11d45943a to your computer and use it in GitHub Desktop.
Save burtonator/96794ce550d4b679200017e11d45943a to your computer and use it in GitHub Desktop.
import * as functions from "firebase-functions";
import {Fetches} from "polar-shared/src/util/Fetch";
import {PassThrough, Writable} from 'stream';
/**
* Perform an HTTP request on behalf of the user. For the web application we
* have to proxy requests to download objects or else they they don't
*
* TODO:
* - I think I worked on this before and couldn't get it to work. I think I
* remember that firebase doesn't support streaming. I think I did this
* with cloud storage and couldn't get it to work. I think it buffers the
* entire response in memory and THEN sends it... I think I had to use a
* signed URL for this or something.
*/
export const ProxyFunction = functions.https.onRequest(async (req, res) => {
// FIXME: make sure to verify idUser so that people can't use this as an open proxy.
// use a stream to send back the response...
//
// https://github.com/firebase/firebase-functions/issues/401
// try with this approach...
const response = await Fetches.fetch('http://www.example.com');
// copy all header values...
for (const header of response.headers) {
const [key, value] = header;
res.setHeader(key, value);
}
// TODO: now add the proper CORS allow origin and expose-headers ...
const targetStream = new PassThrough();
let error: Error | undefined;
targetStream.once('error', err => {
error = err;
});
targetStream.once('end', () => {
if (error) {
res.statusMessage = error.message;
res.sendStatus(500);
}
res.statusMessage = response.statusText;
res.sendStatus(response.status)
});
targetStream.pipe(res);
// TODO what about the HTTP status?
response.body.pipe(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment