[track-changes] Ignore duplicated packs when zipping SL history (#8353)

* [track-changes] Ignore duplicated packs when zipping SL history

* Fix incorrect `ObjectId` import in unit tests

GitOrigin-RevId: 165fcaa0c7bc57d383d01eedaf6dd3b078aff2aa
This commit is contained in:
Miguel Serrano
2022-06-13 08:03:22 +00:00
committed by Copybot
parent c2cfa96983
commit bb04841504
5 changed files with 83 additions and 0 deletions
@@ -11,6 +11,7 @@ const stream = require('stream')
const fs = require('fs')
const os = require('os')
const Path = require('path')
const { packsAreDuplicated } = require('./util/PackUtils')
const streamPipeline = util.promisify(stream.pipeline)
@@ -85,8 +86,13 @@ async function rewindDoc(projectId, docId, zipfile) {
let content = latestContent
let v = version
let update = lastUpdate
let previousUpdate = null
while (update) {
if (packsAreDuplicated(update, previousUpdate)) {
continue
}
const updatePath = `${id}/updates/${update.v}`
zipfile.addBuffer(Buffer.from(JSON.stringify(update)), updatePath, {
@@ -107,6 +113,7 @@ async function rewindDoc(projectId, docId, zipfile) {
ts: update.meta.start_ts,
doc_length: content.length,
})
previousUpdate = update
update = await getUpdate()
}
@@ -0,0 +1,18 @@
const _ = require('lodash')
/**
* Compares a deep equality of Packs excluding _id
*/
function packsAreDuplicated(pack1, pack2) {
return Boolean(
pack1 &&
pack2 &&
pack1.v === pack2.v &&
_.isEqual(pack1.meta, pack2.meta) &&
_.isEqual(pack1.op, pack2.op)
)
}
module.exports = {
packsAreDuplicated,
}