@nitronjs/framework 0.2.5 → 0.2.7
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/lib/HMR/Server.js +68 -8
- package/lib/View/View.js +1 -1
- 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,18 +32,18 @@ class HMRServer {
|
|
|
29
32
|
* @param {import("fastify").FastifyInstance} fastify
|
|
30
33
|
*/
|
|
31
34
|
registerRoutes(fastify) {
|
|
32
|
-
|
|
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
|
-
|
|
38
|
-
if (!this.#clientScript
|
|
39
|
-
return reply.code(
|
|
37
|
+
const handler = (req, reply) => {
|
|
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"));
|
|
43
|
-
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Support both paths for cache compatibility
|
|
46
|
+
fastify.get("/__nitron_client/socket.io.js", handler);
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -153,6 +156,63 @@ class HMRServer {
|
|
|
153
156
|
|
|
154
157
|
this.#connections = 0;
|
|
155
158
|
}
|
|
159
|
+
|
|
160
|
+
// Private Methods
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Finds socket.io client script from multiple possible locations.
|
|
164
|
+
* Searches in order: createRequire resolution, cwd node_modules,
|
|
165
|
+
* parent directories, and monorepo root.
|
|
166
|
+
* @returns {string|null} Path to socket.io.min.js or null if not found
|
|
167
|
+
*/
|
|
168
|
+
#findSocketIoClient() {
|
|
169
|
+
const clientFile = "client-dist/socket.io.min.js";
|
|
170
|
+
const possiblePaths = [];
|
|
171
|
+
|
|
172
|
+
// 1. Try resolving from framework package location
|
|
173
|
+
try {
|
|
174
|
+
const frameworkRequire = createRequire(import.meta.url);
|
|
175
|
+
const socketIoDir = path.dirname(frameworkRequire.resolve("socket.io/package.json"));
|
|
176
|
+
possiblePaths.push(path.join(socketIoDir, clientFile));
|
|
177
|
+
}
|
|
178
|
+
catch {}
|
|
179
|
+
|
|
180
|
+
// 2. Try resolving from project's node_modules
|
|
181
|
+
try {
|
|
182
|
+
const projectRequire = createRequire(path.join(process.cwd(), "package.json"));
|
|
183
|
+
const socketIoDir = path.dirname(projectRequire.resolve("socket.io/package.json"));
|
|
184
|
+
possiblePaths.push(path.join(socketIoDir, clientFile));
|
|
185
|
+
}
|
|
186
|
+
catch {}
|
|
187
|
+
|
|
188
|
+
// 3. Walk up from cwd looking for node_modules/socket.io
|
|
189
|
+
let currentDir = process.cwd();
|
|
190
|
+
for (let i = 0; i < 5; i++) {
|
|
191
|
+
possiblePaths.push(path.join(currentDir, "node_modules", "socket.io", clientFile));
|
|
192
|
+
const parentDir = path.dirname(currentDir);
|
|
193
|
+
if (parentDir === currentDir) break;
|
|
194
|
+
currentDir = parentDir;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// 4. Walk up from framework package location
|
|
198
|
+
currentDir = __dirname;
|
|
199
|
+
for (let i = 0; i < 5; i++) {
|
|
200
|
+
possiblePaths.push(path.join(currentDir, "node_modules", "socket.io", clientFile));
|
|
201
|
+
const parentDir = path.dirname(currentDir);
|
|
202
|
+
if (parentDir === currentDir) break;
|
|
203
|
+
currentDir = parentDir;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Find first existing path
|
|
207
|
+
for (const p of possiblePaths) {
|
|
208
|
+
if (fs.existsSync(p)) {
|
|
209
|
+
return p;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
console.warn("[HMR] socket.io client not found. Searched paths:", possiblePaths);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
156
216
|
}
|
|
157
217
|
|
|
158
218
|
export default new HMRServer();
|
package/lib/View/View.js
CHANGED
|
@@ -689,7 +689,7 @@ class View {
|
|
|
689
689
|
: "";
|
|
690
690
|
|
|
691
691
|
const hmrScript = this.#isDev
|
|
692
|
-
? `<script src="/
|
|
692
|
+
? `<script src="/__nitron_client/socket.io.js"${nonceAttr}></script><script src="/storage/js/hmr.js"${nonceAttr}></script>`
|
|
693
693
|
: "";
|
|
694
694
|
|
|
695
695
|
const hydrateScript = hasHydration
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nitronjs/framework",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
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"
|