use ctx instead params

This commit is contained in:
SrGooglo 2025-03-25 22:46:48 +00:00
parent a5bde05b4e
commit 4cca792318
2 changed files with 40 additions and 43 deletions

View File

@ -1,12 +1,8 @@
import he from "hyper-express" import he from "hyper-express"
import rtengineng from "../../classes/rtengineng" import rtengineng from "../../classes/rtengineng"
import getRouteredFunctions from "../../utils/getRouteredFunctions"
import flatRouteredFunctions from "../../utils/flatRouteredFunctions"
export default class HyperExpressEngineNG { export default class HyperExpressEngineNG {
constructor(params, ctx) { constructor(ctx) {
this.params = params
this.ctx = ctx this.ctx = ctx
} }
@ -14,21 +10,21 @@ export default class HyperExpressEngineNG {
ws = null ws = null
router = null router = null
initialize = async (params) => { initialize = async () => {
console.warn( console.warn(
`hyper-express-ng is a experimental engine, some features may not be available or work properly!`, `hyper-express-ng is a experimental engine, some features may not be available or work properly!`,
) )
const serverParams = { const appParams = {
max_body_length: 50 * 1024 * 1024, //50MB in bytes, max_body_length: 50 * 1024 * 1024, //50MB in bytes,
} }
if (params.ssl) { if (this.ctx.ssl) {
serverParams.key_file_name = params.ssl?.key ?? null appParams.key_file_name = this.ctx.ssl?.key ?? null
serverParams.cert_file_name = params.ssl?.cert ?? null appParams.cert_file_name = this.ctx.ssl?.cert ?? null
} }
this.app = new he.Server(serverParams) this.app = new he.Server(appParams)
this.router = new he.Router() this.router = new he.Router()
@ -47,7 +43,7 @@ export default class HyperExpressEngineNG {
await this.app.use(async (req, res, next) => { await this.app.use(async (req, res, next) => {
if (req.method === "OPTIONS") { if (req.method === "OPTIONS") {
// handle cors // handle cors
if (params.ignoreCors) { if (this.ctx.constructor.ignoreCors) {
res.setHeader("Access-Control-Allow-Methods", "*") res.setHeader("Access-Control-Allow-Methods", "*")
res.setHeader("Access-Control-Allow-Origin", "*") res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Access-Control-Allow-Headers", "*") res.setHeader("Access-Control-Allow-Headers", "*")
@ -69,18 +65,18 @@ export default class HyperExpressEngineNG {
} }
}) })
if (params.enableWebsockets) { if (this.ctx.constructor.enableWebsockets === true) {
this.ws = global.websocket = new rtengineng({ this.ws = global.websocket = new rtengineng({
onUpgrade: params.handleWsUpgrade, onUpgrade: this.ctx.handleWsUpgrade,
onConnection: params.handleWsConnection, onConnection: this.ctx.handleWsConnection,
onDisconnect: params.handleWsDisconnect, onDisconnect: this.ctx.handleWsDisconnect,
}) })
await this.ws.attach(this) await this.ws.attach(this)
} }
} }
listen = async (params) => { listen = async () => {
if (process.env.lb_service) { if (process.env.lb_service) {
let pathOverrides = Object.keys(this.router.map).map((key) => { let pathOverrides = Object.keys(this.router.map).map((key) => {
return key.split("/")[1] return key.split("/")[1]
@ -98,15 +94,15 @@ export default class HyperExpressEngineNG {
return true return true
}) })
if (params.enableWebsockets) { if (this.ctx.constructor.enableWebsockets === true) {
process.send({ process.send({
type: "router:ws:register", type: "router:ws:register",
id: process.env.lb_service.id, id: process.env.lb_service.id,
index: process.env.lb_service.index, index: process.env.lb_service.index,
data: { data: {
namespace: params.refName, namespace: this.ctx.constructor.refName,
listen_port: this.params.listen_port, ws_path: this.ctx.constructor.wsPath ?? "/",
ws_path: params.wsPath ?? "/", listen_port: this.ctx.constructor.listen_port,
}, },
}) })
} }
@ -121,15 +117,15 @@ export default class HyperExpressEngineNG {
router_map: this.router.map, router_map: this.router.map,
path_overrides: pathOverrides, path_overrides: pathOverrides,
listen: { listen: {
ip: this.params.listen_ip, ip: this.ctx.constructor.listen_ip,
port: this.params.listen_port, port: this.ctx.constructor.listen_port,
}, },
}, },
}) })
} }
} }
await this.app.listen(this.params.listen_port) await this.app.listen(this.ctx.constructor.listen_port)
} }
// close must be synchronous // close must be synchronous

View File

@ -2,8 +2,7 @@ import he from "hyper-express"
import rtengine from "../../classes/rtengine" import rtengine from "../../classes/rtengine"
export default class Engine { export default class Engine {
constructor(params, ctx) { constructor(ctx) {
this.params = params
this.ctx = ctx this.ctx = ctx
} }
@ -11,14 +10,14 @@ export default class Engine {
router = null router = null
ws = null ws = null
initialize = async (params) => { initialize = async () => {
const serverParams = { const serverParams = {
max_body_length: 50 * 1024 * 1024, //50MB in bytes, max_body_length: 50 * 1024 * 1024, //50MB in bytes,
} }
if (params.ssl) { if (this.ctx.ssl) {
serverParams.key_file_name = params.ssl?.key ?? null serverParams.key_file_name = this.ctx.ssl?.key ?? null
serverParams.cert_file_name = params.ssl?.cert ?? null serverParams.cert_file_name = this.ctx.ssl?.cert ?? null
} }
this.app = new he.Server(serverParams) this.app = new he.Server(serverParams)
@ -40,7 +39,7 @@ export default class Engine {
await this.app.use(async (req, res, next) => { await this.app.use(async (req, res, next) => {
if (req.method === "OPTIONS") { if (req.method === "OPTIONS") {
// handle cors // handle cors
if (params.ignoreCors) { if (this.ctx.constructor.ignoreCors) {
res.setHeader("Access-Control-Allow-Methods", "*") res.setHeader("Access-Control-Allow-Methods", "*")
res.setHeader("Access-Control-Allow-Origin", "*") res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Access-Control-Allow-Headers", "*") res.setHeader("Access-Control-Allow-Headers", "*")
@ -62,11 +61,11 @@ export default class Engine {
} }
}) })
if (params.enableWebsockets) { if (this.ctx.constructor.enableWebsockets) {
this.ws = global.websocket = new rtengine({ this.ws = global.websocket = new rtengine({
...params, requireAuth: this.ctx.constructor.requiredWsAuth,
handleAuth: params.handleWsAuth, handleAuth: this.ctx.handleWsAuth,
root: `/${params.refName}`, root: `/${this.ctx.constructor.refName}`,
}) })
this.ws.initialize() this.ws.initialize()
@ -75,7 +74,7 @@ export default class Engine {
} }
} }
listen = async (params) => { listen = async () => {
if (process.env.lb_service) { if (process.env.lb_service) {
let pathOverrides = Object.keys(this.router.map).map((key) => { let pathOverrides = Object.keys(this.router.map).map((key) => {
return key.split("/")[1] return key.split("/")[1]
@ -93,15 +92,17 @@ export default class Engine {
return true return true
}) })
if (params.enableWebsockets) { if (this.ctx.constructor.enableWebsockets) {
process.send({ process.send({
type: "router:ws:register", type: "router:ws:register",
id: process.env.lb_service.id, id: process.env.lb_service.id,
index: process.env.lb_service.index, index: process.env.lb_service.index,
data: { data: {
namespace: params.refName, namespace: this.ctx.constructor.refName,
listen_port: this.params.listen_port, listen_port: this.ctx.constructor.listen_port,
ws_path: this.params.ws_path ?? params.refName, ws_path:
this.ctx.constructor.wsPath ??
this.ctx.constructor.refName,
}, },
}) })
} }
@ -116,15 +117,15 @@ export default class Engine {
router_map: this.router.map, router_map: this.router.map,
path_overrides: pathOverrides, path_overrides: pathOverrides,
listen: { listen: {
ip: this.params.listen_ip, ip: this.ctx.constructor.listen_ip,
port: this.params.listen_port, port: this.ctx.constructor.listen_port,
}, },
}, },
}) })
} }
} }
await this.app.listen(this.params.listen_port) await this.app.listen(this.ctx.constructor.listen_port)
} }
// close should be synchronous // close should be synchronous