mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
* [editor-core] share the pieces the editor, real-time and history-v1 all need Project-level history-OT has the browser, real-time and history-v1 all reason about the same changes, so the rules they reason with move into the shared library rather than being reimplemented on each side. - `origin/editor_origin.js` plus `EDITOR_ORIGIN_KIND` and its schema entry: the origin an editor client's change carries, holding the editorId that scopes a change's timestamp. - `change_identity.js`: how a submission is recognised when it comes back, so a resend of a change that already landed is deduplicated rather than applied twice. - `rebase.js`: transforming a change onto concurrent ones, in the same order on both sides, which is what lets the server acknowledge a change with only the version it landed at. - `text_file_defaults.js`: the doc-vs-file extension lists, now read from here by web's settings instead of being spelled out again. - `blob_string_length.js`: the string length of a blob, used by history-v1's blob store and by web when it writes an uploaded file into history. - `file_tree_diff.js`: file-tree operations to a diff, used by project-history's FileTreeDiffGenerator. - `Blob.EMPTY_HASH`, so callers can answer for the empty blob without storing it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * [project-history] build the file-tree diff with the shared helper FileTreeDiffGenerator's hand-rolled walk over add/edit/move/delete operations becomes a call to `buildFileTreeDiff` from overleaf-editor-core, so the editor and project-history read a file-tree change the same way. Adds the unit tests the decaffeinated original never had. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * [web] read the editable-file lists from overleaf-editor-core web's settings spelled out the text extensions and editable filenames that decide whether a file becomes a doc, and every other service that has to make that call carried its own copy. The lists now live in overleaf-editor-core and web builds settings.textExtensions and settings.editableFilenames from them, keeping the ADDITIONAL_TEXT_EXTENSIONS override for Server CE/Pro on top. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * [history-v1] read a blob's string length from overleaf-editor-core Two copies of getStringLengthOfFile lived here, the one in recover_zip.js carrying a TODO asking for exactly this. Both now import the shared implementation, which the blob store keeps re-exporting for backupArchiver. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> GitOrigin-RevId: 31948332f416f56848e3b0dfb5142a4c9f93f508
312 lines
8.4 KiB
JavaScript
312 lines
8.4 KiB
JavaScript
/**
|
|
* Try to recover a zip of the latest version of a project using only data in
|
|
* GCS, where this data may have been (recently) hard deleted (i.e. may exist
|
|
* wholely or in part as non-current versions). This should be able to
|
|
* retrieve the latest content of a project up to 180 days after it was
|
|
* deleted.
|
|
*
|
|
* Usage:
|
|
* node recover_zip.js [--verbose] <HISTORY_ID> <HISTORY_ID> ...
|
|
*
|
|
* Output:
|
|
* Signed URL(s) for the uploaded zip files. Note that these are valid for
|
|
* only 24h, to match the lifecycle rule on the zip bucket.
|
|
*/
|
|
|
|
const fs = require('node:fs')
|
|
const os = require('node:os')
|
|
const path = require('node:path')
|
|
const util = require('node:util')
|
|
const { pipeline } = require('node:stream/promises')
|
|
|
|
// Something is registering 11 listeners, over the limit
|
|
// of 10, which generates a lot of warning noise.
|
|
require('node:events').EventEmitter.defaultMaxListeners = 11
|
|
|
|
const config = require('config')
|
|
// We depend on this via object-persistor.
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
const { Storage } = require('@google-cloud/storage')
|
|
// zip-stream@7 uses ESM default export
|
|
const ZipStream = require('zip-stream').default
|
|
|
|
function createStorage() {
|
|
const opts = {}
|
|
if (config.has('persistor.gcs.endpoint.apiEndpoint')) {
|
|
opts.apiEndpoint = config.get('persistor.gcs.endpoint.apiEndpoint')
|
|
}
|
|
if (config.has('persistor.gcs.endpoint.projectId')) {
|
|
opts.projectId = config.get('persistor.gcs.endpoint.projectId')
|
|
}
|
|
return new Storage(opts)
|
|
}
|
|
|
|
const core = require('overleaf-editor-core')
|
|
const {
|
|
getStringLengthOfFile,
|
|
} = require('overleaf-editor-core/lib/blob_string_length')
|
|
const projectKey = require('@overleaf/object-persistor/src/ProjectKey.js')
|
|
const streams = require('../lib/streams')
|
|
|
|
const {
|
|
values: { verbose: VERBOSE },
|
|
positionals: HISTORY_IDS,
|
|
} = util.parseArgs({
|
|
options: {
|
|
verbose: {
|
|
type: 'boolean',
|
|
default: false,
|
|
},
|
|
},
|
|
allowPositionals: true,
|
|
})
|
|
|
|
if (HISTORY_IDS.length === 0) {
|
|
console.error('no history IDs; see usage')
|
|
process.exit(1)
|
|
}
|
|
|
|
async function listDeletedChunks(historyId) {
|
|
const bucketName = config.get('chunkStore.bucket')
|
|
const storage = createStorage()
|
|
const [files] = await storage.bucket(bucketName).getFiles({
|
|
prefix: projectKey.format(historyId),
|
|
versions: true,
|
|
})
|
|
return files
|
|
}
|
|
|
|
async function findLatestChunk(historyId) {
|
|
const files = await listDeletedChunks(historyId)
|
|
if (files.length === 0) return null
|
|
files.sort((a, b) => {
|
|
if (a.name < b.name) return -1
|
|
if (a.name > b.name) return 1
|
|
return 0
|
|
})
|
|
return files[files.length - 1]
|
|
}
|
|
|
|
async function downloadLatestChunk(tmp, historyId) {
|
|
const latestChunkFile = await findLatestChunk(historyId)
|
|
if (!latestChunkFile) throw new Error('no chunk found to recover')
|
|
|
|
const destination = path.join(tmp, 'latest.json')
|
|
await latestChunkFile.download({ destination })
|
|
return destination
|
|
}
|
|
|
|
async function loadHistory(historyPathname) {
|
|
const data = await fs.promises.readFile(historyPathname)
|
|
const rawHistory = JSON.parse(data)
|
|
return core.History.fromRaw(rawHistory)
|
|
}
|
|
|
|
async function loadChunk(historyPathname, blobStore) {
|
|
const history = await loadHistory(historyPathname)
|
|
|
|
const blobHashes = new Set()
|
|
history.findBlobHashes(blobHashes)
|
|
|
|
await blobStore.fetchBlobs(blobHashes)
|
|
await history.loadFiles('lazy', blobStore)
|
|
|
|
return new core.Chunk(history, 0)
|
|
}
|
|
|
|
class RecoveryBlobStore {
|
|
constructor(historyId, tmp) {
|
|
this.historyId = historyId
|
|
this.tmp = tmp
|
|
this.blobs = new Map()
|
|
}
|
|
|
|
async fetchBlobs(blobHashes) {
|
|
for await (const blobHash of blobHashes) {
|
|
await this.fetchBlob(blobHash)
|
|
}
|
|
}
|
|
|
|
async fetchBlob(hash) {
|
|
if (this.blobs.has(hash)) return
|
|
|
|
if (VERBOSE) console.log('fetching blob', hash)
|
|
|
|
const bucketName = config.get('blobStore.projectBucket')
|
|
const storage = createStorage()
|
|
const [files] = await storage.bucket(bucketName).getFiles({
|
|
prefix: this.makeProjectBlobKey(hash),
|
|
versions: true,
|
|
})
|
|
|
|
const destination = this.getBlobPathname(hash)
|
|
|
|
if (files.length === 0) {
|
|
await this.fetchGlobalBlob(hash, destination)
|
|
} else if (files.length === 1) {
|
|
await files[0].download({ destination })
|
|
} else {
|
|
throw new Error('Multiple versions of blob ' + hash)
|
|
}
|
|
|
|
this.blobs.set(hash, await this.makeBlob(hash, destination))
|
|
}
|
|
|
|
async fetchGlobalBlob(hash, destination) {
|
|
const bucketName = config.get('blobStore.globalBucket')
|
|
const storage = createStorage()
|
|
const file = storage.bucket(bucketName).file(this.makeGlobalBlobKey(hash))
|
|
await file.download({ destination })
|
|
}
|
|
|
|
async makeBlob(hash, pathname) {
|
|
const stat = await fs.promises.stat(pathname)
|
|
const byteLength = stat.size
|
|
const stringLength = await getStringLengthOfFile(byteLength, pathname)
|
|
return new core.Blob(hash, byteLength, stringLength)
|
|
}
|
|
|
|
async getString(hash) {
|
|
const stream = await this.getStream(hash)
|
|
const buffer = await streams.readStreamToBuffer(stream)
|
|
return buffer.toString()
|
|
}
|
|
|
|
async getStream(hash) {
|
|
return fs.createReadStream(this.getBlobPathname(hash))
|
|
}
|
|
|
|
async getBlob(hash) {
|
|
return this.blobs.get(hash)
|
|
}
|
|
|
|
getBlobPathname(hash) {
|
|
return path.join(this.tmp, hash)
|
|
}
|
|
|
|
makeGlobalBlobKey(hash) {
|
|
return `${hash.slice(0, 2)}/${hash.slice(2, 4)}/${hash.slice(4)}`
|
|
}
|
|
|
|
makeProjectBlobKey(hash) {
|
|
return `${projectKey.format(this.historyId)}/${hash.slice(
|
|
0,
|
|
2
|
|
)}/${hash.slice(2)}`
|
|
}
|
|
}
|
|
|
|
async function uploadZip(historyId, zipPathname) {
|
|
const bucketName = config.get('zipStore.bucket')
|
|
const deadline = 24 * 3600 * 1000 // lifecycle limit on the zips bucket
|
|
const storage = createStorage()
|
|
const destination = `${historyId}-recovered.zip`
|
|
await storage.bucket(bucketName).upload(zipPathname, {
|
|
destination,
|
|
resumable: false,
|
|
})
|
|
|
|
if (config.has('persistor.gcs.endpoint.apiEndpoint')) {
|
|
// In emulator mode, signed URLs aren't available
|
|
const apiEndpoint = config.get('persistor.gcs.endpoint.apiEndpoint')
|
|
return `${apiEndpoint}/storage/v1/b/${bucketName}/o/${encodeURIComponent(destination)}?alt=media`
|
|
}
|
|
|
|
const signedUrls = await storage
|
|
.bucket(bucketName)
|
|
.file(destination)
|
|
.getSignedUrl({
|
|
version: 'v4',
|
|
action: 'read',
|
|
expires: Date.now() + deadline,
|
|
})
|
|
|
|
return signedUrls[0]
|
|
}
|
|
|
|
/**
|
|
* Promisified wrapper for ZipStream's entry method.
|
|
*
|
|
* @param {ZipStream} archive
|
|
* @param {Buffer|NodeJS.ReadableStream|string} source
|
|
* @param {{ name: string }} data
|
|
* @return {Promise<void>}
|
|
*/
|
|
function addEntry(archive, source, data) {
|
|
return new Promise((resolve, reject) => {
|
|
archive.entry(source, data, err => {
|
|
if (err) reject(err)
|
|
else resolve()
|
|
})
|
|
})
|
|
}
|
|
|
|
async function restoreProject(historyId) {
|
|
const tmp = await fs.promises.mkdtemp(
|
|
path.join(os.tmpdir(), historyId.toString())
|
|
)
|
|
if (VERBOSE) console.log('recovering', historyId, 'in', tmp)
|
|
|
|
const latestJsonPathname = await downloadLatestChunk(tmp, historyId)
|
|
const blobStore = new RecoveryBlobStore(historyId, tmp)
|
|
const chunk = await loadChunk(latestJsonPathname, blobStore)
|
|
|
|
const snapshot = chunk.getSnapshot()
|
|
for (const change of chunk.getChanges()) {
|
|
change.applyTo(snapshot)
|
|
}
|
|
|
|
if (VERBOSE) console.log('zipping', historyId)
|
|
|
|
const zipPathname = path.join(tmp, `${historyId}.zip`)
|
|
const outputFile = fs.createWriteStream(zipPathname)
|
|
const archive = new ZipStream()
|
|
|
|
const pipelinePromise = pipeline(archive, outputFile)
|
|
|
|
for (const pathname of snapshot.getFilePathnames()) {
|
|
const file = snapshot.getFile(pathname)
|
|
if (!file) continue
|
|
|
|
await file.load('eager', blobStore)
|
|
let content = file.getContent({
|
|
filterTrackedDeletes: true,
|
|
})
|
|
|
|
if (content === null) {
|
|
const hash = file.getHash()
|
|
content = await blobStore.getStream(hash)
|
|
}
|
|
|
|
if (content == null) continue
|
|
|
|
if (typeof content === 'string') {
|
|
content = Buffer.from(content)
|
|
}
|
|
await addEntry(archive, content, { name: pathname })
|
|
if (VERBOSE) console.log(`${pathname} added`)
|
|
}
|
|
|
|
archive.finalize()
|
|
await pipelinePromise
|
|
|
|
if (VERBOSE) {
|
|
console.log(`Wrote ${archive.getBytesWritten()} bytes`)
|
|
}
|
|
|
|
if (VERBOSE) console.log('uploading', historyId)
|
|
|
|
return await uploadZip(historyId, zipPathname)
|
|
}
|
|
|
|
async function main() {
|
|
for (const historyId of HISTORY_IDS) {
|
|
const signedUrl = await restoreProject(historyId)
|
|
console.log(signedUrl)
|
|
}
|
|
}
|
|
main().catch(err => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|