mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 09:09:36 +02:00
Merge pull request #32505 from overleaf/ar-remove-several-usages-of-request
[web] remove several usages of request GitOrigin-RevId: d6259bd47342f6ca4ab6ed6fc71b58f4b4962eee
This commit is contained in:
@@ -70,9 +70,9 @@ function _responseHandler(options, error, response, body, callback) {
|
||||
|
||||
const V1Api = {
|
||||
request: makeRequest,
|
||||
promises: {
|
||||
request: promisifyMultiResult(makeRequest, ['response', 'body']),
|
||||
},
|
||||
}
|
||||
|
||||
V1Api.promises = {
|
||||
request: promisifyMultiResult(V1Api.request, ['response', 'body']),
|
||||
}
|
||||
export default V1Api
|
||||
|
||||
@@ -10,12 +10,11 @@ import fs from 'node:fs'
|
||||
import OError from '@overleaf/o-error'
|
||||
import logger from '@overleaf/logger'
|
||||
import crypto from 'node:crypto'
|
||||
import _ from 'lodash'
|
||||
import Settings from '@overleaf/settings'
|
||||
import request from 'request'
|
||||
import { fetchStream } from '@overleaf/fetch-utils'
|
||||
import { Transform, pipeline } from 'node:stream'
|
||||
import { FileTooLargeError } from '../Features/Errors/Errors.js'
|
||||
import { promisify } from '@overleaf/promise-utils'
|
||||
import { callbackify, promisify } from '@overleaf/promise-utils'
|
||||
|
||||
export class SizeLimitedStream extends Transform {
|
||||
constructor(options) {
|
||||
@@ -146,47 +145,33 @@ const FileWriter = {
|
||||
callback(null, fsPath)
|
||||
})
|
||||
},
|
||||
|
||||
writeUrlToDisk(identifier, url, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
options = options || {}
|
||||
callback = _.once(callback)
|
||||
|
||||
const stream = request.get(url)
|
||||
const errorHandler = function (err) {
|
||||
logger.warn(
|
||||
{ err, identifier, url },
|
||||
'[writeUrlToDisk] something went wrong with writing to disk'
|
||||
)
|
||||
callback(err)
|
||||
}
|
||||
stream.on('error', errorHandler)
|
||||
stream.on('response', function (response) {
|
||||
stream.removeListener('error', errorHandler)
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
FileWriter.writeStreamToDisk(identifier, stream, options, callback)
|
||||
} else {
|
||||
const err = new OError('bad response from url', {
|
||||
statusCode: response.statusCode,
|
||||
})
|
||||
logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
|
||||
return callback(err)
|
||||
}
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
async function writeUrlToDisk(identifier, url, options = {}) {
|
||||
let stream
|
||||
try {
|
||||
stream = await fetchStream(url)
|
||||
} catch (error) {
|
||||
const err = new OError('bad response from url', {
|
||||
statusCode: error.response?.status,
|
||||
})
|
||||
logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
|
||||
throw err
|
||||
}
|
||||
return await FileWriter.promises.writeStreamToDisk(
|
||||
identifier,
|
||||
stream,
|
||||
options
|
||||
)
|
||||
}
|
||||
|
||||
FileWriter.writeUrlToDisk = callbackify(writeUrlToDisk)
|
||||
|
||||
FileWriter.promises = {
|
||||
writeLinesToDisk: promisify(FileWriter.writeLinesToDisk),
|
||||
writeContentToDisk: promisify(FileWriter.writeContentToDisk),
|
||||
writeStreamToDisk: promisify(FileWriter.writeStreamToDisk),
|
||||
writeUrlToDisk: promisify(FileWriter.writeUrlToDisk),
|
||||
writeUrlToDisk,
|
||||
}
|
||||
|
||||
export default FileWriter
|
||||
|
||||
Reference in New Issue
Block a user