mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
1.0.1
This commit is contained in:
71
node_modules/superagent/lib/node/unzip.js
generated
vendored
Normal file
71
node_modules/superagent/lib/node/unzip.js
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const StringDecoder = require('string_decoder').StringDecoder;
|
||||
const Stream = require('stream');
|
||||
const zlib = require('zlib');
|
||||
|
||||
/**
|
||||
* Buffers response data events and re-emits when they're unzipped.
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.unzip = (req, res) => {
|
||||
const unzip = zlib.createUnzip();
|
||||
const stream = new Stream();
|
||||
let decoder;
|
||||
|
||||
// make node responseOnEnd() happy
|
||||
stream.req = req;
|
||||
|
||||
unzip.on('error', err => {
|
||||
if (err && err.code === 'Z_BUF_ERROR') {
|
||||
// unexpected end of file is ignored by browsers and curl
|
||||
stream.emit('end');
|
||||
return;
|
||||
}
|
||||
stream.emit('error', err);
|
||||
});
|
||||
|
||||
// pipe to unzip
|
||||
res.pipe(unzip);
|
||||
|
||||
// override `setEncoding` to capture encoding
|
||||
res.setEncoding = type => {
|
||||
decoder = new StringDecoder(type);
|
||||
};
|
||||
|
||||
// decode upon decompressing with captured encoding
|
||||
unzip.on('data', buf => {
|
||||
if (decoder) {
|
||||
const str = decoder.write(buf);
|
||||
if (str.length) stream.emit('data', str);
|
||||
} else {
|
||||
stream.emit('data', buf);
|
||||
}
|
||||
});
|
||||
|
||||
unzip.on('end', () => {
|
||||
stream.emit('end');
|
||||
});
|
||||
|
||||
// override `on` to capture data listeners
|
||||
const _on = res.on;
|
||||
res.on = function(type, fn) {
|
||||
if ('data' == type || 'end' == type) {
|
||||
stream.on(type, fn);
|
||||
} else if ('error' == type) {
|
||||
stream.on(type, fn);
|
||||
_on.call(res, type, fn);
|
||||
} else {
|
||||
_on.call(res, type, fn);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user