Example #1
0
function test1() {
	var input = new Buffer('lorem ipsum dolor sit amet', 'utf8');

 // What's 'input'?

 // Compress it

// Compress it and convert to utf8 string, just for the heck of it
 zlib.deflate(input, function() {
 	console.log('deflate');
 	console.log(arguments);
 		console.log(arguments[1].toString());
 	console.log(arguments[1].length);
 });

 // Compress, then uncompress (get back what we started with)
 zlib.deflate(input, function(error, value) {
 	console.log('deflate 1');
 	console.log(arguments);
 		console.log(arguments[1].toString());
 	console.log(arguments[1].length);
 	zlib.inflate(value, function() {
 		console.log('deflate & inflate');
 		console.log(arguments);
 		console.log(arguments[1].toString());
 		console.log(arguments[1].length);
 	});
 });

 // Again, and convert back to our initial string
 // zlib.inflate(zlib.deflate(input));
}
Example #2
0
Zip.prototype._flush = function (done) {
	var zlib = require('zlib'),
		self = this;
//	global.log ("\nin Zip..., pushypushy done, this.dataTank="+this.dataTank.join(''));
//	global.log ("in zippyzippy, zlib.Z_DEFAULT_COMPRESSION="+zlib.Z_DEFAULT_COMPRESSION);
  	if (this.acceptEncoding.match(/\bgzip\b/)) {
    	zlib.gzip (
    		this.dataTank.join(''),
    		function (error, result){
    			if (error)
    				error.log("ERROR in zlib.gzip: "+error)
    			else
	    			self.push(result);
				done();
    		});
  	} else if (this.acceptEncoding.match(/\bdeflate\b/)) {
    	zlib.deflate (
    		this.dataTank.join(''),
    		function (error, result){
    			if (error)
    				error.log("ERROR in zlib.deflate: "+error)
    			else
	    			self.push(result);
				done();
    		});
  	} else {
// 	  	global.log ("in Zip ...flushed plain");
		done();
  	}
}
Example #3
0
 brains.get('/resources/deflate', function(req, res) {
   res.setHeader('Transfer-Encoding', 'deflate');
   let image = File.readFileSync(__dirname + '/data/zombie.jpg');
   Zlib.deflate(image, function(error, buffer) {
     res.send(buffer);
   });
 });
Example #4
0
Terrain.prototype.getMapPacket = function (callback) {
    var self = this;

    var columnmetadata = [];
    var columndata = [];
    var len = 0;

    var chunkColumnCount = 0;

    for (var columnKey in self.columns) {
        var data = self.columns[columnKey].getTransmissionBuffer();
        columndata.push(data);
        len += data.length;
        columnmetadata.push(self.columns[columnKey].metadata);
        chunkColumnCount++;
    }

    var total_chunk = Buffer.concat(columndata, len);

    console.log('Chunk count: ' + chunkColumnCount);
    zlib.deflate(total_chunk, function (err, compressed_chunk) {
        self.compressedChunkData = {
            chunkColumnCount: chunkColumnCount,
            dataLength: compressed_chunk.length,
            compressedChunkData: compressed_chunk,
            skyLightSent: true,

            meta: columnmetadata
        };
        callback(self.compressedChunkData);
    });
};
Example #5
0
Terrain.prototype.buildColumnPacket = function (columnsRequested) {
    var self = this;
    var columnmetadata = [];
    var columndata = [];
    var len = 0;

    columnsRequested.forEach(function (columnRequest, idx) {
        for (var i = 0; i < self.columns.length; i++) {
            if (self.columns[i].x === columnRequest.x && self.columns[i].z === columnRequest.z) {
                var data = self.columns[i].getTransmissionBuffer();
                columndata.push(data);
                len += data.length;
                columnmetadata.push(this.columns[i].metadata);
                break;
            }
        }
    });

    var total_chunk = Buffer.concat(columndata, len);

    zlib.deflate(total_chunk, function (err, compressed_chunk) {
        self.compressedChunkData = {
            chunkColumnCount: columnsdata.length,
            dataLength: compressed_chunk.length,
            chunkData: compressed_chunk,
            metadata: columnmetadata
        };
    });
}
Example #6
0
Core.prototype.send = function(msg, addr, port) {
  var json = JSON.stringify(msg);
  var res = new Buffer(json);
  zlib.deflate(res, function(err, buf) {
    server.send(buf, 0, buf.length, port, addr);
  });
};
Example #7
0
DataSender.prototype.invokeMethod = function (message, body) {
  if (!message) {
    throw new Error("Can't send the collector a message without a message type.");
  }

  var data = JSON.stringify(body || []);
  if (this.shouldCompress(data)) {
    logger.trace({data : data}, "Sending with %s (COMPRESSED):", message);

    zlib.deflate(data, function (err, deflated) {
      if (err) {
        return logger.verbose(err, "Error compressing JSON for delivery. Not sending.");
      }

      var headers = this.getHeaders(deflated.length, true);
      this.postToCollector(message, headers, deflated);
    }.bind(this));
  }
  else {
    logger.trace({data: data}, "Sending with %s:", message);

    var headers = this.getHeaders(Buffer.byteLength(data, 'utf8'));
    // ensure that invokeMethod is always asynchronous
    process.nextTick(this.postToCollector.bind(this, message, headers, data));
  }
};
Example #8
0
 brains.get('/resources/deflate', function(req, res) {
   res.setHeader('Content-Encoding', 'deflate');
   const image = File.readFileSync(Path.join(__dirname, '/data/zombie.jpg'));
   Zlib.deflate(image, function(error, buffer) {
     res.send(buffer);
   });
 });
Example #9
0
	kinect.on('colorFrame', function(data){
		//compress the depth data using zlib
		if(!compressing) {
			compressing = true;
			//data is HD bitmap image, which is a bit too heavy to handle in our browser
			//only send every x pixels over to the browser
			var y2 = 0;
			for(var y = 0; y < origHeight; y+=compression) {
				y2++;
				var x2 = 0;
				for(var x = 0; x < origWidth; x+=compression) {
					var i = 4 * (y * origWidth + x);
					var j = 4 * (y2 * compressedWidth + x2);
					resizedBuffer[j] = data[i];
					resizedBuffer[j+1] = data[i+1];
					resizedBuffer[j+2] = data[i+2];
					resizedBuffer[j+3] = data[i+3];
					x2++;
				}
			}

			zlib.deflate(resizedBuffer, function(err, result){
				if(!err) {
					var buffer = result.toString('base64');
					io.sockets.sockets.forEach(function(socket){
						socket.volatile.emit('colorFrame', buffer);
					});
				}
				compressing = false;
			});
		}
	});
Example #10
0
function prepare(d, options, cb) {
  if(options.hashAlgo) {
    var hash = crypto.createHash(options.hashAlgo)
    hash.update(d)
    d.hash = hash.digest('hex')
  }
  
  if(options.gzip) {
    zlib.gzip(d, function(err, r) {
      if(err) return cb(err)
      
      d.gzip = r
      if(options.deflate) {
        zlib.deflate(d, function(err, r) {
          if(err) return cb(err)
          
          d.deflate = r
          cb(null, d)
        })
      } else {
        cb(null, d)
      }
    })
  } else if(options.deflate) {
    zlib.deflate(d, function(err, r) {
      if(err) return cb(err)
      
      d.deflate = r
      cb(null, d)
    })
  } else {
    cb(null, d)
  }
}
 this.counter.increase(function(err, id) {
     if (err) {
         callback(err);
     } else {
         code.set('id', id);
         var data = {
             i: code.get('id'),
             e: parseInt(code.get('expires').getTime() / 1000),
             c: code.get('credentialsId'),
             s: code.get('scopes').join(' '),
             r: code.get('redirectUri'),
             u: code.get('userId')
         };
         data._ = signer.sign(JSON.stringify(data));
         var encoded = JSON.stringify(data);
         zlib.deflate(encoded, function(err, buffer) {
             if (err) {
                 callback(err);
             } else {
                 code.set('value', buffer.toString('base64'));
                 callback(null, code);
             }
         });
     }
 });
Example #12
0
exports.error = function (client, body, callback) {
  zlib.deflate(stringify(body), function (err, buffer) {
    if (err) {
      if (callback) callback(err);
      client.emit('error', err);
      return;
    }

    var options = getRequestOptions(client, buffer, 'errors');
    var req = https.request(options, function (res) {
      if (res.statusCode < 200 || res.statusCode > 299)
        return handleErrorResponse(client, res, callback);
      res.resume();
      var url = res.headers.location;
      if (callback) callback(null, url);
      client.emit('logged', url);
    });

    req.on('error', function (err) {
      if (callback) callback(err);
      client.emit('error', err);
    });

    req.end(buffer);
  });
};
Example #13
0
LooseStorage.prototype.put_raw_object = function(content, type, callback) {
  var self = this;
  // Retrieve size of message
  var size = content.length.toString();    
  // Verify that header is ok
  LooseStorage.verify_header(type, size);  
  // Create header
  var header = "" + type + " " + size + "\0";
  var store = header + content;  
  // Use node crypto library to create sha1 hash
  var hash = crypto.createHash('sha1');
  hash.update(store, 'binary');
  // Return the hash digest
  var sha1 = hash.digest('hex');
  // Create path
  var path = this.directory +  '/' + sha1.substr(0, 2) + '/' + sha1.substr(2);
  try {
    fs.statSync(path);
    callback(sha1);
  } catch(err) {
    // Deflate the data
    var buffStore = new Buffer(store, 'binary');
    var data = zlib.deflate(buffStore, function (err, buffer) {
      if (err) { throw err; }
      // File does not exist create the directory
      fs.mkdir(self.directory + '/' + sha1.substr(0, 2), 16877, function () {
        fs.writeFile(path, buffer, 'binary', function (err) {
          if (err) { throw err; }
          callback(sha1);
        });
      });
    });
  }
}
 connection.deflateQueue.push(function(task) {
   zlib.deflate(packet.buffer, function(err, compressed) {
     var compressedLength = compressed.length;
     if (compressedLength > packetLen) {
       compressHeader.writeUInt8(compressedLength & 0xff, 0);
       compressHeader.writeUInt16LE(compressedLength >> 8, 1);
       compressHeader.writeUInt8(packetSequenceId, 3);
       compressHeader.writeUInt8(packetLen & 0xff, 4);
       compressHeader.writeUInt16LE(packetLen >> 8, 5);
       connection.write(compressHeader);
       connection.write(compressed);
     } else {
       compressedLength = packetLen;
       packetLen = 0;
       compressHeader.writeUInt8(compressedLength & 0xff, 0);
       compressHeader.writeUInt16LE(compressedLength >> 8, 1);
       compressHeader.writeUInt8(packetSequenceId, 3);
       compressHeader.writeUInt8(packetLen & 0xff, 4);
       compressHeader.writeUInt16LE(packetLen >> 8, 5);
       connection.write(compressHeader);
       connection.write(packet.buffer);
     }
     task.done();
   });
 });
Example #15
0
    connection.deflateQueue.push(task => {
      zlib.deflate(buffer, (err, compressed) => {
        if (err) {
          connection._handleFatalError(err);
          return;
        }
        let compressedLength = compressed.length;

        if (compressedLength < packetLen) {
          compressHeader.writeUInt8(compressedLength & 0xff, 0);
          compressHeader.writeUInt16LE(compressedLength >> 8, 1);
          compressHeader.writeUInt8(seqId, 3);
          compressHeader.writeUInt8(packetLen & 0xff, 4);
          compressHeader.writeUInt16LE(packetLen >> 8, 5);
          connection.writeUncompressed(compressHeader);
          connection.writeUncompressed(compressed);
        } else {
          // http://dev.mysql.com/doc/internals/en/uncompressed-payload.html
          // To send an uncompressed payload:
          //   - set length of payload before compression to 0
          //   - the compressed payload contains the uncompressed payload instead.
          compressedLength = packetLen;
          packetLen = 0;
          compressHeader.writeUInt8(compressedLength & 0xff, 0);
          compressHeader.writeUInt16LE(compressedLength >> 8, 1);
          compressHeader.writeUInt8(seqId, 3);
          compressHeader.writeUInt8(packetLen & 0xff, 4);
          compressHeader.writeUInt16LE(packetLen >> 8, 5);
          connection.writeUncompressed(compressHeader);
          connection.writeUncompressed(buffer);
        }
        task.done();
      });
    });
Example #16
0
  encode : function (data, callback) {
    zlib.deflate(JSON.stringify(data), function (err, raw) {
      if (err) return callback(err);

      return callback(null, raw.toString('base64'));
    });
  },
Example #17
0
OutputGelf.prototype.formatPayload = function(data, callback) {
  var m = {
    version: this.version,
    short_message: this.replaceByFields(data, this.message),
    facility: this.replaceByFields(data, this.facility) || 'no_facility',
    level: this.replaceByFields(data, this.level),
    host: data.host,
    timestamp: (new Date(data['@timestamp'])).getTime() / 1000,
  };
  for (var key in data) {
    if (!key.match(/^@/) && key !== 'message' && key !== 'host') {
      m['_' + key] = data[key];
    }
  }
  if (!m.short_message) {
    return;
  }
  logger.debug('Sending GELF', m);
  zlib.deflate(new Buffer(JSON.stringify(m)), function(err, message) {
    if (err) {
      return this.emit('error', new Error('Error while compressing data:' + err));
    }
    callback(message);
  }.bind(this));
};
TokenRepository.prototype.save = function(accessToken, callback) {
    if ((accessToken.get('expires').getTime() - new Date().getTime())  / 1000 > 604800) {
        accessToken.set('expires', new Date(new Date().getTime() + 604800000));
    }

    var signer = this.signer;
    var data = {
        e: parseInt(accessToken.get('expires').getTime() / 1000),
        c: accessToken.get('codeId'),
        r: accessToken.get('credentialsId'),
        s: accessToken.get('scopes').join(' '),
        u: accessToken.get('userId')
    };
    var encoded = JSON.stringify(data);
    zlib.deflate(encoded, function(err, buffer) {
        if (err) {
            callback(err);
        } else {
            var tokenValue = buffer.toString('base64');
            accessToken.set('signatureCredentials', {
                mac_key: signer.sign(tokenValue),
                mac_algorithm: 'hmac-sha-256',
                access_token: tokenValue,
                type: 'symmetric'
            });
            callback(null, accessToken);
        }
    });
};
Example #19
0
OutputGelf.prototype.process = function(data) {
  var m = {
    version: this.version,
    short_message: this.replaceByFields(data, this.message),
    facility: this.replaceByFields(data, this.facility) || 'no_facility',
    level: this.replaceByFields(data, this.level),
    host: data['@source_host'],
    timestamp: (new Date(data['@timestamp'])).getTime() / 1000,
  };

  if (this.include_fields) {
    extend(m, data['@fiields']);
  }

  if (!m.short_message) {
    return;
  }

  logger.debug('Sending GELF', m);

  zlib.deflate(new Buffer(JSON.stringify(m)), function(err, message) {
    if (err) {
      return this.emit('error', new Error('Error while compressing data:' + err));
    }

    this.socket.send(message, 0, message.length, this.port, this.host, function(err, bytes) {
      if (err || bytes != message.length) {
        this.error_buffer.emit('error', new Error('Error while send data to ' + this.host + ':' + this.port + ':' + err));
      }
    }.bind(this));
  }.bind(this));
}
Example #20
0
_.send = function send(kwargs) {
    var self = this;

    // stringify, but don't choke on circular references, see: http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json
    var cache = [];
    var skwargs = JSON.stringify(kwargs, function(k, v) {
        if (typeof v === 'object' && v !== null) {
            if (cache.indexOf(v) !== -1) return;
            cache.push(v);
        }
        return v;
    });

    zlib.deflate(skwargs, function(err, buff) {
        var message = buff.toString('base64'),
            timestamp = new Date().getTime(),
            headers = {
                'X-Sentry-Auth': utils.getAuthHeader(timestamp, self.dsn.public_key, self.dsn.private_key),
                'Content-Type': 'application/octet-stream',
                'Content-Length': message.length
            };

        self.transport.send(self, message, headers);
    });
};
function compressText(req, text, res, cb) {
	'use strict';
	if (typeof cb !== 'function') {
		return;
	}
	if ('accept-encoding' in req.headers) {
		var encs = req.headers['accept-encoding'].replace(/q=\d\.\d/, '').split(/, ?/);
		if (encs.indexOf('gzip') != -1) {
			if (DEBUG) {
				console.log('[compress_debug] using gzip compression');
			}
			res.setHeader('Content-Encoding', 'gzip');
			zlib.gzip(text, cb);
			return;
		}
		else if (encs.indexOf('deflate') != -1) {
			if (DEBUG) {
				console.log('[compress_debug] using deflate compression');
			}
			res.setHeader('Content-Encoding', 'deflate');
			zlib.deflate(text, cb);
			return;
		}
	}
	cb(text);
}
Example #22
0
 res.send = function(data, z, x, y) {
   logger.info(data.features.length + " features in database");
   // logger.debug(data);
   var responseString = 'onKothicDataResponse(' +  JSON.stringify(data) +
                ',' + z + ',' + x + ',' + y +
                ');';
   var acceptEncoding = request.headers['accept-encoding'];
   if (!acceptEncoding) {
     acceptEncoding = '';
   }
   // Note: this is not a conformant accept-encoding parser.
   // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
   if (acceptEncoding.match(/\bdeflate\b/)) {
     headers['content-encoding'] = 'deflate';
     response.writeHead(200, headers);
     zlib.deflate(responseString, function(err, responseBuffer) {
       if (!err) {
         sendResponse(headers, response, responseBuffer);
       }
     });
   } else if (acceptEncoding.match(/\bgzip\b/)) {
     headers['content-encoding'] = 'gzip';
     zlib.gzip(responseString, function(err, responseBuffer) {
       if (!err) {
         sendResponse(headers, response, responseBuffer);
       }
     });
   } else {
     sendResponse(headers, response, responseString);
   }
 };
Example #23
0
 helper.runInTransaction(agent, function() {
   zlib.deflate(CONTENT, function(err, data) {
     t.notOk(err, 'should not error')
     t.equal(data.toString('base64'), DEFLATED_CONTENT)
     verifySegments(t, agent, 'zlib.deflate')
   })
 })
Example #24
0
					fs.read(fd, buffer, 0, size, null, function(err, bytesread, buffer) {
						//fconsole.log("Reading " + fullpath + " " + bytesread)
						if (err) {
							console.log("Error reading from " + fullpath);
							fs.close(fd);
							bufferdone();
							checkdone(true);
							return;
						}
						if (bytesread === 0) {
							fs.close(fd);
							bufferdone();
							checkdone();
							return;
						}
						emitter.emit("filechunk", fullpath, bytesread, stat.size);
						buffer = buffer.slice(0, bytesread);
						var unlog = bytecheck(buffer.length);
						var index = chunks.length;
						chunks.push(null);
						outstanding += 1;

						zlib.deflate(buffer, function(err, compressed_buffer) {
							store.save(compressed_buffer, null, fullpath).then(function(uuid) {
								unlog();
								chunks[index] = uuid;
								bufferdone();
								checkdone();
							}).done();
							readnext();
						})

					});
Example #25
0
            function run () {
                if (end() === true) {
                    return;
                }
                // To demonstrate a big payload for hash set field values, let's create a big array
                var test_arr = [];
                for (var i = 0; i < 80; i++) {
                    var new_obj = JSON.parse(JSON.stringify(test_obj));
                    test_arr.push(new_obj);
                }

                var json = JSON.stringify(test_arr);
                zlib.deflate(new Buffer(json), function (err, buffer) {
                    if (err) {
                        done(err);
                        return;
                    }

                    var multi = client.multi();
                    multi.del('SOME_KEY');

                    for (i = 0; i < 100; i++) {
                        multi.hset('SOME_KEY', 'SOME_FIELD' + i, buffer);
                    }
                    multi.exec(function (err, res) {
                        if (err) {
                            done(err);
                            return;
                        }
                        run();
                    });
                });
            }
Example #26
0
File: gzip.js Project: abpin/hapi
    it('returns without error if given gzipped payload', function (done) {

        var input = JSON.stringify(message);

        Zlib.deflate(input, function (err, buf) {

            var request = {
                method: 'POST',
                url: '/',
                headers: {
                    'content-type': 'application/json',
                    'content-encoding': 'gzip',
                    'content-length': buf.length
                },
                payload: buf
            };

            server.inject(request, function (res) {

                expect(res.result).to.exist;
                expect(res.result).to.deep.equal(message);
                done();
            });
        });
    });
Example #27
0
app.get('/', function(req, res, next){
  zlib.deflate(subject, function(err, buf){
    if (err) return next(err)
    res.set('Content-Type', 'text/plain')
    res.set('Content-Encoding', 'gzip')
    res.send(buf)
  })
})
Example #28
0
File: gzip.js Project: Lilacs/hapi
        Request.get({ url: uri, headers: { 'accept-encoding': 'deflate,q=1; gzip,q=.5' } }, function (err, res, body) {

            Zlib.deflate(rawBody, function (err, zippedBody) {

                expect(body).to.equal(zippedBody.toString());
                done();
            });
        });
Example #29
0
Gelf.prototype.compress = function(message, callback) {
  deflate(message, function(err, buf) {
    if (err) {
      throw err;
    }
    callback && callback(buf);
  });
};
Example #30
0
 namespace.run(function () {
   namespace.set('test', 42);
   zlib.deflate(new Buffer("Goodbye World"), function (err) {
     if (err) throw err;
     t.equal(namespace.get('test'), 42, "mutated state was preserved");
     t.end();
   });
 });