From 8483a469ea9769688685567de0f8c5fce169bbb8 Mon Sep 17 00:00:00 2001 From: Amine Berrichi Date: Mon, 16 Aug 2021 18:46:29 +0200 Subject: [PATCH 1/2] Add middlewares to the config. This can be useful if you want to process some treatment after the body-parser. --- src/server.js | 1 + src/worker.js | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/server.js b/src/server.js index 1d96723..c5b2441 100644 --- a/src/server.js +++ b/src/server.js @@ -25,6 +25,7 @@ const defaultConfig = { files: [], logger: {}, plugins: [], + middlewares: [], port: 8080, host: '0.0.0.0', processJobsConcurrent: true, diff --git a/src/worker.js b/src/worker.js index 59db4fd..3131b92 100644 --- a/src/worker.js +++ b/src/worker.js @@ -3,13 +3,26 @@ import bodyParser from 'body-parser'; import './environment'; import logger from './utils/logger'; import renderBatch from './utils/renderBatch'; -import { runAppLifecycle, errorSync, raceTo } from './utils/lifecycle'; +import { errorSync, raceTo, runAppLifecycle } from './utils/lifecycle'; import BatchManager from './utils/BatchManager'; const attachMiddleware = (app, config) => { app.use(bodyParser.json(config.bodyParser)); }; +const attachMiddlewares = (app, config) => { + attachMiddleware(app, config); + + const { middlewares } = config; + if (Array.isArray(middlewares)) { + middlewares.forEach((middleware) => { + if (typeof middleware === 'function') { + app.use(middleware); + } + }); + } +}; + const attachEndpoint = (app, config, callback) => { app.post(config.endpoint, renderBatch(config, callback)); }; @@ -42,7 +55,9 @@ class Server { try { this.closing = true; this.server.close((e) => { - if (e) { logger.info('Ran into error during close', { stack: e.stack }); } + if (e) { + logger.info('Ran into error during close', { stack: e.stack }); + } resolve(); }); } catch (e) { @@ -117,8 +132,8 @@ const initServer = (app, config, callback) => { }; const worker = (app, config, onServer, workerId) => { - // ===== Middleware ========================================================= - attachMiddleware(app, config); + // ===== Middlewares ========================================================= + attachMiddlewares(app, config); if (onServer) { onServer(app, process); @@ -154,6 +169,7 @@ const worker = (app, config, onServer, workerId) => { }; worker.attachMiddleware = attachMiddleware; +worker.attachMiddlewares = attachMiddlewares; worker.attachEndpoint = attachEndpoint; worker.initServer = initServer; worker.Server = Server; From f3feb130ccb235991f6ab0ade71cf7e042fd9894 Mon Sep 17 00:00:00 2001 From: Amine Berrichi Date: Mon, 16 Aug 2021 18:46:52 +0200 Subject: [PATCH 2/2] Fix eslint warnings --- .../app/assets/javascripts/MyComponent.js | 6 +- src/Module.js | 5 ++ src/createVM.js | 1 + test/BatchManager-test.js | 1 + test/Module-test.js | 1 + test/escape-test.js | 68 +++++++++---------- test/renderBatch-test.js | 4 ++ test/server-test.js | 3 +- 8 files changed, 51 insertions(+), 38 deletions(-) diff --git a/examples/simple/app/assets/javascripts/MyComponent.js b/examples/simple/app/assets/javascripts/MyComponent.js index a5da593..cc28e79 100644 --- a/examples/simple/app/assets/javascripts/MyComponent.js +++ b/examples/simple/app/assets/javascripts/MyComponent.js @@ -1,12 +1,12 @@ -var React = require('react'); -var renderReact = require('hypernova-react').renderReact; +const React = require('react'); +const { renderReact } = require('hypernova-react'); function MyComponent(props) { return React.createElement('div', { onClick() { alert('Click handlers work.'); }, - }, 'Hello, ' + props.name + '!'); + }, `Hello, ${props.name}!`); } module.exports = renderReact('MyComponent.js', MyComponent); diff --git a/src/Module.js b/src/Module.js index 9b11cbd..3214df9 100644 --- a/src/Module.js +++ b/src/Module.js @@ -10,6 +10,7 @@ const NativeModules = process.binding('natives'); // this is cool since we can now have different extensions for VM than for where your program is // running. // If you want to add an extension then you can use addExtension defined and exported below. +// eslint-disable-next-line no-underscore-dangle const moduleExtensions = { ...NativeModule._extensions }; function isNativeModule(id) { @@ -50,6 +51,7 @@ class Module { load(filename) { ok(!this.loaded); this.filename = filename; + // eslint-disable-next-line no-underscore-dangle this.paths = NativeModule._nodeModulePaths(path.dirname(filename)); } @@ -65,12 +67,14 @@ class Module { return Module.loadFile(filePath, this); } + // eslint-disable-next-line no-underscore-dangle _compile(content, filename) { const self = this; function require(filePath) { return self.require(filePath); } + // eslint-disable-next-line no-underscore-dangle require.resolve = request => NativeModule._resolveFilename(request, this); require.main = process.mainModule; require.extensions = moduleExtensions; @@ -98,6 +102,7 @@ class Module { } static loadFile(file, parent) { + // eslint-disable-next-line no-underscore-dangle const filename = NativeModule._resolveFilename(file, parent); if (parent) { diff --git a/src/createVM.js b/src/createVM.js index 89bd864..8d6714e 100644 --- a/src/createVM.js +++ b/src/createVM.js @@ -29,6 +29,7 @@ export default (options = {}) => { const module = new Module(name, environment); module.load(name); + // eslint-disable-next-line no-underscore-dangle module._compile(code, name); exportsCache.set(key, module.exports); diff --git a/test/BatchManager-test.js b/test/BatchManager-test.js index e97b7eb..0d49272 100644 --- a/test/BatchManager-test.js +++ b/test/BatchManager-test.js @@ -31,6 +31,7 @@ jobs.bar.name = 'bar'; // component not registered const req = {}; const res = {}; +// eslint-disable-next-line no-underscore-dangle const _strategies = { [COMPONENT_NAME]: sinon.stub().returns('html'), baz: sinon.stub().returns(undefined), diff --git a/test/Module-test.js b/test/Module-test.js index da78f13..ad63d52 100644 --- a/test/Module-test.js +++ b/test/Module-test.js @@ -8,6 +8,7 @@ function run(code) { const module = new Module(name); module.load(name); + // eslint-disable-next-line no-underscore-dangle module._compile(code, name); return module.exports; diff --git a/test/escape-test.js b/test/escape-test.js index a856983..a420163 100644 --- a/test/escape-test.js +++ b/test/escape-test.js @@ -12,52 +12,52 @@ describe('escaping', () => { }); wrap() - .withGlobal('document', () => ({})) - .describe('with fromScript', () => { - it('loads the escaped content correctly', () => { - const html = toScript({ a: 'b' }, { foo: '', bar: '>', baz: '&' }); - const $ = cheerio.load(html); + .withGlobal('document', () => ({})) + .describe('with fromScript', () => { + it('loads the escaped content correctly', () => { + const html = toScript({ a: 'b' }, { foo: '', bar: '>', baz: '&' }); + const $ = cheerio.load(html); - global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); + global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); - const res = fromScript({ - a: 'b', - }); - - assert.isObject(res); + const res = fromScript({ + a: 'b', + }); - assert.equal(res.foo, ''); - assert.equal(res.bar, '>'); - assert.equal(res.baz, '&'); - }); + assert.isObject(res); - it('escapes multiple times the same, with interleaved decoding', () => { - const makeHTML = () => toScript({ attr: 'key' }, { - props: 'yay', - needsEncoding: '" > ', // "needsEncoding" is necessary + assert.equal(res.foo, ''); + assert.equal(res.bar, '>'); + assert.equal(res.baz, '&'); }); - const script1 = makeHTML(); - const script2 = makeHTML(); - assert.equal(script1, script2, 'two successive toScripts result in identical HTML'); - const $ = cheerio.load(script1); + it('escapes multiple times the same, with interleaved decoding', () => { + const makeHTML = () => toScript({ attr: 'key' }, { + props: 'yay', + needsEncoding: '" > ', // "needsEncoding" is necessary + }); + const script1 = makeHTML(); + const script2 = makeHTML(); + assert.equal(script1, script2, 'two successive toScripts result in identical HTML'); - global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); + const $ = cheerio.load(script1); - const res = fromScript({ attr: 'key' }); + global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); - const script3 = makeHTML(); - assert.equal( - script1, - script3, - 'third toScript after a fromScript call results in the same HTML', - ); + const res = fromScript({ attr: 'key' }); - assert.isObject(res); + const script3 = makeHTML(); + assert.equal( + script1, + script3, + 'third toScript after a fromScript call results in the same HTML', + ); - assert.equal(res.props, 'yay'); + assert.isObject(res); + + assert.equal(res.props, 'yay'); + }); }); - }); it('escapes quotes and fixes data attributes', () => { const markup = toScript({ diff --git a/test/renderBatch-test.js b/test/renderBatch-test.js index 6dc6ebd..9d934a1 100644 --- a/test/renderBatch-test.js +++ b/test/renderBatch-test.js @@ -3,11 +3,13 @@ import renderBatch from '../lib/utils/renderBatch'; class Response { status(status) { + // eslint-disable-next-line no-underscore-dangle this._status = status; return this; } json(res) { + // eslint-disable-next-line no-underscore-dangle this._json = res; return this; } @@ -16,7 +18,9 @@ class Response { getResponse() { return { + // eslint-disable-next-line no-underscore-dangle status: this._status, + // eslint-disable-next-line no-underscore-dangle json: this._json, }; } diff --git a/test/server-test.js b/test/server-test.js index de5f299..dc5e3ec 100644 --- a/test/server-test.js +++ b/test/server-test.js @@ -14,7 +14,8 @@ describe('Hypernova server', () => { '../server.js', ].forEach(module => delete require.cache[require.resolve(module)]); - hypernova = require('../server.js'); // eslint-disable-line global-require + // eslint-disable-next-line import/extensions,global-require + hypernova = require('../server.js'); } catch (e) { console.error('Couldnt remove dependecy or load the hypernova module.'); }