diff --git a/accesRights.js b/accesRights.js index 1d6c974..236cb5c 100644 --- a/accesRights.js +++ b/accesRights.js @@ -1,4 +1,4 @@ -module.exports = class AccesRights { +export class AccesRights { constructor(config, debug){ this.debug = debug diff --git a/actions/chat.js b/actions/chat.js index 012ee53..f65b160 100644 --- a/actions/chat.js +++ b/actions/chat.js @@ -1,4 +1,4 @@ -module.exports.methods = { +export const methods = { /* Creates (predictable) peer-to-peer chan if necessary, or check for lobby existence, then subscribe to it. Request: diff --git a/actions/index.js b/actions/index.js index 63a83b5..2e1956d 100644 --- a/actions/index.js +++ b/actions/index.js @@ -1,9 +1,17 @@ -module.exports.methods = {} -Object.assign(module.exports.methods, require('./utilities').methods ) -Object.assign(module.exports.methods, require('./pubSub').methods ) -Object.assign(module.exports.methods, require('./store').methods ) -Object.assign(module.exports.methods, require('./sessions').methods ) -Object.assign(module.exports.methods, require('./notifications').methods ) -Object.assign(module.exports.methods, require('./chat').methods ) +import { methods as utilities } from './utilities.js' +import { methods as pubSub } from './pubSub.js' +import { methods as store } from './store.js' +import { methods as sessions } from './sessions.js' +import { methods as notifications } from './notifications.js' +import { methods as chat } from './chat.js' + +export const gatewayActions = { + ...utilities, + ...pubSub, + ...store, + ...sessions, + ...notifications, + ...chat +} diff --git a/actions/notifications.js b/actions/notifications.js index 879e740..20cebe2 100644 --- a/actions/notifications.js +++ b/actions/notifications.js @@ -1,4 +1,4 @@ -module.exports.methods = { +export const methods = { async action_NOTIFS(action, payload, reqid){ let reply = { diff --git a/actions/pubSub.js b/actions/pubSub.js index 163755d..d5e0243 100644 --- a/actions/pubSub.js +++ b/actions/pubSub.js @@ -1,4 +1,4 @@ -module.exports.methods = { +export const methods = { /* Request: { "action": "SUB", diff --git a/actions/sessions.js b/actions/sessions.js index 7fabdae..3c834b6 100644 --- a/actions/sessions.js +++ b/actions/sessions.js @@ -1,4 +1,4 @@ -module.exports.methods = { +export const methods = { /* Request payload : null Reply: diff --git a/actions/store.js b/actions/store.js index 0016722..4e98c15 100644 --- a/actions/store.js +++ b/actions/store.js @@ -1,4 +1,4 @@ -module.exports.methods = { +export const methods = { /* Request: { diff --git a/actions/utilities.js b/actions/utilities.js index f6911a7..9a40773 100644 --- a/actions/utilities.js +++ b/actions/utilities.js @@ -1,4 +1,4 @@ -module.exports.methods = { +export const methods = { /* Request: { diff --git a/configHelper.js b/configHelper.js index 237bb06..4575f06 100644 --- a/configHelper.js +++ b/configHelper.js @@ -1,9 +1,8 @@ -const Ajv = require("ajv") -const confSchema = require('./configSchema.json') +import Ajv from 'ajv' +import confSchema from './configSchema.json' with { type: 'json' } +import { pathToFileURL } from 'url' -let DynamoDBClient, GetItemCommand, marshall, unmarshall - -module.exports = class configHelper { +export class configHelper { constructor(options){ this.config = {} @@ -24,7 +23,10 @@ module.exports = class configHelper { async fetchConfigFile(){ let curConfig = this.config - this.config = await require(this.localfile) + const url = pathToFileURL(this.localfile).href + this.config = await import(url, { + with: { type: 'json' } + }).then(m => m.default) if(this.isValidConfig(this.config)) return(this.config) console.error(this.isValidConfig.errors) //revert if invalid conf @@ -37,9 +39,8 @@ module.exports = class configHelper { } async refreshAccessRightsFile(){ - delete require.cache[require.resolve(this.localfile)] let tmp - try { tmp = require(this.localfile) } + try { tmp = import(`${this.localfile}?update=${Date.now()}`, { assert: { type: 'json' } }) } catch(err) { console.error('Error Reloading config !! (bad json?) => Keeping current accessRights !') return diff --git a/package.json b/package.json index a9afaf5..f555da8 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "3.4.6", "description": "Websocket-Redis Message Bus Gateway", "main": "wssGateway.js", + "type": "module", "scripts": { "test": "jest --verbose" }, @@ -15,6 +16,8 @@ "dependencies": { "ajv": "^8.12.0", "cookie-parser": "^1.4.6", + "express-mysql-session": "^3.0.3", + "express-session": "^1.18.2", "pm2": "^6.0.10", "redis": "^4.3.0", "urldecode": "^1.0.1", diff --git a/redisConnexion.js b/redisConnexion.js index 8dafbe5..2e474b4 100644 --- a/redisConnexion.js +++ b/redisConnexion.js @@ -1,6 +1,5 @@ -const redis = require('redis'); - -module.exports = class RedisConnexion { +import redis from 'redis' +export class RedisConnexion { constructor(options) { this.config = options.config; diff --git a/tests/accessRights.test.js b/tests/accessRights.test.js index 666a548..a060f90 100644 --- a/tests/accessRights.test.js +++ b/tests/accessRights.test.js @@ -1,4 +1,4 @@ -const AccesRights = require('../accesRights') +import {AccesRights} from '../accesRights' let accessRights = new AccesRights({ "accessRights": [ { diff --git a/wssConnexion.js b/wssConnexion.js index 2cc1836..3fbb249 100644 --- a/wssConnexion.js +++ b/wssConnexion.js @@ -1,10 +1,10 @@ -const crypto = require('crypto') -const gatewayActions = require('./actions') +import crypto from 'crypto' +import { gatewayActions } from './actions/index.js' -module.exports = class WssConnexion { +export class WssConnexion { constructor(options){ - Object.assign(this, gatewayActions.methods) + Object.assign(this, gatewayActions) this.config = options.config; this.socket = options.socket; diff --git a/wssGateway.js b/wssGateway.js index 37615ce..cfa2230 100644 --- a/wssGateway.js +++ b/wssGateway.js @@ -1,13 +1,14 @@ -const yargs = require('yargs') -const ws = require('ws') -const fs = require('fs') -const RedisConnexion = require('./redisConnexion') -const urlparser = require('url'); -const wssServer = require('./wssServer') -const configHelper = require('./configHelper') +import yargs from 'yargs/yargs' +import { hideBin } from 'yargs/helpers' +import { WebSocketServer } from 'ws' +import fs from 'fs' +import {RedisConnexion} from './redisConnexion.js' +import urlparser from 'url' +import {wssServer} from './wssServer.js' +import {configHelper} from './configHelper.js' -const argv = yargs.command('wssGateway', 'Redis <=> Websocket message bus gateway', {}) +const argv = yargs(hideBin(process.argv)).command('wssGateway', 'Redis <=> Websocket message bus gateway', {}) .options({ 'argv.debug': { description: 'shows debug info', @@ -36,15 +37,15 @@ async function startRedis(wssGatewayConfig) { } -cfgh.fetchConfig().then( wssGatewayConfig => { +cfgh.fetchConfig().then( async wssGatewayConfig => { if((!wssGatewayConfig) || (Object.keys(wssGatewayConfig).length<4)) { console.error('Cannot get a valid configuration ! Aaarrghhh...') process.exit() } let httpLib - if(wssGatewayConfig.server.unsecure) httpLib = require('http') - else httpLib = require('https') + if(wssGatewayConfig.server.unsecure) httpLib = await import('http') + else httpLib = await import('https') /////////////////////// Create & Start servers \\\\\\\\\\\\\\\\\\\\\\ @@ -91,7 +92,7 @@ cfgh.fetchConfig().then( wssGatewayConfig => { wssServerOptions['key'] = fs.readFileSync(wssGatewayConfig.server.certKeyFile) wssServerOptions['cert'] = fs.readFileSync(wssGatewayConfig.server.certFile) } - const WSSServer = new ws.WebSocketServer(wssServerOptions); + const WSSServer = new WebSocketServer(wssServerOptions); console.log(`WS${wssGatewayConfig.server.unsecure ? '': 'S'} server created for ${wssGatewayConfig.server.listenHost}:${wssGatewayConfig.server.listenPort}`) startRedis(wssGatewayConfig).then((rediscnx) => { diff --git a/wssGatewayConfig.json b/wssGatewayConfig.json index b9cb9ac..c17b144 100644 --- a/wssGatewayConfig.json +++ b/wssGatewayConfig.json @@ -2,12 +2,12 @@ "serviceName": "wssGateway", "server": { "listenHost": "0.0.0.0", - "listenPort": 4443, + "listenPort": 3999, "listenPath": "/msgbus", "keepAliveInterval": "30", "keepAliveTimeout": "5", - "certFile": "/etc/letsencrypt/live/eismea.internike.com/fullchain.pem", - "certKeyFile": "/etc/letsencrypt/live/eismea.internike.com/privkey.pem", + "certFile": "/etc/letsencrypt/live/42.internike.com/fullchain.pem", + "certKeyFile": "/etc/letsencrypt/live/42.internike.com/privkey.pem", "challengeExpiration": 20, "unsecure": false, "healthCheckPath": "/status", diff --git a/wssServer.js b/wssServer.js index cc04af9..ac0e1a6 100644 --- a/wssServer.js +++ b/wssServer.js @@ -1,8 +1,10 @@ -const AccesRights = require('./accesRights') -const crypto = require('crypto') -const WssConnexion = require('./wssConnexion') +import {AccesRights} from './accesRights.js' +import crypto from 'crypto' +import {WssConnexion} from './wssConnexion.js' +import session from 'express-session' +import connectMySQL from 'express-mysql-session' -module.exports = class wssServer { +export class wssServer { constructor(configHelper, WSSServer, REDIScnx, debug) { this.debug = debug