First functional version
This commit is contained in:
205
src/repo.ts
205
src/repo.ts
@ -5,8 +5,10 @@ export interface IDataStore {
|
||||
get(key: string): Promise<Uint8Array>;
|
||||
set(key: string, data: Uint8Array): Promise<void>;
|
||||
has(key: string): Promise<boolean>;
|
||||
}
|
||||
close?: () => Promise<void>;
|
||||
|
||||
getLock: () => Promise<() => Promise<void>>;
|
||||
}
|
||||
|
||||
export type NodeType = "tree" | "blob";
|
||||
export type NodeHash = string;
|
||||
@ -14,10 +16,11 @@ export type NodeFilename = string;
|
||||
export type TreeEntry = [NodeType, NodeHash, NodeFilename];
|
||||
|
||||
export type Commit = {
|
||||
id: string;
|
||||
root: string;
|
||||
before: string;
|
||||
date: Date;
|
||||
}
|
||||
};
|
||||
|
||||
// TODOs:
|
||||
// - HEAD locks
|
||||
@ -27,47 +30,46 @@ export type Commit = {
|
||||
|
||||
export default class Repository {
|
||||
#store: IDataStore;
|
||||
#head_lock: any;
|
||||
|
||||
constructor(store: IDataStore) {
|
||||
this.#store = store;
|
||||
this.init();
|
||||
}
|
||||
|
||||
private sha1(data: Uint8Array) {
|
||||
const s = new SHA("SHA-1", "UINT8ARRAY");
|
||||
s.update(data)
|
||||
s.update(data);
|
||||
return s.getHash("HEX");
|
||||
}
|
||||
|
||||
private sha256(data: Uint8Array) {
|
||||
const s = new SHA("SHA3-256", "UINT8ARRAY");
|
||||
s.update(data)
|
||||
s.update(data);
|
||||
return s.getHash("HEX");
|
||||
}
|
||||
|
||||
private splitPath(path: string) {
|
||||
return Path.resolve(path).slice(1).split(Path.delimiter);
|
||||
const resolved = Path.resolve(path).slice(1);
|
||||
if (resolved == "") return [];
|
||||
return resolved.split(Path.delimiter);
|
||||
}
|
||||
|
||||
private async writeObject(data: string, string: true): Promise<string>
|
||||
private async writeObject(data: Uint8Array, string?: false): Promise<string>
|
||||
private async writeObject(data: Uint8Array | string, string = false): Promise<string> {
|
||||
private getHeadLock() {}
|
||||
|
||||
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
|
||||
return objectID;
|
||||
}
|
||||
|
||||
private async hasObject(id: string): Promise<boolean> {
|
||||
return this.#store.has("objects/" + id);
|
||||
}
|
||||
|
||||
|
||||
private async readObject(id: string, string: true): Promise<string>
|
||||
private async readObject(id: string, string?: false): Promise<Uint8Array>
|
||||
private async readObject(id: string, string: true): Promise<string>;
|
||||
private async readObject(id: string, string?: false): Promise<Uint8Array>;
|
||||
private async readObject(id: string, string = false): Promise<Uint8Array | string> {
|
||||
let data = await this.#store.get("objects/" + id);
|
||||
if (string) {
|
||||
@ -77,105 +79,165 @@ export default class Repository {
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
||||
async clean() {
|
||||
// TODO: Cleanup broken things
|
||||
}
|
||||
|
||||
async read() {
|
||||
async readdir(path: string): Promise<TreeEntry[] | undefined> {
|
||||
const parts = this.splitPath(path);
|
||||
console.log({ parts });
|
||||
const head = await this.readHead();
|
||||
if (!head) return undefined;
|
||||
|
||||
const treeID = await this.treeFindObjectID(head.root, parts, "tree");
|
||||
|
||||
if (!treeID) return undefined;
|
||||
|
||||
return this.readTree(treeID);
|
||||
}
|
||||
|
||||
async read(path: string): Promise<Uint8Array | undefined> {
|
||||
const parts = this.splitPath(path);
|
||||
const head = await this.readHead();
|
||||
if (!head) return undefined;
|
||||
|
||||
const objectID = await this.treeFindObjectID(head.root, parts, "blob");
|
||||
|
||||
if (!objectID) return undefined;
|
||||
|
||||
return this.readObject(objectID);
|
||||
}
|
||||
|
||||
async write(path: string, data: Uint8Array) {
|
||||
const parts = this.splitPath(path);
|
||||
|
||||
|
||||
const objectID = await this.writeObject(data);
|
||||
|
||||
const head = await this.readHead();
|
||||
// const file = parts[parts.length - 1];
|
||||
// const folders = parts.slice(0, parts.length - 1)
|
||||
const lock = await this.#store.getLock();
|
||||
try {
|
||||
//TODO: Improve need of locking.
|
||||
const head = await this.readHead();
|
||||
|
||||
const makeTree = async (treeID: string | undefined, parts: string[]) => {
|
||||
let tree: TreeEntry[];
|
||||
if (treeID) {
|
||||
tree = await this.readTree(treeID);
|
||||
} else {
|
||||
tree = [];
|
||||
}
|
||||
|
||||
const makeTree = async (treeID: string | undefined, parts: string[]) => {
|
||||
let tree: TreeEntry[];
|
||||
if (treeID) {
|
||||
tree = await this.readTree(treeID);
|
||||
} else {
|
||||
tree = [];
|
||||
}
|
||||
const current = parts[0];
|
||||
|
||||
const current = parts[0];
|
||||
let existing = tree.findIndex(([, , name]) => name == current);
|
||||
|
||||
let existing = tree.findIndex(([, , name]) => name == current);
|
||||
let entry: TreeEntry;
|
||||
|
||||
let entry: TreeEntry;
|
||||
if (parts.length == 1) {
|
||||
entry = ["blob", objectID, current];
|
||||
} else {
|
||||
entry = [
|
||||
"tree",
|
||||
await makeTree(existing >= 0 ? tree[existing][1] : undefined, parts.slice(1)),
|
||||
current,
|
||||
];
|
||||
}
|
||||
|
||||
if (parts.length == 1) {
|
||||
entry = ["blob", objectID, current];
|
||||
} else {
|
||||
entry = ["tree", await makeTree(existing >= 0 ? tree[existing][1] : undefined, parts.slice(1)), current];
|
||||
}
|
||||
if (existing >= 0) {
|
||||
let ex = tree[existing];
|
||||
if (parts.length == 1 && ex[0] == "tree")
|
||||
throw new Error("This change would overwrite a folder!");
|
||||
tree[existing] = entry;
|
||||
} else {
|
||||
tree.push(entry);
|
||||
}
|
||||
|
||||
if (existing >= 0) {
|
||||
let ex = tree[existing];
|
||||
if (parts.length == 1 && ex[0] == "tree")
|
||||
throw new Error("This change would overwrite a folder!");
|
||||
tree[existing] = entry;
|
||||
} else {
|
||||
tree.push(entry);
|
||||
}
|
||||
let treeString = tree.map(([type, hash, name]) => `${type} ${hash} ${name}`).join("\n");
|
||||
|
||||
let newTreeID = await this.writeObject(treeString);
|
||||
|
||||
let treeString = tree.map(([type, hash, name]) => `${type} ${hash} ${name}`).join("\n");
|
||||
return newTreeID;
|
||||
};
|
||||
|
||||
let obj = this.writeObject(treeString, true);
|
||||
let newTree = await makeTree(head?.root, parts);
|
||||
let commit = await this.makeCommit(newTree, head);
|
||||
await this.writeHead(commit);
|
||||
} finally {
|
||||
await lock();
|
||||
}
|
||||
}
|
||||
|
||||
let newTreeID = "";
|
||||
|
||||
|
||||
|
||||
return newTreeID;
|
||||
async treeFindObjectID(
|
||||
treeID: string,
|
||||
parts: string[],
|
||||
type: NodeType
|
||||
): Promise<string | undefined> {
|
||||
if (parts.length == 0) {
|
||||
if (type == "tree") return treeID;
|
||||
else throw new Error("Cannot open root as file!");
|
||||
}
|
||||
|
||||
let newTree = makeTree(head?.root, parts);
|
||||
const tree = await this.readTree(treeID);
|
||||
const current = parts[0];
|
||||
let entry = tree.find(([type, id, name]) => name == current);
|
||||
if (!entry) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (parts.length == 1) {
|
||||
if (entry[0] != type) throw new Error("Expected 'blob' found 'tree'!");
|
||||
return entry[1];
|
||||
} else {
|
||||
if (entry[0] != "tree") throw new Error("Expected 'tree' found 'blob'!");
|
||||
return this.treeFindObjectID(entry[1], parts.splice(1), type);
|
||||
}
|
||||
}
|
||||
|
||||
async readTree(id: string): Promise<TreeEntry[]> {
|
||||
const tree = new TextDecoder().decode(await this.readObject(id));
|
||||
return tree.split("\n").map(e => {
|
||||
const tree = await this.readObject(id, true);
|
||||
return tree.split("\n").map((e) => {
|
||||
const entry = e.split(" ") as TreeEntry;
|
||||
const [type] = entry;
|
||||
|
||||
switch (type) {
|
||||
case "blob":
|
||||
case "tree":
|
||||
break
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid tree type.") //Might be a newer version or so
|
||||
throw new Error("Invalid tree type."); //Might be a newer version or so
|
||||
}
|
||||
|
||||
return entry;
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async readCommit(id: string): Promise<Commit> {
|
||||
if (!await this.hasObject(id))
|
||||
throw new Error(`Commit with id ${id} not found!`);
|
||||
if (!(await this.hasObject(id))) throw new Error(`Commit with id ${id} not found!`);
|
||||
|
||||
const commitStr = new TextDecoder().decode(await this.readObject(id));
|
||||
const commitStr = await this.readObject(id, true);
|
||||
|
||||
let commit: Commit = {} as any;
|
||||
for (const entry of commitStr.split("n")) {
|
||||
const [type, value] = entry.split(" ", 1);
|
||||
let commit: Commit = { id } as any;
|
||||
for (const entry of commitStr.split("\n")) {
|
||||
const [type] = entry.split(" ", 1);
|
||||
const value = entry.slice(type.length + 1);
|
||||
switch (type) {
|
||||
case "tree": // TODO: Simple validity checks
|
||||
commit.root = value;
|
||||
break;
|
||||
case "before": // TODO: Simple validity checks
|
||||
case "before": // TODO: Simple validity checks
|
||||
commit.before = value;
|
||||
case "date":
|
||||
commit.date = new Date(value)
|
||||
commit.date = new Date(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,10 +249,17 @@ export default class Repository {
|
||||
}
|
||||
|
||||
async readHead(): Promise<Commit | undefined> {
|
||||
if (!await this.#store.has("HEAD"))
|
||||
return undefined;
|
||||
if (!(await this.#store.has("HEAD"))) return undefined;
|
||||
const head = new TextDecoder().decode(await this.#store.get("HEAD"));
|
||||
|
||||
return this.readCommit(head);
|
||||
}
|
||||
}
|
||||
|
||||
async writeHead(commitID: string): Promise<void> {
|
||||
await this.#store.set("HEAD", new TextEncoder().encode(commitID));
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (this.#store.close) return this.#store?.close();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user