33const Emitify = require ( 'emitify/legacy' ) ;
44const getHost = require ( './get-host' ) ;
55const loadSocket = require ( './load-socket' ) ;
6- /*eslint no-unused-vars: 0 */
76const operator = require ( './operator' ) ;
87
98const { 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
2927class 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
0 commit comments