Initial commit

This commit is contained in:
Dorian Niemiec 2024-07-23 20:48:52 +02:00
commit 6120969910
2 changed files with 110 additions and 0 deletions

106
index.js Normal file
View file

@ -0,0 +1,106 @@
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 appPreparationError = null;
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 {
initResponse = await routerServer.initialize({
dir: process.cwd(),
port: port,
dev: dev,
isNodeDebugging: Boolean(nextServerUtil.checkNodeDebugType()) || false,
startServerSpan: undefined,
experimentalHttpsServer: useHTTPS
});
handle = initResponse[0];
isPrepared = true;
} catch (err) {
appPreparationError = err;
}
}
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) {
appPreparationError = err;
});
} 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 (appPreparationError) throw appPreparationError;
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;

4
mod.info Executable file
View file

@ -0,0 +1,4 @@
{
"name": "Next.js integration for SVR.JS",
"version": "Nightly-GitMain"
}