mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 17:19:37 +02:00
Tidy up 'getReadyPipeline'
This commit is contained in:
@@ -94,33 +94,28 @@ async function verifyMd5(persistor, bucket, key, sourceMd5, destMd5 = null) {
|
||||
|
||||
// resolves when a stream is 'readable', or rejects if the stream throws an error
|
||||
// before that happens - this lets us handle protocol-level errors before trying
|
||||
// to read them - these can come from the call to pipeline or the stream itself
|
||||
// to read them
|
||||
function getReadyPipeline(...streams) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const lastStream = streams.slice(-1)[0]
|
||||
let resolvedOrErrored = false
|
||||
|
||||
const onError = function(err) {
|
||||
const handler = function(err) {
|
||||
if (!resolvedOrErrored) {
|
||||
resolvedOrErrored = true
|
||||
reject(
|
||||
wrapError(err, 'error before stream became ready', {}, ReadError)
|
||||
)
|
||||
}
|
||||
}
|
||||
const onStreamReady = function() {
|
||||
if (!resolvedOrErrored) {
|
||||
resolvedOrErrored = true
|
||||
lastStream.removeListener('readable', onStreamReady)
|
||||
lastStream.removeListener('error', onError)
|
||||
|
||||
lastStream.removeListener('readable', handler)
|
||||
if (err) {
|
||||
return reject(
|
||||
wrapError(err, 'error before stream became ready', {}, ReadError)
|
||||
)
|
||||
}
|
||||
resolve(lastStream)
|
||||
}
|
||||
}
|
||||
|
||||
pipeline(...streams).catch(onError)
|
||||
|
||||
lastStream.on('readable', onStreamReady)
|
||||
lastStream.on('error', onError)
|
||||
pipeline(...streams).catch(handler)
|
||||
lastStream.on('readable', handler)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -202,7 +202,7 @@ describe('GcsPersistorTests', function() {
|
||||
|
||||
beforeEach(async function() {
|
||||
Transform.prototype.on = sinon.stub()
|
||||
Transform.prototype.on.withArgs('error').yields(GcsNotFoundError)
|
||||
Stream.pipeline.yields(GcsNotFoundError)
|
||||
try {
|
||||
stream = await GcsPersistor.promises.getFileStream(bucket, key)
|
||||
} catch (err) {
|
||||
@@ -232,7 +232,7 @@ describe('GcsPersistorTests', function() {
|
||||
|
||||
beforeEach(async function() {
|
||||
Transform.prototype.on = sinon.stub()
|
||||
Transform.prototype.on.withArgs('error').yields(genericError)
|
||||
Stream.pipeline.yields(genericError)
|
||||
try {
|
||||
stream = await GcsPersistor.promises.getFileStream(bucket, key)
|
||||
} catch (err) {
|
||||
|
||||
@@ -292,7 +292,7 @@ describe('S3PersistorTests', function() {
|
||||
|
||||
beforeEach(async function() {
|
||||
Transform.prototype.on = sinon.stub()
|
||||
Transform.prototype.on.withArgs('error').yields(S3NotFoundError)
|
||||
Stream.pipeline.yields(S3NotFoundError)
|
||||
try {
|
||||
stream = await S3Persistor.promises.getFileStream(bucket, key)
|
||||
} catch (err) {
|
||||
@@ -322,7 +322,7 @@ describe('S3PersistorTests', function() {
|
||||
|
||||
beforeEach(async function() {
|
||||
Transform.prototype.on = sinon.stub()
|
||||
Transform.prototype.on.withArgs('error').yields(S3AccessDeniedError)
|
||||
Stream.pipeline.yields(S3AccessDeniedError)
|
||||
try {
|
||||
stream = await S3Persistor.promises.getFileStream(bucket, key)
|
||||
} catch (err) {
|
||||
@@ -352,7 +352,7 @@ describe('S3PersistorTests', function() {
|
||||
|
||||
beforeEach(async function() {
|
||||
Transform.prototype.on = sinon.stub()
|
||||
Transform.prototype.on.withArgs('error').yields(genericError)
|
||||
Stream.pipeline.yields(genericError)
|
||||
try {
|
||||
stream = await S3Persistor.promises.getFileStream(bucket, key)
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user