-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a diff route. Some adjustments to vfs queries
- Loading branch information
Showing
8 changed files
with
212 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
import request from "supertest"; | ||
import Vfs from "../../../vfs/index.js"; | ||
import User from "../../../auth/User.js"; | ||
import UserManager from "../../../auth/UserManager.js"; | ||
|
||
|
||
|
||
/** | ||
* Minimal tests as most | ||
*/ | ||
|
||
describe("GET /history/:scene/:id/diff", function(){ | ||
let vfs :Vfs, userManager :UserManager, user :User, admin :User, opponent :User; | ||
let scene_id :number; | ||
this.beforeAll(async function(){ | ||
let locals = await createIntegrationContext(this); | ||
vfs = locals.vfs; | ||
userManager = locals.userManager; | ||
user = await userManager.addUser("bob", "12345678"); | ||
admin = await userManager.addUser("alice", "12345678", true); | ||
opponent = await userManager.addUser("oscar", "12345678"); | ||
scene_id = await vfs.createScene("foo", user.uid); | ||
}); | ||
|
||
this.afterAll(async function(){ | ||
await cleanIntegrationContext(this); | ||
}); | ||
|
||
it("get a text file's diff from previous version", async function(){ | ||
await vfs.writeFile(dataStream(["Hello\n"]), {scene:scene_id, name:"hello.txt", user_id: 0, mime: "text/plain"}); | ||
let ref = await vfs.writeFile(dataStream(["Hello World\n"]), {scene:scene_id, name:"hello.txt", user_id: 0, mime: "text/plain"}); | ||
let res = await request(this.server).get(`/history/foo/${ref.id}/diff`) | ||
.set("Accept", "text/plain") | ||
.expect(200) | ||
.expect("Content-Type", "text/plain; charset=utf-8"); | ||
expect(res.text).to.equal(`1c1\n< Hello\n---\n> Hello World\n`); | ||
}); | ||
|
||
it("get diff summary for documents", async function(){ | ||
await vfs.writeDoc(`{"label":"foo"}`, {scene: scene_id, name: "scene.svx.json", mime: "application/si-dpo-3d.document+json", user_id: 0}); | ||
let ref = await vfs.writeDoc(`{"label":"bar"}`, {scene: scene_id, name: "scene.svx.json", mime: "application/si-dpo-3d.document+json", user_id: 0}); | ||
let res = await request(this.server).get(`/history/foo/${ref.id}/diff`) | ||
.set("Accept", "application/json") | ||
.expect(200) | ||
.expect("Content-Type", "application/json; charset=utf-8"); | ||
expect(res.body).to.have.property("diff", 'Diff of JSON data not yet supported'); | ||
expect(res.body).to.have.property("src"); | ||
expect(res.body).to.have.property("dst"); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,106 @@ | ||
import {execFile} from "node:child_process"; | ||
|
||
import {Request, Response} from "express"; | ||
|
||
import { getVfs } from "../../../utils/locals.js"; | ||
import { BadRequestError } from "../../../utils/errors.js"; | ||
import { FileProps } from "../../../vfs/types.js"; | ||
import { diffDoc } from "../../../utils/merge/index.js"; | ||
import { fromPointers } from "../../../utils/merge/pointers/index.js"; | ||
|
||
const sizeMax = 400*1000; | ||
|
||
/** | ||
* Computes a somewhat arbitrary text representation of the difference between | ||
*/ | ||
export default async function handleGetDiff(req :Request, res :Response){ | ||
|
||
const vfs = getVfs(req); | ||
const {scene, id:idString, from:fromString="-1"} = req.params; | ||
const id = parseInt(idString); | ||
const fromIdOrGen = parseInt(fromString); | ||
if(!Number.isInteger(id)) throw new BadRequestError(`Requested fromId ${idString} is not a valid ID`); | ||
if(!Number.isInteger(fromIdOrGen)) throw new BadRequestError(`Requested toId ${fromString} is not a valid ID`); | ||
|
||
const dstFile = await vfs.getFileById(id); | ||
let fromFile :FileProps; | ||
if(0 < fromIdOrGen){ | ||
fromFile = await vfs.getFileById(fromIdOrGen); | ||
}else if(0 < dstFile.generation + fromIdOrGen){ | ||
fromFile = await vfs.getFileProps({ | ||
scene: scene, | ||
name: dstFile.name, | ||
generation: dstFile.generation + fromIdOrGen, | ||
archive: true, | ||
}, true); | ||
}else{ | ||
fromFile = { | ||
id: -1, //Ensure it _can't_ exist | ||
name: dstFile.name, | ||
mime: dstFile.mime, | ||
hash: null, | ||
data: undefined, | ||
size: 0, | ||
generation: 0, | ||
ctime: new Date(0), | ||
mtime: new Date(0), | ||
author_id: 0, | ||
author: "default", | ||
} | ||
} | ||
let diff:string; | ||
if(fromFile.data && dstFile.data && dstFile.mime == fromFile.mime && fromFile.mime === "application/si-dpo-3d.document+json"){ | ||
try{ | ||
let fromDoc = JSON.parse(fromFile.data); | ||
let toDoc = JSON.parse(dstFile.data); | ||
diff = `STRUCTURED CHANGES SUMMARY\n`+JSON.stringify(fromPointers(diffDoc(fromDoc, toDoc) as any), null, 2); | ||
}catch(e:any){ | ||
console.error("Failed to compile scene diff : ", e); | ||
diff = `Couldn't compile diff from #${fromFile.id} to #${dstFile.id}: ${e.message}`; | ||
} | ||
|
||
}else if(sizeMax < dstFile.size || sizeMax < fromFile.size){ | ||
diff = "Diff not computed for large files" | ||
}else if(fromFile.hash === dstFile.hash){ | ||
diff = "No differences"; | ||
}else if(fromFile.hash === "directory" || dstFile.hash === "directory"){ | ||
diff = "No diff for folders"; | ||
}else{ | ||
let srcPath = (vfs.exists(fromFile))? vfs.getPath(fromFile): "/dev/null"; | ||
let dstPath = (vfs.exists(dstFile))? vfs.getPath(dstFile): "/dev/null"; | ||
let stdout = await new Promise<string>((resolve, reject)=>{ | ||
execFile("diff", [ | ||
"--unified", | ||
"--tabsize=2", | ||
"--ignore-space-change", | ||
"--label", fromFile.name, | ||
"--label", dstFile.name, | ||
srcPath, | ||
dstPath | ||
], { | ||
encoding: "utf8", | ||
timeout: 500, | ||
}, (error, stdout, stderr)=>{ | ||
if(error && error.code != 0 && error.code != 1){ | ||
return reject(error); | ||
} | ||
resolve(stdout); | ||
}); | ||
}); | ||
if(10000000 < stdout.length){ | ||
diff = "Large diff not shown"; | ||
}else{ | ||
diff = stdout; | ||
} | ||
} | ||
|
||
res.format({ | ||
"text/plain": ()=>{ | ||
res.status(200).send(diff); | ||
}, | ||
"application/json": ()=>{ | ||
let src = {...fromFile, data: null}; | ||
const dst = {...dstFile, data: null}; | ||
res.status(200).send({src, dst, diff}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters