Limits.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module.exports = {
  2. // compute the total size of the document in chararacters, including newlines
  3. getTotalSizeOfLines(lines) {
  4. let size = 0
  5. for (const line of lines) {
  6. size += line.length + 1 // include the newline
  7. }
  8. return size
  9. },
  10. // check whether the total size of the document in characters exceeds the
  11. // maxDocLength.
  12. //
  13. // The estimated size should be an upper bound on the true size, typically
  14. // it will be the size of the JSON.stringified array of lines. If the
  15. // estimated size is less than the maxDocLength then we know that the total
  16. // size of lines will also be less than maxDocLength.
  17. docIsTooLarge(estimatedSize, lines, maxDocLength) {
  18. if (estimatedSize <= maxDocLength) {
  19. return false // definitely under the limit, no need to calculate the total size
  20. }
  21. // calculate the total size, bailing out early if the size limit is reached
  22. let size = 0
  23. for (const line of lines) {
  24. size += line.length + 1 // include the newline
  25. if (size > maxDocLength) return true
  26. }
  27. // since we didn't hit the limit in the loop, the document is within the allowed length
  28. return false
  29. },
  30. /**
  31. * @param {StringFileRawData} raw
  32. * @param {number} maxDocLength
  33. */
  34. stringFileDataContentIsTooLarge(raw, maxDocLength) {
  35. let n = raw.content.length
  36. if (n <= maxDocLength) return false // definitely under the limit, no need to calculate the total size
  37. for (const tc of raw.trackedChanges ?? []) {
  38. if (tc.tracking.type !== 'delete') continue
  39. n -= tc.range.length
  40. if (n <= maxDocLength) return false // under the limit now, no need to calculate the exact size
  41. }
  42. return true
  43. },
  44. }