-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollection.js
206 lines (183 loc) · 4.64 KB
/
collection.js
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
module.exports = {
isSwarmHash: isSwarmHash,
Collection: Collection,
CollectionIndex: CollectionIndex,
SimpleManifestAdapter: SimpleManifestAdapter,
};
// brazenly stolen from git:felfele/bee-client
function mergeUint8Arrays(arrays) {
const size = arrays.reduce((prev, curr) => prev + curr.length, 0)
const r = new Uint8Array(size)
let offset = 0
for (const arr of arrays) {
r.set(arr, offset)
offset += arr.length
}
return r
}
function isSwarmHash(hash) {
if (hash === undefined || hash.length === undefined || hash.length != 32) {
return false;
}
return true;
}
function Collection() {
this.hashes = [];
}
Collection.prototype.add = function(hash) {
let new_index = this.hashes.length;
if (!isSwarmHash(hash)) {
return false;
}
this.hashes.push(hash);
return new_index;
};
Collection.prototype.serialize = function() {
return mergeUint8Arrays(this.hashes);
};
Collection.prototype.deserialize = function(b) {
for (let i = 0; i < b.length; i += 32) {
console.debug(i);
if ((b.length - i) % 32 != 0) {
this.hashes = [];
return false;
}
let subArrayCopy = Uint8Array.from(b.slice(i, i+32));
this.hashes.push(subArrayCopy);
}
return true;
};
Collection.prototype.each = function(f, mode) {
for (let i = 0; i < this.hashes.length; i++) {
f(this.hashes[i]);
}
};
function isFilename(name) {
return typeof(name) === 'string';
}
function isMimetype(typ) {
return typeof(typ) === 'string' && typ.indexOf("/") > 0;
}
function validMetaData(key) {
if (key === undefined || key.filename === undefined || key.mimetype === undefined) {
return false;
}
if (!isFilename(key.filename) || !isMimetype(key.mimetype)) {
return false;
}
return true;
}
function CollectionIndex(name) {
this.name = name;
this.keys = {};
this.intermediate = [];
this.values = [];
}
// the key may be anything but needs to have toString?
CollectionIndex.prototype.add = function(key, value) {
if (key === undefined || !isFilename(key.filename) || !isMimetype(key.mimetype)) {
return false;
}
this.values.push(value);
this.intermediate.push(key.mimetype);
this.keys[key.filename] = this.values.length-1;
return true;
};
CollectionIndex.prototype.serialize = function() {
let filenames = [];
let i = 0;
for (const k in this.keys) {
filenames.push(k + ':' + i);
i++;
}
let entries = {};
filenames.sort()
for (let i = 0; i < filenames.length; i++) {
let s = filenames[i];
parts = s.split(':');
let idx = parseInt(parts[1], 10);
//console.debug('serialize parts', parts, idx);
entries[parts[0]] = [this.intermediate[idx], this.values[idx]];
}
//console.debug('serialize entries', entries);
let enc = new TextEncoder();
return enc.encode(JSON.stringify(entries));
};
CollectionIndex.prototype.deserialize = function(b) {
let dec = new TextDecoder();
let entries = JSON.parse(dec.decode(b));
console.debug('entries', entries)
let i = 0;
for (const k in entries) {
this.keys[k] = i;
this.intermediate.push(entries[k][0]);
this.values.push(entries[k][1]);
i++;
}
return true;
};
function SimpleManifestAdapter(collection, index) {
this.collection = collection;
this.index = index;
}
SimpleManifestAdapter.prototype.getReference = function(k) {
let index_position = this.index.keys[k];
let collection_position = this.index.values[index_position];
return this.collection.hashes[collection_position];
};
SimpleManifestAdapter.prototype.getMimetype = function(k) {
let index_position = this.index.keys[k];
return this.index.intermediate[index_position];
};
SimpleManifestAdapter.prototype.list = function(path) {
let entries = [];
let keys = [];
let fullkeys = [];
let dirs = [];
// surely there is a lean library that does this
let clean_path = path
if (clean_path[0] == '/') {
clean_path = clean_path.substring(1);
}
if (clean_path.lastIndexOf('/') != clean_path.length-1) {
clean_path += '/';
}
let i = 0;
for (const k in this.index.keys) {
if (k.startsWith(clean_path)) {
let sub_path = k.substring(clean_path.length);
let slash_index = sub_path.indexOf('/');
let sub_dir_path = sub_path.substring(0, slash_index);
if (keys.includes(sub_dir_path)) {
continue;
}
if (slash_index != -1) {
keys.push(sub_dir_path);
dirs.push(true);
} else {
keys.push(sub_path);
dirs.push(false);
}
fullkeys.push(k);
entries.push(i);
}
i++;
}
let data = [];
for (const e in entries) {
let k = keys[e];
let mimetype = undefined;
let hash = undefined;
if (!dirs[e]) {
mimetype = this.index.intermediate[e];
hash = this.getReference(fullkeys[e]);
}
let item = {
filename: k,
mimetype: mimetype,
hash: hash,
}
data.push(item);
}
return data;
};