Skip to content

Commit

Permalink
fix: unzip zip archive on Windows install
Browse files Browse the repository at this point in the history
  • Loading branch information
leggetter authored and efirs committed May 11, 2023
1 parent 5aac167 commit a0f0d8b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
67 changes: 45 additions & 22 deletions pkg/npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

/* eslint-disable no-console */

const fetch = require('node-fetch'); const path = require('path');
const tar = require('tar'); const mkdirp = require('mkdirp');
const fs = require('fs'); const { execSync } = require('child_process');
const fetch = require('node-fetch');
const path = require('path');
const tar = require('tar');
const AdmZip = require('adm-zip'); // windows unzip
const mkdirp = require('mkdirp');
const fs = require('fs');
const { execSync } = require('child_process');
const crypto = require('crypto');
const process = require('node:process');

Expand All @@ -21,6 +25,8 @@ const PLATFORM_MAPPING = {
win32: 'windows',
};

const IS_WINDOWS = process.platform === 'win32';

function getInstallationPath(callback) {
const out = execSync('npm root');

Expand Down Expand Up @@ -132,7 +138,7 @@ function parsePackageJson() {
};

// Binary name on Windows has .exe suffix
if (process.platform === 'win32') {
if (IS_WINDOWS) {
opts.binName += '.exe';
opts.ext = 'zip';
}
Expand Down Expand Up @@ -165,27 +171,44 @@ function install(callback) {
mkdirp.sync(opts.binPath);

console.log(`Downloading from URL: ${opts.url}`);

fetch(opts.url).then((res) => {
const hash = crypto.createHash('sha256').setEncoding('hex');
res.body.pipe(hash).on('end', () => { hash.end(); });
res.body
.pipe(
tar.x(
{
C: opts.binPath,
},
['tigris'],
),
)
.on('end', () => {
verifyAndPlaceBinary(
opts.binName,
opts.binPath,
hash.read(),
opts.checksums,
callback,
);
});

const handleExtractComplete = () => {
verifyAndPlaceBinary(
opts.binName,
opts.binPath,
hash.read(),
opts.checksums,
callback,
);
};

if (IS_WINDOWS) {
// Numerous unzip Node stream supporting solutions were tried.
// However, none seem to unzip the .exe in a usable format.
// Therefore, saving to disk and then using a trusted unzipper
// that doesn't support streams to extract.
const tmpZip = path.join(opts.binPath, 'tigris-tmp.zip');
res.body
.pipe(fs.createWriteStream(tmpZip))
.on('finish', () => {
const zip = new AdmZip(tmpZip);
zip.extractAllTo(opts.binPath, true);
fs.unlinkSync(tmpZip);

handleExtractComplete();
});
} else {
res.body.pipe(tar.x(
{
C: opts.binPath,
},
['tigris'],
)).on('end', handleExtractComplete);
}
});

return null;
Expand Down
1 change: 1 addition & 0 deletions pkg/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
],
"homepage": "https://www.tigrisdata.com",
"dependencies": {
"adm-zip": "^0.5.10",
"mkdirp": "^1.0.4",
"node-fetch": "^2.6.7",
"tar": "^6.1.12"
Expand Down

0 comments on commit a0f0d8b

Please sign in to comment.