@secrez/fs 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +30 -28
- package/coverage.report +164 -180
- package/package.json +41 -44
- package/src/DataCache.js +64 -66
- package/src/ExternalFs.js +64 -58
- package/src/FileCipher.js +50 -42
- package/src/FsUtils.js +47 -44
- package/src/InternalFs.js +253 -239
- package/src/Messages.js +2 -2
- package/src/Node.js +305 -308
- package/src/Tree.js +340 -318
- package/src/index.js +9 -9
- package/.eslintignore +0 -0
- package/.eslintrc +0 -33
- package/.jshintrc +0 -3
- package/pnpm-lock.yaml +0 -2307
package/src/Node.js
CHANGED
|
@@ -1,330 +1,331 @@
|
|
|
1
|
-
const util = require(
|
|
2
|
-
const {config, Entry} = require(
|
|
3
|
-
const Crypto = require(
|
|
4
|
-
const {ANCESTOR_NOT_FOUND, ENTRY_EXISTS} = require(
|
|
5
|
-
const DataCache = require(
|
|
1
|
+
const util = require("util");
|
|
2
|
+
const { config, Entry } = require("@secrez/core");
|
|
3
|
+
const Crypto = require("@secrez/crypto");
|
|
4
|
+
const { ANCESTOR_NOT_FOUND, ENTRY_EXISTS } = require("./Messages");
|
|
5
|
+
const DataCache = require("./DataCache");
|
|
6
6
|
|
|
7
|
-
let cache = new DataCache
|
|
7
|
+
let cache = new DataCache();
|
|
8
8
|
|
|
9
9
|
class Node {
|
|
10
|
-
|
|
11
10
|
static setCache(dataCache) {
|
|
12
|
-
if (typeof dataCache ===
|
|
13
|
-
cache = dataCache
|
|
11
|
+
if (typeof dataCache === "object") {
|
|
12
|
+
cache = dataCache;
|
|
14
13
|
}
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
static getCache() {
|
|
18
|
-
return cache
|
|
17
|
+
return cache;
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
constructor(entry) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
throw new Error('Node constructor expects an Entry instance')
|
|
21
|
+
if (!entry || entry.constructor.name !== "Entry") {
|
|
22
|
+
throw new Error("Node constructor expects an Entry instance");
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
let isRoot = Node.isRoot(entry)
|
|
25
|
+
let isRoot = Node.isRoot(entry);
|
|
28
26
|
|
|
29
27
|
if (!(Node.isDir(entry) || Node.isFile(entry))) {
|
|
30
|
-
throw new Error(
|
|
28
|
+
throw new Error("Unsupported type");
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
this.type = entry.type
|
|
31
|
+
this.type = entry.type;
|
|
34
32
|
if (isRoot) {
|
|
35
|
-
this.id = config.specialId.ROOT
|
|
33
|
+
this.id = config.specialId.ROOT;
|
|
36
34
|
} else {
|
|
37
35
|
if (entry.id) {
|
|
38
|
-
this.id = entry.id
|
|
36
|
+
this.id = entry.id;
|
|
39
37
|
} else {
|
|
40
|
-
this.id = Crypto.getRandomId(cache ? cache.list(
|
|
38
|
+
this.id = Crypto.getRandomId(cache ? cache.list("id") : null);
|
|
41
39
|
}
|
|
42
|
-
cache.puts(
|
|
40
|
+
cache.puts("id", this.id);
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
if (Node.isDir(entry)) {
|
|
46
|
-
this.children = {}
|
|
44
|
+
this.children = {};
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
if (isRoot) {
|
|
50
|
-
this.rnd = Crypto.getRandomId()
|
|
48
|
+
this.rnd = Crypto.getRandomId();
|
|
51
49
|
} else {
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
if (
|
|
51
|
+
!entry.ts ||
|
|
52
|
+
typeof entry.ts !== "string" ||
|
|
53
|
+
!entry.name ||
|
|
54
|
+
typeof entry.name !== "string" ||
|
|
55
|
+
!entry.encryptedName ||
|
|
56
|
+
typeof entry.encryptedName !== "string"
|
|
55
57
|
) {
|
|
56
|
-
throw new Error(
|
|
58
|
+
throw new Error("Missing parameters");
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
if (Node.isNode(entry.parent)) {
|
|
60
62
|
// a Node can be independent of a tree.
|
|
61
63
|
// But if it is part of a tree, any child must have a parent
|
|
62
|
-
cache.puts(
|
|
63
|
-
this.parent = entry.parent
|
|
64
|
+
cache.puts("id", entry.id.replace(/^_/, ""));
|
|
65
|
+
this.parent = entry.parent;
|
|
64
66
|
}
|
|
65
|
-
this.versions = {}
|
|
66
|
-
this.lastTs = entry.ts
|
|
67
|
+
this.versions = {};
|
|
68
|
+
this.lastTs = entry.ts;
|
|
67
69
|
this.versions[entry.ts] = {
|
|
68
70
|
name: entry.name,
|
|
69
|
-
file: entry.encryptedName
|
|
70
|
-
}
|
|
71
|
+
file: entry.encryptedName,
|
|
72
|
+
};
|
|
71
73
|
if (entry.content) {
|
|
72
|
-
this.versions[entry.ts].content = entry.content
|
|
74
|
+
this.versions[entry.ts].content = entry.content;
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
static isRoot(obj) {
|
|
78
|
-
return obj.type === config.types.ROOT
|
|
80
|
+
return obj.type === config.types.ROOT;
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
static isDir(node) {
|
|
82
|
-
return this.isRoot(node) || node.type === config.types.DIR
|
|
84
|
+
return this.isRoot(node) || node.type === config.types.DIR;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
static isFile(node) {
|
|
86
|
-
return this.isText(node) || this.isBinary(node)
|
|
88
|
+
return this.isText(node) || this.isBinary(node);
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
static isBinary(node) {
|
|
90
|
-
return node.type === config.types.BINARY
|
|
92
|
+
return node.type === config.types.BINARY;
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
static isText(node) {
|
|
94
|
-
return node.type === config.types.TEXT
|
|
96
|
+
return node.type === config.types.TEXT;
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
static fromJSON(json, secrez, allFiles) {
|
|
98
100
|
// It takes an already parsed object to make it an instance of the class.
|
|
99
101
|
// It needs the list of files on disk to correctly recover timestamps and names
|
|
100
|
-
let minSize
|
|
101
|
-
if (typeof json ===
|
|
102
|
-
json = JSON.parse(json)
|
|
102
|
+
let minSize;
|
|
103
|
+
if (typeof json === "string") {
|
|
104
|
+
json = JSON.parse(json);
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
for (let c of json.c) {
|
|
106
108
|
if (c.v[0]) {
|
|
107
|
-
minSize = c.v[0].length
|
|
108
|
-
break
|
|
109
|
+
minSize = c.v[0].length;
|
|
110
|
+
break;
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
|
-
let files = {}
|
|
113
|
+
let files = {};
|
|
112
114
|
for (let f of allFiles) {
|
|
113
|
-
files[f.substring(0, minSize)] = f
|
|
115
|
+
files[f.substring(0, minSize)] = f;
|
|
114
116
|
}
|
|
115
|
-
json = Node.preFormat(json, secrez, files)
|
|
116
|
-
let root = Node.initNode(json)
|
|
117
|
-
return root
|
|
117
|
+
json = Node.preFormat(json, secrez, files);
|
|
118
|
+
let root = Node.initNode(json);
|
|
119
|
+
return root;
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
static preFormat(
|
|
121
|
-
|
|
122
|
+
static preFormat(
|
|
123
|
+
json,
|
|
124
|
+
secrez,
|
|
125
|
+
files,
|
|
126
|
+
trash
|
|
127
|
+
// trash stays to convert format <0.6.0
|
|
122
128
|
) {
|
|
123
|
-
json.V = []
|
|
129
|
+
json.V = [];
|
|
124
130
|
for (let j = 0; j < json.v.length; j++) {
|
|
125
|
-
let v = json.v[j]
|
|
131
|
+
let v = json.v[j];
|
|
126
132
|
if (files[v]) {
|
|
127
|
-
let entry = secrez.decryptEntry(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
let entry = secrez.decryptEntry(
|
|
134
|
+
new Entry({
|
|
135
|
+
encryptedName: files[v],
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
let obj = entry.get(["id", "ts", "name"]);
|
|
131
139
|
if (trash) {
|
|
132
|
-
obj.id =
|
|
140
|
+
obj.id = "_" + obj.id;
|
|
133
141
|
}
|
|
134
|
-
obj.encryptedName = files[v]
|
|
135
|
-
json.V.push(obj)
|
|
136
|
-
delete files[v]
|
|
142
|
+
obj.encryptedName = files[v];
|
|
143
|
+
json.V.push(obj);
|
|
144
|
+
delete files[v];
|
|
137
145
|
} else {
|
|
138
|
-
json.v.splice(j, 1)
|
|
139
|
-
j
|
|
146
|
+
json.v.splice(j, 1);
|
|
147
|
+
j--;
|
|
140
148
|
}
|
|
141
149
|
}
|
|
142
|
-
json.V.sort(Node.sortEntry)
|
|
150
|
+
json.V.sort(Node.sortEntry);
|
|
143
151
|
if (json.c) {
|
|
144
152
|
for (let c of json.c) {
|
|
145
|
-
Node.preFormat(c, secrez, files, trash)
|
|
153
|
+
Node.preFormat(c, secrez, files, trash);
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
|
-
return json
|
|
156
|
+
return json;
|
|
149
157
|
}
|
|
150
158
|
|
|
151
159
|
static sortEntry(a, b) {
|
|
152
|
-
let ta = a
|
|
153
|
-
let tb = b
|
|
154
|
-
if (typeof a ===
|
|
155
|
-
ta = a.ts
|
|
156
|
-
tb = b.ts
|
|
157
|
-
}
|
|
158
|
-
let [A, C] = ta.split(
|
|
159
|
-
let [B, D] = tb.split(
|
|
160
|
-
return
|
|
161
|
-
A > B ? -1
|
|
162
|
-
: A < B ? 1
|
|
163
|
-
: C > D ? -1
|
|
164
|
-
: C < D ? 1
|
|
165
|
-
: 0
|
|
166
|
-
)
|
|
160
|
+
let ta = a;
|
|
161
|
+
let tb = b;
|
|
162
|
+
if (typeof a === "object") {
|
|
163
|
+
ta = a.ts;
|
|
164
|
+
tb = b.ts;
|
|
165
|
+
}
|
|
166
|
+
let [A, C] = ta.split(".").map((e) => parseInt(e));
|
|
167
|
+
let [B, D] = tb.split(".").map((e) => parseInt(e));
|
|
168
|
+
return A > B ? -1 : A < B ? 1 : C > D ? -1 : C < D ? 1 : 0;
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
static initNode(json, parent) {
|
|
170
|
-
|
|
171
172
|
try {
|
|
172
|
-
let V0 = json.V[0]
|
|
173
|
-
let type = V0
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
173
|
+
let V0 = json.V[0];
|
|
174
|
+
let type = V0
|
|
175
|
+
? V0.type || parseInt(V0.encryptedName[0])
|
|
176
|
+
: config.types.ROOT;
|
|
177
|
+
let node = new Node(
|
|
178
|
+
new Entry({
|
|
179
|
+
type,
|
|
180
|
+
id: V0 ? V0.id : undefined,
|
|
181
|
+
ts: V0 ? V0.ts : undefined,
|
|
182
|
+
name: V0 ? V0.name : undefined,
|
|
183
|
+
encryptedName: V0 ? V0.encryptedName : undefined,
|
|
184
|
+
parent,
|
|
185
|
+
})
|
|
186
|
+
);
|
|
183
187
|
for (let i = 1; i < json.V.length; i++) {
|
|
184
|
-
let V = json.V[i]
|
|
188
|
+
let V = json.V[i];
|
|
185
189
|
node.versions[V.ts] = {
|
|
186
190
|
name: V.name,
|
|
187
|
-
file: V.encryptedName
|
|
188
|
-
}
|
|
191
|
+
file: V.encryptedName,
|
|
192
|
+
};
|
|
189
193
|
}
|
|
190
194
|
if (Node.isDir(node)) {
|
|
191
195
|
for (let i = 0; i < json.c.length; i++) {
|
|
192
|
-
let n = Node.initNode(json.c[i], node)
|
|
196
|
+
let n = Node.initNode(json.c[i], node);
|
|
193
197
|
if (n) {
|
|
194
|
-
node.add(n)
|
|
198
|
+
node.add(n);
|
|
195
199
|
}
|
|
196
200
|
}
|
|
197
201
|
}
|
|
198
202
|
|
|
199
|
-
return node
|
|
200
|
-
} catch (e) {
|
|
201
|
-
}
|
|
203
|
+
return node;
|
|
204
|
+
} catch (e) {}
|
|
202
205
|
}
|
|
203
206
|
|
|
204
207
|
toCompressedJSON(
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
+
minSize,
|
|
209
|
+
verbose, // for testing purposes only
|
|
210
|
+
allFiles // in a real scenario, when called by Tree, this should be passed listing all the files in config.dataPath
|
|
208
211
|
) {
|
|
209
212
|
// prepare the object to be stringified and saved on disk
|
|
210
213
|
|
|
211
214
|
const result = {
|
|
212
|
-
v: []
|
|
213
|
-
}
|
|
215
|
+
v: [],
|
|
216
|
+
};
|
|
214
217
|
|
|
215
218
|
if (Node.isRoot(this)) {
|
|
216
|
-
minSize = this.calculateMinSize(allFiles)
|
|
219
|
+
minSize = this.calculateMinSize(allFiles);
|
|
217
220
|
}
|
|
218
221
|
|
|
219
222
|
if (this.versions) {
|
|
220
223
|
for (let ts in this.versions) {
|
|
221
|
-
let version = this.versions[ts]
|
|
222
|
-
let str = version.file.substring(0, minSize)
|
|
224
|
+
let version = this.versions[ts];
|
|
225
|
+
let str = version.file.substring(0, minSize);
|
|
223
226
|
if (verbose) {
|
|
224
|
-
result.v.push(str +
|
|
227
|
+
result.v.push(str + " " + ts + " " + version.name);
|
|
225
228
|
} else {
|
|
226
|
-
result.v.push(str)
|
|
229
|
+
result.v.push(str);
|
|
227
230
|
}
|
|
228
231
|
}
|
|
229
232
|
if (verbose) {
|
|
230
|
-
result.id = this.id
|
|
231
|
-
result.l = this.lastTs
|
|
233
|
+
result.id = this.id;
|
|
234
|
+
result.l = this.lastTs;
|
|
232
235
|
}
|
|
233
236
|
}
|
|
234
237
|
if (this.children) {
|
|
235
|
-
result.c = []
|
|
238
|
+
result.c = [];
|
|
236
239
|
for (let id in this.children) {
|
|
237
|
-
let child = this.children[id]
|
|
238
|
-
let json = child.toCompressedJSON(minSize, verbose)
|
|
240
|
+
let child = this.children[id];
|
|
241
|
+
let json = child.toCompressedJSON(minSize, verbose);
|
|
239
242
|
if (json) {
|
|
240
|
-
result.c.push(json)
|
|
243
|
+
result.c.push(json);
|
|
241
244
|
}
|
|
242
245
|
}
|
|
243
246
|
}
|
|
244
|
-
return result
|
|
247
|
+
return result;
|
|
245
248
|
}
|
|
246
249
|
|
|
247
|
-
|
|
248
250
|
toJSON() {
|
|
249
251
|
// builds a not circular tree removing parents
|
|
250
|
-
let node = {}
|
|
252
|
+
let node = {};
|
|
251
253
|
for (let key in this) {
|
|
252
|
-
if (key ===
|
|
253
|
-
continue
|
|
254
|
+
if (key === "parent") {
|
|
255
|
+
continue;
|
|
254
256
|
}
|
|
255
|
-
if (key ===
|
|
256
|
-
node.children = {}
|
|
257
|
+
if (key === "children") {
|
|
258
|
+
node.children = {};
|
|
257
259
|
} else {
|
|
258
|
-
node[key] = this[key]
|
|
260
|
+
node[key] = this[key];
|
|
259
261
|
}
|
|
260
262
|
}
|
|
261
263
|
if (node.parent) {
|
|
262
|
-
node.parentId = node.parent
|
|
263
|
-
delete node.parent
|
|
264
|
+
node.parentId = node.parent;
|
|
265
|
+
delete node.parent;
|
|
264
266
|
}
|
|
265
267
|
if (this.children) {
|
|
266
268
|
for (let id in this.children) {
|
|
267
|
-
let child = this.children[id]
|
|
268
|
-
node.children[id] = child.toJSON()
|
|
269
|
+
let child = this.children[id];
|
|
270
|
+
node.children[id] = child.toJSON();
|
|
269
271
|
}
|
|
270
272
|
}
|
|
271
|
-
return node
|
|
273
|
+
return node;
|
|
272
274
|
}
|
|
273
275
|
|
|
274
276
|
flat(list = {}) {
|
|
275
277
|
// flats the tree starting from a dir
|
|
276
|
-
let p = this.getPath()
|
|
277
|
-
list[p] = this.versions
|
|
278
|
+
let p = this.getPath();
|
|
279
|
+
list[p] = this.versions;
|
|
278
280
|
if (this.children) {
|
|
279
281
|
for (let id in this.children) {
|
|
280
|
-
let child = this.children[id]
|
|
281
|
-
child.flat(list)
|
|
282
|
+
let child = this.children[id];
|
|
283
|
+
child.flat(list);
|
|
282
284
|
}
|
|
283
285
|
}
|
|
284
|
-
return list
|
|
286
|
+
return list;
|
|
285
287
|
}
|
|
286
288
|
|
|
287
289
|
static getFindRe(options) {
|
|
288
290
|
return RegExp(
|
|
289
|
-
|
|
290
|
-
|
|
291
|
+
Entry.sanitizeName(options.name),
|
|
292
|
+
"g" + (options.sensitive ? "" : "i")
|
|
293
|
+
);
|
|
291
294
|
}
|
|
292
295
|
|
|
293
296
|
async find(options, list = []) {
|
|
294
297
|
if (!options.name) {
|
|
295
|
-
return list
|
|
298
|
+
return list;
|
|
296
299
|
}
|
|
297
|
-
let re = Node.getFindRe(options)
|
|
298
|
-
let p = this.getPath()
|
|
300
|
+
let re = Node.getFindRe(options);
|
|
301
|
+
let p = this.getPath();
|
|
299
302
|
if (this.versions && options.name) {
|
|
300
303
|
for (let ts in this.versions) {
|
|
301
304
|
if (options.all || ts === this.lastTs) {
|
|
302
|
-
let name = this.versions[ts].name
|
|
305
|
+
let name = this.versions[ts].name;
|
|
303
306
|
if (re.test(name)) {
|
|
304
307
|
if (options.getNodes) {
|
|
305
|
-
list.push(this)
|
|
308
|
+
list.push(this);
|
|
306
309
|
} else {
|
|
307
310
|
list.push([
|
|
308
311
|
Node.hashVersion(ts),
|
|
309
|
-
p + (Node.isDir(this) ?
|
|
312
|
+
p + (Node.isDir(this) ? "/" : ""),
|
|
310
313
|
name,
|
|
311
|
-
undefined
|
|
312
|
-
])
|
|
314
|
+
undefined,
|
|
315
|
+
]);
|
|
313
316
|
}
|
|
314
|
-
} else
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
317
|
+
} else if (
|
|
318
|
+
/* istanbul ignore if */
|
|
319
|
+
options.content &&
|
|
320
|
+
this.type === config.types.TEXT &&
|
|
321
|
+
options.tree
|
|
322
|
+
) {
|
|
323
|
+
let { content } = await options.tree.getEntryDetails(this, ts);
|
|
324
|
+
if (re.test(content || "")) {
|
|
319
325
|
if (options.getNodes) {
|
|
320
|
-
list.push(this)
|
|
326
|
+
list.push(this);
|
|
321
327
|
} else {
|
|
322
|
-
list.push([
|
|
323
|
-
Node.hashVersion(ts),
|
|
324
|
-
p,
|
|
325
|
-
undefined,
|
|
326
|
-
true
|
|
327
|
-
])
|
|
328
|
+
list.push([Node.hashVersion(ts), p, undefined, true]);
|
|
328
329
|
}
|
|
329
330
|
}
|
|
330
331
|
}
|
|
@@ -333,158 +334,157 @@ class Node {
|
|
|
333
334
|
}
|
|
334
335
|
if (this.children) {
|
|
335
336
|
for (let id in this.children) {
|
|
336
|
-
let child = this.children[id]
|
|
337
|
-
await child.find(options, list)
|
|
337
|
+
let child = this.children[id];
|
|
338
|
+
await child.find(options, list);
|
|
338
339
|
}
|
|
339
340
|
}
|
|
340
341
|
return list.sort((a, b) => {
|
|
341
|
-
let A = options.getNodes ? a.getPath() : a[1]
|
|
342
|
-
let B = options.getNodes ? b.getPath() : b[1]
|
|
343
|
-
return A > B ? 1 : A < B ? -1 : 0
|
|
344
|
-
})
|
|
342
|
+
let A = options.getNodes ? a.getPath() : a[1];
|
|
343
|
+
let B = options.getNodes ? b.getPath() : b[1];
|
|
344
|
+
return A > B ? 1 : A < B ? -1 : 0;
|
|
345
|
+
});
|
|
345
346
|
}
|
|
346
347
|
|
|
347
348
|
static initGenericRoot() {
|
|
348
349
|
let root = new Node(
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
)
|
|
353
|
-
return root
|
|
350
|
+
new Entry({
|
|
351
|
+
type: config.types.ROOT,
|
|
352
|
+
})
|
|
353
|
+
);
|
|
354
|
+
return root;
|
|
354
355
|
}
|
|
355
356
|
|
|
356
357
|
getAllFiles(child) {
|
|
357
358
|
if (!child) {
|
|
358
|
-
child = this
|
|
359
|
+
child = this;
|
|
359
360
|
}
|
|
360
|
-
let result = []
|
|
361
|
+
let result = [];
|
|
361
362
|
if (child.versions) {
|
|
362
363
|
for (let v in child.versions) {
|
|
363
364
|
if (child.versions[v].file) {
|
|
364
|
-
result.push(child.versions[v].file)
|
|
365
|
+
result.push(child.versions[v].file);
|
|
365
366
|
}
|
|
366
367
|
}
|
|
367
368
|
}
|
|
368
369
|
if (child.children) {
|
|
369
370
|
for (let c in child.children) {
|
|
370
|
-
result = result.concat(this.getAllFiles(child.children[c]))
|
|
371
|
+
result = result.concat(this.getAllFiles(child.children[c]));
|
|
371
372
|
}
|
|
372
373
|
}
|
|
373
|
-
return result
|
|
374
|
+
return result;
|
|
374
375
|
}
|
|
375
376
|
|
|
376
377
|
calculateMinSize(allFiles) {
|
|
377
|
-
|
|
378
378
|
if (!allFiles) {
|
|
379
|
-
allFiles = this.getAllFiles()
|
|
379
|
+
allFiles = this.getAllFiles();
|
|
380
380
|
}
|
|
381
|
-
let min = 0
|
|
382
|
-
let minSize
|
|
383
|
-
let arr = {}
|
|
381
|
+
let min = 0;
|
|
382
|
+
let minSize;
|
|
383
|
+
let arr = {};
|
|
384
384
|
LOOP: while (!minSize) {
|
|
385
|
-
min
|
|
385
|
+
min++;
|
|
386
386
|
for (let f of allFiles) {
|
|
387
|
-
let s = f.substring(0, min)
|
|
387
|
+
let s = f.substring(0, min);
|
|
388
388
|
if (arr[s]) {
|
|
389
|
-
continue LOOP
|
|
389
|
+
continue LOOP;
|
|
390
390
|
}
|
|
391
|
-
arr[s] = true
|
|
391
|
+
arr[s] = true;
|
|
392
392
|
}
|
|
393
|
-
return min
|
|
393
|
+
return min;
|
|
394
394
|
}
|
|
395
395
|
}
|
|
396
396
|
|
|
397
397
|
getName(ts) {
|
|
398
|
-
return this.get(
|
|
398
|
+
return this.get("name", ts);
|
|
399
399
|
}
|
|
400
400
|
|
|
401
401
|
getChildrenNames() {
|
|
402
402
|
if (Node.isFile(this)) {
|
|
403
|
-
throw new Error(
|
|
403
|
+
throw new Error("Files do not have children");
|
|
404
404
|
}
|
|
405
|
-
let names = []
|
|
405
|
+
let names = [];
|
|
406
406
|
for (let id in this.children) {
|
|
407
|
-
names.push(this.children[id].getName())
|
|
407
|
+
names.push(this.children[id].getName());
|
|
408
408
|
}
|
|
409
|
-
return names
|
|
409
|
+
return names;
|
|
410
410
|
}
|
|
411
411
|
|
|
412
412
|
getFile(ts) {
|
|
413
|
-
return this.get(
|
|
413
|
+
return this.get("file", ts);
|
|
414
414
|
}
|
|
415
415
|
|
|
416
416
|
getContent(ts) {
|
|
417
|
-
return this.get(
|
|
417
|
+
return this.get("content", ts);
|
|
418
418
|
}
|
|
419
419
|
|
|
420
420
|
getVersions() {
|
|
421
421
|
if (Node.isRoot(this)) {
|
|
422
|
-
return []
|
|
422
|
+
return [];
|
|
423
423
|
} else {
|
|
424
|
-
return Object.keys(this.versions).sort(Node.sortEntry)
|
|
424
|
+
return Object.keys(this.versions).sort(Node.sortEntry);
|
|
425
425
|
}
|
|
426
426
|
}
|
|
427
427
|
|
|
428
428
|
get(what, ts = this.lastTs) {
|
|
429
429
|
if (Node.isRoot(this)) {
|
|
430
|
-
return undefined
|
|
430
|
+
return undefined;
|
|
431
431
|
}
|
|
432
432
|
try {
|
|
433
|
-
return this.versions[ts][what]
|
|
433
|
+
return this.versions[ts][what];
|
|
434
434
|
} catch (e) {
|
|
435
|
-
throw new Error(
|
|
435
|
+
throw new Error("Version not found");
|
|
436
436
|
}
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
static getRoot(node) {
|
|
440
440
|
if (Node.isRoot(node)) {
|
|
441
|
-
return node
|
|
441
|
+
return node;
|
|
442
442
|
} else {
|
|
443
|
-
return Node.getRoot(node.parent)
|
|
443
|
+
return Node.getRoot(node.parent);
|
|
444
444
|
}
|
|
445
445
|
}
|
|
446
446
|
|
|
447
447
|
static isNode(obj) {
|
|
448
|
-
return obj instanceof Node
|
|
448
|
+
return obj instanceof Node;
|
|
449
449
|
}
|
|
450
450
|
|
|
451
451
|
findDirectChildByName(name, id) {
|
|
452
452
|
if (Node.isFile(this)) {
|
|
453
|
-
throw new Error(
|
|
453
|
+
throw new Error("A file does not have children");
|
|
454
454
|
}
|
|
455
455
|
if (!name) {
|
|
456
|
-
throw new Error(
|
|
456
|
+
throw new Error("Name parameter is missing");
|
|
457
457
|
}
|
|
458
458
|
for (let c in this.children) {
|
|
459
|
-
let child = this.children[c]
|
|
459
|
+
let child = this.children[c];
|
|
460
460
|
if (child.getName() === name) {
|
|
461
461
|
if (id && child.id !== id) {
|
|
462
|
-
break
|
|
462
|
+
break;
|
|
463
463
|
}
|
|
464
|
-
return child
|
|
464
|
+
return child;
|
|
465
465
|
}
|
|
466
466
|
}
|
|
467
467
|
}
|
|
468
468
|
|
|
469
469
|
findChildById(id, includeThis) {
|
|
470
470
|
if (Node.isFile(this)) {
|
|
471
|
-
throw new Error(
|
|
471
|
+
throw new Error("A file does not have children");
|
|
472
472
|
}
|
|
473
473
|
if (!id) {
|
|
474
|
-
throw new Error(
|
|
474
|
+
throw new Error("Id parameter is missing");
|
|
475
475
|
}
|
|
476
476
|
if (includeThis && id === this.id) {
|
|
477
|
-
return this
|
|
477
|
+
return this;
|
|
478
478
|
} else {
|
|
479
479
|
for (let c in this.children) {
|
|
480
|
-
let child = this.children[c]
|
|
480
|
+
let child = this.children[c];
|
|
481
481
|
if (c === id) {
|
|
482
|
-
return child
|
|
482
|
+
return child;
|
|
483
483
|
}
|
|
484
484
|
if (Node.isDir(child)) {
|
|
485
|
-
let found = child.findChildById(id)
|
|
485
|
+
let found = child.findChildById(id);
|
|
486
486
|
if (Node.isNode(found)) {
|
|
487
|
-
return found
|
|
487
|
+
return found;
|
|
488
488
|
}
|
|
489
489
|
}
|
|
490
490
|
}
|
|
@@ -494,126 +494,125 @@ class Node {
|
|
|
494
494
|
static isAncestor(ancestor, node) {
|
|
495
495
|
while (node) {
|
|
496
496
|
if (node.id === ancestor.id) {
|
|
497
|
-
return true
|
|
497
|
+
return true;
|
|
498
498
|
}
|
|
499
|
-
node = node.parent
|
|
499
|
+
node = node.parent;
|
|
500
500
|
}
|
|
501
|
-
return false
|
|
501
|
+
return false;
|
|
502
502
|
}
|
|
503
503
|
|
|
504
504
|
getChildFromPath(p, returnCloserAncestor, dontThrow) {
|
|
505
|
-
p = p.split(
|
|
506
|
-
let node
|
|
507
|
-
let ancestorNode
|
|
508
|
-
let index
|
|
509
|
-
let name
|
|
505
|
+
p = p.split("/").map((e) => Entry.sanitizeName(e));
|
|
506
|
+
let node;
|
|
507
|
+
let ancestorNode;
|
|
508
|
+
let index;
|
|
509
|
+
let name;
|
|
510
510
|
try {
|
|
511
511
|
FOR: for (index = 0; index < p.length; index++) {
|
|
512
|
-
name = p[index]
|
|
512
|
+
name = p[index];
|
|
513
513
|
if (index === 0) {
|
|
514
514
|
switch (name) {
|
|
515
|
-
case
|
|
516
|
-
case
|
|
515
|
+
case "":
|
|
516
|
+
case "~":
|
|
517
517
|
if (Node.isRoot(this)) {
|
|
518
|
-
node = this
|
|
518
|
+
node = this;
|
|
519
519
|
} else {
|
|
520
|
-
node = Node.getRoot(this)
|
|
520
|
+
node = Node.getRoot(this);
|
|
521
521
|
}
|
|
522
|
-
break
|
|
523
|
-
case
|
|
524
|
-
node = this
|
|
525
|
-
break
|
|
526
|
-
case
|
|
522
|
+
break;
|
|
523
|
+
case ".":
|
|
524
|
+
node = this;
|
|
525
|
+
break;
|
|
526
|
+
case "..":
|
|
527
527
|
if (Node.isRoot(this)) {
|
|
528
|
-
node = this
|
|
528
|
+
node = this;
|
|
529
529
|
} else {
|
|
530
|
-
node = this.parent
|
|
530
|
+
node = this.parent;
|
|
531
531
|
}
|
|
532
|
-
break
|
|
532
|
+
break;
|
|
533
533
|
default:
|
|
534
|
-
node = this.findDirectChildByName(name)
|
|
534
|
+
node = this.findDirectChildByName(name);
|
|
535
535
|
}
|
|
536
536
|
} else {
|
|
537
537
|
switch (name) {
|
|
538
|
-
case
|
|
539
|
-
throw new Error()
|
|
540
|
-
case
|
|
541
|
-
case
|
|
542
|
-
continue FOR
|
|
543
|
-
case
|
|
538
|
+
case "~":
|
|
539
|
+
throw new Error();
|
|
540
|
+
case "":
|
|
541
|
+
case ".":
|
|
542
|
+
continue FOR;
|
|
543
|
+
case "..":
|
|
544
544
|
if (!Node.isRoot(node)) {
|
|
545
|
-
node = node.parent
|
|
545
|
+
node = node.parent;
|
|
546
546
|
}
|
|
547
|
-
break
|
|
547
|
+
break;
|
|
548
548
|
default:
|
|
549
|
-
node = node.findDirectChildByName(name)
|
|
549
|
+
node = node.findDirectChildByName(name);
|
|
550
550
|
}
|
|
551
551
|
}
|
|
552
552
|
if (!node) {
|
|
553
|
-
throw new Error()
|
|
553
|
+
throw new Error();
|
|
554
554
|
}
|
|
555
|
-
ancestorNode = node
|
|
555
|
+
ancestorNode = node;
|
|
556
556
|
}
|
|
557
557
|
if (returnCloserAncestor) {
|
|
558
558
|
if (name === p[p.length - 1]) {
|
|
559
|
-
throw new Error(ENTRY_EXISTS)
|
|
559
|
+
throw new Error(ENTRY_EXISTS);
|
|
560
560
|
} else {
|
|
561
|
-
throw new Error(ANCESTOR_NOT_FOUND)
|
|
561
|
+
throw new Error(ANCESTOR_NOT_FOUND);
|
|
562
562
|
}
|
|
563
563
|
}
|
|
564
|
-
return node
|
|
564
|
+
return node;
|
|
565
565
|
} catch (e) {
|
|
566
566
|
if (e.message === ANCESTOR_NOT_FOUND) {
|
|
567
|
-
throw e
|
|
567
|
+
throw e;
|
|
568
568
|
} else if (e.message === ENTRY_EXISTS) {
|
|
569
|
-
throw new Error(util.format(ENTRY_EXISTS, name))
|
|
569
|
+
throw new Error(util.format(ENTRY_EXISTS, name));
|
|
570
570
|
}
|
|
571
571
|
if (returnCloserAncestor && ancestorNode) {
|
|
572
|
-
return [
|
|
573
|
-
ancestorNode,
|
|
574
|
-
p.slice(index).join('/')
|
|
575
|
-
]
|
|
572
|
+
return [ancestorNode, p.slice(index).join("/")];
|
|
576
573
|
}
|
|
577
|
-
throw new Error(
|
|
574
|
+
throw new Error("Path does not exist");
|
|
578
575
|
}
|
|
579
576
|
}
|
|
580
577
|
|
|
581
578
|
exists(p) {
|
|
582
579
|
try {
|
|
583
|
-
Node.getRoot(this).getChildFromPath(p)
|
|
584
|
-
return false
|
|
580
|
+
Node.getRoot(this).getChildFromPath(p);
|
|
581
|
+
return false;
|
|
585
582
|
} catch (e) {
|
|
586
|
-
return true
|
|
583
|
+
return true;
|
|
587
584
|
}
|
|
588
585
|
}
|
|
589
586
|
|
|
590
587
|
getPathToChild(child) {
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
throw new Error('The child is not a Node')
|
|
588
|
+
if (!child || child.constructor.name !== "Node") {
|
|
589
|
+
throw new Error("The child is not a Node");
|
|
594
590
|
}
|
|
595
|
-
let p =
|
|
591
|
+
let p = "";
|
|
596
592
|
while (child.id !== this.id) {
|
|
597
|
-
p = (child.getName() ||
|
|
598
|
-
child = child.parent
|
|
593
|
+
p = (child.getName() || "") + (p ? "/" + p : "");
|
|
594
|
+
child = child.parent;
|
|
599
595
|
}
|
|
600
|
-
if (
|
|
601
|
-
|
|
596
|
+
if (
|
|
597
|
+
child.id === config.specialId.ROOT &&
|
|
598
|
+
child.rnd !== Node.getRoot(this).rnd
|
|
599
|
+
) {
|
|
600
|
+
throw new Error("The child does not below to this tree");
|
|
602
601
|
}
|
|
603
602
|
if (Node.isRoot(this)) {
|
|
604
|
-
p =
|
|
603
|
+
p = "/" + p;
|
|
605
604
|
}
|
|
606
|
-
return p
|
|
605
|
+
return p;
|
|
607
606
|
}
|
|
608
607
|
|
|
609
608
|
getPath() {
|
|
610
|
-
let p =
|
|
611
|
-
let node = this
|
|
609
|
+
let p = "";
|
|
610
|
+
let node = this;
|
|
612
611
|
while (node.id !== config.specialId.ROOT) {
|
|
613
|
-
p = node.getName() + (p ?
|
|
614
|
-
node = node.parent
|
|
612
|
+
p = node.getName() + (p ? "/" + p : "");
|
|
613
|
+
node = node.parent;
|
|
615
614
|
}
|
|
616
|
-
return
|
|
615
|
+
return "/" + p;
|
|
617
616
|
}
|
|
618
617
|
|
|
619
618
|
getOptions(ts = this.lastTs) {
|
|
@@ -623,114 +622,112 @@ class Node {
|
|
|
623
622
|
ts,
|
|
624
623
|
parent: this.parent,
|
|
625
624
|
name: this.getName(ts),
|
|
626
|
-
encryptedName: this.getFile(ts)
|
|
627
|
-
}
|
|
628
|
-
return options
|
|
625
|
+
encryptedName: this.getFile(ts),
|
|
626
|
+
};
|
|
627
|
+
return options;
|
|
629
628
|
}
|
|
630
629
|
|
|
631
630
|
getEntry(ts) {
|
|
632
|
-
return new Entry(this.getOptions(ts))
|
|
631
|
+
return new Entry(this.getOptions(ts));
|
|
633
632
|
}
|
|
634
633
|
|
|
635
634
|
add(children, getChild) {
|
|
636
635
|
if (Node.isDir(this)) {
|
|
637
636
|
// a child is a Node instance
|
|
638
637
|
if (!Array.isArray(children)) {
|
|
639
|
-
children = [children]
|
|
638
|
+
children = [children];
|
|
640
639
|
}
|
|
641
640
|
for (let c of children) {
|
|
642
|
-
c.parent = this
|
|
643
|
-
this.children[c.id] = c
|
|
641
|
+
c.parent = this;
|
|
642
|
+
this.children[c.id] = c;
|
|
644
643
|
if (getChild) {
|
|
645
|
-
return c
|
|
644
|
+
return c;
|
|
646
645
|
}
|
|
647
646
|
}
|
|
648
647
|
} else {
|
|
649
|
-
throw new Error(
|
|
648
|
+
throw new Error("The entry does not represent a folder");
|
|
650
649
|
}
|
|
651
650
|
}
|
|
652
651
|
|
|
653
652
|
addVersion(entry) {
|
|
654
653
|
this.versions[entry.ts] = {
|
|
655
654
|
name: entry.name,
|
|
656
|
-
file: entry.encryptedName || entry.file
|
|
657
|
-
}
|
|
655
|
+
file: entry.encryptedName || entry.file,
|
|
656
|
+
};
|
|
658
657
|
if (entry.content) {
|
|
659
|
-
this.versions[entry.ts].content = entry.content
|
|
658
|
+
this.versions[entry.ts].content = entry.content;
|
|
660
659
|
}
|
|
661
|
-
this.lastTs = Object.keys(this.versions).sort(Node.sortEntry)[0]
|
|
662
|
-
return this.lastTs
|
|
660
|
+
this.lastTs = Object.keys(this.versions).sort(Node.sortEntry)[0];
|
|
661
|
+
return this.lastTs;
|
|
663
662
|
}
|
|
664
663
|
|
|
665
664
|
move(entry) {
|
|
666
665
|
if (Node.isRoot(this)) {
|
|
667
|
-
throw new Error(
|
|
666
|
+
throw new Error("Root cannot be moved");
|
|
668
667
|
}
|
|
669
668
|
if (entry.id !== this.id) {
|
|
670
|
-
throw new Error(
|
|
669
|
+
throw new Error("Id does not match");
|
|
671
670
|
}
|
|
672
671
|
if (!this.versions[entry.ts]) {
|
|
673
|
-
this.addVersion(entry)
|
|
672
|
+
this.addVersion(entry);
|
|
674
673
|
}
|
|
675
674
|
if (Node.isNode(entry.parent)) {
|
|
676
675
|
if (entry.parent.id !== this.parent.id) {
|
|
677
|
-
this.parent.removeChild(this)
|
|
678
|
-
entry.parent.add(this)
|
|
676
|
+
this.parent.removeChild(this);
|
|
677
|
+
entry.parent.add(this);
|
|
679
678
|
}
|
|
680
679
|
}
|
|
681
680
|
}
|
|
682
681
|
|
|
683
682
|
remove(version = []) {
|
|
684
|
-
|
|
685
683
|
if (Node.isRoot(this)) {
|
|
686
|
-
throw new Error(
|
|
684
|
+
throw new Error("Root cannot be removed");
|
|
687
685
|
}
|
|
688
686
|
|
|
689
687
|
if (this.parent) {
|
|
690
688
|
if (!Array.isArray(version)) {
|
|
691
|
-
version = [version]
|
|
689
|
+
version = [version];
|
|
692
690
|
}
|
|
693
691
|
let deleted = {
|
|
694
692
|
id: this.id,
|
|
695
693
|
type: this.type,
|
|
696
|
-
versions: {}
|
|
697
|
-
}
|
|
694
|
+
versions: {},
|
|
695
|
+
};
|
|
698
696
|
if (Node.isDir(this)) {
|
|
699
|
-
deleted.children = this.children
|
|
697
|
+
deleted.children = this.children;
|
|
700
698
|
}
|
|
701
|
-
let result = []
|
|
702
|
-
let deleteAll = version.length === 0
|
|
699
|
+
let result = [];
|
|
700
|
+
let deleteAll = version.length === 0;
|
|
703
701
|
for (let v in this.versions) {
|
|
704
702
|
if (deleteAll || version.includes(Node.hashVersion(v))) {
|
|
705
703
|
result.push({
|
|
706
704
|
id: this.id,
|
|
707
705
|
version: Node.hashVersion(v),
|
|
708
706
|
name: this.versions[v].name,
|
|
709
|
-
file: this.versions[v].file
|
|
710
|
-
})
|
|
711
|
-
deleted.versions[v] = this.versions[v]
|
|
712
|
-
delete this.versions[v]
|
|
707
|
+
file: this.versions[v].file,
|
|
708
|
+
});
|
|
709
|
+
deleted.versions[v] = this.versions[v];
|
|
710
|
+
delete this.versions[v];
|
|
713
711
|
}
|
|
714
712
|
}
|
|
715
713
|
if (!Object.keys(this.versions).length && this.parent.children[this.id]) {
|
|
716
|
-
deleted.all = true
|
|
714
|
+
deleted.all = true;
|
|
717
715
|
}
|
|
718
716
|
|
|
719
717
|
if (deleted.all) {
|
|
720
|
-
this.parent.removeChild(this)
|
|
718
|
+
this.parent.removeChild(this);
|
|
721
719
|
}
|
|
722
|
-
return result
|
|
720
|
+
return result;
|
|
723
721
|
}
|
|
724
722
|
}
|
|
725
723
|
|
|
726
724
|
removeChild(child) {
|
|
727
|
-
delete this.children[child.id]
|
|
725
|
+
delete this.children[child.id];
|
|
728
726
|
}
|
|
729
727
|
|
|
730
728
|
static hashVersion(ts) {
|
|
731
|
-
return Crypto.b58Hash(ts).substring(0, 4)
|
|
729
|
+
return Crypto.b58Hash(ts).substring(0, 4);
|
|
732
730
|
}
|
|
733
|
-
|
|
734
731
|
}
|
|
735
732
|
|
|
736
|
-
module.exports = Node
|
|
733
|
+
module.exports = Node;
|