@@ -294,64 +294,104 @@ export async function emitFilesToDisk<T = BuildOutputAsset | BuildOutputFile>(
294
294
}
295
295
}
296
296
297
- export function createOutputFileFromText (
297
+ export function createOutputFile (
298
298
path : string ,
299
- text : string ,
299
+ data : string | Uint8Array ,
300
300
type : BuildOutputFileType ,
301
301
) : BuildOutputFile {
302
- return {
303
- path,
304
- text,
305
- type,
306
- get hash ( ) {
307
- return createHash ( 'sha256' ) . update ( this . text ) . digest ( 'hex' ) ;
308
- } ,
309
- get contents ( ) {
310
- return Buffer . from ( this . text , 'utf-8' ) ;
311
- } ,
312
- clone ( ) : BuildOutputFile {
313
- return createOutputFileFromText ( this . path , this . text , this . type ) ;
314
- } ,
315
- } ;
302
+ if ( typeof data === 'string' ) {
303
+ let cachedContents : Uint8Array | null = null ;
304
+ let cachedText : string | null = data ;
305
+ let cachedHash : string | null = null ;
306
+
307
+ return {
308
+ path,
309
+ type,
310
+ get contents ( ) : Uint8Array {
311
+ cachedContents ??= new TextEncoder ( ) . encode ( data ) ;
312
+
313
+ return cachedContents ;
314
+ } ,
315
+ set contents ( value : Uint8Array ) {
316
+ cachedContents = value ;
317
+ cachedText = null ;
318
+ } ,
319
+ get text ( ) : string {
320
+ cachedText ??= new TextDecoder ( 'utf-8' ) . decode ( this . contents ) ;
321
+
322
+ return cachedText ;
323
+ } ,
324
+ get size ( ) : number {
325
+ return this . contents . byteLength ;
326
+ } ,
327
+ get hash ( ) : string {
328
+ cachedHash ??= createHash ( 'sha256' )
329
+ . update ( cachedText ?? this . contents )
330
+ . digest ( 'hex' ) ;
331
+
332
+ return cachedHash ;
333
+ } ,
334
+ clone ( ) : BuildOutputFile {
335
+ return createOutputFile ( this . path , cachedText ?? this . contents , this . type ) ;
336
+ } ,
337
+ } ;
338
+ } else {
339
+ let cachedContents = data ;
340
+ let cachedText : string | null = null ;
341
+ let cachedHash : string | null = null ;
342
+
343
+ return {
344
+ get contents ( ) : Uint8Array {
345
+ return cachedContents ;
346
+ } ,
347
+ set contents ( value : Uint8Array ) {
348
+ cachedContents = value ;
349
+ cachedText = null ;
350
+ } ,
351
+ path,
352
+ type,
353
+ get size ( ) : number {
354
+ return this . contents . byteLength ;
355
+ } ,
356
+ get text ( ) : string {
357
+ cachedText ??= new TextDecoder ( 'utf-8' ) . decode ( this . contents ) ;
358
+
359
+ return cachedText ;
360
+ } ,
361
+ get hash ( ) : string {
362
+ cachedHash ??= createHash ( 'sha256' ) . update ( this . contents ) . digest ( 'hex' ) ;
363
+
364
+ return cachedHash ;
365
+ } ,
366
+ clone ( ) : BuildOutputFile {
367
+ return createOutputFile ( this . path , this . contents , this . type ) ;
368
+ } ,
369
+ } ;
370
+ }
316
371
}
317
372
318
- export function createOutputFileFromData (
319
- path : string ,
320
- data : Uint8Array ,
321
- type : BuildOutputFileType ,
322
- ) : BuildOutputFile {
373
+ export function convertOutputFile ( file : OutputFile , type : BuildOutputFileType ) : BuildOutputFile {
374
+ let { contents : cachedContents } = file ;
375
+ let cachedText : string | null = null ;
376
+
323
377
return {
324
- path,
325
- type,
326
- get text ( ) {
327
- return Buffer . from ( data . buffer , data . byteOffset , data . byteLength ) . toString ( 'utf-8' ) ;
378
+ get contents ( ) : Uint8Array {
379
+ return cachedContents ;
328
380
} ,
329
- get hash ( ) {
330
- return createHash ( 'sha256' ) . update ( this . text ) . digest ( 'hex' ) ;
381
+ set contents ( value : Uint8Array ) {
382
+ cachedContents = value ;
383
+ cachedText = null ;
331
384
} ,
332
- get contents ( ) {
333
- return data ;
334
- } ,
335
- clone ( ) : BuildOutputFile {
336
- return createOutputFileFromData ( this . path , this . contents , this . type ) ;
385
+ hash : file . hash ,
386
+ path : file . path ,
387
+ type ,
388
+ get size ( ) : number {
389
+ return this . contents . byteLength ;
337
390
} ,
338
- } ;
339
- }
340
-
341
- export function convertOutputFile ( file : OutputFile , type : BuildOutputFileType ) : BuildOutputFile {
342
- const { path, contents, hash } = file ;
391
+ get text ( ) : string {
392
+ cachedText ??= new TextDecoder ( 'utf-8' ) . decode ( this . contents ) ;
343
393
344
- return {
345
- contents,
346
- hash,
347
- path,
348
- type,
349
- get text ( ) {
350
- return Buffer . from (
351
- this . contents . buffer ,
352
- this . contents . byteOffset ,
353
- this . contents . byteLength ,
354
- ) . toString ( 'utf-8' ) ;
394
+ return cachedText ;
355
395
} ,
356
396
clone ( ) : BuildOutputFile {
357
397
return convertOutputFile ( this , this . type ) ;
0 commit comments