Skip to content

Commit 907c6ba

Browse files
johannesrueJohannes Rückert
andauthored
feat: migrate to zip.js (#316)
* feat: migrate to zip.js * fix: multiple entries in driver zip --------- Co-authored-by: Johannes Rückert <jrueckert@ibis-thome.de>
1 parent 6d2f766 commit 907c6ba

3 files changed

Lines changed: 65 additions & 54 deletions

File tree

package-lock.json

Lines changed: 50 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
},
4848
"devDependencies": {
4949
"@types/node": "^20.12.12",
50-
"@types/unzipper": "^0.10.9",
5150
"@typescript-eslint/eslint-plugin": "^7.9.0",
5251
"@typescript-eslint/parser": "^7.9.0",
5352
"@vitest/coverage-v8": "^1.6.0",
@@ -68,7 +67,7 @@
6867
"decamelize": "^6.0.0",
6968
"edge-paths": "^3.0.5",
7069
"node-fetch": "^3.3.2",
71-
"unzipper": "^0.11.6",
72-
"which": "^4.0.0"
70+
"which": "^4.0.0",
71+
"@zip.js/zip.js": "^2.7.44"
7372
}
7473
}

src/install.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import path from 'node:path'
2-
import fs from 'node:fs'
3-
import fsp from 'node:fs/promises'
2+
import fsp, { writeFile } from 'node:fs/promises'
43
import os from 'node:os'
54
import cp from 'node:child_process'
65
import { format } from 'node:util'
7-
import { Readable } from 'node:stream'
86

97
import fetch from 'node-fetch'
10-
import unzipper, { type Entry } from 'unzipper'
8+
import { BlobReader, BlobWriter, ZipReader } from '@zip.js/zip.js'
119

1210
import findEdgePath from './finder.js'
1311
import { TAGGED_VERSIONS, EDGE_PRODUCTS_API, TAGGED_VERSION_URL, LATEST_RELEASE_URL, DOWNLOAD_URL, BINARY_FILE, log } from './constants.js'
@@ -52,7 +50,7 @@ export async function download (
5250
}
5351

5452
await fsp.mkdir(cacheDir, { recursive: true })
55-
await downloadZip(res.body, cacheDir)
53+
await downloadZip(res, cacheDir)
5654
await fsp.chmod(binaryFilePath, '755')
5755
log.info('Finished downloading Edgedriver')
5856
await sleep() // wait for file to be accessible, avoid ETXTBSY errors
@@ -160,33 +158,20 @@ export async function fetchVersion (edgeVersion: string) {
160158
throw new Error(`Couldn't detect version for ${edgeVersion}`)
161159
}
162160

163-
function downloadZip(body: NodeJS.ReadableStream, cacheDir: string) {
164-
const stream = Readable.from(body).pipe(unzipper.Parse())
165-
const promiseChain: Promise<string | void>[] = [
166-
new Promise((resolve, reject) => {
167-
stream.on('close', () => resolve())
168-
stream.on('error', () => reject())
169-
})
170-
]
171-
172-
stream.on('entry', async (entry: Entry) => {
173-
const unzippedFilePath = path.join(cacheDir, entry.path)
174-
if (entry.type === 'Directory') {
175-
return
161+
async function downloadZip(res: Awaited<ReturnType<typeof fetch>>, cacheDir: string) {
162+
const zipBlob = await res.blob()
163+
const zip = new ZipReader(new BlobReader(zipBlob))
164+
for (const entry of await zip.getEntries()) {
165+
const unzippedFilePath = path.join(cacheDir, entry.filename)
166+
if (entry.directory) {
167+
continue
176168
}
177-
178169
if (!await hasAccess(path.dirname(unzippedFilePath))) {
179170
await fsp.mkdir(path.dirname(unzippedFilePath), { recursive: true })
180171
}
181-
182-
const execStream = entry.pipe(fs.createWriteStream(unzippedFilePath))
183-
promiseChain.push(new Promise((resolve, reject) => {
184-
execStream.on('close', () => resolve(unzippedFilePath))
185-
execStream.on('error', reject)
186-
}))
187-
})
188-
189-
return Promise.all(promiseChain)
172+
const content = await entry.getData<Blob>(new BlobWriter())
173+
await writeFile(unzippedFilePath, content.stream())
174+
}
190175
}
191176

192177
/**

0 commit comments

Comments
 (0)