import concatStream from 'concat-stream' ; import { Parse as TarParser } from 'tar' ; import { basename } from 'path' ; import { Container } from 'dockerode' ; function extractFileFromTarball ( stream , filePath ) { return new Promise < Buffer > ( ( resolve , reject ) => { let success = false ; const parser = new TarParser ( { strict : true , filter : ( currentPath ) => { const match = currentPath === filePath ; if ( match ) success = true ; return match ; } , onentry : ( entry ) => entry . pipe ( concatStream ( resolve ) ) , } ) ; stream . pipe ( parser ) . on ( 'end' , ( ) => { if ( ! success ) { reject ( new Error ( `Could not find file '${ filePath }' in tarball.` ) ) ; } } ) . on ( 'error' , reject ) ; } ) ; } export function getFileFromContainer ( container , filePath ) { return container . getArchive ( { path : filePath } ) . then ( stream => ( extractFileFromTarball ( stream , basename ( filePath ) ) ) ) ; }