mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 11:53:40 -05:00
1dc2bd3a24
[history-v1] gzip chunk string in-place before passing to object layer GitOrigin-RevId: db5ded0b0db32757ed35c31c77d190bc121711a7
100 lines
2.3 KiB
JavaScript
100 lines
2.3 KiB
JavaScript
/**
|
|
* Promises are promises and streams are streams, and ne'er the twain shall
|
|
* meet.
|
|
* @module
|
|
*/
|
|
'use strict'
|
|
|
|
const BPromise = require('bluebird')
|
|
const zlib = require('zlib')
|
|
const { WritableBuffer, ReadableString } = require('@overleaf/stream-utils')
|
|
const { pipeline } = require('stream')
|
|
|
|
function promisePipe(readStream, writeStream) {
|
|
return new BPromise(function (resolve, reject) {
|
|
pipeline(readStream, writeStream, function (err) {
|
|
if (err) {
|
|
reject(err)
|
|
} else {
|
|
resolve()
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Pipe a read stream to a write stream. The promise resolves when the write
|
|
* stream finishes.
|
|
*
|
|
* @function
|
|
* @param {stream.Readable} readStream
|
|
* @param {stream.Writable} writeStream
|
|
* @return {Promise}
|
|
*/
|
|
exports.promisePipe = promisePipe
|
|
|
|
function readStreamToBuffer(readStream) {
|
|
return new BPromise(function (resolve, reject) {
|
|
const bufferStream = new WritableBuffer()
|
|
pipeline(readStream, bufferStream, function (err) {
|
|
if (err) {
|
|
reject(err)
|
|
} else {
|
|
resolve(bufferStream.contents())
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Create a promise for the result of reading a stream to a buffer.
|
|
*
|
|
* @function
|
|
* @param {stream.Readable} readStream
|
|
* @return {Promise.<Buffer>}
|
|
*/
|
|
exports.readStreamToBuffer = readStreamToBuffer
|
|
|
|
function gunzipStreamToBuffer(readStream) {
|
|
const gunzip = zlib.createGunzip()
|
|
const bufferStream = new WritableBuffer()
|
|
return new BPromise(function (resolve, reject) {
|
|
pipeline(readStream, gunzip, bufferStream, function (err) {
|
|
if (err) {
|
|
reject(err)
|
|
} else {
|
|
resolve(bufferStream.contents())
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Create a promise for the result of un-gzipping a stream to a buffer.
|
|
*
|
|
* @function
|
|
* @param {stream.Readable} readStream
|
|
* @return {Promise.<Buffer>}
|
|
*/
|
|
exports.gunzipStreamToBuffer = gunzipStreamToBuffer
|
|
|
|
function gzipStringToStream(string) {
|
|
return new BPromise(function (resolve, reject) {
|
|
zlib.gzip(Buffer.from(string), function (error, result) {
|
|
if (error) {
|
|
reject(error)
|
|
} else {
|
|
resolve(new ReadableString(result))
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Create a write stream that gzips the given string.
|
|
*
|
|
* @function
|
|
* @param {string} string
|
|
* @return {Promise.<stream.Readable>}
|
|
*/
|
|
exports.gzipStringToStream = gzipStringToStream
|