You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

54 lines
1.6 KiB

#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const targetPath = path.resolve(process.argv[2] || ".next/standalone/server.js");
const marker = "POPIART_STANDALONE_TIMEOUT_PATCH";
const patch = `
// ${marker}: keep long-running generation requests alive in production.
const http = require("http");
const originalCreateServer = http.createServer;
http.createServer = function popiartCreateServerWithTimeouts(...args) {
const server = originalCreateServer.apply(this, args);
server.requestTimeout = Number(process.env.REQUEST_TIMEOUT_MS || 600000);
server.headersTimeout = Number(process.env.HEADERS_TIMEOUT_MS || 610000);
return server;
};
`;
function fail(message) {
console.error(`[patch-standalone-server] ${message}`);
process.exit(1);
}
if (!fs.existsSync(targetPath)) {
fail(`target file not found: ${targetPath}`);
}
const source = fs.readFileSync(targetPath, "utf8");
if (source.includes(marker)) {
console.log(`[patch-standalone-server] already patched: ${targetPath}`);
process.exit(0);
}
const pathRequire = "const path = require('path')\n";
const doubleQuotePathRequire = 'const path = require("path")\n';
let nextSource;
if (source.includes(pathRequire)) {
nextSource = source.replace(pathRequire, `${pathRequire}${patch}`);
} else if (source.includes(doubleQuotePathRequire)) {
nextSource = source.replace(doubleQuotePathRequire, `${doubleQuotePathRequire}${patch}`);
} else {
fail("could not find a stable insertion point: const path = require('path')");
}
fs.writeFileSync(targetPath, nextSource);
console.log(`[patch-standalone-server] patched: ${targetPath}`);