First Commit
This commit is contained in:
commit
e21a9fad3f
2
.editorconfig
Normal file
2
.editorconfig
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[*]
|
||||||
|
indent_size = 3
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
19
package.json
Normal file
19
package.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "simplevsc",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"devDependencies": {
|
||||||
|
"ts-node": "^10.2.0",
|
||||||
|
"typescript": "^4.3.5"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"jssha": "^3.2.0"
|
||||||
|
}
|
||||||
|
}
|
116
pnpm-lock.yaml
Normal file
116
pnpm-lock.yaml
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
lockfileVersion: 5.3
|
||||||
|
|
||||||
|
specifiers:
|
||||||
|
jssha: ^3.2.0
|
||||||
|
ts-node: ^10.2.0
|
||||||
|
typescript: ^4.3.5
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
jssha: 3.2.0
|
||||||
|
|
||||||
|
devDependencies:
|
||||||
|
ts-node: 10.2.0_typescript@4.3.5
|
||||||
|
typescript: 4.3.5
|
||||||
|
|
||||||
|
packages:
|
||||||
|
|
||||||
|
/@cspotcode/source-map-consumer/0.8.0:
|
||||||
|
resolution: {integrity: sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==}
|
||||||
|
engines: {node: '>= 12'}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/@cspotcode/source-map-support/0.6.1:
|
||||||
|
resolution: {integrity: sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==}
|
||||||
|
engines: {node: '>=12'}
|
||||||
|
dependencies:
|
||||||
|
'@cspotcode/source-map-consumer': 0.8.0
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/@tsconfig/node10/1.0.8:
|
||||||
|
resolution: {integrity: sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/@tsconfig/node12/1.0.9:
|
||||||
|
resolution: {integrity: sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/@tsconfig/node14/1.0.1:
|
||||||
|
resolution: {integrity: sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/@tsconfig/node16/1.0.2:
|
||||||
|
resolution: {integrity: sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/acorn-walk/8.1.1:
|
||||||
|
resolution: {integrity: sha512-FbJdceMlPHEAWJOILDk1fXD8lnTlEIWFkqtfk+MvmL5q/qlHfN7GEHcsFZWt/Tea9jRNPWUZG4G976nqAAmU9w==}
|
||||||
|
engines: {node: '>=0.4.0'}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/acorn/8.4.1:
|
||||||
|
resolution: {integrity: sha512-asabaBSkEKosYKMITunzX177CXxQ4Q8BSSzMTKD+FefUhipQC70gfW5SiUDhYQ3vk8G+81HqQk7Fv9OXwwn9KA==}
|
||||||
|
engines: {node: '>=0.4.0'}
|
||||||
|
hasBin: true
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/arg/4.1.3:
|
||||||
|
resolution: {integrity: sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/create-require/1.1.1:
|
||||||
|
resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/diff/4.0.2:
|
||||||
|
resolution: {integrity: sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==}
|
||||||
|
engines: {node: '>=0.3.1'}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/jssha/3.2.0:
|
||||||
|
resolution: {integrity: sha512-QuruyBENDWdN4tZwJbQq7/eAK85FqrI4oDbXjy5IBhYD+2pTJyBUWZe8ctWaCkrV0gy6AaelgOZZBMeswEa/6Q==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
|
/make-error/1.3.6:
|
||||||
|
resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/ts-node/10.2.0_typescript@4.3.5:
|
||||||
|
resolution: {integrity: sha512-FstYHtQz6isj8rBtYMN4bZdnXN1vq4HCbqn9vdNQcInRqtB86PePJQIxE6es0PhxKWhj2PHuwbG40H+bxkZPmg==}
|
||||||
|
engines: {node: '>=12.0.0'}
|
||||||
|
hasBin: true
|
||||||
|
peerDependencies:
|
||||||
|
'@swc/core': '>=1.2.50'
|
||||||
|
'@swc/wasm': '>=1.2.50'
|
||||||
|
'@types/node': '*'
|
||||||
|
typescript: '>=2.7'
|
||||||
|
peerDependenciesMeta:
|
||||||
|
'@swc/core':
|
||||||
|
optional: true
|
||||||
|
'@swc/wasm':
|
||||||
|
optional: true
|
||||||
|
dependencies:
|
||||||
|
'@cspotcode/source-map-support': 0.6.1
|
||||||
|
'@tsconfig/node10': 1.0.8
|
||||||
|
'@tsconfig/node12': 1.0.9
|
||||||
|
'@tsconfig/node14': 1.0.1
|
||||||
|
'@tsconfig/node16': 1.0.2
|
||||||
|
acorn: 8.4.1
|
||||||
|
acorn-walk: 8.1.1
|
||||||
|
arg: 4.1.3
|
||||||
|
create-require: 1.1.1
|
||||||
|
diff: 4.0.2
|
||||||
|
make-error: 1.3.6
|
||||||
|
typescript: 4.3.5
|
||||||
|
yn: 3.1.1
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/typescript/4.3.5:
|
||||||
|
resolution: {integrity: sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==}
|
||||||
|
engines: {node: '>=4.2.0'}
|
||||||
|
hasBin: true
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/yn/3.1.1:
|
||||||
|
resolution: {integrity: sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==}
|
||||||
|
engines: {node: '>=6'}
|
||||||
|
dev: true
|
0
src/datasources/fs.ts
Normal file
0
src/datasources/fs.ts
Normal file
14
src/helper/hex.ts
Normal file
14
src/helper/hex.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// Pre-Init
|
||||||
|
const LUT_HEX_4b = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
||||||
|
const LUT_HEX_8b = new Array(0x100);
|
||||||
|
for (let n = 0; n < 0x100; n++) {
|
||||||
|
LUT_HEX_8b[n] = `${LUT_HEX_4b[(n >>> 4) & 0xF]}${LUT_HEX_4b[n & 0xF]}`;
|
||||||
|
}
|
||||||
|
// End Pre-Init
|
||||||
|
export function toHex(buffer: Uint8Array): string {
|
||||||
|
let out = '';
|
||||||
|
for (let idx = 0, edx = buffer.length; idx < edx; idx++) {
|
||||||
|
out += LUT_HEX_8b[buffer[idx]];
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
269
src/helper/path.ts
Normal file
269
src/helper/path.ts
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
// Copyright Joyent, Inc. and other Node contributors.
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
// copy of this software and associated documentation files (the
|
||||||
|
// "Software"), to deal in the Software without restriction, including
|
||||||
|
// without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
// persons to whom the Software is furnished to do so, subject to the
|
||||||
|
// following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included
|
||||||
|
// in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
'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) {
|
||||||
|
var res = [];
|
||||||
|
for (var i = 0; i < parts.length; i++) {
|
||||||
|
var p = parts[i];
|
||||||
|
|
||||||
|
// ignore empty parts
|
||||||
|
if (!p || p === '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (p === '..') {
|
||||||
|
if (res.length && res[res.length - 1] !== '..') {
|
||||||
|
res.pop();
|
||||||
|
} else if (allowAboveRoot) {
|
||||||
|
res.push('..');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.push(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
var lastIndex = arr.length - 1;
|
||||||
|
var start = 0;
|
||||||
|
for (; start <= lastIndex; start++) {
|
||||||
|
if (arr[start])
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var end = lastIndex;
|
||||||
|
for (; end >= 0; end--) {
|
||||||
|
if (arr[end])
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
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}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
|
||||||
|
|
||||||
|
|
||||||
|
function posixSplitPath(filename) {
|
||||||
|
return splitPathRe.exec(filename).slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Posix {
|
||||||
|
static readonly sep = '/';
|
||||||
|
static readonly delimiter = ':';
|
||||||
|
|
||||||
|
static resolve(...paths: string[]) {
|
||||||
|
var resolvedPath = '',
|
||||||
|
resolvedAbsolute = false;
|
||||||
|
|
||||||
|
for (var i = paths.length - 1; i >= -1 && !resolvedAbsolute; 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');
|
||||||
|
} else if (!path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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('/');
|
||||||
|
|
||||||
|
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
static normalize(path: string) {
|
||||||
|
var isAbsolute = Posix.isAbsolute(path),
|
||||||
|
trailingSlash = path && path[path.length - 1] === '/';
|
||||||
|
|
||||||
|
// Normalize the path
|
||||||
|
path = normalizeArray(path.split('/'), !isAbsolute).join('/');
|
||||||
|
|
||||||
|
if (!path && !isAbsolute) {
|
||||||
|
path = '.';
|
||||||
|
}
|
||||||
|
if (path && trailingSlash) {
|
||||||
|
path += '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
return (isAbsolute ? '/' : '') + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static join(...paths: string[]) {
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
if (segment) {
|
||||||
|
if (!path) {
|
||||||
|
path += segment;
|
||||||
|
} else {
|
||||||
|
path += '/' + segment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Posix.normalize(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
static relative(from: string, to: string) {
|
||||||
|
from = Posix.resolve(from).substr(1);
|
||||||
|
to = Posix.resolve(to).substr(1);
|
||||||
|
|
||||||
|
var fromParts = trimArray(from.split('/'));
|
||||||
|
var toParts = trimArray(to.split('/'));
|
||||||
|
|
||||||
|
var length = Math.min(fromParts.length, toParts.length);
|
||||||
|
var samePartsLength = length;
|
||||||
|
for (var i = 0; i < length; i++) {
|
||||||
|
if (fromParts[i] !== toParts[i]) {
|
||||||
|
samePartsLength = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputParts = [];
|
||||||
|
for (var i = samePartsLength; i < fromParts.length; i++) {
|
||||||
|
outputParts.push('..');
|
||||||
|
}
|
||||||
|
|
||||||
|
outputParts = outputParts.concat(toParts.slice(samePartsLength));
|
||||||
|
|
||||||
|
return outputParts.join('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
static dirname(path: string) {
|
||||||
|
var result = posixSplitPath(path),
|
||||||
|
root = result[0],
|
||||||
|
dir = result[1];
|
||||||
|
|
||||||
|
if (!root && !dir) {
|
||||||
|
// No dirname whatsoever
|
||||||
|
return '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dir) {
|
||||||
|
// It has a dirname, strip trailing slash
|
||||||
|
dir = dir.substr(0, dir.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return root + dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
static basename(path: string, ext?: string) {
|
||||||
|
var f = posixSplitPath(path)[2];
|
||||||
|
// TODO: make this comparison case-insensitive on windows?
|
||||||
|
if (ext && f.substr(-1 * ext.length) === ext) {
|
||||||
|
f = f.substr(0, f.length - ext.length);
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static extname(path: string) {
|
||||||
|
return posixSplitPath(path)[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
static format(pathObject: any) {
|
||||||
|
if (typeof pathObject !== "object") {
|
||||||
|
throw new TypeError(
|
||||||
|
"Parameter 'pathObject' must be an object, not " + typeof pathObject
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var root = pathObject.root || '';
|
||||||
|
|
||||||
|
if (typeof root !== "string") {
|
||||||
|
throw new TypeError(
|
||||||
|
"'pathObject.root' must be a string or undefined, not " +
|
||||||
|
typeof pathObject.root
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
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] || '';
|
||||||
|
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static isAbsolute(path: string) {
|
||||||
|
return path.charAt(0) === '/';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Path = Posix;
|
||||||
|
export default Path;
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user