@@ -2,65 +2,128 @@ import type { Store } from '../store/store.js';
22import { Fault } from '../core/util.js' ;
33import { UploadWaitBudget , type UploadPolicy } from '../store/upload-budget.js' ;
44
5- type Flight = { guards : Set < ( ) => void > ; file : Promise < string > } ;
5+ type Subscriber = { check : ( ) => void ; accept : ( id : string ) => void ; fail : ( e : unknown ) => void } ;
6+ type Flight = { callers : Set < Subscriber > ; listeners : Set < ( ) => void > } ;
67const staging = new WeakMap < Store , Map < string , Flight > > ( ) ;
78const joiners = new WeakMap < Store , UploadWaitBudget > ( ) ;
89const key = ( owner : string , subject : string , digest : string ) =>
910 JSON . stringify ( [ owner , subject , digest ] ) ;
1011export function stagingCopy ( store : Store , owner : string , subject : string , digest : string ) {
1112 return staging . get ( store ) ?. has ( key ( owner , subject , digest ) ) ?? false ;
1213}
13- /** Share only the private materialization, not a run, its grants or its acceptance. */
14- export async function stageImage (
14+ function materialize (
1515 store : Store ,
16+ flight : Flight ,
1617 owner : string ,
1718 subject : string ,
18- digest : string ,
1919 name : string ,
2020 bytes : Buffer ,
2121 policy : UploadPolicy ,
22- held : string [ ] ,
2322) {
24- let pool = staging . get ( store ) ;
25- if ( ! pool ) staging . set ( store , ( pool = new Map ( ) ) ) ;
26- const id = key ( owner , subject , digest ) ,
27- check = ( ) => policy . guard ?.( ) ;
28- check ( ) ;
29- let flight = pool . get ( id ) ;
30- let leave : ( ( ) => void ) | undefined ;
31- if ( ! flight ) {
32- const guards = new Set ( [ check ] ) ;
33- const guard = ( ) => {
23+ const guard = Object . assign (
24+ ( ) => {
3425 let failure : unknown ;
35- for ( const check of guards ) {
26+ for ( const c of flight . callers ) {
3627 try {
37- check ( ) ;
28+ c . check ( ) ;
3829 return ;
3930 } catch ( e ) {
4031 failure = e ;
4132 }
4233 }
4334 throw failure ?? new Fault ( 'request_cancelled' , 'No caller is waiting for this image.' , 499 ) ;
44- } ;
45- const file = store
46- . addFile ( owner , name , bytes , undefined , undefined , subject , { ...policy , guard } )
47- . then ( ( f ) => f . id )
48- . finally ( ( ) => pool . delete ( id ) ) ;
49- flight = { guards, file } ;
50- pool . set ( id , flight ) ;
51- } else {
35+ } ,
36+ {
37+ onDisconnect ( notify : ( ) => void ) {
38+ flight . listeners . add ( notify ) ;
39+ return ( ) => {
40+ flight . listeners . delete ( notify ) ;
41+ } ;
42+ } ,
43+ } ,
44+ ) ;
45+ void store
46+ . addFile ( owner , name , bytes , undefined , undefined , subject , { ...policy , guard } )
47+ . then ( async ( file ) => {
48+ // Even if every caller left after commit, retire this unclaimed input through its journal.
49+ const handoff = store . provisionalInputs . hold ( owner , [ file . id ] ) ;
50+ try {
51+ for ( const c of flight . callers ) c . accept ( file . id ) ;
52+ } finally {
53+ await store . provisionalInputs . release ( handoff ) ;
54+ }
55+ } )
56+ . catch ( ( error ) => {
57+ for ( const c of flight . callers ) c . fail ( error ) ;
58+ } ) ;
59+ }
60+
61+ /** Share only the private materialization, not a run, its grants or its acceptance. */
62+ export async function stageImage (
63+ store : Store ,
64+ owner : string ,
65+ subject : string ,
66+ digest : string ,
67+ name : string ,
68+ bytes : Buffer ,
69+ policy : UploadPolicy ,
70+ held : string [ ] ,
71+ ) {
72+ let pool = staging . get ( store ) ;
73+ if ( ! pool ) staging . set ( store , ( pool = new Map ( ) ) ) ;
74+ const id = key ( owner , subject , digest ) ,
75+ existing = pool . get ( id ) ;
76+ const check = ( ) => policy . guard ?.( ) ;
77+ check ( ) ;
78+ const flight : Flight = existing ?? { callers : new Set ( ) , listeners : new Set ( ) } ;
79+ let leave : ( ( ) => void ) | undefined , unsubscribe : ( ( ) => void ) | undefined ;
80+ if ( existing ) {
5281 let budget = joiners . get ( store ) ;
5382 if ( ! budget ) joiners . set ( store , ( budget = new UploadWaitBudget ( ) ) ) ;
5483 leave = budget . take ( owner , bytes . length ) ;
55- flight . guards . add ( check ) ;
56- }
57- try {
58- const file = await flight . file ;
59- held . push ( ...store . provisionalInputs . hold ( owner , [ file ] ) ) ;
60- check ( ) ;
61- return file ;
62- } finally {
63- flight . guards . delete ( check ) ;
84+ } else pool . set ( id , flight ) ;
85+ const { promise, resolve, reject } = Promise . withResolvers < string > ( ) ;
86+ const detach = ( ) => {
87+ if ( ! flight . callers . delete ( caller ) ) return ;
6488 leave ?.( ) ;
89+ unsubscribe ?.( ) ;
90+ if ( ! flight . callers . size ) {
91+ if ( pool . get ( id ) === flight ) pool . delete ( id ) ;
92+ for ( const notify of flight . listeners ) notify ( ) ;
93+ }
94+ } ;
95+ const caller : Subscriber = {
96+ check,
97+ accept ( file ) {
98+ try {
99+ check ( ) ;
100+ held . push ( ...store . provisionalInputs . hold ( owner , [ file ] ) ) ;
101+ resolve ( file ) ;
102+ } catch ( error ) {
103+ reject ( error ) ;
104+ } finally {
105+ detach ( ) ;
106+ }
107+ } ,
108+ fail ( error ) {
109+ detach ( ) ;
110+ reject ( error ) ;
111+ } ,
112+ } ;
113+ flight . callers . add ( caller ) ;
114+ try {
115+ unsubscribe = policy . guard ?. onDisconnect ?.( ( ) => {
116+ try {
117+ check ( ) ;
118+ } catch ( error ) {
119+ caller . fail ( error ) ;
120+ }
121+ } ) ;
122+ if ( ! flight . callers . has ( caller ) ) unsubscribe ?.( ) ;
123+ } catch ( error ) {
124+ caller . fail ( error ) ;
65125 }
126+ if ( ! existing && flight . callers . size )
127+ materialize ( store , flight , owner , subject , name , bytes , policy ) ;
128+ return promise ;
66129}
0 commit comments