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"))])); }); });
tarball.on('close', () => { let buffer = fs.readFileSync(tmpPath); fs.unlinkSync(tmpPath); progress.log(`Package size: ${formatSize(buffer.byteLength)}`); progress.log(`Compressing ...`); zlib.gzip(buffer, (err, result) => { if (err) { return reject(err); } let t = new Date().valueOf() - start; progress.log(`Compressed size: ${formatSize(result.byteLength)}`); progress.log(`Compression: ${((result.byteLength / buffer.byteLength) * 100).toFixed(2)}%`); progress.log(`Pack complete, took ${t}ms!`); resolve(result); }); });
const uploadEvents = async (events) => { const outStream = zlib.createGzip(); events.forEach(e => { outStream.write(`${JSON.stringify(humps.decamelizeKeys(e))}\n`, 'utf8'); }); outStream.end(); const date = new Date().toISOString(); const partitionPrefix = date.substring(0, 13); const fileName = `dt=${partitionPrefix}/${date}.json.gz`; const params = { Bucket: process.env.HN_INSIGHTS_EVENTS_BUCKET || 'hn-insights-events', Key: fileName, Body: outStream }; console.log(`Uploading ${fileName}: ${events.length} events...`); await s3.upload(params).promise(); console.log(`Uploading ${fileName} done`); }
zlib.gunzip(tarball, (err, result) => {
t.equal(arr.length, 2, 'create two files'); t.ok(str, 'use default `gz` extension'); t.equal(unzip(str).toString(), 'Hello world!\n', 'can decompress the file correctly');
req.pipe(zlib.createGunzip()).pipe(tar.extract()) .on('entry', function (entry, stream, next) { if (entry.type === 'file') {
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(longData)])); }); }); next.mockClear(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.deflateSync(shortData)]);
if (compressing) { resHeader["Content-Encoding"] = "gzip"; zlib.gzip(content, (err, buffer) => {
hostname: dev ? process.env.URL_APP : process.env.PRODUCTION_URL_APP, }); const gzip = zlib.createGzip(); .pipe(res) .on('error', (err) => { throw err;
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); }); });
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 '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 '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"))])); }); });
hostname: dev ? process.env.URL_APP : process.env.PRODUCTION_URL_APP, }); const gzip = zlib.createGzip(); .pipe(res) .on('error', (err) => { throw err;