Skip to content

Commit 27065e3

Browse files
committed
feat: add gzip support, send UA containing version
1 parent 39b63ab commit 27065e3

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/LocalBinary.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ var https = require('https'),
44
path = require('path'),
55
os = require('os'),
66
childProcess = require('child_process'),
7+
zlib = require('zlib'),
78
HttpsProxyAgent = require('https-proxy-agent'),
9+
version = require('../package.json').version,
810
LocalError = require('./LocalError');
911

12+
const packageName = 'browserstack-local-nodejs';
13+
1014
function LocalBinary(){
1115
this.hostOS = process.platform;
1216
this.is64bits = process.arch == 'x64';
@@ -81,7 +85,9 @@ function LocalBinary(){
8185
}
8286

8387
try{
84-
const obj = childProcess.spawnSync(cmd, opts);
88+
const userAgent = [packageName, version].join('/');
89+
const env = Object.assign({ 'USER_AGENT': userAgent }, process.env);
90+
const obj = childProcess.spawnSync(cmd, opts, { env: env });
8591
let output;
8692
if(obj.stdout.length > 0) {
8793
if(fs.existsSync(binaryPath)){
@@ -122,12 +128,23 @@ function LocalBinary(){
122128
try {
123129
options.ca = fs.readFileSync(conf.useCaCertificate);
124130
} catch(err) {
125-
console.log("failed to read cert file", err)
131+
console.log('failed to read cert file', err);
126132
}
127133
}
128134

135+
options.headers = Object.assign({}, options.headers, {
136+
'accept-encoding': 'gzip, *',
137+
'user-agent': [packageName, version].join('/'),
138+
});
139+
129140
https.get(options, function (response) {
130-
response.pipe(fileStream);
141+
const contentEncoding = response.headers['content-encoding'];
142+
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
143+
response.pipe(zlib.createGunzip()).pipe(fileStream);
144+
} else {
145+
response.pipe(fileStream);
146+
}
147+
131148
response.on('error', function(err) {
132149
console.error('Got Error in binary download response', err);
133150
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);

lib/download.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const https = require('https'),
22
fs = require('fs'),
33
HttpsProxyAgent = require('https-proxy-agent'),
4-
url = require('url');
4+
url = require('url'),
5+
zlib = require('zlib');
56

67
const binaryPath = process.argv[2], httpPath = process.argv[3], proxyHost = process.argv[4], proxyPort = process.argv[5], useCaCertificate = process.argv[6];
78

@@ -17,13 +18,24 @@ if(proxyHost && proxyPort) {
1718
try {
1819
options.ca = fs.readFileSync(useCaCertificate);
1920
} catch(err) {
20-
console.log("failed to read cert file", err)
21+
console.log('failed to read cert file', err);
2122
}
2223
}
2324
}
2425

26+
options.headers = Object.assign({}, options.headers, {
27+
'accept-encoding': 'gzip, *',
28+
'user-agent': process.env.USER_AGENT,
29+
});
30+
2531
https.get(options, function (response) {
26-
response.pipe(fileStream);
32+
const contentEncoding = response.headers['content-encoding'];
33+
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
34+
response.pipe(zlib.createGunzip()).pipe(fileStream);
35+
} else {
36+
response.pipe(fileStream);
37+
}
38+
2739
response.on('error', function(err) {
2840
console.error('Got Error in binary download response', err);
2941
});

0 commit comments

Comments
 (0)