frosty 0.0.148 → 0.0.149

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frosty",
3
- "version": "0.0.148",
3
+ "version": "0.0.149",
4
4
  "main": "dist/index",
5
5
  "module": "dist/index",
6
6
  "types": "dist/index",
@@ -43,6 +43,7 @@ print_usage() {
43
43
  echo " -b, --build-only Only build, do not run the server"
44
44
  echo " -B, --no-build Skip build step"
45
45
  echo " -p, --port <port> Specify port for the server (default: 8080)"
46
+ echo " -n, --num-workers <count> Specify number of worker processes"
46
47
  echo " -c, --configuration <file> Specify configuration file to use (default: server.config.js)"
47
48
  echo " -s, --src <dir> Specify source root directory"
48
49
  echo " -o, --output <dir> Specify output directory for build artifacts"
@@ -53,6 +54,7 @@ print_usage() {
53
54
  echo ""
54
55
  echo "Examples:"
55
56
  echo " frosty run app.js"
57
+ echo " frosty run -n 4 app.js"
56
58
  }
57
59
 
58
60
  POSITIONAL_ARGS=()
@@ -63,6 +65,7 @@ while [ $# -gt 0 ]; do
63
65
  -b|--build-only) BUILD_ONLY=true; shift ;;
64
66
  -B|--no-build) NO_BUILD=true; shift ;;
65
67
  -p|--port) PORT="$2"; shift 2 ;;
68
+ -n|--num-workers) NUM_WORKERS="$2"; shift 2 ;;
66
69
  -c|--configuration) CONFIG_FILE="$2"; shift 2 ;;
67
70
  -s|--src) SRCROOT="$2"; shift 2 ;;
68
71
  -o|--output) OUTPUT_DIR="$2"; shift 2 ;;
@@ -110,6 +113,7 @@ if [ "$NO_BUILD" != "true" ]; then
110
113
  fi
111
114
  [ -n "$INPUT_FILE" ] && BUILD_OPTS="$BUILD_OPTS --env INPUT_FILE="$INPUT_FILE""
112
115
  [ -n "$SRCROOT" ] && BUILD_OPTS="$BUILD_OPTS --env SRCROOT="$SRCROOT""
116
+ [ -n "$NUM_WORKERS" ] && BUILD_OPTS="$BUILD_OPTS --env NUM_WORKERS="$NUM_WORKERS""
113
117
  [ -n "$PORT" ] && BUILD_OPTS="$BUILD_OPTS --env PORT="$PORT""
114
118
  if [ "$WATCH_MODE" = "true" ]; then
115
119
  node "$FROSTY_CLI_ROOT/node_modules/webpack-cli/bin/cli.js" $BUILD_OPTS --watch &
@@ -24,5 +24,7 @@
24
24
  //
25
25
 
26
26
  import _ from 'lodash';
27
+ import { availableParallelism } from 'os';
27
28
 
28
29
  export const PORT = process.env.PORT ? parseInt(process.env.PORT) : 8080;
30
+ export const NUM_WORKERS = process.env.NUM_WORKERS ? parseInt(process.env.NUM_WORKERS) : availableParallelism();
@@ -26,41 +26,57 @@
26
26
  import _ from 'lodash';
27
27
  import fs from 'fs';
28
28
  import path from 'path';
29
+ import cluster from 'cluster';
29
30
  import { Server } from '@o2ter/server-js';
30
31
  import { FrostyRoute } from './route';
31
32
  import * as __SERVER__ from '__SERVER__';
32
33
  import * as __APPLICATIONS__ from '__APPLICATIONS__';
33
- import { PORT } from './env';
34
+ import { PORT, NUM_WORKERS } from './env';
34
35
 
35
- const app = 'serverOptions' in __SERVER__ ? new Server(__SERVER__.serverOptions) : new Server;
36
+ if (cluster.isPrimary && NUM_WORKERS > 1) {
37
+ console.log(`Primary ${process.pid} is running`);
36
38
 
37
- app.use(Server.static(path.join(__dirname, 'public'), { cacheControl: true }));
38
-
39
- const server_env = {};
40
- if ('default' in __SERVER__) await __SERVER__.default(app, server_env);
41
-
42
- for (const [name, { path: pathname }] of _.toPairs(__applications__)) {
43
- const { default: App } = __APPLICATIONS__[name];
44
- if (!_.isFunction(App)) {
45
- throw new Error(
46
- `Failed to load client app "${name}": default export is not a function.\n` +
47
- `Please ensure "${name}" exports a valid Frosty component as its default export.`
48
- );
39
+ // Fork workers.
40
+ for (let i = 0; i < NUM_WORKERS; i++) {
41
+ cluster.fork();
49
42
  }
50
- const cssExists = fs.existsSync(path.join(__dirname, `public/css/${name}_bundle.css`));
51
- const route = FrostyRoute(App, {
52
- jsSrc: `/${name}_bundle.js`,
53
- cssSrc: cssExists ? `/css/${name}_bundle.css` : undefined,
43
+
44
+ cluster.on('exit', (worker, code, signal) => {
45
+ console.log(`Worker ${worker.process.pid} died`);
54
46
  });
55
- if (_.isEmpty(pathname) || pathname === '/') {
56
- app.use(route);
57
- } else {
58
- app.use(pathname, route);
47
+
48
+ } else {
49
+
50
+ const app = 'serverOptions' in __SERVER__ ? new Server(__SERVER__.serverOptions) : new Server;
51
+
52
+ app.use(Server.static(path.join(__dirname, 'public'), { cacheControl: true }));
53
+
54
+ const server_env = {};
55
+ if ('default' in __SERVER__) await __SERVER__.default(app, server_env);
56
+
57
+ for (const [name, { path: pathname }] of _.toPairs(__applications__)) {
58
+ const { default: App } = __APPLICATIONS__[name];
59
+ if (!_.isFunction(App)) {
60
+ throw new Error(
61
+ `Failed to load client app "${name}": default export is not a function.\n` +
62
+ `Please ensure "${name}" exports a valid Frosty component as its default export.`
63
+ );
64
+ }
65
+ const cssExists = fs.existsSync(path.join(__dirname, `public/css/${name}_bundle.css`));
66
+ const route = FrostyRoute(App, {
67
+ jsSrc: `/${name}_bundle.js`,
68
+ cssSrc: cssExists ? `/css/${name}_bundle.css` : undefined,
69
+ });
70
+ if (_.isEmpty(pathname) || pathname === '/') {
71
+ app.use(route);
72
+ } else {
73
+ app.use(pathname, route);
74
+ }
59
75
  }
60
- }
61
76
 
62
- app.use((err, req, res, next) => {
63
- res.status(500).json(err instanceof Error ? { message: err.message } : err);
64
- });
77
+ app.use((err, req, res, next) => {
78
+ res.status(500).json(err instanceof Error ? { message: err.message } : err);
79
+ });
65
80
 
66
- app.listen(PORT, () => console.info(`listening on port ${PORT}`));
81
+ app.listen(PORT, () => console.info(`Process ${process.pid} listening on port ${PORT}`));
82
+ }
@@ -42,6 +42,7 @@ export default async (env, argv) => {
42
42
  CONFIG_FILE = 'server.config.js',
43
43
  INPUT_FILE,
44
44
  PORT = 8080,
45
+ NUM_WORKERS,
45
46
  } = env;
46
47
 
47
48
  const serverConfig = await (async () => {
@@ -253,7 +254,7 @@ export default async (env, argv) => {
253
254
  optimization: webpackOptimization({ server: true }),
254
255
  plugins: _.compact([
255
256
  ...webpackPlugins,
256
- new webpack.EnvironmentPlugin({ PORT }),
257
+ new webpack.EnvironmentPlugin({ PORT, NUM_WORKERS }),
257
258
  new webpack.DefinePlugin({
258
259
  __applications__: JSON.stringify(_.mapValues(inputs, x => ({
259
260
  path: x.basepath,