@@ -20,7 +20,7 @@ describe('parser', () => {
2020
2121 test ( 'should read files and directories' , async ( ) => {
2222 // Set the file names and their data
23- const dirName = 'dir' ;
23+ const dirName = 'dir/ ' ;
2424 const fileName1 = 'file.txt' ;
2525 const fileName2 = 'dir/file.txt' ;
2626 const fileData = 'testing' ;
@@ -37,7 +37,7 @@ describe('parser', () => {
3737 [ fileName1 , dirName , fileName2 ] ,
3838 ) ;
3939
40- const entries : Record < string , string | undefined > = { } ;
40+ const entries : Record < string , string | null > = { } ;
4141
4242 // Read files and directories and add it to the entries record
4343 const vtar = new VirtualTarParser ( {
@@ -50,7 +50,7 @@ describe('parser', () => {
5050 entries [ header . path ] = fileContent ;
5151 } ,
5252 onDirectory : async ( header ) => {
53- entries [ header . path ] = undefined ;
53+ entries [ header . path ] = null ;
5454 } ,
5555 } ) ;
5656
@@ -59,11 +59,121 @@ describe('parser', () => {
5959 await vtar . write ( chunk ) ;
6060 }
6161
62- // Make sure all the callbacks settle
63- await vtar . settled ( ) ;
62+ expect ( entries [ dirName ] ) . toBeNull ( ) ;
63+ expect ( entries [ fileName1 ] ) . toEqual ( fileData ) ;
64+ expect ( entries [ fileName2 ] ) . toEqual ( fileData ) ;
65+ } ) ;
66+
67+ test ( 'should ignore files if callback is not provided' , async ( ) => {
68+ // Set the file names and their data
69+ const dirName = 'dir/' ;
70+ const fileName1 = 'file.txt' ;
71+ const fileName2 = 'dir/file.txt' ;
72+ const fileData = 'testing' ;
73+
74+ await fs . promises . mkdir ( path . join ( tempDir , dirName ) ) ;
75+ await fs . promises . writeFile ( path . join ( tempDir , fileName1 ) , fileData ) ;
76+ await fs . promises . writeFile ( path . join ( tempDir , fileName2 ) , fileData ) ;
77+
78+ const archive = tar . create (
79+ {
80+ cwd : tempDir ,
81+ preservePaths : true ,
82+ } ,
83+ [ fileName1 , dirName , fileName2 ] ,
84+ ) ;
85+
86+ const entries : Record < string , string | null > = { } ;
87+
88+ // Read files and directories and add it to the entries record
89+ const vtar = new VirtualTarParser ( {
90+ onDirectory : async ( header ) => {
91+ entries [ header . path ] = null ;
92+ } ,
93+ } ) ;
94+
95+ // Enqueue each generated chunk from the archive
96+ for await ( const chunk of archive ) {
97+ await vtar . write ( chunk ) ;
98+ }
99+
100+ expect ( entries [ dirName ] ) . toBeNull ( ) ;
101+ expect ( entries [ fileName1 ] ) . toBeUndefined ( ) ;
102+ expect ( entries [ fileName2 ] ) . toBeUndefined ( ) ;
103+ } ) ;
104+
105+ test ( 'should ignore directories if callback is not provided' , async ( ) => {
106+ // Set the file names and their data
107+ const dirName = 'dir/' ;
108+ const fileName1 = 'file.txt' ;
109+ const fileName2 = 'dir/file.txt' ;
110+ const fileData = 'testing' ;
111+
112+ await fs . promises . mkdir ( path . join ( tempDir , dirName ) ) ;
113+ await fs . promises . writeFile ( path . join ( tempDir , fileName1 ) , fileData ) ;
114+ await fs . promises . writeFile ( path . join ( tempDir , fileName2 ) , fileData ) ;
115+
116+ const archive = tar . create (
117+ {
118+ cwd : tempDir ,
119+ preservePaths : true ,
120+ } ,
121+ [ fileName1 , dirName , fileName2 ] ,
122+ ) ;
123+
124+ const entries : Record < string , string | null > = { } ;
125+
126+ // Read files and directories and add it to the entries record
127+ const vtar = new VirtualTarParser ( {
128+ onFile : async ( header , data ) => {
129+ const content : Array < Uint8Array > = [ ] ;
130+ for await ( const chunk of data ( ) ) {
131+ content . push ( chunk ) ;
132+ }
133+ const fileContent = Buffer . concat ( content ) . toString ( ) ;
134+ entries [ header . path ] = fileContent ;
135+ } ,
136+ } ) ;
137+
138+ // Enqueue each generated chunk from the archive
139+ for await ( const chunk of archive ) {
140+ await vtar . write ( chunk ) ;
141+ }
64142
65143 expect ( entries [ dirName ] ) . toBeUndefined ( ) ;
66144 expect ( entries [ fileName1 ] ) . toEqual ( fileData ) ;
67145 expect ( entries [ fileName2 ] ) . toEqual ( fileData ) ;
68146 } ) ;
147+
148+ test ( 'should ignore everything if callback is not provided' , async ( ) => {
149+ // Set the file names and their data
150+ const dirName = 'dir/' ;
151+ const fileName1 = 'file.txt' ;
152+ const fileName2 = 'dir/file.txt' ;
153+ const fileData = 'testing' ;
154+
155+ await fs . promises . mkdir ( path . join ( tempDir , dirName ) ) ;
156+ await fs . promises . writeFile ( path . join ( tempDir , fileName1 ) , fileData ) ;
157+ await fs . promises . writeFile ( path . join ( tempDir , fileName2 ) , fileData ) ;
158+
159+ const archive = tar . create (
160+ {
161+ cwd : tempDir ,
162+ preservePaths : true ,
163+ } ,
164+ [ fileName1 , dirName , fileName2 ] ,
165+ ) ;
166+
167+ const entries : Record < string , string | null > = { } ;
168+
169+ // Read files and directories and add it to the entries record
170+ const vtar = new VirtualTarParser ( ) ;
171+
172+ // Enqueue each generated chunk from the archive
173+ for await ( const chunk of archive ) {
174+ await vtar . write ( chunk ) ;
175+ }
176+
177+ expect ( Object . keys ( entries ) . length ) . toEqual ( 0 ) ;
178+ } ) ;
69179} ) ;
0 commit comments