Sha256: 2e4eb9de948b289660870f18964cd415285483577df5e01a6defe2c060a7e36e

Contents?: true

Size: 1.79 KB

Versions: 9

Compression:

Stored size: 1.79 KB

Contents

const net = require('net');
const fs = require('fs');

const bundlePath = '../../app/assets/webpack/';
let bundleFileName = 'webpack-bundle.js';

let currentArg;

function Handler() {
  this.queue = [];
  this.initialized = false;
}

Handler.prototype.handle = (connection) => {
  const callback = () => {
    connection.setEncoding('utf8');
    connection.on('data', (data) => {
      console.log(`Processing request: ${data}`);

      // eslint-disable-next-line no-eval
      const result = eval(data);
      connection.write(result);
    });
  };

  if (this.initialized) {
    callback();
  } else {
    this.queue.push(callback);
  }
};

Handler.prototype.initialize = () => {
  console.log(`Processing ${this.queue.length} pending requests`);
  let callback;

  // eslint-disable-next-line no-cond-assign
  while (callback = this.queue.pop()) {
    callback();
  }

  this.initialized = true;
};

const handler = new Handler();

process.argv.forEach((val) => {
  if (val[0] === '-') {
    currentArg = val.slice(1);
    return;
  }

  if (currentArg === 's') {
    bundleFileName = val;
  }
});

try {
  fs.mkdirSync(bundlePath);
} catch (e) {
  if (e.code !== 'EEXIST') throw e;
}

fs.watchFile(bundlePath + bundleFileName, (curr) => {
  if (curr && curr.blocks && curr.blocks > 0) {
    if (handler.initialized) {
      console.log('Reloading server bundle must be implemented by restarting the node process!');
      return;
    }

    // eslint-disable-next-line global-require
    require(bundlePath + bundleFileName);
    console.log(`Loaded server bundle: ${bundlePath + bundleFileName}`);
    handler.initialize();
  }
});

const unixServer = net.createServer((connection) => {
  handler.handle(connection);
});

unixServer.listen('node.sock');

process.on('SIGINT', () => {
  unixServer.close();
  process.exit();
});

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
react_on_rails-6.2.1 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.2.1.rc.3 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.2.1.rc.2 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.2.1.rc.1 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.2.0 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.1.2 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.1.1 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.1.1.rc.1 lib/generators/react_on_rails/templates/node/base/client/node/server.js
react_on_rails-6.1.0 lib/generators/react_on_rails/templates/node/base/client/node/server.js