@secrez/fs 1.1.2 → 1.2.1

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/coverage.report CHANGED
@@ -1,6 +1,5 @@
1
-  WARN  Unsupported engine: wanted: {"node":">=22.23.4"} (current: {"node":"v22.14.0","pnpm":"9.6.0"})
2
1
 
3
- > @secrez/fs@1.1.2 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
2
+ > @secrez/fs@1.2.1 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
4
3
  > cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit
5
4
 
6
5
 
@@ -74,10 +73,10 @@
74
73
  ✔ should not detect conflict risk in non-git repository
75
74
  with local git repository
76
75
  ✔ should detect git repository
77
- ✔ should get git status when up to date with remote (153ms)
78
- ✔ should detect conflict risk when behind remote (281ms)
76
+ ✔ should get git status when up to date with remote (62ms)
77
+ ✔ should detect conflict risk when behind remote (130ms)
79
78
  ✔ should get warning message for conflict risk
80
- ✔ should not show alert again after user proceeds (154ms)
79
+ ✔ should not show alert again after user proceeds (65ms)
81
80
  ✔ should reset cache
82
81
 
83
82
  #InternalFs
@@ -154,6 +153,9 @@
154
153
  ✔ should throw if looking for child of a file
155
154
  ✔ should throw if looking for child of a file
156
155
 
156
+ #recovery with three git clones
157
+ ✔ keeps the added file, drops the deleted one, and ends on one commit (1257ms)
158
+
157
159
  #Tree
158
160
  #constructor
159
161
  ✔ should instantiate the Tree
@@ -165,13 +167,15 @@
165
167
  ✔ should return the entry details of a node
166
168
  #Fix
167
169
  ✔ should load a tree with tags
168
- ✔ should simulate a conflict in the repo and recover lost entries when a valid tree exists (1116ms)
169
- ✔ should simulate a lost index in the repo and recover the entries when no valid tree is found (44ms)
170
+ ✔ should simulate a conflict in the repo and recover lost entries when a valid tree exists (1065ms)
171
+ ✔ should not recover a deleted file when its blob is still on disk
172
+ ✔ should keep a recreated file when the deleted blob reappears
173
+ ✔ should simulate a lost index in the repo and recover the entries when no valid tree is found
170
174
  Multi data sets
171
175
  ✔ should load the trees of two data sets
172
176
 
173
177
 
174
- 110 passing (4s)
178
+ 113 passing (4s)
175
179
  3 failing
176
180
 
177
181
  1) #GitConflictChecker
@@ -245,16 +249,17 @@
245
249
  -----------------------|---------|----------|---------|---------|-----------------------------------
246
250
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
247
251
  -----------------------|---------|----------|---------|---------|-----------------------------------
248
- All files | 83.39 | 70.34 | 88.19 | 83.09 |
252
+ All files | 83.88 | 71.6 | 88.02 | 83.61 |
249
253
  DataCache.js | 89.53 | 74.07 | 90.9 | 89.41 | 18-24,123-124,150
250
254
  ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
251
255
  FileCipher.js | 88.05 | 73.8 | 100 | 88.05 | 9,39,76,85,91,95,131,136
252
256
  FsUtils.js | 100 | 100 | 100 | 100 |
253
- GitConflictChecker.js | 84.87 | 79.81 | 100 | 84.87 | ...89,199,202,212,215,224,242,323
254
- InternalFs.js | 87.5 | 73.5 | 88.88 | 87.4 | ...88,333,344,364,423-428,460,471
257
+ GitConflictChecker.js | 84.87 | 81.65 | 100 | 84.87 | ...89,199,202,212,215,224,242,323
258
+ InternalFs.js | 87.59 | 73.5 | 88.88 | 87.5 | ...89,334,345,365,425-430,462,473
255
259
  Messages.js | 100 | 100 | 100 | 100 |
256
- Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
257
- Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...44,471,481,535,627,635,666-677
260
+ Node.js | 68.03 | 57.69 | 79.59 | 67.22 | ...22,661-665,683,686,700-737,746
261
+ Tree.js | 89.94 | 75.91 | 85.36 | 89.81 | ...79,506,516,570,662,670,701-712
258
262
  fileCipherLegacy.js | 100 | 100 | 100 | 100 |
263
+ index.js | 100 | 100 | 100 | 100 |
259
264
  -----------------------|---------|----------|---------|---------|-----------------------------------
260
265
   ELIFECYCLE  Test failed. See above for more details.
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@secrez/fs",
3
- "version": "1.1.2",
3
+ "version": "1.2.1",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "dependencies": {
9
- "@secrez/core": "~1.1.1",
10
- "@secrez/crypto": "~1.1.1",
9
+ "@secrez/core": "~2.0.0",
10
+ "@secrez/crypto": "~1.2.0",
11
11
  "@secrez/utils": "~1.1.2",
12
12
  "command-line-args": "^5.2.1",
13
13
  "fs-extra": "^9.1.0",
package/src/InternalFs.js CHANGED
@@ -201,6 +201,7 @@ class InternalFs {
201
201
  await this.moveOrUnlink(node, indexFrom, indexTo, fromTrash, entry);
202
202
  let originalParent = treeFrom.root.findChildById(originalParentId, true);
203
203
  originalParent.removeChild(node);
204
+ treeFrom.noteRemoved(node);
204
205
  if (cross) {
205
206
  entry.parent.add(node);
206
207
  }
@@ -379,6 +380,7 @@ class InternalFs {
379
380
  trash.root.add(node);
380
381
  }
381
382
  await this.moveOrUnlink(node, index, 1, fromTrash);
383
+ tree.noteRemoved(node);
382
384
  }
383
385
  await tree.save();
384
386
  await trash.save();
package/src/Node.js CHANGED
@@ -103,19 +103,29 @@ class Node {
103
103
  if (typeof json === "string") {
104
104
  json = JSON.parse(json);
105
105
  }
106
+ // Full encrypted names of blobs that left this dataset. Absent on older indexes.
107
+ let removed = Array.isArray(json.r)
108
+ ? json.r.filter((name) => typeof name === "string" && name)
109
+ : [];
106
110
 
107
- for (let c of json.c) {
111
+ for (let c of json.c || []) {
108
112
  if (c.v[0]) {
109
113
  minSize = c.v[0].length;
110
114
  break;
111
115
  }
112
116
  }
117
+ let removedSet = new Set(removed);
113
118
  let files = {};
114
119
  for (let f of allFiles) {
120
+ // A removed blob must not steal the prefix of a file created later.
121
+ if (removedSet.has(f)) continue;
115
122
  files[f.substring(0, minSize)] = f;
116
123
  }
117
124
  json = Node.preFormat(json, secrez, files);
118
125
  let root = Node.initNode(json);
126
+ if (root) {
127
+ root.removed = removed;
128
+ }
119
129
  return root;
120
130
  }
121
131
 
@@ -217,6 +227,13 @@ class Node {
217
227
 
218
228
  if (Node.isRoot(this)) {
219
229
  minSize = this.calculateMinSize(allFiles);
230
+ let live = new Set(this.getAllFiles());
231
+ let removed = (this.removed || []).filter(
232
+ (name) => name && !live.has(name)
233
+ );
234
+ if (removed.length) {
235
+ result.r = removed.slice().sort();
236
+ }
220
237
  }
221
238
 
222
239
  if (this.versions) {
package/src/Tree.js CHANGED
@@ -112,7 +112,7 @@ class Tree {
112
112
 
113
113
  // verify the tree
114
114
  let allFilesOnTree = Tree.getAllFilesOnTree(this.root).sort();
115
- const filesNotOnTree = [];
115
+ let filesNotOnTree = [];
116
116
  for (let i = 0; i < allSecretsFiles.length; i++) {
117
117
  let file = allSecretsFiles[i];
118
118
  if (!allFilesOnTree.includes(file)) {
@@ -120,28 +120,18 @@ class Tree {
120
120
  }
121
121
  }
122
122
  if (filesNotOnTree.length) {
123
- let toBeRecovered = {};
124
- let recoveredEntries = [];
125
- FOR: for (let i = 1; i < allIndexes.length; i++) {
123
+ let olderRoots = [];
124
+ for (let i = 1; i < allIndexes.length; i++) {
126
125
  json = JSON.parse(allIndexes[i].content);
127
126
  let root = Node.fromJSON(json, this.secrez, allSecretsFiles);
128
- allFilesOnTree = Tree.getAllFilesOnTree(root).sort();
129
- for (let j = 0; j < filesNotOnTree.length; j++) {
130
- let f = filesNotOnTree[j];
131
- if (allFilesOnTree.includes(f.encryptedName)) {
132
- let node = root.findChildById(f.id);
133
- if (node) {
134
- let k = allSecretsFiles.indexOf(f.encryptedName);
135
- toBeRecovered[node.getPath()] = allSecrets[k];
136
- filesNotOnTree.splice(j, 1);
137
- j--;
138
- }
139
- }
140
- if (!files.length) {
141
- break FOR;
142
- }
143
- }
127
+ if (root) olderRoots.push(root);
144
128
  }
129
+ let { toBeRecovered, unknown } = this.classifyMissing(
130
+ filesNotOnTree,
131
+ olderRoots
132
+ );
133
+ let recoveredEntries = [];
134
+ filesNotOnTree = unknown;
145
135
  let paths = Object.keys(toBeRecovered).sort();
146
136
  for (let p of paths) {
147
137
  let entry = toBeRecovered[p];
@@ -212,6 +202,51 @@ class Tree {
212
202
  this.status = Tree.statutes.LOADED;
213
203
  }
214
204
 
205
+ noteRemoved(node) {
206
+ if (!node || typeof node.getAllFiles !== "function") return;
207
+ this.noteRemovedFiles(node.getAllFiles());
208
+ }
209
+
210
+ noteRemovedFile(encryptedName) {
211
+ this.noteRemovedFiles([encryptedName]);
212
+ }
213
+
214
+ noteRemovedFiles(names) {
215
+ if (!this.root.removed) this.root.removed = [];
216
+ let live = new Set(this.root.getAllFiles());
217
+ for (let name of names || []) {
218
+ if (!name || live.has(name) || this.root.removed.includes(name)) continue;
219
+ this.root.removed.push(name);
220
+ }
221
+ }
222
+
223
+ // Newest mention wins. A blob listed in `r` stays out unless a newer index
224
+ // still has it as a live file. Blobs no index mentions stay unknown.
225
+ classifyMissing(filesNotOnTree, olderRoots) {
226
+ let decision = new Map();
227
+ for (let root of [this.root, ...(olderRoots || [])]) {
228
+ for (let name of Tree.getAllFilesOnTree(root)) {
229
+ if (!decision.has(name)) decision.set(name, { present: true, root });
230
+ }
231
+ for (let name of root.removed || []) {
232
+ if (!decision.has(name)) decision.set(name, { present: false });
233
+ }
234
+ }
235
+ let toBeRecovered = {};
236
+ let unknown = [];
237
+ for (let entry of filesNotOnTree) {
238
+ let found = decision.get(entry.encryptedName);
239
+ if (!found) {
240
+ unknown.push(entry);
241
+ } else if (found.present && found.root !== this.root) {
242
+ let node = found.root.findChildById(entry.id);
243
+ if (node) toBeRecovered[node.getPath()] = entry;
244
+ else unknown.push(entry);
245
+ }
246
+ }
247
+ return { toBeRecovered, unknown };
248
+ }
249
+
215
250
  optimizeResults(entries) {
216
251
  entries.reverse();
217
252
  for (let i = 0; i < entries.length; i++) {