From 2d5ce4382acd5c1ea9517177fb3bc3f39345a55b Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 25 Feb 2024 19:28:21 +0300 Subject: [PATCH] refactor: add util pipify --- src/main/js/api/npm.js | 5 ++--- src/main/js/util.js | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/js/api/npm.js b/src/main/js/api/npm.js index 04fd069..c9feb84 100644 --- a/src/main/js/api/npm.js +++ b/src/main/js/api/npm.js @@ -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 @@ -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 diff --git a/src/main/js/util.js b/src/main/js/util.js index d03c4bf..f3e3d11 100644 --- a/src/main/js/util.js +++ b/src/main/js/util.js @@ -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) ?? '') @@ -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)