@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/src/ExternalFs.js CHANGED
@@ -1,202 +1,129 @@
1
- const fs = require('fs-extra')
2
- const path = require('path')
3
- const Crypto = require('@secrez/crypto')
1
+ const fs = require("fs-extra");
2
+ const path = require("path");
4
3
 
5
4
  class ExternalFs {
6
-
7
5
  constructor(secrez) {
8
- if (secrez.constructor.name === 'Secrez') {
9
- this.secrez = secrez
6
+ if (secrez.constructor.name === "Secrez") {
7
+ this.secrez = secrez;
10
8
  } else {
11
- throw new Error('ExternalFs requires a Secrez instance during construction')
9
+ throw new Error(
10
+ "ExternalFs requires a Secrez instance during construction"
11
+ );
12
12
  }
13
13
  }
14
14
 
15
- getNormalizedPath(file = '') {
16
- if (file === '~') {
17
- file = ''
15
+ getNormalizedPath(file = "") {
16
+ if (file === "~") {
17
+ file = "";
18
18
  } else if (/^~\//.test(file)) {
19
- file = file.replace(/^~\//, '')
19
+ file = file.replace(/^~\//, "");
20
20
  }
21
- let resolvedFile = path.resolve(this.secrez.config.localWorkingDir, file)
22
- return path.normalize(resolvedFile)
21
+ let resolvedFile = path.resolve(this.secrez.config.localWorkingDir, file);
22
+ return path.normalize(resolvedFile);
23
23
  }
24
24
 
25
25
  async getFileList(options = {}) {
26
26
  try {
27
- return await this.fileList(options)
27
+ return await this.fileList(options);
28
28
  } catch (e) {
29
- return []
29
+ return [];
30
30
  }
31
31
  }
32
32
 
33
33
  async fileList(options = {}) {
34
- if (typeof options === 'string') {
35
- options = {path: options}
34
+ if (typeof options === "string") {
35
+ options = { path: options };
36
36
  }
37
- let [isDir, list] = (await this.getDir(this.getNormalizedPath(options.path), options.forAutoComplete))
38
- list = list.filter(f => {
39
- 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;
40
43
  if (options.dironly) {
41
- pre = /\/$/.test(f)
44
+ pre = /\/$/.test(f);
42
45
  } else if (options.fileonly) {
43
- pre = !/\/$/.test(f)
46
+ pre = !/\/$/.test(f);
44
47
  }
45
48
  if (pre && !options.all) {
46
- pre = !/^\./.test(f)
49
+ pre = !/^\./.test(f);
47
50
  }
48
- return pre
49
- })
51
+ return pre;
52
+ });
50
53
  if (options.returnIsDir) {
51
- return [isDir, list]
54
+ return [isDir, list];
52
55
  } else {
53
- return list
56
+ return list;
54
57
  }
55
58
  }
56
59
 
57
60
  async mapDir(dir) {
58
- let list = await fs.readdir(dir)
61
+ let list = await fs.readdir(dir);
59
62
  for (let i = 0; i < list.length; i++) {
60
- 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]))) ? "/" : "");
61
65
  }
62
- return list
66
+ return list;
63
67
  }
64
68
 
65
69
  async getDir(dir, forAutoComplete) {
66
- let list = []
67
- let isDir = await this.isDir(dir)
70
+ let list = [];
71
+ let isDir = await this.isDir(dir);
68
72
  if (isDir) {
69
- list = await this.mapDir(dir)
73
+ list = await this.mapDir(dir);
70
74
  } else {
71
- let fn = path.basename(dir)
75
+ let fn = path.basename(dir);
72
76
  if (fn) {
73
77
  if (await this.isFile(dir)) {
74
- list = [fn]
78
+ list = [fn];
75
79
  } else {
76
- dir = dir.replace(/\/[^/]+$/, '/')
80
+ dir = dir.replace(/\/[^/]+$/, "/");
77
81
  if (await this.isDir(dir)) {
78
- list = await this.mapDir(dir)
82
+ list = await this.mapDir(dir);
79
83
  }
80
- fn = '^' + fn.replace(/\?/g, '.{1}').replace(/\*/g, '.*')
81
- + (forAutoComplete ? '' : '(|\\/)$')
82
- let re = RegExp(fn)
83
- list = list.filter(e => {
84
- return re.test(e)
85
- })
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
+ });
86
92
  }
87
93
  }
88
94
  }
89
- return [isDir, list]
95
+ return [isDir, list];
90
96
  }
91
97
 
92
98
  async isDir(dir) {
93
99
  if (await fs.pathExists(dir)) {
94
- return (await fs.lstat(dir)).isDirectory()
100
+ return (await fs.lstat(dir)).isDirectory();
95
101
  }
96
- return false
102
+ return false;
97
103
  }
98
104
 
99
105
  async isFile(fn) {
100
106
  if (await fs.pathExists(fn)) {
101
- return (await fs.lstat(fn)).isFile()
107
+ return (await fs.lstat(fn)).isFile();
102
108
  }
103
- return false
109
+ return false;
104
110
  }
105
111
 
106
112
  async getVersionedBasename(p) {
107
- p = this.getNormalizedPath(p)
108
- let dir = path.dirname(p)
109
- let fn = path.basename(p)
110
- let name = fn
111
- let v = 1
112
- for (; ;) {
113
- 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);
114
120
  if (!(await fs.pathExists(filePath))) {
115
- return name
121
+ return name;
116
122
  } else {
117
- name = fn + '.' + (++v)
118
- }
119
- }
120
- }
121
-
122
- shortPublicKey(pk) {
123
- return pk.split('$')[0].substring(0, 16)
124
- }
125
-
126
- encryptFile(content, options, secrez, stringify) {
127
- let key = Crypto.generateKey()
128
- let result = [
129
- '1', //version
130
- Crypto.encrypt(content, key)
131
- ]
132
- if (options.password) {
133
- result[2] = Crypto.encrypt(key, this.getUint8ArrayPassword(options.password))
134
- } else if (options.publicKeys) {
135
- if (!secrez || !secrez.encryptSharedData) {
136
- throw new Error('A secrez instance is required to encrypt using shared keys')
137
- }
138
- result[2] = this.shortPublicKey(secrez.getPublicKey())
139
- for (let publicKey of options.publicKeys) {
140
- let shortPublicKey = this.shortPublicKey(publicKey)
141
- let encKey = secrez.encryptSharedData(key, publicKey)
142
- result.push(shortPublicKey + encKey)
123
+ name = fn + "." + ++v;
143
124
  }
144
125
  }
145
- return stringify ? result.join(',') : result
146
- }
147
-
148
- getUint8ArrayPassword(password) {
149
- return Crypto.bufferToUint8Array(Crypto.SHA3(password))
150
126
  }
151
-
152
- decryptFile(encryptedContent, options, secrez, contactsPKs, returnUint8Array) {
153
- if (typeof encryptedContent === 'string') {
154
- encryptedContent = encryptedContent.split(',')
155
- }
156
- const [version, content, passwordOrPK, ...keys] = encryptedContent
157
- if (version === '1') {
158
- if (keys && keys.length) {
159
- if (!secrez || !secrez.encryptSharedData) {
160
- throw new Error('A secrez instance is required to encrypt using shared keys')
161
- }
162
- if (!contactsPKs) {
163
- throw new Error('A list of contacts is required to encrypt using shared keys')
164
- }
165
- let contactPublicKey
166
- for (let pk of contactsPKs) {
167
- if (this.shortPublicKey(pk) === passwordOrPK) {
168
- contactPublicKey = pk
169
- break
170
- }
171
- }
172
- if (!contactPublicKey) {
173
- throw new Error('Sender\'s public key not in your contacts')
174
- }
175
- let shortPublicKey = this.shortPublicKey(secrez.getPublicKey())
176
- let key
177
- for (let item of keys) {
178
- let short = item.substring(0, 16)
179
- item = item.substring(16)
180
- if (shortPublicKey === short) {
181
- key = secrez.decryptSharedData(item, contactPublicKey, true)
182
- }
183
- }
184
- if (!key) {
185
- throw new Error('The sender didn\'t encrypt the data for you')
186
- }
187
- return Crypto.decrypt(content, key)
188
- } else {
189
- if (!options.password) {
190
- throw new Error('A password is required')
191
- }
192
- let key = Crypto.decrypt(passwordOrPK, this.getUint8ArrayPassword(options.password))
193
- return Crypto.decrypt(content, key, returnUint8Array)
194
- }
195
- } else {
196
- throw new Error('Unsupported version')
197
- }
198
- }
199
-
200
127
  }
201
128
 
202
- module.exports = ExternalFs
129
+ module.exports = ExternalFs;
@@ -0,0 +1,99 @@
1
+ const Crypto = require("@secrez/crypto");
2
+
3
+ class FileCipher {
4
+ constructor(secrez) {
5
+ if (secrez.constructor.name === "Secrez") {
6
+ this.secrez = secrez;
7
+ } else {
8
+ throw new Error(
9
+ "FileCipher requires a Secrez instance during construction"
10
+ );
11
+ }
12
+ }
13
+
14
+ shortPublicKey(pk) {
15
+ return pk.split("$")[0].substring(0, 16);
16
+ }
17
+
18
+ getUint8ArrayPassword(password) {
19
+ return Crypto.bufferToUint8Array(Crypto.SHA3(password));
20
+ }
21
+
22
+ encryptFile(content, options) {
23
+ let key = Crypto.generateKey();
24
+ let result = [
25
+ "1", //version
26
+ Crypto.encrypt(content, key),
27
+ ];
28
+ if (options.password) {
29
+ result[2] = Crypto.encrypt(
30
+ key,
31
+ this.getUint8ArrayPassword(options.password)
32
+ );
33
+ } else {
34
+ const myShort = this.shortPublicKey(this.secrez.getPublicKey());
35
+ result[2] = myShort;
36
+ if (options.publicKeys) {
37
+ for (let publicKey of options.publicKeys) {
38
+ let short = this.shortPublicKey(publicKey);
39
+ let encKey =
40
+ short === myShort
41
+ ? this.secrez.encryptData(key) // encrypted for themself
42
+ : this.secrez.encryptSharedData(key, publicKey);
43
+ result.push(short + encKey);
44
+ }
45
+ }
46
+ }
47
+ return result;
48
+ }
49
+
50
+ decryptFile(encryptedContent, options) {
51
+ if (typeof encryptedContent === "string") {
52
+ encryptedContent = encryptedContent.split(",");
53
+ }
54
+ const [version, content, passwordOrShortPublicKey, ...keys] =
55
+ encryptedContent;
56
+ if (version === "1") {
57
+ if (keys && keys.length) {
58
+ let myShort = this.shortPublicKey(this.secrez.getPublicKey());
59
+ let contactPublicKey = options.contactPublicKey;
60
+ if (!contactPublicKey) {
61
+ for (let pk of options.contactsPublicKeys || []) {
62
+ if (this.shortPublicKey(pk) === passwordOrShortPublicKey) {
63
+ contactPublicKey = pk;
64
+ break;
65
+ }
66
+ }
67
+ }
68
+ let key;
69
+ for (let item of keys) {
70
+ let short = item.substring(0, 16);
71
+ item = item.substring(16);
72
+ if (myShort === short) {
73
+ key =
74
+ myShort === passwordOrShortPublicKey
75
+ ? this.secrez.decryptData(item)
76
+ : this.secrez.decryptSharedData(item, contactPublicKey, true);
77
+ }
78
+ }
79
+ if (!key) {
80
+ throw new Error("The sender didn't encrypt the data for you");
81
+ }
82
+ return Crypto.decrypt(content, key);
83
+ } else {
84
+ if (!options.password) {
85
+ throw new Error("A password is required");
86
+ }
87
+ let key = Crypto.decrypt(
88
+ passwordOrShortPublicKey,
89
+ this.getUint8ArrayPassword(options.password)
90
+ );
91
+ return Crypto.decrypt(content, key, options.returnUint8Array);
92
+ }
93
+ } else {
94
+ throw new Error("Unsupported version");
95
+ }
96
+ }
97
+ }
98
+
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;