FileSystem pulls the file and pulls from localStorage cache to checksum both versions
This commit is contained in:
@@ -6,15 +6,39 @@ export class FileSystem {
|
||||
private _fs: Map<number, TreeNode>;
|
||||
private _root: number;
|
||||
|
||||
private constructor() {
|
||||
private constructor(fs: Map<number, TreeNode>) {
|
||||
this._fs = fs;
|
||||
this._root = this._fs.values().next().value!.inode;
|
||||
|
||||
if (!this._root) throw new Error('First entry in the filesystem map empty!');
|
||||
}
|
||||
|
||||
public static async Init() {
|
||||
const fsJson: JSON = await this._fetchFileSystem('/temp/path');
|
||||
const checksum: any = MD5.hash(JSON.stringify(fsJson));
|
||||
let fsJson: JSON = await this._fetchFileSystem('/src/lib/assets/fs/fs.json');
|
||||
const fsChecksum = MD5.hash(JSON.stringify(fsJson));
|
||||
|
||||
const cache: string | null = localStorage.getItem('fs-json');
|
||||
if (cache) {
|
||||
const fsCacheChecksum = MD5.hash(cache);
|
||||
|
||||
console.log(fsChecksum);
|
||||
console.log(fsCacheChecksum);
|
||||
|
||||
if (fsChecksum !== fsCacheChecksum) {
|
||||
//calculate version control difference to preserve user made changes (do a merge)
|
||||
}
|
||||
fsJson = JSON.parse(cache);
|
||||
}
|
||||
|
||||
localStorage.setItem('fs-json', JSON.stringify(fsJson));
|
||||
const fs: Map<number, TreeNode> = this._jsonToNodeTable(fsJson);
|
||||
|
||||
// This produces a different checksum!
|
||||
|
||||
// Find a way to produce the same hash from Map and a fetched json file or cached stringified json
|
||||
console.log(MD5.hash(JSON.stringify(Array.from(fs))));
|
||||
|
||||
FileSystem.#instance = new FileSystem(fs);
|
||||
}
|
||||
|
||||
private static async _fetchFileSystem(path: string): Promise<JSON> {
|
||||
@@ -38,10 +62,12 @@ export class FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchFsJson(sig: string): Promise<JSON> {
|
||||
const signature: string | null = localStorage.getItem('signature');
|
||||
private _fsToJsonString(): string {}
|
||||
|
||||
if (signature !== sig || signature === null || localStorage.getItem('fs') === null) {
|
||||
private static async fetchFsJson(): Promise<JSON> {
|
||||
const checksum: string | null = localStorage.getItem('fs-checksum');
|
||||
|
||||
if (checksum !== sig || signature === null || localStorage.getItem('fs') === null) {
|
||||
const fs: JSON = await this._fetchFileSystem('/src/lib/assets/fs/fs.json');
|
||||
localStorage.setItem('signature', sig);
|
||||
localStorage.setItem('fs', JSON.stringify(fs));
|
||||
@@ -59,7 +85,7 @@ export class FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
private _jsonToNodeTable(data: JSON): Map<number, TreeNode> {
|
||||
private static _jsonToNodeTable(data: JSON): Map<number, TreeNode> {
|
||||
const FsTable: Map<number, TreeNode> = new Map<number, TreeNode>();
|
||||
const entryList = Object.entries(data);
|
||||
|
||||
@@ -288,7 +314,7 @@ export class FileSystem {
|
||||
|
||||
public static get instance(): FileSystem {
|
||||
if (!FileSystem.#instance) {
|
||||
FileSystem.#instance = new FileSystem();
|
||||
throw new Error(`FileSystem was not initialized properly!`);
|
||||
}
|
||||
|
||||
return FileSystem.instance;
|
||||
|
||||
+129
-6
@@ -1,11 +1,110 @@
|
||||
export class MD5 {
|
||||
private static encoder: TextEncoder = new TextEncoder();
|
||||
|
||||
// prettier-ignore
|
||||
private static K: number[] = [
|
||||
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
|
||||
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
|
||||
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
|
||||
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
|
||||
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
|
||||
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
|
||||
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
|
||||
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
|
||||
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
|
||||
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
|
||||
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
|
||||
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
|
||||
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
|
||||
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
|
||||
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
|
||||
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
|
||||
];
|
||||
|
||||
// prettier-ignore
|
||||
private static S: number[] = [
|
||||
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
||||
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
||||
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
||||
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
|
||||
];
|
||||
|
||||
public static hash(input: string) {
|
||||
const uIntArr: Uint8Array<ArrayBuffer> = this.encoder.encode(input);
|
||||
const inputArr: Uint8Array<ArrayBuffer> = this.encoder.encode(input);
|
||||
const padded = MD5.pad(inputArr);
|
||||
|
||||
let A = 0x67452301;
|
||||
let B = 0xefcdab89;
|
||||
let C = 0x98badcfe;
|
||||
let D = 0x10325476;
|
||||
|
||||
function processBlock(M: number[]) {
|
||||
function F(b: number, c: number, d: number): number {
|
||||
return (b & c) | (~b & d);
|
||||
}
|
||||
function H(b: number, c: number, d: number): number {
|
||||
return b ^ c ^ d;
|
||||
}
|
||||
function I(b: number, c: number, d: number): number {
|
||||
return c ^ (b | ~d);
|
||||
}
|
||||
function G(b: number, c: number, d: number): number {
|
||||
return (b & d) | (c & ~d);
|
||||
}
|
||||
|
||||
let AA = A,
|
||||
BB = B,
|
||||
CC = C,
|
||||
DD = D;
|
||||
|
||||
for (let i = 0; i < 64; i++) {
|
||||
let f: number, g: number;
|
||||
|
||||
if (i < 16) {
|
||||
f = F(B, C, D);
|
||||
g = i;
|
||||
} else if (i < 32) {
|
||||
f = G(B, C, D);
|
||||
g = (5 * i + 1) % 16;
|
||||
} else if (i < 48) {
|
||||
f = H(B, C, D);
|
||||
g = (3 * i + 5) % 16;
|
||||
} else {
|
||||
f = I(B, C, D);
|
||||
g = (7 * i) % 16;
|
||||
}
|
||||
|
||||
const sum = MD5.add32(A, MD5.add32(f, MD5.add32(M[g], MD5.K[i])));
|
||||
const temp = MD5.add32(B, MD5.leftRotate(sum, MD5.S[i]));
|
||||
|
||||
A = D;
|
||||
D = C;
|
||||
C = B;
|
||||
B = temp;
|
||||
}
|
||||
|
||||
A = MD5.add32(A, AA);
|
||||
B = MD5.add32(B, BB);
|
||||
C = MD5.add32(C, CC);
|
||||
D = MD5.add32(D, DD);
|
||||
}
|
||||
|
||||
for (let i = 0; i < padded.length; i += 64) {
|
||||
const M = MD5.sliceToWords(padded.slice(i, i + 64));
|
||||
processBlock(M); // modifies A, B, C, D directly
|
||||
}
|
||||
|
||||
const hashBytes = new Uint8Array([
|
||||
...MD5.wordToBytesLE(A),
|
||||
...MD5.wordToBytesLE(B),
|
||||
...MD5.wordToBytesLE(C),
|
||||
...MD5.wordToBytesLE(D)
|
||||
]);
|
||||
|
||||
return MD5.toHexString(hashBytes);
|
||||
}
|
||||
|
||||
private pad(input: Uint8Array) {
|
||||
private static pad(input: Uint8Array): Uint8Array {
|
||||
const inputBitLength: number = input.length * 8;
|
||||
|
||||
const out: Uint8Array = new Uint8Array([...input, 0x80]);
|
||||
@@ -27,21 +126,21 @@ export class MD5 {
|
||||
(high >> 24) & 0xff
|
||||
]);
|
||||
|
||||
return new Int8Array([...out, ...zeroArray, ...lengthBytes]);
|
||||
return new Uint8Array([...out, ...zeroArray, ...lengthBytes]);
|
||||
}
|
||||
|
||||
private sliceToBlocks(input: Uint8Array): number[][] {
|
||||
private static sliceToBlocks(input: Uint8Array): number[][] {
|
||||
const blocks: number[][] = [];
|
||||
|
||||
for (let i = 0; i < input.length; i += 64) {
|
||||
const block = input.slice(i, i + 64);
|
||||
blocks.push(this.sliceToWords(block));
|
||||
blocks.push(MD5.sliceToWords(block));
|
||||
}
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private sliceToWords(block: Uint8Array): number[] {
|
||||
private static sliceToWords(block: Uint8Array): number[] {
|
||||
const words: number[] = new Array<number>(16);
|
||||
|
||||
for (let i = 0; i < 16; i++) {
|
||||
@@ -57,4 +156,28 @@ export class MD5 {
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
private static leftRotate(x: number, n: number): number {
|
||||
x = x >>> 0;
|
||||
return ((x << n) | (x >>> (32 - n))) >>> 0;
|
||||
}
|
||||
|
||||
private static add32(a: number, b: number): number {
|
||||
return ((a >>> 0) + (b >>> 0)) >>> 0;
|
||||
}
|
||||
|
||||
private static wordToBytesLE(word: number): Uint8Array {
|
||||
return new Uint8Array([
|
||||
word & 0xff,
|
||||
(word >>> 8) & 0xff,
|
||||
(word >>> 16) & 0xff,
|
||||
(word >>> 24) & 0xff
|
||||
]);
|
||||
}
|
||||
|
||||
private static toHexString(bytes: Uint8Array): string {
|
||||
return Array.from(bytes)
|
||||
.map((b) => b.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<script lang="ts">
|
||||
import '../app.css';
|
||||
import favicon from '$lib/assets/favicon.svg';
|
||||
import { onMount, type Snippet } from 'svelte';
|
||||
import { theme } from '$lib/stores/theme';
|
||||
import Loading from '../modules/Loading.svelte';
|
||||
import Shimeji from '../modules/Shimeji.svelte';
|
||||
import { onMount, type Snippet } from 'svelte';
|
||||
import '../app.css';
|
||||
//import Loading from '../modules/Loading.svelte';
|
||||
//import Shimeji from '../modules/Shimeji.svelte';
|
||||
|
||||
let { children }: { children: Snippet } = $props();
|
||||
|
||||
@@ -44,7 +44,6 @@
|
||||
class:dark={$theme === 'dark'}
|
||||
class:light={$theme === 'light'}
|
||||
>
|
||||
<Shimeji />
|
||||
{@render children()}
|
||||
</main>
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<script lang="ts">
|
||||
import { FileSystem } from '$lib/termos/filesystem/filesystem';
|
||||
import { MD5 } from '$lib/termos/util/hash';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
console.log(MD5.hash(''));
|
||||
console.log(MD5.hash('a'));
|
||||
console.log(MD5.hash('abc'));
|
||||
|
||||
onMount(() => {
|
||||
FileSystem.Init();
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user