From e01ee4575b68530d2cfdce48f4101be831820c1f Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 21 Mar 2024 09:56:12 +0200 Subject: [PATCH] feature: server: route: get rid of mock-require --- .editorconfig | 2 +- server/cloudcmd.js | 22 +++++++++++++++++----- server/depstore.js | 15 +++++++++++++++ server/route.js | 14 ++++++++------ server/route.spec.js | 43 +++++++++++-------------------------------- 5 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 server/depstore.js diff --git a/.editorconfig b/.editorconfig index 09fa94224b..439abfd03a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/server/cloudcmd.js b/server/cloudcmd.js index efa7936d7e..f8aa9425b4 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -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, ]); diff --git a/server/depstore.js b/server/depstore.js new file mode 100644 index 0000000000..2ab395f1d7 --- /dev/null +++ b/server/depstore.js @@ -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; + }; +}; diff --git a/server/route.js b/server/route.js index 3acc63d49a..ab5ea49166 100644 --- a/server/route.js +++ b/server/route.js @@ -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); diff --git a/server/route.spec.js b/server/route.spec.js index 2c55250920..2a70a66d2d 100644 --- a/server/route.spec.js +++ b/server/route.spec.js @@ -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(); });