@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/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;
package/src/ExternalFs.js CHANGED
@@ -1,123 +1,129 @@
1
- const fs = require('fs-extra')
2
- const path = require('path')
1
+ const fs = require("fs-extra");
2
+ const path = require("path");
3
3
 
4
4
  class ExternalFs {
5
-
6
5
  constructor(secrez) {
7
- if (secrez.constructor.name === 'Secrez') {
8
- this.secrez = secrez
6
+ if (secrez.constructor.name === "Secrez") {
7
+ this.secrez = secrez;
9
8
  } else {
10
- throw new Error('ExternalFs requires a Secrez instance during construction')
9
+ throw new Error(
10
+ "ExternalFs requires a Secrez instance during construction"
11
+ );
11
12
  }
12
13
  }
13
14
 
14
- getNormalizedPath(file = '') {
15
- if (file === '~') {
16
- file = ''
15
+ getNormalizedPath(file = "") {
16
+ if (file === "~") {
17
+ file = "";
17
18
  } else if (/^~\//.test(file)) {
18
- file = file.replace(/^~\//, '')
19
+ file = file.replace(/^~\//, "");
19
20
  }
20
- let resolvedFile = path.resolve(this.secrez.config.localWorkingDir, file)
21
- return path.normalize(resolvedFile)
21
+ let resolvedFile = path.resolve(this.secrez.config.localWorkingDir, file);
22
+ return path.normalize(resolvedFile);
22
23
  }
23
24
 
24
25
  async getFileList(options = {}) {
25
26
  try {
26
- return await this.fileList(options)
27
+ return await this.fileList(options);
27
28
  } catch (e) {
28
- return []
29
+ return [];
29
30
  }
30
31
  }
31
32
 
32
33
  async fileList(options = {}) {
33
- if (typeof options === 'string') {
34
- options = {path: options}
34
+ if (typeof options === "string") {
35
+ options = { path: options };
35
36
  }
36
- let [isDir, list] = (await this.getDir(this.getNormalizedPath(options.path), options.forAutoComplete))
37
- list = list.filter(f => {
38
- let pre = true
37
+ let [isDir, list] = await this.getDir(
38
+ this.getNormalizedPath(options.path),
39
+ options.forAutoComplete
40
+ );
41
+ list = list.filter((f) => {
42
+ let pre = true;
39
43
  if (options.dironly) {
40
- pre = /\/$/.test(f)
44
+ pre = /\/$/.test(f);
41
45
  } else if (options.fileonly) {
42
- pre = !/\/$/.test(f)
46
+ pre = !/\/$/.test(f);
43
47
  }
44
48
  if (pre && !options.all) {
45
- pre = !/^\./.test(f)
49
+ pre = !/^\./.test(f);
46
50
  }
47
- return pre
48
- })
51
+ return pre;
52
+ });
49
53
  if (options.returnIsDir) {
50
- return [isDir, list]
54
+ return [isDir, list];
51
55
  } else {
52
- return list
56
+ return list;
53
57
  }
54
58
  }
55
59
 
56
60
  async mapDir(dir) {
57
- let list = await fs.readdir(dir)
61
+ let list = await fs.readdir(dir);
58
62
  for (let i = 0; i < list.length; i++) {
59
- list[i] = list[i] + ((await this.isDir(path.join(dir, list[i]))) ? '/' : '')
63
+ list[i] =
64
+ list[i] + ((await this.isDir(path.join(dir, list[i]))) ? "/" : "");
60
65
  }
61
- return list
66
+ return list;
62
67
  }
63
68
 
64
69
  async getDir(dir, forAutoComplete) {
65
- let list = []
66
- let isDir = await this.isDir(dir)
70
+ let list = [];
71
+ let isDir = await this.isDir(dir);
67
72
  if (isDir) {
68
- list = await this.mapDir(dir)
73
+ list = await this.mapDir(dir);
69
74
  } else {
70
- let fn = path.basename(dir)
75
+ let fn = path.basename(dir);
71
76
  if (fn) {
72
77
  if (await this.isFile(dir)) {
73
- list = [fn]
78
+ list = [fn];
74
79
  } else {
75
- dir = dir.replace(/\/[^/]+$/, '/')
80
+ dir = dir.replace(/\/[^/]+$/, "/");
76
81
  if (await this.isDir(dir)) {
77
- list = await this.mapDir(dir)
82
+ list = await this.mapDir(dir);
78
83
  }
79
- fn = '^' + fn.replace(/\?/g, '.{1}').replace(/\*/g, '.*')
80
- + (forAutoComplete ? '' : '(|\\/)$')
81
- let re = RegExp(fn)
82
- list = list.filter(e => {
83
- return re.test(e)
84
- })
84
+ fn =
85
+ "^" +
86
+ fn.replace(/\?/g, ".{1}").replace(/\*/g, ".*") +
87
+ (forAutoComplete ? "" : "(|\\/)$");
88
+ let re = RegExp(fn);
89
+ list = list.filter((e) => {
90
+ return re.test(e);
91
+ });
85
92
  }
86
93
  }
87
94
  }
88
- return [isDir, list]
95
+ return [isDir, list];
89
96
  }
90
97
 
91
98
  async isDir(dir) {
92
99
  if (await fs.pathExists(dir)) {
93
- return (await fs.lstat(dir)).isDirectory()
100
+ return (await fs.lstat(dir)).isDirectory();
94
101
  }
95
- return false
102
+ return false;
96
103
  }
97
104
 
98
105
  async isFile(fn) {
99
106
  if (await fs.pathExists(fn)) {
100
- return (await fs.lstat(fn)).isFile()
107
+ return (await fs.lstat(fn)).isFile();
101
108
  }
102
- return false
109
+ return false;
103
110
  }
104
111
 
105
112
  async getVersionedBasename(p) {
106
- p = this.getNormalizedPath(p)
107
- let dir = path.dirname(p)
108
- let fn = path.basename(p)
109
- let name = fn
110
- let v = 1
111
- for (; ;) {
112
- let filePath = path.join(dir, name)
113
+ p = this.getNormalizedPath(p);
114
+ let dir = path.dirname(p);
115
+ let fn = path.basename(p);
116
+ let name = fn;
117
+ let v = 1;
118
+ for (;;) {
119
+ let filePath = path.join(dir, name);
113
120
  if (!(await fs.pathExists(filePath))) {
114
- return name
121
+ return name;
115
122
  } else {
116
- name = fn + '.' + (++v)
123
+ name = fn + "." + ++v;
117
124
  }
118
125
  }
119
126
  }
120
-
121
127
  }
122
128
 
123
- module.exports = ExternalFs
129
+ module.exports = ExternalFs;
package/src/FileCipher.js CHANGED
@@ -1,91 +1,99 @@
1
- const Crypto = require('@secrez/crypto')
1
+ const Crypto = require("@secrez/crypto");
2
2
 
3
3
  class FileCipher {
4
-
5
4
  constructor(secrez) {
6
- if (secrez.constructor.name === 'Secrez') {
7
- this.secrez = secrez
5
+ if (secrez.constructor.name === "Secrez") {
6
+ this.secrez = secrez;
8
7
  } else {
9
- throw new Error('FileCipher requires a Secrez instance during construction')
8
+ throw new Error(
9
+ "FileCipher requires a Secrez instance during construction"
10
+ );
10
11
  }
11
12
  }
12
13
 
13
14
  shortPublicKey(pk) {
14
- return pk.split('$')[0].substring(0, 16)
15
+ return pk.split("$")[0].substring(0, 16);
15
16
  }
16
17
 
17
18
  getUint8ArrayPassword(password) {
18
- return Crypto.bufferToUint8Array(Crypto.SHA3(password))
19
+ return Crypto.bufferToUint8Array(Crypto.SHA3(password));
19
20
  }
20
21
 
21
22
  encryptFile(content, options) {
22
- let key = Crypto.generateKey()
23
+ let key = Crypto.generateKey();
23
24
  let result = [
24
- '1', //version
25
- Crypto.encrypt(content, key)
26
- ]
25
+ "1", //version
26
+ Crypto.encrypt(content, key),
27
+ ];
27
28
  if (options.password) {
28
- result[2] = Crypto.encrypt(key, this.getUint8ArrayPassword(options.password))
29
+ result[2] = Crypto.encrypt(
30
+ key,
31
+ this.getUint8ArrayPassword(options.password)
32
+ );
29
33
  } else {
30
- const myShort = this.shortPublicKey(this.secrez.getPublicKey())
31
- result[2] = myShort
34
+ const myShort = this.shortPublicKey(this.secrez.getPublicKey());
35
+ result[2] = myShort;
32
36
  if (options.publicKeys) {
33
37
  for (let publicKey of options.publicKeys) {
34
- let short = this.shortPublicKey(publicKey)
38
+ let short = this.shortPublicKey(publicKey);
35
39
  let encKey =
36
- short === myShort
37
- ? this.secrez.encryptData(key) // encrypted for themself
38
- : this.secrez.encryptSharedData(key, publicKey)
39
- result.push(short + encKey)
40
+ short === myShort
41
+ ? this.secrez.encryptData(key) // encrypted for themself
42
+ : this.secrez.encryptSharedData(key, publicKey);
43
+ result.push(short + encKey);
40
44
  }
41
45
  }
42
46
  }
43
- return result
47
+ return result;
44
48
  }
45
49
 
46
50
  decryptFile(encryptedContent, options) {
47
- if (typeof encryptedContent === 'string') {
48
- encryptedContent = encryptedContent.split(',')
51
+ if (typeof encryptedContent === "string") {
52
+ encryptedContent = encryptedContent.split(",");
49
53
  }
50
- const [version, content, passwordOrShortPublicKey, ...keys] = encryptedContent
51
- if (version === '1') {
54
+ const [version, content, passwordOrShortPublicKey, ...keys] =
55
+ encryptedContent;
56
+ if (version === "1") {
52
57
  if (keys && keys.length) {
53
- let myShort = this.shortPublicKey(this.secrez.getPublicKey())
54
- let contactPublicKey = options.contactPublicKey
58
+ let myShort = this.shortPublicKey(this.secrez.getPublicKey());
59
+ let contactPublicKey = options.contactPublicKey;
55
60
  if (!contactPublicKey) {
56
- for (let pk of (options.contactsPublicKeys || [])) {
61
+ for (let pk of options.contactsPublicKeys || []) {
57
62
  if (this.shortPublicKey(pk) === passwordOrShortPublicKey) {
58
- contactPublicKey = pk
59
- break
63
+ contactPublicKey = pk;
64
+ break;
60
65
  }
61
66
  }
62
67
  }
63
- let key
68
+ let key;
64
69
  for (let item of keys) {
65
- let short = item.substring(0, 16)
66
- item = item.substring(16)
70
+ let short = item.substring(0, 16);
71
+ item = item.substring(16);
67
72
  if (myShort === short) {
68
- key = myShort === passwordOrShortPublicKey
73
+ key =
74
+ myShort === passwordOrShortPublicKey
69
75
  ? this.secrez.decryptData(item)
70
- : this.secrez.decryptSharedData(item, contactPublicKey, true)
76
+ : this.secrez.decryptSharedData(item, contactPublicKey, true);
71
77
  }
72
78
  }
73
79
  if (!key) {
74
- throw new Error('The sender didn\'t encrypt the data for you')
80
+ throw new Error("The sender didn't encrypt the data for you");
75
81
  }
76
- return Crypto.decrypt(content, key)
82
+ return Crypto.decrypt(content, key);
77
83
  } else {
78
84
  if (!options.password) {
79
- throw new Error('A password is required')
85
+ throw new Error("A password is required");
80
86
  }
81
- let key = Crypto.decrypt(passwordOrShortPublicKey, this.getUint8ArrayPassword(options.password))
82
- return Crypto.decrypt(content, key, options.returnUint8Array)
87
+ let key = Crypto.decrypt(
88
+ passwordOrShortPublicKey,
89
+ this.getUint8ArrayPassword(options.password)
90
+ );
91
+ return Crypto.decrypt(content, key, options.returnUint8Array);
83
92
  }
84
93
  } else {
85
- throw new Error('Unsupported version')
94
+ throw new Error("Unsupported version");
86
95
  }
87
96
  }
88
-
89
97
  }
90
98
 
91
- module.exports = FileCipher
99
+ module.exports = FileCipher;
package/src/FsUtils.js CHANGED
@@ -1,78 +1,81 @@
1
- const _ = require('lodash')
2
- const path = require('path')
3
- const utils = require('@secrez/utils')
1
+ const _ = require("lodash");
2
+ const path = require("path");
3
+ const utils = require("@secrez/utils");
4
4
 
5
- const commandLineArgs = require('command-line-args')
5
+ const commandLineArgs = require("command-line-args");
6
6
 
7
7
  class FsUtils {
8
-
9
8
  static preParseCommandLine(commandLine) {
10
- let argv = []
11
- let k = 0
12
- let sep
13
- commandLine = _.trim(commandLine)
9
+ let argv = [];
10
+ let k = 0;
11
+ let sep;
12
+ commandLine = _.trim(commandLine);
14
13
  for (let i = 0; i < commandLine.length; i++) {
15
- let c = commandLine[i]
14
+ let c = commandLine[i];
16
15
  if (!sep && /("|')/.test(c)) {
17
- sep = c
16
+ sep = c;
18
17
  } else if (sep && c === sep) {
19
- sep = null
20
- } else if (!sep && c === '\\' && commandLine[i + 1] && commandLine[i + 1] !== '\\') {
21
- utils.addTo(argv, k, commandLine[i + 1])
22
- i++
23
- } else if (!sep && c === ' ') {
24
- k++
18
+ sep = null;
19
+ } else if (
20
+ !sep &&
21
+ c === "\\" &&
22
+ commandLine[i + 1] &&
23
+ commandLine[i + 1] !== "\\"
24
+ ) {
25
+ utils.addTo(argv, k, commandLine[i + 1]);
26
+ i++;
27
+ } else if (!sep && c === " ") {
28
+ k++;
25
29
  } else {
26
- utils.addTo(argv, k, c)
30
+ utils.addTo(argv, k, c);
27
31
  }
28
32
  }
29
- return argv
33
+ return argv;
30
34
  }
31
35
 
32
36
  static parseCommandLine(definitions, commandLine) {
33
37
  if (definitions && definitions.length) {
34
38
  let argv = {
35
39
  partial: true,
36
- camelCase: true
37
- }
40
+ camelCase: true,
41
+ };
38
42
  if (commandLine) {
39
- argv.argv = this.preParseCommandLine(commandLine)
43
+ argv.argv = this.preParseCommandLine(commandLine);
40
44
  }
41
- return commandLineArgs(definitions, argv)
45
+ return commandLineArgs(definitions, argv);
42
46
  }
43
- return {}
47
+ return {};
44
48
  }
45
49
 
46
50
  static async filterLs(options = {}, list = []) {
47
- if (typeof options === 'string') {
48
- options = {path: options}
51
+ if (typeof options === "string") {
52
+ options = { path: options };
49
53
  }
50
- let file = options.path || ''
51
- let bn = file ? path.basename(file) : ''
52
- if (bn === '.' || bn === '..') {
53
- bn = ''
54
+ let file = options.path || "";
55
+ let bn = file ? path.basename(file) : "";
56
+ if (bn === "." || bn === "..") {
57
+ bn = "";
54
58
  }
55
59
  if (bn && list.includes(bn)) {
56
- return [bn]
60
+ return [bn];
57
61
  } else if (bn) {
58
62
  bn = bn
59
- .replace(/\./g, '\\.')
60
- .replace(/\*/g, '.*')
61
- .replace(/\?/g, '.{1}}')
62
- let re = RegExp(bn)
63
- list = _.filter(list, e => re.test(e))
63
+ .replace(/\./g, "\\.")
64
+ .replace(/\*/g, ".*")
65
+ .replace(/\?/g, ".{1}}");
66
+ let re = RegExp(bn);
67
+ list = _.filter(list, (e) => re.test(e));
64
68
  }
65
- return list.map(f => {
66
- f = f.split('/')
67
- let l = f.length - 1
69
+ return list.map((f) => {
70
+ f = f.split("/");
71
+ let l = f.length - 1;
68
72
  if (f[l]) {
69
- return f[l]
73
+ return f[l];
70
74
  } else {
71
- return f[l - 1] + '/'
75
+ return f[l - 1] + "/";
72
76
  }
73
- })
77
+ });
74
78
  }
75
-
76
79
  }
77
80
 
78
- module.exports = FsUtils
81
+ module.exports = FsUtils;