<commit_before>(function() {
/** Policy generation script of autoCSP. */

// will contain a Map of Node types we want to visit
var visit = null;

var sanitizeUri = function(uri) {
    /** Nullifies data- and Same-Origin-URIs. */
    return ($.empty(uri) || $s.startsWith(uri, 'data:') ||
            $s.startsWith(uri, location.origin)) ? null : uri;
};

/** Overwrite JS APIs for connect-src. */
var xhrOpen = window.XMLHttpRequest.prototype.open;
var newOpen = function() {
    var uri = sanitizeUri(arguments[1]);
    if (uri) {
        $.sendToBackend({'connect-src': [uri]}, '{{ report_uri }}');
    }
    xhrOpen.apply(this, arguments);
};
Object.defineProperty(window.XMLHttpRequest.prototype, 'open',
                      {configurable: false, value: newOpen});
var wsConstructor = window.WebSocket;
window.WebSocket = function(uri, protocols) {
    var connect_uri = sanitizeUri(uri);
    if (connect_uri) {
        $.sendToBackend({'connect-src': [connect_uri]}, '{{ report_uri }}');
    }
    return new wsConstructor(uri, protocols);
};
window.WebSocket.prototype = wsConstructor;

/** Contains all functions used to extract sources. **/
(function() {
    var getSrc = function(e) {
        return sanitizeUri(e.src);
    };
    var getBaseUri = function(e) {
        return sanitizeUri(e.href);
    };
    var getAppletAttr = function(e) {
        var uris = [];
        $a.forEach(['code', 'archive', 'codebase'], function(prop) {
            if (e[prop]) {
                uris.push(sanitizeUri($.toAbsoluteUri(e[prop])));
            }
        });
        return uris;
    };
    var frames = [];
    var getFrameSrc = function(e) {
        if (!$a.in(frames, e)) {
            frames.push(e);
            var blocknext = false;
            e.addEventListener('load', function() {
                if (blocknext) {
                    blocknext = false;
                } else {
                    // re-set the src to the navigated location to trigger a
                    // Mutation Observer attribute change
                    // XXX: can cause problems
                    this.src = e.contentWindow.location;
                    blocknext = true;
                }
            });
        }
        return getSrc(e);
    };
    var getFormAction = function(e) {
        return sanitizeUri(e.action);
    };
    var getButtonFormAction = function(e) {
        return sanitizeUri(e.formAction);
    };
    var getObjectData = function(e) {
        return sanitizeUri(e.data);
    };
    var getBackgroundImage = function(e) {
        /** Retrieve all background image URIs. */
        var bg = window.getComputedStyle(e, false).backgroundImage;
        var match = bg.match(/url\('?([^)]*[^']+)'?\)/);
        return (match) ? sanitizeUri(match[1]) : null;
    };
    var getIcon = function(e) {
        return (e.rel === 'icon') ? sanitizeUri(e.href) : null;
    };

    // remembers all stylesheets (prevent crawling rules a second time)
    var styles = [];
    var appendStyle = function(stylesheet, newStyles) {
        /** If a stylesheet was found this will check if its new and recurse. */
        // unfortunately we cannot know if we already visited an inline style
        var uri = sanitizeUri(stylesheet.href);
        if (!$a.in(styles, uri)) {
            // don't add to styles list if inline
            if (!$.empty(uri)) {
                newStyles.push(uri);
                styles.push(uri);
            }
            if ($.empty(stylesheet.rules)) {
                return;
            }
            for (var i = 0; i < stylesheet.rules.length; i++) {
                // @import rules are always on top so break if no import
                if (stylesheet.rules[i].constructor !== CSSImportRule) {
                    break;
                }
                // recurse into stylesheet (could contain more @imports)
                appendStyle(stylesheet.rules[i].styleSheet, newStyles);
            }
        }
    };
    var checkStyles = function(e) {
        /** Checks for new stylesheets in document.styleSheets. */
        var newStyles = [];
        $a.forEach(document.styleSheets, function(stylesheet) {
            appendStyle(stylesheet, newStyles);
        });
        return newStyles;
    };

    visit = {
        '*': {'img-src': getBackgroundImage},
        'APPLET': {'object-src': getAppletAttr},
        'AUDIO': {'media-src': getSrc},
        'BASE': {'base-uri': getBaseUri},
        'BUTTON': {'form-action': getButtonFormAction},
        'EMBED': {'object-src': getSrc},
        'FORM': {'form-action': getFormAction},
        'FRAME': {'frame-src': getFrameSrc},
        'IFRAME': {'frame-src': getFrameSrc},
        'IMG': {'img-src': getSrc},
        'INPUT': {'form-action': getButtonFormAction},
        'LINK': {'img-src': getIcon, 'style-src': checkStyles},
        'OBJECT': {'object-src': getObjectData},
        'SCRIPT': {'script-src': getSrc},
        'SOURCE': {'media-src': getSrc},
        'STYLE': {'style-src': checkStyles},
        'TRACK': {'media-src': getSrc},
        'VIDEO': {'media-src': getSrc},
    };
})();

window.addEventListener('load', function() {
    $.log('Policy.js: Start processing all nodes to infer rules...');
    var startTime = Date.now();

    var rules = $.processNodes(document.getElementsByTagName('*'), visit);
    $.sendToBackend(rules, '{{ report_uri }}');

    var delta = Date.now() - startTime;
    $.log('Policy.js: Finished processing all nodes in ' + delta + ' ms.');

    // register an observer for future changes (tree mod and attributes)
    var observer = new MutationObserver(function(mutations) {
        $a.forEach(mutations, function(mutation) {
            var nodes = (mutation.type === 'childList') ? mutation.addedNodes :
                                                          [mutation.target];
            var rules = $.processNodes(nodes, visit);
            $.sendToBackend(rules, '{{ report_uri }}');
        });
    });
    observer.observe(document.body.parentNode,
                     {subtree: true, childList: true, attributes: true});
});

})();<commit_msg>Fix of relative URIs in XMLHttpRequest.<commit_after>(function() {
/** Policy generation script of autoCSP. */

// will contain a Map of Node types we want to visit
var visit = null;

var sanitizeUri = function(uri) {
    /** Nullifies data- and Same-Origin-URIs. */
    return ($.empty(uri) || $s.startsWith(uri, 'data:') ||
            $s.startsWith(uri, location.origin)) ? null : uri;
};

/** Overwrite JS APIs for connect-src. */
var xhrOpen = window.XMLHttpRequest.prototype.open;
var newOpen = function() {
    var uri = sanitizeUri($.toAbsoluteUri(arguments[1]));
    if (uri) {
        $.sendToBackend({'connect-src': [uri]}, '{{ report_uri }}');
    }
    xhrOpen.apply(this, arguments);
};
Object.defineProperty(window.XMLHttpRequest.prototype, 'open',
                      {configurable: false, value: newOpen});
var wsConstructor = window.WebSocket;
window.WebSocket = function(uri, protocols) {
    var connect_uri = sanitizeUri(uri);
    if (connect_uri) {
        $.sendToBackend({'connect-src': [connect_uri]}, '{{ report_uri }}');
    }
    return new wsConstructor(uri, protocols);
};
window.WebSocket.prototype = wsConstructor;

/** Contains all functions used to extract sources. **/
(function() {
    var getSrc = function(e) {
        return sanitizeUri(e.src);
    };
    var getBaseUri = function(e) {
        return sanitizeUri(e.href);
    };
    var getAppletAttr = function(e) {
        var uris = [];
        $a.forEach(['code', 'archive', 'codebase'], function(prop) {
            if (e[prop]) {
                uris.push(sanitizeUri($.toAbsoluteUri(e[prop])));
            }
        });
        return uris;
    };
    var frames = [];
    var getFrameSrc = function(e) {
        if (!$a.in(frames, e)) {
            frames.push(e);
            var blocknext = false;
            e.addEventListener('load', function() {
                if (blocknext) {
                    blocknext = false;
                } else {
                    // re-set the src to the navigated location to trigger a
                    // Mutation Observer attribute change
                    // XXX: can cause problems
                    this.src = e.contentWindow.location;
                    blocknext = true;
                }
            });
        }
        return getSrc(e);
    };
    var getFormAction = function(e) {
        return sanitizeUri(e.action);
    };
    var getButtonFormAction = function(e) {
        return sanitizeUri(e.formAction);
    };
    var getObjectData = function(e) {
        return sanitizeUri(e.data);
    };
    var getBackgroundImage = function(e) {
        /** Retrieve all background image URIs. */
        var bg = window.getComputedStyle(e, false).backgroundImage;
        var match = bg.match(/url\('?([^)]*[^']+)'?\)/);
        return (match) ? sanitizeUri(match[1]) : null;
    };
    var getIcon = function(e) {
        return (e.rel === 'icon') ? sanitizeUri(e.href) : null;
    };

    // remembers all stylesheets (prevent crawling rules a second time)
    var styles = [];
    var appendStyle = function(stylesheet, newStyles) {
        /** If a stylesheet was found this will check if its new and recurse. */
        // unfortunately we cannot know if we already visited an inline style
        var uri = sanitizeUri(stylesheet.href);
        if (!$a.in(styles, uri)) {
            // don't add to styles list if inline
            if (!$.empty(uri)) {
                newStyles.push(uri);
                styles.push(uri);
            }
            if ($.empty(stylesheet.rules)) {
                return;
            }
            for (var i = 0; i < stylesheet.rules.length; i++) {
                // @import rules are always on top so break if no import
                if (stylesheet.rules[i].constructor !== CSSImportRule) {
                    break;
                }
                // recurse into stylesheet (could contain more @imports)
                appendStyle(stylesheet.rules[i].styleSheet, newStyles);
            }
        }
    };
    var checkStyles = function(e) {
        /** Checks for new stylesheets in document.styleSheets. */
        var newStyles = [];
        $a.forEach(document.styleSheets, function(stylesheet) {
            appendStyle(stylesheet, newStyles);
        });
        return newStyles;
    };

    visit = {
        '*': {'img-src': getBackgroundImage},
        'APPLET': {'object-src': getAppletAttr},
        'AUDIO': {'media-src': getSrc},
        'BASE': {'base-uri': getBaseUri},
        'BUTTON': {'form-action': getButtonFormAction},
        'EMBED': {'object-src': getSrc},
        'FORM': {'form-action': getFormAction},
        'FRAME': {'frame-src': getFrameSrc},
        'IFRAME': {'frame-src': getFrameSrc},
        'IMG': {'img-src': getSrc},
        'INPUT': {'form-action': getButtonFormAction},
        'LINK': {'img-src': getIcon, 'style-src': checkStyles},
        'OBJECT': {'object-src': getObjectData},
        'SCRIPT': {'script-src': getSrc},
        'SOURCE': {'media-src': getSrc},
        'STYLE': {'style-src': checkStyles},
        'TRACK': {'media-src': getSrc},
        'VIDEO': {'media-src': getSrc},
    };
})();

window.addEventListener('load', function() {
    $.log('Policy.js: Start processing all nodes to infer rules...');
    var startTime = Date.now();

    var rules = $.processNodes(document.getElementsByTagName('*'), visit);
    $.sendToBackend(rules, '{{ report_uri }}');

    var delta = Date.now() - startTime;
    $.log('Policy.js: Finished processing all nodes in ' + delta + ' ms.');

    // register an observer for future changes (tree mod and attributes)
    var observer = new MutationObserver(function(mutations) {
        $a.forEach(mutations, function(mutation) {
            var nodes = (mutation.type === 'childList') ? mutation.addedNodes :
                                                          [mutation.target];
            var rules = $.processNodes(nodes, visit);
            $.sendToBackend(rules, '{{ report_uri }}');
        });
    });
    observer.observe(document.body.parentNode,
                     {subtree: true, childList: true, attributes: true});
});

})();<|endoftext|>


<commit_before>/*global define*/
define([
         'Scene/Polygon',
         '../Specs/createContext',
         '../Specs/destroyContext',
         '../Specs/sceneState',
         'Core/Cartesian3',
         'Core/Cartographic3',
         'Core/Ellipsoid',
         'Core/Matrix4',
         'Core/Math'
     ], function(
         Polygon,
         createContext,
         destroyContext,
         sceneState,
         Cartesian3,
         Cartographic3,
         Ellipsoid,
         Matrix4,
         CesiumMath) {
    "use strict";
    /*global expect*/

    //createContext and destroyContext happen in the spec file.
    function renderMaterial(material, context) {
        var polygon = new Polygon();
        var camera = {
            eye : new Cartesian3(1.02, 0.0, 0.0),
            target : Cartesian3.ZERO,
            up : Cartesian3.UNIT_Z
        };
        var us = context.getUniformState();
        us.setView(Matrix4.createLookAt(camera.eye, camera.target, camera.up));
        us.setProjection(Matrix4.createPerspectiveFieldOfView(CesiumMath.toRadians(60.0), 1.0, 0.01, 10.0));

        var ellipsoid = Ellipsoid.UNIT_SPHERE;
        polygon.ellipsoid = ellipsoid;
        polygon.granularity = CesiumMath.toRadians(20.0);
        polygon.setPositions([
                              ellipsoid.toCartesian(CesiumMath.cartographic3ToRadians(new Cartographic3(-50.0, -50.0, 0.0))),
                              ellipsoid.toCartesian(CesiumMath.cartographic3ToRadians(new Cartographic3(50.0, -50.0, 0.0))),
                              ellipsoid.toCartesian(CesiumMath.cartographic3ToRadians(new Cartographic3(50.0, 50.0, 0.0))),
                              ellipsoid.toCartesian(CesiumMath.cartographic3ToRadians(new Cartographic3(-50.0, 50.0, 0.0)))
                             ]);
        polygon.material = material;

        context.clear();
        expect(context.readPixels()).toEqualArray([0, 0, 0, 0]);

        polygon.update(context, sceneState);
        polygon.render(context, us);
        var pixel = context.readPixels();

        polygon = polygon && polygon.destroy();
        return pixel;
    }

    return renderMaterial;
});<commit_msg>Revert "Revert "Partial material tests fix""<commit_after>/*global define*/
define([
         'Scene/Polygon',
         '../Specs/createContext',
         '../Specs/destroyContext',
         '../Specs/sceneState',
         'Core/Cartesian3',
         'Core/Cartographic',
         'Core/Ellipsoid',
         'Core/Matrix4',
         'Core/Math'
     ], function(
         Polygon,
         createContext,
         destroyContext,
         sceneState,
         Cartesian3,
         Cartographic,
         Ellipsoid,
         Matrix4,
         CesiumMath) {
    "use strict";
    /*global expect*/

    //createContext and destroyContext happen in the spec file.
    function renderMaterial(material, context) {
        var polygon = new Polygon();
        var camera = {
            eye : new Cartesian3(1.02, 0.0, 0.0),
            target : Cartesian3.ZERO,
            up : Cartesian3.UNIT_Z
        };
        var us = context.getUniformState();
        us.setView(Matrix4.createLookAt(camera.eye, camera.target, camera.up));
        us.setProjection(Matrix4.createPerspectiveFieldOfView(CesiumMath.toRadians(60.0), 1.0, 0.01, 10.0));

        var ellipsoid = Ellipsoid.UNIT_SPHERE;
        polygon.ellipsoid = ellipsoid;
        polygon.granularity = CesiumMath.toRadians(20.0);
        polygon.setPositions([
                              ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(-50.0, -50.0, 0.0)),
                              ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(50.0, -50.0, 0.0)),
                              ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(50.0, 50.0, 0.0)),
                              ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(-50.0, 50.0, 0.0))
                             ]);
        polygon.material = material;

        context.clear();
        expect(context.readPixels()).toEqualArray([0, 0, 0, 0]);

        polygon.update(context, sceneState);
        polygon.render(context, us);
        var pixel = context.readPixels();

        polygon = polygon && polygon.destroy();
        return pixel;
    }

    return renderMaterial;
});<|endoftext|>


<commit_before>import React from 'react';

import Show from '../Show/Show';

import './List.css'

function List({ items, emptyMessage = 'Empty List' }) {
  const listItems = items.length
    ? items.map(item =>

          <Show item={item} />

      )
    :
{emptyMessage}
;

  return (

      {listItems}

  );
}

export default List;
<commit_msg>feat(List) Integrate "withPagination" HOC<commit_after>import React from 'react';

import withPagination from '../withPagination/withPagination'

import Show from '../Show/Show';

import './List.css'

function List({ items, emptyMessage = 'Empty List' }) {
  const listItems = items.length
    ? items.map(item =>

          <Show item={item} />

      )
    :
{emptyMessage}
;

  return (

      {listItems}

  );
}

export default withPagination(List);
<|endoftext|>


<commit_before>'use strict';

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Token = mongoose.model('Token');
var jwt = require('jsonwebtoken');

router.post('/', function(req, res, next) {
    let token = jwt.sign({
        data: req.body.key
    }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRES_IN });

    let payload = {
        token: token
    };

    Token.create(payload, function(err, data){
        if (err) {
            req.body = err;
            return res.status(400).json(err);
        }

        res.status(201).json(data);
    });

});

router.get('/', function(req, res, next) {

    Token.find(function(err, data){
        res.status(200).json(data);
    });

});

router.delete('/', function(req, res, next) {

    Token.remove(req.params.id, function(err, data){
        if(err)
            return res.status(400).json(err);

        res.status(202).send(data);
    });
});

router.get('/:id', function(req, res, next) {

    Token.findById(req.params.id, function(err, data){
        if(!data)
            return res.status(404).send();

        res.status(200).json(data);
    });

});

router.delete('/:id', function(req, res, next) {

    Token.findByIdAndRemove(req.params.id, function(err, data){
        if(err)
            return res.status(400).json(err);

        if(!data)
            return res.status(404).send();

        res.status(202).send(data);
    });
});

module.exports = router;
<commit_msg>use promises for mongoose<commit_after>'use strict';

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Token = mongoose.model('Token');
var jwt = require('jsonwebtoken');

router.post('/', function(req, res, next) {
    let token = jwt.sign({
        data: req.body.key
    }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRES_IN });

    Token.create({token: token})
        .then((data) => {
            res.status(201).json(data);
        })
        .catch((err) => {
            res.status(400).json(err);
        });
});

router.get('/', function(req, res, next) {
    Token.find()
        .then(function(data){
            res.status(200).json(data);
        })
        .catch(function(err){
            res.status(500).json(err);
        });
});

router.delete('/', function(req, res, next) {

    Token.remove()
        .then(function(data){
            res.status(202).send(data);
        })
        .catch(function(err){
            res.status(400).json(err);
        });
});

router.get('/:id', function(req, res, next) {

    Token.findById(req.params.id)
        .then((data) => {
            res.status(200).json(data);
        })
        .catch((err) => {
            res.status(404).send();
        });
});

router.delete('/:id', function(req, res, next) {

    Token.findByIdAndRemove(req.params.id)
        .then((data) => {
            if(!data)
                return res.status(404).send();

            res.status(202).send(data);
        })
        .catch((err) => {
            res.status(400).json(err);
        });
});

module.exports = router;
<|endoftext|>


<commit_before>var fs = require("fs");
var https = require("https");
var http = require("http");
var express = require("express");
var console = require("console");

var app = express();
var cors = require('cors');

try
{
    var settings = JSON.parse(fs.readFileSync("settings.json"));
}
catch(e)
{
    console.log("Error: Configuration file settings.json couldn't be parsed.\nSee Troubleshooting section in README.md for instructions.");
    process.exit(1);
}

if(settings.Server.enable_https)
{
    try
    {
        var privateKey  = fs.readFileSync(settings.Server.ssl_key_file).toString();
    }
    catch(e)
    {
        console.log("Error: unable to load SSL key. Please edit setting.json and put the correct path to your SSL private key file into \"ssl_key_file.\"");
        process.exit(1);
    }
    try
    {
        var certificate = fs.readFileSync(settings.Server.ssl_certificate_file).toString();
    }
    catch(e)
    {
        console.log("Error: unable to load SSL certificate. Please edit setting.json and put the correct path to your SSL certificate file into \"ssl_certificate_file.\"");
        process.exit(1);
    }
    var credentials = {key: privateKey, cert: certificate};
}

maxCallsPerMinute = {};
maxCallsPerMinutePerIP = {};
callsRemaining = {};
perIPCounter = {};
droppedCallsCounter = {};

var invalidRequestCounter = 0;
var forbiddenCallCounter = 0;
var connectionErrorMessageDisplayed = false;

var auth = "Basic " + new Buffer(settings.RPC.user + ":" + settings.RPC.password).toString("base64");

settings.CallLimits.forEach(function(x) {
    maxCallsPerMinute[x.name] = x.maxPerMinute;
    maxCallsPerMinutePerIP[x.name] = x.maxPerMinutePerIP;
    callsRemaining[x.name] = x.maxPerMinute;
    droppedCallsCounter[x.name] = 0;
});

CounterInstance = function()
{
    this.overLimit=0;
    this.invalidRequests=0;
    this.forbiddenCalls=0;
    this.callsRemaining={};
    for (x in maxCallsPerMinutePerIP)
    {
        if(maxCallsPerMinutePerIP[x]!==null)
        this.callsRemaining[x]=maxCallsPerMinutePerIP[x];
    }
}

require("log-timestamp");

app.use(cors());

app.get("*", function(request, response)
{
    if(settings.Server.enable_https&&request.protocol=="http")
    {
        if(settings.Server.https_port==443)
            secureUrl="https://"+request.host+request.path;
        else
            secureUrl="https://"+request.host+":"+settings.Server.https_port+request.path;
        response.writeHead(302, {"Location": secureUrl});
        response.end();
        return;
    }

    request.headers.Authorization = auth;

    var webProxy = http.request({host: settings.RPC.host, port: settings.RPC.port, method: request.method, path: request.path, headers: request.headers}, function (proxy_res)
    {
        proxy_res.pipe(response, {end: true});
    });

    webProxy.on("error", function(error)
    {
        if(!connectionErrorMessageDisplayed)
        {
            console.log("Error: cannot connect to twisterd.\nSee Troubleshooting section in README.md for instructions.");
            connectionErrorMessageDisplayed=true;
        }
        response.send(502);
    });

    request.pipe(webProxy, {end: true});
});

app.post("/", function(request, response)
{
    request.rawBody = "";
    request.setEncoding("utf8");
    request.headers.Authorization = auth;

    request.addListener("data", function(chunk)
    {
        request.rawBody += chunk;
    });

    request.addListener("end", function()
    {
        var remoteIP = request.connection.remoteAddress;

        if(perIPCounter[remoteIP]===undefined)
        {
            perIPCounter[remoteIP]=new CounterInstance();
        }
        try
        {
            bodyJson=JSON.parse(request.rawBody);
            rpcMethod=bodyJson.method;
        }
        catch(e)
        {
            perIPCounter[remoteIP].invalidRequests++;
            invalidRequestCounter++;
            return;
        }
        if(maxCallsPerMinute[rpcMethod]===undefined || maxCallsPerMinute[rpcMethod]===0)
        {
            perIPCounter[remoteIP].forbiddenCalls++;
            forbiddenCallCounter++;
            return;
        }
        if(maxCallsPerMinute[rpcMethod]!==null)
        {
            if(callsRemaining[rpcMethod]<1)
            {
                droppedCallsCounter[rpcMethod]++;
                return;
            }
            else
            {
                callsRemaining[rpcMethod]--;
            }
        }

        if(maxCallsPerMinutePerIP[rpcMethod]!==null)
        {
            if(perIPCounter[remoteIP].callsRemaining[rpcMethod]<1)
            {
                perIPCounter[remoteIP].overLimit++;
                return;
            }
            else
            {
                perIPCounter[remoteIP].callsRemaining[rpcMethod]--;
            }
        }

        var rpcProxy = http.request({host: settings.RPC.host, port: settings.RPC.port, method: request.method, headers: request.headers}, function(proxy_res)
        {
            proxy_res.on("data", function(chunk)
            {
                response.write(chunk, "binary");
            });

            proxy_res.on("end", function(chunk)
            {
                response.end();
            });

            proxy_res.on("error", function(error)
            {
                if(!connectionErrorMessageDisplayed)
                {
                    console.log("Error: cannot connect to twisterd.\nSee Troubleshooting section in README.md for instructions.");
                    connectionErrorMessageDisplayed=true;
                }
                response.send(502);
            });

            response.writeHead(proxy_res.statusCode, proxy_res.headers);
        });

        rpcProxy.write(request.rawBody, "binary");
        rpcProxy.end();
    });
});

if(settings.Server.enable_https)
{
    https.createServer(credentials, app).listen(settings.Server.https_port);
}

http.createServer(app).listen(settings.Server.http_port);

setInterval(function()
{
    for(method in maxCallsPerMinute)
    {
        callsRemaining[method] = maxCallsPerMinute[method];
        if(droppedCallsCounter[method]!==0)
        {
            console.log("Dropped "+droppedCallsCounter[method]+" calls to "+method+" over the limit of "+maxCallsPerMinute[method]);
            droppedCallsCounter[method] = 0;
        }
    };
    if(invalidRequestCounter!==0)
    {
        console.log("Received "+invalidRequestCounter+" invalid POST requests.");
        invalidRequestCounter = 0;
    }
    if(forbiddenCallCounter!==0)
    {
        console.log("Denied "+forbiddenCallCounter+" attempts to access forbidden API calls.");
        forbiddenCallCounter = 0;
    }

    for(ip in perIPCounter)
    {
        if(perIPCounter[ip].overLimit>=settings.LogAsAttackThreshold.callsOverLimits)
        {
            console.log("IP "+ip+" tried to send "+perIPCounter[ip].overLimit+" calls more than the limits allow.");
        };
        if(perIPCounter[ip].invalidRequests>=settings.LogAsAttackThreshold.invalidRequests)
        {
            console.log("IP "+ip+" sent "+perIPCounter[ip].invalidRequests+" invalid request that couldn't be parsed.");
        };
        if(perIPCounter[ip].forbiddenCalls>=settings.LogAsAttackThreshold.forbiddenCalls)
        {
            console.log("IP "+ip+" tried to send "+perIPCounter[ip].forbiddenCalls+" calls to forbidden functions.");
        };
    }

    perIPCounter = {};
    connectionErrorMessageDisplayed = false;

}, 60000);
<commit_msg>Handle the Listen EAccess error<commit_after>var fs = require("fs");
var https = require("https");
var http = require("http");
var express = require("express");
var console = require("console");

var app = express();
var cors = require('cors');

function handlePortAccessError()
{
    console.log("Error: Node.js doesn't allow access to ports lower than 1024 in normal user mode.");
    console.log("   Possible solutions:");
    console.log("       1 - Edit settings.json and change \"http_port\": 80 to a higher number. It will then run under any user.");
    console.log("       2 - If you want to use standard HTTP port, run the server as root: sudo node twister-proxy.js &");
    process.exit(1);
}

try
{
    var settings = JSON.parse(fs.readFileSync("settings.json"));
}
catch(e)
{
    console.log("Error: Configuration file settings.json couldn't be parsed.\nSee Troubleshooting section in README.md for instructions.");
    process.exit(1);
}

if(settings.Server.enable_https)
{
    try
    {
        var privateKey  = fs.readFileSync(settings.Server.ssl_key_file).toString();
    }
    catch(e)
    {
        console.log("Error: unable to load SSL key. Please edit setting.json and put the correct path to your SSL private key file into \"ssl_key_file.\"");
        process.exit(1);
    }
    try
    {
        var certificate = fs.readFileSync(settings.Server.ssl_certificate_file).toString();
    }
    catch(e)
    {
        console.log("Error: unable to load SSL certificate. Please edit setting.json and put the correct path to your SSL certificate file into \"ssl_certificate_file.\"");
        process.exit(1);
    }
    var credentials = {key: privateKey, cert: certificate};
}

maxCallsPerMinute = {};
maxCallsPerMinutePerIP = {};
callsRemaining = {};
perIPCounter = {};
droppedCallsCounter = {};

var invalidRequestCounter = 0;
var forbiddenCallCounter = 0;
var connectionErrorMessageDisplayed = false;

var auth = "Basic " + new Buffer(settings.RPC.user + ":" + settings.RPC.password).toString("base64");

settings.CallLimits.forEach(function(x) {
    maxCallsPerMinute[x.name] = x.maxPerMinute;
    maxCallsPerMinutePerIP[x.name] = x.maxPerMinutePerIP;
    callsRemaining[x.name] = x.maxPerMinute;
    droppedCallsCounter[x.name] = 0;
});

CounterInstance = function()
{
    this.overLimit=0;
    this.invalidRequests=0;
    this.forbiddenCalls=0;
    this.callsRemaining={};
    for (x in maxCallsPerMinutePerIP)
    {
        if(maxCallsPerMinutePerIP[x]!==null)
        this.callsRemaining[x]=maxCallsPerMinutePerIP[x];
    }
}

require("log-timestamp");

app.use(cors());

app.get("*", function(request, response)
{
    if(settings.Server.enable_https&&request.protocol=="http")
    {
        if(settings.Server.https_port==443)
            secureUrl="https://"+request.host+request.path;
        else
            secureUrl="https://"+request.host+":"+settings.Server.https_port+request.path;
        response.writeHead(302, {"Location": secureUrl});
        response.end();
        return;
    }

    request.headers.Authorization = auth;

    var webProxy = http.request({host: settings.RPC.host, port: settings.RPC.port, method: request.method, path: request.path, headers: request.headers}, function (proxy_res)
    {
        proxy_res.pipe(response, {end: true});
    });

    webProxy.on("error", function(error)
    {
        if(!connectionErrorMessageDisplayed)
        {
            console.log("Error: cannot connect to twisterd.\nSee Troubleshooting section in README.md for instructions.");
            connectionErrorMessageDisplayed=true;
        }
        response.send(502);
    });

    request.pipe(webProxy, {end: true});
});

app.post("/", function(request, response)
{
    request.rawBody = "";
    request.setEncoding("utf8");
    request.headers.Authorization = auth;

    request.addListener("data", function(chunk)
    {
        request.rawBody += chunk;
    });

    request.addListener("end", function()
    {
        var remoteIP = request.connection.remoteAddress;

        if(perIPCounter[remoteIP]===undefined)
        {
            perIPCounter[remoteIP]=new CounterInstance();
        }
        try
        {
            bodyJson=JSON.parse(request.rawBody);
            rpcMethod=bodyJson.method;
        }
        catch(e)
        {
            perIPCounter[remoteIP].invalidRequests++;
            invalidRequestCounter++;
            return;
        }
        if(maxCallsPerMinute[rpcMethod]===undefined || maxCallsPerMinute[rpcMethod]===0)
        {
            perIPCounter[remoteIP].forbiddenCalls++;
            forbiddenCallCounter++;
            return;
        }
        if(maxCallsPerMinute[rpcMethod]!==null)
        {
            if(callsRemaining[rpcMethod]<1)
            {
                droppedCallsCounter[rpcMethod]++;
                return;
            }
            else
            {
                callsRemaining[rpcMethod]--;
            }
        }

        if(maxCallsPerMinutePerIP[rpcMethod]!==null)
        {
            if(perIPCounter[remoteIP].callsRemaining[rpcMethod]<1)
            {
                perIPCounter[remoteIP].overLimit++;
                return;
            }
            else
            {
                perIPCounter[remoteIP].callsRemaining[rpcMethod]--;
            }
        }

        var rpcProxy = http.request({host: settings.RPC.host, port: settings.RPC.port, method: request.method, headers: request.headers}, function(proxy_res)
        {
            proxy_res.on("data", function(chunk)
            {
                response.write(chunk, "binary");
            });

            proxy_res.on("end", function(chunk)
            {
                response.end();
            });

            proxy_res.on("error", function(error)
            {
                if(!connectionErrorMessageDisplayed)
                {
                    console.log("Error: cannot connect to twisterd.\nSee Troubleshooting section in README.md for instructions.");
                    connectionErrorMessageDisplayed=true;
                }
                response.send(502);
            });

            response.writeHead(proxy_res.statusCode, proxy_res.headers);
        });

        rpcProxy.write(request.rawBody, "binary");
        rpcProxy.end();
    });
});

if(settings.Server.enable_https)
    https.createServer(credentials, app).listen(settings.Server.https_port).on("error", handlePortAccessError);

http.createServer(app).listen(settings.Server.http_port).on("error", handlePortAccessError);

setInterval(function()
{
    for(method in maxCallsPerMinute)
    {
        callsRemaining[method] = maxCallsPerMinute[method];
        if(droppedCallsCounter[method]!==0)
        {
            console.log("Dropped "+droppedCallsCounter[method]+" calls to "+method+" over the limit of "+maxCallsPerMinute[method]);
            droppedCallsCounter[method] = 0;
        }
    };
    if(invalidRequestCounter!==0)
    {
        console.log("Received "+invalidRequestCounter+" invalid POST requests.");
        invalidRequestCounter = 0;
    }
    if(forbiddenCallCounter!==0)
    {
        console.log("Denied "+forbiddenCallCounter+" attempts to access forbidden API calls.");
        forbiddenCallCounter = 0;
    }

    for(ip in perIPCounter)
    {
        if(perIPCounter[ip].overLimit>=settings.LogAsAttackThreshold.callsOverLimits)
        {
            console.log("IP "+ip+" tried to send "+perIPCounter[ip].overLimit+" calls more than the limits allow.");
        };
        if(perIPCounter[ip].invalidRequests>=settings.LogAsAttackThreshold.invalidRequests)
        {
            console.log("IP "+ip+" sent "+perIPCounter[ip].invalidRequests+" invalid request that couldn't be parsed.");
        };
        if(perIPCounter[ip].forbiddenCalls>=settings.LogAsAttackThreshold.forbiddenCalls)
        {
            console.log("IP "+ip+" tried to send "+perIPCounter[ip].forbiddenCalls+" calls to forbidden functions.");
        };
    }

    perIPCounter = {};
    connectionErrorMessageDisplayed = false;

}, 60000);
<|endoftext|>


<commit_before>'use strict';

/**
 * Module dependencies.
 */

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const { getToday } = require('../utils');

/**
 * Tide Schema
 */

const TideSchema = new Schema({
    shift: Number,
    state: String,
    timestamp: Number
});

/**
 * Port Schema
 */

const PortSchema = new Schema({
    name: String,
    lat: Number,
    lon: Number,
    distance: Number,
    unit: String
})

/**
 * Condition Schema
 */

const ConditionSchema = new Schema({
    _id: Number,
    _spot: { type: Number, ref: 'Spot' },
    tide: [TideSchema],
    sunriseTwilight: Number,
    sunrise: Number,
    sunsetTwilight: Number,
    sunset: Number,
    meta: {
        timestamp: Number,
        mswd: {
            images: {
                full: String
            }
        },
        unit: String
    },
    port: PortSchema,
    createdAt: { type : Date, default : Date.now }
});

/**
 * Statics
 */

ConditionSchema.statics = {
    get: function (spotId) {
        var dt = getToday() / 1000,
            fields = ['tide'];

        return this.find({_spot: spotId, 'meta.timestamp': {$gt: dt}})
            .select(fields)
            .exec()
            .then(function (data) {
                return data || null
            });
    }
};

mongoose.model('Condition', ConditionSchema);
<commit_msg>condition<commit_after>'use strict';

/**
 * Module dependencies.
 */

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const { getToday } = require('../utils');

/**
 * Tide Schema
 */

const TideSchema = new Schema({
    shift: Number,
    state: String,
    timestamp: Number
});

/**
 * Port Schema
 */

const PortSchema = new Schema({
    name: String,
    lat: Number,
    lon: Number,
    distance: Number,
    unit: String
})

/**
 * Condition Schema
 */

const ConditionSchema = new Schema({
    _id: Number,
    _spot: { type: Number, ref: 'Spot' },
    tide: [TideSchema],
    sunriseTwilight: Number,
    sunrise: Number,
    sunsetTwilight: Number,
    sunset: Number,
    meta: {
        timestamp: Number,
        mswd: {
            images: {
                full: String
            }
        },
        unit: String
    },
    port: PortSchema,
    createdAt: { type : Date, default : Date.now }
});

/**
 * Statics
 */

ConditionSchema.statics = {
    get: function (spotId) {
        var dt = getToday() / 1000,
            fields = ['tide'];

        return this.find({_spot: spotId, 'meta.timestamp': {$gt: dt}})
            .select(fields)
            .exec()
            .then(function (data) {
                if (data.length) {
                    return data;
                }
                return null;
            });
    }
};

mongoose.model('Condition', ConditionSchema);
<|endoftext|>


<commit_before>var handleWebsocket = require('./https').handleWebsocket;
var hparser = require('hparser');
var Buffer = require('safe-buffer').Buffer;
var pluginMgr = require('./plugins');
var util = require('./util');
var config = require('./config');

var formatHeaders = hparser.formatHeaders;
var getRawHeaderNames = hparser.getRawHeaderNames;
var getRawHeaders = hparser.getRawHeaders;
var WS_RE = /\bwebsocket\b/i;
var PLUGIN_PATH_RE = /^\/(whistle|plugin)\.([^/?#]+)(\/)?/;

function getPluginNameByReq(req, callback) {
  if (!req) {
    return callback();
  }
  var host = req.headers.host;
  var index = host.indexOf(':');
  var port = req.isHttps ? 443 : 80;
  if (index !== -1) {
    port = host.substring(index + 1);
    host = host.substring(0, index);
  }
  var isWebUI = (port == config.port || port == config.uiport) && util.isLocalAddress(host);
  isWebUI = isWebUI || config.isWebUIHost(host);
  var pluginName;
  var isPluginUrl;
  if (!isWebUI) {
    pluginName = config.getPluginNameByHost(host);
  } else if (PLUGIN_PATH_RE.test(req.url)) {
    isPluginUrl = RegExp.$1 === 'plugin';
    pluginName = RegExp.$2;
  }
  if (!pluginName) {
    return callback();
  }
  req.url = req.url.replace(PLUGIN_PATH_RE, '$3');
  if (req.url[0] !== '/') {
    req.url = '/' + req.url;
  }
  var plugin = isPluginUrl ? pluginMgr.getPluginByName(name) : pluginMgr.getPlugin(name + ':');
  pluginMgr.loadPlugin(plugin, function(err, ports) {
    var uiPort = ports && ports.uiPort;
    if (err || !uiPort) {
      var msg = ['HTTP/1.1', err ? 500 : 404, err ? 'Internal Server Error' : 'Not Found'].join(' ');
      err = err ? '
' + err + '
' : 'Not Found';
      var length = Buffer.byteLength(err);
      err = [
        msg,
        'Content-Type: text/html; charset=utf8',
        'Content-Length: ' + length,
        '\r\n',
        err
      ].join('\r\n');
    }
    callback(err, uiPort);
  });
}

function upgradeHandler(req, socket) {
  var destroyed, resSocket;
  function destroy(err) {
    if (destroyed) {
      return;
    }
    destroyed = true;
    socket.destroy(err);
    resSocket && resSocket.destroy(err);
  }
  socket.on('error', destroy);

  var headers = req.headers;
  var getBuffer = function(method, newHeaders, path) {
    var rawData = [(method || 'GET') + ' ' + (path || req.url) + ' ' + 'HTTP/1.1'];
    newHeaders = formatHeaders(newHeaders || headers, req.rawHeaders);
    rawData.push(getRawHeaders(newHeaders));
    return Buffer.from(rawData.join('\r\n') + '\r\n\r\n');
  };
  socket.headers = headers;
  req.isHttps = socket.isHttps = !!headers[config.HTTPS_FIELD];
  // format headers
  socket.fullUrl = util.getFullUrl(socket).replace('http', 'ws');
  var isWs = WS_RE.test(headers.upgrade);
  getPluginNameByReq(isWs ? null : req, function(err, uiPort) {
    if (err) {
      return socket.write(err);
    }
    // 其它协议直接转成普通https请求，方便抓包调试
    // 如果是插件的websocket请求，直接转插件
    if (!isWs || uiPort) {
      if (!isWs) {
        delete headers.upgrade;
        delete headers['http2-settings'];
        headers.connection = 'keep-alive';
      }
      req.pause();
      util.connect({
        port: uiPort || config.port,
        localhost: '127.0.0.1'
      }, function(err, s) {
        if (err) {
          return destroy(err);
        }
        resSocket = s;
        resSocket.on('error', destroy);
        resSocket.write(getBuffer(req.method));
        req.pipe(resSocket).pipe(socket).resume();
      });
      return;
    }
  // 不能放到上面，否则转发后的协议将丢失
    delete headers[config.HTTPS_FIELD];
    socket.rawHeaderNames = getRawHeaderNames(req.rawHeaders);
    socket.url = req.url;
    var clientInfo = util.parseClientInfo(req);
    var clientIp = clientInfo[0] || util.getClientIp(req);
    var clientPort = clientInfo[1] || util.getClientPort(req);
    delete socket.headers[config.CLIENT_PORT_HEAD];
    socket.getBuffer = function(newHeaders, path) {
      return getBuffer(null, newHeaders, path);
    };
    handleWebsocket(socket, clientIp, clientPort);
  });
}

module.exports = function(server) {
  server.on('upgrade', upgradeHandler);
};
<commit_msg>feat: support for using websocket to connect plugin ui server<commit_after>var handleWebsocket = require('./https').handleWebsocket;
var hparser = require('hparser');
var Buffer = require('safe-buffer').Buffer;
var pluginMgr = require('./plugins');
var util = require('./util');
var config = require('./config');

var formatHeaders = hparser.formatHeaders;
var getRawHeaderNames = hparser.getRawHeaderNames;
var getRawHeaders = hparser.getRawHeaders;
var WS_RE = /\bwebsocket\b/i;
var PLUGIN_PATH_RE = /^\/(whistle|plugin)\.([^/?#]+)(\/)?/;

function getPluginNameByReq(req, callback) {
  if (!req) {
    return callback();
  }
  var host = req.headers.host;
  var index = host.indexOf(':');
  var port = req.isHttps ? 443 : 80;
  if (index !== -1) {
    port = host.substring(index + 1);
    host = host.substring(0, index);
  }
  var isWebUI = (port == config.port || port == config.uiport) && util.isLocalAddress(host);
  isWebUI = isWebUI || config.isWebUIHost(host);
  var pluginName;
  var isPluginUrl;
  if (!isWebUI) {
    pluginName = config.getPluginNameByHost(host);
  } else if (PLUGIN_PATH_RE.test(req.url)) {
    isPluginUrl = RegExp.$1 === 'plugin';
    pluginName = RegExp.$2;
  }
  if (!pluginName) {
    return callback();
  }
  req.url = req.url.replace(PLUGIN_PATH_RE, '$3');
  if (req.url[0] !== '/') {
    req.url = '/' + req.url;
  }
  var plugin = isPluginUrl ? pluginMgr.getPluginByName(name) : pluginMgr.getPlugin(name + ':');
  pluginMgr.loadPlugin(plugin, function(err, ports) {
    var uiPort = ports && ports.uiPort;
    if (err || !uiPort) {
      var msg = ['HTTP/1.1', err ? 500 : 404, err ? 'Internal Server Error' : 'Not Found'].join(' ');
      err = err ? '
' + err + '
' : 'Not Found';
      var length = Buffer.byteLength(err);
      err = [
        msg,
        'Content-Type: text/html; charset=utf8',
        'Content-Length: ' + length,
        '\r\n',
        err
      ].join('\r\n');
    }
    callback(err, uiPort);
  });
}

function upgradeHandler(req, socket) {
  var destroyed, resSocket;
  function destroy(err) {
    if (destroyed) {
      return;
    }
    destroyed = true;
    socket.destroy(err);
    resSocket && resSocket.destroy(err);
  }
  socket.on('error', destroy);

  var headers = req.headers;
  var getBuffer = function(method, newHeaders, path) {
    var rawData = [(method || 'GET') + ' ' + (path || req.url) + ' ' + 'HTTP/1.1'];
    newHeaders = formatHeaders(newHeaders || headers, req.rawHeaders);
    rawData.push(getRawHeaders(newHeaders));
    return Buffer.from(rawData.join('\r\n') + '\r\n\r\n');
  };
  socket.headers = headers;
  req.isHttps = socket.isHttps = !!headers[config.HTTPS_FIELD];
  // format headers
  socket.fullUrl = util.getFullUrl(socket).replace('http', 'ws');
  var isWs = WS_RE.test(headers.upgrade);
  getPluginNameByReq(isWs ? req : null, function(err, uiPort) {
    if (err) {
      return socket.write(err);
    }
    // 其它协议直接转成普通https请求，方便抓包调试
    // 如果是插件的websocket请求，直接转插件
    if (!isWs || uiPort) {
      if (!isWs) {
        delete headers.upgrade;
        delete headers['http2-settings'];
        headers.connection = 'keep-alive';
      }
      req.pause();
      util.connect({
        port: uiPort || config.port,
        localhost: '127.0.0.1'
      }, function(err, s) {
        if (err) {
          return destroy(err);
        }
        resSocket = s;
        resSocket.on('error', destroy);
        resSocket.write(getBuffer(req.method));
        req.pipe(resSocket).pipe(socket).resume();
      });
      return;
    }
  // 不能放到上面，否则转发后的协议将丢失
    delete headers[config.HTTPS_FIELD];
    socket.rawHeaderNames = getRawHeaderNames(req.rawHeaders);
    socket.url = req.url;
    var clientInfo = util.parseClientInfo(req);
    var clientIp = clientInfo[0] || util.getClientIp(req);
    var clientPort = clientInfo[1] || util.getClientPort(req);
    delete socket.headers[config.CLIENT_PORT_HEAD];
    socket.getBuffer = function(newHeaders, path) {
      return getBuffer(null, newHeaders, path);
    };
    handleWebsocket(socket, clientIp, clientPort);
  });
}

module.exports = function(server) {
  server.on('upgrade', upgradeHandler);
};
<|endoftext|>


<commit_before>db.triangles.createIndex( { valid_date : 1, valid_time : 1, parameter_name : 1, triangle : "2dsphere" } )
db.triangles.createIndex( { valid_date : 1, valid_time : 1, parameter_name : 1  } )
db.hourlydata.createIndex( { measurement_key: 1, valid_date : 1, valid_time : 1 } )
db.hourlydata.createIndex( { valid_date : 1, valid_time : 1, parameter_name : 1 } )

<commit_msg>Update mongo_setup.js<commit_after>db.triangles.createIndex( { valid_date : 1, valid_time : 1, parameter_name : 1, triangle : "2dsphere" } );
db.triangles.createIndex( { valid_date : 1, valid_time : 1, parameter_name : 1  } );
db.hourlydata.createIndex( { measurement_key: 1, valid_date : 1, valid_time : 1 } );
db.hourlydata.createIndex( { valid_date : 1, valid_time : 1, parameter_name : 1 } );
<|endoftext|>


<commit_before>// Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;

// Schema
var jobShema = mongoose.Schema({
	name : String,
	date : String
})

// Model
module.exports = restful.model('Jobs', jobShema);
<commit_msg>modify rest api model job<commit_after>// Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;

// Schema
var jobShema = mongoose.Schema({
	libelle : String,
	company : String,
	contractType : String,
	salary : String,
	level : String,
	city : String,
	author: String,
	date : String,
})

// Model
module.exports = restful.model('Jobs', jobShema);
<|endoftext|>


<commit_before>"use strict";

const http = require("http"),
	path = require("path"),
	coerce = require("tiny-coerce"),
	regex = require(path.join(__dirname, "regex.js")),
	{URL} = require("url");

function clone (arg) {
	return JSON.parse(JSON.stringify(arg));
}

function each (arr, fn) {
	const nth = arr.length;
	let i = -1;

	while (++i < nth) {
		fn(arr[i], i);
	}

	return arr;
}

function http2Normalize (arg) {
	const obj = clone(arg);

	obj.headers.host = obj.headers[":authority"].replace(/:.*$/, "");
	obj.method = obj.headers[":method"];
	obj.connection = {remoteAddress: "0.0.0.0"};
	obj.url = obj.headers[":path"]; //`${obj.headers[":scheme"]}//${obj.headers[":authority"]}${obj.headers[":path"]}`;
	obj.httpVersion = "2.0";
	obj.httpVersionMajor = 2;
	obj.httpVersionMinor = 0;

	delete obj.headers[":authority"];
	delete obj.headers[":method"];
	delete obj.headers[":path"];
	delete obj.headers[":scheme"];

	return obj;
}

function http2Send (req, res, body, nstatus = 200, headers = null) {
	const status = res.statusCode < nstatus ? nstatus : res.statusCode,
		output = {":status": status},
		empty = regex.isHead.test(req.method) || status === 204 || status === 304 || body === "";

	each(Object.keys(res._headers).filter(i => regex.invalid.test(i) === false), i => {
		output[i] = res._headers[i];
	});

	if (headers !== null) {
		each(Object.keys(headers).filter(i => regex.invalid.test(i) === false), i => {
			output[i] = headers[i];
			res._headers[i] = headers[i];
		});
	}

	if (req.file === void 0 || empty) {
		res.respond(output);

		if (res.writable) {
			res.end(empty === false ? body : void 0);
		}
	} else {
		res.respondWithFile(req.file.path, output);
	}
}

function normalize (...args) {
	let [route, fn, method] = args;

	if (typeof route !== "string") {
		method = fn;
		fn = route;
		route = "/.*";
	}

	method = method !== void 0 ? method.toUpperCase() : "GET";

	return [route, fn, method];
}

function last (req, res, reject, err) {
	const status = res.statusCode || 0;

	if (err === void 0) {
		reject(new Error(regex.hasGet.test(req.allow || "") ? 405 : 404));
	} else if (isNaN(status) === false && status >= 400) {
		reject(err);
	} else {
		reject(new Error(status >= 400 ? status : isNaN(err.message) === false ? err.message : http.STATUS_CODES[err.message || err] || 500));
	}
}

function next (req, res, reject, middleware) {
	const fn = err => process.nextTick(() => {
		let obj = middleware.next();

		if (obj.done === false) {
			if (err !== void 0) {
				while (obj.done === false && obj.value.length < 4) {
					obj = middleware.next();
				}

				if (obj.done === false) {
					obj.value(err, req, res, fn);
				} else {
					last(req, res, reject, err);
				}
			} else {
				obj.value(req, res, fn);
			}
		} else {
			last(req, res, reject, err);
		}
	});

	return fn;
}

function params (req, pos = []) {
	if (pos.length > 0) {
		const uri = req.parsed.pathname.replace(regex.slashEnd, "").split("/");

		each(pos, i => {
			req.params[i[1]] = coerce(uri[i[0]]);
		});
	}
}

function parse (arg) {
	return new URL(typeof arg === "string" ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
}

function partial (req, res, buffered, status, headers) {
	if (regex.partial.test(req.headers.range) === true) {
		const options = {},
			size = Buffer.byteLength(buffered);

		each(req.headers.range.replace(regex.partial, "").split(",")[0].split("-"), (i, idx) => {
			options[idx === 0 ? "start" : "end"] = i ? parseInt(i, 10) : void 0;
		});

		// Byte offsets
		if (isNaN(options.start) === true && isNaN(options.end) === false) {
			options.start = size - options.end;
			options.end = size;
		} else if (isNaN(options.end) === true) {
			options.end = size;
		}

		if ((options.start >= options.end || isNaN(options.start) || isNaN(options.end)) === false) {
			req.range = options;
			headers["content-range"] = `bytes ${options.start}-${options.end}/${size}`;
			headers["content-length"] = `${options.end - options.start + 1}`;
			status = res.statusCode = 206;
			res.removeHeader("etag"); // Removing etag since this rep is incomplete
			delete headers.etag;
		}
	}
}

function pipeable (method, arg) {
	return regex.isHead.test(method) === false && arg !== null && typeof arg.on === "function";
}

function reduce (uri, map, arg) {
	if (map !== void 0) {
		each(Array.from(map.keys()).filter(route => {
			let now = false,
				valid;

			if (regex.hasParam.test(route) === true && regex.braceLeft.test(route) === false && arg.params === false) {
				arg.params = true;
				now = true;

				each(route.replace(regex.slashEnd, "").replace(regex.slashEnd, "").split("/"), (i, idx) => {
					if (regex.isParam.test(i) === true) {
						arg.pos.push([idx, i.replace(regex.isParam, "")]);
					}
				});

				route = route.replace(/\/:(\w*)/g, "/(.*)");
			}

			valid = new RegExp("^" + route + "$", "i").test(uri);

			if (now === true && valid === false) {
				arg.params = false;
				arg.pos = [];
			}

			return valid;
		}), route => each(map.get(route), i => arg.middleware.push(i)));
	}
}

function schemeless (arg) {
	return arg.replace(/^.*:\/\//, "").replace(/\/$/, "");
}

function wrapped (arg) {
	return arg[0] === '"' && arg[arg.length - 1] === '"';
}

function writeHead (res, status, headers) {
	if (res.statusCode < status) {
		res.statusCode = status;
	}

	res.writeHead(res.statusCode, headers);
}

module.exports = {
	clone,
	each,
	http2Normalize,
	http2Send,
	normalize,
	last,
	next,
	params,
	parse,
	partial,
	pipeable,
	reduce,
	schemeless,
	wrapped,
	writeHead
};
<commit_msg>Keeping `utility.js` alphabetically ordered<commit_after>"use strict";

const http = require("http"),
	path = require("path"),
	coerce = require("tiny-coerce"),
	regex = require(path.join(__dirname, "regex.js")),
	{URL} = require("url");

function clone (arg) {
	return JSON.parse(JSON.stringify(arg));
}

function each (arr, fn) {
	const nth = arr.length;
	let i = -1;

	while (++i < nth) {
		fn(arr[i], i);
	}

	return arr;
}

function http2Normalize (arg) {
	const obj = clone(arg);

	obj.headers.host = obj.headers[":authority"].replace(/:.*$/, "");
	obj.method = obj.headers[":method"];
	obj.connection = {remoteAddress: "0.0.0.0"};
	obj.url = obj.headers[":path"]; //`${obj.headers[":scheme"]}//${obj.headers[":authority"]}${obj.headers[":path"]}`;
	obj.httpVersion = "2.0";
	obj.httpVersionMajor = 2;
	obj.httpVersionMinor = 0;

	delete obj.headers[":authority"];
	delete obj.headers[":method"];
	delete obj.headers[":path"];
	delete obj.headers[":scheme"];

	return obj;
}

function http2Send (req, res, body, nstatus = 200, headers = null) {
	const status = res.statusCode < nstatus ? nstatus : res.statusCode,
		output = {":status": status},
		empty = regex.isHead.test(req.method) || status === 204 || status === 304 || body === "";

	each(Object.keys(res._headers).filter(i => regex.invalid.test(i) === false), i => {
		output[i] = res._headers[i];
	});

	if (headers !== null) {
		each(Object.keys(headers).filter(i => regex.invalid.test(i) === false), i => {
			output[i] = headers[i];
			res._headers[i] = headers[i];
		});
	}

	if (req.file === void 0 || empty) {
		res.respond(output);

		if (res.writable) {
			res.end(empty === false ? body : void 0);
		}
	} else {
		res.respondWithFile(req.file.path, output);
	}
}

function last (req, res, reject, err) {
	const status = res.statusCode || 0;

	if (err === void 0) {
		reject(new Error(regex.hasGet.test(req.allow || "") ? 405 : 404));
	} else if (isNaN(status) === false && status >= 400) {
		reject(err);
	} else {
		reject(new Error(status >= 400 ? status : isNaN(err.message) === false ? err.message : http.STATUS_CODES[err.message || err] || 500));
	}
}

function next (req, res, reject, middleware) {
	const fn = err => process.nextTick(() => {
		let obj = middleware.next();

		if (obj.done === false) {
			if (err !== void 0) {
				while (obj.done === false && obj.value.length < 4) {
					obj = middleware.next();
				}

				if (obj.done === false) {
					obj.value(err, req, res, fn);
				} else {
					last(req, res, reject, err);
				}
			} else {
				obj.value(req, res, fn);
			}
		} else {
			last(req, res, reject, err);
		}
	});

	return fn;
}

function normalize (...args) {
	let [route, fn, method] = args;

	if (typeof route !== "string") {
		method = fn;
		fn = route;
		route = "/.*";
	}

	method = method !== void 0 ? method.toUpperCase() : "GET";

	return [route, fn, method];
}

function params (req, pos = []) {
	if (pos.length > 0) {
		const uri = req.parsed.pathname.replace(regex.slashEnd, "").split("/");

		each(pos, i => {
			req.params[i[1]] = coerce(uri[i[0]]);
		});
	}
}

function parse (arg) {
	return new URL(typeof arg === "string" ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
}

function partial (req, res, buffered, status, headers) {
	if (regex.partial.test(req.headers.range) === true) {
		const options = {},
			size = Buffer.byteLength(buffered);

		each(req.headers.range.replace(regex.partial, "").split(",")[0].split("-"), (i, idx) => {
			options[idx === 0 ? "start" : "end"] = i ? parseInt(i, 10) : void 0;
		});

		// Byte offsets
		if (isNaN(options.start) === true && isNaN(options.end) === false) {
			options.start = size - options.end;
			options.end = size;
		} else if (isNaN(options.end) === true) {
			options.end = size;
		}

		if ((options.start >= options.end || isNaN(options.start) || isNaN(options.end)) === false) {
			req.range = options;
			headers["content-range"] = `bytes ${options.start}-${options.end}/${size}`;
			headers["content-length"] = `${options.end - options.start + 1}`;
			status = res.statusCode = 206;
			res.removeHeader("etag"); // Removing etag since this rep is incomplete
			delete headers.etag;
		}
	}
}

function pipeable (method, arg) {
	return regex.isHead.test(method) === false && arg !== null && typeof arg.on === "function";
}

function reduce (uri, map, arg) {
	if (map !== void 0) {
		each(Array.from(map.keys()).filter(route => {
			let now = false,
				valid;

			if (regex.hasParam.test(route) === true && regex.braceLeft.test(route) === false && arg.params === false) {
				arg.params = true;
				now = true;

				each(route.replace(regex.slashEnd, "").replace(regex.slashEnd, "").split("/"), (i, idx) => {
					if (regex.isParam.test(i) === true) {
						arg.pos.push([idx, i.replace(regex.isParam, "")]);
					}
				});

				route = route.replace(/\/:(\w*)/g, "/(.*)");
			}

			valid = new RegExp("^" + route + "$", "i").test(uri);

			if (now === true && valid === false) {
				arg.params = false;
				arg.pos = [];
			}

			return valid;
		}), route => each(map.get(route), i => arg.middleware.push(i)));
	}
}

function schemeless (arg) {
	return arg.replace(/^.*:\/\//, "").replace(/\/$/, "");
}

function wrapped (arg) {
	return arg[0] === '"' && arg[arg.length - 1] === '"';
}

function writeHead (res, status, headers) {
	if (res.statusCode < status) {
		res.statusCode = status;
	}

	res.writeHead(res.statusCode, headers);
}

module.exports = {
	clone,
	each,
	http2Normalize,
	http2Send,
	last,
	next,
	normalize,
	params,
	parse,
	partial,
	pipeable,
	reduce,
	schemeless,
	wrapped,
	writeHead
};
<|endoftext|>


<commit_before>"use strict"

var xdTesting = require('../lib/index');
var q = require('q');

function debug() {

}

var utility = {

    waitforTimeout: 30 * 1000,

    initWithDevices: function (devices) {

        var self = this;

        // Store id into device
        Object.keys(devices).forEach(function (id) {
            var device = devices[id];
            debug(id, device);
            device['id'] = id;
            device['waitforTimeout'] = utility.waitforTimeout;
            debug(id, device);
        });

        debug(devices);

        self.deviceOptions = devices;

        // New browser instance with WebdriverIO
        self.devices = xdTesting.multiremote(self.deviceOptions);

        var tileWidth = Math.floor(1600 / self.devicesCount());

        return self.devices.init()
            .timeoutsAsyncScript(15 * 1000)
            .then(function () {
                // Align windows on screen
                var x = 0;
                Object.keys(self.deviceOptions).forEach(deviceName => {
                    var device = self.devices.select(deviceName);
                    var width = self.deviceOptions[deviceName].width || tileWidth;
                    var height = self.deviceOptions[deviceName].height || 600;

                    // Set window sizes
                    device.windowHandleSize({width: width, height: height})

                    // Align window on screen
                    device.windowHandlePosition({x: x, y: 0});
                    x += tileWidth;
                });
            });
    },

    /**
     * @callback multiActionCallback
     * @param {WebdriverIO.Client} device
     */

    /**
     * Executes callback on devices matching deviceNames.
     * Returns a promise.
     * @param devices
     * @param deviceIds
     * @param {multiActionCallback} callback
     * @returns Q.Promise<T[]>
     */
    multiAction: function multiAction (devices, deviceIds, callback) {
        var promises = [];
        debug(deviceIds);
        deviceIds.forEach(function(id) {
            var device = devices.select(id);
            debug('multiAction ' + id + ' ' + device.options.id, device.options);

            promises.push(callback(device));
        });
        return q.all(promises);
    }

};

module.exports = utility;
<commit_msg>Cleanup code<commit_after>"use strict"

var xdTesting = require('../lib/index');
var q = require('q');

var utility = {

    waitforTimeout: 30 * 1000,

    initWithDevices: function (devices) {

        var self = this;

        // Store id into device
        Object.keys(devices).forEach(function (id) {
            var device = devices[id];
            device['id'] = id;
            device['waitforTimeout'] = utility.waitforTimeout;
        });

        self.deviceOptions = devices;

        // New browser instance with WebdriverIO
        self.devices = xdTesting.multiremote(self.deviceOptions);

        var tileWidth = Math.floor(1600 / self.devicesCount());

        return self.devices.init()
            .timeoutsAsyncScript(15 * 1000)
            .then(function () {
                // Align windows on screen
                var x = 0;
                Object.keys(self.deviceOptions).forEach(deviceName => {
                    var device = self.devices.select(deviceName);
                    var width = self.deviceOptions[deviceName].width || tileWidth;
                    var height = self.deviceOptions[deviceName].height || 600;

                    // Set window sizes
                    device.windowHandleSize({width: width, height: height})

                    // Align window on screen
                    device.windowHandlePosition({x: x, y: 0});
                    x += tileWidth;
                });
            });
    },

    /**
     * @callback multiActionCallback
     * @param {WebdriverIO.Client} device
     */

    /**
     * Executes callback on devices matching deviceNames.
     * Returns a promise.
     * @param devices
     * @param deviceIds
     * @param {multiActionCallback} callback
     * @returns Q.Promise<T[]>
     */
    multiAction: function multiAction (devices, deviceIds, callback) {
        var promises = [];
        debug(deviceIds);
        deviceIds.forEach(function(id) {
            var device = devices.select(id);
            debug('multiAction ' + id + ' ' + device.options.id, device.options);

            promises.push(callback(device));
        });
        return q.all(promises);
    }

};

module.exports = utility;
<|endoftext|>


<commit_before>import { equal } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Resource from '@rancher/ember-api-store/models/resource';
import C from 'ui/utils/constants';
import { get, computed } from '@ember/object'
import Identicon from 'identicon.js';

var Principal = Resource.extend({
  intl: service(),

  isUser: equal('parsedExternalType', C.PROJECT.TYPE_USER),
  isTeam: equal('parsedExternalType', C.PROJECT.TYPE_TEAM),
  isOrg:  equal('parsedExternalType', C.PROJECT.TYPE_ORG),

  parsedExternalType: computed('id', function() {
    return get(this, 'id').split(':')
      .get('firstObject');
  }),

  avatarSrc: computed('isGithub', 'id', 'profilePicture', function() {
    if ( get(this, 'isGithub') && get(this, 'profilePicture') ) {
      return get(this, 'profilePicture');
    } else {
      let id = get(this, 'id') || 'Unknown';

      id = id.replace('local://', '');

      return `data:image/png;base64,${ new Identicon(AWS.util.crypto.md5(id, 'hex'), 80, 0.01).toString() }`;
    }
  }),

  isGithub: computed('parsedExternalType', function() {
    // console.log('is github?', get(this, 'provider'));
    return (get(this, 'provider') || '').toLowerCase() === 'github';
  }),

  logicalType: computed('parsedExternalType', function() {
    switch ( get(this, 'parsedExternalType') ) {
    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_USER:
    case C.PROJECT.TYPE_ADFS_USER:
    case C.PROJECT.TYPE_AZURE_USER:
    case C.PROJECT.TYPE_FREEIPA_USER:
    case C.PROJECT.TYPE_GITHUB_USER:
    case C.PROJECT.TYPE_GOOGLE_USER:
    case C.PROJECT.TYPE_KEYCLOAK_USER:
    case C.PROJECT.TYPE_LDAP_USER:
    case C.PROJECT.TYPE_OPENLDAP_USER:
    case C.PROJECT.TYPE_PING_USER:
    case C.PROJECT.TYPE_RANCHER:
    case C.PROJECT.TYPE_SHIBBOLETH_USER:
      return C.PROJECT.PERSON;

    case C.PROJECT.TYPE_GITHUB_TEAM:
      return C.PROJECT.TEAM;

    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_GROUP:
    case C.PROJECT.TYPE_ADFS_GROUP:
    case C.PROJECT.TYPE_AZURE_GROUP:
    case C.PROJECT.TYPE_FREEIPA_GROUP:
    case C.PROJECT.TYPE_GITHUB_ORG:
    case C.PROJECT.TYPE_KEYCLOAK_GROUP:
    case C.PROJECT.TYPE_LDAP_GROUP:
    case C.PROJECT.TYPE_OPENLDAP_GROUP:
    case C.PROJECT.TYPE_PING_GROUP:
    case C.PROJECT.TYPE_SHIBBOLETH_GROUP:
    case C.PROJECT.TYPE_GOOGLE_GROUP:
      return C.PROJECT.ORG;
    }
  }),

  logicalTypeSort: computed('logicalType', function() {
    switch (get(this, 'logicalType') ) {
    case C.PROJECT.ORG: return 1;
    case C.PROJECT.TEAM: return 2;
    case C.PROJECT.PERSON: return 3;
    default: return 4;
    }
  }),

  displayType: computed('parsedExternalType', 'intl.locale', function() {
    let key = 'model.identity.displayType.unknown';
    let type = get(this, 'parsedExternalType');

    switch ( type ) {
    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_USER:
    case C.PROJECT.TYPE_ADFS_USER:
    case C.PROJECT.TYPE_OKTA_USER:
    case C.PROJECT.TYPE_AZURE_USER:
    case C.PROJECT.TYPE_FREEIPA_USER:
    case C.PROJECT.TYPE_GITHUB_USER:
    case C.PROJECT.TYPE_GOOGLE_USER:
    case C.PROJECT.TYPE_KEYCLOAK_USER:
    case C.PROJECT.TYPE_LDAP_USER:
    case C.PROJECT.TYPE_OPENLDAP_USER:
    case C.PROJECT.TYPE_PING_USER:
    case C.PROJECT.TYPE_SHIBBOLETH_USER:
      key = 'model.identity.displayType.user';
      break;

    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_GROUP:
    case C.PROJECT.TYPE_ADFS_GROUP:
    case C.PROJECT.TYPE_OKTA_GROUP:
    case C.PROJECT.TYPE_AZURE_GROUP:
    case C.PROJECT.TYPE_FREEIPA_GROUP:
    case C.PROJECT.TYPE_KEYCLOAK_GROUP:
    case C.PROJECT.TYPE_LDAP_GROUP:
    case C.PROJECT.TYPE_OPENLDAP_GROUP:
    case C.PROJECT.TYPE_PING_GROUP:
    case C.PROJECT.TYPE_SHIBBOLETH_GROUP:
    case C.PROJECT.TYPE_GOOGLE_GROUP:
      key = 'model.identity.displayType.group';
      break;

    case C.PROJECT.TYPE_GITHUB_TEAM:
      key = 'model.identity.displayType.team';
      break;

    case C.PROJECT.TYPE_GITHUB_ORG:
      key = 'model.identity.displayType.org';
      break;

    case C.PROJECT.TYPE_RANCHER:
      key = 'model.identity.displayType.localUser';
      break;
    }

    return get(this, 'intl').t(key, { type });
  }),
});

Principal.reopenClass({
  mangleIn(data/* , store */) {
    if ( data.displayName ) {
      // set to name then delete
      data.name = data.displayName;
      delete data.displayName;
    }

    return data;
  },
});

export default Principal;
<commit_msg>Bugfix #21435<commit_after>import { equal } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Resource from '@rancher/ember-api-store/models/resource';
import C from 'ui/utils/constants';
import { get, computed } from '@ember/object'
import Identicon from 'identicon.js';

var Principal = Resource.extend({
  intl: service(),

  isUser: equal('parsedExternalType', C.PROJECT.TYPE_USER),
  isTeam: equal('parsedExternalType', C.PROJECT.TYPE_TEAM),
  isOrg:  equal('parsedExternalType', C.PROJECT.TYPE_ORG),

  parsedExternalType: computed('id', function() {
    return get(this, 'id').split(':')
      .get('firstObject');
  }),

  avatarSrc: computed('isGithub', 'isGoogleOauth', 'id', 'profilePicture', function() {
    if ( (get(this, 'isGithub') && get(this, 'profilePicture')) || (get(this, 'isGoogleOauth') && get(this, 'profilePicture')) ) {
      return get(this, 'profilePicture');
    } else {
      let id = get(this, 'id') || 'Unknown';

      id = id.replace('local://', '');

      return `data:image/png;base64,${ new Identicon(AWS.util.crypto.md5(id, 'hex'), 80, 0.01).toString() }`;
    }
  }),

  isGithub: computed('parsedExternalType', function() {
    // console.log('is github?', get(this, 'provider'));
    return (get(this, 'provider') || '').toLowerCase() === 'github';
  }),

  isGoogleOauth: computed('parsedExternalType', function() {
    return (get(this, 'provider') || '').toLowerCase() === 'googleoauth';
  }),

  logicalType: computed('parsedExternalType', function() {
    switch ( get(this, 'parsedExternalType') ) {
    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_USER:
    case C.PROJECT.TYPE_ADFS_USER:
    case C.PROJECT.TYPE_AZURE_USER:
    case C.PROJECT.TYPE_FREEIPA_USER:
    case C.PROJECT.TYPE_GITHUB_USER:
    case C.PROJECT.TYPE_GOOGLE_USER:
    case C.PROJECT.TYPE_KEYCLOAK_USER:
    case C.PROJECT.TYPE_LDAP_USER:
    case C.PROJECT.TYPE_OPENLDAP_USER:
    case C.PROJECT.TYPE_PING_USER:
    case C.PROJECT.TYPE_RANCHER:
    case C.PROJECT.TYPE_SHIBBOLETH_USER:
      return C.PROJECT.PERSON;

    case C.PROJECT.TYPE_GITHUB_TEAM:
      return C.PROJECT.TEAM;

    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_GROUP:
    case C.PROJECT.TYPE_ADFS_GROUP:
    case C.PROJECT.TYPE_AZURE_GROUP:
    case C.PROJECT.TYPE_FREEIPA_GROUP:
    case C.PROJECT.TYPE_GITHUB_ORG:
    case C.PROJECT.TYPE_KEYCLOAK_GROUP:
    case C.PROJECT.TYPE_LDAP_GROUP:
    case C.PROJECT.TYPE_OPENLDAP_GROUP:
    case C.PROJECT.TYPE_PING_GROUP:
    case C.PROJECT.TYPE_SHIBBOLETH_GROUP:
    case C.PROJECT.TYPE_GOOGLE_GROUP:
      return C.PROJECT.ORG;
    }
  }),

  logicalTypeSort: computed('logicalType', function() {
    switch (get(this, 'logicalType') ) {
    case C.PROJECT.ORG: return 1;
    case C.PROJECT.TEAM: return 2;
    case C.PROJECT.PERSON: return 3;
    default: return 4;
    }
  }),

  displayType: computed('parsedExternalType', 'intl.locale', function() {
    let key = 'model.identity.displayType.unknown';
    let type = get(this, 'parsedExternalType');

    switch ( type ) {
    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_USER:
    case C.PROJECT.TYPE_ADFS_USER:
    case C.PROJECT.TYPE_OKTA_USER:
    case C.PROJECT.TYPE_AZURE_USER:
    case C.PROJECT.TYPE_FREEIPA_USER:
    case C.PROJECT.TYPE_GITHUB_USER:
    case C.PROJECT.TYPE_GOOGLE_USER:
    case C.PROJECT.TYPE_KEYCLOAK_USER:
    case C.PROJECT.TYPE_LDAP_USER:
    case C.PROJECT.TYPE_OPENLDAP_USER:
    case C.PROJECT.TYPE_PING_USER:
    case C.PROJECT.TYPE_SHIBBOLETH_USER:
      key = 'model.identity.displayType.user';
      break;

    case C.PROJECT.TYPE_ACTIVE_DIRECTORY_GROUP:
    case C.PROJECT.TYPE_ADFS_GROUP:
    case C.PROJECT.TYPE_OKTA_GROUP:
    case C.PROJECT.TYPE_AZURE_GROUP:
    case C.PROJECT.TYPE_FREEIPA_GROUP:
    case C.PROJECT.TYPE_KEYCLOAK_GROUP:
    case C.PROJECT.TYPE_LDAP_GROUP:
    case C.PROJECT.TYPE_OPENLDAP_GROUP:
    case C.PROJECT.TYPE_PING_GROUP:
    case C.PROJECT.TYPE_SHIBBOLETH_GROUP:
    case C.PROJECT.TYPE_GOOGLE_GROUP:
      key = 'model.identity.displayType.group';
      break;

    case C.PROJECT.TYPE_GITHUB_TEAM:
      key = 'model.identity.displayType.team';
      break;

    case C.PROJECT.TYPE_GITHUB_ORG:
      key = 'model.identity.displayType.org';
      break;

    case C.PROJECT.TYPE_RANCHER:
      key = 'model.identity.displayType.localUser';
      break;
    }

    return get(this, 'intl').t(key, { type });
  }),
});

Principal.reopenClass({
  mangleIn(data/* , store */) {
    if ( data.displayName ) {
      // set to name then delete
      data.name = data.displayName;
      delete data.displayName;
    }

    return data;
  },
});

export default Principal;
<|endoftext|>


<commit_before>var fs                  = require('fs'),
    readdirp            = require('readdirp'),
    _                   = require('underscore'),
    lastChange          = Math.floor((new Date).getTime()),
    watchedFiles        = {},
    watchedDirectories  = {},
    checkDirPause       = 2000; //this is due to the unix filesystem checking every 2s -- via wiki

/**
*
* Watches the directory passed and its contained files
*
**/
function watchDirectories(directories, cb, partial) {
  directories.forEach(function(dir) {
    readdirp({ root: dir, directoryFilter: '!components' }, function(err, res) {
      res.files.forEach(function(file) {
        watchFile(file, cb);
      });
    });
  });
  !partial && setInterval(function(){checkDirectories(cb)}, checkDirPause);
}

/**
*
* Checks to see if something in the directory has changed
*
**/
function checkDirectories(cb) {
  _.each(watchedDirectories, function(lastModified, path) {
    fs.stat(path, function(err, stats) {
      var statsStamp = (new Date(stats.mtime)).getTime();
      if (statsStamp != lastModified) {
        watchedDirectories[path] = statsStamp;
        watchDirectories([path], cb, true);
      }
    });
  });
}

/**
*
* Watches the file passed and its containing directory
*
**/
function watchFile(file, cb) {
  watchDirectory(file);
  !watchedFiles[file.fullPath] && fs.watch(file.fullPath, function(e, filename) {
    watchedFiles[file.fullPath] = true;
    checkLastChange(cb);
  });
}

/**
*
* Sets up a store of the folders being watched
* and saves the last modification timestamp for it
*
**/
function watchDirectory(file) {
  var directory = file.fullParentDir;
  if (!watchedDirectories[directory]) {
    fs.stat(directory, function(err, stats) {
      watchedDirectories[directory] = (new Date(stats.mtime)).getTime();; //saves a ref to the last modification time
    });
  }
}

/**
*
* Fix for duplicate file change calls
* http://stackoverflow.com/questions/10468504/why-fs-watchfile-called-twice-in-node
*
**/
function checkLastChange(cb) {
  var minTime = 500; // in miliseconds
  var currentTime = Math.floor((new Date).getTime());
  if (currentTime - lastChange > minTime) {
    lastChange = currentTime;
    cb();
  }
}

exports.watchDirectories = watchDirectories;<commit_msg>fix duplicate file reload on osx<commit_after>var fs                  = require('fs'),
    readdirp            = require('readdirp'),
    _                   = require('underscore'),
    lastChange          = Math.floor((new Date).getTime()),
    watchedFiles        = {},
    watchedDirectories  = {},
    checkDirPause       = 2000, //this is due to the unix filesystem checking every 2s -- via wiki
    isWindows           = process.platform === 'win32';
/**
*
* Watches the directory passed and its contained files
*
**/
function watchDirectories(directories, cb, partial) {
  directories.forEach(function(dir) {
    readdirp({ root: dir, directoryFilter: '!components' }, function(err, res) {
      res.files.forEach(function(file) {
        watchFile(file, cb);
      });
    });
  });
  !partial && setInterval(function(){checkDirectories(cb)}, checkDirPause);
}

/**
*
* Checks to see if something in the directory has changed
*
**/
function checkDirectories(cb) {
  _.each(watchedDirectories, function(lastModified, path) {
    fs.stat(path, function(err, stats) {
      var statsStamp = (new Date(stats.mtime)).getTime();
      if (statsStamp != lastModified) {
        watchedDirectories[path] = statsStamp;
        watchDirectories([path], cb, true);
      }
    });
  });
}

/**
*
* Watches the file passed and its containing directory
*
* borrowed from stylus -- looks life FS.watch does not work on osx :\
**/
function watchFile(file, cb) {
  watchDirectory(file);
  if (!watchedFiles[file.fullPath]) {
    watchedFiles[file.fullPath] = true;
    if (isWindows) {
      fs.watch(file.fullPath, {interval: 300}, function() {checkLastChange(cb);});
    } else {
      fs.watchFile(file.fullPath, {interval: 300}, function() {checkLastChange(cb);});
    }
  }

}

/**
*
* Sets up a store of the folders being watched
* and saves the last modification timestamp for it
*
**/
function watchDirectory(file) {
  var directory = file.fullParentDir;
  if (!watchedDirectories[directory]) {
    fs.stat(directory, function(err, stats) {
      watchedDirectories[directory] = (new Date(stats.mtime)).getTime();; //saves a ref to the last modification time
    });
  }
}

/**
*
* Fix for duplicate file change calls
* http://stackoverflow.com/questions/10468504/why-fs-watchfile-called-twice-in-node
*
**/
function checkLastChange(cb) {
  var minTime = 500; // in miliseconds
  var currentTime = Math.floor((new Date).getTime());
  console.log(currentTime - lastChange);
  if (currentTime - lastChange > minTime) {
    lastChange = currentTime;
    cb();
  }
}

exports.watchDirectories = watchDirectories;<|endoftext|>


<commit_before>var chokidar = require('chokidar');
var http = require('http');
var fs = require('graceful-fs-extra')
var winston = require('winston');
var path = require('path');

var utils = require('./utils');

module.exports = {
  start: function(server_address, server_port, sharedDir, max_retries, logLevel) {

    winston.level = logLevel;
    max_retries = max_retries || 1;

    // We don't really need filesystem events.
    // We just need to state be able to produce the same state as on monitored direcotry.
    // Unlink doesn't really care if it is directory or file.
    // Changed doesnt really care if the file is changed or just craeted.
    var generateChange = function(file, retry) {
      var localFile = path.join(sharedDir, file);
      var sharedFile = file;
      fs.stat(localFile, function(err, stats) {
        // Just send event directly to the remote machine for missing files
        if (err && err.code === 'ENOENT') {
          sendChange('unlink', sharedFile, retry, null);
          return;
        }
        if (err) {
          winston.error('unexpected error occured ' + JSON.stringify(err));
          return;
        }

        winston.debug('stats for ' + file + ' are ' + JSON.stringify(stats));
        if(stats.isFile()) {
          utils.calcHash(localFile, function(error, sha1) {
            sendChange('copy', sharedFile, retry, sha1);
          });
        } else {
          sendChange('addDir', sharedFile, retry, null);
        }
      })
    }

    var sendChange = function(event, file, retry, checkSum) {
      winston.debug('sending ' + event + ' for ' + file + ' retried ' + retry);
      if (retry > max_retries) {
        winston.error('unabled to syncronize ' + file + ' retried ' + retry + ' times.');
        return;
      }
      var options = {
        host: server_address,
        port: server_port,
        path: '/',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        }
      };

      var req = http.request(options, function(res) {
        var data="";
        res.on('data', function (chunk) {
          data+=chunk;
        });

        res.on('end', function () {
          if (JSON.parse(data)["code"] === utils.ERROR_CODE) {
            winston.warn('error occured while syncing ' + file + ' ' + data);
            generateChange(file, retry + 1);
          } else {
            winston.debug('successful response received ' + data);
          }
        });
      });

      postData = JSON.stringify({
        event: event,
        file: file,
        checkSum: checkSum,
      });

      req.write(postData);

      req.end();
    };

    chokidar.watch(
      sharedDir,
      { ignored: /[\/\\]\./, ignoreInitial: true}
    ).on('all', function(event, file) {
      var sharedFile=file.replace(sharedDir, '');
      // replace all path.sep with / becuase in *nix file system \
      // and cause errors we have cross platform syncronization from windows to *nix
      // all commands thats should normalize the path before using it
      sharedFile = sharedFile.replace(path.sep, '/');
      generateChange(sharedFile, 0);
    }).on('error', function(err) {
      winston.error('unexpected error occured ' + JSON.stringify(err));
    });
  }
}
<commit_msg>fixed some variable names<commit_after>var chokidar = require('chokidar');
var http = require('http');
var fs = require('graceful-fs-extra')
var winston = require('winston');
var path = require('path');

var utils = require('./utils');

module.exports = {
  start: function(serverAddress, serverPort, sharedDir, maxRetries, logLevel) {

    winston.level = logLevel;
    maxRetries = maxRetries || 1;

    // We don't really need filesystem events.
    // We just need to state be able to produce the same state as on monitored direcotry.
    // Unlink doesn't really care if it is directory or file.
    // Changed doesnt really care if the file is changed or just craeted.
    var generateChange = function(file, retry) {
      var localFile = path.join(sharedDir, file);
      var sharedFile = file;
      fs.stat(localFile, function(err, stats) {
        // Just send event directly to the remote machine for missing files
        if (err && err.code === 'ENOENT') {
          sendChange('unlink', sharedFile, retry, null);
          return;
        }
        if (err) {
          winston.error('unexpected error occured ' + JSON.stringify(err));
          return;
        }

        winston.debug('stats for ' + file + ' are ' + JSON.stringify(stats));
        if(stats.isFile()) {
          utils.calcHash(localFile, function(error, sha1) {
            sendChange('copy', sharedFile, retry, sha1);
          });
        } else {
          sendChange('addDir', sharedFile, retry, null);
        }
      })
    }

    var sendChange = function(event, file, retry, checkSum) {
      winston.debug('sending ' + event + ' for ' + file + ' retried ' + retry);
      if (retry > maxRetries) {
        winston.error('unabled to syncronize ' + file + ' retried ' + retry + ' times.');
        return;
      }
      var options = {
        host: serverAddress,
        port: serverPort,
        path: '/',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        }
      };

      var req = http.request(options, function(res) {
        var data="";
        res.on('data', function (chunk) {
          data+=chunk;
        });

        res.on('end', function () {
          if (JSON.parse(data)["code"] === utils.ERROR_CODE) {
            winston.warn('error occured while syncing ' + file + ' ' + data);
            generateChange(file, retry + 1);
          } else {
            winston.debug('successful response received ' + data);
          }
        });
      });

      postData = JSON.stringify({
        event: event,
        file: file,
        checkSum: checkSum,
      });

      req.write(postData);

      req.end();
    };

    chokidar.watch(
      sharedDir,
      { ignored: /[\/\\]\./, ignoreInitial: true}
    ).on('all', function(event, file) {
      var sharedFile=file.replace(sharedDir, '');
      // replace all path.sep with / becuase in *nix file system \
      // and cause errors we have cross platform syncronization from windows to *nix
      // all commands thats should normalize the path before using it
      sharedFile = sharedFile.replace(path.sep, '/');
      generateChange(sharedFile, 0);
    }).on('error', function(err) {
      winston.error('unexpected error occured ' + JSON.stringify(err));
    });
  }
}
<|endoftext|>