Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD server side data functions and response setter #37

Open
wants to merge 25 commits into
base: 1.2.0-beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
750f212
fix: changed components configuration
Oct 1, 2021
36fd1a9
Added Request Dispatcher Layer
Nov 1, 2021
412dba3
FIX server.js to customize it in other projects
cihanselim Dec 17, 2021
431e7b7
Merge pull request #29 from hepsiburada/feat/requestDispatcher
ahmetkuslular Dec 21, 2021
277f8df
Merge branch 'master' of https://github.com/hepsiburada/VoltranJS int…
Jan 11, 2022
efc05d5
Remove unnecessary header
Jan 11, 2022
ae3b44d
Merge branch 'master' of https://github.com/hepsiburada/VoltranJS int…
Jan 11, 2022
ac758ee
Merge branch 'feat/custom-server-js' into feat/custom-serverjs-and-fixes
Jan 11, 2022
ca6e445
FIX some bugs and missings
Jan 11, 2022
ce95779
FIX user-agent header null check
Jan 11, 2022
d73f3cb
FIX voltranconfig entry condition
Jan 12, 2022
049bd52
ADD console to webpack server
Jan 12, 2022
dd82862
Merge pull request #35 from hepsiburada/feat/custom-serverjs-and-fixes
cihanselim Feb 4, 2022
f6a2827
ADD server side data functions and response setter
Feb 4, 2022
6482ec9
FIX code review issues
Feb 4, 2022
e33858d
ADD History service
Mar 1, 2022
cdb6b89
ADD feature toggle component field
Mar 3, 2022
4012cf4
ADD custom main js feature and extraPropKeys (#42)
cihanselim Apr 6, 2022
acb1752
Some fixes about history location provider in moria
Apr 6, 2022
75a8df4
ADD fullComponentPath to render multiple, minor changes in serverjs &…
Apr 20, 2022
c8b5ced
FIX api service manager
ahmetkuslular Apr 27, 2022
3b3a5ce
Minor updates
ahmetkuslular Jun 6, 2022
a70e8d6
ADD request dispatcher client
ahmetkuslular Jun 13, 2022
2d105e8
New Welcome screen
ahmetkuslular Jun 15, 2022
660dccd
ADD api service middleware
ahmetkuslular Jun 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
window: true,
hepsiBus: true,
global: true,
jest: true,
jest: true
},
parserOptions: {
ecmaFeatures: {
Expand Down Expand Up @@ -46,7 +46,13 @@ module.exports = {
'no-nested-ternary': 'off',
'no-underscore-dangle': 'off',
'consistent-return': 'off',
'array-callback-return': 'off'
'array-callback-return': 'off',
'no-restricted-syntax': [
'error',
'FunctionExpression',
'WithStatement',
"BinaryExpression[operator='in']"
]
},
env: {
jest: true,
Expand Down
1 change: 1 addition & 0 deletions config/emptyModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
65 changes: 57 additions & 8 deletions config/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,76 @@ const voltranConfig = require('../voltran.config');

const prometheusFile = voltranConfig.monitoring.prometheus;

function replaceString () {
const data = [
{ search: '__V_COMPONENTS__', replace: normalizeUrl(voltranConfig.routing.components), flags: 'g' },
function replaceString() {
const data = [
{
search: '__V_COMPONENTS__',
replace: normalizeUrl(voltranConfig.routing.components),
flags: 'g'
},
{
search: '__APP_CONFIG__',
replace: normalizeUrl(`${voltranConfig.appConfigFile.output.path}/${voltranConfig.appConfigFile.output.name}.js`),
replace: normalizeUrl(
`${voltranConfig.appConfigFile.output.path}/${voltranConfig.appConfigFile.output.name}.js`
),
flags: 'g'
},
{
search: '__ASSETS_FILE_PATH__',
replace: normalizeUrl(`${voltranConfig.inputFolder}/assets.json`),
flags: 'g'
},
{ search: '__V_DICTIONARY__', replace: normalizeUrl(voltranConfig.routing.dictionary), flags: 'g' },
{ search: '@voltran/core', replace: normalizeUrl(path.resolve(__dirname, '../src/index')), flags: 'g' },
{
search: '__V_DICTIONARY__',
replace: normalizeUrl(voltranConfig.routing.dictionary),
flags: 'g'
},
{
search: '__V_REQUEST_CONFIGS__',
replace: normalizeUrl(
voltranConfig.requestConfigs || path.resolve(__dirname, './emptyModule.js')
),
flags: 'g'
},
{
search: '__V_PREVIEW__',
replace: normalizeUrl(voltranConfig.preview || path.resolve(__dirname, './emptyModule.js')),
flags: 'g'
},
{
search: '@voltran/core',
replace: normalizeUrl(path.resolve(__dirname, '../src/index')),
flags: 'g'
},
{
search: '@voltran/server',
replace: normalizeUrl(path.resolve(__dirname, '../src/server')),
flags: 'g'
},
{
search: '__V_MAIN__',
replace: normalizeUrl(
voltranConfig.entry.main || path.resolve(__dirname, './emptyModule.js')
),
flags: 'g'
},
{
search: '__V_SERVER__',
replace: normalizeUrl(
voltranConfig.entry.server || path.resolve(__dirname, './emptyModule.js')
),
flags: 'g'
},
{ search: '"__V_styles__"', replace: getStyles() }
];

data.push({ search: '__V_PROMETHEUS__', replace: normalizeUrl(prometheusFile ? prometheusFile : '../lib/tools/prom.js'), flags: 'g' });
data.push({
search: '__V_PROMETHEUS__',
replace: normalizeUrl(prometheusFile || '../lib/tools/prom.js'),
flags: 'g'
});

return data;
return data;
}

module.exports = replaceString;
7 changes: 5 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Jest configuration
// https://facebook.github.io/jest/docs/en/configuration.html
module.exports = {
verbose: true,
automock: false,
Expand All @@ -11,6 +9,11 @@ module.exports = {
'!src/public/**',
'!src/tools/**'
],
env: {
production: {
plugins: ['transform-es2015-modules-commonjs']
}
},
coverageDirectory: '<rootDir>/coverage',
globals: {
window: true,
Expand Down
12 changes: 8 additions & 4 deletions lib/os.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const path = require('path');

function normalizeUrl(url) {
const urlArray = url.split(path.sep);

return urlArray.join('/');
if (url) {
const urlArray = url?.split(path.sep);

return urlArray.join('/');
}

return '';
}

module.exports = normalizeUrl;
module.exports = normalizeUrl;
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"classnames": "2.2.6",
"clean-webpack-plugin": "1.0.0",
"cli-color": "^2.0.0",
"colors": "^1.4.0",
"compose-middleware": "5.0.0",
"compression": "^1.7.4",
"cookie-parser": "1.4.3",
Expand All @@ -59,6 +60,7 @@
"file-loader": "1.1.11",
"helmet": "3.21.3",
"hiddie": "^1.0.0",
"history": "^5.3.0",
"husky": "^3.1.0",
"identity-obj-proxy": "3.0.0",
"intersection-observer": "0.7.0",
Expand Down Expand Up @@ -132,5 +134,9 @@
"commitLimit": false,
"template": "changelog-template.hbs",
"package": true
},
"peerDependencies": {
"react": ">=16.13.0",
"react-dom": ">=16.13.0"
}
}
21 changes: 16 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import withBaseComponent from './universal/partials/withBaseComponent';
import { SERVICES } from './universal/utils/constants';
import voltran from './universal/partials/withBaseComponent';
import apiService, {
ClientApiManager,
ServerApiManager,
apiServiceMiddleware
} from './universal/core/apiService';
import requestDispatcher from './universal/utils/requestDispatcher';
import useRequestDispatcher from './universal/hooks/useRequestDispatcher';

export default {
withBaseComponent,
SERVICES
export default voltran;
export {
ClientApiManager,
ServerApiManager,
apiService,
apiServiceMiddleware,
requestDispatcher,
useRequestDispatcher
};
34 changes: 24 additions & 10 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,48 @@ import Hiddie from 'hiddie';
import http from 'http';
import voltranConfig from '../voltran.config';
import prom from 'prom-client';
import {HTTP_STATUS_CODES} from './universal/utils/constants';
import { HTTP_STATUS_CODES } from './universal/utils/constants';

const voltranMain = require('__V_MAIN__');

const enablePrometheus = voltranConfig.monitoring.prometheus;

function triggerMessageListener(worker) {
worker.on('message', function (message) {
worker.on('message', function(message) {
if (message?.options?.forwardAllWorkers) {
sendMessageToAllWorkers(message);
}
});
}

function sendMessageToAllWorkers(message) {
Object.keys(cluster.workers).forEach(function (key) {
Object.keys(cluster.workers).forEach(function(key) {
const worker = cluster.workers[key];
worker.send({
msg: message.msg,
msg: message.msg
});
}, this);
}

cluster.on('fork', (worker) => {
cluster.on('fork', worker => {
triggerMessageListener(worker);
});

if (cluster.isMaster) {
for (let i = 0; i < os.cpus().length; i += 1) {
const DEFAULT_CPU_COUNT = os.cpus().length;

function forkClusters(cpuCount = DEFAULT_CPU_COUNT) {
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}

cluster.on('exit', worker => {
logger.error(`Worker ${worker.id} died`);
cluster.fork();
const newWorker = cluster.fork();
cluster.emit('message', newWorker, 'NEW_WORKER');
});
}

if (cluster.isMaster) {
if (enablePrometheus) {
const aggregatorRegistry = new prom.AggregatorRegistry();
const metricsPort = voltranConfig.port + 1;
Expand All @@ -52,7 +59,7 @@ if (cluster.isMaster) {
return res.end(await aggregatorRegistry.clusterMetrics());
}
res.statusCode = HTTP_STATUS_CODES.NOT_FOUND;
res.end(JSON.stringify({message: 'not found'}));
res.end(JSON.stringify({ message: 'not found' }));
});

http.createServer(hiddie.run).listen(metricsPort, () => {
Expand All @@ -63,7 +70,14 @@ if (cluster.isMaster) {
);
});
}

if (voltranConfig.entry.main) {
const voltranMainFile = voltranMain.default;
forkClusters(voltranMainFile.cpuCount);
voltranMainFile.load(cluster);
}
} else if (voltranConfig.entry.server) {
require('__V_SERVER__');
} else {
// eslint-disable-next-line global-require
require('./server');
}
Loading