nextjs-integration/index.js

109 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-07-23 20:48:52 +02:00
var fs = require("fs");
var routerServer = null;
var nextServerUtil = null;
var nextError = null;
var dev = process.env.NODE_ENV == "development";
var handle = function () {
throw nextError;
}
var isPrepared = false;
var configJSON = {};
try {
configJSON = JSON.parse(fs.readFileSync(__dirname + "/../../config.json"));
} catch (err) {
// Ignore the error
}
var useHTTPS = configJSON.secure === undefined ? false : configJSON.secure;
var port = useHTTPS ? (configJSON.sport === undefined ? 443 : configJSON.sport) : (configJSON.port === undefined ? 80 : configJSON.port);
try {
// process.cwd() is the webroot, the webroot will then act as a Next.js app directory
try {
// Use Next.js from Next.js app directory
routerServer = require(process.cwd() + "/node_modules/next/dist/server/lib/router-server.js");
nextServerUtil = require(process.cwd() + "/node_modules/next/dist/server/lib/utils.js");
} catch (err) {
// Fallback Next.js module.
try {
require(process.cwd() + "/node_modules/next");
throw new Error("Next.js version mismatch");
} catch (err) {
// No Next.js in Next.js app directory
}
routerServer = require("next/dist/server/lib/router-server.js");
nextServerUtil = require("next/dist/server/lib/utils.js");
}
async function init() {
try {
2024-11-01 10:15:34 +01:00
var checkNodeDebugType = function() {return null};
if (nextServerUtil.getNodeDebugType) checkNodeDebugType = nextServerUtil.getNodeDebugType;
if (nextServerUtil.checkNodeDebugType) checkNodeDebugType = nextServerUtil.checkNodeDebugType;
2024-07-23 20:48:52 +02:00
initResponse = await routerServer.initialize({
dir: process.cwd(),
port: port,
dev: dev,
2024-11-01 10:15:34 +01:00
isNodeDebugging: Boolean(checkNodeDebugType()) || false,
2024-07-23 20:48:52 +02:00
startServerSpan: undefined,
experimentalHttpsServer: useHTTPS
});
handle = initResponse[0];
isPrepared = true;
} catch (err) {
nextError = err;
2024-07-23 20:48:52 +02:00
}
}
init();
} catch (err) {
// process.cwd() is the webroot, the webroot will then act as a Next.js app directory
try {
var next = null;
try {
// Use Next.js from Next.js app directory
next = require(process.cwd() + "/node_modules/next");
} catch (err) {
// Fallback Next.js module.
next = require("next");
}
var app = next({
dir: process.cwd(),
port: port,
dev: dev,
customServer: false
});
handle = app.getRequestHandler();
app.prepare().then(function () {
isPrepared = true;
}).catch(function (err) {
nextError = err;
2024-07-23 20:48:52 +02:00
});
} catch (err) {
nextError = err;
}
}
// The returned function has to be an async function, or else the errors will crash SVR.JS
function Mod() {}
Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, elseCallback, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData) {
return async function () {
try {
if (nextError) throw nextError;
2024-07-23 20:48:52 +02:00
if (!isPrepared) {
callServerError(503, "nextjs-integration/1.0.0");
serverconsole.errmessage("Next.js application not yet fully loaded.");
return;
}
await handle(req, res);
serverconsole.resmessage("Next.js request successfully processed.");
} catch (err) {
callServerError(500, "nextjs-integration/1.0.0", err);
}
}
}
module.exports = Mod;