First functional version

This commit is contained in:
Fabian Stamm
2021-08-09 22:43:58 +02:00
parent e21a9fad3f
commit cf1347ca3a
9 changed files with 1070 additions and 163 deletions

View File

@ -0,0 +1,53 @@
import type { IDataStore } from "../repo";
import * as Fs from "fs";
import * as Path from "path";
export default class FSDataStore implements IDataStore {
#path: string;
#l = false;
constructor(path: string) {
this.#path = path;
}
async get(name: string) {
return Fs.promises.readFile(Path.join(this.#path, name));
}
async has(name: string) {
return Fs.promises
.access(Path.join(this.#path, name))
.then(() => true)
.catch(() => false);
}
async set(name: string, data: Uint8Array) {
const path = Path.join(this.#path, name);
await Fs.promises.mkdir(Path.dirname(path), { recursive: true });
await Fs.promises.writeFile(path, data);
}
async _delete(name: string) {
await Fs.promises.rm(Path.join(this.#path, name));
}
async getLock() {
//TODO: This is not atomic yet! FIX
while ((await this.has("lock")) || this.#l) {
try {
let con = (await this.get("lock")).toString("utf-8");
if (Number(con) < Date.now() - 10000) break;
await new Promise((y) => setTimeout(y, 10));
} catch (err) {}
}
this.#l = true;
await this.set("lock", Buffer.from(`${Date.now()}`));
this.#l = false;
return async () => {
await this._delete("lock");
};
}
}

View File

@ -19,28 +19,25 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
"use strict";
// resolves . and .. elements in a path array with directory names there
// must be no slashes or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
function normalizeArray(parts: string[], allowAboveRoot: boolean) {
var res = [];
for (var i = 0; i < parts.length; i++) {
var p = parts[i];
// ignore empty parts
if (!p || p === '.')
continue;
if (!p || p === ".") continue;
if (p === '..') {
if (res.length && res[res.length - 1] !== '..') {
if (p === "..") {
if (res.length && res[res.length - 1] !== "..") {
res.pop();
} else if (allowAboveRoot) {
res.push('..');
res.push("..");
}
} else {
res.push(p);
@ -52,107 +49,91 @@ function normalizeArray(parts, allowAboveRoot) {
// returns an array with empty elements removed from either end of the input
// array or the original array if no elements need to be removed
function trimArray(arr) {
function trimArray(arr: any[]) {
var lastIndex = arr.length - 1;
var start = 0;
for (; start <= lastIndex; start++) {
if (arr[start])
break;
if (arr[start]) break;
}
var end = lastIndex;
for (; end >= 0; end--) {
if (arr[end])
break;
if (arr[end]) break;
}
if (start === 0 && end === lastIndex)
return arr;
if (start > end)
return [];
if (start === 0 && end === lastIndex) return arr;
if (start > end) return [];
return arr.slice(start, end + 1);
}
// // Regex to split the tail part of the above into [*, dir, basename, ext]
// const splitTailRe =
// /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
// function normalizeUNCRoot(device) {
// return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
// }
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
const splitPathRe =
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
function posixSplitPath(filename) {
return splitPathRe.exec(filename).slice(1);
function posixSplitPath(filename: string) {
return splitPathRe.exec(filename)?.slice(1) as string[];
}
export class Posix {
static readonly sep = '/';
static readonly delimiter = ':';
static readonly sep = "/";
static readonly delimiter = ":";
static resolve(...paths: string[]) {
var resolvedPath = '',
var resolvedPath = "",
resolvedAbsolute = false;
for (var i = paths.length - 1; i >= -1 && !resolvedAbsolute; i--) {
var path = (i >= 0) ? paths[i] : "/";
var path = i >= 0 ? paths[i] : "/";
// Skip empty and invalid entries
if (typeof path !== "string") {
throw new TypeError('Arguments to path.resolve must be strings');
throw new TypeError("Arguments to path.resolve must be strings");
} else if (!path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path[0] === '/';
resolvedPath = path + "/" + resolvedPath;
resolvedAbsolute = path[0] === "/";
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(resolvedPath.split('/'),
!resolvedAbsolute).join('/');
resolvedPath = normalizeArray(resolvedPath.split("/"), !resolvedAbsolute).join("/");
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
return (resolvedAbsolute ? "/" : "") + resolvedPath || ".";
}
static normalize(path: string) {
var isAbsolute = Posix.isAbsolute(path),
trailingSlash = path && path[path.length - 1] === '/';
trailingSlash = path && path[path.length - 1] === "/";
// Normalize the path
path = normalizeArray(path.split('/'), !isAbsolute).join('/');
path = normalizeArray(path.split("/"), !isAbsolute).join("/");
if (!path && !isAbsolute) {
path = '.';
path = ".";
}
if (path && trailingSlash) {
path += '/';
path += "/";
}
return (isAbsolute ? '/' : '') + path;
return (isAbsolute ? "/" : "") + path;
}
static join(...paths: string[]) {
var path = '';
var path = "";
for (var i = 0; i < arguments.length; i++) {
var segment = arguments[i];
if (typeof segment !== "string") {
throw new TypeError('Arguments to path.join must be strings');
throw new TypeError("Arguments to path.join must be strings");
}
if (segment) {
if (!path) {
path += segment;
} else {
path += '/' + segment;
path += "/" + segment;
}
}
}
@ -163,8 +144,8 @@ export class Posix {
from = Posix.resolve(from).substr(1);
to = Posix.resolve(to).substr(1);
var fromParts = trimArray(from.split('/'));
var toParts = trimArray(to.split('/'));
var fromParts = trimArray(from.split("/"));
var toParts = trimArray(to.split("/"));
var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
@ -177,22 +158,22 @@ export class Posix {
var outputParts = [];
for (var i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
outputParts.push("..");
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
return outputParts.join("/");
}
static dirname(path: string) {
var result = posixSplitPath(path),
var result = posixSplitPath(path) as string[],
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
return ".";
}
if (dir) {
@ -218,52 +199,47 @@ export class Posix {
static format(pathObject: any) {
if (typeof pathObject !== "object") {
throw new TypeError(
"Parameter 'pathObject' must be an object, not " + typeof pathObject
);
throw new TypeError("Parameter 'pathObject' must be an object, not " + typeof pathObject);
}
var root = pathObject.root || '';
var root = pathObject.root || "";
if (typeof root !== "string") {
throw new TypeError(
"'pathObject.root' must be a string or undefined, not " +
typeof pathObject.root
"'pathObject.root' must be a string or undefined, not " + typeof pathObject.root
);
}
var dir = pathObject.dir ? pathObject.dir + Posix.sep : '';
var base = pathObject.base || '';
var dir = pathObject.dir ? pathObject.dir + Posix.sep : "";
var base = pathObject.base || "";
return dir + base;
}
static parse(pathString: string) {
if (typeof pathString !== "string") {
throw new TypeError(
"Parameter 'pathString' must be a string, not " + typeof pathString
);
throw new TypeError("Parameter 'pathString' must be a string, not " + typeof pathString);
}
var allParts = posixSplitPath(pathString);
if (!allParts || allParts.length !== 4) {
throw new TypeError("Invalid path '" + pathString + "'");
}
allParts[1] = allParts[1] || '';
allParts[2] = allParts[2] || '';
allParts[3] = allParts[3] || '';
allParts[1] = allParts[1] || "";
allParts[2] = allParts[2] || "";
allParts[3] = allParts[3] || "";
return {
root: allParts[0],
dir: allParts[0] + allParts[1].slice(0, -1),
base: allParts[2],
ext: allParts[3],
name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
name: allParts[2].slice(0, allParts[2].length - allParts[3].length),
};
}
static isAbsolute(path: string) {
return path.charAt(0) === '/';
return path.charAt(0) === "/";
}
};
}
export const Path = Posix;
export default Path;
export default Path;

View File

@ -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();
}
}

27
src/test.ts Normal file
View File

@ -0,0 +1,27 @@
import Repository from "./repo";
import FSDataStore from "./datasources/fs";
import * as Fs from "fs";
const t = (t: string) => new TextEncoder().encode(t);
async function test() {
await Fs.promises.rm("./testrepo", { recursive: true, force: true });
const ds = new FSDataStore("./testrepo");
const rep = new Repository(ds);
console.log(new TextDecoder().decode(await rep.read("hi.txt")));
await Promise.all([
rep.write("hi.txt", t("Hallo Welt: " + new Date().toLocaleString())),
rep.write("hi_hallo.txt", t("Hallo Welt: " + new Date().toLocaleString())),
rep.write("hi_hallo3.txt", t("Hallo Welt: " + new Date().toLocaleString())),
]);
console.log(new TextDecoder().decode(await rep.read("hi.txt")));
console.log((await rep.readdir("/"))?.map((e) => e[2]));
await rep.close();
}
test();