11import type { TarType , DirectoryContent } from './types' ;
22import fs from 'fs' ;
33import path from 'path' ;
4+ import { TarTypes } from './types' ;
45import * as errors from './errors' ;
56
67/**
@@ -23,7 +24,7 @@ function createHeader(filePath: string, stat: fs.Stats, type: TarType): Buffer {
2324 ) ;
2425 }
2526
26- const size = type === '0' ? stat . size : 0 ;
27+ const size = type === TarTypes . FILE ? stat . size : 0 ;
2728 const header = Buffer . alloc ( BLOCK_SIZE , 0 ) ;
2829
2930 // The TAR headers follow this structure
@@ -47,7 +48,6 @@ function createHeader(filePath: string, stat: fs.Stats, type: TarType): Buffer {
4748 // 345 155 File name (last 155 bytes, total 255 bytes, null-padded)
4849 // 500 12 '\0' (unused)
4950
50- // FIXME: Assuming file path is under 100 characters long
5151 header . write ( filePath . slice ( 0 , 99 ) . padEnd ( 100 , '\0' ) , 0 , 100 , 'utf8' ) ;
5252 header . write ( stat . mode . toString ( 8 ) . padStart ( 7 , '0' ) + '\0' , 100 , 12 , 'ascii' ) ;
5353 header . write ( stat . uid . toString ( 8 ) . padStart ( 7 , '0' ) + '\0' , 108 , 12 , 'ascii' ) ;
@@ -109,10 +109,10 @@ async function* walkDirectory(
109109 const tarPath = path . join ( relativePath , entry ) ;
110110
111111 if ( stat . isDirectory ( ) ) {
112- yield { path : tarPath + '/' , stat : stat , type : '5' } ;
112+ yield { path : tarPath + '/' , stat : stat , type : TarTypes . DIRECTORY } ;
113113 yield * walkDirectory ( baseDir , path . join ( relativePath , entry ) ) ;
114114 } else if ( stat . isFile ( ) ) {
115- yield { path : tarPath , stat : stat , type : '0' } ;
115+ yield { path : tarPath , stat : stat , type : TarTypes . FILE } ;
116116 }
117117 }
118118}
@@ -122,7 +122,7 @@ async function* createTar(baseDir: string): AsyncGenerator<Buffer, void, void> {
122122 // Create header
123123 yield createHeader ( entry . path , entry . stat , entry . type ) ;
124124
125- if ( entry . type === '0' ) {
125+ if ( entry . type === TarTypes . FILE ) {
126126 // Get file contents
127127 yield * readFile ( path . join ( baseDir , entry . path ) ) ;
128128 }
@@ -133,14 +133,4 @@ async function* createTar(baseDir: string): AsyncGenerator<Buffer, void, void> {
133133 yield Buffer . alloc ( BLOCK_SIZE , 0 ) ;
134134}
135135
136- // NOTE: probably need to remove this, idk
137- // this is a library and should only worry about tarring itself and not writing to fs
138- async function writeArchive ( inputFile : string , outputFile : string ) {
139- const fileHandle = await fs . promises . open ( outputFile , 'w+' ) ;
140- for await ( const chunk of createTar ( inputFile ) ) {
141- await fileHandle . write ( chunk ) ;
142- }
143- await fileHandle . close ( ) ;
144- }
145-
146- export { createHeader , readFile , createTar , writeArchive } ;
136+ export { createHeader , readFile , createTar } ;
0 commit comments