Skip to content

Commit 18fbd96

Browse files
committed
fix(fileop) socketPrefix
1 parent 802f82d commit 18fbd96

File tree

12 files changed

+103
-77
lines changed

12 files changed

+103
-77
lines changed
File renamed without changes.

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ coverage
66
yarn-error.log
77
yarn.lock
88

9+
madrun.js
10+

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Initialize `operator`.
1919
- `options`:
2020
- `prefix` - prefix of `fileop` (`/fileop` by default)
2121
- `socketPrefix` - prefix of `socket.io` (`` by default)
22-
2322

2423
Returns `operator` in callback.
2524

client/fileop.js

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const Emitify = require('emitify/legacy');
44
const getHost = require('./get-host');
55
const loadSocket = require('./load-socket');
6-
/*eslint no-unused-vars: 0 */
76
const operator = require('./operator');
87

98
const {promisify} = require('es6-promisify');
@@ -14,27 +13,57 @@ module.exports = (options, callback) => {
1413
options = {};
1514
}
1615

17-
const prefix = options.prefix || '/fileop';
18-
const socketPrefix = options.socketPrefix || '';
19-
20-
const socketPath = `${socketPrefix}/socket.io`;
16+
const socketPrefix = options.socketPrefix || '/fileop';
17+
const prefix = options.prefix || '';
18+
const socketPath = `${prefix}/socket.io`;
2119

2220
loadSocket((io) => {
23-
const fileop = new Fileop(io, prefix, socketPath);
21+
const fileop = new Fileop(io, socketPrefix, socketPath);
2422

2523
callback(null, fileop);
2624
});
2725
};
2826

2927
class Fileop extends Emitify {
28+
#operate(name, from, to, files, fn = files) {
29+
const {socket} = this;
30+
31+
socket.emit('operation', name, from, to, files);
32+
socket.once('id', (id) => {
33+
fn(null, operator(id, socket));
34+
});
35+
}
36+
37+
#setListeners(socket) {
38+
this.on('auth', (username, password) => {
39+
socket.emit('auth', username, password);
40+
});
41+
42+
socket.on('accept', () => {
43+
this.emit('accept');
44+
});
45+
46+
socket.on('reject', () => {
47+
this.emit('reject');
48+
});
49+
50+
socket.on('connect', () => {
51+
this.emit('connect');
52+
});
53+
54+
socket.on('disconnect', () => {
55+
this.emit('disconnect');
56+
});
57+
}
58+
3059
constructor(io, room, socketPath) {
3160
super();
3261

3362
const href = getHost();
3463
const FIVE_SECONDS = 5000;
3564

3665
const socket = io.connect(href + room, {
37-
'max reconnection attempts' : Math.pow(2, 32),
66+
'max reconnection attempts' : 2 ** 32,
3867
'reconnection limit' : FIVE_SECONDS,
3968
path: socketPath,
4069
});
@@ -43,62 +72,29 @@ class Fileop extends Emitify {
4372
this.operate = promisify(this.#operate);
4473
this.socket = socket;
4574
}
46-
75+
4776
copy(from, to, files) {
4877
return this.operate('copy', from, to, files);
4978
}
50-
79+
5180
move(from, to, files) {
5281
return this.operate('move', from, to, files);
5382
}
54-
83+
5584
zip(from, to, files) {
5685
return this.operate('zip', from, to, files);
5786
}
58-
87+
5988
tar(from, to, files) {
6089
return this.operate('tar', from, to, files);
6190
}
62-
91+
6392
extract(from, to) {
6493
return this.operate('extract', from, to);
6594
}
66-
95+
6796
remove(from, files) {
6897
return this.operate('remove', from, files);
6998
}
70-
71-
/*eslint no-undef: 0 */
72-
#operate(name, from, to, files, fn = files) {
73-
const {socket} = this;
74-
75-
socket.emit('operation', name, from, to, files);
76-
socket.once('id', (id) => {
77-
fn(null, operator(id, socket));
78-
});
79-
}
80-
81-
/*eslint no-undef: 0 */
82-
#setListeners(socket) {
83-
this.on('auth', (username, password) => {
84-
socket.emit('auth', username, password);
85-
});
86-
87-
socket.on('accept', () => {
88-
this.emit('accept');
89-
});
90-
91-
socket.on('reject', () => {
92-
this.emit('reject');
93-
});
94-
95-
socket.on('connect', () => {
96-
this.emit('connect');
97-
});
98-
99-
socket.on('disconnect', () => {
100-
this.emit('disconnect');
101-
});
102-
}
10399
}
104100

madrun.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const {run} = require('madrun');
4+
5+
module.exports = {
6+
'test': () => `tape 'test/**/*.js'`,
7+
'lint:server': () => 'eslint server -c .eslintrc.server',
8+
'lint:test': () => 'eslint test madrun.js webpack.config.js',
9+
'lint:client': () => 'eslint --env browser --rule \'no-console:0\' client',
10+
'lint': () => run(['putout', 'lint:*']),
11+
'fix:lint': () => run(['putout', 'lint:*'], '--fix'),
12+
'putout': () => `putout client server test madrun.js webpack.config.js`,
13+
'coverage': () => `nyc ${run('test')}`,
14+
'report': () => `nyc report --reporter=text-lcov | coveralls || true`,
15+
'build': () => run(['rmdir', 'build:*']),
16+
'build-progress': () => 'BABEL_ENV=client webpack --progress',
17+
'build:client': () => run(['build-progress'], '--mode production'),
18+
'build:client:dev': () => `NODE_ENV=development ${run('build-progress', '--mode development')}`,
19+
'watcher': () => 'nodemon -w client -w server -w test --exec',
20+
'watch:test': () => run(['watcher'], '\'npm test\''),
21+
'watch:coverage': () => run(['watcher'], '\'npm run coverage\''),
22+
'watch:lint': () => run(['watcher'], '\'npm run lint\''),
23+
'watch:client': () => run(['build:client'], '--watch'),
24+
'watch:client:dev': () => run(['build:client:dev'], '--watch'),
25+
'wisdom': () => run(['build']),
26+
'rmdir': () => 'rimraf dist dist-dev',
27+
};
28+

package.json

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
"main": "server/index.js",
66
"scripts": {
77
"test": "madrun test",
8-
"coverage": "nyc npm test",
9-
"report": "nyc report --reporter=text-lcov | coveralls",
10-
"lint": "madrun lint:*",
8+
"watch:test": "madrun watch:test",
119
"lint:client": "madrun lint:client",
1210
"lint:server": "madrun lint:server",
13-
"lint:test": "madrun test",
14-
"build": "redrun rmdir build:*",
15-
"build-progress": "BABEL_ENV=client webpack --progress",
16-
"build:client": "npm run build-progress -- --mode production",
17-
"build:client:dev": "NODE_ENV=development npm run build-progress -- --mode development",
18-
"watcher": "nodemon -w client -w server -w test --exec",
19-
"watch:test": "npm run watcher -- 'npm test'",
20-
"watch:coverage": "npm run watcher -- 'npm run coverage'",
21-
"watch:lint": "npm run watcher -- 'npm run lint'",
22-
"watch:client": "redrun build:client -- --watch",
23-
"watch:client:dev": "redrun build:client:dev -- --watch",
24-
"wisdom": "npm run build",
25-
"rmdir": "rimraf dist dist-dev"
11+
"lint:test": "madrun lint:test",
12+
"lint": "madrun lint || true",
13+
"fix:lint": "madrun fix:lint || true",
14+
"putout": "madrun putout",
15+
"coverage": "madrun coverage",
16+
"report": "madrun report",
17+
"build": "madrun build",
18+
"build-progress": "madrun build-progress",
19+
"build:client": "madrun build:client",
20+
"build:client:dev": "madrun build:client:dev",
21+
"watcher": "madrun watcher",
22+
"watch:coverage": "madrun watch:coverage",
23+
"watch:lint": "madrun watch:lint",
24+
"watch:client": "madrun watch:client",
25+
"watch:client:dev": "madrun watch:client:dev",
26+
"wisdom": "madrun wisdom",
27+
"rmdir": "madrun rmdir"
2628
},
2729
"repository": {
2830
"type": "git",
@@ -59,7 +61,7 @@
5961
"devDependencies": {
6062
"@babel/cli": "^7.0.0",
6163
"@babel/core": "^7.0.0",
62-
"@babel/plugin-proposal-class-properties": "^7.3.0",
64+
"@babel/plugin-proposal-class-properties": "^7.4.0",
6365
"@babel/plugin-proposal-private-methods": "^7.3.0",
6466
"@babel/preset-env": "^7.0.0",
6567
"@babel/register": "^7.0.0",

server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require('path');
55

66
const currify = require('currify');
77
const express = require('express');
8-
const Router = express.Router;
8+
const {Router} = express;
99

1010
const listen = require('./listen');
1111

server/listen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function check(auth) {
2525
function listen(socket, options) {
2626
options = options || {};
2727

28-
const auth = options.auth;
28+
const {auth} = options;
2929
const prefix = options.prefix || '/fileop';
3030
const root = options.root || '/';
3131

@@ -80,6 +80,6 @@ function getOperation(name) {
8080
}
8181

8282
function _wrongOperation(name, id, root, socket) {
83-
socket.emit(`${id}#err`, `Wrong operation: "${name}"`);
83+
socket.emit(`${id}#err`, `Wrong operation: "${name}"`);
8484
}
8585

server/operate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = currify((type, id, root, socket, from, to, files) => {
3030
to = pathToWin(to, root);
3131

3232
if (getPaths(from, to).some(isRootWin32(root)))
33-
socket.emit(`${id}#error`, getRootError(type));
33+
socket.emit(`${id}#error`, getRootError(type));
3434

3535
operate(type, id, socket, from, to, files);
3636
});
@@ -72,14 +72,14 @@ function operate(type, id, socket, from, to, files) {
7272
rmListeners();
7373
};
7474

75-
const onContinue = () => {
75+
const onContinue = () => {
7676
operator.continue();
7777
rmListeners();
7878
};
7979

8080
socket.emit(`${id}#error`, msg, name);
8181
socket.on(`${id}#continue`, onContinue);
82-
socket.on(`${id}#abort`, onAbort);
82+
socket.on(`${id}#abort`, onAbort);
8383
});
8484

8585
operator.on('end', () => {

test/client/fileop.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ test('client: options', async (t) => {
281281
accept();
282282
};
283283

284-
const prefix = '/hello';
284+
const socketPrefix = '/hello';
285285
const {done, origin} = await connect({
286-
prefix,
286+
prefix: socketPrefix,
287287
auth,
288288
});
289289

290290
before({origin});
291291

292-
const operator = await fileop({prefix});
292+
const operator = await fileop({socketPrefix});
293293
const destroy = getDestroy(operator);
294294

295295
operator.emit('auth');
@@ -302,3 +302,4 @@ test('client: options', async (t) => {
302302
t.end();
303303
});
304304
});
305+

0 commit comments

Comments
 (0)