0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

Merge pull request #170 from Meeeeow/fix_yarn_upstream

Fix upstream search not work with gzip
This commit is contained in:
jotadeveloper 2017-05-08 10:40:30 +02:00 committed by GitHub
commit f4058bb5c2
3 changed files with 19 additions and 9 deletions

View file

@ -371,8 +371,8 @@ class Storage {
}, function() { }, function() {
let lstream = self.local.search(startkey, options); let lstream = self.local.search(startkey, options);
stream.abort = function() { stream.abort = function() {
lstream.abort(); lstream.abort();
}; };
lstream.pipe(stream, {end: true}); lstream.pipe(stream, {end: true});
lstream.on('error', function(err) { lstream.on('error', function(err) {
self.logger.error({err: err}, 'search error: @{err.message}'); self.logger.error({err: err}, 'search error: @{err.message}');

View file

@ -9,6 +9,7 @@ const parse_interval = require('./config').parse_interval;
const Logger = require('./logger'); const Logger = require('./logger');
const MyStreams = require('./streams'); const MyStreams = require('./streams');
const Utils = require('./utils'); const Utils = require('./utils');
const zlib = require('zlib');
const encode = function(thing) { const encode = function(thing) {
return encodeURIComponent(thing).replace(/^%40/, '@'); return encodeURIComponent(thing).replace(/^%40/, '@');
}; };
@ -410,18 +411,27 @@ class Storage {
}, },
}); });
let parsePackage = (pkg) => {
if (Utils.is_object(pkg)) {
stream.emit('data', pkg);
}
};
req.on('response', (res) => { req.on('response', (res) => {
if (!String(res.statusCode).match(/^2\d\d$/)) { if (!String(res.statusCode).match(/^2\d\d$/)) {
return stream.emit('error', Error('bad status code ' + res.statusCode + ' from uplink')); return stream.emit('error', Error('bad status code ' + res.statusCode + ' from uplink'));
} }
res.pipe(JSONStream.parse('*')).on('data', (pkg) => { // See https://github.com/request/request#requestoptions-callback
if (Utils.is_object(pkg)) { // Request library will not decode gzip stream.
stream.emit('data', pkg); let jsonStream;
} if (res.headers['content-encoding'] === 'gzip') {
}); jsonStream = res.pipe(zlib.createUnzip());
} else {
res.on('end', () => { jsonStream = res;
}
jsonStream.pipe(JSONStream.parse('*')).on('data', parsePackage);
jsonStream.on('end', () => {
stream.emit('end'); stream.emit('end');
}); });
}); });