import type { DevframeNodeContext } from 'devframe' import { existsSync } from 'node:fs' import fsp from 'node:fs/promises' import { configureAssets } from './context' import { UPLOAD_CHANNEL } from './rpc/functions/upload' import { readFunctions, writeFunctions } from './rpc/index' import { watchAssetsDir } from './watcher' export interface SetupAssetsOptions { /** Directory this devframe manages. */ dir: string /** Whether upload / rename / delete / mkdir are registered. */ write: boolean /** Extensions `upload` accepts, or `'*'` to accept any. */ uploadExtensions: readonly string[] | '*' /** * URL base the managed directory is served at, the base every asset's * `publicPath` is resolved against. */ baseURL: string /** * Whether this devframe should serve the managed directory's bytes * itself. Left `false` when mounted into a host (Vite / Nuxt / …) that * already serves `public/` at {@link baseURL}; set `true` only for the * standalone CLI, which is its own host. */ serveStatic: boolean /** Start the live file watcher (dev mode only). */ watch: boolean } const watchers = new WeakMap Promise>() /** * Register the assets RPC surface on a devframe node context: ensures the * managed directory exists, registers the read RPCs (always) and write * RPCs (when enabled), and starts a live file watcher in dev mode. * * Raw asset bytes (for ``/`