1
1
import type { TarType , DirectoryContent } from './types' ;
2
2
import fs from 'fs' ;
3
3
import path from 'path' ;
4
+ import { TarTypes } from './types' ;
4
5
import * as errors from './errors' ;
5
6
6
7
/**
@@ -23,7 +24,7 @@ function createHeader(filePath: string, stat: fs.Stats, type: TarType): Buffer {
23
24
) ;
24
25
}
25
26
26
- const size = type === '0' ? stat . size : 0 ;
27
+ const size = type === TarTypes . FILE ? stat . size : 0 ;
27
28
const header = Buffer . alloc ( BLOCK_SIZE , 0 ) ;
28
29
29
30
// The TAR headers follow this structure
@@ -47,7 +48,6 @@ function createHeader(filePath: string, stat: fs.Stats, type: TarType): Buffer {
47
48
// 345 155 File name (last 155 bytes, total 255 bytes, null-padded)
48
49
// 500 12 '\0' (unused)
49
50
50
- // FIXME: Assuming file path is under 100 characters long
51
51
header . write ( filePath . slice ( 0 , 99 ) . padEnd ( 100 , '\0' ) , 0 , 100 , 'utf8' ) ;
52
52
header . write ( stat . mode . toString ( 8 ) . padStart ( 7 , '0' ) + '\0' , 100 , 12 , 'ascii' ) ;
53
53
header . write ( stat . uid . toString ( 8 ) . padStart ( 7 , '0' ) + '\0' , 108 , 12 , 'ascii' ) ;
@@ -109,10 +109,10 @@ async function* walkDirectory(
109
109
const tarPath = path . join ( relativePath , entry ) ;
110
110
111
111
if ( stat . isDirectory ( ) ) {
112
- yield { path : tarPath + '/' , stat : stat , type : '5' } ;
112
+ yield { path : tarPath + '/' , stat : stat , type : TarTypes . DIRECTORY } ;
113
113
yield * walkDirectory ( baseDir , path . join ( relativePath , entry ) ) ;
114
114
} else if ( stat . isFile ( ) ) {
115
- yield { path : tarPath , stat : stat , type : '0' } ;
115
+ yield { path : tarPath , stat : stat , type : TarTypes . FILE } ;
116
116
}
117
117
}
118
118
}
@@ -122,7 +122,7 @@ async function* createTar(baseDir: string): AsyncGenerator<Buffer, void, void> {
122
122
// Create header
123
123
yield createHeader ( entry . path , entry . stat , entry . type ) ;
124
124
125
- if ( entry . type === '0' ) {
125
+ if ( entry . type === TarTypes . FILE ) {
126
126
// Get file contents
127
127
yield * readFile ( path . join ( baseDir , entry . path ) ) ;
128
128
}
@@ -133,14 +133,4 @@ async function* createTar(baseDir: string): AsyncGenerator<Buffer, void, void> {
133
133
yield Buffer . alloc ( BLOCK_SIZE , 0 ) ;
134
134
}
135
135
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