| process.on('uncaughtException', function(err) { |
| if (err.code === 'EPIPE') return; |
| if (err.code === 'ECONNRESET' || err.code === 'ENOTCONN' || err.code === 'ERR_STREAM_DESTROYED') return; |
| throw err; |
| }); |
| |
| |
| |
| |
| |
| |
| "use strict"; |
|
|
| const http = require("http"); |
|
|
| const origEmit = http.Server.prototype.emit; |
|
|
| http.Server.prototype.emit = function (event, ...args) { |
| if (event === "request") { |
| const [, res] = args; |
|
|
| |
| const expectedPort = Number(process.env.GATEWAY_PORT || 7860); |
| const serverPort = this.address && this.address() && this.address().port; |
| if (serverPort && serverPort !== expectedPort) { |
| return origEmit.apply(this, [event, ...args]); |
| } |
|
|
| |
| const origWriteHead = res.writeHead; |
| res.writeHead = function (statusCode, ...whArgs) { |
| if (res.getHeader) { |
| |
| res.removeHeader("x-frame-options"); |
|
|
| |
| const csp = res.getHeader("content-security-policy"); |
| if (csp && typeof csp === "string") { |
| res.setHeader( |
| "content-security-policy", |
| csp.replace( |
| /frame-ancestors\s+'none'/i, |
| "frame-ancestors 'self' https://huggingface.co https://*.hf.space", |
| ), |
| ); |
| } |
| } |
| return origWriteHead.apply(this, [statusCode, ...whArgs]); |
| }; |
| } |
|
|
| return origEmit.apply(this, [event, ...args]); |
| }; |
|
|