55 compressImageToByteLimit ,
66 isHeicImageFile ,
77 MAX_COMPRESSIBLE_SOURCE_BYTES ,
8- MAX_HEIC_DECODE_PIXELS ,
98 MAX_STASH_IMAGE_DATA_URL_CHARS ,
109 prepareImageForAttachment ,
1110} from "./imageCompression" ;
@@ -33,45 +32,6 @@ function makeFile(sizeBytes: number, type = "image/png"): File {
3332 return new File ( [ new Uint8Array ( sizeBytes ) . fill ( 7 ) ] , "shot.png" , { type } ) ;
3433}
3534
36- function makeHeicFile ( options ?: {
37- name ?: string ;
38- type ?: string ;
39- width ?: number ;
40- height ?: number ;
41- lastModified ?: number ;
42- } ) : File {
43- const encoder = new TextEncoder ( ) ;
44- const makeBox = ( name : string , ...contents : Uint8Array [ ] ) => {
45- const bytes = new Uint8Array ( 8 + contents . reduce ( ( size , content ) => size + content . length , 0 ) ) ;
46- new DataView ( bytes . buffer ) . setUint32 ( 0 , bytes . length ) ;
47- bytes . set ( encoder . encode ( name ) , 4 ) ;
48- let offset = 8 ;
49- for ( const content of contents ) {
50- bytes . set ( content , offset ) ;
51- offset += content . length ;
52- }
53- return bytes ;
54- } ;
55-
56- const dimensions = new Uint8Array ( 12 ) ;
57- const dimensionView = new DataView ( dimensions . buffer ) ;
58- dimensionView . setUint32 ( 4 , options ?. width ?? 4000 ) ;
59- dimensionView . setUint32 ( 8 , options ?. height ?? 3000 ) ;
60- const imageProperties = makeBox ( "iprp" , makeBox ( "ipco" , makeBox ( "ispe" , dimensions ) ) ) ;
61-
62- return new File (
63- [
64- makeBox ( "ftyp" , encoder . encode ( "heic" ) , new Uint8Array ( 4 ) ) ,
65- makeBox ( "meta" , new Uint8Array ( 4 ) , imageProperties ) ,
66- ] ,
67- options ?. name ?? "photo.heic" ,
68- {
69- type : options ?. type ?? "image/heic" ,
70- ...( options ?. lastModified !== undefined ? { lastModified : options . lastModified } : { } ) ,
71- } ,
72- ) ;
73- }
74-
7535/**
7636 * Installs a fake bitmap + canvas whose encoded size follows `sizeForQuality`.
7737 * `supportsWebp: false` makes `convertToBlob` hand back a differently-typed
@@ -315,9 +275,7 @@ describe("HEIC attachment preparation", () => {
315275 } ) ;
316276
317277 it ( "converts a HEIC photo with a missing MIME type into a named JPEG" , async ( ) => {
318- const original = makeHeicFile ( {
319- name : "IMG_1234.HEIC" ,
320- type : "" ,
278+ const original = new File ( [ new Uint8Array ( [ 1 , 2 , 3 ] ) ] , "IMG_1234.HEIC" , {
321279 lastModified : 123 ,
322280 } ) ;
323281 mocks . heicTo . mockResolvedValueOnce (
@@ -339,8 +297,7 @@ describe("HEIC attachment preparation", () => {
339297 } ) ;
340298
341299 it ( "keeps oversized converted photos in JPEG format while shrinking them" , async ( ) => {
342- const original = makeHeicFile ( {
343- name : "photo.heif" ,
300+ const original = new File ( [ new Uint8Array ( [ 1 , 2 , 3 ] ) ] , "photo.heif" , {
344301 type : "image/heif" ,
345302 } ) ;
346303 mocks . heicTo . mockResolvedValueOnce (
@@ -356,44 +313,27 @@ describe("HEIC attachment preparation", () => {
356313 expect ( fillRect ) . toHaveBeenCalled ( ) ;
357314 } ) ;
358315
359- it ( "compresses converted JPEGs that grow beyond the original source size limit" , async ( ) => {
360- const original = makeHeicFile ( ) ;
316+ it ( "compresses JPEG intermediates above the source safety ceiling" , async ( ) => {
317+ const original = new File ( [ new Uint8Array ( [ 1 , 2 , 3 ] ) ] , "large.heic" , {
318+ type : "image/heic" ,
319+ } ) ;
361320 mocks . heicTo . mockResolvedValueOnce (
362- new Blob ( [ new Uint8Array ( MAX_COMPRESSIBLE_SOURCE_BYTES + 1 ) ] , { type : "image/jpeg" } ) ,
321+ new Blob ( [ new Uint8Array ( MAX_COMPRESSIBLE_SOURCE_BYTES + 1 ) ] , {
322+ type : "image/jpeg" ,
323+ } ) ,
363324 ) ;
364- stubCanvasPipeline ( ( ) => 200_000 ) ;
325+ const { close } = stubCanvasPipeline ( ( ) => 200_000 ) ;
365326
366327 const result = await prepareImageForAttachment ( original , 1_000_000 ) ;
367328
329+ expect ( result . ok && result . file . name ) . toBe ( "large.jpg" ) ;
368330 expect ( result . ok && result . file . type ) . toBe ( "image/jpeg" ) ;
369331 expect ( result . ok && result . file . size ) . toBeLessThanOrEqual ( 1_000_000 ) ;
370- } ) ;
371-
372- it ( "rejects oversized HEIC dimensions before loading the decoder" , async ( ) => {
373- const original = makeHeicFile ( { width : MAX_HEIC_DECODE_PIXELS , height : 2 } ) ;
374-
375- expect ( await prepareImageForAttachment ( original , 1024 ) ) . toEqual ( {
376- ok : false ,
377- reason : "too-large" ,
378- } ) ;
379- expect ( mocks . heicTo ) . not . toHaveBeenCalled ( ) ;
380- } ) ;
381-
382- it ( "rejects invalid HEIC metadata before loading the decoder" , async ( ) => {
383- const original = new File ( [ new Uint8Array ( [ 1 , 2 , 3 ] ) ] , "broken.heic" , {
384- type : "image/heic" ,
385- } ) ;
386-
387- expect ( await prepareImageForAttachment ( original , 1024 ) ) . toEqual ( {
388- ok : false ,
389- reason : "unreadable" ,
390- } ) ;
391- expect ( mocks . heicTo ) . not . toHaveBeenCalled ( ) ;
332+ expect ( close ) . toHaveBeenCalled ( ) ;
392333 } ) ;
393334
394335 it ( "reports unreadable when HEIC decoding fails" , async ( ) => {
395- const original = makeHeicFile ( {
396- name : "broken.heic" ,
336+ const original = new File ( [ new Uint8Array ( [ 1 , 2 , 3 ] ) ] , "broken.heic" , {
397337 type : "image/heic" ,
398338 } ) ;
399339 mocks . heicTo . mockRejectedValueOnce ( new Error ( "Invalid HEIC image" ) ) ;
0 commit comments