@@ -17,6 +17,7 @@ import {WebSocketServer} from "ws";
1717import type { Config } from "./config.js" ;
1818import { readConfig } from "./config.js" ;
1919import type { LoaderResolver } from "./dataloader.js" ;
20+ import type { LoadEffects } from "./dataloader.js" ;
2021import { HttpError , isEnoent , isHttpError , isSystemError } from "./error.js" ;
2122import { getClientPath } from "./files.js" ;
2223import type { FileWatchers } from "./fileWatchers.js" ;
@@ -54,17 +55,20 @@ export class PreviewServer {
5455 private readonly _server : ReturnType < typeof createServer > ;
5556 private readonly _socketServer : WebSocketServer ;
5657 private readonly _verbose : boolean ;
58+ private readonly _effects : LoadEffects ;
5759
5860 private constructor ( {
5961 config,
6062 root,
6163 server,
62- verbose
64+ verbose,
65+ effects
6366 } : {
6467 config ?: string ;
6568 root ?: string ;
6669 server : Server ;
6770 verbose : boolean ;
71+ effects : LoadEffects ;
6872 } ) {
6973 this . _config = config ;
7074 this . _root = root ;
@@ -73,6 +77,7 @@ export class PreviewServer {
7377 this . _server . on ( "request" , this . _handleRequest ) ;
7478 this . _socketServer = new WebSocketServer ( { server : this . _server } ) ;
7579 this . _socketServer . on ( "connection" , this . _handleConnection ) ;
80+ this . _effects = effects ;
7681 }
7782
7883 static async start ( { verbose = true , hostname, port, open, ...options } : PreviewOptions ) {
@@ -102,8 +107,12 @@ export class PreviewServer {
102107 console . log ( "" ) ;
103108 }
104109 if ( open ) openBrowser ( address ) ;
105- process . env . OBSERVABLEHQ_ADDRESS = address ; // global!
106- return new PreviewServer ( { server, verbose, ...options } ) ;
110+ const effects : LoadEffects = {
111+ logger : console ,
112+ output : process . stdout ,
113+ address
114+ } ;
115+ return new PreviewServer ( { server, verbose, effects, ...options } ) ;
107116 }
108117
109118 async _readConfig ( ) {
@@ -159,12 +168,12 @@ export class PreviewServer {
159168 const [ caller , path ] = pathname . slice ( "/_chain" . length ) . split ( "::" ) ;
160169 // now any file that watches caller should also watch path
161170 console . warn ( "chained data loader" , { caller, path, loaders} ) ;
162- const file = await getFile ( path , root , loaders ) ;
171+ const file = await this . getFile ( path , root , loaders ) ;
163172 if ( file !== undefined ) return void send ( req , file , { root} ) . pipe ( res ) ;
164173 throw new HttpError ( `Not found: ${ pathname } ` , 404 ) ;
165174 } else if ( pathname . startsWith ( "/_file/" ) ) {
166175 const path = pathname . slice ( "/_file" . length ) ;
167- const file = await getFile ( path , root , loaders ) ;
176+ const file = await this . getFile ( path , root , loaders ) ;
168177 if ( file !== undefined ) return void send ( req , file , { root} ) . pipe ( res ) ;
169178 throw new HttpError ( `Not found: ${ pathname } ` , 404 ) ;
170179 } else {
@@ -235,25 +244,25 @@ export class PreviewServer {
235244 get server ( ) : PreviewServer [ "_server" ] {
236245 return this . _server ;
237246 }
238- }
239-
240- async function getFile ( path : string , root : string , loaders : LoaderResolver ) : Promise < string | undefined > {
241- const filepath = join ( root , path ) ;
242- try {
243- await access ( filepath , constants . R_OK ) ;
244- return path ;
245- } catch ( error ) {
246- if ( ! isEnoent ( error ) ) throw error ;
247- }
248247
249- // Look for a data loader for this file.
250- const loader = loaders . find ( path ) ;
251- if ( loader ) {
248+ async getFile ( path : string , root : string , loaders : LoaderResolver ) : Promise < string | undefined > {
249+ const filepath = join ( root , path ) ;
252250 try {
253- return await loader . load ( ) ;
251+ await access ( filepath , constants . R_OK ) ;
252+ return path ;
254253 } catch ( error ) {
255254 if ( ! isEnoent ( error ) ) throw error ;
256255 }
256+
257+ // Look for a data loader for this file.
258+ const loader = loaders . find ( path ) ;
259+ if ( loader ) {
260+ try {
261+ return await loader . load ( this . _effects ) ;
262+ } catch ( error ) {
263+ if ( ! isEnoent ( error ) ) throw error ;
264+ }
265+ }
257266 }
258267}
259268
0 commit comments