2023-01-13 07:42:29 -05:00
|
|
|
/**
|
|
|
|
* Promises are promises and streams are streams, and ne'er the twain shall
|
|
|
|
* meet.
|
|
|
|
* @module
|
|
|
|
*/
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const BPromise = require('bluebird')
|
2024-11-08 05:21:56 -05:00
|
|
|
const zlib = require('node:zlib')
|
2023-06-01 07:38:45 -04:00
|
|
|
const { WritableBuffer, ReadableString } = require('@overleaf/stream-utils')
|
2024-11-08 05:21:56 -05:00
|
|
|
const { pipeline } = require('node:stream')
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-02-26 08:51:52 -05:00
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
*/
|
2023-05-09 05:10:28 -04:00
|
|
|
function promisePipe(readStream, writeStream) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return new BPromise(function (resolve, reject) {
|
2023-05-09 05:10:28 -04:00
|
|
|
pipeline(readStream, writeStream, function (err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:51:52 -05:00
|
|
|
exports.promisePipe = promisePipe
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
/**
|
2024-02-26 08:51:52 -05:00
|
|
|
* Create a promise for the result of reading a stream to a buffer.
|
2023-01-13 07:42:29 -05:00
|
|
|
*
|
|
|
|
* @function
|
|
|
|
* @param {stream.Readable} readStream
|
2024-02-26 08:51:52 -05:00
|
|
|
* @return {Promise.<Buffer>}
|
2023-01-13 07:42:29 -05:00
|
|
|
*/
|
|
|
|
function readStreamToBuffer(readStream) {
|
|
|
|
return new BPromise(function (resolve, reject) {
|
2023-05-09 05:10:28 -04:00
|
|
|
const bufferStream = new WritableBuffer()
|
|
|
|
pipeline(readStream, bufferStream, function (err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(bufferStream.contents())
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:51:52 -05:00
|
|
|
exports.readStreamToBuffer = readStreamToBuffer
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
/**
|
2024-02-26 08:51:52 -05:00
|
|
|
* Create a promise for the result of un-gzipping a stream to a buffer.
|
2023-01-13 07:42:29 -05:00
|
|
|
*
|
|
|
|
* @function
|
|
|
|
* @param {stream.Readable} readStream
|
|
|
|
* @return {Promise.<Buffer>}
|
|
|
|
*/
|
|
|
|
function gunzipStreamToBuffer(readStream) {
|
|
|
|
const gunzip = zlib.createGunzip()
|
2023-05-09 05:10:28 -04:00
|
|
|
const bufferStream = new WritableBuffer()
|
2023-01-13 07:42:29 -05:00
|
|
|
return new BPromise(function (resolve, reject) {
|
2023-05-09 05:10:28 -04:00
|
|
|
pipeline(readStream, gunzip, bufferStream, function (err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(bufferStream.contents())
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:51:52 -05:00
|
|
|
exports.gunzipStreamToBuffer = gunzipStreamToBuffer
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
/**
|
2024-02-26 08:51:52 -05:00
|
|
|
* Create a write stream that gzips the given string.
|
2023-01-13 07:42:29 -05:00
|
|
|
*
|
|
|
|
* @function
|
2024-02-26 08:51:52 -05:00
|
|
|
* @param {string} string
|
|
|
|
* @return {Promise.<stream.Readable>}
|
2023-01-13 07:42:29 -05:00
|
|
|
*/
|
|
|
|
function gzipStringToStream(string) {
|
2023-10-25 09:47:02 -04:00
|
|
|
return new BPromise(function (resolve, reject) {
|
|
|
|
zlib.gzip(Buffer.from(string), function (error, result) {
|
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve(new ReadableString(result))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.gzipStringToStream = gzipStringToStream
|