Starting on one-way sync

This commit is contained in:
user
2021-08-11 17:19:57 +02:00
parent f3f3d250b4
commit 247b7854aa
17 changed files with 2065 additions and 55 deletions

View File

@ -19,6 +19,7 @@ export type Commit = {
id: string;
root: string;
before: string;
merge: string;
date: Date;
};
@ -30,12 +31,25 @@ export type NodeLog = {
commit: Commit
}
export type ISyncTransferStream = {
on(type: "data", func: (data: Uint8Array) => void): void;
send(data: Uint8Array): void;
}
// TODOs:
// - HEAD locks
// - HEAD/Tree Cache
// - Remote synchronisation
// - Add DataStore Locking for access from multiple sources
export const ObjectTypes = {
blob: new Uint8Array([98, 108, 111, 98, 10]),
tree: new Uint8Array([116, 114, 101, 101, 10]),
comm: new Uint8Array([99, 111, 109, 109, 10]),
}
export type ObjectTypeNames = keyof typeof ObjectTypes;
export default class Repository {
//#region local variables
#store: IDataStore;
@ -65,30 +79,6 @@ export default class Repository {
return resolved.split(Path.sep);
}
private async writeObject(data: Uint8Array | string): Promise<string> {
if (typeof data == "string") {
data = new TextEncoder().encode(data);
}
const objectID = this.sha1(data);
await this.#store.set("objects/" + objectID, data);
return objectID;
}
private async hasObject(id: string): Promise<boolean> {
return this.#store.has("objects/" + id);
}
private async readObject(id: string, string: true): Promise<string | undefined>;
private async readObject(id: string, string?: false): Promise<Uint8Array | undefined>;
private async readObject(id: string, string = false): Promise<Uint8Array | string | undefined> {
let data = await this.#store.get("objects/" + id);
if (string) {
return new TextDecoder().decode(data);
} else {
return data;
}
}
private async treeFindObjectID(
treeID: string,
parts: string[],
@ -118,7 +108,106 @@ export default class Repository {
private async readTree(id: string): Promise<TreeEntry[]> {
const tree = await this.readObject(id, true);
if (tree == undefined) throw new Error("Invalid treeID");
return tree.split("\n").filter(e => e !== "").map((e) => {
return this.parseTree(tree);
}
private async makeCommit(treeID: string, old?: Commit, merge?: Commit) {
if (!old) {
// Could be called once more than necessary, if no HEAD exists.
old = await this.readHead();
}
let commitStr =
`tree ${treeID}\ndate ${new Date().toISOString()}\n`;
if (old) {
commitStr += `before ${old?.id}\n`;
if (merge) {
commitStr += `merge ${old?.id}\n`;
}
}
return await this.writeObject(commitStr, "comm");
}
//#endregion
//#region public
async clean() {
// TODO: Cleanup broken things
}
async writeHead(commitID: string): Promise<void> {
await this.#store.set("HEAD", new TextEncoder().encode(commitID));
}
public async writeObject(data: Uint8Array | string, type: ObjectTypeNames): Promise<string> {
if (typeof data == "string") {
data = new TextEncoder().encode(data);
}
const objectID = this.sha1(data);
let merged = new Uint8Array(5 + data.length);
merged.set(ObjectTypes[type], 0)
merged.set(data, 5);
await this.#store.set("objects/" + objectID, merged);
return objectID;
}
public async hasObject(id: string): Promise<boolean> {
return this.#store.has("objects/" + id);
}
public async readObjectRaw(id: string) {
return await this.#store.get("objects/" + id);
}
public async readObjectTyped(id: String) {
let data = await this.#store.get("objects/" + id);
if (!data)
return undefined;
let type = new TextDecoder().decode(data.slice(0, 4))
return {
type: type as ObjectTypeNames,
data: data.slice(5)
}
}
public async readObject(id: string, string: true): Promise<string | undefined>;
public async readObject(id: string, string?: false): Promise<Uint8Array | undefined>;
public async readObject(id: string, string = false): Promise<Uint8Array | string | undefined> {
const res = await this.readObjectTyped(id);
if (!res)
return undefined;
const { data } = res;
if (string) {
return new TextDecoder().decode(data);
} else {
return data;
}
}
public async readObjectType(id: string): Promise<ObjectTypeNames | undefined> {
const res = await this.readObjectTyped(id);
if (!res)
return undefined;
return res.type;
}
async hasCommit(id: string) {
let type = await this.readObjectType(id)
return type == "comm";
}
parseTree(treeStr: string | Uint8Array): TreeEntry[] {
if (typeof treeStr !== "string")
treeStr = new TextDecoder().decode(treeStr);
return treeStr.split("\n").filter(e => e !== "").map((e) => {
const entry = e.split(" ") as TreeEntry;
const [type] = entry;
@ -134,21 +223,9 @@ export default class Repository {
});
}
private async makeCommit(treeID: string, old?: Commit) {
if (!old) {
// Could be called once more than necessary, if no HEAD exists.
old = await this.readHead();
}
const commitStr =
`tree ${treeID}\ndate ${new Date().toISOString()}\n` + (old ? `before ${old?.id}\n` : "");
return await this.writeObject(commitStr);
}
private async readCommit(id: string): Promise<Commit> {
const commitStr = await this.readObject(id, true);
if (!commitStr)
throw new Error(`Commit with id ${id} not found!`);
parseCommit(id: string, commitStr: string | Uint8Array,) {
if (typeof commitStr !== "string")
commitStr = new TextDecoder().decode(commitStr);
let commit: Commit = { id } as any;
for (const entry of commitStr.split("\n")) {
@ -161,12 +238,25 @@ export default class Repository {
case "before": // TODO: Simple validity checks
commit.before = value;
break;
case "merge": // TODO: Simple validity checks
commit.merge = value;
break;
case "date":
commit.date = new Date(value);
break;
}
}
return commit;
}
async readCommit(id: string): Promise<Commit> {
const commitStr = await this.readObject(id, true);
if (!commitStr)
throw new Error(`Commit with id ${id} not found!`);
let commit = this.parseCommit(id, commitStr);
if (!commit.root) {
throw new Error("No tree defined in this commit!");
}
@ -174,23 +264,35 @@ export default class Repository {
return commit;
}
private async readHead(): Promise<Commit | undefined> {
async readHead(): Promise<Commit | undefined> {
if (!(await this.#store.has("HEAD"))) return undefined;
const head = new TextDecoder().decode(await this.#store.get("HEAD"));
return this.readCommit(head);
}
private async writeHead(commitID: string): Promise<void> {
await this.#store.set("HEAD", new TextEncoder().encode(commitID));
async mergeCommits(commit1: string, commit2: string): Promise<string> {
throw new Error("WIP");
// let newCommit = this.makeCommit()
}
//#endregion
async readCommitLog(till?: string, merges?: boolean): Promise<Commit[]> {
let head = await this.readHead();
if (!head)
return [];
let log: Commit[] = [head];
//#region public
async clean() {
// TODO: Cleanup broken things
let current = head;
while (current.before || (till && current.id == till)) {
current = await this.readCommit(current.before);
if (!current)
throw new Error("Repository sems damaged! Can't read commit!");
log.push(current)
}
return log;
}
async readdir(path: string): Promise<TreeEntry[]> {
@ -221,7 +323,7 @@ export default class Repository {
return this.readObject(id);
}
async log(path: string): Promise<any[]> {
async fileLog(path: string): Promise<any[]> {
const parts = this.splitPath(path);
const head = await this.readHead();
if (!head) return [];
@ -269,7 +371,7 @@ export default class Repository {
let objectID: string | undefined = undefined;
if (data) {
objectID = await this.writeObject(data);
objectID = await this.writeObject(data, "blob");
}
const lock = await this.#store.getLock();
@ -323,7 +425,7 @@ export default class Repository {
if (tree.length > 0) {
let treeString = tree.map(([type, hash, name]) => `${type} ${hash} ${name}`).join("\n");
let newTreeID = await this.writeObject(treeString);
let newTreeID = await this.writeObject(treeString, "tree");
return newTreeID;
} else {
@ -333,7 +435,7 @@ export default class Repository {
let newTree = await makeTree(head?.root, parts);
if (!newTree) { //TODO: Is this what i want?
newTree = await this.writeObject("");
newTree = await this.writeObject("", "tree");
}
let commit = await this.makeCommit(newTree, head);
await this.writeHead(commit);
@ -347,4 +449,4 @@ export default class Repository {
}
//#endregion
}
}