coralite-scripts 0.36.2 → 0.36.3

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/libs/server.js +92 -63
  2. package/package.json +4 -3
package/libs/server.js CHANGED
@@ -14,6 +14,90 @@ import portfinder from 'portfinder'
14
14
  * @import {CoraliteScriptConfig, CoraliteScriptOptions} from '../types/index.js'
15
15
  */
16
16
 
17
+ /**
18
+ * Resolves the requested path to a physical file or a virtual page.
19
+ *
20
+ * @param {string} reqPath - The requested URL path.
21
+ * @param {string} extension - The extension of the requested path.
22
+ * @param {CoraliteScriptConfig} config - The Coralite configuration.
23
+ * @param {any} coralite - The Coralite instance.
24
+ * @param {Map<string, string>} memoryPageSource - Map of in-memory page sources.
25
+ * @returns {Promise<{pathname: string, key: string}|null>}
26
+ */
27
+ export async function resolveSource (reqPath, extension, config, coralite, memoryPageSource) {
28
+ const candidates = []
29
+
30
+ // Ensure relative path doesn't start with / for joining
31
+ const relPath = reqPath.startsWith('/') ? reqPath.slice(1) : reqPath
32
+
33
+ if (reqPath.endsWith('/')) {
34
+ const key = join(relPath, 'index.html')
35
+ candidates.push({
36
+ path: join(config.pages, key),
37
+ key
38
+ })
39
+ } else if (extension === '.html') {
40
+ const key = relPath
41
+ candidates.push({
42
+ path: join(config.pages, key),
43
+ key
44
+ })
45
+ } else {
46
+ // No extension, no trailing slash
47
+ const key1 = relPath + '.html'
48
+ candidates.push({
49
+ path: join(config.pages, key1),
50
+ key: key1
51
+ })
52
+
53
+ const key2 = join(relPath, 'index.html')
54
+ candidates.push({
55
+ path: join(config.pages, key2),
56
+ key: key2
57
+ })
58
+ }
59
+
60
+ for (const candidate of candidates) {
61
+ try {
62
+ await access(candidate.path, constants.R_OK)
63
+ // Normalize key for consistency (use forward slashes)
64
+ const normalizedKey = candidate.key.split(sep).join('/')
65
+ return {
66
+ pathname: candidate.path,
67
+ key: normalizedKey
68
+ }
69
+ } catch {
70
+ // continue
71
+ }
72
+ }
73
+
74
+ // Fallback check coralite pages collection (supports virtual pages)
75
+ for (const candidate of candidates) {
76
+ const item = coralite.pages.getItem(candidate.path)
77
+
78
+ if (item) {
79
+ const normalizedKey = candidate.key.split(sep).join('/')
80
+ return {
81
+ pathname: candidate.path,
82
+ key: normalizedKey
83
+ }
84
+ }
85
+ }
86
+
87
+ // Fallback check memoryPageSource
88
+ for (const candidate of candidates) {
89
+ const normalizedKey = candidate.key.split(sep).join('/')
90
+ if (memoryPageSource.has(normalizedKey)) {
91
+ return {
92
+ pathname: memoryPageSource.get(normalizedKey),
93
+ key: normalizedKey
94
+ }
95
+ }
96
+ }
97
+
98
+ return null
99
+ }
100
+
17
101
  /**
18
102
  * Starts a development server with hot-reloading capabilities
19
103
  * @param {CoraliteScriptConfig} config - Coralite configuration
@@ -261,68 +345,7 @@ async function server (config, options) {
261
345
  return res.sendStatus(404)
262
346
  }
263
347
 
264
- const resolveSource = async () => {
265
- const candidates = []
266
-
267
- // Ensure relative path doesn't start with / for joining
268
- const relPath = reqPath.startsWith('/') ? reqPath.slice(1) : reqPath
269
-
270
- if (reqPath.endsWith('/')) {
271
- const key = join(relPath, 'index.html')
272
- candidates.push({
273
- path: join(config.pages, key),
274
- key
275
- })
276
- } else if (extension === '.html') {
277
- const key = relPath
278
- candidates.push({
279
- path: join(config.pages, key),
280
- key
281
- })
282
- } else {
283
- // No extension, no trailing slash
284
- const key1 = relPath + '.html'
285
- candidates.push({
286
- path: join(config.pages, key1),
287
- key: key1
288
- })
289
-
290
- const key2 = join(relPath, 'index.html')
291
- candidates.push({
292
- path: join(config.pages, key2),
293
- key: key2
294
- })
295
- }
296
-
297
- for (const candidate of candidates) {
298
- try {
299
- await access(candidate.path, constants.R_OK)
300
- // Normalize key for consistency (use forward slashes)
301
- const normalizedKey = candidate.key.split(sep).join('/')
302
- return {
303
- pathname: candidate.path,
304
- key: normalizedKey
305
- }
306
- } catch {
307
- // continue
308
- }
309
- }
310
-
311
- // Fallback check memoryPageSource
312
- for (const candidate of candidates) {
313
- const normalizedKey = candidate.key.split(sep).join('/')
314
- if (memoryPageSource.has(normalizedKey)) {
315
- return {
316
- pathname: memoryPageSource.get(normalizedKey),
317
- key: normalizedKey
318
- }
319
- }
320
- }
321
-
322
- return null
323
- }
324
-
325
- const result = await resolveSource()
348
+ const result = await resolveSource(reqPath, extension, config, coralite, memoryPageSource)
326
349
 
327
350
  if (!result) {
328
351
  res.sendStatus(404)
@@ -350,7 +373,13 @@ async function server (config, options) {
350
373
  rebuildScript += ' </script>\n'
351
374
  rebuildScript += '</body>\n'
352
375
 
353
- await coralite.pages.setItem(pathname)
376
+ // Only set item if it's not already in the collection (virtual pages are pre-registered)
377
+ const item = coralite.pages.getItem(pathname)
378
+
379
+ if (!item || item.physical !== false) {
380
+ await coralite.pages.setItem(pathname)
381
+ }
382
+
354
383
  // build the HTML for this page using the built-in compiler.
355
384
  const documents = await coralite.build(pathname, async (result) => {
356
385
  // inject a script to enable live reload via Server-Sent Events
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coralite-scripts",
3
- "version": "0.36.2",
3
+ "version": "0.36.3",
4
4
  "description": "Configuration and scripts for Create Coralite.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -61,10 +61,11 @@
61
61
  "portfinder": "^1.0.38",
62
62
  "postcss": "^8.5.6",
63
63
  "sass": "^1.91.0",
64
- "coralite": "0.36.2"
64
+ "coralite": "0.36.3"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "premove dist && pnpm build-types",
68
- "build-types": "tsc"
68
+ "build-types": "tsc",
69
+ "test-unit": "node --test tests/**/*.spec.js"
69
70
  }
70
71
  }