2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const fs = require('fs')
|
2020-08-11 05:35:08 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const uuid = require('uuid')
|
|
|
|
const _ = require('underscore')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const request = require('request')
|
2020-05-22 07:17:17 -04:00
|
|
|
const { Transform, pipeline } = require('stream')
|
2020-05-25 05:20:58 -04:00
|
|
|
const { FileTooLargeError } = require('../Features/Errors/Errors')
|
2020-03-04 04:37:43 -05:00
|
|
|
const { promisifyAll } = require('../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-05-22 07:17:17 -04:00
|
|
|
class SizeLimitedStream extends Transform {
|
|
|
|
constructor(options) {
|
|
|
|
options.autoDestroy = true
|
|
|
|
super(options)
|
|
|
|
|
|
|
|
this.bytes = 0
|
2020-06-01 09:54:29 -04:00
|
|
|
this.maxSizeBytes = options.maxSizeBytes
|
2020-05-22 07:17:17 -04:00
|
|
|
this.drain = false
|
|
|
|
this.on('error', () => {
|
|
|
|
this.drain = true
|
|
|
|
this.resume()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_transform(chunk, encoding, done) {
|
|
|
|
if (this.drain) {
|
|
|
|
// mechanism to drain the source stream on error, to avoid leaks
|
|
|
|
// we consume the rest of the incoming stream and don't push it anywhere
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.bytes += chunk.length
|
2020-06-01 09:54:29 -04:00
|
|
|
if (this.maxSizeBytes && this.bytes > this.maxSizeBytes) {
|
2020-05-22 07:17:17 -04:00
|
|
|
return done(
|
2020-05-25 05:20:58 -04:00
|
|
|
new FileTooLargeError({
|
2020-05-22 07:17:17 -04:00
|
|
|
message: 'stream size limit reached',
|
2021-04-27 03:52:58 -04:00
|
|
|
info: { size: this.bytes },
|
2020-05-22 07:17:17 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
this.push(chunk)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 04:37:43 -05:00
|
|
|
const FileWriter = {
|
2019-05-29 05:21:06 -04:00
|
|
|
ensureDumpFolderExists(callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return fs.mkdir(Settings.path.dumpFolder, function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null && error.code !== 'EEXIST') {
|
|
|
|
// Ignore error about already existing
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
writeLinesToDisk(identifier, lines, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return FileWriter.writeContentToDisk(identifier, lines.join('\n'), callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
writeContentToDisk(identifier, content, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
callback = _.once(callback)
|
|
|
|
const fsPath = `${Settings.path.dumpFolder}/${identifier}_${uuid.v4()}`
|
2021-04-14 09:17:21 -04:00
|
|
|
return FileWriter.ensureDumpFolderExists(function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return fs.writeFile(fsPath, content, function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, fsPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-05-22 07:17:17 -04:00
|
|
|
writeStreamToDisk(identifier, stream, options, callback) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-05-22 07:17:17 -04:00
|
|
|
options = options || {}
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const fsPath = `${Settings.path.dumpFolder}/${identifier}_${uuid.v4()}`
|
|
|
|
|
|
|
|
stream.pause()
|
2020-05-22 07:17:17 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
FileWriter.ensureDumpFolderExists(function (error) {
|
2020-05-22 07:17:17 -04:00
|
|
|
const writeStream = fs.createWriteStream(fsPath)
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
stream.resume()
|
|
|
|
|
2020-05-22 07:17:17 -04:00
|
|
|
const passThrough = new SizeLimitedStream({
|
2021-04-27 03:52:58 -04:00
|
|
|
maxSizeBytes: options.maxSizeBytes,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2020-05-22 07:17:17 -04:00
|
|
|
|
|
|
|
// if writing fails, we want to consume the bytes from the source, to avoid leaks
|
|
|
|
for (const evt of ['error', 'close']) {
|
2021-04-14 09:17:21 -04:00
|
|
|
writeStream.on(evt, function () {
|
2020-05-22 07:17:17 -04:00
|
|
|
passThrough.unpipe(writeStream)
|
|
|
|
passThrough.resume()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
pipeline(stream, passThrough, writeStream, function (err) {
|
2020-05-25 05:20:58 -04:00
|
|
|
if (
|
2020-06-01 09:54:29 -04:00
|
|
|
options.maxSizeBytes &&
|
2020-05-25 05:20:58 -04:00
|
|
|
passThrough.bytes >= options.maxSizeBytes &&
|
|
|
|
!(err instanceof FileTooLargeError)
|
|
|
|
) {
|
|
|
|
err = new FileTooLargeError({
|
|
|
|
message: 'stream size limit reached',
|
2021-04-27 03:52:58 -04:00
|
|
|
info: { size: passThrough.bytes },
|
2020-05-25 05:20:58 -04:00
|
|
|
}).withCause(err || {})
|
|
|
|
}
|
2020-05-22 07:17:17 -04:00
|
|
|
if (err) {
|
2020-08-11 05:35:08 -04:00
|
|
|
OError.tag(
|
|
|
|
err,
|
|
|
|
'[writeStreamToDisk] something went wrong writing the stream to disk',
|
|
|
|
{
|
|
|
|
identifier,
|
2021-04-27 03:52:58 -04:00
|
|
|
fsPath,
|
2020-08-11 05:35:08 -04:00
|
|
|
}
|
2020-05-22 07:17:17 -04:00
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ identifier, fsPath },
|
|
|
|
'[writeStreamToDisk] write stream finished'
|
|
|
|
)
|
2020-05-22 07:17:17 -04:00
|
|
|
callback(null, fsPath)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-05-22 07:17:17 -04:00
|
|
|
writeUrlToDisk(identifier, url, options, callback) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-05-22 07:17:17 -04:00
|
|
|
options = options || {}
|
2019-05-29 05:21:06 -04:00
|
|
|
callback = _.once(callback)
|
2020-05-22 07:17:17 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const stream = request.get(url)
|
2021-04-14 09:17:21 -04:00
|
|
|
stream.on('error', function (err) {
|
2019-07-08 04:56:08 -04:00
|
|
|
logger.warn(
|
|
|
|
{ err, identifier, url },
|
|
|
|
'[writeUrlToDisk] something went wrong with writing to disk'
|
|
|
|
)
|
|
|
|
callback(err)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
stream.on('response', function (response) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
2020-05-22 07:17:17 -04:00
|
|
|
FileWriter.writeStreamToDisk(identifier, stream, options, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
const err = new Error(`bad response from url: ${response.statusCode}`)
|
2019-07-08 04:56:08 -04:00
|
|
|
logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-04 04:37:43 -05:00
|
|
|
|
|
|
|
module.exports = FileWriter
|
|
|
|
module.exports.promises = promisifyAll(FileWriter)
|
2020-05-22 07:17:17 -04:00
|
|
|
module.exports.SizeLimitedStream = SizeLimitedStream
|