11import { MockedFileSystem , mockFileSystem } from '@backtrace/sdk-core/tests/_mocks/fileSystem' ;
22import path from 'path' ;
3- import { Writable } from 'stream' ;
4- import { NodeFileSystem , WritableStream } from '../../src/storage/interfaces/NodeFileSystem' ;
3+ import { Readable , Writable } from 'stream' ;
4+ import { NodeFileSystem } from '../../src/storage/interfaces/NodeFileSystem' ;
55
66export function mockStreamFileSystem ( files ?: Record < string , string > ) : MockedFileSystem < NodeFileSystem > {
77 const fs = mockFileSystem ( files ) ;
@@ -22,7 +22,7 @@ export function mockStreamFileSystem(files?: Record<string, string>): MockedFile
2222 } ) ,
2323
2424 createWriteStream : jest . fn ( ) . mockImplementation ( ( p : string ) => {
25- const writable = new Writable ( {
25+ return new Writable ( {
2626 write ( chunk , encoding , callback ) {
2727 const str = Buffer . isBuffer ( chunk )
2828 ? chunk . toString ( 'utf-8' )
@@ -40,11 +40,27 @@ export function mockStreamFileSystem(files?: Record<string, string>): MockedFile
4040 callback && callback ( ) ;
4141 } ,
4242 } ) ;
43+ } ) ,
4344
44- ( writable as Partial < WritableStream > ) . close = ( ) => writable . end ( ) ;
45- ( writable as Partial < WritableStream > ) . writeSync = ( chunk ) => writable . write ( chunk ) ;
45+ createReadStream : jest . fn ( ) . mockImplementation ( ( p : string ) => {
46+ const fullPath = path . resolve ( p ) ;
47+ const file = fs . files [ fullPath ] ;
48+ if ( ! file ) {
49+ throw new Error ( `File ${ p } does not exist` ) ;
50+ }
4651
47- return writable ;
52+ let position = 0 ;
53+ return new Readable ( {
54+ read ( size ) {
55+ const chunk = file . substring ( position , position + size ) ;
56+ if ( ! chunk ) {
57+ this . push ( null ) ;
58+ } else {
59+ this . push ( Buffer . from ( chunk ) ) ;
60+ position += size ;
61+ }
62+ } ,
63+ } ) ;
4864 } ) ,
4965 } ;
5066}
0 commit comments