cloudcmd 17.1.5 → 17.2.0

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.
@@ -1,40 +1,37 @@
1
- 'use strict';
1
+ import path, {dirname, join} from 'node:path';
2
+ import {fileURLToPath} from 'node:url';
3
+ import process from 'node:process';
4
+ import fs from 'node:fs';
5
+ import fullstore from 'fullstore';
6
+ import currify from 'currify';
7
+ import apart from 'apart';
8
+ import ponse from 'ponse';
9
+ import restafary from 'restafary';
10
+ import restbox from 'restbox';
11
+ import konsole from 'console-io';
12
+ import edward from 'edward';
13
+ import dword from 'dword';
14
+ import deepword from 'deepword';
15
+ import nomine from 'nomine';
16
+ import fileop from '@cloudcmd/fileop';
17
+ import cloudfunc from '../common/cloudfunc.js';
18
+ import authentication from './auth.js';
19
+ import {createConfig, configPath} from './config.js';
20
+ import modulas from './modulas.js';
21
+ import userMenu from './user-menu.mjs';
22
+ import rest from './rest/index.js';
23
+ import route from './route.js';
24
+ import validate from './validate.js';
25
+ import prefixer from './prefixer.js';
26
+ import terminal from './terminal.js';
27
+ import distribute from './distribute/index.js';
28
+ import {createDepStore} from './depstore.js';
2
29
 
3
- const fullstore = require('fullstore');
4
- const process = require('node:process');
30
+ const __filename = fileURLToPath(import.meta.url);
31
+ const __dirname = dirname(__filename);
32
+ const {assign} = Object;
5
33
  const DIR = `${__dirname}/`;
6
- const DIR_COMMON = `${DIR}../common/`;
7
- const path = require('node:path');
8
-
9
- const fs = require('node:fs');
10
- const cloudfunc = require(`${DIR_COMMON}cloudfunc`);
11
-
12
- const authentication = require(`${DIR}auth`);
13
- const {createConfig, configPath} = require(`${DIR}config`);
14
-
15
- const modulas = require(`${DIR}modulas`);
16
-
17
- const userMenu = require(`${DIR}user-menu`);
18
- const rest = require(`${DIR}rest`);
19
- const route = require(`${DIR}route`);
20
- const validate = require(`${DIR}validate`);
21
- const prefixer = require(`${DIR}prefixer`);
22
- const terminal = require(`${DIR}terminal`);
23
- const distribute = require(`${DIR}distribute`);
24
- const currify = require('currify');
25
-
26
- const apart = require('apart');
27
- const ponse = require('ponse');
28
- const restafary = require('restafary');
29
- const restbox = require('restbox');
30
- const konsole = require('console-io');
31
- const edward = require('edward');
32
- const dword = require('dword');
33
- const deepword = require('deepword');
34
- const nomine = require('nomine');
35
- const fileop = require('@cloudcmd/fileop');
36
- const DIR_ROOT = `${DIR}../`;
37
-
34
+ const DIR_ROOT = join(DIR, '..');
38
35
  const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
39
36
 
40
37
  const isDev = fullstore(process.env.NODE_ENV === 'development');
@@ -49,7 +46,9 @@ const clean = (a) => a.filter(notEmpty);
49
46
  const isUndefined = (a) => typeof a === 'undefined';
50
47
  const isFn = (a) => typeof a === 'function';
51
48
 
52
- module.exports = (params) => {
49
+ export default cloudcmd;
50
+
51
+ function cloudcmd(params) {
53
52
  const p = params || {};
54
53
  const options = p.config || {};
55
54
  const config = p.configManager || createConfig({
@@ -57,7 +56,6 @@ module.exports = (params) => {
57
56
  });
58
57
 
59
58
  const {modules} = p;
60
-
61
59
  const keys = Object.keys(options);
62
60
 
63
61
  for (const name of keys) {
@@ -87,16 +85,20 @@ module.exports = (params) => {
87
85
  socket: p.socket,
88
86
  });
89
87
 
90
- return cloudcmd({
88
+ return cloudcmdMiddle({
91
89
  modules,
92
90
  config,
93
91
  });
94
- };
92
+ }
93
+
94
+ const depStore = createDepStore();
95
95
 
96
- module.exports.createConfigManager = createConfig;
97
- module.exports.configPath = configPath;
96
+ export const createConfigManager = createConfig;
97
+ export {
98
+ configPath,
99
+ };
98
100
 
99
- module.exports._getIndexPath = getIndexPath;
101
+ export const _getIndexPath = getIndexPath;
100
102
 
101
103
  function defaultValue(config, name, options) {
102
104
  const value = options[name];
@@ -108,7 +110,8 @@ function defaultValue(config, name, options) {
108
110
  return value;
109
111
  }
110
112
 
111
- module.exports._getPrefix = getPrefix;
113
+ export const _getPrefix = getPrefix;
114
+
112
115
  function getPrefix(prefix) {
113
116
  if (isFn(prefix))
114
117
  return prefix() || '';
@@ -116,8 +119,7 @@ function getPrefix(prefix) {
116
119
  return prefix || '';
117
120
  }
118
121
 
119
- module.exports._initAuth = _initAuth;
120
- function _initAuth(config, accept, reject, username, password) {
122
+ export function _initAuth(config, accept, reject, username, password) {
121
123
  if (!config('auth'))
122
124
  return accept();
123
125
 
@@ -176,7 +178,7 @@ function listen({prefixSocket, socket, config}) {
176
178
  distribute.export(config, socket);
177
179
  }
178
180
 
179
- function cloudcmd({modules, config}) {
181
+ function cloudcmdMiddle({modules, config}) {
180
182
  const online = apart(config, 'online');
181
183
  const cache = false;
182
184
  const diff = apart(config, 'diff');
@@ -240,9 +242,14 @@ function cloudcmd({modules, config}) {
240
242
  userMenu({
241
243
  menuName: '.cloudcmd.menu.js',
242
244
  }),
243
- rest(config),
245
+ rest({
246
+ config,
247
+ fs: depStore('fs'),
248
+ moveFiles: depStore('moveFiles'),
249
+ }),
244
250
  route(config, {
245
251
  html,
252
+ win32: depStore('win32'),
246
253
  }),
247
254
  ponseStatic,
248
255
  ]);
@@ -257,8 +264,9 @@ function logout(req, res, next) {
257
264
  res.sendStatus(401);
258
265
  }
259
266
 
260
- module.exports._isDev = isDev;
261
- module.exports._replaceDist = replaceDist;
267
+ export const _isDev = isDev;
268
+ export const _replaceDist = replaceDist;
269
+
262
270
  function replaceDist(url) {
263
271
  if (!isDev())
264
272
  return url;
@@ -284,3 +292,8 @@ function setSW(req, res, next) {
284
292
 
285
293
  next();
286
294
  }
295
+
296
+ assign(cloudcmd, {
297
+ depStore,
298
+ createConfigManager,
299
+ });
@@ -0,0 +1,180 @@
1
+ import path, {dirname} from 'node:path';
2
+ import {fileURLToPath} from 'node:url';
3
+ import serveOnce from 'serve-once';
4
+ import {test, stub} from 'supertape';
5
+ import cloudcmd, {
6
+ _isDev,
7
+ _replaceDist,
8
+ createConfigManager,
9
+ _getPrefix,
10
+ _initAuth,
11
+ _getIndexPath,
12
+ } from './cloudcmd.mjs';
13
+
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = dirname(__filename);
16
+
17
+ const {request} = serveOnce(cloudcmd, {
18
+ config: {
19
+ auth: false,
20
+ dropbox: false,
21
+ },
22
+ });
23
+
24
+ test('cloudcmd: defaults: config', (t) => {
25
+ const configManager = createConfigManager();
26
+
27
+ configManager('configDialog', false);
28
+
29
+ cloudcmd({
30
+ configManager,
31
+ });
32
+
33
+ t.notOk(configManager('configDialog'), 'should not override config with defaults');
34
+ t.end();
35
+ });
36
+
37
+ test('cloudcmd: defaults: console', (t) => {
38
+ const configManager = createConfigManager();
39
+ configManager('console', false);
40
+
41
+ cloudcmd({
42
+ configManager,
43
+ });
44
+
45
+ t.notOk(configManager('console'), 'should not override config with defaults');
46
+ t.end();
47
+ });
48
+
49
+ test('cloudcmd: getPrefix', (t) => {
50
+ const value = 'hello';
51
+ const result = _getPrefix(value);
52
+
53
+ t.equal(result, value);
54
+ t.end();
55
+ });
56
+
57
+ test('cloudcmd: getPrefix: function', (t) => {
58
+ const value = 'hello';
59
+ const fn = () => value;
60
+ const result = _getPrefix(fn);
61
+
62
+ t.equal(result, value);
63
+ t.end();
64
+ });
65
+
66
+ test('cloudcmd: getPrefix: function: empty', (t) => {
67
+ const value = null;
68
+ const fn = () => value;
69
+ const result = _getPrefix(fn);
70
+
71
+ t.equal(result, '');
72
+ t.end();
73
+ });
74
+
75
+ test('cloudcmd: replaceDist', (t) => {
76
+ const currentIsDev = _isDev();
77
+
78
+ _isDev(true);
79
+ const url = '/dist/hello';
80
+ const result = _replaceDist(url);
81
+ const expected = '/dist-dev/hello';
82
+
83
+ _isDev(currentIsDev);
84
+
85
+ t.equal(result, expected);
86
+ t.end();
87
+ });
88
+
89
+ test('cloudcmd: replaceDist: !isDev', (t) => {
90
+ const url = '/dist/hello';
91
+
92
+ const currentIsDev = _isDev();
93
+ _isDev(false);
94
+ const result = _replaceDist(url);
95
+
96
+ _isDev(currentIsDev);
97
+
98
+ t.equal(result, url);
99
+ t.end();
100
+ });
101
+
102
+ test('cloudcmd: auth: reject', (t) => {
103
+ const accept = stub();
104
+ const reject = stub();
105
+
106
+ const config = createConfigManager();
107
+
108
+ const username = 'root';
109
+ const password = 'toor';
110
+
111
+ config('auth', true);
112
+ config('username', username);
113
+ config('password', password);
114
+
115
+ _initAuth(config, accept, reject, username, 'abc');
116
+
117
+ t.ok(reject.called, 'should reject');
118
+ t.end();
119
+ });
120
+
121
+ test('cloudcmd: auth: accept', (t) => {
122
+ const accept = stub();
123
+ const reject = stub();
124
+
125
+ const username = 'root';
126
+ const password = 'toor';
127
+ const auth = true;
128
+
129
+ const config = createConfigManager();
130
+ config('username', username);
131
+ config('password', password);
132
+ config('auth', auth);
133
+
134
+ _initAuth(config, accept, reject, username, password);
135
+
136
+ t.ok(accept.called, 'should accept');
137
+ t.end();
138
+ });
139
+
140
+ test('cloudcmd: auth: accept: no auth', (t) => {
141
+ const accept = stub();
142
+ const reject = stub();
143
+
144
+ const auth = false;
145
+ const username = 'root';
146
+ const password = 'toor';
147
+
148
+ const config = createConfigManager();
149
+ config('username', username);
150
+ config('password', password);
151
+ config('auth', auth);
152
+
153
+ _initAuth(config, accept, reject, username, password);
154
+
155
+ t.ok(accept.called, 'should accept');
156
+ t.end();
157
+ });
158
+
159
+ test('cloudcmd: getIndexPath: production', (t) => {
160
+ const isDev = false;
161
+ const name = path.join(__dirname, '..', 'dist', 'index.html');
162
+
163
+ t.equal(_getIndexPath(isDev), name);
164
+ t.end();
165
+ });
166
+
167
+ test('cloudcmd: getIndexPath: development', (t) => {
168
+ const isDev = true;
169
+ const name = path.join(__dirname, '..', 'dist-dev', 'index.html');
170
+
171
+ t.equal(_getIndexPath(isDev), name);
172
+ t.end();
173
+ });
174
+
175
+ test('cloudcmd: sw', async (t) => {
176
+ const {status} = await request.get('/sw.js');
177
+
178
+ t.equal(status, 200, 'should return sw');
179
+ t.end();
180
+ });
package/server/config.js CHANGED
@@ -153,7 +153,7 @@ function listen(manage, sock, auth) {
153
153
  .on('connection', (socket) => {
154
154
  if (!manage('auth'))
155
155
  return connection(manage, socket);
156
-
156
+
157
157
  const reject = () => socket.emit('reject');
158
158
  socket.on('auth', auth(connectionWraped(manage, socket), reject));
159
159
  });
@@ -0,0 +1,105 @@
1
+ import {createRequire} from 'node:module';
2
+ import {test, stub} from 'supertape';
3
+ import {createConfig, _cryptoPass} from './config.js';
4
+ import {apiURL} from '../common/cloudfunc.js';
5
+ import {connect} from '../test/before.mjs';
6
+
7
+ const require = createRequire(import.meta.url);
8
+ const fixture = require('./config.fixture.json');
9
+
10
+ const config = createConfig();
11
+
12
+ test('config: manage', (t) => {
13
+ t.equal(undefined, config(), 'should return "undefined"');
14
+ t.end();
15
+ });
16
+
17
+ test('config: manage: get', async (t) => {
18
+ const editor = 'deepword';
19
+ const configManager = createConfig();
20
+
21
+ const {done} = await connect({
22
+ config: {
23
+ editor,
24
+ },
25
+ configManager,
26
+ });
27
+
28
+ done();
29
+
30
+ t.equal(configManager('editor'), editor, 'should get config');
31
+ t.end();
32
+ });
33
+
34
+ test('config: manage: get: config', async (t) => {
35
+ const editor = 'deepword';
36
+ const conf = {
37
+ editor,
38
+ };
39
+
40
+ const {done} = await connect({
41
+ config: conf,
42
+ });
43
+
44
+ config('editor', 'dword');
45
+ done();
46
+
47
+ t.equal('dword', config('editor'), 'should set config');
48
+ t.end();
49
+ });
50
+
51
+ test('config: manage: get: *', (t) => {
52
+ const data = config('*');
53
+ const keys = Object.keys(data);
54
+
55
+ t.ok(keys.length > 1, 'should return config data');
56
+ t.end();
57
+ });
58
+
59
+ test('config: cryptoPass: no password', (t) => {
60
+ const json = {
61
+ hello: 'world',
62
+ };
63
+
64
+ const config = createConfig();
65
+ const result = _cryptoPass(config, json);
66
+
67
+ t.deepEqual(result, [config, json], 'should not change json');
68
+ t.end();
69
+ });
70
+
71
+ test('config: cryptoPass', (t) => {
72
+ const json = {
73
+ password: 'hello',
74
+ };
75
+
76
+ const {password} = fixture;
77
+
78
+ const expected = {
79
+ password,
80
+ };
81
+
82
+ const config = createConfig();
83
+ const result = _cryptoPass(config, json);
84
+
85
+ t.deepEqual(result, [config, expected], 'should crypt password');
86
+ t.end();
87
+ });
88
+
89
+ test('config: middle: no', (t) => {
90
+ const {middle} = config;
91
+ const next = stub();
92
+ const res = null;
93
+ const url = `${apiURL}/config`;
94
+ const method = 'POST';
95
+
96
+ const req = {
97
+ url,
98
+ method,
99
+ };
100
+
101
+ middle(req, res, next);
102
+
103
+ t.calledWithNoArgs(next, 'should call next');
104
+ t.end();
105
+ });
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ module.exports.createDepStore = () => {
4
+ let deps = {};
5
+
6
+ return (name, value) => {
7
+ if (!name)
8
+ return deps = {};
9
+
10
+ if (!value)
11
+ return deps[name];
12
+
13
+ deps[name] = value;
14
+ };
15
+ };
@@ -0,0 +1,68 @@
1
+ import Config from '../config.js';
2
+ import {once} from 'node:events';
3
+ import test from 'supertape';
4
+ import io from 'socket.io-client';
5
+ import {connect} from '../../test/before.mjs';
6
+
7
+ const config = Config.createConfig();
8
+
9
+ test('distribute: export', async (t) => {
10
+ const defaultConfig = {
11
+ export: true,
12
+ exportToken: 'a',
13
+ vim: true,
14
+ log: false,
15
+ prefix: '',
16
+ };
17
+
18
+ const {port, done} = await connect({
19
+ config: defaultConfig,
20
+ configManager: config,
21
+ });
22
+
23
+ const url = `http://localhost:${port}/distribute?port=1111`;
24
+ const socket = io.connect(url);
25
+
26
+ await once(socket, 'connect');
27
+ socket.emit('auth', 'a');
28
+
29
+ await once(socket, 'accept');
30
+ config('vim', false);
31
+ config('auth', true);
32
+
33
+ await once(socket, 'change');
34
+
35
+ socket.close();
36
+ await done();
37
+
38
+ t.pass('should emit change');
39
+ t.end();
40
+ });
41
+
42
+ test('distribute: export: config', async (t) => {
43
+ const defaultConfig = {
44
+ export: true,
45
+ exportToken: 'a',
46
+ vim: true,
47
+ log: false,
48
+ };
49
+
50
+ const {port, done} = await connect({
51
+ config: defaultConfig,
52
+ });
53
+
54
+ const url = `http://localhost:${port}/distribute?port=1111`;
55
+ const socket = io.connect(url);
56
+
57
+ socket.once('connect', () => {
58
+ socket.emit('auth', 'a');
59
+ });
60
+
61
+ const data = await once(socket, 'config');
62
+
63
+ socket.close();
64
+ await done();
65
+
66
+ t.equal(typeof data, 'object', 'should emit object');
67
+ t.end();
68
+ });