3333import { createHash } from "node:crypto" ;
3434import {
3535 copyFileSync ,
36+ constants ,
37+ closeSync ,
38+ fstatSync ,
39+ openSync ,
3640 existsSync ,
3741 mkdirSync ,
3842 readdirSync ,
3943 readFileSync ,
4044 rmSync ,
41- statSync ,
4245 unlinkSync ,
4346 writeFileSync ,
4447} from "node:fs" ;
@@ -87,6 +90,8 @@ interface Plan {
8790 source : string ;
8891 /** Content-addressed object key, without the bucket prefix. */
8992 key : string ;
93+ /** Full digest of the bytes used to derive the key. */
94+ digest : string ;
9095 /** Public URL the manifest will carry. */
9196 url : string ;
9297}
@@ -124,28 +129,41 @@ function needsHosting(file: FileTarget): boolean {
124129/** Point one file at the CDN, and say which bytes have to get there. */
125130function hostFile ( itemName : string , itemDir : string , file : FileTarget ) : Plan {
126131 const source = resolve ( itemDir , file . path ) ;
127- if ( ! existsSync ( source ) || ! statSync ( source ) . isFile ( ) ) {
128- throw new Error ( `${ itemName } : files[] declares "${ file . path } ", which is not on disk.` ) ;
132+ const fd = openSync ( source , constants . O_RDONLY | constants . O_NONBLOCK ) ;
133+ let digest : string ;
134+ try {
135+ if ( ! fstatSync ( fd ) . isFile ( ) ) {
136+ throw new Error ( `${ itemName } : files[] declares "${ file . path } ", which is not a file.` ) ;
137+ }
138+ digest = createHash ( "sha256" ) . update ( readFileSync ( fd ) ) . digest ( "hex" ) ;
139+ } finally {
140+ closeSync ( fd ) ;
129141 }
130- const digest = createHash ( "sha256" ) . update ( readFileSync ( source ) ) . digest ( "hex" ) . slice ( 0 , 16 ) ;
131- const key = `${ digest } ${ extname ( file . path ) . toLowerCase ( ) } ` ;
142+ const key = `${ digest . slice ( 0 , 16 ) } ${ extname ( file . path ) . toLowerCase ( ) } ` ;
132143 file . url = `${ CDN_BASE } /${ key } ` ;
133- return { source, key, url : file . url } ;
144+ return { source, key, digest , url : file . url } ;
134145}
135146
136- /** Rewrite one item's manifest, and report which bytes have to reach the CDN. */
137- function planItem ( itemDir : string ) : Plan [ ] {
147+ /** Prepare a manifest update and identify the bytes that must reach the CDN. */
148+ function planItem ( itemDir : string ) : {
149+ plans : Plan [ ] ;
150+ manifestPath : string ;
151+ manifest : RegistryItem ;
152+ } {
138153 const manifestPath = join ( itemDir , "registry-item.json" ) ;
139154 const manifest = JSON . parse ( readFileSync ( manifestPath , "utf-8" ) ) as RegistryItem ;
140155
141156 const plans = ( manifest . files ?? [ ] )
142157 . filter ( needsHosting )
143158 . map ( ( file ) => hostFile ( manifest . name , itemDir , file ) ) ;
144159
145- if ( plans . length > 0 ) {
160+ return { plans, manifestPath, manifest } ;
161+ }
162+
163+ function writeManifests ( items : ReturnType < typeof planItem > [ ] ) : void {
164+ for ( const { manifestPath, manifest } of items ) {
146165 writeFileSync ( manifestPath , `${ JSON . stringify ( manifest , null , 2 ) } \n` , "utf-8" ) ;
147166 }
148- return plans ;
149167}
150168
151169/** Collect every planned object into one flat directory, deduplicated by key. */
@@ -156,7 +174,13 @@ function stage(plans: Plan[]): number {
156174 for ( const plan of plans ) {
157175 if ( seen . has ( plan . key ) ) continue ;
158176 seen . add ( plan . key ) ;
159- copyFileSync ( plan . source , join ( STAGING_DIR , plan . key ) ) ;
177+ const stagedPath = join ( STAGING_DIR , plan . key ) ;
178+ copyFileSync ( plan . source , stagedPath ) ;
179+ const digest = createHash ( "sha256" ) . update ( readFileSync ( stagedPath ) ) . digest ( "hex" ) ;
180+ if ( digest !== plan . digest ) {
181+ unlinkSync ( stagedPath ) ;
182+ throw new Error ( `Asset changed while staging: ${ plan . source } . No upload was attempted.` ) ;
183+ }
160184 }
161185 return seen . size ;
162186}
@@ -186,19 +210,10 @@ function upload(): void {
186210 ) ;
187211}
188212
189- /**
190- * Report what would move, changing nothing.
191- *
192- * Planning writes the manifest, so a dry run restores each one after asking
193- * it. Cheaper than a second code path that could disagree with the real one
194- * about what counts as hostable.
195- */
213+ /** Report what would move, changing nothing. */
196214function reportDryRun ( itemDirs : string [ ] ) : void {
197215 for ( const dir of itemDirs ) {
198- const manifestPath = join ( dir , "registry-item.json" ) ;
199- const before = readFileSync ( manifestPath , "utf-8" ) ;
200- const plans = planItem ( dir ) ;
201- writeFileSync ( manifestPath , before , "utf-8" ) ;
216+ const { plans } = planItem ( dir ) ;
202217 if ( plans . length > 0 ) {
203218 console . log ( `${ dir . replace ( `${ repoRoot } /` , "" ) } : ${ plans . length } file(s) would be hosted` ) ;
204219 }
@@ -228,13 +243,15 @@ export async function main(argv: string[]): Promise<void> {
228243 return ;
229244 }
230245
231- const plans = itemDirs . flatMap ( planItem ) ;
246+ const items = itemDirs . map ( planItem ) . filter ( ( item ) => item . plans . length > 0 ) ;
247+ const plans = items . flatMap ( ( item ) => item . plans ) ;
232248 if ( plans . length === 0 ) {
233249 console . log ( "No unhosted binary assets found — nothing to do." ) ;
234250 return ;
235251 }
236252
237253 const objects = stage ( plans ) ;
254+ writeManifests ( items ) ;
238255 console . log (
239256 `Staged ${ objects } object(s) from ${ plans . length } manifest entr(ies) → ${ STAGING_DIR } ` ,
240257 ) ;
0 commit comments