Skip to content

Commit

Permalink
feat: migrate to zip.js (#316)
Browse files Browse the repository at this point in the history
* feat: migrate to zip.js

* fix: multiple entries in driver zip

---------

Co-authored-by: Johannes Rückert <jrueckert@ibis-thome.de>
  • Loading branch information
johannesrue and Johannes Rückert committed May 28, 2024
1 parent 6d2f766 commit 907c6ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 54 deletions.
73 changes: 50 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
},
"devDependencies": {
"@types/node": "^20.12.12",
"@types/unzipper": "^0.10.9",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"@vitest/coverage-v8": "^1.6.0",
Expand All @@ -68,7 +67,7 @@
"decamelize": "^6.0.0",
"edge-paths": "^3.0.5",
"node-fetch": "^3.3.2",
"unzipper": "^0.11.6",
"which": "^4.0.0"
"which": "^4.0.0",
"@zip.js/zip.js": "^2.7.44"
}
}
41 changes: 13 additions & 28 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import path from 'node:path'
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import fsp, { writeFile } from 'node:fs/promises'
import os from 'node:os'
import cp from 'node:child_process'
import { format } from 'node:util'
import { Readable } from 'node:stream'

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

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

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

function downloadZip(body: NodeJS.ReadableStream, cacheDir: string) {
const stream = Readable.from(body).pipe(unzipper.Parse())
const promiseChain: Promise<string | void>[] = [
new Promise((resolve, reject) => {
stream.on('close', () => resolve())
stream.on('error', () => reject())
})
]

stream.on('entry', async (entry: Entry) => {
const unzippedFilePath = path.join(cacheDir, entry.path)
if (entry.type === 'Directory') {
return
async function downloadZip(res: Awaited<ReturnType<typeof fetch>>, cacheDir: string) {
const zipBlob = await res.blob()
const zip = new ZipReader(new BlobReader(zipBlob))
for (const entry of await zip.getEntries()) {
const unzippedFilePath = path.join(cacheDir, entry.filename)
if (entry.directory) {
continue
}

if (!await hasAccess(path.dirname(unzippedFilePath))) {
await fsp.mkdir(path.dirname(unzippedFilePath), { recursive: true })
}

const execStream = entry.pipe(fs.createWriteStream(unzippedFilePath))
promiseChain.push(new Promise((resolve, reject) => {
execStream.on('close', () => resolve(unzippedFilePath))
execStream.on('error', reject)
}))
})

return Promise.all(promiseChain)
const content = await entry.getData<Blob>(new BlobWriter())
await writeFile(unzippedFilePath, content.stream())
}
}

/**
Expand Down

0 comments on commit 907c6ba

Please sign in to comment.