Skip to content

Commit e01ee45

Browse files
committed
feature: server: route: get rid of mock-require
1 parent a03185e commit e01ee45

File tree

5 files changed

+52
-44
lines changed

5 files changed

+52
-44
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ root = true
99
charset = utf-8
1010
end_of_line = lf
1111
insert_final_newline = true
12-
trim_trailing_whitespace = true
12+
trim_trailing_whitespace = false
1313
indent_style = space
1414
indent_size = 4
1515

server/cloudcmd.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const {join} = require('node:path');
34
const fullstore = require('fullstore');
45
const process = require('node:process');
56
const path = require('node:path');
@@ -31,8 +32,10 @@ const validate = require(`./validate`);
3132
const prefixer = require(`./prefixer`);
3233
const terminal = require(`./terminal`);
3334
const distribute = require(`./distribute`);
34-
const DIR_ROOT = `../`;
35+
const {createDepStore} = require('./depstore');
36+
const {assign} = Object;
3537
const DIR = `${__dirname}/`;
38+
const DIR_ROOT = join(DIR, '..');
3639
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
3740

3841
const isDev = fullstore(process.env.NODE_ENV === 'development');
@@ -47,7 +50,9 @@ const clean = (a) => a.filter(notEmpty);
4750
const isUndefined = (a) => typeof a === 'undefined';
4851
const isFn = (a) => typeof a === 'function';
4952

50-
module.exports = (params) => {
53+
module.exports = cloudcmd;
54+
55+
function cloudcmd(params) {
5156
const p = params || {};
5257
const options = p.config || {};
5358
const config = p.configManager || createConfig({
@@ -84,11 +89,17 @@ module.exports = (params) => {
8489
socket: p.socket,
8590
});
8691

87-
return cloudcmd({
92+
return cloudcmdMiddle({
8893
modules,
8994
config,
9095
});
91-
};
96+
}
97+
98+
const depStore = createDepStore();
99+
100+
assign(cloudcmd, {
101+
depStore,
102+
});
92103

93104
module.exports.createConfigManager = createConfig;
94105
module.exports.configPath = configPath;
@@ -173,7 +184,7 @@ function listen({prefixSocket, socket, config}) {
173184
distribute.export(config, socket);
174185
}
175186

176-
function cloudcmd({modules, config}) {
187+
function cloudcmdMiddle({modules, config}) {
177188
const online = apart(config, 'online');
178189
const cache = false;
179190
const diff = apart(config, 'diff');
@@ -240,6 +251,7 @@ function cloudcmd({modules, config}) {
240251
rest(config),
241252
route(config, {
242253
html,
254+
win32: depStore('win32'),
243255
}),
244256
ponseStatic,
245257
]);

server/depstore.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
};

server/route.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const {extname} = require('node:path');
44

5-
const {read} = require('win32');
5+
const _win32 = require('win32');
66
const ponse = require('ponse');
77
const rendy = require('rendy');
88
const format = require('format-io');
@@ -34,9 +34,9 @@ const sendIndex = (params, data) => {
3434
const onceRequire = once(require);
3535
const getPrefix = (config) => prefixer(config('prefix'));
3636

37-
const getReadDir = (config) => {
37+
const getReadDir = (config, {win32 = _win32} = {}) => {
3838
if (!config('dropbox'))
39-
return read;
39+
return win32.read;
4040

4141
const {readDir} = onceRequire('@cloudcmd/dropbox');
4242

@@ -78,13 +78,15 @@ async function route({config, options, request, response}) {
7878
const rootName = name.replace(CloudFunc.FS, '') || '/';
7979
const fullPath = root(rootName, config('root'));
8080

81-
const read = getReadDir(config);
81+
const {html, win32} = options;
82+
const read = getReadDir(config, {
83+
win32,
84+
});
85+
8286
const [error, stream] = await tryToCatch(read, fullPath, {
8387
root: config('root'),
8488
});
8589

86-
const {html} = options;
87-
8890
if (error)
8991
return ponse.sendError(error, p);
9092

server/route.spec.js

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ const fs = require('node:fs');
77

88
const tryToCatch = require('try-to-catch');
99
const {test, stub} = require('supertape');
10-
const mockRequire = require('mock-require');
11-
const cloudcmdPath = './cloudcmd';
1210

13-
const cloudcmd = require(cloudcmdPath);
11+
const cloudcmd = require('./cloudcmd');
1412

1513
const serveOnce = require('serve-once');
16-
const {createConfigManager} = cloudcmd;
1714

18-
const routePath = './route';
15+
const {_getReadDir} = require('./route');
1916
const fixtureDir = path.join(__dirname, '..', 'test', 'fixture');
20-
21-
const {reRequire, stopAll} = mockRequire;
17+
const {createConfigManager} = cloudcmd;
2218

2319
const defaultConfig = {
2420
auth: false,
@@ -231,20 +227,17 @@ test('cloudcmd: route: sendIndex: encode', async (t) => {
231227

232228
const read = stub().resolves(stream);
233229

234-
mockRequire('win32', {
230+
cloudcmd.depStore('win32', {
235231
read,
236232
});
237233

238-
reRequire(routePath);
239-
const cloudcmd = reRequire(cloudcmdPath);
240-
241234
const {request} = serveOnce(cloudcmd, {
242235
configManager: createConfigManager(),
243236
});
244237

245238
const {body} = await request.get('/');
246239

247-
stopAll();
240+
cloudcmd.depStore();
248241

249242
t.match(body, nameEncoded, 'should encode name');
250243
t.end();
@@ -270,17 +263,14 @@ test('cloudcmd: route: sendIndex: encode: not encoded', async (t) => {
270263

271264
const read = stub().resolves(stream);
272265

273-
mockRequire('win32', {
266+
cloudcmd.depStore('win32', {
274267
read,
275268
});
276269

277-
reRequire(routePath);
278-
const cloudcmd = reRequire(cloudcmdPath);
279-
280270
const {request} = serveOnce(cloudcmd);
281271
const {body} = await request.get('/');
282272

283-
stopAll();
273+
cloudcmd.depStore();
284274

285275
t.notOk(body.includes(name), 'should not put not encoded name');
286276
t.end();
@@ -306,20 +296,16 @@ test('cloudcmd: route: sendIndex: ddos: render', async (t) => {
306296

307297
const read = stub().resolves(stream);
308298

309-
mockRequire('win32', {
299+
cloudcmd.depStore('win32', {
310300
read,
311301
});
312-
313-
reRequire(routePath);
314-
const cloudcmd = reRequire(cloudcmdPath);
315-
316302
const {request} = serveOnce(cloudcmd, {
317303
config: defaultConfig,
318304
});
319305

320306
const {status} = await request.get('/');
321307

322-
stopAll();
308+
cloudcmd.depStore();
323309

324310
t.equal(status, 200, 'should not hang up');
325311
t.end();
@@ -422,8 +408,6 @@ test('cloudcmd: route: dropbox', async (t) => {
422408
config('dropbox', true);
423409
config('dropboxToken', '');
424410

425-
const {_getReadDir} = reRequire(routePath);
426-
427411
const readdir = _getReadDir(config);
428412
const [e] = await tryToCatch(readdir, '/root');
429413

@@ -452,14 +436,10 @@ test('cloudcmd: route: read: root', async (t) => {
452436
stream.contentLength = 5;
453437

454438
const read = stub().returns(stream);
455-
456-
mockRequire('win32', {
439+
cloudcmd.depStore('win32', {
457440
read,
458441
});
459442

460-
reRequire(routePath);
461-
462-
const cloudcmd = reRequire(cloudcmdPath);
463443
const configManager = createConfigManager();
464444
const root = '/hello';
465445

@@ -470,13 +450,12 @@ test('cloudcmd: route: read: root', async (t) => {
470450
});
471451

472452
await request.get('/fs/route.js');
453+
cloudcmd.depStore();
473454

474455
const expected = ['/hello/route.js', {
475456
root,
476457
}];
477458

478-
stopAll();
479-
480459
t.calledWith(read, expected);
481460
t.end();
482461
});

0 commit comments

Comments
 (0)