mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-04 21:34:53 -05:00
7f48c67512
* 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
62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
/* eslint-disable
|
|
no-unused-vars,
|
|
n/no-deprecated-api,
|
|
*/
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
// Fix any style issues and re-enable lint.
|
|
/*
|
|
* decaffeinate suggestions:
|
|
* DS101: Remove unnecessary use of Array.from
|
|
* 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
|
|
*/
|
|
let SafeReader
|
|
const fs = require('node:fs')
|
|
const logger = require('@overleaf/logger')
|
|
|
|
module.exports = SafeReader = {
|
|
// safely read up to size bytes from a file and return result as a
|
|
// string
|
|
|
|
readFile(file, size, encoding, callback) {
|
|
if (callback == null) {
|
|
callback = function () {}
|
|
}
|
|
return fs.open(file, 'r', function (err, fd) {
|
|
if (err != null && err.code === 'ENOENT') {
|
|
return callback()
|
|
}
|
|
if (err != null) {
|
|
return callback(err)
|
|
}
|
|
|
|
// safely return always closing the file
|
|
const callbackWithClose = (err, ...result) =>
|
|
fs.close(fd, function (err1) {
|
|
if (err != null) {
|
|
return callback(err)
|
|
}
|
|
if (err1 != null) {
|
|
return callback(err1)
|
|
}
|
|
return callback(null, ...Array.from(result))
|
|
})
|
|
const buff = Buffer.alloc(size) // fills with zeroes by default
|
|
return fs.read(
|
|
fd,
|
|
buff,
|
|
0,
|
|
buff.length,
|
|
0,
|
|
function (err, bytesRead, buffer) {
|
|
if (err != null) {
|
|
return callbackWithClose(err)
|
|
}
|
|
const result = buffer.toString(encoding, 0, bytesRead)
|
|
return callbackWithClose(null, result, bytesRead)
|
|
}
|
|
)
|
|
})
|
|
},
|
|
}
|