@camstack/system 1.0.4 → 1.0.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.
@@ -0,0 +1,48 @@
1
+ import * as path from 'node:path'
2
+ import { pathToFileURL } from 'node:url'
3
+
4
+ // Mirror of host-externals.ts isHostExternal — kept inline because loader
5
+ // hooks run as raw ESM (no TS build). The host-externals.spec + this file's
6
+ // spec both pin the list; if they drift a test fails.
7
+ const HOST_EXTERNAL_SPECIFIERS = [
8
+ '@camstack/system',
9
+ '@camstack/shm-ring',
10
+ '@camstack/types',
11
+ '@camstack/sdk',
12
+ 'zod',
13
+ '@trpc/server',
14
+ '@trpc/client',
15
+ // Common native deps addons genuinely import (sharp, node-av) — resolved from
16
+ // the framework's node_modules. better-sqlite3 is intentionally excluded: no
17
+ // addon imports it (only @camstack/system's hub-side sqlite-storage builtin),
18
+ // so a stray addon import must fail rather than be silently redirected. Keep
19
+ // in sync with host-externals.ts (a spec pins both).
20
+ 'sharp',
21
+ 'node-av',
22
+ ]
23
+
24
+ function isHostExternal(specifier) {
25
+ for (const name of HOST_EXTERNAL_SPECIFIERS) {
26
+ if (specifier === name || specifier.startsWith(`${name}/`)) return true
27
+ }
28
+ return false
29
+ }
30
+
31
+ let anchorParentURL = null
32
+
33
+ // Receives { frameworkDir } via module.register(..., { data }). Computes the
34
+ // anchor file URL inside frameworkDir/node_modules so nextResolve walks up from
35
+ // there and finds @camstack/* via standard package resolution (honors exports).
36
+ export function initialize(data) {
37
+ const frameworkDir = data && typeof data.frameworkDir === 'string' ? data.frameworkDir : null
38
+ anchorParentURL = frameworkDir
39
+ ? pathToFileURL(path.join(frameworkDir, 'node_modules', '__camstack_resolver_anchor__.js')).href
40
+ : null
41
+ }
42
+
43
+ export async function resolve(specifier, context, nextResolve) {
44
+ if (anchorParentURL && isHostExternal(specifier)) {
45
+ return nextResolve(specifier, { ...context, parentURL: anchorParentURL })
46
+ }
47
+ return nextResolve(specifier, context)
48
+ }