| 31 | constructor(private options: ExpressBasedEmulatorOptions) {} |
| 32 | |
| 33 | protected createExpressApp(): Promise<express.Express> { |
| 34 | const app = express(); |
| 35 | if (!this.options.noCors) { |
| 36 | // Enable CORS for all APIs, all origins (reflected), and all headers (reflected). |
| 37 | // This is enabled by default since most emulators are cookieless. |
| 38 | app.use(cors({ origin: true })); |
| 39 | |
| 40 | // Return access-control-allow-private-network heder if requested |
| 41 | // Enables accessing locahost when site is exposed via tunnel see https://github.com/firebase/firebase-tools/issues/4227 |
| 42 | // Aligns with https://wicg.github.io/private-network-access/#headers |
| 43 | // Replace with cors option if adopted, see https://github.com/expressjs/cors/issues/236 |
| 44 | app.use((req, res, next) => { |
| 45 | if (req.headers["access-control-request-private-network"]) { |
| 46 | res.setHeader("access-control-allow-private-network", "true"); |
| 47 | } |
| 48 | next(); |
| 49 | }); |
| 50 | } |
| 51 | if (!this.options.noBodyParser) { |
| 52 | app.use(bodyParser.json({ limit: "130mb" })); // used in most emulators |
| 53 | } |
| 54 | app.set("json spaces", 2); |
| 55 | |
| 56 | return Promise.resolve(app); |
| 57 | } |
| 58 | |
| 59 | async start(): Promise<void> { |
| 60 | const app = await this.createExpressApp(); |