overleaf/services/document-updater/app/js/Limits.js
Brian Gough 865a973426 Merge pull request #5132 from overleaf/bg-fix-size-check
[docupdater] use more accurate doc size check

GitOrigin-RevId: f66d68a7f7fdc127cc31539abdcab65549823d02
2021-09-21 08:02:55 +00:00

31 lines
1.2 KiB
JavaScript

module.exports = {
// compute the total size of the document in chararacters, including newlines
getTotalSizeOfLines(lines) {
let size = 0
for (const line of lines) {
size += line.length + 1 // include the newline
}
return size
},
// check whether the total size of the document in characters exceeds the
// maxDocLength.
//
// The estimated size should be an upper bound on the true size, typically
// it will be the size of the JSON.stringified array of lines. If the
// estimated size is less than the maxDocLength then we know that the total
// size of lines will also be less than maxDocLength.
docIsTooLarge(estimatedSize, lines, maxDocLength) {
if (estimatedSize <= maxDocLength) {
return false // definitely under the limit, no need to calculate the total size
}
// calculate the total size, bailing out early if the size limit is reached
let size = 0
for (const line of lines) {
size += line.length + 1 // include the newline
if (size > maxDocLength) return true
}
// since we didn't hit the limit in the loop, the document is within the allowed length
return false
},
}