@secrez/fs 1.0.2 → 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 CHANGED
File without changes
package/README.md CHANGED
@@ -6,13 +6,13 @@ This is the filesystem component.
6
6
 
7
7
  @secrez/fs exposes:
8
8
 
9
- * InternalFs
10
- * ExternalFs
11
- * FsUtils
12
- * Tree
13
- * Node
14
- * DataCache
15
-
9
+ - InternalFs
10
+ - ExternalFs
11
+ - FsUtils
12
+ - Tree
13
+ - Node
14
+ - DataCache
15
+ - FileCipher (starting from 1.0.3)
16
16
 
17
17
  ## TODO
18
18
 
@@ -20,29 +20,35 @@ API documentation
20
20
 
21
21
  ## History
22
22
 
23
- __1.0.2__
24
- * Adds `encryptFile` and `decryptFile` to ExternalFs
23
+ **1.0.3**
24
+
25
+ - Move `encryptFile` and `decryptFile` from ExternalFs to FileCipher
26
+
27
+ **1.0.2**
28
+
29
+ - Adds `encryptFile` and `decryptFile` to ExternalFs
25
30
 
26
31
  ## Test coverage
27
32
 
28
33
  ```
29
- 95 passing (3s)
34
+ 97 passing (3s)
35
+ 1 pending
30
36
 
31
37
  ---------------|---------|----------|---------|---------|-------------------------------------------
32
38
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
33
39
  ---------------|---------|----------|---------|---------|-------------------------------------------
34
- All files | 91.06 | 80.03 | 91.97 | 90.94 |
35
- DataCache.js | 89.41 | 74.07 | 90.91 | 89.29 | 18-24,123-124,150
36
- ExternalFs.js | 94.03 | 76.92 | 100 | 93.94 | 10,28,34,50
40
+ All files | 80.92 | 68.1 | 85.21 | 80.54 |
41
+ DataCache.js | 89.41 | 74.07 | 90.91 | 89.29 | 17-23,122-123,149
42
+ ExternalFs.js | 94.03 | 76.92 | 100 | 93.94 | 9,29,35,54
43
+ FileCipher.js | 88.64 | 73.33 | 100 | 88.64 | 8,52,80,85,94
37
44
  FsUtils.js | 100 | 100 | 100 | 100 |
38
- InternalFs.js | 86.99 | 72.63 | 88.46 | 86.89 | ...19,248,265,274,312,322,348,404-409,441
45
+ InternalFs.js | 86.99 | 72.63 | 88.46 | 86.89 | ...25,257,274,283,328,339,359,418-423,455
39
46
  Messages.js | 100 | 100 | 100 | 100 |
40
- Node.js | 93.7 | 87.45 | 95.74 | 93.55 | ...77,495-501,561,567,582-586,645,691,699
41
- Tree.js | 89.92 | 74.1 | 86.49 | 89.86 | ...53,399-422,453,463,512,604,610,638-647
42
- index.js | 100 | 100 | 100 | 100 |
47
+ Node.js | 65.82 | 55.19 | 78.72 | 65.03 | ...67,579-605,644-648,666,669,683-720,729
48
+ Tree.js | 83.65 | 70.48 | 81.08 | 83.19 | ...72,417-439,466,476,530,622,630,661-672
43
49
  ---------------|---------|----------|---------|---------|-------------------------------------------
44
- ```
45
50
 
51
+ ```
46
52
 
47
53
  ## Copyright
48
54
 
@@ -51,4 +57,3 @@ All files | 91.06 | 80.03 | 91.97 | 90.94 |
51
57
  ## Licence
52
58
 
53
59
  MIT
54
-
package/coverage.report CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
- > @secrez/fs@1.0.2 test /Users/sullof/Projects/Personal/secrez/packages/fs
3
- > cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text ./node_modules/.bin/_mocha test/*.test.js test/**/*.test.js --exit
2
+ > @secrez/fs@1.0.4 test /Users/francescosullo/Projects/Personal/secrez/packages/fs
3
+ > cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit
4
4
 
5
5
 
6
6
 
@@ -34,9 +34,12 @@
34
34
  ✓ should return that "config.test.js" is a file
35
35
  ✓ should confirm that "utils" is not a file
36
36
  ✓ should return that a not-existent file is not a dir
37
+
38
+ #FileCipher
37
39
  encrypt/decrypt external file
38
40
  ✓ should encrypt a file using a specific password
39
41
  ✓ should encrypt a file using a shared key
42
+ ✓ should encrypt a file using user's own public key
40
43
 
41
44
  #FsUtils
42
45
  preParseCommandLine
@@ -126,7 +129,7 @@
126
129
  #getPathToChild
127
130
  ✓ should find a node starting from a path
128
131
  ✓ should throw if child is not a Node
129
- 1) should throw if child belows to another tree
132
+ ✓ should throw if child belows to another tree
130
133
  #findDirectChildByName && #findChildById
131
134
  ✓ should find a direct child by name and by id
132
135
  ✓ should throw if looking for child of a file
@@ -142,39 +145,26 @@
142
145
  getEntryDetails
143
146
  ✓ should return the entry details of a node
144
147
  #Fix
145
- ✓ should load a tree with tags (52ms)
146
- ✓ should simulate a conflict in the repo and recover lost entries (843ms)
147
- ✓ should simulate a lost index in the repo and recover the entries (70ms)
148
+ ✓ should load a tree with tags
149
+ ✓ should simulate a conflict in the repo and recover lost entries (1844ms)
150
+ - should simulate a lost index in the repo and recover the entries
148
151
  Multi data sets
149
152
  ✓ should load the trees of two data sets
150
153
 
151
154
 
152
- 96 passing (3s)
153
- 1 failing
154
-
155
- 1) #Node
156
- #getPathToChild
157
- should throw if child belows to another tree:
158
- RangeError: Invalid count value
159
- at String.repeat (<anonymous>)
160
- at Function.getTimestampWithMicroseconds (/Users/sullof/Projects/Personal/secrez/packages/crypto/src/index.js:374:55)
161
- at Secrez.encryptEntry (/Users/sullof/Projects/Personal/secrez/packages/core/src/Secrez.js:289:22)
162
- at initRandomNode (/Users/sullof/Projects/Personal/secrez/packages/test-helpers/src/index.js:188:20)
163
- at Context.<anonymous> (test/Node.test.js:795:19)
164
-
165
-
155
+ 97 passing (3s)
156
+ 1 pending
166
157
 
167
158
  ---------------|---------|----------|---------|---------|-------------------------------------------
168
159
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
169
160
  ---------------|---------|----------|---------|---------|-------------------------------------------
170
- All files | 90.65 | 79.37 | 92.2 | 90.52 |
171
- DataCache.js | 89.41 | 74.07 | 90.91 | 89.29 | 18-24,123-124,150
172
- ExternalFs.js | 89.29 | 72.6 | 100 | 89.19 | ...,35,51,136,154,160,163,173,185,190,196
161
+ All files | 80.92 | 68.1 | 85.21 | 80.54 |
162
+ DataCache.js | 89.41 | 74.07 | 90.91 | 89.29 | 17-23,122-123,149
163
+ ExternalFs.js | 94.03 | 76.92 | 100 | 93.94 | 9,29,35,54
164
+ FileCipher.js | 88.64 | 73.33 | 100 | 88.64 | 8,52,80,85,94
173
165
  FsUtils.js | 100 | 100 | 100 | 100 |
174
- InternalFs.js | 86.99 | 72.63 | 88.46 | 86.89 | ...19,248,265,274,312,322,348,404-409,441
166
+ InternalFs.js | 86.99 | 72.63 | 88.46 | 86.89 | ...25,257,274,283,328,339,359,418-423,455
175
167
  Messages.js | 100 | 100 | 100 | 100 |
176
- Node.js | 93.41 | 87.07 | 95.74 | 93.26 | ...95-501,561,567,582-586,601,645,691,699
177
- Tree.js | 89.92 | 74.1 | 86.49 | 89.86 | ...53,399-422,453,463,512,604,610,638-647
178
- index.js | 100 | 100 | 100 | 100 |
168
+ Node.js | 65.82 | 55.19 | 78.72 | 65.03 | ...67,579-605,644-648,666,669,683-720,729
169
+ Tree.js | 83.65 | 70.48 | 81.08 | 83.19 | ...72,417-439,466,476,530,622,630,661-672
179
170
  ---------------|---------|----------|---------|---------|-------------------------------------------
180
-  ERROR  Test failed. See above for more details.
package/package.json CHANGED
@@ -1,45 +1,42 @@
1
1
  {
2
- "name": "@secrez/fs",
3
- "version": "1.0.2",
4
- "license": "MIT",
5
- "publishConfig": {
6
- "access": "public"
7
- },
8
- "scripts": {
9
- "lint": "eslint -c .eslintrc 'src/**/*.js' 'test/**/*.js'",
10
- "all-tests": "find test/** -name '*.test.js' | xargs ./node_modules/.bin/mocha -R spec",
11
- "test-only": "cross-env NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js test/**/*.test.js --exit",
12
- "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text ./node_modules/.bin/_mocha test/*.test.js test/**/*.test.js --exit",
13
- "posttest": "nyc check-coverage --statements 85 --branches 80 --functions 85 --lines 85"
14
- },
15
- "dependencies": {
16
- "@secrez/core": "~1.0.2",
17
- "@secrez/crypto": "~1.0.1",
18
- "@secrez/utils": "~1.0.0",
19
- "command-line-args": "^5.1.1",
20
- "fs-extra": "^9.1.0",
21
- "lodash": "^4.17.15"
22
- },
23
- "devDependencies": {
24
- "@secrez/test-helpers": "~1.0.1",
25
- "chai": "^4.2.0",
26
- "cross-env": "^7.0.2",
27
- "eslint": "^6.8.0",
28
- "eslint-plugin-node": "^11.0.0",
29
- "mocha": "^7.1.1",
30
- "nyc": "^15.1.0"
31
- },
32
- "description": "The Secrez fs library.",
33
- "main": "src/index.js",
34
- "repository": {
35
- "type": "git",
36
- "url": "git+https://github.com/secrez/secrez.git"
37
- },
38
- "author": {
39
- "name": "Francesco Sullo",
40
- "email": "francesco@sullo.co"
41
- },
42
- "engines": {
43
- "node": ">=10.0.0"
44
- }
45
- }
2
+ "name": "@secrez/fs",
3
+ "version": "1.0.4",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "dependencies": {
9
+ "@secrez/core": "~1.0.4",
10
+ "@secrez/crypto": "~1.0.3",
11
+ "@secrez/utils": "~1.0.3",
12
+ "command-line-args": "^5.1.1",
13
+ "fs-extra": "^9.1.0",
14
+ "lodash": "^4.17.15"
15
+ },
16
+ "devDependencies": {
17
+ "@secrez/test-helpers": "~2.0.0",
18
+ "chai": "^4.2.0",
19
+ "cross-env": "^7.0.2",
20
+ "mocha": "^7.1.1",
21
+ "nyc": "^15.1.0"
22
+ },
23
+ "description": "The Secrez fs library.",
24
+ "main": "src/index.js",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/secrez/secrez.git"
28
+ },
29
+ "author": {
30
+ "name": "Francesco Sullo",
31
+ "email": "francesco@sullo.co"
32
+ },
33
+ "engines": {
34
+ "node": ">=10.0.0"
35
+ },
36
+ "scripts": {
37
+ "all-tests": "find test/** -name '*.test.js' | xargs ./node_modules/.bin/mocha -R spec",
38
+ "test-only": "cross-env NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js test/**/*.test.js --exit",
39
+ "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit",
40
+ "posttest": "nyc check-coverage --statements 80 --branches 65 --functions 80 --lines 80"
41
+ }
42
+ }
package/src/DataCache.js CHANGED
@@ -1,156 +1,154 @@
1
- const path = require('path')
2
- const fs = require('fs-extra')
1
+ const path = require("path");
2
+ const fs = require("fs-extra");
3
3
 
4
4
  class DataCache {
5
-
6
5
  constructor(dataPath, secrez) {
7
- this.secrez = secrez
8
- this.cache = {}
9
- this.encrypted = {}
6
+ this.secrez = secrez;
7
+ this.cache = {};
8
+ this.encrypted = {};
10
9
  if (dataPath) {
11
- this.dataPath = dataPath
12
- fs.ensureDirSync(dataPath)
13
- this.ensured = {}
10
+ this.dataPath = dataPath;
11
+ fs.ensureDirSync(dataPath);
12
+ this.ensured = {};
14
13
  }
15
14
  }
16
15
 
17
16
  async reset() {
18
- if (this.cache && process.env.NODE_ENV === 'test') {
17
+ if (this.cache && process.env.NODE_ENV === "test") {
19
18
  if (this.dataPath) {
20
- await fs.emptyDir(this.dataPath)
19
+ await fs.emptyDir(this.dataPath);
21
20
  }
22
- this.cache = {}
23
- this.ensured = {}
24
- this.encrypted = {}
21
+ this.cache = {};
22
+ this.ensured = {};
23
+ this.encrypted = {};
25
24
  }
26
25
  }
27
26
 
28
27
  initEncryption(...keys) {
29
28
  for (let key of keys) {
30
- this.encrypted[key] = true
29
+ this.encrypted[key] = true;
31
30
  }
32
31
  }
33
32
 
34
33
  async load(key) {
35
34
  if (this.dataPath) {
36
- this.ensure(key)
37
- let p = path.join(this.dataPath, key)
38
- let files = await fs.readdir(p)
35
+ this.ensure(key);
36
+ let p = path.join(this.dataPath, key);
37
+ let files = await fs.readdir(p);
39
38
  for (let i = 0; i < files.length; i++) {
40
- let content = await fs.readFile(path.join(p, files[i]), 'utf8')
39
+ let content = await fs.readFile(path.join(p, files[i]), "utf8");
41
40
  if (this.encrypted[key]) {
42
41
  files[i] = {
43
42
  encryptedValue: files[i],
44
43
  value: this.secrez.decryptData(files[i], true),
45
- }
44
+ };
46
45
  if (content) {
47
- files[i].content = this.secrez.decryptData(content)
46
+ files[i].content = this.secrez.decryptData(content);
48
47
  }
49
48
  } else {
50
49
  files[i] = {
51
- value: files[i]
52
- }
50
+ value: files[i],
51
+ };
53
52
  if (content) {
54
- files[i].content = content
53
+ files[i].content = content;
55
54
  }
56
55
  }
57
56
  }
58
- return this.puts(key, files, true)
57
+ return this.puts(key, files, true);
59
58
  }
60
- return false
59
+ return false;
61
60
  }
62
61
 
63
62
  async puts(key, data, dontSave) {
64
63
  if (!this.cache[key]) {
65
- this.cache[key] = {}
64
+ this.cache[key] = {};
66
65
  }
67
66
  if (!Array.isArray(data)) {
68
- data = [data]
67
+ data = [data];
69
68
  }
70
- let counter = 0
69
+ let counter = 0;
71
70
  for (let v of data) {
72
- if (typeof v === 'string') {
71
+ if (typeof v === "string") {
73
72
  v = {
74
- value: v
75
- }
73
+ value: v,
74
+ };
76
75
  }
77
76
  if (!dontSave) {
78
- v = await this.save(key, v)
77
+ v = await this.save(key, v);
79
78
  }
80
- let value = v.value
81
- delete v.value
82
- this.cache[key][value] = v
83
- counter++
79
+ let value = v.value;
80
+ delete v.value;
81
+ this.cache[key][value] = v;
82
+ counter++;
84
83
  }
85
- return counter
84
+ return counter;
86
85
  }
87
86
 
88
87
  list(key) {
89
- return Object.keys(this.get(key))
88
+ return Object.keys(this.get(key));
90
89
  }
91
90
 
92
91
  get(key, value) {
93
- let all = this.cache[key] || {}
92
+ let all = this.cache[key] || {};
94
93
  if (value) {
95
- all = all[value]
94
+ all = all[value];
96
95
  }
97
- return all
96
+ return all;
98
97
  }
99
98
 
100
99
  is(key, value) {
101
- return !!this.get(key, value)
100
+ return !!this.get(key, value);
102
101
  }
103
102
 
104
103
  ensure(key) {
105
104
  if (!this.ensured[key]) {
106
- fs.ensureDirSync(path.join(this.dataPath, key))
107
- this.ensured[key] = true
105
+ fs.ensureDirSync(path.join(this.dataPath, key));
106
+ this.ensured[key] = true;
108
107
  }
109
108
  }
110
109
 
111
110
  async save(key, data) {
112
111
  if (this.dataPath) {
113
- this.ensure(key)
114
- let value = data.value
115
- let content = data.content || ''
112
+ this.ensure(key);
113
+ let value = data.value;
114
+ let content = data.content || "";
116
115
  if (this.encrypted[key]) {
117
- value = this.secrez.encryptData(value, true)
118
- content = content ? this.secrez.encryptData(content) : ''
116
+ value = this.secrez.encryptData(value, true);
117
+ content = content ? this.secrez.encryptData(content) : "";
119
118
  }
120
- let p = path.join(this.dataPath, key, value)
121
- let changed = true
119
+ let p = path.join(this.dataPath, key, value);
120
+ let changed = true;
122
121
  if (await fs.pathExists(p)) {
123
- if (await fs.readFile(p, 'utf8') === content) {
124
- changed = false
122
+ if ((await fs.readFile(p, "utf8")) === content) {
123
+ changed = false;
125
124
  }
126
125
  }
127
126
  if (changed) {
128
- await fs.writeFile(p, content)
127
+ await fs.writeFile(p, content);
129
128
  }
130
129
  if (this.encrypted[key]) {
131
- data.encryptedValue = value
130
+ data.encryptedValue = value;
132
131
  }
133
- return data
132
+ return data;
134
133
  }
135
- return data
134
+ return data;
136
135
  }
137
136
 
138
137
  async remove(key, value) {
139
- let data = this.get(key, value)
138
+ let data = this.get(key, value);
140
139
  if (data) {
141
140
  if (this.dataPath) {
142
- let p = path.join(this.dataPath, key, data.encryptedValue || value)
141
+ let p = path.join(this.dataPath, key, data.encryptedValue || value);
143
142
  if (await fs.pathExists(p)) {
144
- await fs.unlink(p)
143
+ await fs.unlink(p);
145
144
  }
146
145
  }
147
- delete this.cache[key][value]
148
- return true
146
+ delete this.cache[key][value];
147
+ return true;
149
148
  } else {
150
- return false
149
+ return false;
151
150
  }
152
151
  }
153
-
154
152
  }
155
153
 
156
- module.exports = DataCache
154
+ module.exports = DataCache;