11import type { DevframeNodeContext } from 'devframe/types'
22import { existsSync } from 'node:fs'
33import fsp from 'node:fs/promises'
4- import { resolve } from 'pathe'
54import { UPLOAD_CHANNEL } from '../rpc/functions/upload'
65import { alwaysFunctions , readFunctions , writeFunctions } from '../rpc/index'
76import { configureAssets } from './context'
@@ -28,14 +27,7 @@ export interface SetupAssetsOptions {
2827 serveStatic : boolean
2928}
3029
31- // Keyed by absolute managed directory, not by context: a host (Nuxt / Vite)
32- // recreates the dev bridge — and thus a fresh `DevframeNodeContext` — on every
33- // config reload and once per build environment. Keying by directory caps live
34- // watchers at one per directory no matter how many times setup re-runs, so
35- // they can't accumulate and exhaust the heap. `ctxToDir` lets a specific
36- // context dispose its own watcher (tests).
37- const watchersByDir = new Map < string , ( ) => Promise < void > > ( )
38- const ctxToDir = new WeakMap < DevframeNodeContext , string > ( )
30+ const watchers = new WeakMap < DevframeNodeContext , ( ) => Promise < void > > ( )
3931
4032/**
4133 * Register the assets RPC surface on a devframe node context: ensures the
@@ -77,14 +69,8 @@ export async function setupAssets(ctx: DevframeNodeContext, options: SetupAssets
7769 ctx . rpc . register ( fn )
7870 }
7971
80- if ( ctx . mode === 'dev' ) {
81- const dir = resolve ( options . dir )
82- // Replace any watcher left over from a prior dev-bridge cycle on the
83- // same directory before starting a fresh one bound to this context.
84- await watchersByDir . get ( dir ) ?.( )
85- watchersByDir . set ( dir , watchAssetsDir ( ctx , options . dir ) )
86- ctxToDir . set ( ctx , dir )
87- }
72+ if ( ctx . mode === 'dev' )
73+ watchers . set ( ctx , watchAssetsDir ( ctx , options . dir ) )
8874}
8975
9076/**
@@ -93,14 +79,10 @@ export async function setupAssets(ctx: DevframeNodeContext, options: SetupAssets
9379 * leaked chokidar watcher doesn't keep the process alive.
9480 */
9581export async function disposeAssetsWatcher ( ctx : DevframeNodeContext ) : Promise < void > {
96- const dir = ctxToDir . get ( ctx )
97- if ( ! dir )
98- return
99- ctxToDir . delete ( ctx )
100- const dispose = watchersByDir . get ( dir )
82+ const dispose = watchers . get ( ctx )
10183 if ( ! dispose )
10284 return
103- watchersByDir . delete ( dir )
85+ watchers . delete ( ctx )
10486 await dispose ( )
10587}
10688
0 commit comments