<commit_before>// jshint varstmt: false
var fs = require('fs');
var gutil = require('gulp-util');
var getStage = require('./stage');
var getEnvironment = require('./environment.js');
var browserSync = require('browser-sync').create('Toolkit');

var bsServer;
var serverStatus;

function server(done) {

  var stage = getStage();

  var timestamps = true;

  var codeSync = true;
  var minify = false;
  var logLevel = 'info';
  var notify = true;
  var logConnections = true;
  var injectChanges = true;
  var ui = {};
  var logFileChanges = true;

  if (stage === 'production') {
    codeSync = false;
    minify = true;
    logLevel = 'info';
    notify = false;
    injectChanges = false;
    ui = false;
  }

  var server = {
    baseDir: './app'
  };

  if (stage === 'develop') {
    server.middleware = devMiddleware;
  } else {
    server.middleware = middleware;
    server.routes = {
      '/.well-known/runtime': 'node_modules/runtime-browser/bin'
    };

    gutil.log('|******************************************************************************|');
    gutil.log('|                                                                              |');
    gutil.log('|                     THE TOOLKIT IS IN PRODUCTION MODE                        |');
    gutil.log('|       THE LOCAL CATALOGUE WAS NOT ACCESSIBLE, WILL BE USED THE REMOTE        |');
    gutil.log('|                     BASED ON YOUR CONFIGURATION FILE                         |');
    gutil.log('|                                                                              |');
    gutil.log('|******************************************************************************|');
  }

  // Serve files from the root of this project
  bsServer = browserSync.init({
    open: false,
    online: false,
    timestamps: timestamps,
    logLevel: logLevel,
    cors: true,
    logFileChanges: logFileChanges,
    port: process.env.PORT || 443,
    minify: minify,
    notify: notify,
    ui: ui,
    injectChanges: injectChanges,
    ghostMode: false,
    https: {
      key: 'rethink-certificate.key',
      cert: 'rethink-certificate.cert'
    },
    logConnections: logConnections,
    codeSync: codeSync,
    server: server
  }, function(err) {
    if (err) {
      gutil.log('Check the documentation on Gulp Task.');
      gutil.log('Or open an issue here https://github.com/reTHINK-project/dev-hyperty-toolkit/issues');
      done(err);
    }

    if (serverStatus === 'open') {
      browserSync.exit();

      gutil.log('|******************************************************************************|');
      gutil.log('|                                                                              |');
      gutil.log('|                         THE TOOLKIT WILL STOP RUNNING.                       |');
      gutil.log('|              CHECK IF YOU HAVE OTHER SEVICE RUNNING ON PORT 443.             |');
      gutil.log('|                                                                              |');
      gutil.log('|******************************************************************************|');

      return false;
    }

    browserSync.reload();
    done();
  });

  checkStatus();

}

function checkStatus() {
  bsServer.utils.portscanner.checkPortStatus(443, {}, function(err, status) {
    if (err) throw Error(err);

    serverStatus = status;
  });
}

function middleware(req, res, next) {
  gutil.log(req.originalUrl);
  res.setHeader('Access-Control-Allow-Origin', '*');
  return next();
}

function devMiddleware(req, res, next) {
  var paths;
  gutil.log(req.originalUrl);
  res.setHeader('Access-Control-Allow-Origin', '*');

  if (req.originalUrl.includes('.well-known')) {
    var path = req.originalUrl;
    var pathIndex = path.indexOf('.well-known');
    if (pathIndex !== -1) { path = path.substr(pathIndex - 1); }
    paths = path.split('/');
    var type = paths[2];
    var resource = paths[3];

    if (req.originalUrl.includes('index.html') || req.originalUrl.includes('.js')) {
      if (req.originalUrl.includes('index.html')) {
        res.writeHeader(200, {'Content-Type': 'text/html'});
        res.end(fs.readFileSync('node_modules/runtime-browser/bin/index.html', 'utf8'));
      } else {
        res.writeHeader(200, {'Content-Type': 'application/javascript'});
        res.end(fs.readFileSync('node_modules/runtime-browser/bin/' + resource, 'utf8'));
      }

    } else if (req.originalUrl.includes('sourcepackage')) {
      // paths = req.originalUrl.split('/');<commit_msg>Add sandbox.html resource to gulp server<commit_after>// jshint varstmt: false
var fs = require('fs');
var gutil = require('gulp-util');
var getStage = require('./stage');
var getEnvironment = require('./environment.js');
var browserSync = require('browser-sync').create('Toolkit');

var bsServer;
var serverStatus;

function server(done) {

  var stage = getStage();

  var timestamps = true;

  var codeSync = true;
  var minify = false;
  var logLevel = 'info';
  var notify = true;
  var logConnections = true;
  var injectChanges = true;
  var ui = {};
  var logFileChanges = true;

  if (stage === 'production') {
    codeSync = false;
    minify = true;
    logLevel = 'info';
    notify = false;
    injectChanges = false;
    ui = false;
  }

  var server = {
    baseDir: './app'
  };

  if (stage === 'develop') {
    server.middleware = devMiddleware;
  } else {
    server.middleware = middleware;
    server.routes = {
      '/.well-known/runtime': 'node_modules/runtime-browser/bin'
    };

    gutil.log('|******************************************************************************|');
    gutil.log('|                                                                              |');
    gutil.log('|                     THE TOOLKIT IS IN PRODUCTION MODE                        |');
    gutil.log('|       THE LOCAL CATALOGUE WAS NOT ACCESSIBLE, WILL BE USED THE REMOTE        |');
    gutil.log('|                     BASED ON YOUR CONFIGURATION FILE                         |');
    gutil.log('|                                                                              |');
    gutil.log('|******************************************************************************|');
  }

  // Serve files from the root of this project
  bsServer = browserSync.init({
    open: false,
    online: false,
    timestamps: timestamps,
    logLevel: logLevel,
    cors: true,
    logFileChanges: logFileChanges,
    port: process.env.PORT || 443,
    minify: minify,
    notify: notify,
    ui: ui,
    injectChanges: injectChanges,
    ghostMode: false,
    https: {
      key: 'rethink-certificate.key',
      cert: 'rethink-certificate.cert'
    },
    logConnections: logConnections,
    codeSync: codeSync,
    server: server
  }, function(err) {
    if (err) {
      gutil.log('Check the documentation on Gulp Task.');
      gutil.log('Or open an issue here https://github.com/reTHINK-project/dev-hyperty-toolkit/issues');
      done(err);
    }

    if (serverStatus === 'open') {
      browserSync.exit();

      gutil.log('|******************************************************************************|');
      gutil.log('|                                                                              |');
      gutil.log('|                         THE TOOLKIT WILL STOP RUNNING.                       |');
      gutil.log('|              CHECK IF YOU HAVE OTHER SEVICE RUNNING ON PORT 443.             |');
      gutil.log('|                                                                              |');
      gutil.log('|******************************************************************************|');

      return false;
    }

    browserSync.reload();
    done();
  });

  checkStatus();

}

function checkStatus() {
  bsServer.utils.portscanner.checkPortStatus(443, {}, function(err, status) {
    if (err) throw Error(err);

    serverStatus = status;
  });
}

function middleware(req, res, next) {
  gutil.log(req.originalUrl);
  res.setHeader('Access-Control-Allow-Origin', '*');
  return next();
}

function devMiddleware(req, res, next) {
  var paths;
  gutil.log(req.originalUrl);
  res.setHeader('Access-Control-Allow-Origin', '*');

  if (req.originalUrl.includes('.well-known')) {
    var path = req.originalUrl;
    var pathIndex = path.indexOf('.well-known');
    if (pathIndex !== -1) { path = path.substr(pathIndex - 1); }
    paths = path.split('/');
    var type = paths[2];
    var resource = paths[3];

    if (req.originalUrl.includes('.html') || req.originalUrl.includes('.js')) {
      if (req.originalUrl.includes('sandbox.html')) {
        res.writeHeader(200, {'Content-Type': 'text/html'});
        res.end(fs.readFileSync('node_modules/runtime-browser/bin/sandbox.html', 'utf8'));
      } else if (req.originalUrl.includes('index.html')) {
        res.writeHeader(200, {'Content-Type': 'text/html'});
        res.end(fs.readFileSync('node_modules/runtime-browser/bin/index.html', 'utf8'));
      } else {
        res.writeHeader(200, {'Content-Type': 'application/javascript'});
        res.end(fs.readFileSync('node_modules/runtime-browser/bin/' + resource, 'utf8'));
      }

    } else if (req.originalUrl.includes('sourcepackage')) {
      // paths = req.originalUrl.split('/');
{'fraction_non_alphanumeric': 0.20488701481241214, 'fraction_numerical': 0.004973510649799978, 'mean_word_length': 2.2410651716888577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453543', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2565, 'n_words': 564}