secrez 1.1.1 → 1.1.2

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.
Files changed (68) hide show
  1. package/README.md +414 -291
  2. package/bin/secrez.js +50 -47
  3. package/coverage.report +63 -305
  4. package/package.json +10 -12
  5. package/src/Command.js +78 -57
  6. package/src/PreCommand.js +74 -70
  7. package/src/Welcome.js +144 -134
  8. package/src/cliConfig.js +14 -14
  9. package/src/commands/Alias.js +123 -100
  10. package/src/commands/Bash.js +10 -12
  11. package/src/commands/Cat.js +117 -107
  12. package/src/commands/Cd.js +39 -42
  13. package/src/commands/Chat.js +75 -63
  14. package/src/commands/Conf.js +123 -99
  15. package/src/commands/Contacts.js +189 -171
  16. package/src/commands/Copy.js +132 -113
  17. package/src/commands/Courier.js +123 -105
  18. package/src/commands/Ds.js +88 -76
  19. package/src/commands/Edit.js +122 -103
  20. package/src/commands/Export.js +153 -114
  21. package/src/commands/Find.js +115 -110
  22. package/src/commands/Help.js +20 -23
  23. package/src/commands/Import.js +296 -225
  24. package/src/commands/Lcat.js +36 -39
  25. package/src/commands/Lcd.js +38 -39
  26. package/src/commands/Lls.js +58 -55
  27. package/src/commands/Lpwd.js +20 -24
  28. package/src/commands/Ls.js +107 -97
  29. package/src/commands/Mkdir.js +35 -38
  30. package/src/commands/Mv.js +147 -114
  31. package/src/commands/Paste.js +68 -65
  32. package/src/commands/Pwd.js +18 -23
  33. package/src/commands/Quit.js +22 -24
  34. package/src/commands/Rm.js +78 -70
  35. package/src/commands/Shell.js +31 -32
  36. package/src/commands/Ssh.js +77 -63
  37. package/src/commands/Tag.js +133 -112
  38. package/src/commands/Totp.js +166 -136
  39. package/src/commands/Touch.js +169 -56
  40. package/src/commands/Use.js +44 -41
  41. package/src/commands/Ver.js +16 -18
  42. package/src/commands/Whoami.js +34 -37
  43. package/src/commands/chat/Contacts.js +41 -44
  44. package/src/commands/chat/Help.js +20 -23
  45. package/src/commands/chat/Join.js +59 -55
  46. package/src/commands/chat/Leave.js +16 -22
  47. package/src/commands/chat/Quit.js +19 -24
  48. package/src/commands/chat/Send.js +58 -57
  49. package/src/commands/chat/Show.js +60 -51
  50. package/src/commands/chat/Whoami.js +18 -22
  51. package/src/commands/index.js +20 -22
  52. package/src/index.js +3 -3
  53. package/src/prompts/ChatPrompt.js +87 -82
  54. package/src/prompts/ChatPromptMock.js +11 -17
  55. package/src/prompts/CommandPrompt.js +146 -138
  56. package/src/prompts/Completion.js +64 -69
  57. package/src/prompts/MainPrompt.js +84 -77
  58. package/src/prompts/MainPromptMock.js +19 -30
  59. package/src/prompts/MultiEditorPrompt.js +21 -22
  60. package/src/prompts/SigintManager.js +21 -24
  61. package/src/utils/AliasManager.js +16 -18
  62. package/src/utils/ContactManager.js +15 -17
  63. package/src/utils/Fido2Client.js +59 -49
  64. package/src/utils/HelpProto.js +130 -117
  65. package/src/utils/Logger.js +48 -50
  66. package/.eslintignore +0 -0
  67. package/.eslintrc +0 -33
  68. package/.jshintrc +0 -3
@@ -1,207 +1,225 @@
1
- const {sleep} = require('@secrez/utils')
2
- const {ConfigUtils} = require('@secrez/core')
3
-
4
- class Conf extends require('../Command') {
1
+ const { sleep } = require("@secrez/utils");
2
+ const { ConfigUtils } = require("@secrez/core");
5
3
 
4
+ class Conf extends require("../Command") {
6
5
  setHelpAndCompletion() {
7
6
  this.cliConfig.completion.courier = {
8
- _self: this
9
- }
10
- this.cliConfig.completion.help.courier = true
7
+ _self: this,
8
+ };
9
+ this.cliConfig.completion.help.courier = true;
11
10
  this.optionDefinitions = [
12
11
  {
13
- name: 'help',
14
- alias: 'h',
15
- type: Boolean
16
- }
17
- ]
12
+ name: "help",
13
+ alias: "h",
14
+ type: Boolean,
15
+ },
16
+ ];
18
17
  }
19
18
 
20
19
  help() {
21
20
  return {
22
- description: ['Configure the connection to a local courier'],
23
- examples: [
24
- 'courier'
25
- ]
26
- }
21
+ description: ["Configure the connection to a local courier"],
22
+ examples: ["courier"],
23
+ };
27
24
  }
28
25
 
29
26
  async publishToHubIfNotYet(options) {
30
27
  try {
31
- const {port, caCrt} = options.env.courier
32
- let id = options.env.courier.tunnel ? options.env.courier.tunnel.clientId : 0
33
- this.prompt.loadingMessage = 'Publishing the courier'
34
- this.prompt.loading()
35
- let res = await this.callCourier({
36
- action: {
37
- name: 'publish'
38
- },
39
- id
28
+ const { port, caCrt } = options.env.courier;
29
+ let id = options.env.courier.tunnel
30
+ ? options.env.courier.tunnel.clientId
31
+ : 0;
32
+ this.prompt.loadingMessage = "Publishing the courier";
33
+ this.prompt.loading();
34
+ let res = await this.callCourier(
35
+ {
36
+ action: {
37
+ name: "publish",
40
38
  },
41
- port,
42
- caCrt,
43
- '/admin'
44
- )
45
- this.prompt.showLoading = false
46
- await sleep(100)
39
+ id,
40
+ },
41
+ port,
42
+ caCrt,
43
+ "/admin"
44
+ );
45
+ this.prompt.showLoading = false;
46
+ await sleep(100);
47
47
  try {
48
- process.stdout.clearLine()
49
- } catch(e) {
48
+ process.stdout.clearLine();
49
+ } catch (e) {
50
50
  // most likely we are running workspace testing
51
51
  }
52
52
  if (res.info.error) {
53
- throw new Error(`The connection to the hub ${res.info.hub} is refused. Verify that your Secrez Courier is connecting to an active hub, please, and try again.`)
53
+ throw new Error(
54
+ `The connection to the hub ${res.info.hub} is refused. Verify that your Secrez Courier is connecting to an active hub, please, and try again.`
55
+ );
54
56
  } else {
55
- return res.info
57
+ return res.info;
56
58
  }
57
59
  } catch (e) {
58
60
  // console.error(e)
59
- throw new Error(e.message)
61
+ throw new Error(e.message);
60
62
  }
61
63
  }
62
64
 
63
65
  async isCourierReady(options) {
64
66
  if (!options.env) {
65
- options.env = await ConfigUtils.getEnv(this.secrez.config)
67
+ options.env = await ConfigUtils.getEnv(this.secrez.config);
66
68
  }
67
69
  if (options.env.courier) {
68
- const {port, caCrt} = options.env.courier
69
- return await this.callCourier({action: {name: 'ready'}}, port, caCrt, '/admin')
70
+ const { port, caCrt } = options.env.courier;
71
+ return await this.callCourier(
72
+ { action: { name: "ready" } },
73
+ port,
74
+ caCrt,
75
+ "/admin"
76
+ );
70
77
  } else {
71
- throw new Error('No courier set up yet.')
78
+ throw new Error("No courier set up yet.");
72
79
  }
73
80
  }
74
81
 
75
82
  async getRecentMessages(options = {}) {
76
- const env = await ConfigUtils.getEnv(this.secrez.config)
83
+ const env = await ConfigUtils.getEnv(this.secrez.config);
77
84
  if (!env.messages) {
78
- env.messages = {}
85
+ env.messages = {};
79
86
  }
80
- this.lastRead = env.messages.lastRead || 0
87
+ this.lastRead = env.messages.lastRead || 0;
81
88
  if (env.courier) {
82
- const {port, caCrt} = env.courier
83
- const payload = Object.assign({
84
- minTimestamp: this.lastRead
85
- }, options)
86
- let messages = await this.callCourier(payload, port, caCrt, '/messages')
89
+ const { port, caCrt } = env.courier;
90
+ const payload = Object.assign(
91
+ {
92
+ minTimestamp: this.lastRead,
93
+ },
94
+ options
95
+ );
96
+ let messages = await this.callCourier(payload, port, caCrt, "/messages");
87
97
  for (let message of messages.result) {
88
- this.lastRead = Math.max(this.lastRead, message.timestamp + 1)
98
+ this.lastRead = Math.max(this.lastRead, message.timestamp + 1);
89
99
  }
90
- env.messages.lastRead = this.lastRead
91
- await ConfigUtils.putEnv(this.secrez.config, env)
92
- return messages.result.map(e => this.decryptMessage(e))
100
+ env.messages.lastRead = this.lastRead;
101
+ await ConfigUtils.putEnv(this.secrez.config, env);
102
+ return messages.result.map((e) => this.decryptMessage(e));
93
103
  } else {
94
- throw new Error('No courier set up yet.')
104
+ throw new Error("No courier set up yet.");
95
105
  }
96
106
  }
97
107
 
98
108
  async getSomeMessages(options = {}) {
99
- const env = await ConfigUtils.getEnv(this.secrez.config)
109
+ const env = await ConfigUtils.getEnv(this.secrez.config);
100
110
  if (env.courier) {
101
- const {port, caCrt} = env.courier
102
- const payload = options.payload
103
- let messages = await this.callCourier(payload, port, caCrt, '/messages')
104
- return messages.result.map(e => this.decryptMessage(e))
111
+ const { port, caCrt } = env.courier;
112
+ const payload = options.payload;
113
+ let messages = await this.callCourier(payload, port, caCrt, "/messages");
114
+ return messages.result.map((e) => this.decryptMessage(e));
105
115
  } else {
106
- throw new Error('No courier set up yet.')
116
+ throw new Error("No courier set up yet.");
107
117
  }
108
118
  }
109
119
 
110
120
  decryptMessage(message) {
111
- message.decrypted = this.secrez.decryptSharedData(JSON.parse(message.payload).message, message.publickey)
112
- return message
121
+ message.decrypted = this.secrez.decryptSharedData(
122
+ JSON.parse(message.payload).message,
123
+ message.publickey
124
+ );
125
+ return message;
113
126
  }
114
127
 
115
128
  async preInit(options) {
116
- const env = options.env
117
- let ready
129
+ const env = options.env;
130
+ let ready;
118
131
  if (env.courier && env.courier.port) {
119
- let body = await this.isCourierReady(options)
120
- ready = body.success
132
+ let body = await this.isCourierReady(options);
133
+ ready = body.success;
121
134
  if (ready) {
122
- env.courier.caCrt = body.caCrt
135
+ env.courier.caCrt = body.caCrt;
123
136
  if (!body.tunnel.url) {
124
- body.tunnel = await this.publishToHubIfNotYet(options)
137
+ body.tunnel = await this.publishToHubIfNotYet(options);
125
138
  }
126
- env.courier.tunnel = body.tunnel
127
- await ConfigUtils.putEnv(this.secrez.config, env)
128
- options.ready = true
139
+ env.courier.tunnel = body.tunnel;
140
+ await ConfigUtils.putEnv(this.secrez.config, env);
141
+ options.ready = true;
129
142
  }
130
143
  }
131
144
  }
132
145
 
133
146
  async initCourier(options) {
134
- const env = options.env = await ConfigUtils.getEnv(this.secrez.config)
135
- await this.preInit(options)
136
- if (process.env.NODE_ENV === 'test') {
137
- return await this.setUpCorier(options)
147
+ const env = (options.env = await ConfigUtils.getEnv(this.secrez.config));
148
+ await this.preInit(options);
149
+ if (process.env.NODE_ENV === "test") {
150
+ return await this.setUpCorier(options);
138
151
  }
139
152
  if (options.ready) {
140
- this.Logger.reset(`A courier is already set and is listening on port ${env.courier.port}`)
153
+ this.Logger.reset(
154
+ `A courier is already set and is listening on port ${env.courier.port}`
155
+ );
141
156
  } else {
142
157
  let yes = await this.useConfirm({
143
- message: 'No Secrez Courier configured yet.\nDid you launch one?',
144
- default: true
145
- })
158
+ message: "No Secrez Courier configured yet.\nDid you launch one?",
159
+ default: true,
160
+ });
146
161
  if (yes) {
147
162
  options.port = await this.useInput({
148
- message: 'Which port the courier is listening to?'
149
- })
163
+ message: "Which port the courier is listening to?",
164
+ });
150
165
  if (options.port) {
151
- await this.setUpCorier(options)
166
+ await this.setUpCorier(options);
152
167
  } else {
153
- this.Logger.grey('Operation canceled')
168
+ this.Logger.grey("Operation canceled");
154
169
  }
155
170
  } else {
156
- this.Logger.grey('Operation canceled')
171
+ this.Logger.grey("Operation canceled");
157
172
  }
158
173
  }
159
174
  }
160
175
 
161
176
  async setUpCorier(options) {
162
- const port = options.port
177
+ const port = options.port;
163
178
  try {
164
- const body = await this.callCourier({action: {name: 'ready'}}, port, undefined, '/admin')
179
+ const body = await this.callCourier(
180
+ { action: { name: "ready" } },
181
+ port,
182
+ undefined,
183
+ "/admin"
184
+ );
165
185
  if (body.success) {
166
- let previousTunnel
186
+ let previousTunnel;
167
187
  if (options.env.courier && options.env.courier.port === port) {
168
- previousTunnel = options.env.courier.tunnel
188
+ previousTunnel = options.env.courier.tunnel;
169
189
  }
170
190
  options.env.courier = {
171
191
  port,
172
192
  caCrt: body.caCrt,
173
- tunnel: previousTunnel
174
- }
175
- body.tunnel = await this.publishToHubIfNotYet(options)
176
- options.env.courier.tunnel = body.tunnel
177
- await ConfigUtils.putEnv(this.secrez.config, options.env)
178
- this.Logger.reset('Connected')
193
+ tunnel: previousTunnel,
194
+ };
195
+ body.tunnel = await this.publishToHubIfNotYet(options);
196
+ options.env.courier.tunnel = body.tunnel;
197
+ await ConfigUtils.putEnv(this.secrez.config, options.env);
198
+ this.Logger.reset("Connected");
179
199
  } else {
180
- throw new Error('Courier not found')
200
+ throw new Error("Courier not found");
181
201
  }
182
- } catch(e) {
183
- throw new Error('Courier not available')
202
+ } catch (e) {
203
+ throw new Error("Courier not available");
184
204
  }
185
205
  }
186
206
 
187
207
  async courier(options) {
188
- return this.initCourier(options)
208
+ return this.initCourier(options);
189
209
  }
190
210
 
191
211
  async exec(options = {}) {
192
212
  if (options.help) {
193
- return this.showHelp()
213
+ return this.showHelp();
194
214
  }
195
215
  try {
196
- await this.courier(options)
216
+ await this.courier(options);
197
217
  } catch (e) {
198
218
  // console.error(e)
199
- this.Logger.red(e.message)
219
+ this.Logger.red(e.message);
200
220
  }
201
- await this.prompt.run()
221
+ await this.prompt.run();
202
222
  }
203
223
  }
204
224
 
205
- module.exports = Conf
206
-
207
-
225
+ module.exports = Conf;
@@ -1,152 +1,164 @@
1
- class Ds extends require('../Command') {
2
-
1
+ class Ds extends require("../Command") {
3
2
  setHelpAndCompletion() {
4
3
  this.cliConfig.completion.ds = {
5
4
  _func: this.selfCompletion(this),
6
- _self: this
7
- }
8
- this.cliConfig.completion.help.ds = true
5
+ _self: this,
6
+ };
7
+ this.cliConfig.completion.help.ds = true;
9
8
  this.optionDefinitions = [
10
9
  {
11
- name: 'help',
12
- alias: 'h',
13
- type: Boolean
10
+ name: "help",
11
+ alias: "h",
12
+ type: Boolean,
14
13
  },
15
14
  {
16
- name: 'list',
17
- alias: 'l',
18
- type: Boolean
15
+ name: "list",
16
+ alias: "l",
17
+ type: Boolean,
19
18
  },
20
19
  {
21
- name: 'create',
22
- alias: 'c',
23
- type: String
20
+ name: "create",
21
+ alias: "c",
22
+ type: String,
24
23
  },
25
24
  {
26
- name: 'rename',
27
- alias: 'r',
25
+ name: "rename",
26
+ alias: "r",
28
27
  multiple: true,
29
- type: String
28
+ type: String,
30
29
  },
31
30
  {
32
- name: 'delete',
33
- alias: 'd',
34
- type: String
35
- }
36
- ]
31
+ name: "delete",
32
+ alias: "d",
33
+ type: String,
34
+ },
35
+ ];
37
36
  }
38
37
 
39
38
  help() {
40
39
  return {
41
- description: ['Manages datasets'],
40
+ description: ["Manages datasets"],
42
41
  examples: [
43
- ['ds -l', 'lists all datasets'],
44
- ['ds -c archive', 'create the dataset named "archive";', 'if the dataset does not exists it creates it'],
45
- ['ds -r archive unused', 'if the "archive" dataset exists, renames it "unused"'],
46
- ['ds -d cryptos', 'deletes the dataset "cryptos" if it exists']
47
-
48
- ]
49
- }
42
+ ["ds -l", "lists all datasets"],
43
+ [
44
+ "ds -c archive",
45
+ 'create the dataset named "archive";',
46
+ "if the dataset does not exists it creates it",
47
+ ],
48
+ [
49
+ "ds -r archive unused",
50
+ 'if the "archive" dataset exists, renames it "unused"',
51
+ ],
52
+ ["ds -d cryptos", 'deletes the dataset "cryptos" if it exists'],
53
+ ],
54
+ };
50
55
  }
51
56
 
52
57
  async exists(set) {
53
- let datasetsInfo = await this.internalFs.getDatasetsInfo()
58
+ let datasetsInfo = await this.internalFs.getDatasetsInfo();
54
59
  for (let dataset of datasetsInfo) {
55
60
  if (dataset.name.toLowerCase() === set.toLowerCase()) {
56
- return dataset.index
61
+ return dataset.index;
57
62
  }
58
63
  }
59
- return -1
64
+ return -1;
60
65
  }
61
66
 
62
67
  async ds(options) {
63
- let datasetsInfo = await this.internalFs.getDatasetsInfo()
68
+ let datasetsInfo = await this.internalFs.getDatasetsInfo();
64
69
  if (options.rename) {
65
- let name = options.rename[0]
66
- let newName = options.rename[1]
67
- this.internalFs.tree.validateDatasetName(newName)
68
- let index = await this.exists(name)
70
+ let name = options.rename[0];
71
+ let newName = options.rename[1];
72
+ this.internalFs.tree.validateDatasetName(newName);
73
+ let index = await this.exists(name);
69
74
  if (index >= 0) {
70
- if (['main', 'trash'].includes(name)) {
71
- throw new Error('main and trash cannot be renamed')
75
+ if (["main", "trash"].includes(name)) {
76
+ throw new Error("main and trash cannot be renamed");
72
77
  }
73
78
  if ((await this.exists(newName)) >= 0) {
74
- throw new Error(`A dataset named ${newName} already exists`)
79
+ throw new Error(`A dataset named ${newName} already exists`);
75
80
  }
76
- await this.internalFs.trees[index].nameDataset(newName)
77
- this.internalFs.updateTreeCache(index, newName)
78
- return `The dataset ${name} has been renamed ${newName}`
81
+ await this.internalFs.trees[index].nameDataset(newName);
82
+ this.internalFs.updateTreeCache(index, newName);
83
+ return `The dataset ${name} has been renamed ${newName}`;
79
84
  }
80
85
  } else if (options.create) {
81
- let newName = options.create
86
+ let newName = options.create;
82
87
  if ((await this.exists(newName)) >= 0) {
83
- throw new Error(`A dataset named ${newName} already exists`)
88
+ throw new Error(`A dataset named ${newName} already exists`);
84
89
  }
85
- this.internalFs.tree.validateDatasetName(newName)
86
- let index = datasetsInfo[datasetsInfo.length - 1].index + 1
87
- await this.internalFs.mountTree(index, true)
88
- await this.internalFs.tree.nameDataset(newName)
89
- return `The dataset ${newName} has been created`
90
+ this.internalFs.tree.validateDatasetName(newName);
91
+ let index = datasetsInfo[datasetsInfo.length - 1].index + 1;
92
+ await this.internalFs.mountTree(index, true);
93
+ await this.internalFs.tree.nameDataset(newName);
94
+ return `The dataset ${newName} has been created`;
90
95
  } else if (options.delete) {
91
- let name = options.delete
92
- let index = await this.exists(name)
96
+ let name = options.delete;
97
+ let index = await this.exists(name);
93
98
  if (index < 0) {
94
- throw new Error(`A dataset named ${name} does not exist`)
99
+ throw new Error(`A dataset named ${name} does not exist`);
95
100
  }
96
101
  if (index === this.internalFs.treeIndex) {
97
- return 'You can not delete the active dataset'
102
+ return "You can not delete the active dataset";
98
103
  }
99
104
  let list = await this.prompt.commands.ls.ls({
100
- path: `${name}:/`
101
- })
102
- if (list.length > 0 && process.env.NODE_ENV !== 'test') {
105
+ path: `${name}:/`,
106
+ });
107
+ if (list.length > 0 && process.env.NODE_ENV !== "test") {
103
108
  let yes = await this.useConfirm({
104
109
  message: `The dataset ${name} contains files. Are you sure you want to delete it?`,
105
- default: false
106
- })
110
+ default: false,
111
+ });
107
112
  if (!yes) {
108
- return 'Operation canceled'
113
+ return "Operation canceled";
109
114
  }
110
115
  }
111
116
  for (let file of list) {
112
117
  await this.prompt.commands.mv.mv({
113
118
  path: `${name}:/${file}`,
114
- newPath: 'trash:/'
115
- })
119
+ newPath: "trash:/",
120
+ });
116
121
  }
117
122
  if (this.internalFs.deleteDataset(index)) {
118
- return `The dataset ${name} has been canceled. Its content has been moved to the "trash" dataset`
123
+ return `The dataset ${name} has been canceled. Its content has been moved to the "trash" dataset`;
119
124
  } else {
120
- throw new Error('Unknown error')
125
+ throw new Error("Unknown error");
121
126
  }
122
127
  } else if (options.list) {
123
- return (await this.internalFs.getDatasetsInfo()).map(e => e.name)
128
+ return (await this.internalFs.getDatasetsInfo()).map((e) => e.name);
124
129
  }
125
- throw new Error('Wrong parameters')
130
+ throw new Error("Wrong parameters");
126
131
  }
127
132
 
128
133
  async exec(options = {}) {
129
134
  if (options.help) {
130
- return this.showHelp()
135
+ return this.showHelp();
131
136
  }
132
137
  try {
133
138
  if (!Object.keys(options).length) {
134
- options.list = true
139
+ options.list = true;
135
140
  }
136
- this.validate(options)
137
- let result = await this.ds(options)
141
+ this.validate(options);
142
+ let result = await this.ds(options);
138
143
  if (result) {
139
144
  if (options.list) {
140
- this.Logger.reset(this.prompt.commandPrompt.formatList(result, 26, true, this.threeRedDots()))
145
+ this.Logger.reset(
146
+ this.prompt.commandPrompt.formatList(
147
+ result,
148
+ 26,
149
+ true,
150
+ this.threeRedDots()
151
+ )
152
+ );
141
153
  } else {
142
- this.Logger.reset(result)
154
+ this.Logger.reset(result);
143
155
  }
144
156
  }
145
157
  } catch (e) {
146
- this.Logger.red(e.message)
158
+ this.Logger.red(e.message);
147
159
  }
148
- await this.prompt.run()
160
+ await this.prompt.run();
149
161
  }
150
162
  }
151
163
 
152
- module.exports = Ds
164
+ module.exports = Ds;