11import  {  execSync  }  from  "child_process" ; 
2- import  {  mkdir ,  rm ,  writeFile ,  readFile ,  readdir ,  stat  }  from  "fs/promises" ; 
3- import  {  join ,  dirname  }  from  "path" ; 
4- import  {  createReadStream ,  createWriteStream  }  from  "fs" ; 
5- import  {  pipeline  }  from  "stream/promises" ; 
2+ import  {  join  }  from  "path" ; 
3+ import  {  mkdir ,  readFile ,  rm ,  writeFile  }  from  "fs/promises" ; 
64import  *  as  tar  from  "tar" ; 
75
86export  interface  OCIConversionResult  { 
@@ -12,14 +10,14 @@ export interface OCIConversionResult {
1210
1311export  async  function  convertDockerTarToOCIBundle ( 
1412	dockerTarPath : string , 
15- 	tempDir :  string   =  "/tmp/oci-conversion" 
13+ 	tempDir   =  "/tmp/oci-conversion" , 
1614) : Promise < OCIConversionResult >  { 
1715	const  conversionId  =  Math . random ( ) . toString ( 36 ) . substring ( 7 ) ; 
1816	const  workDir  =  join ( tempDir ,  conversionId ) ; 
19- 	 
17+ 
2018	try  { 
2119		await  mkdir ( workDir ,  {  recursive : true  } ) ; 
22- 		 
20+ 
2321		const  dockerImagePath  =  join ( workDir ,  "docker-image.tar" ) ; 
2422		const  ociImagePath  =  join ( workDir ,  "oci-image" ) ; 
2523		const  ociBundlePath  =  join ( workDir ,  "oci-bundle" ) ; 
@@ -30,54 +28,71 @@ export async function convertDockerTarToOCIBundle(
3028		await  writeFile ( dockerImagePath ,  dockerTarData ) ; 
3129
3230		// Convert Docker image to OCI image using skopeo 
33- 		console . log ( `Converting Docker image to OCI image: ${ dockerImagePath }   -> ${ ociImagePath }  ` ) ; 
34- 		execSync ( `skopeo copy docker-archive:${ dockerImagePath }   oci:${ ociImagePath }  :default` ,  { 
35- 			stdio : "pipe" 
36- 		} ) ; 
31+ 		console . log ( 
32+ 			`Converting Docker image to OCI image: ${ dockerImagePath }   -> ${ ociImagePath }  ` , 
33+ 		) ; 
34+ 		execSync ( 
35+ 			`skopeo copy docker-archive:${ dockerImagePath }   oci:${ ociImagePath }  :default` , 
36+ 			{ 
37+ 				stdio : "pipe" , 
38+ 			} , 
39+ 		) ; 
3740
3841		// Convert OCI image to OCI bundle using umoci 
39- 		console . log ( `Converting OCI image to OCI bundle: ${ ociImagePath }   -> ${ ociBundlePath }  ` ) ; 
40- 		execSync ( `umoci unpack --rootless --image ${ ociImagePath }  :default ${ ociBundlePath }  ` ,  { 
41- 			stdio : "pipe" 
42- 		} ) ; 
42+ 		console . log ( 
43+ 			`Converting OCI image to OCI bundle: ${ ociImagePath }   -> ${ ociBundlePath }  ` , 
44+ 		) ; 
45+ 		execSync ( 
46+ 			`umoci unpack --rootless --image ${ ociImagePath }  :default ${ ociBundlePath }  ` , 
47+ 			{ 
48+ 				stdio : "pipe" , 
49+ 			} , 
50+ 		) ; 
4351
4452		// Create tar from OCI bundle 
45- 		console . log ( `Creating tar from OCI bundle: ${ ociBundlePath }   -> ${ bundleTarPath }  ` ) ; 
53+ 		console . log ( 
54+ 			`Creating tar from OCI bundle: ${ ociBundlePath }   -> ${ bundleTarPath }  ` , 
55+ 		) ; 
4656		await  tar . create ( 
4757			{ 
4858				file : bundleTarPath , 
4959				cwd : ociBundlePath , 
5060			} , 
51- 			[ "." ] 
61+ 			[ "." ] , 
5262		) ; 
5363
5464		// Clean up intermediate files 
5565		await  Promise . all ( [ 
5666			rm ( dockerImagePath ,  {  force : true  } ) , 
5767			rm ( ociImagePath ,  {  recursive : true ,  force : true  } ) , 
58- 			rm ( ociBundlePath ,  {  recursive : true ,  force : true  } ) 
68+ 			rm ( ociBundlePath ,  {  recursive : true ,  force : true  } ) , 
5969		] ) ; 
6070
6171		const  cleanup  =  async  ( )  =>  { 
6272			try  { 
6373				await  rm ( workDir ,  {  recursive : true ,  force : true  } ) ; 
6474			}  catch  ( error )  { 
65- 				console . warn ( `Failed to cleanup OCI conversion directory ${ workDir }  :` ,  error ) ; 
75+ 				console . warn ( 
76+ 					`Failed to cleanup OCI conversion directory ${ workDir }  :` , 
77+ 					error , 
78+ 				) ; 
6679			} 
6780		} ; 
6881
6982		return  { 
7083			bundleTarPath, 
71- 			cleanup
84+ 			cleanup, 
7285		} ; 
7386	}  catch  ( error )  { 
7487		// Cleanup on error 
7588		try  { 
7689			await  rm ( workDir ,  {  recursive : true ,  force : true  } ) ; 
7790		}  catch  ( cleanupError )  { 
78- 			console . warn ( `Failed to cleanup after error in ${ workDir }  :` ,  cleanupError ) ; 
91+ 			console . warn ( 
92+ 				`Failed to cleanup after error in ${ workDir }  :` , 
93+ 				cleanupError , 
94+ 			) ; 
7995		} 
8096		throw  new  Error ( `OCI conversion failed: ${ error }  ` ) ; 
8197	} 
8298} 
83- 
0 commit comments