diff --git a/src/backend/exports.js b/src/backend/exports.js
index 0ed27f99..3367710e 100644
--- a/src/backend/exports.js
+++ b/src/backend/exports.js
@@ -28,6 +28,7 @@ const { Context } = require("./src/util/context.js");
const { TestDriversModule } = require("./src/modules/test-drivers/TestDriversModule.js");
const { PuterAIModule } = require("./src/modules/puterai/PuterAIModule.js");
const { BroadcastModule } = require("./src/modules/broadcast/BroadcastModule.js");
+const { WebModule } = require("./src/modules/web/WebModule.js");
module.exports = {
@@ -42,9 +43,15 @@ module.exports = {
Context,
Kernel,
+
+ EssentialModules: [
+ CoreModule,
+ WebModule,
+ ],
// Pre-built modules
CoreModule,
+ WebModule,
DatabaseModule,
PuterDriversModule,
LocalDiskStorageModule,
diff --git a/src/backend/src/CoreModule.js b/src/backend/src/CoreModule.js
index a482892e..bd84efbf 100644
--- a/src/backend/src/CoreModule.js
+++ b/src/backend/src/CoreModule.js
@@ -129,7 +129,6 @@ const install = async ({ services, app, useapi, modapi }) => {
const { ConfigurableCountingService } = require('./services/ConfigurableCountingService');
const { FSLockService } = require('./services/fs/FSLockService');
const { StrategizedService } = require('./services/StrategizedService');
- const WebServerService = require('./services/WebServerService');
const FilesystemAPIService = require('./services/FilesystemAPIService');
const ServeGUIService = require('./services/ServeGUIService');
const PuterAPIService = require('./services/PuterAPIService');
@@ -143,7 +142,6 @@ const install = async ({ services, app, useapi, modapi }) => {
services.registerService('server-health', ServerHealthService);
services.registerService('log-service', LogService);
services.registerService('commands', CommandService);
- services.registerService('web-server', WebServerService, { app });
services.registerService('__api-filesystem', FilesystemAPIService);
services.registerService('__api', PuterAPIService);
services.registerService('__gui', ServeGUIService);
@@ -354,6 +352,9 @@ const install = async ({ services, app, useapi, modapi }) => {
const { UserService } = require('./services/UserService');
services.registerService('user', UserService);
+
+ const { WSPushService } = require('./services/WSPushService');
+ services.registerService('__event-push-ws', WSPushService);
}
const install_legacy = async ({ services }) => {
@@ -361,7 +362,6 @@ const install_legacy = async ({ services }) => {
// const { FilesystemService } = require('./filesystem/FilesystemService');
const PerformanceMonitor = require('./monitor/PerformanceMonitor');
const { OperationTraceService } = require('./services/OperationTraceService');
- const { WSPushService } = require('./services/WSPushService');
const { ClientOperationService } = require('./services/ClientOperationService');
const { EngPortalService } = require('./services/EngPortalService');
const { AppInformationService } = require('./services/AppInformationService');
@@ -371,7 +371,6 @@ const install_legacy = async ({ services }) => {
services.registerService('process-event', ProcessEventService);
// services.registerService('filesystem', FilesystemService);
services.registerService('operationTrace', OperationTraceService);
- services.registerService('__event-push-ws', WSPushService);
services.registerService('file-cache', FileCacheService);
services.registerService('client-operation', ClientOperationService);
services.registerService('app-information', AppInformationService);
diff --git a/src/backend/src/filesystem/FilesystemService.js b/src/backend/src/filesystem/FilesystemService.js
index 48da46c7..a22b1947 100644
--- a/src/backend/src/filesystem/FilesystemService.js
+++ b/src/backend/src/filesystem/FilesystemService.js
@@ -44,7 +44,6 @@ class FilesystemService extends BaseService {
static MODULES = {
_path: require('path'),
uuidv4: require('uuid').v4,
- socketio: require('../socketio.js'),
config: require('../config.js'),
}
diff --git a/src/backend/src/filesystem/hl_operations/hl_mkdir.js b/src/backend/src/filesystem/hl_operations/hl_mkdir.js
index 648c783b..41e656a9 100644
--- a/src/backend/src/filesystem/hl_operations/hl_mkdir.js
+++ b/src/backend/src/filesystem/hl_operations/hl_mkdir.js
@@ -242,7 +242,6 @@ class HLMkdir extends HLFilesystemOperation {
static MODULES = {
_path: require('path'),
- socketio: require('../../socketio.js'),
}
static PROPERTIES = {
@@ -258,7 +257,7 @@ class HLMkdir extends HLFilesystemOperation {
async _run () {
const { context, values } = this;
- const { _path, socketio } = this.modules;
+ const { _path } = this.modules;
const fs = context.get('services').get('filesystem');
if ( ! is_valid_path(values.path, {
diff --git a/src/backend/src/filesystem/hl_operations/hl_write.js b/src/backend/src/filesystem/hl_operations/hl_write.js
index 70b5b929..c0f71101 100644
--- a/src/backend/src/filesystem/hl_operations/hl_write.js
+++ b/src/backend/src/filesystem/hl_operations/hl_write.js
@@ -116,7 +116,6 @@ class HLWrite extends HLFilesystemOperation {
static MODULES = {
_path: require('path'),
- socketio: require('../../socketio.js'),
mime: require('mime-types'),
}
diff --git a/src/backend/src/modules/web/SocketioService.js b/src/backend/src/modules/web/SocketioService.js
new file mode 100644
index 00000000..1f98a10c
--- /dev/null
+++ b/src/backend/src/modules/web/SocketioService.js
@@ -0,0 +1,51 @@
+const BaseService = require('../../services/BaseService');
+
+class SocketioService extends BaseService {
+ static MODULES = {
+ socketio: require('socket.io'),
+ };
+
+ ['__on_install.socketio'] (_, { server }) {
+ const require = this.require;
+
+ const socketio = require('socket.io');
+ /**
+ * @type {import('socket.io').Server}
+ */
+ this.io = socketio(server, {
+ cors: {
+ origin: '*',
+ }
+ });
+ }
+
+ async send (socket_specifiers, key, data) {
+ const svc_getUser = this.services.get('get-user');
+
+ if ( ! Array.isArray(socket_specifiers) ) {
+ socket_specifiers = [socket_specifiers];
+ }
+
+ for ( const socket_specifier of socket_specifiers ) {
+ if ( socket_specifier.room ) {
+ this.io.to(socket_specifier.room).emit(key, data);
+ } else if ( socket_specifier.socket ) {
+ const io = this.io.sockets.sockets.get(socket_specifier.socket)
+ if ( ! io ) continue;
+ io.emit(key, data);
+ }
+ }
+ }
+
+ has (socket_specifier) {
+ if ( socket_specifier.room ) {
+ const room = this.io.sockets.adapter.rooms.get(socket_specifier.room);
+ return (!!room) && room.size > 0;
+ }
+ if ( socket_specifier.socket ) {
+ return this.io.sockets.sockets.has(socket_specifier.socket);
+ }
+ }
+}
+
+module.exports = SocketioService;
diff --git a/src/backend/src/modules/web/WebModule.js b/src/backend/src/modules/web/WebModule.js
new file mode 100644
index 00000000..e7d3b166
--- /dev/null
+++ b/src/backend/src/modules/web/WebModule.js
@@ -0,0 +1,17 @@
+const { AdvancedBase } = require("@heyputer/putility");
+
+class WebModule extends AdvancedBase {
+ async install (context) {
+ const services = context.get('services');
+
+ const SocketioService = require("./SocketioService");
+ services.registerService('socketio', SocketioService);
+
+ const WebServerService = require("./WebServerService");
+ services.registerService('web-server', WebServerService);
+ }
+}
+
+module.exports = {
+ WebModule,
+};
diff --git a/src/backend/src/services/WebServerService.js b/src/backend/src/modules/web/WebServerService.js
similarity index 95%
rename from src/backend/src/services/WebServerService.js
rename to src/backend/src/modules/web/WebServerService.js
index a0f7fd93..a9fd059a 100644
--- a/src/backend/src/services/WebServerService.js
+++ b/src/backend/src/modules/web/WebServerService.js
@@ -18,18 +18,19 @@
* along with this program. If not, see
Your email has been successfully confirmed.
`; diff --git a/src/backend/src/routers/change_email.js b/src/backend/src/routers/change_email.js index 328dea99..52870eba 100644 --- a/src/backend/src/routers/change_email.js +++ b/src/backend/src/routers/change_email.js @@ -83,10 +83,8 @@ const CHANGE_EMAIL_CONFIRM = eggspress('/change_email/confirm', { }); invalidate_cached_user_by_id(user_id); - let socketio = require('../socketio.js').getio(); - if(socketio){ - socketio.to(user_id).emit('user.email_changed', {}) - } + const svc_socketio = req.services.get('socketio'); + svc_socketio.send({ room: user_id }, 'user.email_changed', {}); const h = `Your email has been successfully confirmed.
`; return res.send(h); diff --git a/src/backend/src/routers/confirm-email.js b/src/backend/src/routers/confirm-email.js index d8269e2b..922e71d6 100644 --- a/src/backend/src/routers/confirm-email.js +++ b/src/backend/src/routers/confirm-email.js @@ -87,14 +87,14 @@ router.post('/confirm-email', auth, express.json(), async (req, res, next)=>{ // Send realtime success msg to client if(req.body.code === req.user.email_confirm_code){ - let socketio = require('../socketio.js').getio(); - if(socketio){ - socketio.to(req.user.id).emit('user.email_confirmed', {original_client_socket_id: req.body.original_client_socket_id}) - } + const svc_socketio = req.services.get('socketio'); + svc_socketio.send({ room: req.user.id }, 'user.email_confirmed', { + original_client_socket_id: req.body.original_client_socket_id + }); } // return results return res.send(res_obj) }) -module.exports = router \ No newline at end of file +module.exports = router diff --git a/src/backend/src/routers/filesystem_api/delete.js b/src/backend/src/routers/filesystem_api/delete.js index 076ff79a..d680d806 100644 --- a/src/backend/src/routers/filesystem_api/delete.js +++ b/src/backend/src/routers/filesystem_api/delete.js @@ -48,8 +48,6 @@ module.exports = eggspress('/delete', { else if(paths.length === 0) return res.status(400).send('paths cannot be empty') - const socketio = require('../../socketio.js').getio(); - // try to delete each path in the array one by one (if glob, resolve first) // TODO: remove this pseudo-batch for(let j=0; j < paths.length; j++){ @@ -67,9 +65,11 @@ module.exports = eggspress('/delete', { }); // send realtime success msg to client - if(socketio){ - socketio.to(req.user.id).emit('item.removed', {path: item_path, descendants_only: descendants_only}) - } + const svc_socketio = req.services.get('socketio'); + svc_socketio.send({ room: req.user.id }, 'item.removed', { + path: item_path, + descendants_only: descendants_only, + }); } res.send({}); diff --git a/src/backend/src/routers/filesystem_api/rename.js b/src/backend/src/routers/filesystem_api/rename.js index b2632178..ac416e8e 100644 --- a/src/backend/src/routers/filesystem_api/rename.js +++ b/src/backend/src/routers/filesystem_api/rename.js @@ -174,10 +174,8 @@ module.exports = eggspress('/rename', { }; // send realtime success msg to client - let socketio = require('../../socketio.js').getio(); - if(socketio){ - socketio.to(req.user.id).emit('item.renamed', return_obj) - } + const svc_socketio = req.services.get('socketio'); + svc_socketio.send({ room: req.user.id }, 'item.renamed', return_obj); return res.send(return_obj); }); diff --git a/src/backend/src/routers/rao.js b/src/backend/src/routers/rao.js index 0dfb8f59..68b1aaca 100644 --- a/src/backend/src/routers/rao.js +++ b/src/backend/src/routers/rao.js @@ -90,8 +90,8 @@ router.post('/rao', auth, express.json(), async (req, res, next)=>{ } // Update clients - const socketio = require('../socketio.js').getio(); - socketio.to(req.user.id).emit('app.opened', { + const svc_socketio = req.services.get('socketio'); + svc_socketio.send({ room: req.user.id }, 'app.opened', { uuid: opened_app.uid, uid: opened_app.uid, name: opened_app.name, diff --git a/src/backend/src/routers/writeFile.js b/src/backend/src/routers/writeFile.js index b913a925..02a78d8d 100644 --- a/src/backend/src/routers/writeFile.js +++ b/src/backend/src/routers/writeFile.js @@ -457,10 +457,8 @@ module.exports = eggspress('/writeFile', { }; // send realtime success msg to client - let socketio = require('../socketio.js').getio(); - if(socketio){ - socketio.to(fsentry.user_id).emit('item.renamed', return_obj) - } + const svc_socketio = req.services.get('socketio'); + svc_socketio.send({ room: req.user.id }, 'item.renamed', return_obj); return res.send(return_obj); } diff --git a/src/backend/src/services/EngPortalService.js b/src/backend/src/services/EngPortalService.js index 478e83c1..54859429 100644 --- a/src/backend/src/services/EngPortalService.js +++ b/src/backend/src/services/EngPortalService.js @@ -32,7 +32,6 @@ const { AdvancedBase } = require("@heyputer/putility"); */ class EngPortalService extends AdvancedBase { static MODULES = { - socketio: require('../socketio.js'), uuidv4: require('uuid').v4, }; diff --git a/src/backend/src/services/WSPushService.js b/src/backend/src/services/WSPushService.js index 6fb0c669..7b92cc72 100644 --- a/src/backend/src/services/WSPushService.js +++ b/src/backend/src/services/WSPushService.js @@ -17,17 +17,10 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see