Example #1
0
		return request(url, { encoding: null }).then(function(bodyBuf) {
			if(!bodyBuf)
				throw "Invalid response from server.";

			if(bodyBuf[0] == 0x42 && bodyBuf[1] == 0x5a && bodyBuf[2] == 0x68) {// bzip2
				return new Buffer(compressjs.Bzip2.decompressFile(bodyBuf));
			}
			else if(bodyBuf[0] == 0x1f && bodyBuf[1] == 0x8b && bodyBuf[2] == 0x08) // gzip
				return Promise.promisify(zlib.gunzip.bind(zlib))(bodyBuf);
			else
				return bodyBuf;
		}).then(function(bodyBuf) {
Example #2
0
function unzipBody(encoding, body, callback) {
	var unzip;
	switch (body && util.toLowerCase(encoding)) {
	    case 'gzip':
	    	unzip = zlib.gunzip.bind(zlib);
	      break;
	    case 'deflate':
	    	unzip = zlib.inflate.bind(zlib);
	      break;
	}
	
	unzip ? unzip(body, function(err, data) {
		err ? zlib.inflateRaw(body, callback) : callback(null, data);
	}) : callback(null, body);

}
Example #3
0
		res._transform = function(chunk, encoding, callback) {
			if (chunk) {
				if (resBody || resBody === false) {
					resBody = resBody ? Buffer.concat([resBody, chunk]) : chunk;
				}
				
				resSize += chunk.length;
			}
			
			if (resBody && resBody.length > MAX_RES_SIZE) {
				resBody = null;
			}
			
			if (!chunk) {
				curData.endTime = Date.now();
				curData.resEnd = true;
				resData.size = resSize;
				if (resBody) {
					var unzip;
					switch (util.toLowerCase(res.headers['content-encoding'])) {
					    case 'gzip':
					    	unzip = zlib.gunzip.bind(zlib);
					      break;
					    case 'deflate':
					    	unzip = zlib.inflate.bind(zlib);
					      break;
					}
					
					if (unzip) {
						unzip(resBody, function(err, body) {
							if (err) {
								callback(err, chunk);
								return;
							}
							resData.body = decode(body);
							callback(null, chunk);
						});
						return;
					}
				}
				resData.body = decode(resBody);
			}
			
			callback(null, chunk);
		};
Example #4
0
var util = require('util');
var iconv = require('iconv-lite');
var zlib = require('zlib');

var streamDecompressors = {
  'gzip': function () { return zlib.createGunzip() },
  'deflate': function () {  return zlib.createInflate() }
}


var bufferDecompressors = {
  'gzip': zlib.gunzip.bind(zlib),
  'deflate': zlib.inflate.bind(zlib)
}

var xmlEncodingRegex = /^<\?xml (.*)encoding=("|')(.*)("|')(.*)\?>/;
var htmlEncodingRegex = /<meta (.*)content=("|')(.*)charset=(.*)("|')/;
var html5EncodingRegex = /<meta (.*)charset=("|')(.*)("|')/;

var iconvDecode = function(charset, buffer) {
  if (!charset || charset.length === 0) return buffer;
  charset = charset.toLowerCase();
  if (iconv.encodingExists(charset)) {
    return new Buffer(iconv.decode(buffer, charset));
  }
  return buffer;
}

exports.decodeXML = function (xmlBuffer) {
  var extract = xmlEncodingRegex.exec(xmlBuffer.toString());
  if (extract) {
Example #5
0
'use strict';
const zlib = require('zlib');
const pify = require('pify');

const gunzip = pify(zlib.gunzip.bind(zlib));

module.exports = opts => {
	opts = opts || {};

	return ctx => {
		if (!ctx.path && ctx.req.awslogs && ctx.req.awslogs.data) {
			const payload = new Buffer(ctx.req.awslogs.data, 'base64');

			return gunzip(payload)
				.then(result => {
					result = JSON.parse(result.toString('utf8'));

					if (result.logEvents && result.subscriptionFilters && result.subscriptionFilters.length > 0 && result.messageType === 'DATA_MESSAGE') {
						const filter = result.subscriptionFilters[0];

						ctx.request.body = result.logEvents;
						Object.defineProperty(ctx, 'path', {enumerable: true, value: `cloudwatch:${opts[filter] || filter}`});
						Object.defineProperty(ctx, 'method', {enumerable: true, value: 'post'});
					}
				})
				.catch(() => {
					// do nothing upon failure
				});
		}
	};
};