<commit_before>import fs from 'fs';
import http from 'http';
import crypto from 'crypto';
import _ from 'lodash';

import { Server as WebSocketServer } from 'ws';
import 'babel-polyfill';

import BuildPlugin from './buildPlugin';

/* */

// process.argv[0] <-- full node binary path
// process.argv[1] <-- full path for this file
const ACCEL_ID = process.argv[2];
const HOT_PORT = process.argv[3];

process.env.INSIDE_ACCELERATOR = true;

console.log('=> Starting gadicc:ecmascript-hot Accelerator(' + ACCEL_ID
  + ') on port ' + HOT_PORT + '.\n');

function log(/* arguments */) {
  var args = Array.prototype.slice.call(arguments);
  var pre = '\n[gadicc:hot] Accelerator(' + ACCEL_ID + '): ';

  if (typeof args[0] === 'string')
    args[0] =  pre + args[0];
  else
    args.splice(0, 0, pre);

  console.log.apply(console, args);
}

const server = http.createServer(function (req, res) {
  var hash = req.url.match(/^\/hot.js\?hash=(.*)$/);
  if (!(hash && (hash=hash[1]) && hot.bundles[hash])) {
    res.writeHead(404);
    res.end();
    return;
  }

  res.writeHead(200, {'Content-Type': 'application/javascript; charset=UTF-8'});
  res.end(hot.bundles[hash].contents, 'utf8');
}).listen(HOT_PORT);

const wss = new WebSocketServer({ server: server });

// straight out of https://www.npmjs.com/package/ws
wss.broadcast = function broadcast(data) {
  wss.clients.forEach(function each(client) {
    client.send(data);
  });
};

const handlers = {};

process.on('disconnect', function() {
  // unclear from docs if this works within the child!
  log('disconnect event received, exiting.');
  handlers.close();
});

process.on('message', function(msg) {
  if (handlers[msg.type])
    return handlers[msg.type](msg,
      BuildPlugin.byId(msg.pluginId));

  log('unknown message: ' + JSON.stringify(msg));
});

handlers.close = function() {
  try {
    wss.close();
  } catch (err) {
    log('error closing websocket server (safe to ignore)');
    log('message', err.message);
    log('name', err.name);
  }

  try {
    server.close();
  } catch (err) {
    if (err.message !== 'Not running')
      throw err;
  }

  if (process.connected) {
    process.send({type: 'closed'});
    process.disconnect();
  }
  process.exit();
};

var bundleQueue = [];
function addJavaScript(source) {
  bundleQueue.push(source);
  hot.process(bundleQueue);
  bundleQueue = [];
}

handlers.PLUGIN_INIT = function({id, name, path}) {
  new BuildPlugin(id, name, path, addJavaScript);
}

handlers.setDiskCacheDirectory = function({dir}, plugin) {
  plugin.setDiskCacheDirectory(dir);

  if (plugin.compiler._hotInitFakeCompile) {
    plugin.compiler._hotInitFakeCompile();
  }
};<commit_msg>accelerator: for local packages, delay PLUGIN_INIT until load confirmation<commit_after>import fs from 'fs';
import http from 'http';
import crypto from 'crypto';
import _ from 'lodash';

import { Server as WebSocketServer } from 'ws';
import 'babel-polyfill';

import BuildPlugin from './buildPlugin';

/* */

// process.argv[0] <-- full node binary path
// process.argv[1] <-- full path for this file
const ACCEL_ID = process.argv[2];
const HOT_PORT = process.argv[3];

process.env.INSIDE_ACCELERATOR = true;

console.log('=> Starting gadicc:ecmascript-hot Accelerator(' + ACCEL_ID
  + ') on port ' + HOT_PORT + '.\n');

function log(/* arguments */) {
  var args = Array.prototype.slice.call(arguments);
  var pre = '\n[gadicc:hot] Accelerator(' + ACCEL_ID + '): ';

  if (typeof args[0] === 'string')
    args[0] =  pre + args[0];
  else
    args.splice(0, 0, pre);

  console.log.apply(console, args);
}

const server = http.createServer(function (req, res) {
  var hash = req.url.match(/^\/hot.js\?hash=(.*)$/);
  if (!(hash && (hash=hash[1]) && hot.bundles[hash])) {
    res.writeHead(404);
    res.end();
    return;
  }

  res.writeHead(200, {'Content-Type': 'application/javascript; charset=UTF-8'});
  res.end(hot.bundles[hash].contents, 'utf8');
}).listen(HOT_PORT);

const wss = new WebSocketServer({ server: server });

// straight out of https://www.npmjs.com/package/ws
wss.broadcast = function broadcast(data) {
  wss.clients.forEach(function each(client) {
    client.send(data);
  });
};

const handlers = {};

process.on('disconnect', function() {
  // unclear from docs if this works within the child!
  log('disconnect event received, exiting.');
  handlers.close();
});

process.on('message', function(msg) {
  if (msg.type === 'PLUGIN_INIT')
    return handlers[msg.type](msg);
  else if (handlers[msg.type]) {
    // set during devel to make sure plugin has finished building itself
    // before we try load it.
    if (initQueue[msg.pluginId]) {
      let {id, name, path} = initQueue[msg.pluginId];
      new BuildPlugin(id, name, path, addJavaScript);
      delete initQueue[msg.pluginId];
    }

    return handlers[msg.type](msg,
      BuildPlugin.byId(msg.pluginId));
  }

  log('unknown message: ' + JSON.stringify(msg));
});

handlers.close = function() {
  try {
    wss.close();
  } catch (err) {
    log('error closing websocket server (safe to ignore)');
    log('message', err.message);
    log('name', err.name);
  }

  try {
    server.close();
  } catch (err) {
    if (err.message !== 'Not running')
      throw err;
  }

  if (process.connected) {
    process.send({type: 'closed'});
    process.disconnect();
  }
  process.exit();
};

var bundleQueue = [];
function addJavaScript(source) {
  bundleQueue.push(source);
  hot.process(bundleQueue);
  bundleQueue = [];
}

const initQueue = {};
handlers.PLUGIN_INIT = function({id, name, path}) {
  // During devel the package might still be building itself at init time.
  if (path.match(/local/))
    initQueue[id] = {id, name, path};
  else
    new BuildPlugin(id, name, path, addJavaScript);
}

handlers.setDiskCacheDirectory = function({dir}, plugin) {
  plugin.setDiskCacheDirectory(dir);

  if (plugin.compiler._hotInitFakeCompile) {
    plugin.compiler._hotInitFakeCompile();
  }
};
{'fraction_non_alphanumeric': 0.17103054716347768, 'fraction_numerical': 0.006042296072507553, 'mean_word_length': 3.8017727639000807, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2860662', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1998, 'n_words': 467}