aboutsummaryrefslogtreecommitdiff
path: root/src/js/arching-kaos-file-system.js
blob: 4ceb5072c6a109bfdfed8ffd15a36a5c92c62812 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Arching Kaos File System
//
// Kaotisk Hund - 2024
//
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
//

import { akfsGetChunkURL, akfsGetLeafURL, akfsGetMapURL } from "./url-generators.js";
import { archingKaosFetchBlob } from "./arching-kaos-fetch.js";
import { offerDownloadableData } from "./utils.js";

var thingy = {};
var leafs_counter = 0;
var leafs_head_counter = 0;
var first_chunk_spotted = 0;
var first_chunk_size = 0;
var final_array_seq = [];
var status = [];
var output = "";

function akfsReset()
{
    thingy = null;
    thingy = {};
    leafs_counter = 0;
    leafs_head_counter = 0;
    first_chunk_spotted = 0;
    first_chunk_size = 0;
    final_array_seq = null;
    final_array_seq = [];
    status = [];
    output = "";
}

export function akfsGetMap(hash)
{
    akfsReset();
    archingKaosFetchBlob(akfsGetMapURL(hash), akfsFromMapGetLevelOneMapHash, [hash])
    return hash;
}

function akfsFromMapGetOriginalHash(reply, params)
{
    const [ hash ] = params;
    if(typeof(reply) === "string")
    {
        console.log(reply.split('\n')[0].split('  ')[0]);
    }
}

function akfsFromMapGetOriginalFilename(reply, params)
{
    const [ hash ] = params;
    if(typeof(reply) === "string")
    {
        console.log(reply.split('\n')[0].split('  ')[1]);
    }
}

function akfsFromMapGetLevelOneMapHash(reply, params)
{
    const [ hash ] = params;
    var hashregexp = /^[0-9a-f]{128}$/;
    const level_1_map = reply.split('\n')[1].split('  ')[0];
    if(typeof(reply) === "string" && hashregexp.test(level_1_map))
    {
        archingKaosFetchBlob(akfsGetLeafURL(level_1_map), akfsChunkOrLeaf, [level_1_map, hash]);
        thingy = {
            leafs: [],
            chunks: [],
            root_hash: level_1_map,
            map_hash: hash
        };
        thingy[`${level_1_map}`] = [];
        status[`${level_1_map}`] = 'working';
    }
}

function akfsSerializeChunks(hash)
{
    if ( thingy.chunks[hash] !== undefined )
    {
        final_array_seq.push(hash);
    }
    else if ( thingy.leafs[hash] !== undefined )
    {
        if ( thingy.leafs[hash].head !== undefined )
        {
            akfsSerializeChunks(thingy.leafs[hash].head);
        }
        if ( thingy.leafs[hash].head !== thingy.leafs[hash].tail )
        {
            akfsSerializeChunks(thingy.leafs[hash].tail);
        }
    }
    else
    {
        console.log(`The following hash is failing: ${hash}`)
    }
}

function makeUpData()
{
    let response = "";
    for ( var i = 0; i < final_array_seq.length; i++ )
    {
        // console.log(`${i} ${final_array_seq[i]}`);
        response += thingy.chunks[final_array_seq[i]].data;
    }
    return Uint8Array.fromBase64(response.replaceAll('\n', ''));
}

function akfsWorkOnChunks()
{
    akfsSerializeChunks(thingy.root_hash);
    var data = makeUpData();
    offerDownloadableData(data);
}

function akfsChunkOrLeaf(reply, params)
{
    const [ hash, previous ] = params;
    var hashregexp = /^[0-9a-f]{128}$/;
    var base64regexp = /^[-A-Za-z0-9+/]*={0,3}$/;
    if(typeof(reply) === "string")
    {
        if ( hashregexp.test(reply.split('\n')[0]) && hashregexp.test(reply.split('\n')[1]) && ( thingy.leafs[hash] === undefined ) )
        {
            if ( leafs_counter === 0 )
            {
                leafs_counter++;
            }
            leafs_head_counter++;
            if ( previous === thingy.map_hash )
            {
                console.log("Previous is a map_hash");
            }
            else if ( previous === thingy.root_hash )
            {
                console.log("Previous is a root_hash");
            }
            if ( hash === thingy.map_hash )
            {
                console.log("Current is a map_hash");
            }
            else if ( hash === thingy.root_hash )
            {
                console.log("Current is a root_hash");
            }
            // if ( thingy[hash] !== undefined )
            // {
            //     thingy[hash][reply.split('\n')[0]] = [];
            //     thingy[hash][reply.split('\n')[1]] = [];
            // }
            var leaf = {
                hash: hash,
                head: reply.split('\n')[0],
                tail: reply.split('\n')[1]
            };
            thingy.leafs[leaf.hash] = leaf;
            archingKaosFetchBlob(akfsGetLeafURL(leaf.head), akfsChunkOrLeaf, [leaf.head, hash]);
            archingKaosFetchBlob(akfsGetChunkURL(leaf.head), akfsChunkOrLeaf, [leaf.head, hash]);
            if ( leaf.head !== leaf.tail )
            {
                archingKaosFetchBlob(akfsGetLeafURL(leaf.tail), akfsChunkOrLeaf, [leaf.tail, hash]);
                archingKaosFetchBlob(akfsGetChunkURL(leaf.tail), akfsChunkOrLeaf, [leaf.tail, hash]);
            }
        }
        else if ( base64regexp.test(reply.replaceAll('\n', '')) && ( thingy.chunks[hash] === undefined ) )
        {
            if ( first_chunk_spotted === 0 )
            {
                first_chunk_spotted = leafs_head_counter;
                // console.log(`first_chunk_spotted : ${first_chunk_spotted}`);
                first_chunk_size = reply.length;
            }
            var chunk = {
                hash: hash,
                data: reply
            };
            thingy.chunks[chunk.hash] = chunk;
            if ( first_chunk_size > chunk.data.length || chunk.data.length < 1024 || ( chunk.data.length < 4096 && first_chunk_size >= 4096 ) )
            {
                akfsWorkOnChunks();
            }
        }
        else
        {
            console.log("Unreachable");
        }
    }
    else if (JSON.parse(reply))
    {
        const obj = JSON.parse(reply);
        if ( obj.error !== undefined ) console.log( obj.error );
    }
    else if (typeof(reply) === "object")
    {
        console.log("Unreachable");
    }
    else 
    {
        console.log("Unreachable");
    }
}

function akfsStoreChunk(chunk, params)
{
    const [ hash, previous ] = params;
    var base64regexp = /^[-A-Za-z0-9+/]*={0,3}$/
    if ( base64regexp.test(chunk) )
    {
        thingy.chunks[hash] = {
            data: chunk
        };
    }
}

function akfsGetChunk(chunk, params)
{
    const [ hash, previous ] = params;
    akfsStoreChunk(chunk, [hash, previous]);
}

function akfsGetLeaf(leaf, params)
{
    const [hash, previous] = params;
    if(typeof(leaf) === "object")
    {
        archingKaosFetchBlob(akfsGetLeafURL(leaf.head), akfsChunkOrLeaf, [hash, previous]);
        archingKaosFetchBlob(akfsGetChunkURL(leaf.head), akfsChunkOrLeaf, [hash, previous]);
        if ( leaf.head !== leaf.tail )
        {
            archingKaosFetchBlob(akfsGetLeafURL(leaf.tail), akfsChunkOrLeaf, [hash, previous]);
            archingKaosFetchBlob(akfsGetChunkURL(leaf.tail), akfsChunkOrLeaf, [hash, previous]);
        }
    }
}

// @license-end