@nuraly/lumenjs 0.5.0 → 0.6.0

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.
@@ -9,17 +9,24 @@ export async function buildServer(opts) {
9
9
  // Include all pages in server build (enables SSR for .md endpoints)
10
10
  for (const entry of pageEntries) {
11
11
  serverEntries[`pages/${entry.name}`] = entry.filePath;
12
- // Co-located _loader.ts for folder index pages
12
+ // Co-located _loader.ts / _socket.ts for folder index pages
13
13
  if (path.basename(entry.filePath).replace(/\.(ts|js)$/, '') === 'index') {
14
14
  const dir = path.dirname(entry.filePath);
15
+ const entryDir = path.dirname(entry.name);
15
16
  for (const ext of ['.ts', '.js']) {
16
17
  const loaderFile = path.join(dir, `_loader${ext}`);
17
18
  if (fs.existsSync(loaderFile)) {
18
- const entryDir = path.dirname(entry.name);
19
19
  serverEntries[`pages/${entryDir}/_loader`] = loaderFile;
20
20
  break;
21
21
  }
22
22
  }
23
+ for (const ext of ['.ts', '.js']) {
24
+ const socketFile = path.join(dir, `_socket${ext}`);
25
+ if (fs.existsSync(socketFile)) {
26
+ serverEntries[`pages/${entryDir}/_socket`] = socketFile;
27
+ break;
28
+ }
29
+ }
23
30
  }
24
31
  }
25
32
  for (const entry of layoutEntries) {
@@ -14,13 +14,16 @@ function analyzePageFile(filePath) {
14
14
  return false;
15
15
  return true;
16
16
  };
17
- const hasColocatedLoader = path.basename(filePath).replace(/\.(ts|js)$/, '') === 'index' &&
18
- (fs.existsSync(path.join(path.dirname(filePath), '_loader.ts')) ||
19
- fs.existsSync(path.join(path.dirname(filePath), '_loader.js')));
17
+ const isIndex = path.basename(filePath).replace(/\.(ts|js)$/, '') === 'index';
18
+ const dir = path.dirname(filePath);
19
+ const hasColocatedLoader = isIndex &&
20
+ (fs.existsSync(path.join(dir, '_loader.ts')) || fs.existsSync(path.join(dir, '_loader.js')));
21
+ const hasColocatedSocket = isIndex &&
22
+ (fs.existsSync(path.join(dir, '_socket.ts')) || fs.existsSync(path.join(dir, '_socket.js')));
20
23
  return {
21
24
  hasLoader: hasExportBefore(/export\s+(async\s+)?function\s+loader\s*\(/) || hasColocatedLoader,
22
25
  hasSubscribe: hasExportBefore(/export\s+(async\s+)?function\s+subscribe\s*\(/),
23
- hasSocket: /export\s+(function|const)\s+socket[\s(=]/.test(content),
26
+ hasSocket: /export\s+(function|const)\s+socket[\s(=]/.test(content) || hasColocatedSocket,
24
27
  hasAuth: hasExportBefore(/export\s+const\s+auth\s*=/),
25
28
  hasMeta: hasExportBefore(/export\s+(const\s+meta\s*=|(async\s+)?function\s+meta\s*\()/),
26
29
  hasStandalone: hasExportBefore(/export\s+const\s+standalone\s*=/),
@@ -1,5 +1,7 @@
1
1
  import { createRequire } from 'module';
2
2
  import { pathToFileURL } from 'url';
3
+ import fs from 'fs';
4
+ import path from 'path';
3
5
  export async function setupSocketIO(options) {
4
6
  // Resolve socket.io from the project's node_modules (not from lumenjs's own node_modules)
5
7
  let SocketIOServer;
@@ -23,7 +25,20 @@ export async function setupSocketIO(options) {
23
25
  io.of(ns).on('connection', async (socket) => {
24
26
  try {
25
27
  const mod = await options.loadModule(route.filePath);
26
- if (!mod.socket)
28
+ let socketFn = mod?.socket;
29
+ // Co-located _socket.ts fallback (folder route convention)
30
+ if (!socketFn && path.basename(route.filePath).replace(/\.(ts|js)$/, '') === 'index') {
31
+ const dir = path.dirname(route.filePath);
32
+ for (const ext of ['.ts', '.js']) {
33
+ const colocated = path.join(dir, `_socket${ext}`);
34
+ if (fs.existsSync(colocated)) {
35
+ const socketMod = await options.loadModule(colocated);
36
+ socketFn = socketMod?.socket;
37
+ break;
38
+ }
39
+ }
40
+ }
41
+ if (!socketFn)
27
42
  return;
28
43
  const push = (data) => socket.emit('nk:data', data);
29
44
  const on = (event, handler) => {
@@ -38,7 +53,7 @@ export async function setupSocketIO(options) {
38
53
  const params = socket.handshake.query.__params
39
54
  ? JSON.parse(socket.handshake.query.__params) : {};
40
55
  const locale = socket.handshake.query.__locale;
41
- const cleanup = mod.socket({ on, push, room, params, headers: socket.handshake.headers, locale, socket });
56
+ const cleanup = socketFn({ on, push, room, params, headers: socket.handshake.headers, locale, socket });
42
57
  socket.on('disconnect', () => { if (typeof cleanup === 'function')
43
58
  cleanup(); });
44
59
  }
@@ -228,6 +228,13 @@ export function fileHasMeta(filePath) {
228
228
  */
229
229
  export function fileHasSocket(filePath) {
230
230
  try {
231
+ // Check for co-located _socket.ts (folder route convention: index.ts + _socket.ts)
232
+ if (path.basename(filePath).replace(/\.(ts|js)$/, '') === 'index') {
233
+ const dir = path.dirname(filePath);
234
+ if (fs.existsSync(path.join(dir, '_socket.ts')) || fs.existsSync(path.join(dir, '_socket.js'))) {
235
+ return true;
236
+ }
237
+ }
231
238
  const content = fs.readFileSync(filePath, 'utf-8');
232
239
  return /export\s+(function|const)\s+socket[\s(=]/.test(content);
233
240
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuraly/lumenjs",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Full-stack Lit web component framework with file-based routing, server loaders, SSR, and API routes",
5
5
  "type": "module",
6
6
  "main": "dist/cli.js",