From c82451f8887f2f4035f530620e81018d4cbcbc77 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 20 Apr 2023 08:02:49 +0100 Subject: [PATCH] Update koa <> socket.io integation to improve fake koa context and allow current app middleware --- .../src/components/sheet/lib/websocket.js | 6 +++--- packages/server/src/api/controllers/row/index.ts | 10 +++++----- .../server/src/websockets/{spreadsheet.ts => grid.ts} | 4 ++-- packages/server/src/websockets/index.ts | 8 ++++---- packages/server/src/websockets/websocket.ts | 5 +++-- 5 files changed, 17 insertions(+), 16 deletions(-) rename packages/server/src/websockets/{spreadsheet.ts => grid.ts} (91%) diff --git a/packages/frontend-core/src/components/sheet/lib/websocket.js b/packages/frontend-core/src/components/sheet/lib/websocket.js index fe9b1c05e6..af2e247f98 100644 --- a/packages/frontend-core/src/components/sheet/lib/websocket.js +++ b/packages/frontend-core/src/components/sheet/lib/websocket.js @@ -10,7 +10,7 @@ export const createWebsocket = context => { const host = location.hostname const port = location.port || (tls ? 443 : 80) const socket = io(`${proto}//${host}:${port}`, { - path: "/socket/spreadsheet", + path: "/socket/grid", // Cap reconnection attempts to 3 (total of 15 seconds before giving up) reconnectionAttempts: 3, // Delay reconnection attempt by 5 seconds @@ -48,10 +48,10 @@ export const createWebsocket = context => { users.actions.removeUser(user) }) socket.on("connect_error", err => { - console.log("Failed to connect to websocket:", err.message) + console.log("Failed to connect to grid websocket:", err.message) }) - // Change websocket connection when dataspace changes + // Change websocket connection when table changes tableId.subscribe(connectToTable) // Notify selected cell changes diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index f48d9bcf26..c9f79dcb49 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -2,7 +2,7 @@ import { quotas } from "@budibase/pro" import * as internal from "./internal" import * as external from "./external" import { isExternalTable } from "../../../integrations/utils" -import { spreadsheetSocket } from "../../../websockets" +import { gridSocket } from "../../../websockets" import { Ctx } from "@budibase/types" import * as utils from "./utils" @@ -50,7 +50,7 @@ export async function patch(ctx: any): Promise { ctx.body = row // Notify websocket change - spreadsheetSocket?.emit("row-update", { id: row._id }) + gridSocket?.emit("row-update", { id: row._id }) } catch (err) { ctx.throw(400, err) } @@ -75,7 +75,7 @@ export const save = async (ctx: any) => { ctx.body = row // Notify websocket change - spreadsheetSocket?.emit("row-update", { id: row._id }) + gridSocket?.emit("row-update", { id: row._id }) } export async function fetchView(ctx: any) { const tableId = getTableId(ctx) @@ -115,7 +115,7 @@ export async function destroy(ctx: any) { for (let row of rows) { ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, appId, row) // Notify websocket change - spreadsheetSocket?.emit("row-update", { id: row._id }) + gridSocket?.emit("row-update", { id: row._id }) } } else { let resp = await quotas.addQuery(() => pickApi(tableId).destroy(ctx), { @@ -126,7 +126,7 @@ export async function destroy(ctx: any) { row = resp.row ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, appId, row) // Notify websocket change - spreadsheetSocket?.emit("row-update", { id: row._id }) + gridSocket?.emit("row-update", { id: row._id }) } ctx.status = 200 // for automations include the row that was deleted diff --git a/packages/server/src/websockets/spreadsheet.ts b/packages/server/src/websockets/grid.ts similarity index 91% rename from packages/server/src/websockets/spreadsheet.ts rename to packages/server/src/websockets/grid.ts index 1372f194a6..886784cd2c 100644 --- a/packages/server/src/websockets/spreadsheet.ts +++ b/packages/server/src/websockets/grid.ts @@ -4,9 +4,9 @@ import { permissions } from "@budibase/backend-core" import http from "http" import Koa from "koa" -export default class SpreadsheetSocket extends Socket { +export default class GridSocket extends Socket { constructor(app: Koa, server: http.Server) { - super(app, server, "/socket/spreadsheet", [authorized(permissions.BUILDER)]) + super(app, server, "/socket/grid", [authorized(permissions.BUILDER)]) this.io.on("connection", socket => { const user = socket.data.user diff --git a/packages/server/src/websockets/index.ts b/packages/server/src/websockets/index.ts index 6995989151..60cdfb8aed 100644 --- a/packages/server/src/websockets/index.ts +++ b/packages/server/src/websockets/index.ts @@ -1,14 +1,14 @@ import http from "http" import Koa from "koa" -import SpreadsheetSocket from "./spreadsheet" +import GridSocket from "./grid" import ClientAppSocket from "./client" let clientAppSocket: ClientAppSocket -let spreadsheetSocket: SpreadsheetSocket +let gridSocket: GridSocket export const initialise = (app: Koa, server: http.Server) => { clientAppSocket = new ClientAppSocket(app, server) - spreadsheetSocket = new SpreadsheetSocket(app, server) + gridSocket = new GridSocket(app, server) } -export { clientAppSocket, spreadsheetSocket } +export { clientAppSocket, gridSocket } diff --git a/packages/server/src/websockets/websocket.ts b/packages/server/src/websockets/websocket.ts index d3c1a7a8b3..e88c1f9744 100644 --- a/packages/server/src/websockets/websocket.ts +++ b/packages/server/src/websockets/websocket.ts @@ -26,7 +26,7 @@ export default class Socket { const middlewares = [ userAgent, authenticate, - // currentApp, + currentApp, ...(additionalMiddlewares || []), ] @@ -46,7 +46,9 @@ export default class Socket { }, // Needed for koa-useragent middleware + headers: socket.request.headers, header: socket.request.headers, + path: "/socket/grid", } // Run all koa middlewares @@ -60,7 +62,6 @@ export default class Socket { socket.data.user = { id: ctx.user._id, email: ctx.user.email, - selectedCellId: null, } next() }