@webjsdev/server 0.8.30 → 0.8.31

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webjsdev/server",
3
- "version": "0.8.30",
3
+ "version": "0.8.31",
4
4
  "type": "module",
5
5
  "description": "webjs dev/prod server: SSR, router, API, server actions, live reload",
6
6
  "main": "index.js",
package/src/asset-hash.js CHANGED
@@ -39,9 +39,9 @@
39
39
 
40
40
  import { createHash } from 'node:crypto';
41
41
  import { readFileSync } from 'node:fs';
42
- import { extname, join, resolve, sep } from 'node:path';
42
+ import { dirname, extname, join, relative, resolve, sep } from 'node:path';
43
43
  import { redactStringsAndTemplates } from './js-scan.js';
44
- import { resolveImport } from './module-graph.js';
44
+ import { resolveImport, expandImportAlias } from './module-graph.js';
45
45
 
46
46
  /**
47
47
  * Length of the emitted hex hash. 12 hex chars (48 bits) is plenty to
@@ -306,6 +306,24 @@ const JS_SPEC_EXTS = new Set(['.js', '.mjs', '.jsx', '.cjs']);
306
306
  * @param {string} abs the resolved absolute file path
307
307
  * @returns {string}
308
308
  */
309
+ /**
310
+ * A base-path-safe relative specifier from the importing module to a resolved
311
+ * target file, used to rewrite a `#`-alias import in served source (#555). The
312
+ * browser resolves a relative specifier against the importer's own URL (already
313
+ * base-path-prefixed), so the result needs no importmap and no basePath fix-up,
314
+ * and it carries the `?v` the caller appends. Uses the resolved file's actual
315
+ * extension (so no `normalizeSpecToResolved` pass is needed).
316
+ *
317
+ * @param {string} importerAbs absolute path of the module being served
318
+ * @param {string} abs resolved absolute path of the import target
319
+ * @returns {string}
320
+ */
321
+ function relativeSpecifier(importerAbs, abs) {
322
+ let rel = relative(dirname(importerAbs), abs).split(sep).join('/');
323
+ if (!rel.startsWith('.')) rel = './' + rel;
324
+ return rel;
325
+ }
326
+
309
327
  function normalizeSpecToResolved(spec, abs) {
310
328
  const absExt = extname(abs);
311
329
  if (!absExt) return spec;
@@ -391,9 +409,15 @@ export function versionModuleImports(source, importerAbs) {
391
409
  // inside a string / template / comment and is not a real import edge.
392
410
  if (masked[/** @type {number} */ (m.index)] === ' ') continue;
393
411
  const spec = m[2];
394
- // Only `./` `../` relative specifiers (base-path-correct, the app pattern).
395
- // Bare -> importmap target; `/`-absolute -> pre-existing basePath gap.
396
- if (spec[0] !== '.') continue;
412
+ const isRelative = spec[0] === '.';
413
+ // A `#`-style subpath alias (#555) is rewritten to a base-path-safe
414
+ // RELATIVE specifier below (so it carries `?v` and matches the preload,
415
+ // exactly like a relative import). Only `./`/`../` relatives and
416
+ // app-imports aliases qualify: a bare specifier is importmap-resolved and
417
+ // versioned at its target; a `/`-root-absolute specifier would miss the
418
+ // basePath prefix.
419
+ const isAlias = !isRelative && expandImportAlias(spec, _appDir) !== null;
420
+ if (!isRelative && !isAlias) continue;
397
421
  if (spec.includes('?')) continue;
398
422
  const abs = resolveImport(spec, importerAbs, _appDir);
399
423
  if (!abs || SERVER_FILE_RE.test(abs)) continue;
@@ -402,9 +426,14 @@ export function versionModuleImports(source, importerAbs) {
402
426
  if (!hash) continue;
403
427
  // `/d` flag: indices[2] is [start, end] of the specifier CONTENT (no
404
428
  // quotes). Replace it with the resolved-extension form + `?v` so the
405
- // fetched URL is byte-identical to the preload href.
429
+ // fetched URL is byte-identical to the preload href. An alias is emitted
430
+ // as a relative specifier to the resolved file (the browser resolves it
431
+ // against the importer's already-prefixed URL, so it stays base-path-safe
432
+ // without the importmap, and the `?v` collapses fetch + preload to one
433
+ // immutable cache key).
406
434
  const [start, end] = /** @type {[number, number]} */ (m.indices[2]);
407
- edits.push({ start, end, text: `${normalizeSpecToResolved(spec, abs)}?v=${hash}` });
435
+ const versioned = isAlias ? relativeSpecifier(importerAbs, abs) : normalizeSpecToResolved(spec, abs);
436
+ edits.push({ start, end, text: `${versioned}?v=${hash}` });
408
437
  }
409
438
  }
410
439
  if (edits.length === 0) return source;
package/src/dev.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createServer as createHttp1Server } from 'node:http';
2
2
  import { stat, readFile, watch as fsWatch } from 'node:fs/promises';
3
- import { existsSync, readFileSync } from 'node:fs';
3
+ import { existsSync, readFileSync, readdirSync } from 'node:fs';
4
4
  import { join, extname, resolve, dirname, relative, sep } from 'node:path';
5
5
  import { createRequire } from 'node:module';
6
6
  import { fileURLToPath, pathToFileURL } from 'node:url';
@@ -45,7 +45,7 @@ import {
45
45
  varyWithAcceptEncoding,
46
46
  } from './listener-core.js';
47
47
  import { scanBareImports, resolveVendorImports, serveDownloadedBundle, clearVendorCache, hasVendorPin, readPinFile, prunePinToReachable } from './vendor.js';
48
- import { buildModuleGraph, transitiveDeps, reachableFromEntries, resolveImport } from './module-graph.js';
48
+ import { buildModuleGraph, transitiveDeps, reachableFromEntries, resolveImport, appImportsMap } from './module-graph.js';
49
49
  import { primeComponentRegistry, findOrphanComponents, scanComponents } from './component-scanner.js';
50
50
  import { analyzeElision, elideImportsFromSource } from './component-elision.js';
51
51
 
@@ -85,7 +85,7 @@ function resolveRequestId(req) {
85
85
  function shouldAccessLog(pathname) {
86
86
  return !pathname.startsWith('/__webjs/');
87
87
  }
88
- import { setVendorEntries, setCoreInstall, publishBuildId, setBasePath, basePath } from './importmap.js';
88
+ import { setVendorEntries, setCoreInstall, publishBuildId, setBasePath, basePath, setImportAliasEntries, importAliasBrowserEntries } from './importmap.js';
89
89
  import { readBasePath, stripBasePath, withBasePath } from './base-path.js';
90
90
  import { setAssetRoots, clearAssetHashCache, setElisionFingerprint, withAssetHash, assetHashFor, versionModuleImports } from './asset-hash.js';
91
91
  import { urlFromRequest } from './forwarded.js';
@@ -493,6 +493,16 @@ export async function createRequestHandler(opts) {
493
493
  existsSync(join(distDir, 'webjs-core-browser.js'));
494
494
  await setCoreInstall(coreDir, distComplete);
495
495
 
496
+ // Path-alias imports (#555). Emit the browser importmap scopes for the app's
497
+ // `package.json "imports"` aliases, derived from the SAME map the server
498
+ // resolver (`expandImportAlias`) reads, so SSR and the browser agree. The
499
+ // scaffold ships the single catch-all `"#*": "./*"` (one key, zero
500
+ // maintenance, Bun-safe), which expands to one browser prefix scope per
501
+ // top-level dir (`#lib/` -> `/lib/`, ...); the dir scan is here so a new
502
+ // folder is picked up on boot. An app with no `"imports"` block yields {}
503
+ // (a no-op, byte-identical to before this feature).
504
+ await setImportAliasEntries(importAliasBrowserEntries(appImportsMap(appDir), appTopLevelDirs(appDir)));
505
+
496
506
  // Content-hash asset URLs for immutable caching (issue #243). Bind the
497
507
  // asset-hash module to the app + core roots and enable fingerprinting in
498
508
  // PROD only. In dev `withAssetHash` stays a pure no-op (never enabled), so
@@ -2361,6 +2371,26 @@ function computeBrowserBoundFiles(routeTable, moduleGraph, components, appDir) {
2361
2371
  return reachableFromEntries(moduleGraph, [...entries], appDir);
2362
2372
  }
2363
2373
 
2374
+ /**
2375
+ * List the app's top-level source directory names, for expanding a `#*`
2376
+ * catch-all import alias into one browser importmap prefix scope per dir (#555).
2377
+ * Excludes infra dirs that are never imported via `#` (node_modules, dotfiles,
2378
+ * the build/vendor caches). A new top-level folder is picked up on the next boot
2379
+ * (dev restarts on changes), so the alias stays zero-maintenance.
2380
+ * @param {string} appDir
2381
+ * @returns {string[]}
2382
+ */
2383
+ function appTopLevelDirs(appDir) {
2384
+ const SKIP = new Set(['node_modules', 'dist', 'public']);
2385
+ try {
2386
+ return readdirSync(appDir, { withFileTypes: true })
2387
+ .filter((e) => e.isDirectory() && !e.name.startsWith('.') && !SKIP.has(e.name))
2388
+ .map((e) => e.name);
2389
+ } catch {
2390
+ return [];
2391
+ }
2392
+ }
2393
+
2364
2394
  /**
2365
2395
  * Find the absolute directory of the `@webjsdev/core` package, regardless of
2366
2396
  * whether we're running from the monorepo or an installed copy.
package/src/importmap.js CHANGED
@@ -28,6 +28,17 @@ function escapeAttr(s) {
28
28
  /** @type {Record<string, string>} */
29
29
  let _extraEntries = {};
30
30
 
31
+ /**
32
+ * Browser importmap entries for the app's `package.json "imports"` subpath
33
+ * aliases (#555), e.g. `{ "#lib/": "/lib/" }` for `"#lib/*": "./lib/*"`. Kept in lockstep
34
+ * with the server-side resolver (`module-graph.js`'s `expandImportAlias`):
35
+ * BOTH are derived from the one `"imports"` map, so an alias that resolves in
36
+ * SSR never 404s in the browser (the #158/#159 preload-mismatch class). Set at
37
+ * boot by `dev.js` via `setImportAliasEntries`.
38
+ * @type {Record<string, string>}
39
+ */
40
+ let _aliasEntries = {};
41
+
31
42
  /**
32
43
  * The normalized `webjs.basePath` for a sub-path deployment (issue #256),
33
44
  * `''` (the default) for a root mount. When non-empty, every same-origin
@@ -67,6 +78,65 @@ export function basePath() {
67
78
  return _basePath;
68
79
  }
69
80
 
81
+ /**
82
+ * Derive the BROWSER importmap entries for an app's `package.json "imports"`
83
+ * subpath-alias map (#555). The scaffold ships a single root catch-all key
84
+ * `"#*": "./*"` (one key, zero maintenance: a new top-level folder is aliased
85
+ * with no config change, and `#*` resolves natively on Node AND Bun, unlike a
86
+ * `#/`-prefixed key which Bun rejects). A browser importmap needs a
87
+ * trailing-slash PREFIX key to match, and a bare `#` is not one, so a catch-all
88
+ * is expanded into one prefix scope PER top-level directory (`topLevelDirs`):
89
+ * `#lib/` -> `/lib/`, `#components/` -> `/components/`, etc. The dirs are scanned
90
+ * by the caller (dev.js) so this stays pure; a new folder produces a new scope
91
+ * on the next boot. A non-catch-all key is mapped directly: a per-dir wildcard
92
+ * `"#lib/*": "./lib/*"` becomes `"#lib/": "/lib/"`, an exact `"#db": "./x.ts"`
93
+ * becomes `"#db": "/x.ts"`. The leading `./` becomes a root-absolute `/` and the
94
+ * `*` is dropped (the trailing slash carries the prefix match). A non-default
95
+ * base (`"#*": "./src/*"`) folds into the emitted URL. Only string targets are
96
+ * mappable (a conditional-export object is skipped). Derived from the SAME map
97
+ * the server resolver reads, so SSR and the browser agree.
98
+ *
99
+ * @param {Record<string, unknown> | null | undefined} importsMap
100
+ * @param {string[]} [topLevelDirs] app top-level dir names, for expanding a `#*` catch-all
101
+ * @returns {Record<string, string>}
102
+ */
103
+ export function importAliasBrowserEntries(importsMap, topLevelDirs = []) {
104
+ /** @type {Record<string, string>} */
105
+ const out = {};
106
+ if (!importsMap || typeof importsMap !== 'object') return out;
107
+ for (const [key, value] of Object.entries(importsMap)) {
108
+ if (typeof value !== 'string') continue;
109
+ if (!value.startsWith('./')) continue; // only app-root-relative targets map to a URL
110
+ const star = key.indexOf('*');
111
+ if (star !== -1 && key.slice(0, star) === '#') {
112
+ // Root catch-all `#*` -> `./<base>*`: expand to a prefix scope per dir.
113
+ // base is the part of the value between `.` and `*` (`'' ` for `./*`,
114
+ // `/src/` for `./src/*`), so `#<dir>/` -> `/<base><dir>/`.
115
+ const base = value.slice(1, value.lastIndexOf('*')); // './*' -> '/', './src/*' -> '/src/'
116
+ for (const dir of topLevelDirs) out[`#${dir}/`] = `${base}${dir}/`;
117
+ continue;
118
+ }
119
+ // Per-dir wildcard or exact key: map directly.
120
+ out[key.replace('*', '')] = value.replace(/^\./, '').replace('*', '');
121
+ }
122
+ return out;
123
+ }
124
+
125
+ /**
126
+ * Bind the browser importmap to the app's `package.json "imports"` aliases
127
+ * (#555). Called once at boot by `dev.js`, derived from the SAME `"imports"`
128
+ * map the server resolver reads, so SSR and the browser agree. The hash is
129
+ * recomputed eagerly (like `setBasePath` / `setCoreInstall`) so
130
+ * `importMapHash()` stays synchronous on the per-request path.
131
+ *
132
+ * @param {Record<string, string>} entries browser entries from `importAliasBrowserEntries`
133
+ * @returns {Promise<void>}
134
+ */
135
+ export async function setImportAliasEntries(entries) {
136
+ _aliasEntries = entries && typeof entries === 'object' ? entries : {};
137
+ _importMapHash = await digestHex('SHA-256', JSON.stringify(buildImportMap({ fingerprint: false })));
138
+ }
139
+
70
140
  /**
71
141
  * SRI integrity hashes keyed by FINAL URL (post-importmap-rewrite).
72
142
  * Populated only when a pin file with `integrity` is present;
@@ -338,6 +408,7 @@ export function buildImportMap(opts = {}) {
338
408
  const merged = {
339
409
  ..._coreEntries,
340
410
  ..._extraEntries,
411
+ ..._aliasEntries,
341
412
  };
342
413
  // Sort keys so logically-identical importmaps serialize byte-for-byte
343
414
  // identically. The client router compares textContent to detect
@@ -12,10 +12,71 @@
12
12
  */
13
13
 
14
14
  import { readFile, readdir, stat } from 'node:fs/promises';
15
- import { existsSync } from 'node:fs';
15
+ import { existsSync, readFileSync } from 'node:fs';
16
16
  import { join, resolve, dirname, extname, sep } from 'node:path';
17
17
  import { redactStringsAndTemplates } from './js-scan.js';
18
18
 
19
+ /**
20
+ * Per-appDir cache of the parsed `package.json "imports"` subpath-alias map
21
+ * (#555). `null` means "no imports block" (the common case for an app that
22
+ * does not use the `#` alias). Read once per appDir; cleared with the parse
23
+ * cache on rebuild.
24
+ * @type {Map<string, Record<string,string> | null>}
25
+ */
26
+ const IMPORTS_CACHE = new Map();
27
+
28
+ /**
29
+ * Read an app's `package.json "imports"` subpath-import map (Node's native
30
+ * import-alias mechanism). Cached per appDir.
31
+ * @param {string} appDir
32
+ * @returns {Record<string,string> | null}
33
+ */
34
+ export function appImportsMap(appDir) {
35
+ if (IMPORTS_CACHE.has(appDir)) return IMPORTS_CACHE.get(appDir) ?? null;
36
+ let map = null;
37
+ try {
38
+ const pkg = JSON.parse(readFileSync(join(appDir, 'package.json'), 'utf8'));
39
+ if (pkg && typeof pkg.imports === 'object' && pkg.imports) map = pkg.imports;
40
+ } catch { /* no package.json / unparseable: no aliases */ }
41
+ IMPORTS_CACHE.set(appDir, map);
42
+ return map;
43
+ }
44
+
45
+ /**
46
+ * Expand a `package.json "imports"` subpath alias (e.g. `#lib/db.server.ts`
47
+ * with the scaffold's catch-all `"#*": "./*"`) to its real APP-RELATIVE target
48
+ * string (`./lib/db.server.ts`).
49
+ * The security-critical seam (#555): the graph walker, auth gate, elision, and
50
+ * `no-server-import-in-browser-module` all route through `resolveImport`, so
51
+ * expanding the alias here (to the real path) is what stops an alias from
52
+ * laundering a `.server.ts` past those checks. Driven off the actual `"imports"`
53
+ * map so a non-default base (`"#lib/*": "./src/lib/*"`) is honored; never
54
+ * hardcodes `./`. Supports one trailing `*` wildcard per key (Node's rule) plus
55
+ * exact keys. (A `#/`-prefixed key is avoided in the scaffold because Bun's
56
+ * native resolver rejects it; the expansion here is key-shape-agnostic.)
57
+ * @param {string} spec the import specifier, e.g. `'#lib/db.server.ts'`
58
+ * @param {string} appDir
59
+ * @returns {string | null} the app-relative target (e.g. `'./lib/db.server.ts'`), or null if no alias matches
60
+ */
61
+ export function expandImportAlias(spec, appDir) {
62
+ const map = appImportsMap(appDir);
63
+ if (!map) return null;
64
+ for (const [key, value] of Object.entries(map)) {
65
+ if (typeof value !== 'string') continue; // conditional-export objects are not graph-resolvable
66
+ const star = key.indexOf('*');
67
+ if (star === -1) {
68
+ if (spec === key) return value;
69
+ continue;
70
+ }
71
+ const prefix = key.slice(0, star);
72
+ const suffix = key.slice(star + 1);
73
+ if (!spec.startsWith(prefix) || !spec.endsWith(suffix)) continue;
74
+ const middle = spec.slice(prefix.length, spec.length - suffix.length);
75
+ return value.replace('*', middle);
76
+ }
77
+ return null;
78
+ }
79
+
19
80
  /** @type {RegExp} match static `import … from '…'` and `import '…'` */
20
81
  const IMPORT_RE = /\bimport\s+(?:(?:[\w*{}\s,]+)\s+from\s+)?['"]([^'"]+)['"]/g;
21
82
 
@@ -52,6 +113,9 @@ const EXPORT_FROM_RE = /\bexport\b[^'";]+?\sfrom\s+['"]([^'"]+)['"]/g;
52
113
  * @returns {Promise<ModuleGraph>}
53
114
  */
54
115
  export async function buildModuleGraph(appDir) {
116
+ // Re-read the app's `package.json "imports"` each build so an edit to the
117
+ // alias map (#555) is picked up on rebuild.
118
+ IMPORTS_CACHE.delete(appDir);
55
119
  /** @type {ModuleGraph} */
56
120
  const graph = new Map();
57
121
  /** @type {Set<string>} every file walked this build (graph holds only files
@@ -286,8 +350,11 @@ async function parseFile(file, appDir, graph, seen) {
286
350
  // and is not a real import edge.
287
351
  if (masked[m.index] === ' ') continue;
288
352
  const spec = m[1];
289
- // Only resolve relative imports within the project.
290
- if (!spec.startsWith('.') && !spec.startsWith('/')) continue;
353
+ // Only resolve relative imports + `#`-style subpath aliases (#555)
354
+ // within the project. A bare npm specifier (dayjs) has no alias match
355
+ // and is skipped; an aliased `#lib/x.server.ts` IS followed so the
356
+ // graph / auth gate / elision see the real path through the alias.
357
+ if (!spec.startsWith('.') && !spec.startsWith('/') && !expandImportAlias(spec, appDir)) continue;
291
358
  const resolved = resolveImport(spec, file, appDir);
292
359
  if (resolved) deps.add(resolved);
293
360
  }
@@ -308,7 +375,13 @@ async function parseFile(file, appDir, graph, seen) {
308
375
  export function resolveImport(spec, fromFile, appDir) {
309
376
  const base = dirname(fromFile);
310
377
  let target;
311
- if (spec.startsWith('/')) {
378
+ const aliased = expandImportAlias(spec, appDir);
379
+ if (aliased) {
380
+ // `#`-style subpath alias (#555): the app-relative target is resolved
381
+ // against the app root, then run through the same extension fallback below
382
+ // so the boundary / elision / preload all see the real on-disk path.
383
+ target = resolve(appDir, aliased);
384
+ } else if (spec.startsWith('/')) {
312
385
  // Absolute from app root (how browser sees it)
313
386
  target = join(appDir, spec);
314
387
  } else {