@@ -44,6 +44,23 @@ async function createCar (blocks: Array<{ cid: CID, bytes: Uint8Array }>): Promi
4444  return  out 
4545} 
4646
47+ async  function  createReadableStreamFromCar ( car : AsyncIterable < Uint8Array > ) : Promise < ReadableStream >  { 
48+   const  stream  =  new  ReadableStream ( { 
49+     async  start  ( controller )  { 
50+       try  { 
51+         for  await  ( const  chunk  of  car )  { 
52+           controller . enqueue ( chunk ) 
53+         } 
54+         controller . close ( ) 
55+       }  catch  ( err )  { 
56+         controller . error ( err ) 
57+       } 
58+     } 
59+   } ) 
60+ 
61+   return  stream 
62+ } 
63+ 
4764/** 
4865 * @typedef  {import('ipfsd-ctl').Factory } Factory 
4966 */ 
@@ -179,5 +196,23 @@ export function testImport (factory: Factory<KuboNode>, options: MochaConfig): v
179196      const  result  =  await  all ( ipfs . dag . import ( async  function  *  ( )  {  yield  input  } ( ) ) ) 
180197      expect ( result ) . to . have . deep . nested . property ( '[0].root.cid' ,  cids [ 0 ] ) 
181198    } ) 
199+ 
200+     it ( 'should be able to import car file as a ReadableStream' ,  async  ( )  =>  { 
201+       const  blocks  =  await  createBlocks ( 5 ) 
202+       const  car  =  await  createCar ( blocks ) 
203+ 
204+       const  stream  =  await  createReadableStreamFromCar ( car ) 
205+ 
206+       const  result  =  await  all ( ipfs . dag . import ( stream ) ) 
207+       expect ( result ) . to . have . lengthOf ( 1 ) 
208+       expect ( result ) . to . have . deep . nested . property ( '[0].root.cid' ,  blocks [ 0 ] . cid ) 
209+ 
210+       for  ( const  {  cid }  of  blocks )  { 
211+         await  expect ( ipfs . block . get ( cid ) ) . to . eventually . be . ok ( ) 
212+       } 
213+ 
214+       await  expect ( all ( ipfs . pin . ls ( {  paths : blocks [ 0 ] . cid  } ) ) ) . to . eventually . have . lengthOf ( 1 ) 
215+         . and . have . nested . property ( '[0].type' ,  'recursive' ) 
216+     } ) 
182217  } ) 
183218} 
0 commit comments