@jay-framework/editor-server 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -24,6 +24,7 @@ declare class EditorServer implements DevServerProtocol {
24
24
  onHasImage(callback: (params: HasImageMessage) => Promise<HasImageResponse>): void;
25
25
  private handlePortDiscovery;
26
26
  private setupSocketHandlers;
27
+ private isLocalhost;
27
28
  private handleProtocolMessage;
28
29
  }
29
30
  declare function createEditorServer(options: EditorServerOptions): EditorServer;
@@ -1,15 +1,13 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
3
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
3
  var __publicField = (obj, key, value) => {
5
4
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
5
  return value;
7
6
  };
8
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
9
- const socket_io = require("socket.io");
10
- const http = require("http");
11
- const getPort = require("get-port");
12
- const editorProtocol = require("@jay-framework/editor-protocol");
7
+ import { Server } from "socket.io";
8
+ import { createServer } from "http";
9
+ import getPort from "get-port";
10
+ import { createProtocolResponse } from "@jay-framework/editor-protocol";
13
11
  class EditorServer {
14
12
  constructor(options) {
15
13
  __publicField(this, "io", null);
@@ -25,7 +23,18 @@ class EditorServer {
25
23
  }
26
24
  async start() {
27
25
  this.port = await getPort({ port: this.portRange });
28
- this.httpServer = http.createServer((req, res) => {
26
+ this.httpServer = createServer((req, res) => {
27
+ const clientIP = req.socket.remoteAddress || req.connection.remoteAddress;
28
+ if (!this.isLocalhost(clientIP)) {
29
+ console.warn(`Rejected connection from non-localhost IP: ${clientIP}`);
30
+ res.writeHead(403, { "Content-Type": "application/json" });
31
+ res.end(
32
+ JSON.stringify({
33
+ error: "Access denied: Only localhost connections are allowed"
34
+ })
35
+ );
36
+ return;
37
+ }
29
38
  if (req.url?.startsWith("/editor-connect")) {
30
39
  this.handlePortDiscovery(req, res);
31
40
  } else {
@@ -33,11 +42,12 @@ class EditorServer {
33
42
  res.end("Not Found");
34
43
  }
35
44
  });
36
- this.io = new socket_io.Server(this.httpServer, {
45
+ this.io = new Server(this.httpServer, {
37
46
  cors: {
38
- origin: "*",
47
+ origin: ["http://localhost:*", "http://127.0.0.1:*"],
39
48
  methods: ["GET", "POST"]
40
- }
49
+ },
50
+ allowEIO3: true
41
51
  });
42
52
  this.setupSocketHandlers();
43
53
  return new Promise((resolve, reject) => {
@@ -90,7 +100,13 @@ class EditorServer {
90
100
  if (!this.io)
91
101
  return;
92
102
  this.io.on("connection", (socket) => {
93
- console.log(`Editor connected: ${socket.id}`);
103
+ const clientIP = socket.handshake.address;
104
+ if (!this.isLocalhost(clientIP)) {
105
+ console.warn(`Rejected WebSocket connection from non-localhost IP: ${clientIP}`);
106
+ socket.disconnect(true);
107
+ return;
108
+ }
109
+ console.log(`Editor connected: ${socket.id} from ${clientIP}`);
94
110
  socket.on("protocol-message", async (message) => {
95
111
  try {
96
112
  const response = await this.handleProtocolMessage(message);
@@ -101,7 +117,7 @@ class EditorServer {
101
117
  success: false,
102
118
  error: error instanceof Error ? error.message : "Unknown error"
103
119
  };
104
- const errorResponse = editorProtocol.createProtocolResponse(message.id, errorPayload);
120
+ const errorResponse = createProtocolResponse(message.id, errorPayload);
105
121
  socket.emit("protocol-response", errorResponse);
106
122
  }
107
123
  });
@@ -110,6 +126,9 @@ class EditorServer {
110
126
  });
111
127
  });
112
128
  }
129
+ isLocalhost(ip) {
130
+ return ip === "127.0.0.1" || ip === "localhost" || ip === "::1" || ip === "::ffff:127.0.0.1";
131
+ }
113
132
  async handleProtocolMessage(message) {
114
133
  const { id, payload } = message;
115
134
  switch (payload.type) {
@@ -118,19 +137,19 @@ class EditorServer {
118
137
  throw new Error("Publish handler not registered");
119
138
  }
120
139
  const publishResult = await this.handlers.publish(payload);
121
- return editorProtocol.createProtocolResponse(id, publishResult);
140
+ return createProtocolResponse(id, publishResult);
122
141
  case "saveImage":
123
142
  if (!this.handlers.saveImage) {
124
143
  throw new Error("Save image handler not registered");
125
144
  }
126
145
  const saveResult = await this.handlers.saveImage(payload);
127
- return editorProtocol.createProtocolResponse(id, saveResult);
146
+ return createProtocolResponse(id, saveResult);
128
147
  case "hasImage":
129
148
  if (!this.handlers.hasImage) {
130
149
  throw new Error("Has image handler not registered");
131
150
  }
132
151
  const hasResult = await this.handlers.hasImage(payload);
133
- return editorProtocol.createProtocolResponse(id, hasResult);
152
+ return createProtocolResponse(id, hasResult);
134
153
  default:
135
154
  throw new Error(`Unknown message type: ${payload.type}`);
136
155
  }
@@ -139,5 +158,7 @@ class EditorServer {
139
158
  function createEditorServer(options) {
140
159
  return new EditorServer(options);
141
160
  }
142
- exports.EditorServer = EditorServer;
143
- exports.createEditorServer = createEditorServer;
161
+ export {
162
+ EditorServer,
163
+ createEditorServer
164
+ };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@jay-framework/editor-server",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
- "main": "dist/index.cjs",
6
+ "main": "dist/index.js",
7
7
  "files": [
8
8
  "dist",
9
9
  "readme.md"
@@ -20,16 +20,16 @@
20
20
  "test:watch": "vitest"
21
21
  },
22
22
  "dependencies": {
23
- "@jay-framework/editor-protocol": "^0.6.0",
23
+ "@jay-framework/editor-protocol": "^0.6.1",
24
24
  "get-port": "^7.0.0",
25
25
  "socket.io": "^4.7.4",
26
26
  "uuid": "^9.0.1",
27
27
  "yaml": "^2.3.4"
28
28
  },
29
29
  "devDependencies": {
30
- "@jay-framework/dev-environment": "^0.6.0",
31
- "@jay-framework/editor-client": "^0.6.0",
32
- "@jay-framework/jay-cli": "^0.6.0",
30
+ "@jay-framework/dev-environment": "^0.6.1",
31
+ "@jay-framework/editor-client": "^0.6.1",
32
+ "@jay-framework/jay-cli": "^0.6.1",
33
33
  "@types/express": "^5.0.2",
34
34
  "@types/node": "^22.15.21",
35
35
  "@types/uuid": "^9.0.7",