it("should decompress with 'deflate'", () => { const mw = Middleware({ threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.deflateSync(Buffer.from("compressed data"))]); return receive("topic", compressedData, meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", Buffer.from("compressed data"), meta); }); });
transporterReceive(next) { return (cmd, data, s) => { const decrypter = iv ? crypto.createDecipheriv(algorithm, password, iv) : crypto.createDecipher(algorithm, password); const res = Buffer.concat([decrypter.update(data), decrypter.final()]); return next(cmd, res, s); }; }
req.on('data', function(chunk) { result = result ? Buffer.concat([result, chunk]) : chunk; if (result.length > MAX_OBJECT_SIZE) { req.removeAllListeners('data'); callback(new Error('The file size can not exceed 6m.')); } });
var cb = function() { if (self._allowInject && self._ended && self._bottom) { chunk = chunk ? Buffer.concat([chunk, self._bottom]) : self._bottom; self._bottom = null; } if (chunk && self._speed) { setTimeout(function() { callback(null, chunk); }, Math.round(chunk.length * 1000 / self._speed)); } else { callback(null, chunk); } }
transporterSend(next) { return (topic, data, meta) => { if (threshold != null && data.length < threshold) { logger.debug(`Packet '${topic}' is small and not compressed. Size: ${data.length}`); return next(topic, Buffer.concat([Buffer.from([0x00]), data]), meta); } return compress(data) .then(res => { logger.debug(`Packet '${topic}' compressed. Saving: ${Number((1 - (res.length / data.length)) * 100).toFixed(0)}%`, data.length, res.length); return next(topic, Buffer.concat([Buffer.from([0x01]), res]), meta); }); }; }
it("should compress with 'gzip'", () => { const mw = Middleware({ method: "gzip", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(() => Promise.resolve()); const send = mw.transporterSend.call(broker, next); return send("topic", Buffer.from("uncompressed data"), meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", expect.any(Buffer), meta); expect(next.mock.calls[0][1]).toEqual(Buffer.concat([COMPRESSED_FLAG, zlib.gzipSync(Buffer.from("uncompressed data"))])); }); });
function zlibBufferOnEnd() { var buf; var err; var bufs = this.buffers; buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread)); this.close(); if (err) this.cb(err); else if (this._info) this.cb(null, { buffer: buf, engine: this }); else this.cb(null, buf); }
req.on('end', () => { let buffer = Buffer.concat(buffers); req.headers['user-agent'] = null; this.send(search, req.method, req.headers, buffer, callback); });
https.request({ hostname: 'f.stdlib.com', port: 443, path: `/utils/reflect/${search}`, method: method, headers: headers }, (libres) => { let lbuffers = []; libres.on('data', chunk => lbuffers.push(chunk)); libres.on('end', () => { let lbuffer = Buffer.concat(lbuffers); let json = {}; try { json = JSON.parse(lbuffer.toString()); } catch (e) { return callback(new Error('Unexpected stdlib reflect response: ' + lbuffer.toString())); } callback(null, json); }); })
function makeCallback(resolve, reject) { return function (response) { const chunks = []; response.on('data', chunk => { chunks.push(chunk); }); response.on('end', () => { const body = Buffer.concat(chunks); const httpResponse = new _HTTPResponse.default(response, body); // Consider <200 && >= 400 as errors if (httpResponse.status < 200 || httpResponse.status >= 400) { return reject(httpResponse); } else { return resolve(httpResponse); } }); response.on('error', reject); }; }
transporterSend(next) { return (topic, data, meta) => { const encrypter = iv ? crypto.createCipheriv(algorithm, password, iv) : crypto.createCipher(algorithm, password); const res = Buffer.concat([encrypter.update(data), encrypter.final()]); return next(topic, res, meta); }; }
it("should decompress with 'gzip'", () => { const mw = Middleware({ method: "gzip", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.gzipSync(Buffer.from("compressed data"))]); return receive("topic", compressedData, meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", Buffer.from("compressed data"), meta); }); });
it("should compress with 'deflateRaw'", () => { const mw = Middleware({ method: "deflateRaw", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(() => Promise.resolve()); const send = mw.transporterSend.call(broker, next); return send("topic", Buffer.from("uncompressed data"), meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", expect.any(Buffer), meta); expect(next.mock.calls[0][1]).toEqual(Buffer.concat([COMPRESSED_FLAG, zlib.deflateRawSync(Buffer.from("uncompressed data"))])); }); });
it("should decompress with 'deflateRaw'", () => { const mw = Middleware({ method: "deflateRaw", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.deflateRawSync(Buffer.from("compressed data"))]); return receive("topic", compressedData, meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", Buffer.from("compressed data"), meta); }); });
it("should compress with 'deflate'", () => { const mw = Middleware({ threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(() => Promise.resolve()); const send = mw.transporterSend.call(broker, next); return send("topic", Buffer.from("uncompressed data"), meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", expect.any(Buffer), meta); expect(next.mock.calls[0][1]).toEqual(Buffer.concat([COMPRESSED_FLAG, zlib.deflateSync(Buffer.from("uncompressed data"))])); }); });