@nitronjs/framework 0.2.5 → 0.2.6

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.
Files changed (2) hide show
  1. package/lib/HMR/Server.js +63 -6
  2. package/package.json +1 -1
package/lib/HMR/Server.js CHANGED
@@ -2,6 +2,9 @@ import { Server as SocketServer } from "socket.io";
2
2
  import { createRequire } from "module";
3
3
  import path from "path";
4
4
  import fs from "fs";
5
+ import { fileURLToPath } from "url";
6
+
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
5
8
 
6
9
  /**
7
10
  * HMR (Hot Module Replacement) server for development mode.
@@ -29,14 +32,11 @@ class HMRServer {
29
32
  * @param {import("fastify").FastifyInstance} fastify
30
33
  */
31
34
  registerRoutes(fastify) {
32
- const require = createRequire(import.meta.url);
33
- const socketIoDir = path.dirname(require.resolve("socket.io/package.json"));
34
-
35
- this.#clientScript = path.join(socketIoDir, "client-dist", "socket.io.min.js");
35
+ this.#clientScript = this.#findSocketIoClient();
36
36
 
37
37
  fastify.get("/__nitron_hmr/socket.io.js", (req, reply) => {
38
- if (!this.#clientScript || !fs.existsSync(this.#clientScript)) {
39
- return reply.code(404).send("Socket.io client not found");
38
+ if (!this.#clientScript) {
39
+ return reply.code(503).send("// HMR disabled: socket.io client not found");
40
40
  }
41
41
 
42
42
  reply.type("application/javascript").send(fs.readFileSync(this.#clientScript, "utf-8"));
@@ -153,6 +153,63 @@ class HMRServer {
153
153
 
154
154
  this.#connections = 0;
155
155
  }
156
+
157
+ // Private Methods
158
+
159
+ /**
160
+ * Finds socket.io client script from multiple possible locations.
161
+ * Searches in order: createRequire resolution, cwd node_modules,
162
+ * parent directories, and monorepo root.
163
+ * @returns {string|null} Path to socket.io.min.js or null if not found
164
+ */
165
+ #findSocketIoClient() {
166
+ const clientFile = "client-dist/socket.io.min.js";
167
+ const possiblePaths = [];
168
+
169
+ // 1. Try resolving from framework package location
170
+ try {
171
+ const frameworkRequire = createRequire(import.meta.url);
172
+ const socketIoDir = path.dirname(frameworkRequire.resolve("socket.io/package.json"));
173
+ possiblePaths.push(path.join(socketIoDir, clientFile));
174
+ }
175
+ catch {}
176
+
177
+ // 2. Try resolving from project's node_modules
178
+ try {
179
+ const projectRequire = createRequire(path.join(process.cwd(), "package.json"));
180
+ const socketIoDir = path.dirname(projectRequire.resolve("socket.io/package.json"));
181
+ possiblePaths.push(path.join(socketIoDir, clientFile));
182
+ }
183
+ catch {}
184
+
185
+ // 3. Walk up from cwd looking for node_modules/socket.io
186
+ let currentDir = process.cwd();
187
+ for (let i = 0; i < 5; i++) {
188
+ possiblePaths.push(path.join(currentDir, "node_modules", "socket.io", clientFile));
189
+ const parentDir = path.dirname(currentDir);
190
+ if (parentDir === currentDir) break;
191
+ currentDir = parentDir;
192
+ }
193
+
194
+ // 4. Walk up from framework package location
195
+ currentDir = __dirname;
196
+ for (let i = 0; i < 5; i++) {
197
+ possiblePaths.push(path.join(currentDir, "node_modules", "socket.io", clientFile));
198
+ const parentDir = path.dirname(currentDir);
199
+ if (parentDir === currentDir) break;
200
+ currentDir = parentDir;
201
+ }
202
+
203
+ // Find first existing path
204
+ for (const p of possiblePaths) {
205
+ if (fs.existsSync(p)) {
206
+ return p;
207
+ }
208
+ }
209
+
210
+ console.warn("[HMR] socket.io client not found. Searched paths:", possiblePaths);
211
+ return null;
212
+ }
156
213
  }
157
214
 
158
215
  export default new HMRServer();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitronjs/framework",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "NitronJS is a modern and extensible Node.js MVC framework built on Fastify. It focuses on clean architecture, modular structure, and developer productivity, offering built-in routing, middleware, configuration management, CLI tooling, and native React integration for scalable full-stack applications.",
5
5
  "bin": {
6
6
  "njs": "./cli/njs.js"