1
1
import { readFile } from 'node:fs/promises'
2
2
import { join } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
3
4
4
5
import test from 'ava'
5
6
6
7
import { Archive } from '../index'
7
- import { fileURLToPath } from 'node:url'
8
8
9
9
const __dirname = join ( fileURLToPath ( import . meta. url ) , '..' )
10
10
@@ -16,6 +16,10 @@ test('should be able to read archive', (t) => {
16
16
} )
17
17
18
18
test ( 'should be able to unpack archive' , ( t ) => {
19
+ if ( process . env . NAPI_RS_FORCE_WASI ) {
20
+ t . pass ( 'Skipping unpack test on WASI' )
21
+ return
22
+ }
19
23
const archive = new Archive ( join ( __dirname , 'src.tar' ) )
20
24
archive . unpack ( __dirname )
21
25
t . pass ( )
@@ -58,7 +62,7 @@ test('should be able to extract single file with asBytes', (t) => {
58
62
const content = entry . asBytes ( )
59
63
t . true ( content instanceof Buffer , 'asBytes should return a Buffer' )
60
64
t . true ( content . length > 0 , 'Content should not be empty' )
61
-
65
+
62
66
// The content should be valid Rust code, so let's check for some expected content
63
67
const contentStr = content . toString ( 'utf-8' )
64
68
t . true ( contentStr . includes ( 'use' ) , 'Should contain Rust use statements' )
@@ -72,19 +76,19 @@ test('should be able to extract single file with asBytes', (t) => {
72
76
test ( 'should be able to extract multiple files with asBytes' , ( t ) => {
73
77
const archive = new Archive ( join ( __dirname , 'src.tar' ) )
74
78
const extractedFiles = new Map < string , Buffer > ( )
75
-
79
+
76
80
for ( const entry of archive . entries ( ) ) {
77
81
const path = entry . path ( )
78
82
if ( path && path . endsWith ( '.rs' ) ) {
79
83
const content = entry . asBytes ( )
80
84
extractedFiles . set ( path , content )
81
85
}
82
86
}
83
-
87
+
84
88
t . true ( extractedFiles . size >= 2 , 'Should extract at least 2 .rs files' )
85
89
t . true ( extractedFiles . has ( 'src/lib.rs' ) , 'Should have extracted src/lib.rs' )
86
90
t . true ( extractedFiles . has ( 'src/entry.rs' ) , 'Should have extracted src/entry.rs' )
87
-
91
+
88
92
// Verify all extracted content is non-empty and valid
89
93
for ( const [ path , content ] of extractedFiles ) {
90
94
t . true ( content instanceof Buffer , `Content of ${ path } should be a Buffer` )
@@ -95,11 +99,11 @@ test('should be able to extract multiple files with asBytes', (t) => {
95
99
96
100
test ( 'should work with asBytes on compressed archives' , async ( t ) => {
97
101
const formats = [ 'src.tar.gz' , 'src.tar.bz2' , 'src.tar.xz' ]
98
-
102
+
99
103
for ( const format of formats ) {
100
104
const archive = new Archive ( join ( __dirname , format ) )
101
105
let foundFile = false
102
-
106
+
103
107
for ( const entry of archive . entries ( ) ) {
104
108
const path = entry . path ( )
105
109
if ( path === 'src/lib.rs' ) {
@@ -110,22 +114,22 @@ test('should work with asBytes on compressed archives', async (t) => {
110
114
break
111
115
}
112
116
}
113
-
117
+
114
118
t . true ( foundFile , `Should find src/lib.rs in ${ format } ` )
115
119
}
116
120
} )
117
121
118
122
test ( 'should work with asBytes from buffer-based archive' , async ( t ) => {
119
123
const archiveBuffer = await readFile ( join ( __dirname , 'src.tar' ) )
120
124
const archive = new Archive ( archiveBuffer )
121
-
125
+
122
126
for ( const entry of archive . entries ( ) ) {
123
127
const path = entry . path ( )
124
128
if ( path === 'src/lib.rs' ) {
125
129
const content = entry . asBytes ( )
126
130
t . true ( content instanceof Buffer , 'asBytes should return a Buffer' )
127
131
t . true ( content . length > 0 , 'Content should not be empty' )
128
-
132
+
129
133
const contentStr = content . toString ( 'utf-8' )
130
134
t . true ( contentStr . includes ( 'napi' ) , 'Should contain napi imports' )
131
135
return
@@ -137,7 +141,7 @@ test('should work with asBytes from buffer-based archive', async (t) => {
137
141
test ( 'Docker OCI use case - extract specific file like index.json' , ( t ) => {
138
142
// This test demonstrates the exact use case mentioned in issue #58
139
143
// where you want to extract a specific file from a tarball (like Docker OCI images)
140
-
144
+
141
145
// Function to extract a specific file by name, similar to: tar -x -O -f something.tar index.json
142
146
function extractFile ( archivePath : string , targetPath : string ) : Buffer | null {
143
147
const archive = new Archive ( archivePath )
@@ -149,19 +153,19 @@ test('Docker OCI use case - extract specific file like index.json', (t) => {
149
153
}
150
154
return null
151
155
}
152
-
156
+
153
157
const archivePath = join ( __dirname , 'src.tar' )
154
-
158
+
155
159
// Extract src/lib.rs (simulating extracting index.json from a Docker image)
156
160
const libRsContent = extractFile ( archivePath , 'src/lib.rs' )
157
161
t . not ( libRsContent , null , 'Should be able to extract src/lib.rs' )
158
162
t . true ( libRsContent instanceof Buffer , 'Extracted content should be a Buffer' )
159
163
t . true ( libRsContent ! . length > 0 , 'Extracted content should not be empty' )
160
-
164
+
161
165
// Verify the content is correct
162
166
const contentStr = libRsContent ! . toString ( 'utf-8' )
163
167
t . true ( contentStr . includes ( '#![deny(clippy::all)]' ) , 'Should contain expected Rust code' )
164
-
168
+
165
169
// Try to extract a non-existent file
166
170
const nonExistentContent = extractFile ( archivePath , 'non-existent.json' )
167
171
t . is ( nonExistentContent , null , 'Should return null for non-existent files' )
0 commit comments