Skip to content

Commit

Permalink
refactor: add util pipify
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Feb 25, 2024
1 parent f480971 commit 2d5ce43
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/main/js/api/npm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {log} from '../log.js'
import {$, fs, INI, fetch, tempy} from 'zx-extra'
import {unzip} from '../util.js'
import {Readable} from 'node:stream'
import {pipify, unzip} from '../util.js'

// https://stackoverflow.com/questions/19978452/how-to-extract-single-file-from-tar-gz-archive-using-node-js

Expand All @@ -27,7 +26,7 @@ export const fetchPkg = async (pkg) => {
})
clearTimeout(timeoutId)

await unzip(Readable.from(tarball.body), {cwd, strip: 1, omit: ['package.json']})
await unzip(pipify(tarball.body), {cwd, strip: 1, omit: ['package.json']})

log({pkg})(`fetch duration '${id}': ${Date.now() - now}`)
pkg.fetched = true
Expand Down
3 changes: 3 additions & 0 deletions src/main/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import zlib from 'node:zlib'
import fs from 'node:fs/promises'
import path from 'node:path'
import tar from 'tar-stream'
import {Readable} from 'node:stream'

export const tpl = (str, context) =>
str?.replace(/\$\{\{\s*([.a-z0-9]+)\s*}}/gi, (matched, key) => get(context, key) ?? '')
Expand Down Expand Up @@ -114,3 +115,5 @@ export const unzip = (stream, {pick, omit, cwd = process.cwd(), strip = 0} = {})
.pipe(zlib.createGunzip())
.pipe(extract)
})

export const pipify = (stream) => stream.pipe ? stream : Readable.from(stream)

0 comments on commit 2d5ce43

Please sign in to comment.