@@ -69,8 +69,12 @@ export type {
6969 ViteInspectTransformListItem ,
7070} from './types'
7171
72- export async function createViteInspectStore ( options : ViteInspectStoreOptions = { } ) : Promise < ViteInspectStore > {
73- const filename = options . filename === ':memory:' ? undefined : options . filename
72+ interface InspectArchives {
73+ payloads : InspectPayloadStore
74+ pluginCalls : InspectPluginCallStore
75+ }
76+
77+ async function openArchives ( filename ?: string ) : Promise < InspectArchives > {
7478 if ( filename )
7579 mkdirSync ( dirname ( filename ) , { recursive : true } )
7680
@@ -80,7 +84,7 @@ export async function createViteInspectStore(options: ViteInspectStoreOptions =
8084 const pluginCalls = await createInspectPluginCallStore (
8185 filename ? join ( dirname ( filename ) , 'plugin-calls.bin' ) : undefined ,
8286 )
83- return new PayloadViteInspectStore ( payloads , pluginCalls , options )
87+ return { payloads, pluginCalls }
8488 }
8589 catch ( error ) {
8690 await payloads ?. close ( ) . catch ( ( ) => { } )
@@ -89,11 +93,14 @@ export async function createViteInspectStore(options: ViteInspectStoreOptions =
8993 }
9094}
9195
96+ export function createViteInspectStore ( options : ViteInspectStoreOptions = { } ) : ViteInspectStore {
97+ return new PayloadViteInspectStore ( options )
98+ }
99+
92100class PayloadViteInspectStore implements ViteInspectStore {
93101 private readonly maxBatchItems : number
94102 private readonly maxBatchBytes : number
95- private readonly payloads : InspectPayloadStore
96- private readonly pluginCalls : InspectPluginCallStore
103+ private archives ?: InspectArchives
97104 private readonly scopes = new Map < string , ScopeData > ( )
98105
99106 private queue : Array < PendingWrite | undefined > = [ ]
@@ -116,13 +123,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
116123 private activeReaders = 0
117124 private readerWaiters : Array < ( ) => void > = [ ]
118125
119- constructor (
120- payloads : InspectPayloadStore ,
121- pluginCalls : InspectPluginCallStore ,
122- options : ViteInspectStoreOptions ,
123- ) {
124- this . payloads = payloads
125- this . pluginCalls = pluginCalls
126+ constructor ( private readonly options : ViteInspectStoreOptions ) {
126127 this . maxBatchItems = positiveInteger ( options . maxBatchItems , DEFAULT_MAX_BATCH_ITEMS )
127128 this . maxBatchBytes = positiveInteger ( options . maxBatchBytes , DEFAULT_MAX_BATCH_BYTES )
128129 }
@@ -279,7 +280,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
279280 }
280281
281282 async getPluginCalls ( scope : string , pluginId : number ) : Promise < ViteInspectPluginCallInfo [ ] > {
282- return this . readSnapshot ( ( ) => this . pluginCalls . read ( scope , pluginId ) )
283+ return this . readSnapshot ( ( ) => this . archives ?. pluginCalls . read ( scope , pluginId ) ?? Promise . resolve ( [ ] ) )
283284 }
284285
285286 async getFirstResolveResult ( scope : string , sourceId : string ) : Promise < string | undefined > {
@@ -346,8 +347,9 @@ class PayloadViteInspectStore implements ViteInspectStore {
346347 this . startPump ( )
347348 if ( this . pumpPromise )
348349 await this . pumpPromise
350+ this . throwWriteError ( )
349351 }
350- await this . pluginCalls . flush ( )
352+ await this . archives ?. pluginCalls . flush ( )
351353 this . throwWriteError ( )
352354 }
353355
@@ -468,6 +470,12 @@ class PayloadViteInspectStore implements ViteInspectStore {
468470 private async writeDataBatch (
469471 writes : Array < Exclude < QueuedWrite , { operation : 'invalidate' | 'clearScope' | 'clearPendingLoadMetrics' } > > ,
470472 ) : Promise < void > {
473+ // The write queue has a single consumer. Config-only callers never open the archives.
474+ if ( ! this . archives ) {
475+ const filename = typeof this . options . filename === 'function' ? this . options . filename ( ) : this . options . filename
476+ this . archives = await openArchives ( filename === ':memory:' ? undefined : filename )
477+ }
478+ const { payloads, pluginCalls } = this . archives
471479 const values : Array < string | null | undefined > = [ ]
472480 const prepared : PreparedWrite [ ] = [ ]
473481 const pluginCallWrites : InspectPluginCallWrite [ ] = [ ]
@@ -536,8 +544,8 @@ class PayloadViteInspectStore implements ViteInspectStore {
536544 }
537545
538546 const [ ranges ] = await Promise . all ( [
539- values . length > 0 ? this . payloads . write ( values ) : Promise . resolve ( [ ] ) ,
540- this . pluginCalls . write ( pluginCallWrites ) ,
547+ values . length > 0 ? payloads . write ( values ) : Promise . resolve ( [ ] ) ,
548+ pluginCalls . write ( pluginCallWrites ) ,
541549 ] )
542550 for ( const item of prepared )
543551 this . applyPreparedWrite ( item , ranges )
@@ -601,7 +609,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
601609 // Replace the previous snapshot even when it contains a load error result.
602610 if ( ! transforms || ( write . transformRequest && transforms . transformRequest !== write . transformRequest ) ) {
603611 if ( transforms )
604- this . payloads . reclaim ( collectPayloadRanges ( transforms . items ) )
612+ this . archives ?. payloads . reclaim ( collectPayloadRanges ( transforms . items ) )
605613 transforms = {
606614 transformRequest : write . transformRequest ,
607615 publicModuleId : write . publicModuleId ,
@@ -635,7 +643,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
635643 this . commitDeclinedLoadMetrics ( data , write . publicModuleId , write . transformRequest )
636644 const existing = data . transforms . get ( write . moduleId )
637645 if ( existing )
638- this . payloads . reclaim ( collectPayloadRanges ( existing . items ) )
646+ this . archives ?. payloads . reclaim ( collectPayloadRanges ( existing . items ) )
639647 setTransforms ( data , write . moduleId , {
640648 transformRequest : write . transformRequest ,
641649 publicModuleId : write . publicModuleId ,
@@ -660,7 +668,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
660668 private invalidateNow ( scope : string , moduleId : string , publicModuleId : string ) : void {
661669 const data = this . scopes . get ( scope )
662670 if ( ! data ) {
663- this . pluginCalls . invalidate ( scope , new Set ( [ publicModuleId ] ) )
671+ this . archives ?. pluginCalls . invalidate ( scope , new Set ( [ publicModuleId ] ) )
664672 return
665673 }
666674
@@ -714,7 +722,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
714722 reclaimed . push ( ...collectPayloadRanges ( transforms . items ) )
715723 deleteTransforms ( data , id )
716724 }
717- this . payloads . reclaim ( reclaimed )
725+ this . archives ?. payloads . reclaim ( reclaimed )
718726
719727 deleteResolveIds ( data , linkedResolveIds )
720728
@@ -727,25 +735,25 @@ class PayloadViteInspectStore implements ViteInspectStore {
727735 }
728736 }
729737
730- this . pluginCalls . invalidate ( scope , invalidPublicIds )
738+ this . archives ?. pluginCalls . invalidate ( scope , invalidPublicIds )
731739 }
732740
733741 private clearScopeNow ( scope : string ) : void {
734742 const data = this . scopes . get ( scope )
735743 if ( ! data ) {
736- this . pluginCalls . clearScope ( scope )
744+ this . archives ?. pluginCalls . clearScope ( scope )
737745 return
738746 }
739747 const ranges : ViteInspectPayloadRange [ ] = [ ]
740748 for ( const transforms of data . transforms . values ( ) )
741749 ranges . push ( ...collectPayloadRanges ( transforms . items ) )
742- this . payloads . reclaim ( ranges )
743- this . pluginCalls . clearScope ( scope )
750+ this . archives ?. payloads . reclaim ( ranges )
751+ this . archives ?. pluginCalls . clearScope ( scope )
744752 this . scopes . delete ( scope )
745753 }
746754
747755 private async restoreTransformInfo ( info : StoredViteInspectTransformInfo ) : Promise < ViteInspectTransformInfo > {
748- const result = info . result ? await this . payloads . read ( info . result ) : undefined
756+ const result = info . result ? await this . archives ! . payloads . read ( info . result ) : undefined
749757 return {
750758 name : info . name ,
751759 plugin_id : info . pluginId ,
@@ -812,14 +820,14 @@ class PayloadViteInspectStore implements ViteInspectStore {
812820 this . scheduledPump = undefined
813821 }
814822 try {
815- await this . payloads . close ( )
823+ await this . archives ?. payloads . close ( )
816824 }
817825 catch ( error ) {
818826 thrownError ??= error
819827 reportStorageError ( 'closing the payload archive' , error )
820828 }
821829 try {
822- await this . pluginCalls . close ( )
830+ await this . archives ?. pluginCalls . close ( )
823831 }
824832 catch ( error ) {
825833 thrownError ??= error
0 commit comments