From e6f04743d6893903e01c86d2d5e807163c6991ea Mon Sep 17 00:00:00 2001 From: Ben Irvin Date: Tue, 16 Jan 2024 14:33:05 +0100 Subject: [PATCH] feat: add gzip support to utils --- cli-tests/utils/fs.js | 44 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/cli-tests/utils/fs.js b/cli-tests/utils/fs.js index b6e8c9a77f..ffbc27a7f7 100644 --- a/cli-tests/utils/fs.js +++ b/cli-tests/utils/fs.js @@ -3,6 +3,7 @@ const path = require('node:path'); const { pipeline } = require('node:stream'); const fs = require('node:fs'); +const zlib = require('node:zlib'); const { parser: jsonlParser } = require('stream-json/jsonl/Parser'); const { chain } = require('stream-chain'); const tar = require('tar'); @@ -84,31 +85,34 @@ const jsonCollector = async (entry) => { */ const readFile = async (archive, file, options = {}) => { const { collector = stringCollector } = options; - /** - * @type {string | undefined} - */ + + // Check if the file is a .tar.gz + const isGzipped = archive.endsWith('.tar.gz'); let content = undefined; await new Promise((resolve, reject) => { - pipeline( - [ - // Source: Archive stream - fs.createReadStream(archive), + const streams = [fs.createReadStream(archive)]; - // Transform: tar parser - new tar.Parse({ - // Match tar entry with the given filename - filter: (filePath, entry) => { - console.log(filePath); - return entry.type === 'File' && file === filePath; - }, - async onentry(entry) { - content = await collector(entry); - }, - }), - ], - (err) => (err ? reject(err) : resolve()) + // If the file is gzipped, add a decompression step + if (isGzipped) { + streams.push(zlib.createGunzip()); + } + + streams.push( + // Transform: tar parser + new tar.Parse({ + // Match tar entry with the given filename + filter: (filePath, entry) => { + console.log(filePath); + return entry.type === 'File' && file === filePath; + }, + async onentry(entry) { + content = await collector(entry); + }, + }) ); + + pipeline(streams, (err) => (err ? reject(err) : resolve())); }); if (content === undefined) {