Skip to content

Commit 6204be6

Browse files
committed
UPDATE resource handling strategies
1 parent ff03296 commit 6204be6

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

src/index.js

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const ARCH_MAPPING = {
1717
"arm": "arm"
1818
};
1919

20-
// Mapping between Node's `process.platform` to Golang's
20+
// Mapping between Node's `process.platform` to Golang's
2121
const PLATFORM_MAPPING = {
2222
"darwin": "darwin",
2323
"linux": "linux",
@@ -58,11 +58,11 @@ function verifyAndPlaceBinary(binName, binPath, callback) {
5858
getInstallationPath(function(err, installationPath) {
5959
if (err) return callback("Error getting binary installation path from `npm bin`");
6060

61-
// Move the binary file
61+
// Move the binary file and make sure it is executable
6262
fs.renameSync(path.join(binPath, binName), path.join(installationPath, binName));
6363
fs.chmodSync(path.join(installationPath, binName), "755");
64-
65-
console.log("Placed binary on", path.join(installationPath, binName))
64+
65+
console.log("Placed binary on", path.join(installationPath, binName));
6666

6767
callback(null);
6868
});
@@ -146,6 +146,52 @@ function parsePackageJson() {
146146
}
147147
}
148148

149+
/**
150+
* Unzip strategy for resources using `.tar.gz`.
151+
*
152+
* First we will Un-GZip, then we will untar. So once untar is completed,
153+
* binary is downloaded into `binPath`. Verify the binary and call it good.
154+
*/
155+
function untarStrategy(opts, req, callback) {
156+
157+
const ungz = zlib.createGunzip();
158+
const untar = tar.Extract({path: opts.binPath});
159+
160+
ungz.on('error', callback);
161+
untar.on('error', callback);
162+
163+
// First we will Un-GZip, then we will untar. So once untar is completed,
164+
// binary is downloaded into `binPath`. Verify the binary and call it good
165+
untar.on('end', verifyAndPlaceBinary.bind(null, opts.binName, opts.binPath, callback));
166+
167+
req.pipe(ungz).pipe(untar);
168+
}
169+
170+
/**
171+
* Move strategy for binary resources without compression.
172+
*/
173+
function moveStrategy(opts, req, callback) {
174+
175+
const stream = fs.createWriteStream(path.join(opts.binPath, opts.binName));
176+
177+
stream.on('error', callback);
178+
stream.on('close', verifyAndPlaceBinary.bind(null, opts.binName, opts.binPath, callback));
179+
180+
req.pipe(stream);
181+
}
182+
183+
/**
184+
* Select a resource handling strategy based on given options.
185+
*/
186+
function getStrategy(opts) {
187+
188+
if (opts.url.endsWith('.tar.gz')) {
189+
return untarStrategy;
190+
} else {
191+
return moveStrategy;
192+
}
193+
}
194+
149195
/**
150196
* Reads the configuration from application's package.json,
151197
* validates properties, downloads the binary, untars, and stores at
@@ -157,36 +203,19 @@ function parsePackageJson() {
157203
const INVALID_INPUT = "Invalid inputs";
158204
function install(callback) {
159205

160-
let opts = parsePackageJson();
206+
const opts = parsePackageJson();
161207
if (!opts) return callback(INVALID_INPUT);
162208

209+
const strategy = getStrategy(opts);
163210
mkdirp.sync(opts.binPath);
164-
let ungz = zlib.createGunzip();
165-
let untar = tar.Extract({path: opts.binPath});
166-
167-
ungz.on('error', callback);
168-
untar.on('error', callback);
169-
170-
// First we will Un-GZip, then we will untar. So once untar is completed,
171-
// binary is downloaded into `binPath`. Verify the binary and call it good
172-
untar.on('end', verifyAndPlaceBinary.bind(null, opts.binName, opts.binPath, callback));
173211

174212
console.log("Downloading from URL: " + opts.url);
175213
let req = request({uri: opts.url});
176214
req.on('error', callback.bind(null, "Error downloading from URL: " + opts.url));
177215
req.on('response', function(res) {
178216
if (res.statusCode !== 200) return callback("Error downloading binary. HTTP Status Code: " + res.statusCode);
179217

180-
if (opts.url.endsWith('.tar.gz')) {
181-
req.pipe(ungz).pipe(untar);
182-
} else {
183-
const stream = fs.createWriteStream(path.join(opts.binPath, opts.binName));
184-
185-
stream.on('close', verifyAndPlaceBinary.bind(null, opts.binName, opts.binPath, callback));
186-
stream.on('error', callback);
187-
188-
req.pipe(stream);
189-
}
218+
strategy(opts, req, callback);
190219
});
191220
}
192221

@@ -230,6 +259,3 @@ if (argv && argv.length > 2) {
230259
}
231260
});
232261
}
233-
234-
235-

0 commit comments

Comments
 (0)