Skip to content

Commit ebdd6e0

Browse files
committed
skip unpack on wasi
1 parent 6475e64 commit ebdd6e0

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

__test__/index.spec.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { readFile } from 'node:fs/promises'
22
import { join } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
34

45
import test from 'ava'
56

67
import { Archive } from '../index'
7-
import { fileURLToPath } from 'node:url'
88

99
const __dirname = join(fileURLToPath(import.meta.url), '..')
1010

@@ -16,6 +16,10 @@ test('should be able to read archive', (t) => {
1616
})
1717

1818
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+
}
1923
const archive = new Archive(join(__dirname, 'src.tar'))
2024
archive.unpack(__dirname)
2125
t.pass()
@@ -58,7 +62,7 @@ test('should be able to extract single file with asBytes', (t) => {
5862
const content = entry.asBytes()
5963
t.true(content instanceof Buffer, 'asBytes should return a Buffer')
6064
t.true(content.length > 0, 'Content should not be empty')
61-
65+
6266
// The content should be valid Rust code, so let's check for some expected content
6367
const contentStr = content.toString('utf-8')
6468
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) => {
7276
test('should be able to extract multiple files with asBytes', (t) => {
7377
const archive = new Archive(join(__dirname, 'src.tar'))
7478
const extractedFiles = new Map<string, Buffer>()
75-
79+
7680
for (const entry of archive.entries()) {
7781
const path = entry.path()
7882
if (path && path.endsWith('.rs')) {
7983
const content = entry.asBytes()
8084
extractedFiles.set(path, content)
8185
}
8286
}
83-
87+
8488
t.true(extractedFiles.size >= 2, 'Should extract at least 2 .rs files')
8589
t.true(extractedFiles.has('src/lib.rs'), 'Should have extracted src/lib.rs')
8690
t.true(extractedFiles.has('src/entry.rs'), 'Should have extracted src/entry.rs')
87-
91+
8892
// Verify all extracted content is non-empty and valid
8993
for (const [path, content] of extractedFiles) {
9094
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) => {
9599

96100
test('should work with asBytes on compressed archives', async (t) => {
97101
const formats = ['src.tar.gz', 'src.tar.bz2', 'src.tar.xz']
98-
102+
99103
for (const format of formats) {
100104
const archive = new Archive(join(__dirname, format))
101105
let foundFile = false
102-
106+
103107
for (const entry of archive.entries()) {
104108
const path = entry.path()
105109
if (path === 'src/lib.rs') {
@@ -110,22 +114,22 @@ test('should work with asBytes on compressed archives', async (t) => {
110114
break
111115
}
112116
}
113-
117+
114118
t.true(foundFile, `Should find src/lib.rs in ${format}`)
115119
}
116120
})
117121

118122
test('should work with asBytes from buffer-based archive', async (t) => {
119123
const archiveBuffer = await readFile(join(__dirname, 'src.tar'))
120124
const archive = new Archive(archiveBuffer)
121-
125+
122126
for (const entry of archive.entries()) {
123127
const path = entry.path()
124128
if (path === 'src/lib.rs') {
125129
const content = entry.asBytes()
126130
t.true(content instanceof Buffer, 'asBytes should return a Buffer')
127131
t.true(content.length > 0, 'Content should not be empty')
128-
132+
129133
const contentStr = content.toString('utf-8')
130134
t.true(contentStr.includes('napi'), 'Should contain napi imports')
131135
return
@@ -137,7 +141,7 @@ test('should work with asBytes from buffer-based archive', async (t) => {
137141
test('Docker OCI use case - extract specific file like index.json', (t) => {
138142
// This test demonstrates the exact use case mentioned in issue #58
139143
// where you want to extract a specific file from a tarball (like Docker OCI images)
140-
144+
141145
// Function to extract a specific file by name, similar to: tar -x -O -f something.tar index.json
142146
function extractFile(archivePath: string, targetPath: string): Buffer | null {
143147
const archive = new Archive(archivePath)
@@ -149,19 +153,19 @@ test('Docker OCI use case - extract specific file like index.json', (t) => {
149153
}
150154
return null
151155
}
152-
156+
153157
const archivePath = join(__dirname, 'src.tar')
154-
158+
155159
// Extract src/lib.rs (simulating extracting index.json from a Docker image)
156160
const libRsContent = extractFile(archivePath, 'src/lib.rs')
157161
t.not(libRsContent, null, 'Should be able to extract src/lib.rs')
158162
t.true(libRsContent instanceof Buffer, 'Extracted content should be a Buffer')
159163
t.true(libRsContent!.length > 0, 'Extracted content should not be empty')
160-
164+
161165
// Verify the content is correct
162166
const contentStr = libRsContent!.toString('utf-8')
163167
t.true(contentStr.includes('#![deny(clippy::all)]'), 'Should contain expected Rust code')
164-
168+
165169
// Try to extract a non-existent file
166170
const nonExistentContent = extractFile(archivePath, 'non-existent.json')
167171
t.is(nonExistentContent, null, 'Should return null for non-existent files')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"devDependencies": {
6565
"@napi-rs/cli": "^3.1.3",
6666
"@napi-rs/lzma": "^1.4.4",
67+
"@napi-rs/wasm-runtime": "^1.0.3",
6768
"@oxc-node/core": "^0.0.32",
6869
"@taplo/cli": "^0.7.0",
6970
"@types/node": "^24.2.1",

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ __metadata:
560560
dependencies:
561561
"@napi-rs/cli": "npm:^3.1.3"
562562
"@napi-rs/lzma": "npm:^1.4.4"
563+
"@napi-rs/wasm-runtime": "npm:^1.0.3"
563564
"@oxc-node/core": "npm:^0.0.32"
564565
"@taplo/cli": "npm:^0.7.0"
565566
"@types/node": "npm:^24.2.1"

0 commit comments

Comments
 (0)