@@ -319,6 +319,66 @@ describe("Attachments Session", function () {
319319 }
320320 } ) ;
321321
322+ it ( "binary attachment preserves all byte values including LF (0x0A)" , async ( ) => {
323+ // This test verifies that binary attachments are not corrupted.
324+ // Previously, LF bytes (0x0A) were being converted to CRLF (0x0D 0x0A)
325+ // when FormData.append() received a Buffer without Blob wrapper.
326+
327+ // Create binary data with all possible byte values (0x00 to 0xFF)
328+ const binaryData = new Uint8Array ( 256 ) ;
329+ for ( let i = 0 ; i < 256 ; i ++ ) {
330+ binaryData [ i ] = i ;
331+ }
332+ const originalBuffer = Buffer . from ( binaryData ) ;
333+
334+ {
335+ const session = store . openSession ( ) ;
336+ const user = new User ( ) ;
337+ user . name = "BinaryTest" ;
338+ await session . store ( user , "users/binary" ) ;
339+ session . advanced . attachments . store (
340+ user ,
341+ "allbytes.bin" ,
342+ originalBuffer ,
343+ "application/octet-stream"
344+ ) ;
345+ await session . saveChanges ( ) ;
346+ }
347+
348+ {
349+ const session = store . openSession ( ) ;
350+ const result = await session . advanced . attachments . get ( "users/binary" , "allbytes.bin" ) ;
351+
352+ let retrievedBuffer = Buffer . from ( [ ] ) ;
353+ result . data . pipe ( new Writable ( {
354+ write ( chunk , enc , cb ) {
355+ retrievedBuffer = Buffer . concat ( [ retrievedBuffer , chunk ] ) ;
356+ cb ( ) ;
357+ }
358+ } ) ) ;
359+
360+ await finishedAsync ( result . data ) ;
361+ result . dispose ( ) ;
362+
363+ // Size should be exactly 256 bytes
364+ assert . strictEqual (
365+ retrievedBuffer . length ,
366+ originalBuffer . length ,
367+ `Binary attachment corrupted: expected ${ originalBuffer . length } bytes, got ${ retrievedBuffer . length } bytes`
368+ ) ;
369+
370+ // Content should be identical
371+ assert . ok (
372+ Buffer . compare ( retrievedBuffer , originalBuffer ) === 0 ,
373+ "Binary attachment content was modified during storage/retrieval"
374+ ) ;
375+
376+ // Specifically verify byte 0x0A (LF) was not converted to 0x0D 0x0A (CRLF)
377+ assert . strictEqual ( retrievedBuffer [ 10 ] , 0x0A , "Byte at index 10 should be 0x0A (LF)" ) ;
378+ assert . strictEqual ( retrievedBuffer [ 11 ] , 0x0B , "Byte at index 11 should be 0x0B, not 0x0A from CRLF expansion" ) ;
379+ }
380+ } ) ;
381+
322382 it ( "attachment exists" , async ( ) => {
323383 {
324384 const session = store . openSession ( ) ;
0 commit comments