mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-22 18:29:26 +00:00
feature: server: route: get rid of mock-require
This commit is contained in:
parent
a03185e14e
commit
e01ee4575b
5 changed files with 52 additions and 44 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
@ -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');
|
||||
|
|
@ -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({
|
||||
|
|
@ -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;
|
||||
|
|
@ -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');
|
||||
|
|
@ -240,6 +251,7 @@ function cloudcmd({modules, config}) {
|
|||
rest(config),
|
||||
route(config, {
|
||||
html,
|
||||
win32: depStore('win32'),
|
||||
}),
|
||||
ponseStatic,
|
||||
]);
|
||||
|
|
|
|||
15
server/depstore.js
Normal file
15
server/depstore.js
Normal file
|
|
@ -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;
|
||||
};
|
||||
};
|
||||
|
|
@ -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');
|
||||
|
|
@ -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');
|
||||
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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();
|
||||
|
|
@ -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');
|
||||
|
||||
|
|
@ -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';
|
||||
|
||||
|
|
@ -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();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue