First Commit
This commit is contained in:
196
src/repo.ts
Normal file
196
src/repo.ts
Normal file
@ -0,0 +1,196 @@
|
||||
import SHA from "jssha";
|
||||
import Path from "./helper/path";
|
||||
|
||||
export interface IDataStore {
|
||||
get(key: string): Promise<Uint8Array>;
|
||||
set(key: string, data: Uint8Array): Promise<void>;
|
||||
has(key: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
|
||||
export type NodeType = "tree" | "blob";
|
||||
export type NodeHash = string;
|
||||
export type NodeFilename = string;
|
||||
export type TreeEntry = [NodeType, NodeHash, NodeFilename];
|
||||
|
||||
export type Commit = {
|
||||
root: string;
|
||||
before: string;
|
||||
date: Date;
|
||||
}
|
||||
|
||||
// TODOs:
|
||||
// - HEAD locks
|
||||
// - HEAD/Tree Cache
|
||||
// - Remote synchronisation
|
||||
// - Add DataStore Locking for access from multiple sources
|
||||
|
||||
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)
|
||||
return s.getHash("HEX");
|
||||
}
|
||||
|
||||
private sha256(data: Uint8Array) {
|
||||
const s = new SHA("SHA3-256", "UINT8ARRAY");
|
||||
s.update(data)
|
||||
return s.getHash("HEX");
|
||||
}
|
||||
|
||||
private splitPath(path: string) {
|
||||
return Path.resolve(path).slice(1).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> {
|
||||
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>
|
||||
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) {
|
||||
return new TextDecoder().decode(data);
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
|
||||
}
|
||||
|
||||
async read() {
|
||||
|
||||
}
|
||||
|
||||
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 makeTree = async (treeID: string | undefined, parts: string[]) => {
|
||||
let tree: TreeEntry[];
|
||||
if (treeID) {
|
||||
tree = await this.readTree(treeID);
|
||||
} else {
|
||||
tree = [];
|
||||
}
|
||||
|
||||
const current = parts[0];
|
||||
|
||||
let existing = tree.findIndex(([, , name]) => name == current);
|
||||
|
||||
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 (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 obj = this.writeObject(treeString, true);
|
||||
|
||||
let newTreeID = "";
|
||||
|
||||
|
||||
|
||||
return newTreeID;
|
||||
}
|
||||
|
||||
let newTree = makeTree(head?.root, parts);
|
||||
|
||||
}
|
||||
|
||||
async readTree(id: string): Promise<TreeEntry[]> {
|
||||
const tree = new TextDecoder().decode(await this.readObject(id));
|
||||
return tree.split("\n").map(e => {
|
||||
const entry = e.split(" ") as TreeEntry;
|
||||
const [type] = entry;
|
||||
|
||||
switch (type) {
|
||||
case "blob":
|
||||
case "tree":
|
||||
break
|
||||
default:
|
||||
throw new Error("Invalid tree type.") //Might be a newer version or so
|
||||
}
|
||||
|
||||
return entry;
|
||||
})
|
||||
}
|
||||
|
||||
async readCommit(id: string): Promise<Commit> {
|
||||
if (!await this.hasObject(id))
|
||||
throw new Error(`Commit with id ${id} not found!`);
|
||||
|
||||
const commitStr = new TextDecoder().decode(await this.readObject(id));
|
||||
|
||||
let commit: Commit = {} as any;
|
||||
for (const entry of commitStr.split("n")) {
|
||||
const [type, value] = entry.split(" ", 1);
|
||||
switch (type) {
|
||||
case "tree": // TODO: Simple validity checks
|
||||
commit.root = value;
|
||||
break;
|
||||
case "before": // TODO: Simple validity checks
|
||||
commit.before = value;
|
||||
case "date":
|
||||
commit.date = new Date(value)
|
||||
}
|
||||
}
|
||||
|
||||
if (!commit.root) {
|
||||
throw new Error("No tree defined in this commit!");
|
||||
}
|
||||
|
||||
return commit;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user