|
1 | 1 | 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' |
4 | 3 | import os from 'node:os' |
5 | 4 | import cp from 'node:child_process' |
6 | 5 | import { format } from 'node:util' |
7 | | -import { Readable } from 'node:stream' |
8 | 6 |
|
9 | 7 | import fetch from 'node-fetch' |
10 | | -import unzipper, { type Entry } from 'unzipper' |
| 8 | +import { BlobReader, BlobWriter, ZipReader } from '@zip.js/zip.js' |
11 | 9 |
|
12 | 10 | import findEdgePath from './finder.js' |
13 | 11 | 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 ( |
52 | 50 | } |
53 | 51 |
|
54 | 52 | await fsp.mkdir(cacheDir, { recursive: true }) |
55 | | - await downloadZip(res.body, cacheDir) |
| 53 | + await downloadZip(res, cacheDir) |
56 | 54 | await fsp.chmod(binaryFilePath, '755') |
57 | 55 | log.info('Finished downloading Edgedriver') |
58 | 56 | await sleep() // wait for file to be accessible, avoid ETXTBSY errors |
@@ -160,33 +158,20 @@ export async function fetchVersion (edgeVersion: string) { |
160 | 158 | throw new Error(`Couldn't detect version for ${edgeVersion}`) |
161 | 159 | } |
162 | 160 |
|
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 |
176 | 168 | } |
177 | | - |
178 | 169 | if (!await hasAccess(path.dirname(unzippedFilePath))) { |
179 | 170 | await fsp.mkdir(path.dirname(unzippedFilePath), { recursive: true }) |
180 | 171 | } |
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 | + } |
190 | 175 | } |
191 | 176 |
|
192 | 177 | /** |
|
0 commit comments