overleaf/services/history-v1/storage/lib/streams.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

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('node:zlib')
const { WritableBuffer, ReadableString } = require('@overleaf/stream-utils')
const { pipeline } = require('node:stream')
/**
* 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}
*/
function promisePipe(readStream, writeStream) {
return new BPromise(function (resolve, reject) {
pipeline(readStream, writeStream, function (err) {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
exports.promisePipe = promisePipe
/**
* Create a promise for the result of reading a stream to a buffer.
*
* @function
* @param {stream.Readable} readStream
* @return {Promise.<Buffer>}
*/
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())
}
})
})
}
exports.readStreamToBuffer = readStreamToBuffer
/**
* Create a promise for the result of un-gzipping a stream to a buffer.
*
* @function
* @param {stream.Readable} readStream
* @return {Promise.<Buffer>}
*/
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())
}
})
})
}
exports.gunzipStreamToBuffer = gunzipStreamToBuffer
/**
* Create a write stream that gzips the given string.
*
* @function
* @param {string} string
* @return {Promise.<stream.Readable>}
*/
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))
}
})
})
}
exports.gzipStringToStream = gzipStringToStream