Skip to content

Commit 050e7d1

Browse files
committed
fix: fileop: copymitter (coderaiser/cloudcmd#457)
1 parent 80a3e63 commit 050e7d1

3 files changed

Lines changed: 57 additions & 81 deletions

File tree

server/listen.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function listen(socket, options) {
3030
auth,
3131
prefix = '/fileop',
3232
root = '/',
33+
...overrides
3334
} = options;
3435

3536
check(auth);
@@ -38,41 +39,46 @@ function listen(socket, options) {
3839
.of(prefix)
3940
.on('connection', (socket) => {
4041
if (!auth)
41-
return connection(root, socket);
42+
return connection(root, socket, overrides);
4243

4344
const reject = () => socket.emit('reject');
44-
socket.on('auth', auth(connectionWrapped(root, socket), reject));
45+
const onConnect = connectionWrapped(root, socket, overrides);
46+
47+
const onAuth = auth(onConnect, reject);
48+
49+
socket.on('auth', onAuth);
4550
});
4651
}
4752

48-
function connection(rootRaw, socket) {
53+
function connection(rootRaw, socket, overrides) {
4954
socket.emit('accept');
5055
socket.on('operation', (name, from, to, files) => {
5156
socket.emit('id', inc(id));
5257

5358
socket.once(`${id()}#start`, () => {
5459
socket.emit(`${id()}started`);
5560

56-
const operation = getOperation(name);
61+
const operation = getOperation(name, overrides);
62+
5763
const root = getValue(rootRaw);
5864

5965
operation(id(), root, socket, from, to, files);
6066
});
6167
});
6268
}
6369

64-
function getOperation(name) {
70+
function getOperation(name, overrides = {}) {
6571
if (name === 'remove')
66-
return operate('remove');
72+
return operate('remove', overrides);
6773

6874
if (name === 'extract')
6975
return extract;
7076

7177
if (name === 'copy')
72-
return operate('copy');
78+
return operate('copy', overrides);
7379

7480
if (name === 'move')
75-
return operate('move');
81+
return operate('move', overrides);
7682

7783
if (name === 'tar')
7884
return pack('tar');

server/operate.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
const currify = require('currify');
44

55
const remy = require('remy');
6-
const copymitter = require('copymitter');
6+
const {copymitter: _copymitter} = require('copymitter');
77
const moveFiles = require('@cloudcmd/move-files');
88
const {webToWin} = require('mellow');
99
const isString = (a) => typeof a === 'string';
1010

11-
const isRootWin32 = currify(require('./is-root-win32'));
11+
const _isRootWin32 = currify(require('./is-root-win32'));
1212

1313
const getPaths = (from, to) => {
1414
if (!isString(to))
@@ -26,17 +26,25 @@ const safeWebToWin = (path, root) => {
2626
return webToWin(path, root);
2727
};
2828

29-
module.exports = currify((type, id, root, socket, from, to, files) => {
29+
module.exports = currify((type, overrides, id, root, socket, from, to, files) => {
30+
const {
31+
isRootWin32 = _isRootWin32,
32+
} = overrides;
33+
3034
from = safeWebToWin(from, root);
3135
to = safeWebToWin(to, root);
3236

3337
if (getPaths(from, to).some(isRootWin32(root)))
3438
socket.emit(`${id}#error`, getRootError(type));
3539

36-
operate(type, id, socket, from, to, files);
40+
operate(type, id, socket, from, to, files, overrides);
3741
});
3842

39-
function getOperation(type) {
43+
function getOperation(type, overrides) {
44+
const {
45+
copymitter = _copymitter,
46+
} = overrides;
47+
4048
if (type === 'remove')
4149
return remy;
4250

@@ -46,8 +54,8 @@ function getOperation(type) {
4654
return copymitter;
4755
}
4856

49-
function operate(type, id, socket, from, to, files) {
50-
const operate = getOperation(type);
57+
function operate(type, id, socket, from, to, files, overrides) {
58+
const operate = getOperation(type, overrides);
5159
const operator = operate(from, to, files);
5260
const onPause = () => operator.pause();
5361
const onContinue = () => operator.continue();

test/server/copy.js

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
'use strict';
22

33
const {once} = require('node:events');
4-
54
const {test, stub} = require('supertape');
6-
const mock = require('mock-require');
7-
const clear = require('clear-module');
85

96
const {
107
endEmitter,
@@ -14,24 +11,18 @@ const {
1411
customEmitter,
1512
} = require('../lib/emitters');
1613

17-
const clearFileop = require('../lib/clear');
14+
const connect = require('../lib/connect');
1815

1916
const connectPath = '../lib/connect';
20-
const copyPath = 'copymitter';
21-
const isRootPath = '../../server/is-root-win32';
2217

2318
test('operate: copy: error', async (t) => {
24-
clearFileop();
25-
clear(copyPath);
26-
2719
const from = '/hello';
2820
const to = '/world';
2921
const names = ['abc'];
3022

31-
mock(copyPath, errorEmitter);
32-
const connect = require(connectPath);
33-
34-
const {socket, done} = await connect();
23+
const {socket, done} = await connect({
24+
copymitter: errorEmitter,
25+
});
3526

3627
const error = 'EACCES: /hello/abc';
3728

@@ -48,19 +39,14 @@ test('operate: copy: error', async (t) => {
4839
});
4940

5041
test('operate: copy: file', async (t) => {
51-
clearFileop();
52-
clear(copyPath);
53-
5442
const from = '/hello';
5543
const to = '/world';
5644
const names = ['abc'];
57-
58-
mock(copyPath, fileEmitter);
59-
const connect = require(connectPath);
6045
const root = '/';
6146

6247
const {socket, done} = await connect({
6348
root,
49+
copymitter: fileEmitter,
6450
});
6551

6652
socket.emit('operation', 'copy', from, to, names);
@@ -76,19 +62,15 @@ test('operate: copy: file', async (t) => {
7662
});
7763

7864
test('operate: copy: progress', async (t) => {
79-
clearFileop();
80-
clear(copyPath);
81-
8265
const from = '/hello';
8366
const to = '/world';
8467
const names = ['abc'];
8568

86-
mock(copyPath, progressEmitter);
87-
const connect = require(connectPath);
8869
const root = '/';
8970

9071
const {socket, done} = await connect({
9172
root,
73+
copymitter: progressEmitter,
9274
});
9375

9476
socket.emit('operation', 'copy', from, to, names);
@@ -97,51 +79,46 @@ test('operate: copy: progress', async (t) => {
9779
socket.emit(`${id}#start`);
9880
const [n] = await once(socket, `${id}#progress`);
9981

100-
t.equal(n, 100);
10182
done();
83+
84+
t.equal(n, 100);
10285
t.end();
10386
});
10487

10588
test('operate: copy: end', async (t) => {
106-
clearFileop();
107-
clear(copyPath);
108-
10989
const from = '/hello';
11090
const to = '/world';
11191
const names = ['abc'];
11292

113-
mock(copyPath, endEmitter);
114-
const connect = require(connectPath);
11593
const root = '/';
11694

11795
const {socket, done} = await connect({
11896
root,
97+
copymitter: endEmitter,
11998
});
12099

121100
socket.emit('operation', 'copy', from, to, names);
122101
const [id] = await once(socket, 'id');
123102

124103
socket.emit(`${id}#start`);
125104
await once(socket, `${id}#end`);
126-
t.pass('should emit end');
127105
done();
106+
107+
t.pass('should emit end');
128108
t.end();
129109
});
130110

131111
test('operate: copy: abort', async (t) => {
132-
clearFileop();
133-
clear(copyPath);
134-
135112
const from = '/hello';
136113
const to = '/world';
137114
const names = ['abc'];
138115

139-
mock(copyPath, errorEmitter);
140116
const connect = require(connectPath);
141117
const root = '/';
142118

143119
const {socket, done} = await connect({
144120
root,
121+
copymitter: errorEmitter,
145122
});
146123

147124
socket.emit('operation', 'copy', from, to, names);
@@ -151,23 +128,20 @@ test('operate: copy: abort', async (t) => {
151128
await once(socket, `${id}#error`);
152129
socket.emit(`${id}#abort`);
153130
await once(socket, `${id}#end`);
154-
t.pass('should emit abort');
155131
done();
132+
133+
t.pass('should emit abort');
156134
t.end();
157135
});
158136

159137
test('operate: copy: continue', async (t) => {
160-
clearFileop();
161-
clear(copyPath);
162-
163138
const from = '/';
164139
const to = '/world';
165140
const names = ['abc'];
166141

167-
mock(copyPath, errorEmitter);
168-
const connect = require(connectPath);
169-
170-
const {socket, done} = await connect();
142+
const {socket, done} = await connect({
143+
copymitter: errorEmitter,
144+
});
171145

172146
socket.emit('operation', 'copy', from, to, names);
173147
const [id] = await once(socket, 'id');
@@ -183,29 +157,28 @@ test('operate: copy: continue', async (t) => {
183157
});
184158

185159
test('operate: copy: pause', async (t) => {
186-
clearFileop();
187-
clear(copyPath);
188-
189160
const from = '/';
190161
const to = '/world';
191162
const names = ['abc'];
192163

193164
const pause = stub();
194-
mock(copyPath, customEmitter({
165+
const copymitter = customEmitter({
195166
pause,
196167
continue: stub(),
197-
}));
198-
199-
const connect = require(connectPath);
168+
});
200169

201-
const {socket, done} = await connect();
170+
const {socket, done} = await connect({
171+
copymitter,
172+
});
202173

203174
socket.emit('operation', 'copy', from, to, names);
204175

205176
const [id] = await once(socket, 'id');
177+
206178
socket.emit(`${id}#start`);
207179
socket.emit(`${id}#pause`);
208180
socket.emit(`${id}#continue`);
181+
209182
await once(socket, `${id}#end`);
210183

211184
done();
@@ -215,26 +188,16 @@ test('operate: copy: pause', async (t) => {
215188
});
216189

217190
test('operate: copy: error: root', async (t) => {
218-
clearFileop();
219-
clear(isRootPath);
220-
221191
const from = '/hello';
222192
const to = '/world';
223193
const names = ['abc'];
224-
const truth = () => true;
194+
const success = stub().returns(true);
195+
const isRootWin32 = stub().returns(success);
225196

226-
Object.defineProperty(truth, 'length', {
227-
value: 2,
197+
const {socket, done} = await connect({
198+
isRootWin32,
228199
});
229200

230-
const isRoot = require(isRootPath);
231-
mock(isRootPath, truth);
232-
233-
const connect = require(connectPath);
234-
mock(isRootPath, isRoot);
235-
236-
const {socket, done} = await connect();
237-
238201
socket.emit('operation', 'copy', from, to, names);
239202
const [id] = await once(socket, 'id');
240203

@@ -243,7 +206,6 @@ test('operate: copy: error: root', async (t) => {
243206
const [e] = await once(socket, `${id}#error`);
244207

245208
done();
246-
clear(isRootPath);
247209

248210
t.equal(e, error, 'should emit error');
249211
t.end();

0 commit comments

Comments
 (0)