<commit_before>(function() {
    'use strict';

    if (!global.cloudcmd)
        return console.log(
             '# server.js'                                      + '\n'  +
             '# -----------'                                    + '\n'  +
             '# Module is part of Cloud Commander,'             + '\n'  +
             '# easy to use web server.'                        + '\n'  +
             '# http://cloudcmd.io'                             + '\n');

    var main                = global.cloudcmd.main,

        DIR                 = main.DIR,
        LIBDIR              = main.LIBDIR,
        SRVDIR              = main.SRVDIR,

        URL                 = main.url,
        Path                = main.path,
        Querystring         = main.querystring,

        Minify              = main.minify,
        CloudFunc           = main.cloudfunc,
        AppCache            = main.appcache,
        Socket              = main.socket,

        http                = main.http,
        https               = main.https,
        Util                = main.util,
        express             = main.express,
        expressApp          = express.getApp(controller),
        files               = main.files,

        Server, Rest, Route;

    /* базовая инициализация  */
    function init(pAppCachProcessing) {
        var config = main.config;

        /* создаём файл app cache */
        if (config.appcache  && AppCache && config.server )
            Util.exec( pAppCachProcessing );
    }

    /**
     * start server function
     * @param pConfig
     * @param pProcessing {index, appcache, rest}
     */
    function start(pProcessing) {
        var lConfig         = main.config;

        if (!pProcessing)
            pProcessing = {};

        Rest            =   pProcessing.rest;
        Route           =   pProcessing.route;

        init(pProcessing.appcache);

        var lPort       =   process.env.PORT            ||  /* c9           */
                            process.env.app_port        ||  /* nodester     */
                            process.env.VCAP_APP_PORT   ||  /* cloudfoundry */
                            lConfig.port,

            lIP         =   process.env.IP              ||  /* c9           */
                            lConfig.ip                  ||
                            (main.WIN32 ? '127.0.0.1' : '0.0.0.0'),

            lSSL        = pProcessing.ssl,
            lSSLPort    = lConfig.sslPort,
            lHTTP       = 'http://',
            lHTTPS      = 'https://',

            lSockets    = function(pServer) {
                var lListen, msg, status;

                if (lConfig.socket && Socket)
                    lListen = Socket.listen(pServer);

                status  = lListen ? 'on' : 'off';
                msg     = CloudFunc.formatMsg('sockets', '', status);

                Util.log(msg);
            },

            lHTTPServer = function() {
                Server =  http.createServer(expressApp || controller);
                Server.on('error', Util.log);
                Server.listen(lPort, lIP);
                lServerLog(lHTTP, lPort);
                lSockets(Server);
            },

            lServerLog  = function(pHTTP, pPort) {
                Util.log('* Server running at ' + pHTTP + lIP + ':' + pPort);
            };
        /* server mode or testing mode */
        if (lConfig.server) {
            if (lSSL) {
                Util.log('* Redirection http -> https is setted up');
                lServerLog(lHTTP, lPort);
                var lRedirectServer =  http.createServer( function(pReq, pRes) {
                     var lURL,
                        lHost       = pReq.headers.host,
                        lParsed     = URL.parse(lHost),
                        lHostName   = lParsed.protocol;

                    lURL            = lHTTPS + lHostName + lSSLPort + pReq.url;

                    main.redirect({
                        response: pRes,
                        url: lURL
                    });
                });

                lRedirectServer.listen(lPort, lIP);

                Server  =  https.createServer(lSSL, controller);
                Server.on('error', function (pError) {
                    Util.log('Could not use https port: ' + lSSLPort);
                    Util.log(pError);
                    lRedirectServer.close();
                    Util.log('* Redirection http -> https removed');
                    lHTTPServer();
                });

                lSockets(Server);

                Server.listen(lSSLPort, lIP);
                lServerLog(lHTTPS, lSSLPort);
            } else
                lHTTPServer();
        } else
            Util.log('Cloud Commander testing mode');
    }

    /**
     * Главная функция, через которую проихсодит
     * взаимодействие, обмен данными с клиентом
     * @param req - запрос клиента (Request)
     * @param res - ответ сервера (Response)
     */
    function controller(pReq, pRes) {
        /* Читаем содержимое папки, переданное в url */
        var lRet, lName, lMin, lExt, lResult,
            lConfig     = main.config,
            lParsedUrl  = URL.parse(pReq.url),
            lQuery      = lParsedUrl.search || '',
            lPath       = lParsedUrl.pathname;

        /* added supporting of Russian language in directory names */
        lPath = Querystring.unescape(lPath);

        if (!expressApp)
            Util.log(pReq.method + ' ' + lPath + lQuery);

        var lData = {
            name        : lPath,
            request     : pReq,
            response    : pRes
        };

        if (lConfig.rest )
            lRet = Util.exec(Rest, lData);

        if (!lRet && Route)
            lRet = Util.exec(Route, lData);

        if (!lRet)
            lRet = combine(lData);

        if (!lRet) {
            lName = lData.name;

            /* watching is file changed */
            if (lConfig.appcache)
                AppCache.watch(lName);

            lName   = Path.join(DIR, lName);
            lMin    = lConfig.minify,
            lExt    = Util.getExtension(lName),
            lResult = lMin && Util.strCmp(lExt, ['.js', '.css', '.html']);

            Util.ifExec(!lResult,
                function(pParams) {
                    var lSendName = pParams && pParams.name || lName;

                    main.sendFile({
                        name        : lSendName,
                        cache       : lConfig.cache,
                        gzip        : true,
                        request     : pReq,
                        response    : pRes
                    });
            }, function(pCallBack) {
                Minify.optimize(lName, {
                    callback    : pCallBack,
                    returnName  : true
                });
            });
        }
    }

    function combine(params) {
        var names, i, n, name, minName,
            config      = main.config,
            dir         = DIR,
            p           = params,
            path        = params.name,
            COMBINE     = '/combine/',
            isCombine   = Util.isContainStrAtBegin(path, COMBINE);

        if (isCombine) {
            path        = Util.removeStrOneTime(path, COMBINE);
            names       = path.split(':');
            n           = names.length;

            if (config.minify)
                for (i = 0; i < n; i++) {
                    name        = Path.join(DIR, names[i]);
                    minName     = Minify.getName(name);
                    if (name !== minName) {
                        names[i] = minName;
                        dir = '';
                    }
                }

            console.log(names);

            files.readPipe(names, dir, p.response, function(error) {
                if (error)
                    main.sendError(params, error);
            });
        }

        return isCombine;
    }

    exports.start           = start;

})();
<commit_msg>chore(server) rm console.log<commit_after>(function() {
    'use strict';

    if (!global.cloudcmd)
        return console.log(
             '# server.js'                                      + '\n'  +
             '# -----------'                                    + '\n'  +
             '# Module is part of Cloud Commander,'             + '\n'  +
             '# easy to use web server.'                        + '\n'  +
             '# http://cloudcmd.io'                             + '\n');

    var main                = global.cloudcmd.main,

        DIR                 = main.DIR,
        LIBDIR              = main.LIBDIR,
        SRVDIR              = main.SRVDIR,

        URL                 = main.url,
        Path                = main.path,
        Querystring         = main.querystring,

        Minify              = main.minify,
        CloudFunc           = main.cloudfunc,
        AppCache            = main.appcache,
        Socket              = main.socket,

        http                = main.http,
        https               = main.https,
        Util                = main.util,
        express             = main.express,
        expressApp          = express.getApp(controller),
        files               = main.files,

        Server, Rest, Route;

    /* базовая инициализация  */
    function init(pAppCachProcessing) {
        var config = main.config;

        /* создаём файл app cache */
        if (config.appcache  && AppCache && config.server )
            Util.exec( pAppCachProcessing );
    }

    /**
     * start server function
     * @param pConfig
     * @param pProcessing {index, appcache, rest}
     */
    function start(pProcessing) {
        var lConfig         = main.config;

        if (!pProcessing)
            pProcessing = {};

        Rest            =   pProcessing.rest;
        Route           =   pProcessing.route;

        init(pProcessing.appcache);

        var lPort       =   process.env.PORT            ||  /* c9           */
                            process.env.app_port        ||  /* nodester     */
                            process.env.VCAP_APP_PORT   ||  /* cloudfoundry */
                            lConfig.port,

            lIP         =   process.env.IP              ||  /* c9           */
                            lConfig.ip                  ||
                            (main.WIN32 ? '127.0.0.1' : '0.0.0.0'),

            lSSL        = pProcessing.ssl,
            lSSLPort    = lConfig.sslPort,
            lHTTP       = 'http://',
            lHTTPS      = 'https://',

            lSockets    = function(pServer) {
                var lListen, msg, status;

                if (lConfig.socket && Socket)
                    lListen = Socket.listen(pServer);

                status  = lListen ? 'on' : 'off';
                msg     = CloudFunc.formatMsg('sockets', '', status);

                Util.log(msg);
            },

            lHTTPServer = function() {
                Server =  http.createServer(expressApp || controller);
                Server.on('error', Util.log);
                Server.listen(lPort, lIP);
                lServerLog(lHTTP, lPort);
                lSockets(Server);
            },

            lServerLog  = function(pHTTP, pPort) {
                Util.log('* Server running at ' + pHTTP + lIP + ':' + pPort);
            };
        /* server mode or testing mode */
        if (lConfig.server) {
            if (lSSL) {
                Util.log('* Redirection http -> https is setted up');
                lServerLog(lHTTP, lPort);
                var lRedirectServer =  http.createServer( function(pReq, pRes) {
                     var lURL,
                        lHost       = pReq.headers.host,
                        lParsed     = URL.parse(lHost),
                        lHostName   = lParsed.protocol;

                    lURL            = lHTTPS + lHostName + lSSLPort + pReq.url;

                    main.redirect({
                        response: pRes,
                        url: lURL
                    });
                });

                lRedirectServer.listen(lPort, lIP);

                Server  =  https.createServer(lSSL, controller);
                Server.on('error', function (pError) {
                    Util.log('Could not use https port: ' + lSSLPort);
                    Util.log(pError);
                    lRedirectServer.close();
                    Util.log('* Redirection http -> https removed');
                    lHTTPServer();
                });

                lSockets(Server);

                Server.listen(lSSLPort, lIP);
                lServerLog(lHTTPS, lSSLPort);
            } else
                lHTTPServer();
        } else
            Util.log('Cloud Commander testing mode');
    }

    /**
     * Главная функция, через которую проихсодит
     * взаимодействие, обмен данными с клиентом
     * @param req - запрос клиента (Request)
     * @param res - ответ сервера (Response)
     */
    function controller(pReq, pRes) {
        /* Читаем содержимое папки, переданное в url */
        var lRet, lName, lMin, lExt, lResult,
            lConfig     = main.config,
            lParsedUrl  = URL.parse(pReq.url),
            lQuery      = lParsedUrl.search || '',
            lPath       = lParsedUrl.pathname;

        /* added supporting of Russian language in directory names */
        lPath = Querystring.unescape(lPath);

        if (!expressApp)
            Util.log(pReq.method + ' ' + lPath + lQuery);

        var lData = {
            name        : lPath,
            request     : pReq,
            response    : pRes
        };

        if (lConfig.rest )
            lRet = Util.exec(Rest, lData);

        if (!lRet && Route)
            lRet = Util.exec(Route, lData);

        if (!lRet)
            lRet = combine(lData);

        if (!lRet) {
            lName = lData.name;

            /* watching is file changed */
            if (lConfig.appcache)
                AppCache.watch(lName);

            lName   = Path.join(DIR, lName);
            lMin    = lConfig.minify,
            lExt    = Util.getExtension(lName),
            lResult = lMin && Util.strCmp(lExt, ['.js', '.css', '.html']);

            Util.ifExec(!lResult,
                function(pParams) {
                    var lSendName = pParams && pParams.name || lName;

                    main.sendFile({
                        name        : lSendName,
                        cache       : lConfig.cache,
                        gzip        : true,
                        request     : pReq,
                        response    : pRes
                    });
            }, function(pCallBack) {
                Minify.optimize(lName, {
                    callback    : pCallBack,
                    returnName  : true
                });
            });
        }
    }

    function combine(params) {
        var names, i, n, name, minName,
            config      = main.config,
            dir         = DIR,
            p           = params,
            path        = params.name,
            COMBINE     = '/combine/',
            isCombine   = Util.isContainStrAtBegin(path, COMBINE);

        if (isCombine) {
            path        = Util.removeStrOneTime(path, COMBINE);
            names       = path.split(':');
            n           = names.length;

            if (config.minify)
                for (i = 0; i < n; i++) {
                    name        = Path.join(DIR, names[i]);
                    minName     = Minify.getName(name);
                    if (name !== minName) {
                        names[i] = minName;
                        dir = '';
                    }
                }

            files.readPipe(names, dir, p.response, function(error) {
                if (error)
                    main.sendError(params, error);
            });
        }

        return isCombine;
    }

    exports.start           = start;

})();
<|endoftext|>


<commit_before>const port = '8080';
const host = '0.0.0.0';
const express = require('express');
const app = express();

const rules = require('./app');
app.use(rules);

app.listen(port, host);

console.log('Server running on %s:%d...', host, port);
<commit_msg>Prepare app for Heroku deploy<commit_after>const port = process.env.PORT || '8080';
const host = process.env.HOST || '0.0.0.0';
const express = require('express');
const app = express();

const rules = require('./app');
app.use(rules);

app.listen(port, host);

console.log('Server running on %s:%d...', host, port);
<|endoftext|>


<commit_before>var gulp = require('gulp');

var fs = require('fs');
var path = require('path');
var autoprefixer = require('autoprefixer');
var browserSync = require('browser-sync').create();
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var debug = require('gulp-debug');
var postcss = require('gulp-postcss');
var rename = require("gulp-rename");
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var surge = require('gulp-surge');
var watch = require('gulp-watch');

gulp.task('cssclean', function(){
	return gulp.src('dist/*.css', { read: false }).pipe(clean());
});

gulp.task('csscompile', ['cssclean'], function(){
	return gulp
		.src('src/sass/index.scss')
		.pipe(sass().on('error', sass.logError))
		.pipe(sourcemaps.init())
		.pipe(postcss([autoprefixer()]))
		.pipe(sourcemaps.write('.'))
		.pipe(gulp.dest('dist'))
		.pipe(browserSync.stream());
});

gulp.task('jsclean', function(){
	return gulp.src('dist/*.js', { read: false }).pipe(clean());
});

gulp.task('jsconcat', ['jsclean'], function(){
	return gulp.src([
		'src/js/highlight.pack.js',
		'src/js/jquery-3.1.1.min.js',
		'src/js/marked.js',
		'src/js/flat-file.js',
		'src/js/gitter.js'
	])
		.pipe(concat('all.js'))
		.pipe(gulp.dest('dist'))
		.pipe(browserSync.stream());
});

gulp.task('lekceclean', function(){
	return gulp
		.src(['lekce/**/*.html', '!lekce/*.html'], { read: false })
		.pipe(clean());
});

// function to get list of future html files from .md present
var getHtmlFileList = function(dir, filelist){
	var files = fs.readdirSync(dir); // where to find .md
	filelist = filelist || [];

	files.forEach(function(file){
		if (fs.statSync(path.join(dir, file)).isDirectory()){
			filelist = getHtmlFileList(path.join(dir, file), filelist);
		} else if (path.parse(file).ext == '.md'){
			filelist.push(path.join(dir, path.parse(file).name + '.html'));
		}
	});

	return filelist;
};

gulp.task('lekcecreate', ['lekceclean'], function(){
	var filelist = getHtmlFileList('lekce');

	var p = gulp.src('lekce/sablona.html'); // source to be copied

	// copy source to each location on the list
	for (var i = 0; i < filelist.length; i++){
		p = p
			.pipe(rename(filelist[i]))
			.pipe(gulp.dest(''));
	}

	// return and reload browserSync
	return p.pipe(browserSync.stream());
});

gulp.task('develop', ['distprep'], function(){
	browserSync.init({
		server: {
			baseDir: './',
			serveStaticOptions: {
				extensions: ['html']
			}
		},
		open: false,
		port: 3030,
		files:['*.html'] // only root html files watched
		// for other files browserSync is called manually via browserSync.stream()
	});

	gulp.watch('src/sass/*.scss', ['csscompile']);
	gulp.watch('src/js/*.js', ['jsconcat']);
	gulp.watch(['lekce/**/*.md', 'lekce/sablona.html'])
		.on('change', function(event){
			if (event.type != 'renamed'){ // preventing multiple calls
				gulp.start('lekcecreate');
			}
		});
});

gulp.task('distprep', ['csscompile', 'jsconcat', 'lekcecreate']);

gulp.task('deploy', ['distprep'], function(){
	return surge({
		project: './',
		domain: 'webkurz.pyladies.cz'
	})
});

gulp.task('default', ['develop']);
<commit_msg>refactor: whitespace to spaces<commit_after>var gulp = require('gulp');

var fs = require('fs');
var path = require('path');
var autoprefixer = require('autoprefixer');
var browserSync = require('browser-sync').create();
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var debug = require('gulp-debug');
var postcss = require('gulp-postcss');
var rename = require("gulp-rename");
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var surge = require('gulp-surge');
var watch = require('gulp-watch');

gulp.task('cssclean', function(){
    return gulp.src('dist/*.css', { read: false }).pipe(clean());
});

gulp.task('csscompile', ['cssclean'], function(){
    return gulp
        .src('src/sass/index.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(sourcemaps.init())
        .pipe(postcss([autoprefixer()]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('dist'))
        .pipe(browserSync.stream());
});

gulp.task('jsclean', function(){
    return gulp.src('dist/*.js', { read: false }).pipe(clean());
});

gulp.task('jsconcat', ['jsclean'], function(){
    return gulp.src([
        'src/js/highlight.pack.js',
        'src/js/jquery-3.1.1.min.js',
        'src/js/marked.js',
        'src/js/flat-file.js',
        'src/js/gitter.js'
    ])
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist'))
        .pipe(browserSync.stream());
});

gulp.task('lekceclean', function(){
    return gulp
        .src(['lekce/**/*.html', '!lekce/*.html'], { read: false })
        .pipe(clean());
});

// function to get list of future html files from .md present
var getHtmlFileList = function(dir, filelist){
    var files = fs.readdirSync(dir); // where to find .md
    filelist = filelist || [];

    files.forEach(function(file){
        if (fs.statSync(path.join(dir, file)).isDirectory()){
            filelist = getHtmlFileList(path.join(dir, file), filelist);
        } else if (path.parse(file).ext == '.md'){
            filelist.push(path.join(dir, path.parse(file).name + '.html'));
        }
    });

    return filelist;
};

gulp.task('lekcecreate', ['lekceclean'], function(){
    var filelist = getHtmlFileList('lekce');

    var p = gulp.src('lekce/sablona.html'); // source to be copied

    // copy source to each location on the list
    for (var i = 0; i < filelist.length; i++){
        p = p
            .pipe(rename(filelist[i]))
            .pipe(gulp.dest(''));
    }

    // return and reload browserSync
    return p.pipe(browserSync.stream());
});

gulp.task('develop', ['distprep'], function(){
    browserSync.init({
        server: {
            baseDir: './',
            serveStaticOptions: {
                extensions: ['html']
            }
        },
        open: false,
        port: 3030,
        files: ['*.html'] // only root html files watched
        // for other files browserSync is called manually via browserSync.stream()
    });

    gulp.watch('src/sass/*.scss', ['csscompile']);
    gulp.watch('src/js/*.js', ['jsconcat']);
    gulp.watch(['lekce/**/*.md', 'lekce/sablona.html'])
        .on('change', function(event){
            if (event.type != 'renamed'){ // preventing multiple calls
                gulp.start('lekcecreate');
            }
        });
});

gulp.task('distprep', ['csscompile', 'jsconcat', 'lekcecreate']);

gulp.task('deploy', ['distprep'], function(){
    return surge({
        project: './',
        domain: 'webkurz.pyladies.cz'
    })
});

gulp.task('default', ['develop']);
<|endoftext|>


<commit_before>var express = require('express');
var http = require('http');
var WebSocketServer = require('ws').Server;
var EventEmitter = require('events').EventEmitter;
var util = require('./util');
var url = require('url');

function PeerKitServer(options) {
  if (!(this instanceof PeerKitServer)) return new PeerKitServer(options);
  EventEmitter.call(this);

  this._app = express();
  this._httpServer = http.createServer(this._app);

  this._options = util.extend({
    port: 8080,
    debug: true,
    multiplicity: 5
  }, options);

  util.debug = this._options.debug;

  // Connected clients
  /**
   * A mapping of client id to client information object and ws information
   */
  this._clients = {};

  /**
   * A mapping of resource url to a mapping of clients and boolean if they have it or not
   */
  this._resources = {};

  /**
   * A mapping of a given resource url to its size
   */
  this._resourceSizes = {};

  this._initializeWSS();

  this._initializeHTTP();
  this._httpServer.listen(this._options.port);

};

util.inherits(PeerKitServer, EventEmitter);

PeerKitServer.prototype._initializeWSS = function(){

  var self = this;

  // Create WebSocket server as well.
  this._wss = new WebSocketServer({ path: '/ws', server: this._httpServer });

  this._wss.on('connection', function(socket) {
    //var query = url.parse(socket.upgradeReq.url, true).query;
    var client = {
      id: self._generateId(),
      socket: socket,
      resources: {},
      conns: []
    };
    self._clients[client.id] = client;
    socket.on('message', function(data){
      util.log(data);
      var message;
      try {
        message = JSON.parse(data);
        if (!message.type) {
          throw new Error('Invalid message format: missing type');
        }
      } catch(e) {
        if (self.debug) {
          throw e;
        }
        util.log('Invalid message', data);
        return;
      }
      switch (message.type) {
        case 'RESPONSE':
          // Response to offer with ICE and answer
          //
          // message.id   (String, id of destination)
          // message.data (String, JSON of ice and answer)
          //
          var resp = self._forward(message.id, message.data);
          /*if (resp.error) {
            socket.send(JSON.stringify(
              type: 'ERROR-CLIENT-DEAD',

            ));
          }*/
          break;
        case 'REPORT':
          // New resource available
          //
          // message.url (String, resource absolute url)
          //
          self._reportResource(client, message.resource);
          break;
        case 'REQUEST':
          // Asking for resource
          //
          // message.url (String, resource absolute url)
          //
          var conns = self._requestResource(message.url);
          socket.send(JSON.stringify(conns));
          break;
        case 'CONN':
          // Connection information to store
          //
          // message.conn (mixed data)
          //
          client.conns.push(message.conn);
          break;
        case 'SERVED':
          // Served a file, increment analytics
          break;
        default:
          util.log('Message unrecognized');
      }
    });
    socket.on('close', function() {
      util.log('Socket closed:', client.id);
      var resources = client.resources;
      var hashes = Object.keys(resources);
      var ii = hashes.length;
      for (var i = 0; i < ii; i++) {
        delete self._resources[hashes[i]][client.id];
      }
      delete self._clients[client.id];
    });
  });
};

PeerKitServer.prototype._initializeHTTP = function(){
  var self = this;

  /*this._app.post('/response', function(req, res){
    // Responding to an offer with ICE and answer
    //
    // params.id    (String, id of destination)
    // body.data    (String, JSON of ice and answer
    //
    var resp = self._forward(req.params.id, req.body.data);
    res.send(resp);
  });*/
  this._app.enable("jsonp callback");

  this._app.get('/requestResource', function(req, res) {
    var urls = req.query.urls;
    if (!Array.isArray(urls)) {
        res.send("need array", 401);
    }
    var data = {};
    var ii = urls.length;
    for (var i = 0; i < ii; i++) {
      data[urls[i]] = self._requestResource(urls[i]);
    }
    res.jsonp(data);
    // Get Javascript library along with necessary data
  });
};

/**
 * Tells the server that we have the resource at given url
 * @param client the ws client
 * @param resource object with both the url and size of resource
 */
PeerKitServer.prototype._reportResource = function(client, resource) {
  var self = this;
  if (!client.resources[resource.url]) {
    client.resources[resource.url] = true;
    if (!self._resources[resource.url]) {
      self._resources[resource.url] = {};
    }
    self._resources[resource.url][client.id] = true;
    self._resourceSizes[resource.url] = resource.size;
    console.log(self._resources);
  }
};

/**
 * Returns a list of connections that the server has for the given url
 */
PeerKitServer.prototype._requestResource = function(url) {
  var resources = this._resources[url];
  if (!resources) {
    return [];
  }
  var clients = Object.keys(resources);
  var conns = [];
  if (clients.length <= this._options.multiplicity) {
    // Add each client
    console.log(this._clients);
    for (var i = 0; i < clients.length; i++) {
      if (this._clients[clients[i]].conns.length > 0) {
        conns.push(this._clients[clients[i]].conns.pop());
      }
    }
  } else {
    var rand;
    var seen = {};
    while (conns.length < this._options.multiplicity) {
      rand = Math.floor(Math.random() * messages.length);
      if (!seen[rand]) {
        // Add a connect from that client
        conns.push(this._clients[clients[rand]].conns.pop());
        seen[rand] = true;
      }
    }
  }
  return conns;
};

PeerKitServer.prototype._forward = function(dest, data) {
  if (!this._clients[dest]) {
    return {error: 'Client gone'};
  }
  this._client[dest].socket.send(data);
  return {success: true};
};

PeerKitServer.prototype._generateId = function(key) {
  var clientId = util.randomId();
  while (!!this._clients[clientId]) {
    clientId = util.randomId();
  }
  return clientId;
};

module.exports = PeerKitServer;
<commit_msg>fixed some bugs<commit_after>var express = require('express');
var http = require('http');
var WebSocketServer = require('ws').Server;
var EventEmitter = require('events').EventEmitter;
var util = require('./util');
var url = require('url');

function PeerKitServer(options) {
  if (!(this instanceof PeerKitServer)) return new PeerKitServer(options);
  EventEmitter.call(this);

  this._app = express();
  this._httpServer = http.createServer(this._app);

  this._options = util.extend({
    port: 8080,
    debug: true,
    multiplicity: 5
  }, options);

  util.debug = this._options.debug;

  // Connected clients
  /**
   * A mapping of client id to client information object and ws information
   */
  this._clients = {};

  /**
   * A mapping of resource url to a mapping of clients and boolean if they have it or not
   */
  this._resources = {};

  /**
   * A mapping of a given resource url to its size
   */
  this._resourceSizes = {};

  this._initializeWSS();

  this._initializeHTTP();
  this._httpServer.listen(this._options.port);

};

util.inherits(PeerKitServer, EventEmitter);

PeerKitServer.prototype._initializeWSS = function(){

  var self = this;

  // Create WebSocket server as well.
  this._wss = new WebSocketServer({ path: '/ws', server: this._httpServer });

  this._wss.on('connection', function(socket) {
    //var query = url.parse(socket.upgradeReq.url, true).query;
    var client = {
      id: self._generateId(),
      socket: socket,
      resources: {},
      conns: []
    };
    self._clients[client.id] = client;
    socket.on('message', function(data){
      util.log(data);
      var message;
      try {
        message = JSON.parse(data);
        if (!message.type) {
          throw new Error('Invalid message format: missing type');
        }
      } catch(e) {
        if (self.debug) {
          throw e;
        }
        util.log('Invalid message', data);
        return;
      }
      switch (message.type) {
        case 'RESPONSE':
          // Response to offer with ICE and answer
          //
          // message.id   (String, id of destination)
          // message.data (String, JSON of ice and answer)
          //
          var resp = self._forward(message.id, message.data);
          /*if (resp.error) {
            socket.send(JSON.stringify(
              type: 'ERROR-CLIENT-DEAD',

            ));
          }*/
          break;
        case 'REPORT':
          // New resource available
          //
          // message.url (String, resource absolute url)
          //
          self._reportResource(client, message.resource);
          break;
        case 'REQUEST':
          // Asking for resource
          //
          // message.url (String, resource absolute url)
          //
          var conns = self._requestResource(message.url);
          socket.send(JSON.stringify(conns));
          break;
        case 'CONN':
          // Connection information to store
          //
          // message.conn (mixed data)
          //
          client.conns.push(message.conn);
          break;
        case 'SERVED':
          // Served a file, increment analytics
          break;
        default:
          util.log('Message unrecognized');
      }
    });
    socket.on('close', function() {
      util.log('Socket closed:', client.id);
      var resources = client.resources;
      var hashes = Object.keys(resources);
      var ii = hashes.length;
      for (var i = 0; i < ii; i++) {
        delete self._resources[hashes[i]][client.id];
      }
      delete self._clients[client.id];
    });
  });
};

PeerKitServer.prototype._initializeHTTP = function(){
  var self = this;

  /*this._app.post('/response', function(req, res){
    // Responding to an offer with ICE and answer
    //
    // params.id    (String, id of destination)
    // body.data    (String, JSON of ice and answer
    //
    var resp = self._forward(req.params.id, req.body.data);
    res.send(resp);
  });*/
  this._app.enable("jsonp callback");

  this._app.get('/requestResource', function(req, res) {
    var urls = req.query.urls;
    if (!Array.isArray(urls)) {
        res.send("need array", 401);
    }
    var data = {};
    var ii = urls.length;
    for (var i = 0; i < ii; i++) {
      data[urls[i]] = self._requestResource(urls[i]);
    }
    res.jsonp(data);
    // Get Javascript library along with necessary data
  });
};

/**
 * Tells the server that we have the resource at given url
 * @param client the ws client
 * @param resource object with both the url and size of resource
 */
PeerKitServer.prototype._reportResource = function(client, resource) {
  var self = this;
  if (!client.resources[resource.url]) {
    client.resources[resource.url] = true;
    if (!self._resources[resource.url]) {
      self._resources[resource.url] = {};
    }
    self._resources[resource.url][client.id] = true;
    self._resourceSizes[resource.url] = resource.size;
    console.log(self._resources);
  }
};

/**
 * Returns a list of connections that the server has for the given url
 */
PeerKitServer.prototype._requestResource = function(url) {
  var resources = this._resources[url];
  if (!resources) {
    return [];
  }
  var clients = Object.keys(resources);
  var conns = [];
  if (clients.length <= this._options.multiplicity) {
    // Add each client
    for (var i = 0; i < clients.length; i++) {
      if (this._clients[clients[i]].conns.length > 0) {
        conns.push(this._clients[clients[i]].conns.pop());
      }
    }
  } else {
    var rand;
    var seen = {};
    while (conns.length < this._options.multiplicity) {
      rand = Math.floor(Math.random() * messages.length);
      if (!seen[rand]) {
        // Add a connect from that client
        conns.push(this._clients[clients[rand]].conns.pop());
        seen[rand] = true;
      }
    }
  }
  return conns;
};

PeerKitServer.prototype._forward = function(dest, data) {
  if (!this._clients[dest]) {
    return {error: 'Client gone'};
  }
  this._client[dest].socket.send(data);
  return {success: true};
};

PeerKitServer.prototype._generateId = function(key) {
  var clientId = util.randomId();
  while (!!this._clients[clientId]) {
    clientId = util.randomId();
  }
  return clientId;
};

module.exports = PeerKitServer;
<|endoftext|>


<commit_before>// Copyright 2018-2021, University of Colorado Boulder

/**
 * These are the units that can be associated with Property instances.
 *
 * When adding units to this file, please add abbreviations, preferably SI abbreviations.
 * And keep the array alphabetized by value.
 * See https://github.com/phetsims/phet-io/issues/530
 *
 * @author Sam Reid (PhET Interactive Simulations)
 */

import axon from './axon.js';

const units = {
  values: [
    '%', // percent
    'A', // amperes
    'AMU', // atomic mass unit
    'atm', // atmospheres
    'cm', // centimeters
    'cm^2', // centimeters squared
    'C', // coulombs
    '\u00B0', // °, degrees (angle)
    '\u00B0C', // °C, degrees Celsius
    'F', // farad
    'g', // grams
    'Hz', // hertz
    'J', // Joules
    'K', // Kelvin
    'kg', // kilograms
    'kg/m^3', // kg/cubic meter
    'kg\u00b7m/s', // kg·m/s, kilogram-meters/second
    'kPa', // kilopascals
    'L',
    'L/s',
    'm', // meters
    'm/s', // meters/second
    'm/s/s', // meters/second/second
    'm/s^2', // meters/seconds squared
    'mA', // milliampere
    'mm', //millimeters
    'mol',
    'mol/L',
    'N', // Newtons
    'N/m', // Newtons/meter
    'nm', // nanometers
    'nm/ps', // nanometers/picosecond
    'N\u00b7s/m', // N·s/m, Newton-seconds/meter
    '\u2126', // Ω, ohms - don't use the one in MathSymbols to prevent a dependency on scenery-phet
    '\u2126\u00b7cm', // Ω·cm, ohm-centimeters
    'particles/ps', // particles/picosecond
    'pm', // picometers
    'pm/ps', // picometers/picosecond
    'pm/s', // picometers/second
    'pm/s^2', // picometers/second-squared
    'pm^3', // picometers cubed
    'ps', // picoseconds
    'radians', // radians - note this has the same abbreviation as the radiation term "rad" so we use the full term
    'radians/s', // radians/second
    's', // seconds
    'V', // volts
    'view-coordinates/s'
  ],

  isValidUnits: function( unit ) {
    return _.includes( units.values, unit );
  }
};

axon.register( 'units', units );

export default units;<commit_msg>Adding units for density<commit_after>// Copyright 2018-2021, University of Colorado Boulder

/**
 * These are the units that can be associated with Property instances.
 *
 * When adding units to this file, please add abbreviations, preferably SI abbreviations.
 * And keep the array alphabetized by value.
 * See https://github.com/phetsims/phet-io/issues/530
 *
 * @author Sam Reid (PhET Interactive Simulations)
 */

import axon from './axon.js';

const units = {
  values: [
    '%', // percent
    'A', // amperes
    'AMU', // atomic mass unit
    'atm', // atmospheres
    'cm', // centimeters
    'cm^2', // centimeters squared
    'C', // coulombs
    '\u00B0', // °, degrees (angle)
    '\u00B0C', // °C, degrees Celsius
    'F', // farad
    'g', // grams
    'Hz', // hertz
    'J', // Joules
    'K', // Kelvin
    'kg', // kilograms
    'kg/m^3', // kg/cubic meter
    'kg\u00b7m/s', // kg·m/s, kilogram-meters/second
    'kPa', // kilopascals
    'L',
    'L/s',
    'm', // meters
    'm^3', // cubic meter
    'm/s', // meters/second
    'm/s/s', // meters/second/second
    'm/s^2', // meters/seconds squared
    'mA', // milliampere
    'mm', //millimeters
    'mol',
    'mol/L',
    'N', // Newtons
    'N/m', // Newtons/meter
    'nm', // nanometers
    'nm/ps', // nanometers/picosecond
    'N\u00b7s/m', // N·s/m, Newton-seconds/meter
    '\u2126', // Ω, ohms - don't use the one in MathSymbols to prevent a dependency on scenery-phet
    '\u2126\u00b7cm', // Ω·cm, ohm-centimeters
    'Pa\u00b7s', // Pascal-seconds
    'particles/ps', // particles/picosecond
    'pm', // picometers
    'pm/ps', // picometers/picosecond
    'pm/s', // picometers/second
    'pm/s^2', // picometers/second-squared
    'pm^3', // picometers cubed
    'ps', // picoseconds
    'radians', // radians - note this has the same abbreviation as the radiation term "rad" so we use the full term
    'radians/s', // radians/second
    's', // seconds
    'V', // volts
    'view-coordinates/s'
  ],

  isValidUnits: function( unit ) {
    return _.includes( units.values, unit );
  }
};

axon.register( 'units', units );

export default units;<|endoftext|>


<commit_before>var http = require('http')
var fs = require('fs')
var after = require('after-all')
var pump = require('pump')
var path = require('path')
var mkdirp = require('mkdirp')

module.exports = function(root) {
  if (!root) root = '/'
  root = fs.realpathSync(root)

  var trim = function(u) {
    u = u.replace(root, '')
    if (u[0] !== '/') u = '/'+u
    return u
  }

  var server = http.createServer(function(req, res) {
    var onerror = function(err) {
      if (!err) return res.end()
      res.statusCode = err.code === 'ENOENT' ? 404 : 500
      res.end(err.message)
    }

    res.setHeader('Access-Control-Allow-Origin', '*')

    var name = path.join('/', req.url.split('?')[0])
    var u = path.join(root, name)

    if (req.method === 'POST') return mkdirp(u, onerror)
    if (req.method === 'PUT') return pump(req, fs.createWriteStream(u))

    var onfile = function(st) {
      server.emit('file', u, st)
      res.setHeader('Content-Length', st.size)
      pump(fs.createReadStream(u), res)
    }

    var ondirectory = function(st) {
      server.emit('directory', u, st)
      fs.readdir(u, function(err, files) {
        if (err) return onerror(err)

        var next = after(function() {
          res.end(JSON.stringify(files))
        })

        files.forEach(function(file, i) {
          var n = next()

          fs.stat(path.join(u, file), function(err, st) {
            if (err) return n(err)

            files[i] = {
              path: trim(path.join(u, file)),
              type: st.isDirectory() ? 'directory' : 'file',
              size: st.size
            }

            n()
          })
        })
      })
    }

    fs.stat(u, function(err, st) {
      if (err) return onerror(err)
      if (st.isDirectory()) return ondirectory(st)
      onfile(st)
    })
  })

  server.root = root

  return server
}<commit_msg>fix PUT hanging<commit_after>var http = require('http')
var fs = require('fs')
var after = require('after-all')
var pump = require('pump')
var path = require('path')
var mkdirp = require('mkdirp')

module.exports = function(root) {
  if (!root) root = '/'
  root = fs.realpathSync(root)

  var trim = function(u) {
    u = u.replace(root, '')
    if (u[0] !== '/') u = '/'+u
    return u
  }

  var server = http.createServer(function(req, res) {
    var onerror = function(err) {
      if (!err) return res.end()
      res.statusCode = err.code === 'ENOENT' ? 404 : 500
      res.end(err.message)
    }

    res.setHeader('Access-Control-Allow-Origin', '*')

    var name = path.join('/', req.url.split('?')[0])
    var u = path.join(root, name)

    if (req.method === 'POST') return mkdirp(u, onerror)
    if (req.method === 'PUT') return pump(req, fs.createWriteStream(u), onerror)

    var onfile = function(st) {
      server.emit('file', u, st)
      res.setHeader('Content-Length', st.size)
      pump(fs.createReadStream(u), res)
    }

    var ondirectory = function(st) {
      server.emit('directory', u, st)
      fs.readdir(u, function(err, files) {
        if (err) return onerror(err)

        var next = after(function() {
          res.end(JSON.stringify(files))
        })

        files.forEach(function(file, i) {
          var n = next()

          fs.stat(path.join(u, file), function(err, st) {
            if (err) return n(err)

            files[i] = {
              path: trim(path.join(u, file)),
              type: st.isDirectory() ? 'directory' : 'file',
              size: st.size
            }

            n()
          })
        })
      })
    }

    fs.stat(u, function(err, st) {
      if (err) return onerror(err)
      if (st.isDirectory()) return ondirectory(st)
      onfile(st)
    })
  })

  server.root = root

  return server
}<|endoftext|>


<commit_before>/*
 * weh - WebExtensions Help
 *
 * @summary workflow and base code for developing WebExtensions browser add-ons
 * @author Michel Gutierrez
 * @link https://github.com/mi-g/weh
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

const gulp = require("gulp");
const gutil = require("gulp-util");
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const del = require("del");
const sass = require('gulp-sass');
const concat = require("gulp-concat");
const replace = require('gulp-replace');
const gulpif = require('gulp-if');
const debug = require('gulp-debug');
const cleanCSS = require('gulp-clean-css');
const header = require("gulp-header");
const manifest = require('./gulp-chrome-manifest');
const userefWeh = require("./gulp-useref-weh");
const runSequence = require('run-sequence');
const es2015 = require('babel-preset-es2015');
const react = require('babel-preset-react');

const fs = require("fs");
const argv = require('yargs').argv;
const path = require("path");
const glob = require("glob");

var dev = !!argv.dev;
var prjDir = argv.prjdir || 'tmp/trash-prj';
var buildDir = path.join(prjDir,argv.builddir || "build");
var template = argv.template || "skeleton";

var wehBackgroundModules = ["core","inspect","prefs","ui","ajax"];

var jsBanner = null, jsBannerData;

if(argv.jsheader || (!dev && argv.jsheader!==false)) {
    try {
        jsBannerData = {
            manifest: require(path.join(prjDir,"src/manifest.json"))
        }
        try {
            jsBanner = fs.readFileSync(path.join(prjDir,"etc/jsbanner.txt"),"utf8");
        } catch(e) {
            jsBanner = fs.readFileSync("etc/jsbanner.txt","utf8");
        }
    } catch(e) {}
}

gulp.task("prj-content",function(cb) {

    var staticSrc = [path.join(prjDir,"src/content/**/*.{js,css}")];
    staticSrc.push("src/content/**/*.{js,css}");
    staticSrc.push("node_modules/react/dist/**/*.{js,css}");
    staticSrc.push("node_modules/react-dom/dist/**/*.{js,css}");
    staticSrc.push("node_modules/bootstrap/dist/css/*.css");
    staticSrc.push("node_modules/bootstrap/dist/js/*.js");
    staticSrc.push("node_modules/jquery/dist/**/*.js");

    function AddScripts(org,match) {
        var scripts = [];
        function AddReactScripts() {
            if(argv.react!==false) {
                scripts.push("<script src=\"react.js\"></script>");
                scripts.push("<script src=\"react-dom.js\"></script>");
            }
        }
        function AddWehScripts() {
            if(argv.weh!==false) {
                scripts.push("<script src=\"weh-ct.js\"></script>");
                if(argv["weh-prefs"]!==false)
                    scripts.push("<script src=\"weh-ct-react.jsx\"></script>");
            }
        }
        match.split(",").map(function(term) {
            return term.trim();
        }).forEach(function(term) {
            switch(term) {
                case "jquery":
                    scripts.push("<script src=\"jquery.js\"></script>");
                    break;
                case "bootstrap":
                    scripts.push("<script src=\"bootstrap.js\"></script>");
                    break;
                case "weh-all":
                    AddReactScripts();
                    AddWehScripts();
                    break;
                case "weh":
                    AddWehScripts();
                    break;
            }
        });
        return scripts.join("\n");
    }

    return gulp.src(path.join(prjDir,"src/content/**/*.html"))
        .pipe(replace(/<\!--\s*weh:js\s*(.*?)\s*-->/g,AddScripts))
        .pipe(userefWeh([{
            src: staticSrc,
        },{
            src: [path.join(prjDir,"src/content/**/*.jsx"),"src/content/**/*.jsx"],
            stream: function(fileName) {
                return gulp.src(fileName)
                    .pipe(babel({
                        presets: [es2015,react]
                    }));
            }
        },{
            src: [path.join(prjDir,"src/content/**/*.scss")],
            stream: function(fileName) {
                return gulp.src(fileName)
                    .pipe(sass());
            }
        }],{
            noconcat: dev,
            changeExt: {
                "jsx": "js",
                "scss": "css"
            }
        }))
        .on("error",function(err) {
            console.log('[Compilation Error]');
            console.log(err.fileName + ( err.loc ? `( ${err.loc.line}, ${err.loc.column} ): ` : ': '));
            console.log('error Babel: ' + err.message + '\n');
            console.log(err.codeFrame);
            this.emit("end");
        })
        .pipe(gulpif(!dev,gulpif('*.js', uglify())))
        .pipe(gulpif(!!jsBanner,gulpif('*.js',header(jsBanner,jsBannerData))))
        .pipe(gulpif(!dev,gulpif('*.css',cleanCSS({compatibility: 'ie8'}))))
        .pipe(gulp.dest(path.join(buildDir,"content")));
});

gulp.task("prj-content-assets",function() {
    return gulp.src([path.join(prjDir,"src/content/assets/**/*")])
        .pipe(gulp.dest(path.join(buildDir,"content")));
});

gulp.task("prj-assets",function() {
    return gulp.src(path.join(prjDir,"src/assets/**/*"))
        .pipe(gulp.dest(buildDir))
});

gulp.task("prj-manifest",function() {
    return gulp.src(path.join(prjDir,"src/manifest.json"))
        .pipe(manifest({
            background: {
                target: 'background/background.js',
                initialScripts: wehBackgroundModules.map(function(module) {
                    return "background/weh-"+module+".js";
                }),
                searchPath: [path.resolve("src")],
                noconcat: dev
            }
        }))
        .pipe(gulpif(!dev,gulpif('*.js', uglify())))
        .pipe(gulpif(!!jsBanner,gulpif('*.js',header(jsBanner,jsBannerData))))
        .pipe(gulp.dest(buildDir));
});

gulp.task("prj-locales",function() {
    return gulp.src(path.join(prjDir,"src/locales/**/*"))
        .pipe(gulp.dest(path.join(buildDir,"_locales")));
});

gulp.task("clean",function() {
    return del([buildDir+"/*"],{force:true});
});

gulp.task("build-weh",[
    "weh-background-scripts",
    "weh-content-scripts",
]);

gulp.task("build",[
    "prj-manifest",
    "prj-content",
    "prj-content-assets",
    "prj-assets",
    "prj-locales"
]);

gulp.task("watch-weh",function() {
    gulp.watch(["src/background/*.js","src/content/*.js","src/content/*.jsx"], ["build"]);
});

gulp.task("watch-prj",function() {
    gulp.watch(path.join(prjDir,"src/**/*"), ["build"]);
});

gulp.task("default", function(callback) {
    if(argv.help)
        return runSequence("help");
    var tasks = ["clean","build"];
    if(argv["watch-weh"])
        tasks.push("watch-weh");
    if(argv["watch"]!==false)
        tasks.push("watch-prj");
    tasks.push(callback);
    runSequence.apply(null,tasks);
});

gulp.task("copy-template",function(callback) {
    try {
        fs.accessSync(prjDir,fs.F_OK);
        if(!force)
            return callback(new Error(prjDir+" already exists"));
    } catch(e) {}
    gulp.src("templates/"+template+"/**/*")
        .pipe(gulp.dest(prjDir))
        .on("end",callback);
});

gulp.task("init", function(callback) {
    runSequence("copy-template","build",callback);
});

gulp.task("help", function() {
    var help = [
        "usage: gulp [<commands>] [<options>]",
        "",
        "commands:",
        "  build: generate project build",
        "  clean: remove project build recursively",
        "  watch-prj: watch project and build dynamically on changes",
        "  init: generate project from template",
        "  watch-weh: watch weh source and update project build dynamically",
        "default commands: clean + build + watch-prj",
        "",
        "options:",
        "  --prjdir <dir>: project directory (required for most commands)",
        "  --dev: addon generated for development",
        "  --template <template>: template to be used when creating a new project",
        "  --no-watch! do generate builds dynamically",
        "  --watch-weh! generate builds dynamically when weh source is modified",
        "  --no-react: do not include ReactJS vendor library",
        "  --no-bootstrap: do not include Bootstrap vendor library",
        "  --force: force overwrite output directory",
        "  --jsheader/--no-jsheader: force JS headers on dev builds/disable JS headers on non-dev builds"
    ];
    console.log(help.join("\n"));
    process.exit(0);
});
<commit_msg>remove inspect module<commit_after>/*
 * weh - WebExtensions Help
 *
 * @summary workflow and base code for developing WebExtensions browser add-ons
 * @author Michel Gutierrez
 * @link https://github.com/mi-g/weh
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

const gulp = require("gulp");
const gutil = require("gulp-util");
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const del = require("del");
const sass = require('gulp-sass');
const concat = require("gulp-concat");
const replace = require('gulp-replace');
const gulpif = require('gulp-if');
const debug = require('gulp-debug');
const cleanCSS = require('gulp-clean-css');
const header = require("gulp-header");
const manifest = require('./gulp-chrome-manifest');
const userefWeh = require("./gulp-useref-weh");
const runSequence = require('run-sequence');
const es2015 = require('babel-preset-es2015');
const react = require('babel-preset-react');

const fs = require("fs");
const argv = require('yargs').argv;
const path = require("path");
const glob = require("glob");

var dev = !!argv.dev;
var prjDir = argv.prjdir || 'tmp/trash-prj';
var buildDir = path.join(prjDir,argv.builddir || "build");
var template = argv.template || "skeleton";

var wehBackgroundModules = ["core","prefs","ui","ajax"];

var jsBanner = null, jsBannerData;

if(argv.jsheader || (!dev && argv.jsheader!==false)) {
    try {
        jsBannerData = {
            manifest: require(path.join(prjDir,"src/manifest.json"))
        }
        try {
            jsBanner = fs.readFileSync(path.join(prjDir,"etc/jsbanner.txt"),"utf8");
        } catch(e) {
            jsBanner = fs.readFileSync("etc/jsbanner.txt","utf8");
        }
    } catch(e) {}
}

gulp.task("prj-content",function(cb) {

    var staticSrc = [path.join(prjDir,"src/content/**/*.{js,css}")];
    staticSrc.push("src/content/**/*.{js,css}");
    staticSrc.push("node_modules/react/dist/**/*.{js,css}");
    staticSrc.push("node_modules/react-dom/dist/**/*.{js,css}");
    staticSrc.push("node_modules/bootstrap/dist/css/*.css");
    staticSrc.push("node_modules/bootstrap/dist/js/*.js");
    staticSrc.push("node_modules/jquery/dist/**/*.js");

    function AddScripts(org,match) {
        var scripts = [];
        function AddReactScripts() {
            if(argv.react!==false) {
                scripts.push("<script src=\"react.js\"></script>");
                scripts.push("<script src=\"react-dom.js\"></script>");
            }
        }
        function AddWehScripts() {
            if(argv.weh!==false) {
                scripts.push("<script src=\"weh-ct.js\"></script>");
                if(argv["weh-prefs"]!==false)
                    scripts.push("<script src=\"weh-ct-react.jsx\"></script>");
            }
        }
        match.split(",").map(function(term) {
            return term.trim();
        }).forEach(function(term) {
            switch(term) {
                case "jquery":
                    scripts.push("<script src=\"jquery.js\"></script>");
                    break;
                case "bootstrap":
                    scripts.push("<script src=\"bootstrap.js\"></script>");
                    break;
                case "weh-all":
                    AddReactScripts();
                    AddWehScripts();
                    break;
                case "weh":
                    AddWehScripts();
                    break;
            }
        });
        return scripts.join("\n");
    }

    return gulp.src(path.join(prjDir,"src/content/**/*.html"))
        .pipe(replace(/<\!--\s*weh:js\s*(.*?)\s*-->/g,AddScripts))
        .pipe(userefWeh([{
            src: staticSrc,
        },{
            src: [path.join(prjDir,"src/content/**/*.jsx"),"src/content/**/*.jsx"],
            stream: function(fileName) {
                return gulp.src(fileName)
                    .pipe(babel({
                        presets: [es2015,react]
                    }));
            }
        },{
            src: [path.join(prjDir,"src/content/**/*.scss")],
            stream: function(fileName) {
                return gulp.src(fileName)
                    .pipe(sass());
            }
        }],{
            noconcat: dev,
            changeExt: {
                "jsx": "js",
                "scss": "css"
            }
        }))
        .on("error",function(err) {
            console.log('[Compilation Error]');
            console.log(err.fileName + ( err.loc ? `( ${err.loc.line}, ${err.loc.column} ): ` : ': '));
            console.log('error Babel: ' + err.message + '\n');
            console.log(err.codeFrame);
            this.emit("end");
        })
        .pipe(gulpif(!dev,gulpif('*.js', uglify())))
        .pipe(gulpif(!!jsBanner,gulpif('*.js',header(jsBanner,jsBannerData))))
        .pipe(gulpif(!dev,gulpif('*.css',cleanCSS({compatibility: 'ie8'}))))
        .pipe(gulp.dest(path.join(buildDir,"content")));
});

gulp.task("prj-content-assets",function() {
    return gulp.src([path.join(prjDir,"src/content/assets/**/*")])
        .pipe(gulp.dest(path.join(buildDir,"content")));
});

gulp.task("prj-assets",function() {
    return gulp.src(path.join(prjDir,"src/assets/**/*"))
        .pipe(gulp.dest(buildDir))
});

gulp.task("prj-manifest",function() {
    return gulp.src(path.join(prjDir,"src/manifest.json"))
        .pipe(manifest({
            background: {
                target: 'background/background.js',
                initialScripts: wehBackgroundModules.map(function(module) {
                    return "background/weh-"+module+".js";
                }),
                searchPath: [path.resolve("src")],
                noconcat: dev
            }
        }))
        .pipe(gulpif(!dev,gulpif('*.js', uglify())))
        .pipe(gulpif(!!jsBanner,gulpif('*.js',header(jsBanner,jsBannerData))))
        .pipe(gulp.dest(buildDir));
});

gulp.task("prj-locales",function() {
    return gulp.src(path.join(prjDir,"src/locales/**/*"))
        .pipe(gulp.dest(path.join(buildDir,"_locales")));
});

gulp.task("clean",function() {
    return del([buildDir+"/*"],{force:true});
});

gulp.task("build-weh",[
    "weh-background-scripts",
    "weh-content-scripts",
]);

gulp.task("build",[
    "prj-manifest",
    "prj-content",
    "prj-content-assets",
    "prj-assets",
    "prj-locales"
]);

gulp.task("watch-weh",function() {
    gulp.watch(["src/background/*.js","src/content/*.js","src/content/*.jsx"], ["build"]);
});

gulp.task("watch-prj",function() {
    gulp.watch(path.join(prjDir,"src/**/*"), ["build"]);
});

gulp.task("default", function(callback) {
    if(argv.help)
        return runSequence("help");
    var tasks = ["clean","build"];
    if(argv["watch-weh"])
        tasks.push("watch-weh");
    if(argv["watch"]!==false)
        tasks.push("watch-prj");
    tasks.push(callback);
    runSequence.apply(null,tasks);
});

gulp.task("copy-template",function(callback) {
    try {
        fs.accessSync(prjDir,fs.F_OK);
        if(!force)
            return callback(new Error(prjDir+" already exists"));
    } catch(e) {}
    gulp.src("templates/"+template+"/**/*")
        .pipe(gulp.dest(prjDir))
        .on("end",callback);
});

gulp.task("init", function(callback) {
    runSequence("copy-template","build",callback);
});

gulp.task("help", function() {
    var help = [
        "usage: gulp [<commands>] [<options>]",
        "",
        "commands:",
        "  build: generate project build",
        "  clean: remove project build recursively",
        "  watch-prj: watch project and build dynamically on changes",
        "  init: generate project from template",
        "  watch-weh: watch weh source and update project build dynamically",
        "default commands: clean + build + watch-prj",
        "",
        "options:",
        "  --prjdir <dir>: project directory (required for most commands)",
        "  --dev: addon generated for development",
        "  --template <template>: template to be used when creating a new project",
        "  --no-watch! do generate builds dynamically",
        "  --watch-weh! generate builds dynamically when weh source is modified",
        "  --no-react: do not include ReactJS vendor library",
        "  --no-bootstrap: do not include Bootstrap vendor library",
        "  --force: force overwrite output directory",
        "  --jsheader/--no-jsheader: force JS headers on dev builds/disable JS headers on non-dev builds"
    ];
    console.log(help.join("\n"));
    process.exit(0);
});
<|endoftext|>


<commit_before>/*jslint node: true, vars: true, nomen: true */
'use strict';

var express = require('express');
var Http = require('http');
var debug = require('debug')('xpl-db:server');
var async = require('async');
var noCache = require('connect-nocache')();
var bodyParser = require('body-parser');
var serve_static = require('serve-static');
var compress = require('compression');

class Server {
  constructor(configuration, store) {
    this.configuration = configuration || {};
    this.store = store;

    this.app = this.configuration.express || express();

    if (this.configuration.compression !== false) {
      try {
        var compression = require('compression');
        this.app.use(compression())
      } catch (x) {
        console.error("No compression module !");
      }
    }
  }

  listen(callback) {
    var app = this.app;

    app.enable('etag');
    app.use(compress());
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
      extended: true
    }));
//    app.use(noCache);

    app.get(/^\/last\/(.*)$/, this.getLast.bind(this));
    app.get(/^\/history\/(.*)$/, this.getHistory.bind(this));
    app.get(/^\/minMaxAvgSum\/(.*)$/, this.getMinMaxAvgSum.bind(this));

    app.post('/last', this.getLastSet.bind(this));
    app.post('/history', this.getHistorySet.bind(this));
    app.post('/minMaxAvgSum', this.getMinMaxAvgSumSet.bind(this));

    if (this.configuration.configPath) {
      app.use("/config", serve_static(this.configuration.configPath, {
        'index' : false
      }));
    }

    app.use(function(req, res, next) {
      res.status(404).send('Sorry cant find that!');
    });

    var server = app.listen(this.configuration.httpPort || 8480, (error) => {
      if (error) {
        console.error("Server can not listen", error);
        return;
      }
      debug("listen", "Server is listening ", server.address());

      callback(error);
    });
  };

  _set(name, request, response, func) {
    var keys = Object.keys(request.body);
    debug("_set", name, "set keys=", keys);

    var options = formatOptions(request);

    var results = {};
    keys.forEach(key => results[key]=null);

    async.eachLimit(keys, 8, (key, callback) => {
      var reg=/(.*)\/([^/]+)$/.exec(key);
      if (reg) {
        key=reg[1]+"@"+reg[2]; // Transform @ to /
      }

      func.call(this.store, key, options, (error, value) => {
        debug("_set", name, " set key=", key, "value=", value, "error=", error);
        if (error) {
          return callback(error);
        }

        var reg2=/(.*)\@([^@]+)$/.exec(key);
        if (reg2) {
          key=reg2[1]+"/"+reg[2]; // Transform / to @
        }

        results[key] = value;

        callback();
      });

    }, (error) => {
      if (error) {
        // send 500
        if (error.code === 'NOT_FOUND') {
          response.status(404).body("Key '" + error.key + "' not found");
          return;
        }

        response.status(500).body(String(error));
        return;
      }

      response.json(results);
    });
  };

  _get(name, request, response, func) {
    var key = request.params[0];
    debug("_get", "type=", name, "key=", key);

    var reg=/(.*)\/([^/]+)$/.exec(key);
    if (reg) {
      key=reg[1]+"@"+reg[2]; // Transform @ to /
    }

    var options = formatOptions(request);

    func.call(this.store, key, options, (error, values) => {
      debug("_get", "type=", name, "key=", key, "values=", values);
      if (error) {
        // send 500
        if (error.code === 'NOT_FOUND') {
          response.status(404).send('Key not found');
          return;
        }

        response.status(500).send(String(error));
        return;
      }

      response.json(values);
    });
  }

  getLast(request, response) {
    this._get("getLast", request, response, this.store.getLast);
  }

  getHistory(request, response) {
    this._get("getHistory", request, response, this.store.getHistory);
  }

  getMinMaxAvgSum(request, response) {
    this._get("getMinMaxAvgSum", request, response, this.store.getMinMaxAvgSum);
  }

  getLastSet(request, response) {
    this._set("getLastSet", request, response, this.store.getLast);
  }

  getHistorySet(request, response) {
    this._set("getHistorySet", request, response, this.store.getHistory);
  }

  getMinMaxAvgSumSet(request, response) {
    this._set("getMinMaxAvgSumSet", request, response, this.store.getMinMaxAvgSum);
  }
}

function formatOptions(request) {
  var ret = {};

  var query = request.query;
  if (query.limit) {
    ret.limit = parseInt(query.limit, 10);
  }

  if (query.minDate) {
    ret.minDate = new Date(query.minDate);
  }

  if (query.maxDate) {
    ret.maxDate = new Date(query.maxDate);
  }

  if (query.averageMs) {
    ret.averageMs = parseInt(query.averageMs, 10);
  }

  if (query.step) {
    ret.stepMs = parseInt(query.step, 10) * 1000;
  }

  return ret;
}

module.exports = Server;
<commit_msg>Configure max-age<commit_after>/*jslint node: true, vars: true, nomen: true */
'use strict';

var express = require('express');
var Http = require('http');
var debug = require('debug')('xpl-db:server');
var async = require('async');
var noCache = require('connect-nocache')();
var bodyParser = require('body-parser');
var serve_static = require('serve-static');
var compress = require('compression');

class Server {
  constructor(configuration, store) {
    this.configuration = configuration || {};
    this.store = store;

    this.app = this.configuration.express || express();

    if (this.configuration.compression !== false) {
      try {
        var compression = require('compression');
        this.app.use(compression())
      } catch (x) {
        console.error("No compression module !");
      }
    }
  }

  listen(callback) {
    var app = this.app;

    app.enable('etag');
    app.use(compress());
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
      extended: true
    }));
//    app.use(noCache);

    app.get(/^\/last\/(.*)$/, this.getLast.bind(this));
    app.get(/^\/history\/(.*)$/, this.getHistory.bind(this));
    app.get(/^\/minMaxAvgSum\/(.*)$/, this.getMinMaxAvgSum.bind(this));

    app.post('/last', this.getLastSet.bind(this));
    app.post('/history', this.getHistorySet.bind(this));
    app.post('/minMaxAvgSum', this.getMinMaxAvgSumSet.bind(this));

    if (this.configuration.configPath) {

      var oneYear = 60*60*24*365;

      app.use(express.static(__dirname + '/public', {  }));

      app.use("/config", serve_static(this.configuration.configPath, {
        index : false,
        maxAge: oneYear
      }));
    }

    app.use(function(req, res, next) {
      res.status(404).send('Sorry cant find that!');
    });

    var server = app.listen(this.configuration.httpPort || 8480, (error) => {
      if (error) {
        console.error("Server can not listen", error);
        return;
      }
      debug("listen", "Server is listening ", server.address());

      callback(error);
    });
  };

  _set(name, request, response, func) {
    var keys = Object.keys(request.body);
    debug("_set", name, "set keys=", keys);

    var options = formatOptions(request);

    var results = {};
    keys.forEach(key => results[key]=null);

    async.eachLimit(keys, 8, (key, callback) => {
      var reg=/(.*)\/([^/]+)$/.exec(key);
      if (reg) {
        key=reg[1]+"@"+reg[2]; // Transform @ to /
      }

      func.call(this.store, key, options, (error, value) => {
        debug("_set", name, " set key=", key, "value=", value, "error=", error);
        if (error) {
          return callback(error);
        }

        var reg2=/(.*)\@([^@]+)$/.exec(key);
        if (reg2) {
          key=reg2[1]+"/"+reg[2]; // Transform / to @
        }

        results[key] = value;

        callback();
      });

    }, (error) => {
      if (error) {
        // send 500
        if (error.code === 'NOT_FOUND') {
          response.status(404).body("Key '" + error.key + "' not found");
          return;
        }

        response.status(500).body(String(error));
        return;
      }

      response.json(results);
    });
  };

  _get(name, request, response, func) {
    var key = request.params[0];
    debug("_get", "type=", name, "key=", key);

    var reg=/(.*)\/([^/]+)$/.exec(key);
    if (reg) {
      key=reg[1]+"@"+reg[2]; // Transform @ to /
    }

    var options = formatOptions(request);

    func.call(this.store, key, options, (error, values) => {
      debug("_get", "type=", name, "key=", key, "values=", values);
      if (error) {
        // send 500
        if (error.code === 'NOT_FOUND') {
          response.status(404).send('Key not found');
          return;
        }

        response.status(500).send(String(error));
        return;
      }

      response.json(values);
    });
  }

  getLast(request, response) {
    this._get("getLast", request, response, this.store.getLast);
  }

  getHistory(request, response) {
    this._get("getHistory", request, response, this.store.getHistory);
  }

  getMinMaxAvgSum(request, response) {
    this._get("getMinMaxAvgSum", request, response, this.store.getMinMaxAvgSum);
  }

  getLastSet(request, response) {
    this._set("getLastSet", request, response, this.store.getLast);
  }

  getHistorySet(request, response) {
    this._set("getHistorySet", request, response, this.store.getHistory);
  }

  getMinMaxAvgSumSet(request, response) {
    this._set("getMinMaxAvgSumSet", request, response, this.store.getMinMaxAvgSum);
  }
}

function formatOptions(request) {
  var ret = {};

  var query = request.query;
  if (query.limit) {
    ret.limit = parseInt(query.limit, 10);
  }

  if (query.minDate) {
    ret.minDate = new Date(query.minDate);
  }

  if (query.maxDate) {
    ret.maxDate = new Date(query.maxDate);
  }

  if (query.averageMs) {
    ret.averageMs = parseInt(query.averageMs, 10);
  }

  if (query.step) {
    ret.stepMs = parseInt(query.step, 10) * 1000;
  }

  return ret;
}

module.exports = Server;
<|endoftext|>


<commit_before>const gulp = require("gulp");
const { parallel } = require("gulp");
const sass = require('gulp-sass');
const cleancss = require('gulp-clean-css');
const csscomb = require('gulp-csscomb');
const rename = require('gulp-rename');
const pug = require('gulp-pug');
const autoprefixer = require('gulp-autoprefixer');

function build() {
  return gulp
    .src('./src/*.scss')
    .pipe(sass({outputStyle: 'compact', precision: 10})
      .on('error', sass.logError)
    )
    .pipe(autoprefixer())
    .pipe(csscomb())
    .pipe(gulp.dest('./dist'))
    .pipe(cleancss())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./dist'));
}

function docs_css() {
  return gulp
    .src(['./src/*.scss', './docs/src/scss/*.scss'])
    .pipe(sass({outputStyle: 'compact', precision: 10})
      .on('error', sass.logError)
    )
    .pipe(autoprefixer())
    .pipe(csscomb())
    .pipe(gulp.dest('./docs/dist'))
    .pipe(cleancss())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./docs/dist'));
}

function docs_pug() {
  return gulp
    .src('docs/src/**/!(_)*.pug')
    .pipe(pug({
      pretty: true
    }))
    .pipe(gulp.dest('./docs/'));
}

function watch() {
  gulp.watch('./**/*.scss', parallel(build, docs_css));
  gulp.watch('./**/*.pug', docs_pug);
}

exports.watch = watch;
exports.build = build;
exports.docs_css = docs_css;
exports.docs_pug = docs_pug;
exports.default = build;
<commit_msg>Fix gulp docs<commit_after>const gulp = require("gulp");
const { parallel } = require("gulp");
const sass = require('gulp-sass');
const cleancss = require('gulp-clean-css');
const csscomb = require('gulp-csscomb');
const rename = require('gulp-rename');
const pug = require('gulp-pug');
const autoprefixer = require('gulp-autoprefixer');

function build() {
  return gulp
    .src('./src/*.scss')
    .pipe(sass({outputStyle: 'compact', precision: 10})
      .on('error', sass.logError)
    )
    .pipe(autoprefixer())
    .pipe(csscomb())
    .pipe(gulp.dest('./dist'))
    .pipe(cleancss())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./dist'));
}

function docs_css() {
  return gulp
    .src(['./src/*.scss', './docs/src/scss/*.scss'])
    .pipe(sass({outputStyle: 'compact', precision: 10})
      .on('error', sass.logError)
    )
    .pipe(autoprefixer())
    .pipe(csscomb())
    .pipe(gulp.dest('./docs/dist'))
    .pipe(cleancss())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./docs/dist'));
}

function docs_pug() {
  return gulp
    .src('docs/src/**/!(_)*.pug')
    .pipe(pug({
      pretty: true
    }))
    .pipe(gulp.dest('./docs/'));
}

function watch() {
  gulp.watch('./**/*.scss', parallel(build, docs_css));
  gulp.watch('./**/*.pug', docs_pug);
}

exports.watch = watch;
exports.build = build;
exports.docs = parallel(docs_pug, docs_css);
exports.default = build;
<|endoftext|>


<commit_before>'use strict';
var packageJson = require('../package.json');
var http = require('http');
var express = require('express');
var accepts = require('accepts');
var expressSession = require('express-session');
var morgan = require('morgan');
var nodePath = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var domv = require('domv');
var P = require('bluebird');
var secureRandomBytes = P.promisify(require('crypto').randomBytes);

var mongoInit = require('./mongo-init');
var config = require('./server-config');
var primusServer = require('./primus-server');
var passportInit = require('./passport-init');

var BaseDocument = require('./view/BaseDocument');
var ClientContext = require('./ClientContext');
var FrontController = require('./controller/FrontController');
var router = require('./controller/router');
var resources = require('./resource/server');
var MongoSessionStore = require('./MongoSessionStore');

function staticFile(path, type)
{
        path = nodePath.resolve(__dirname + '/../' + path);
        var gzPath = path + '.gz';

        return function(req, res)
        {
                var accept = accepts(req);
                var method = accept.encodings(['gzip']);

                function plain()
                {
                        res.sendFile(path, {
                                lastModified: true,
                                maxAge      : '7d',
                                headers     : {
                                        'Content-Type': type
                                }
                        });
                }

                if (method === 'gzip')
                {
                        return res.sendFile(
                                gzPath,
                                {
                                        lastModified: true,
                                        maxAge      : '7d',
                                        headers     : {
                                                'Content-Encoding': 'gzip',
                                                'Content-Type': type
                                        }
                                },
                                function(err)
                                {
                                        if (err)
                                        {
                                                plain();
                                        }
                                }
                        );
                }

                plain();
        };
}

function errorRequest(err, req, res, next)
{
        console.error(err.stack, '\n');
        res.header('Content-Type', 'text/plain; charset=UTF-8');
        res.status(500);
        res.send(err.stack);
        next();
}

function getOrSetCsrfToken(req, res)
{
        if (req.cookies.csrf)
        {
                return P.resolve(req.cookies.csrf);
        }

        // secureRandomBytes() might fail if there is not entropy
        return secureRandomBytes(128).then(function(buf)
        {
                var csrf = buf.toString('base64');
                csrf = csrf.replace(/\+/g, '_');
                csrf = csrf.replace(/\//g, '-');
                var setCookie = res.getHeader('Set-Cookie') || [];
                setCookie.push('csrf=' + csrf+'; HttpOnly');
                res.setHeader('Set-Cookie', setCookie);
                return csrf;
        });
}

function serverInitialize(config)
{
        var model = mongoInit(config);
        var app = express();
        var server = http.createServer(app);
        var primus = primusServer(server);
        var document = domv.createHtmlDomDocument(true);
        var sessionStore = new MongoSessionStore(model);

        app.set('x-powered-by', false);

        app.use(morgan('short'));
        //primus.before('morgan', morgan('short'));

        app.use('/bundle.css', staticFile('generated-web/bundle.css', 'text/css; charset=UTF-8'));
        app.use('/bundle.js'  , staticFile('generated-web/bundle.js', 'text/javascript; charset=UTF-8'));

        //app.use('/favicon.ico', staticFile('lib/view/style/favicon.ico'));

        app.use(cookieParser());

        app.use(bodyParser.urlencoded({
                extended: true
        }));

        app.use(expressSession({
                name: 'sid',
                secret: config.sessionSecret,
                cookie: { path: '/', httpOnly: true, secure: false, maxAge: null },
                resave: false,
                saveUninitialized: false,
                store: sessionStore
        }));

        passportInit(model, config, app, primus);

        app.use(function controllerRequest(req, res)
        {
                // These Resource objects are used for the server to make
                // request for its own controllers.  Therefor spark = null;
                var contextResources = resources(null, model, config, req.user);

                var context = new ClientContext(true);
                context.router = router;
                context.document = new BaseDocument(document, router);
                context.resource = contextResources;
                context.setUserFromDB(req.user);
                context.sessionID = req.sessionID;
                context.document.addJSONData('mySessionID', context.sessionID);

                context.document.setUser(context.user);

                var front = new FrontController(context);
                var states = context.router.parse(req.url);

                if (config.googleAnalyticsUACode)
                {
                        context.document.addGoogleAnalytics(
                                config.googleAnalyticsUACode,
                                packageJson.name,
                                packageJson.version
                        );
                }

                getOrSetCsrfToken(req, res).then(function(token)
                {
                        context.csrfToken = token;
                        context.document.addJSONData('myCsrfToken', token);

                        if (req.method === 'POST')
                        {
                                context.csrfValid = req.body.csrf === context.csrfToken;
                                context.postData = req.body;
                                delete context.postData.csrf;
                        }

                }).then(function()
                {
                        return front.state(states);

                }).then(function()
                {
                        if (states[0] === '404')
                        {
                                res.statusCode = 404;
                        }

                        context.document.sendResponseAsHtml(res);

                }).catch(function(err)
                {
                        errorRequest(err, req, res, function(){});
                })
                .done();
        });

        app.use(errorRequest);

        primus.on('connection', function(spark)
        {
                spark.on('EHLO', function(data, result)
                {
                        spark.mySessionID = (data.sessionID || '') + '';

                        sessionStore.getAsync(spark.mySessionID)
                        .bind({ // result object
                                user: null
                        })
                        .then(function(session)
                        {
                                spark.mySession = session;
                                if (!session ||
                                    !session.passport ||
                                    !session.passport.user)
                                {
                                        return P.resolve(null);
                                }

                                return P.using(model.acquire(), function(client)
                                {
                                        var userCollection = P.promisifyAll(client.collection('user'));
                                        return userCollection.findOneAsync({_id: session.passport.user});
                                });
                        })
                        .then(function(user)
                        {
                                this.user = ClientContext.convertUserFromDB(user);
                                spark.myUser = user;
                                spark.resources = resources(spark, model, config, user);
                        })
                        .catch(function(err)
                        {
                                console.error('Error retrieving session/user', err);
                                spark.resources = resources(spark, model, config, null);
                        }).finally(function()
                        {
                                result(this);
                        });
                });
        });

        return P.resolve({
                server: server,
                primus: primus
        });
}

function startServer()
{
        process.title = packageJson.name + ' ' + packageJson.version + ' (nodejs)';

        console.log(packageJson.name + ' ' + packageJson.version +
                    ' starting server...');

        serverInitialize(config)
                .then(function(initialized)
                {
                        initialized.server.listen(
                                config.listenPort,
                                config.listenHostname
                        );

                        console.log(packageJson.name + ' ' + packageJson.version +
                                    ' server running at http://' +
                                    config.listenHostname +
                                    ':' +
                                    config.listenPort +
                                    '/');
                }).done();
}

module.exports = serverInitialize;
if (require.main === module)
{
        startServer();
}<commit_msg>favicon explicit 404<commit_after>'use strict';
var packageJson = require('../package.json');
var http = require('http');
var express = require('express');
var accepts = require('accepts');
var expressSession = require('express-session');
var morgan = require('morgan');
var nodePath = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var domv = require('domv');
var P = require('bluebird');
var secureRandomBytes = P.promisify(require('crypto').randomBytes);

var mongoInit = require('./mongo-init');
var config = require('./server-config');
var primusServer = require('./primus-server');
var passportInit = require('./passport-init');

var BaseDocument = require('./view/BaseDocument');
var ClientContext = require('./ClientContext');
var FrontController = require('./controller/FrontController');
var router = require('./controller/router');
var resources = require('./resource/server');
var MongoSessionStore = require('./MongoSessionStore');

function staticFile(path, type)
{
        path = nodePath.resolve(__dirname + '/../' + path);
        var gzPath = path + '.gz';

        return function(req, res)
        {
                var accept = accepts(req);
                var method = accept.encodings(['gzip']);

                function plain()
                {
                        res.sendFile(path, {
                                lastModified: true,
                                maxAge      : '7d',
                                headers     : {
                                        'Content-Type': type
                                }
                        });
                }

                if (method === 'gzip')
                {
                        return res.sendFile(
                                gzPath,
                                {
                                        lastModified: true,
                                        maxAge      : '7d',
                                        headers     : {
                                                'Content-Encoding': 'gzip',
                                                'Content-Type': type
                                        }
                                },
                                function(err)
                                {
                                        if (err)
                                        {
                                                plain();
                                        }
                                }
                        );
                }

                plain();
        };
}

function errorRequest(err, req, res, next)
{
        console.error(err.stack, '\n');
        res.header('Content-Type', 'text/plain; charset=UTF-8');
        res.status(500);
        res.send(err.stack);
        next();
}

function getOrSetCsrfToken(req, res)
{
        if (req.cookies.csrf)
        {
                return P.resolve(req.cookies.csrf);
        }

        // secureRandomBytes() might fail if there is not entropy
        return secureRandomBytes(128).then(function(buf)
        {
                var csrf = buf.toString('base64');
                csrf = csrf.replace(/\+/g, '_');
                csrf = csrf.replace(/\//g, '-');
                var setCookie = res.getHeader('Set-Cookie') || [];
                setCookie.push('csrf=' + csrf+'; HttpOnly');
                res.setHeader('Set-Cookie', setCookie);
                return csrf;
        });
}

function serverInitialize(config)
{
        var model = mongoInit(config);
        var app = express();
        var server = http.createServer(app);
        var primus = primusServer(server);
        var document = domv.createHtmlDomDocument(true);
        var sessionStore = new MongoSessionStore(model);

        app.set('x-powered-by', false);

        app.use(morgan('short'));
        //primus.before('morgan', morgan('short'));

        app.use('/favicon.ico', function(req, res)
        {
                res.statusCode = 404;
                res.setHeader('Content-Type' ,'text/plain; charset=UTF-8');
                res.end('404');
        });
        app.use('/bundle.css', staticFile('generated-web/bundle.css', 'text/css; charset=UTF-8'));
        app.use('/bundle.js'  , staticFile('generated-web/bundle.js', 'text/javascript; charset=UTF-8'));

        //app.use('/favicon.ico', staticFile('lib/view/style/favicon.ico'));

        app.use(cookieParser());

        app.use(bodyParser.urlencoded({
                extended: true
        }));

        app.use(expressSession({
                name: 'sid',
                secret: config.sessionSecret,
                cookie: { path: '/', httpOnly: true, secure: false, maxAge: null },
                resave: false,
                saveUninitialized: false,
                store: sessionStore
        }));

        passportInit(model, config, app, primus);

        app.use(function controllerRequest(req, res)
        {
                // These Resource objects are used for the server to make
                // request for its own controllers.  Therefor spark = null;
                var contextResources = resources(null, model, config, req.user);

                var context = new ClientContext(true);
                context.router = router;
                context.document = new BaseDocument(document, router);
                context.resource = contextResources;
                context.setUserFromDB(req.user);
                context.sessionID = req.sessionID;
                context.document.addJSONData('mySessionID', context.sessionID);

                context.document.setUser(context.user);

                var front = new FrontController(context);
                var states = context.router.parse(req.url);

                if (config.googleAnalyticsUACode)
                {
                        context.document.addGoogleAnalytics(
                                config.googleAnalyticsUACode,
                                packageJson.name,
                                packageJson.version
                        );
                }

                getOrSetCsrfToken(req, res).then(function(token)
                {
                        context.csrfToken = token;
                        context.document.addJSONData('myCsrfToken', token);

                        if (req.method === 'POST')
                        {
                                context.csrfValid = req.body.csrf === context.csrfToken;
                                context.postData = req.body;
                                delete context.postData.csrf;
                        }

                }).then(function()
                {
                        return front.state(states);

                }).then(function()
                {
                        if (states[0] === '404')
                        {
                                res.statusCode = 404;
                        }

                        context.document.sendResponseAsHtml(res);

                }).catch(function(err)
                {
                        errorRequest(err, req, res, function(){});
                })
                .done();
        });

        app.use(errorRequest);

        primus.on('connection', function(spark)
        {
                spark.on('EHLO', function(data, result)
                {
                        spark.mySessionID = (data.sessionID || '') + '';

                        sessionStore.getAsync(spark.mySessionID)
                        .bind({ // result object
                                user: null
                        })
                        .then(function(session)
                        {
                                spark.mySession = session;
                                if (!session ||
                                    !session.passport ||
                                    !session.passport.user)
                                {
                                        return P.resolve(null);
                                }

                                return P.using(model.acquire(), function(client)
                                {
                                        var userCollection = P.promisifyAll(client.collection('user'));
                                        return userCollection.findOneAsync({_id: session.passport.user});
                                });
                        })
                        .then(function(user)
                        {
                                this.user = ClientContext.convertUserFromDB(user);
                                spark.myUser = user;
                                spark.resources = resources(spark, model, config, user);
                        })
                        .catch(function(err)
                        {
                                console.error('Error retrieving session/user', err);
                                spark.resources = resources(spark, model, config, null);
                        }).finally(function()
                        {
                                result(this);
                        });
                });
        });

        return P.resolve({
                server: server,
                primus: primus
        });
}

function startServer()
{
        process.title = packageJson.name + ' ' + packageJson.version + ' (nodejs)';

        console.log(packageJson.name + ' ' + packageJson.version +
                    ' starting server...');

        serverInitialize(config)
                .then(function(initialized)
                {
                        initialized.server.listen(
                                config.listenPort,
                                config.listenHostname
                        );

                        console.log(packageJson.name + ' ' + packageJson.version +
                                    ' server running at http://' +
                                    config.listenHostname +
                                    ':' +
                                    config.listenPort +
                                    '/');
                }).done();
}

module.exports = serverInitialize;
if (require.main === module)
{
        startServer();
}<|endoftext|>


<commit_before>const md = require('markdown-it')
const mdAttrs = require('markdown-it-attrs')
const _ = require('lodash')

const quoteStyles = {
  Chinese: '””‘’',
  English: '“”‘’',
  French: ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'],
  German: '„“‚‘',
  Greek: '«»‘’',
  Japanese: '「」「」',
  Hungarian: '„”’’',
  Polish: '„”‚‘',
  Portuguese: '«»‘’',
  Russian: '«»„“',
  Spanish: '«»‘’',
  Swedish: '””’’'
}

module.exports = {
  async render() {
    const mkdown = md({
      html: this.config.allowHTML,
      breaks: this.config.linebreaks,
      linkify: this.config.linkify,
      typographer: this.config.typographer,
      quotes: _.get(quoteStyles, this.config.quotes, quoteStyles.English),
      highlight(str, lang) {
        return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
      }
    })

    mkdown.use(mdAttrs)

    for (let child of this.children) {
      const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
      renderer.init(mkdown, child.config)
    }

    return mkdown.render(this.input)
  }
}
<commit_msg>fix: exclude non-class attributes from markdown rendering<commit_after>const md = require('markdown-it')
const mdAttrs = require('markdown-it-attrs')
const _ = require('lodash')

const quoteStyles = {
  Chinese: '””‘’',
  English: '“”‘’',
  French: ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'],
  German: '„“‚‘',
  Greek: '«»‘’',
  Japanese: '「」「」',
  Hungarian: '„”’’',
  Polish: '„”‚‘',
  Portuguese: '«»‘’',
  Russian: '«»„“',
  Spanish: '«»‘’',
  Swedish: '””’’'
}

module.exports = {
  async render() {
    const mkdown = md({
      html: this.config.allowHTML,
      breaks: this.config.linebreaks,
      linkify: this.config.linkify,
      typographer: this.config.typographer,
      quotes: _.get(quoteStyles, this.config.quotes, quoteStyles.English),
      highlight(str, lang) {
        return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
      }
    })

    mkdown.use(mdAttrs, {
      allowedAttributes: ['id', 'class']
    })

    for (let child of this.children) {
      const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
      renderer.init(mkdown, child.config)
    }

    return mkdown.render(this.input)
  }
}
<|endoftext|>