From 6120969910c96d42fda6ab328c5c0df69de887b8 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Tue, 23 Jul 2024 20:48:52 +0200 Subject: [PATCH] Initial commit --- index.js | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ mod.info | 4 +++ 2 files changed, 110 insertions(+) create mode 100644 index.js create mode 100755 mod.info diff --git a/index.js b/index.js new file mode 100644 index 0000000..bd093d0 --- /dev/null +++ b/index.js @@ -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; diff --git a/mod.info b/mod.info new file mode 100755 index 0000000..ad0abe9 --- /dev/null +++ b/mod.info @@ -0,0 +1,4 @@ +{ + "name": "Next.js integration for SVR.JS", + "version": "Nightly-GitMain" +}