Skip to content

Commit

Permalink
feature: server: route: get rid of mock-require
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Mar 21, 2024
1 parent a03185e commit e01ee45
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ root = true
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
trim_trailing_whitespace = false
indent_style = space
indent_size = 4

Expand Down
22 changes: 17 additions & 5 deletions server/cloudcmd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const {join} = require('node:path');
const fullstore = require('fullstore');
const process = require('node:process');
const path = require('node:path');
Expand Down Expand Up @@ -31,8 +32,10 @@ const validate = require(`./validate`);
const prefixer = require(`./prefixer`);
const terminal = require(`./terminal`);
const distribute = require(`./distribute`);
const DIR_ROOT = `../`;
const {createDepStore} = require('./depstore');
const {assign} = Object;
const DIR = `${__dirname}/`;
const DIR_ROOT = join(DIR, '..');
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';

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

module.exports = (params) => {
module.exports = cloudcmd;

function cloudcmd(params) {
const p = params || {};
const options = p.config || {};
const config = p.configManager || createConfig({
Expand Down Expand Up @@ -84,11 +89,17 @@ module.exports = (params) => {
socket: p.socket,
});

return cloudcmd({
return cloudcmdMiddle({
modules,
config,
});
};
}

const depStore = createDepStore();

assign(cloudcmd, {
depStore,
});

module.exports.createConfigManager = createConfig;
module.exports.configPath = configPath;
Expand Down Expand Up @@ -173,7 +184,7 @@ function listen({prefixSocket, socket, config}) {
distribute.export(config, socket);
}

function cloudcmd({modules, config}) {
function cloudcmdMiddle({modules, config}) {
const online = apart(config, 'online');
const cache = false;
const diff = apart(config, 'diff');
Expand Down Expand Up @@ -240,6 +251,7 @@ function cloudcmd({modules, config}) {
rest(config),
route(config, {
html,
win32: depStore('win32'),
}),
ponseStatic,
]);
Expand Down
15 changes: 15 additions & 0 deletions server/depstore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports.createDepStore = () => {
let deps = {};

return (name, value) => {
if (!name)
return deps = {};

if (!value)
return deps[name];

deps[name] = value;
};
};
14 changes: 8 additions & 6 deletions server/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

const {read} = require('win32');
const _win32 = require('win32');
const ponse = require('ponse');
const rendy = require('rendy');
const format = require('format-io');
Expand Down Expand Up @@ -34,9 +34,9 @@ const sendIndex = (params, data) => {
const onceRequire = once(require);
const getPrefix = (config) => prefixer(config('prefix'));

const getReadDir = (config) => {
const getReadDir = (config, {win32 = _win32} = {}) => {
if (!config('dropbox'))
return read;
return win32.read;

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

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

const read = getReadDir(config);
const {html, win32} = options;
const read = getReadDir(config, {
win32,
});

const [error, stream] = await tryToCatch(read, fullPath, {
root: config('root'),
});

const {html} = options;

if (error)
return ponse.sendError(error, p);

Expand Down
43 changes: 11 additions & 32 deletions server/route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ const fs = require('node:fs');

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

const cloudcmd = require(cloudcmdPath);
const cloudcmd = require('./cloudcmd');

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

const routePath = './route';
const {_getReadDir} = require('./route');
const fixtureDir = path.join(__dirname, '..', 'test', 'fixture');

const {reRequire, stopAll} = mockRequire;
const {createConfigManager} = cloudcmd;

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

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

mockRequire('win32', {
cloudcmd.depStore('win32', {
read,
});

reRequire(routePath);
const cloudcmd = reRequire(cloudcmdPath);

const {request} = serveOnce(cloudcmd, {
configManager: createConfigManager(),
});

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

stopAll();
cloudcmd.depStore();

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

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

mockRequire('win32', {
cloudcmd.depStore('win32', {
read,
});

reRequire(routePath);
const cloudcmd = reRequire(cloudcmdPath);

const {request} = serveOnce(cloudcmd);
const {body} = await request.get('/');

stopAll();
cloudcmd.depStore();

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

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

mockRequire('win32', {
cloudcmd.depStore('win32', {
read,
});

reRequire(routePath);
const cloudcmd = reRequire(cloudcmdPath);

const {request} = serveOnce(cloudcmd, {
config: defaultConfig,
});

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

stopAll();
cloudcmd.depStore();

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

const {_getReadDir} = reRequire(routePath);

const readdir = _getReadDir(config);
const [e] = await tryToCatch(readdir, '/root');

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

const read = stub().returns(stream);

mockRequire('win32', {
cloudcmd.depStore('win32', {
read,
});

reRequire(routePath);

const cloudcmd = reRequire(cloudcmdPath);
const configManager = createConfigManager();
const root = '/hello';

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

await request.get('/fs/route.js');
cloudcmd.depStore();

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

stopAll();

t.calledWith(read, expected);
t.end();
});

0 comments on commit e01ee45

Please sign in to comment.