<commit_before>var async = require('async');
var defaults = require('lodash.defaults');
var glob = require('glob');
var path = require('path');

var routes = require('../src/routes.json');

const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';

const PASS_REQUEST_CONDITION_NAME = 'Pass';
const NOT_PASS_REQUEST_CONDITION_NAME = '!(Pass)';
const PASS_CACHE_CONDITION_NAME = 'Cache Pass';
const BUCKET_NAME_HEADER_NAME = 'Bucket name';

var fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, FASTLY_SERVICE_ID);

var extraAppRoutes = [
    // Homepage with querystring.
    // TODO: Should this be added for every route?
    '^/\\?',
    // Version output by build
    '/version\.txt$',
    // View html
    '^/[^\/]*\.html'
];

/*
 * Given the relative path to the static directory, return an array of
 * patterns matching the files and directories there.
 */
var getStaticPaths = function (pathToStatic) {
    var staticPaths = glob.sync(path.resolve(__dirname, pathToStatic));
    return staticPaths.map( function (pathName) {
        // Reduce absolute path to relative paths like '/js'
        var base = path.dirname(path.resolve(__dirname, pathToStatic));
        return '^' + pathName.replace(base, '');
    });
};

/*
 * Given a list of express routes, return a list of patterns to match
 * the express route and a static view file associated with the route
 */
var getViewPaths = function (routes) {
    return routes.map(function (route) {
        return route.pattern;
    });
};

/*
 * Given a list of patterns for paths, OR all of them together into one
 * string suitable for a Fastly condition
 */
var pathsToCondition = function (paths) {
    return paths.reduce(function (conditionString, pattern) {
        var patternCondition = 'req.url~"' + pattern + '"';
        return conditionString + (conditionString ? '||' : '') + patternCondition;
    }, '');
};

/*
 * Helper method to NOT a condition statement
 */
var negateConditionStatement = function (statement) {
    return '!(' + statement + ')';
};

/*
 * Combine static paths, routes, and any additional paths to a single
 * fastly condition to match req.url
 */
var getAppRouteCondition = function (pathToStatic, routes, additionalPaths) {
    var staticPaths = getStaticPaths(pathToStatic);
    var viewPaths = getViewPaths(routes);
    var allPaths = [].concat(staticPaths, viewPaths, additionalPaths);
    return pathsToCondition(allPaths);
};

var getConditionNameForRoute = function (route, type) {
    return 'routes/' + route.pattern + ' (' + type + ')';
};

var getHeaderNameForRoute = function (route) {
    if (route.name) return 'rewrites/' + route.name;
    if (route.redirect) return 'redirects/' + route.pattern;
};

var getResponseNameForRoute = function (route) {
    return 'redirects/' + route.pattern;
};

async.auto({
    version: function (cb) {
        fastly.getLatestVersion(function (err, response) {
            if (err) return cb(err);
            // Validate latest version before continuing
            if (response.active || response.locked) {
                fastly.cloneVersion(response.number, function (err, response) {
                    if (err) return cb('Failed to clone latest version: ' + err);
                    cb(null, response.number);
                });
            } else {
                cb(null, response.number);
            }
        });
    },
    notPassRequestCondition: ['version', function (cb, results) {
        var statement = getAppRouteCondition('../static/*', routes, extraAppRoutes);
        var condition = {
            name: NOT_PASS_REQUEST_CONDITION_NAME,
            statement: statement,
            type: 'REQUEST',
            priority: 10
        };
        fastly.setCondition(results.version, condition, cb);
    }],
    setBucketNameHeader: ['version', 'notPassRequestCondition', function (cb, results) {
        var header = {
            name: BUCKET_NAME_HEADER_NAME,
            action: 'set',
            ignore_if_set: 0,
            type: 'REQUEST',
            dst: 'http.host',
            src: '"' + S3_BUCKET_NAME + '"',
            request_condition: results.notPassRequestCondition.name,
            priority: 1
        };
        fastly.setFastlyHeader(results.version, header, cb);
    }],
    passRequestCondition: ['version', 'notPassRequestCondition', function (cb, results) {
        var condition = {
            name: PASS_REQUEST_CONDITION_NAME,
            statement: negateConditionStatement(results.notPassRequestCondition.statement),
            type: 'REQUEST',
            priority: 10
        };
        fastly.setCondition(results.version, condition, cb);
    }],
    passRequestSettingsCondition: ['version', 'passRequestCondition', function (cb, results) {
        fastly.request(
            'PUT',
            fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/request_settings/Pass',
            {request_condition: results.passRequestCondition.name},
            cb
        );
    }],
    backendCondition: ['version', 'notPassRequestCondition', function (cb, results) {
        fastly.request(
            'PUT',
            fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/backend/s3',
            {request_condition: results.notPassRequestCondition.name},
            cb
        );
    }],
    passCacheCondition: ['version', 'passRequestCondition', function (cb, results) {
        var condition = defaults(
            {name: PASS_CACHE_CONDITION_NAME, type: 'CACHE'},
            results.passRequestCondition
        );
        fastly.setCondition(results.version, condition, cb);
    }],
    passCacheSettingsCondition: ['version', 'passCacheCondition', function (cb, results) {
        fastly.request(
            'PUT',
            fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/cache_settings/Pass',
            {cache_condition: results.passCacheCondition.name},
            cb
        );
    }],
    appRouteRequestConditions: ['version', function (cb, results) {
        var conditions = {};
        async.forEachOf(routes, function (route, id, cb2) {
            var condition = {
                name: getConditionNameForRoute(route, 'request'),
                statement: 'req.url ~ "' + route.pattern + '"',
                type: 'REQUEST',
                priority: id
            };
            fastly.setCondition(results.version, condition, function (err, response) {
                if (err) return cb2(err);
                conditions[id] = response;
                cb2(null, response);
            });
        }, function (err) {
            if (err) return cb(err);
            cb(null, conditions);
        });
    }],
    appRouteHeaders: ['version', 'appRouteRequestConditions', function (cb, results) {
        var headers = {};
        async.forEachOf(routes, function (route, id, cb2) {
            if (route.redirect) {
                async.auto({
                    responseCondition: function (cb3) {
                        var condition = {
                            name: getConditionNameForRoute(route, 'response'),
                            statement: 'req.url ~ "' + route.pattern + '"',
                            type: 'RESPONSE',
                            priority: id
                        };
                        fastly.setCondition(results.version, condition, cb3);
                    },
                    responseObject: function (cb3) {
                        var responseObject = {
                            name: getResponseNameForRoute(route),
                            status: 301,
                            response: 'Moved Permanently',
                            request_condition: getConditionNameForRoute(route, 'request')
                        };
                        fastly.setResponseObject(results.version, responseObject, cb3);
                    },
                    redirectHeader: ['responseCondition', function (cb3, redirectResults) {
                        var header = {
                            name: getHeaderNameForRoute(route),
                            action: 'set',
                            ignore_if_set: 0,
                            type: 'RESPONSE',
                            dst: 'http.Location',
                            src: '"' + route.redirect + '"',
                            response_condition: redirectResults.responseCondition.name
                        };
                        fastly.setFastlyHeader(results.version, header, cb3);
                    }]
                }, function (err, redirectResults) {
                    if (err) return cb2(err);
                    headers[id] = redirectResults.redirectHeader;
                    cb2(null, redirectResults);
                });
            } else {
                var header = {
                    name: getHeaderNameForRoute(route, 'request'),
                    action: 'set',
                    ignore_if_set: 0,
                    type: 'REQUEST',
                    dst: 'url',
                    src: '"/' + route.name + '.html"',
                    request_condition: results.appRouteRequestConditions[id].name,
                    priority: 10
                };
                fastly.setFastlyHeader(results.version, header, function (err, response) {
                    if (err) return cb2(err);
                    headers[id] = response;
                    cb2(null, response);
                });
            }
        }, function (err) {
            if (err) return cb(err);
            cb(null, headers);
        });
    }]},
    function (err, results) {
        if (err) throw new Error(err);
        if (process.env.FASTLY_ACTIVATE_CHANGES) {
            fastly.activateVersion(results.version, function (err, response) {
                if (err) throw new Error(err);
                process.stdout.write('Successfully configured and activated version ' + response.number + '\n');
            });
        }
    }
);
<commit_msg>End static directories with / in Pass condition<commit_after>var async = require('async');
var defaults = require('lodash.defaults');
var glob = require('glob');
var path = require('path');

var routes = require('../src/routes.json');

const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';

const PASS_REQUEST_CONDITION_NAME = 'Pass';
const NOT_PASS_REQUEST_CONDITION_NAME = '!(Pass)';
const PASS_CACHE_CONDITION_NAME = 'Cache Pass';
const BUCKET_NAME_HEADER_NAME = 'Bucket name';

var fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, FASTLY_SERVICE_ID);

var extraAppRoutes = [
    // Homepage with querystring.
    // TODO: Should this be added for every route?
    '^/\\?',
    // Version output by build
    '/version\.txt$',
    // View html
    '^/[^\/]*\.html'
];

/*
 * Given the relative path to the static directory, return an array of
 * patterns matching the files and directories there.
 */
var getStaticPaths = function (pathToStatic) {
    var staticPaths = glob.sync(path.resolve(__dirname, pathToStatic));
    return staticPaths.map(function (pathName) {
        // Reduce absolute path to relative paths like '/js'
        var base = path.dirname(path.resolve(__dirname, pathToStatic));
        return '^' + pathName.replace(base, '') + (path.extname(pathName) ? '' : '/');
    });
};

/*
 * Given a list of express routes, return a list of patterns to match
 * the express route and a static view file associated with the route
 */
var getViewPaths = function (routes) {
    return routes.map(function (route) {
        return route.pattern;
    });
};

/*
 * Given a list of patterns for paths, OR all of them together into one
 * string suitable for a Fastly condition
 */
var pathsToCondition = function (paths) {
    return paths.reduce(function (conditionString, pattern) {
        var patternCondition = 'req.url~"' + pattern + '"';
        return conditionString + (conditionString ? '||' : '') + patternCondition;
    }, '');
};

/*
 * Helper method to NOT a condition statement
 */
var negateConditionStatement = function (statement) {
    return '!(' + statement + ')';
};

/*
 * Combine static paths, routes, and any additional paths to a single
 * fastly condition to match req.url
 */
var getAppRouteCondition = function (pathToStatic, routes, additionalPaths) {
    var staticPaths = getStaticPaths(pathToStatic);
    var viewPaths = getViewPaths(routes);
    var allPaths = [].concat(staticPaths, viewPaths, additionalPaths);
    return pathsToCondition(allPaths);
};

var getConditionNameForRoute = function (route, type) {
    return 'routes/' + route.pattern + ' (' + type + ')';
};

var getHeaderNameForRoute = function (route) {
    if (route.name) return 'rewrites/' + route.name;
    if (route.redirect) return 'redirects/' + route.pattern;
};

var getResponseNameForRoute = function (route) {
    return 'redirects/' + route.pattern;
};

async.auto({
    version: function (cb) {
        fastly.getLatestVersion(function (err, response) {
            if (err) return cb(err);
            // Validate latest version before continuing
            if (response.active || response.locked) {
                fastly.cloneVersion(response.number, function (err, response) {
                    if (err) return cb('Failed to clone latest version: ' + err);
                    cb(null, response.number);
                });
            } else {
                cb(null, response.number);
            }
        });
    },
    notPassRequestCondition: ['version', function (cb, results) {
        var statement = getAppRouteCondition('../static/*', routes, extraAppRoutes);
        var condition = {
            name: NOT_PASS_REQUEST_CONDITION_NAME,
            statement: statement,
            type: 'REQUEST',
            priority: 10
        };
        fastly.setCondition(results.version, condition, cb);
    }],
    setBucketNameHeader: ['version', 'notPassRequestCondition', function (cb, results) {
        var header = {
            name: BUCKET_NAME_HEADER_NAME,
            action: 'set',
            ignore_if_set: 0,
            type: 'REQUEST',
            dst: 'http.host',
            src: '"' + S3_BUCKET_NAME + '"',
            request_condition: results.notPassRequestCondition.name,
            priority: 1
        };
        fastly.setFastlyHeader(results.version, header, cb);
    }],
    passRequestCondition: ['version', 'notPassRequestCondition', function (cb, results) {
        var condition = {
            name: PASS_REQUEST_CONDITION_NAME,
            statement: negateConditionStatement(results.notPassRequestCondition.statement),
            type: 'REQUEST',
            priority: 10
        };
        fastly.setCondition(results.version, condition, cb);
    }],
    passRequestSettingsCondition: ['version', 'passRequestCondition', function (cb, results) {
        fastly.request(
            'PUT',
            fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/request_settings/Pass',
            {request_condition: results.passRequestCondition.name},
            cb
        );
    }],
    backendCondition: ['version', 'notPassRequestCondition', function (cb, results) {
        fastly.request(
            'PUT',
            fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/backend/s3',
            {request_condition: results.notPassRequestCondition.name},
            cb
        );
    }],
    passCacheCondition: ['version', 'passRequestCondition', function (cb, results) {
        var condition = defaults(
            {name: PASS_CACHE_CONDITION_NAME, type: 'CACHE'},
            results.passRequestCondition
        );
        fastly.setCondition(results.version, condition, cb);
    }],
    passCacheSettingsCondition: ['version', 'passCacheCondition', function (cb, results) {
        fastly.request(
            'PUT',
            fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/cache_settings/Pass',
            {cache_condition: results.passCacheCondition.name},
            cb
        );
    }],
    appRouteRequestConditions: ['version', function (cb, results) {
        var conditions = {};
        async.forEachOf(routes, function (route, id, cb2) {
            var condition = {
                name: getConditionNameForRoute(route, 'request'),
                statement: 'req.url ~ "' + route.pattern + '"',
                type: 'REQUEST',
                priority: id
            };
            fastly.setCondition(results.version, condition, function (err, response) {
                if (err) return cb2(err);
                conditions[id] = response;
                cb2(null, response);
            });
        }, function (err) {
            if (err) return cb(err);
            cb(null, conditions);
        });
    }],
    appRouteHeaders: ['version', 'appRouteRequestConditions', function (cb, results) {
        var headers = {};
        async.forEachOf(routes, function (route, id, cb2) {
            if (route.redirect) {
                async.auto({
                    responseCondition: function (cb3) {
                        var condition = {
                            name: getConditionNameForRoute(route, 'response'),
                            statement: 'req.url ~ "' + route.pattern + '"',
                            type: 'RESPONSE',
                            priority: id
                        };
                        fastly.setCondition(results.version, condition, cb3);
                    },
                    responseObject: function (cb3) {
                        var responseObject = {
                            name: getResponseNameForRoute(route),
                            status: 301,
                            response: 'Moved Permanently',
                            request_condition: getConditionNameForRoute(route, 'request')
                        };
                        fastly.setResponseObject(results.version, responseObject, cb3);
                    },
                    redirectHeader: ['responseCondition', function (cb3, redirectResults) {
                        var header = {
                            name: getHeaderNameForRoute(route),
                            action: 'set',
                            ignore_if_set: 0,
                            type: 'RESPONSE',
                            dst: 'http.Location',
                            src: '"' + route.redirect + '"',
                            response_condition: redirectResults.responseCondition.name
                        };
                        fastly.setFastlyHeader(results.version, header, cb3);
                    }]
                }, function (err, redirectResults) {
                    if (err) return cb2(err);
                    headers[id] = redirectResults.redirectHeader;
                    cb2(null, redirectResults);
                });
            } else {
                var header = {
                    name: getHeaderNameForRoute(route, 'request'),
                    action: 'set',
                    ignore_if_set: 0,
                    type: 'REQUEST',
                    dst: 'url',
                    src: '"/' + route.name + '.html"',
                    request_condition: results.appRouteRequestConditions[id].name,
                    priority: 10
                };
                fastly.setFastlyHeader(results.version, header, function (err, response) {
                    if (err) return cb2(err);
                    headers[id] = response;
                    cb2(null, response);
                });
            }
        }, function (err) {
            if (err) return cb(err);
            cb(null, headers);
        });
    }]},
    function (err, results) {
        if (err) throw new Error(err);
        if (process.env.FASTLY_ACTIVATE_CHANGES) {
            fastly.activateVersion(results.version, function (err, response) {
                if (err) throw new Error(err);
                process.stdout.write('Successfully configured and activated version ' + response.number + '\n');
            });
        }
    }
);
<|endoftext|>


<commit_before>const pool = require('../connection/mysqlConn')
const dbBase = require('./dbBase')

function Table(tableName) {
    if (!tableName || typeof tableName !== 'string' || !tableName.trim())
        throw new Error('error table name.')
    else this.tableName = tableName
}
const proto = Table.prototype

proto.add = function (data) {
    let insert = data ? 'SET' : ''
    if (data) {
        for (let key in data) {
            let value = data[key]
            if (value) {
                let set = ` ${key}='${value}',`
                insert += set
            }
        }
        if (insert.lastIndexOf(',') === insert.length - 1) insert = insert.substr(0, insert.length - 1)
    }

    let sqlStr = `INSERT INTO ${this.tableName} ${insert}`
    return dbBase.execute(sqlStr)
}

proto.find = function (query) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }

    let sqlStr = `SELECT * FROM ${this.tableName} ${where}`;
    return dbBase.execute(sqlStr);
}

proto.findOne = function (query) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }

    let sqlStr = `SELECT * FROM ${this.tableName} ${where} LIMIT 1`;
    return dbBase.execute(sqlStr);
}

proto.update = function (query, updateData) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }
    let updateStrArr = []
    if (updateData) {
        for (let key in updateData) {
            let value = updateData[key]
            let condition = ` ${key}='${value}'`
            updateStrArr.push(condition)
        }
    }
    let updateStr = updateStrArr.join(',')
    let sqlStr = `UPDATE ${this.tableName} SET ${updateStr} ${where}`;
    return dbBase.execute(sqlStr);
    /**
     * UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
     * SET col_name1=expr1 [, col_name2=expr2 ...]
     * [WHERE where_definition]
     * [ORDER BY ...]
     * [LIMIT row_count]
     */
}

proto.remove = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

proto.save = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

proto.query = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

module.exports = Table<commit_msg>添加like函数，修改find函数，查询条件使用and分割<commit_after>const pool = require('../connection/mysqlConn')
const dbBase = require('./dbBase')

function Table(tableName) {
    if (!tableName || typeof tableName !== 'string' || !tableName.trim())
        throw new Error('error table name.')
    else this.tableName = tableName
}
const proto = Table.prototype

proto.add = function (data) {
    let insert = data ? 'SET' : ''
    if (data) {
        for (let key in data) {
            let value = data[key]
            if (value) {
                let set = ` ${key}='${value}',`
                insert += set
            }
        }
        if (insert.lastIndexOf(',') === insert.length - 1) insert = insert.substr(0, insert.length - 1)
    }

    let sqlStr = `INSERT INTO ${this.tableName} ${insert}`
    return dbBase.execute(sqlStr)
}

proto.find = function (query) {
    let where = query ? 'WHERE' : ''
    let whereArr = []
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            whereArr.push(condition)
        }
    }
    where += whereArr.join(' and ')
    let sqlStr = `SELECT * FROM ${this.tableName} ${where}`;
    return dbBase.execute(sqlStr);
}
proto.like = function (query) {
    let where = query ? 'WHERE' : ''
    let whereArr = []
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key} like '%${value}%%'`
            whereArr.push(condition)
        }
    }
    where += whereArr.join(' and ')
    let sqlStr = `SELECT * FROM ${this.tableName} ${where}`;
    return dbBase.execute(sqlStr);
}
proto.findOne = function (query) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }

    let sqlStr = `SELECT * FROM ${this.tableName} ${where} LIMIT 1`;
    return dbBase.execute(sqlStr);
}

proto.update = function (query, updateData) {
    let where = query ? 'WHERE' : ''
    if (query) {
        for (let key in query) {
            let value = query[key]
            let condition = ` ${key}='${value}'`
            where += condition
        }
    }
    let updateStrArr = []
    if (updateData) {
        for (let key in updateData) {
            let value = updateData[key]
            let condition = ` ${key}='${value}'`
            updateStrArr.push(condition)
        }
    }
    let updateStr = updateStrArr.join(',')
    let sqlStr = `UPDATE ${this.tableName} SET ${updateStr} ${where}`;
    return dbBase.execute(sqlStr);
    /**
     * UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
     * SET col_name1=expr1 [, col_name2=expr2 ...]
     * [WHERE where_definition]
     * [ORDER BY ...]
     * [LIMIT row_count]
     */
}

proto.remove = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

proto.save = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

proto.query = function () {
    let sqlStr = '';
    return dbBase.execute(sqlStr);
}

module.exports = Table<|endoftext|>


<commit_before>#!/usr/bin/env node
try {
  if (process.argv[2] === '-install') {
    var laCmd = process.mainModule.filename
    // var exec = require('child_process').execSync
    // exec('powershell -command "Set-ItemProperty -Path \'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment\' -Name LOGAGENT_CONFIG –Value \'' + process.argv[3] +'\'',{stdio: [0,1,2]})
    console.log(laCmd)
    let Service = require('node-windows').Service
    let svc = new Service({
      name: 'Logagent',
      description: 'Sematext Logagent',
      script: laCmd,
      wait: 2,
      grow: 0.5
    })
    svc.on('error', console.log)
    // Listen for the "install" event, which indicates the
    // process is available as a service.
    svc.on('install', function () {
      console.log('Logagent service installed')
      svc.start()
    })
    svc.on('start', function () {
      console.log('Logagent service started')
    })
    svc.install()
  }
  if (process.argv[2] === '-uninstall') {
    let Service = require('node-windows').Service
    let svc = new Service({
      name: 'Logagent',
      description: 'Sematext Logagent',
      script: process.mainModule.filename
    })
    svc.on('uninstall', function () {
      console.log('Service Logagent removed.')
    })
    svc.uninstall()
  } else {
    process.argv.push('--config')
    process.argv.push(process.env.LOGAGENT_CONFIG || process.env.ProgramData + '\\Sematext\\logagent.conf')
    new (require('./logagent.js'))()
  }
} catch (err) {
  console.log(err.stack)
}
<commit_msg>fix 'error unknown option -t'<commit_after>#!/usr/bin/env node
'use strict'
try {
  if (process.argv[2] === '-install') {
    var laCmd = process.mainModule.filename
    // var exec = require('child_process').execSync
    // exec('powershell -command "Set-ItemProperty -Path \'Registry::HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment\' -Name LOGAGENT_CONFIG –Value \'' + process.argv[3] +'\'',{stdio: [0,1,2]})
    console.log(laCmd)
    let Service = require('node-windows').Service
    let svc = new Service({
      name: 'Logagent',
      description: 'Sematext Logagent',
      script: laCmd,
      wait: 2,
      grow: 0.5
    })
    svc.on('error', console.log)
    // Listen for the "install" event, which indicates the
    // process is available as a service.
    svc.on('install', function () {
      console.log('Logagent service installed')
      svc.start()
    })
    svc.on('start', function () {
      console.log('Logagent service started')
    })
    svc.install()
  } else if (process.argv[2] === '-uninstall') {
    let Service = require('node-windows').Service
    let svc = new Service({
      name: 'Logagent',
      description: 'Sematext Logagent',
      script: process.mainModule.filename
    })
    svc.on('uninstall', function () {
      console.log('Service Logagent removed.')
    })
    svc.uninstall()
  } else {
    if (process.argv.join(',').indexOf('--config') === -1) {
      // use default config
      process.argv.push('--config')
      process.argv.push(process.env.LOGAGENT_CONFIG || process.env.ProgramData + '\\Sematext\\logagent.conf')
    }
    new (require('./logagent.js'))()
  }
} catch (err) {
  console.log(err.stack)
}
<|endoftext|>


<commit_before>/*
 Typically invoked by cron.
 */
var home = require('./../lib/homepath'),
	config = require(home.path() + '/.telldus2graphite/config.json'),
	graphite = require('./../lib/graphite.js')(config),
	client = require('telldus-live-promise').Client(config),
	sensors = require('telldus-live-promise').Sensors(client),
	logger = require('log4js-extras')(config).getLogger(__filename);

sensors.getSensors().then(sensors.getSensorInfos).then(graphite.logAll).catch(logger.error);
<commit_msg>Update README<commit_after>/*
 Typically invoked by cron.
 */
var home = require('./../lib/homepath'),
	config = require(home.path() + '/.telldus2graphite/config.json'),
	graphite = require('./../lib/graphite.js')(config),
	logger = require('log4js-extras')(config).getLogger(__filename);
config.logger = logger;
var telldus = require('telldus-live-promise'),
	client = telldus.Client(config),
	sensors = telldus.Sensors(client);

sensors.getSensors().then(sensors.getSensorInfos).then(graphite.logAll).catch(logger.error);
<|endoftext|>


<commit_before>#!/usr/bin/env node

var UInt160 = require('../').UInt160;
var address = process.argv[2];

if (address === '-') {
  readInput(validateAddress);
} else {
  validateAddress(address);
}

function readInput(callback) {
  var result = '';
  process.stdin.resume();
  process.stdin.setEncoding('utf8');
  process.stdin.on('data', function(data) {
    result += data;
  });
  process.stdin.on('end', function() {
    callback(result.trim());
  });
};

function validateAddress(address) {
  process.stdout.write((UInt160.is_valid(address) ? '0' : '1') + '\r\n');
};
<commit_msg>Remove validate_account (it exists as bin/validate_address.js)<commit_after><|endoftext|>


<commit_before>'use strict';

/*
Field unpacking functions for the general binary parser.

(C) 2015 Jeroen F.J. Laros <J.F.J.Laros@lumc.nl>
*/

var yaml = require('js-yaml');

module.exports.operators = {
  'not': function(a) { return !a; },
  'and': function(a, b) { return a && b; },
  'or': function(a, b) { return a || b; },
  'xor': function(a, b) { return a ^ b; },
  'eq': function(a, b) { return a === b; },
  'ne': function(a, b) { return a !== b; },
  'ge': function(a, b) { return a >= b; },
  'gt': function(a, b) { return a > b; },
  'le': function(a, b) { return a <= b; },
  'lt': function(a, b) { return a < b; },
  'mod': function(a, b) { return a % b; },
  'contains': function(a, b) { return a in b; }
};

/*
Miscellaneous functions.
*/

function ord(character) {
  return character.charCodeAt(0);
}

function hex(value) {
  return value.toString(16);
}

/*
Pad a string with leading zeroes.

:arg any input: String to be padded.
:arg int length: Length of the resulting string.

:return str: Padded string.
*/
function pad(input, length) {
  var string = input.toString(),
      padding = '',
      index;

  for (index = 0; index < length - string.length; index++) {
    padding += '0';
  }
  return padding + string;
}

/*
Encode a string in hexadecimal.

:arg str data: Input string.

:return str: Hexadecimal representation of {data}.
*/
function convertToHex(data) {
  var result = '',
      index;

  for (index = 0; index < data.length; index++) {
    result += pad(hex(data.charCodeAt(index)), 2);
  }
  return result;
}

/*
Update a dictionary with the properties of another dictionary.

:arg dict target: Target dictionary.
:arg dict source: Source dictionary.
*/
function update(target, source) {
  var item;

  for (item in source) {
    target[item] = source[item];
  }
}

function BinParseFunctions(typesHandle) {
  this.raw = function(data) {
    return convertToHex(data);
  };

  this.bit = function(data) {
    var bitField = this.int(data),
        mask,
        result = '';

    for (mask = 0x80; mask; mask >>= 1) {
      result += (+Boolean(bitField & mask)).toString();
    }
    return result;
  };

  /*
  Decode a little-endian encoded integer.

  Decoding is done as follows:
  - Reverse the order of the bits.
  - Convert the bits to ordinals.
  - Interpret the list of ordinals as digits in base 256.

  :arg str data: Little-endian encoded integer.

  :return int: Integer representation of {data}
  */
  this.int = function(data) {
    var result = 0,
        index;

    for (index = data.length - 1; index >= 0; index--) {
      result = result * 0x100 + data.charCodeAt(index);
    }
    return result;
  };

  this.colour = function(data) {
    return '0x' + pad(hex(integer(data)), 6);
  };

  this.trim = function(data) { // FIXME
    return data.split(''.join(map(chr, types['trim']['delimiter'])))[0];
  };

  this.text = function(data, delimiter) { // FIXME
    var field = data.split(''.join(map(chr, types['text']['delimiter'])))[0];

    if (delimiter !== undefined) {
      return '\n'.join(field.split(''.join(map(chr,
        types['text']['data'][delimiter]))));
    }
    return field;
  };

  /*
  Decode a date.

  The date is encoded as an integer, representing the year followed by the
  (zero padded) day of the year.

  :arg str data: Binary encoded date.

  :return str: Date in format '%Y%j', 'defined' or 'unknown'.
  */
  this.date = function(data) {
    var dateInt = integer(data);

    if (dateInt in fields.maps.date) {
      return fields.maps.date[dateInt];
    }
    return dateInt.toString();
  };

  /*
  Replace a value with its annotation.

  :arg str data: Encoded data.
  :arg dict annotation: Annotation of {data}.

  :return str: Annotated representation of {data}.
  */
  this.map = function(data, annotation) {
    var index = ord(data);

    if (index in fields.maps[annotation]) {
      return fields.maps[annotation][index];
    }
    return convertToHex(data);
  };

  /*
  Explode a bitfield into flags.

  :arg int data: Bit field.
  :arg str annotation: Annotation of {data}.
  */
  this.flags = function(data, annotation) {
    var bitfield = integer(data),
        destination = {},
        flag,
        value;

    for (flag = 0x01; flag < 0x100; flag <<= 1) {
      value = Boolean(flag & bitfield);

      if (!(flag in fields.flags[annotation])) {
        if (value) {
          destination['flags_' + annotation + '_' + pad(hex(flag), 2)] = value;
        }
      }
      else {
        destination[fields.flags[annotation][flag]] = value;
      }
    }
    return destination;
  };

  var types = yaml.load(typesHandle);

  // Add standard data types.
  types['raw'] = {};
  types['list'] = {};

  // Set default data type.
  if (!('default' in types)) {
    types['default'] = 'text';
  }
}

module.exports.BinParseFunctions = BinParseFunctions;
<commit_msg>Fixed trim function.<commit_after>'use strict';

/*
Field unpacking functions for the general binary parser.

(C) 2015 Jeroen F.J. Laros <J.F.J.Laros@lumc.nl>
*/

var yaml = require('js-yaml');

module.exports.operators = {
  'not': function(a) { return !a; },
  'and': function(a, b) { return a && b; },
  'or': function(a, b) { return a || b; },
  'xor': function(a, b) { return a ^ b; },
  'eq': function(a, b) { return a === b; },
  'ne': function(a, b) { return a !== b; },
  'ge': function(a, b) { return a >= b; },
  'gt': function(a, b) { return a > b; },
  'le': function(a, b) { return a <= b; },
  'lt': function(a, b) { return a < b; },
  'mod': function(a, b) { return a % b; },
  'contains': function(a, b) { return a in b; }
};

/*
Miscellaneous functions.
*/

function ord(character) {
  return character.charCodeAt(0);
}

function hex(value) {
  return value.toString(16);
}

/*
Pad a string with leading zeroes.

:arg any input: String to be padded.
:arg int length: Length of the resulting string.

:return str: Padded string.
*/
function pad(input, length) {
  var string = input.toString(),
      padding = '',
      index;

  for (index = 0; index < length - string.length; index++) {
    padding += '0';
  }
  return padding + string;
}

/*
Encode a string in hexadecimal.

:arg str data: Input string.

:return str: Hexadecimal representation of {data}.
*/
function convertToHex(data) {
  var result = '',
      index;

  for (index = 0; index < data.length; index++) {
    result += pad(hex(data.charCodeAt(index)), 2);
  }
  return result;
}

/*
Update a dictionary with the properties of another dictionary.

:arg dict target: Target dictionary.
:arg dict source: Source dictionary.
*/
function update(target, source) {
  var item;

  for (item in source) {
    target[item] = source[item];
  }
}

function BinParseFunctions(typesHandle) {
  this.raw = function(data) {
    return convertToHex(data);
  };

  this.bit = function(data) {
    var bitField = data.charCodeAt(0),
        result = '',
        mask;

    for (mask = 0x80; mask; mask >>= 1) {
      result += (+Boolean(bitField & mask)).toString();
    }
    return result;
  };

  /*
  Decode a little-endian encoded integer.

  Decoding is done as follows:
  - Reverse the order of the bits.
  - Convert the bits to ordinals.
  - Interpret the list of ordinals as digits in base 256.

  :arg str data: Little-endian encoded integer.

  :return int: Integer representation of {data}
  */
  this.int = function(data) {
    var result = 0,
        index;

    for (index = data.length - 1; index >= 0; index--) {
      result = result * 0x100 + data.charCodeAt(index);
    }
    return result;
  };

  this.colour = function(data) {
    return '0x' + pad(hex(integer(data)), 6);
  };

  this.trim = function(data) {
    var delimiter = '',
        index;

    for (index = 0; index < types['trim']['delimiter'].length; index++) {
      delimiter += String.fromCharCode(types['trim']['delimiter'][index]);
    }

    return data.split(delimiter)[0];
  };

  this.text = function(data, delimiter) { // FIXME
    var field = data.split(''.join(map(chr, types['text']['delimiter'])))[0];

    if (delimiter !== undefined) {
      return '\n'.join(field.split(''.join(map(chr,
        types['text']['data'][delimiter]))));
    }
    return field;
  };

  /*
  Decode a date.

  The date is encoded as an integer, representing the year followed by the
  (zero padded) day of the year.

  :arg str data: Binary encoded date.

  :return str: Date in format '%Y%j', 'defined' or 'unknown'.
  */
  this.date = function(data) {
    var dateInt = integer(data);

    if (dateInt in fields.maps.date) {
      return fields.maps.date[dateInt];
    }
    return dateInt.toString();
  };

  /*
  Replace a value with its annotation.

  :arg str data: Encoded data.
  :arg dict annotation: Annotation of {data}.

  :return str: Annotated representation of {data}.
  */
  this.map = function(data, annotation) {
    var index = ord(data);

    if (index in fields.maps[annotation]) {
      return fields.maps[annotation][index];
    }
    return convertToHex(data);
  };

  /*
  Explode a bitfield into flags.

  :arg int data: Bit field.
  :arg str annotation: Annotation of {data}.
  */
  this.flags = function(data, annotation) {
    var bitfield = integer(data),
        destination = {},
        flag,
        value;

    for (flag = 0x01; flag < 0x100; flag <<= 1) {
      value = Boolean(flag & bitfield);

      if (!(flag in fields.flags[annotation])) {
        if (value) {
          destination['flags_' + annotation + '_' + pad(hex(flag), 2)] = value;
        }
      }
      else {
        destination[fields.flags[annotation][flag]] = value;
      }
    }
    return destination;
  };

  var types = yaml.load(typesHandle);

  // Add standard data types.
  types['raw'] = {};
  types['list'] = {};
  types['trim'] = { 'delimiter': [0x00] };

  // Set default data type.
  if (!('default' in types)) {
    types['default'] = 'text';
  }
}

module.exports.BinParseFunctions = BinParseFunctions;
<|endoftext|>


<commit_before>CloudPebble.Sidebar = (function() {

    var suspended_panes = {};
    var mProjectType = 'native';

    var suspend_active_pane = function() {
        var pane_id = $('#main-pane').data('pane-id');
        if(!pane_id) {
            destroy_active_pane();
            $('#pane-parent').append($('

'));
            return;
        }
        var pane = $('#main-pane');

        var suspend_function = pane.data('pane-suspend-function');
        if(suspend_function) suspend_function();

        var list_entry = $('#sidebar-pane-' + pane_id);
        if(list_entry) {
            list_entry.removeClass('active');
        }
        suspended_panes[pane_id] = pane;
        pane.detach();
        // Create a new empty one.
        var empty_pane = $('

');
        $('#pane-parent').append(empty_pane);
    };

    var destroy_active_pane = function() {
        var pane_id = $('#main-pane').data('pane-id');
        var pane = $('#main-pane');
        if(pane.data('pane-destroy-function')) {
            pane.data('pane-destroy-function')(pane);
        }
        pane.remove();
        var list_entry = $('#sidebar-pane-' + pane_id);
        if(list_entry) {
            list_entry.removeClass('active');
        }
    };

    var restore_suspended_pane = function(id) {
        var pane = suspended_panes[id] ;
        if(pane) {
            $('#main-pane').remove();
            $('#pane-parent').append(pane);
            delete suspended_panes[id];

            var list_entry = $('#sidebar-pane-' + id);
            if(list_entry) {
                list_entry.addClass('active');
            }

            if(pane.data('pane-restore-function')) {
                pane.data('pane-restore-function')();
            }

            return true;
        }
        return false;
    };

    var set_main_pane = function(pane, id, restore_function, destroy_function) {
        $('#main-pane').append(pane).data('pane-id', id);
        if(restore_function) {
            $('#main-pane').data('pane-restore-function', restore_function);
        }
        if(destroy_function) {
            $('#main-pane').data('pane-destroy-function', destroy_function);
        }
    };

    var set_active_menu_entry = function(id) {
        $('#sidebar-pane-' + id).addClass('active');
    };

    var init = function() {
    }

    return {
        SuspendActive: function() {
            suspend_active_pane();
        },
        DestroyActive: function() {
            destroy_active_pane();
        },
        Restore: function(id) {
            var restored = restore_suspended_pane(id);
            if(restored) {
                set_active_menu_entry(id);
            }
            return restored;
        },
        SetActivePane: function(pane, id, restore_function, destroy_function) {
            set_main_pane(pane, id, restore_function, destroy_function);
            set_active_menu_entry(id);
        },
        AddResource: function(resource, on_click) {
            var end = $('#end-resources-' + resource.kind);
            if(!end.length) {
                // Create an appropriate section
                var res_end = $('#end-resources');
                end = $('
');
                res_end.before(end);
            }
            var link = $('').text(resource.file_name).click(on_click);
            var li = $('
');
            li.append(link);
            end.before(li);
            return li;
        },
        AddSourceFile: function(file, on_click) {
            var end = $('#end-source-files');
            var link = $('');
            link.text(file.name + ' ');
            link.click(on_click);
            var li = $('
');
            li.append(link);
            end.before(li);
            return li;
        },
        Remove: function(id) {
            $('#sidebar-pane-' + id).remove();
        },
        SetIcon: function(pane_id, icon) {
            var a = $('#sidebar-pane-' + pane_id).find('a');
            var i = a.find('i');
            if(i.length === 0) {
                i = $('');
                a.append(i);
            }

            a.find('i').removeClass().addClass('icon-' + icon);
        },
        ClearIcon: function(pane_id) {
            $('#sidebar-pane-' + pane_id).find('a > i').remove();
        },
        Init: function() {
            $('#sidebar-pane-new-resource > a').click(CloudPebble.Resources.Create);
            $('#sidebar-pane-compile > a').click(CloudPebble.Compile.Show);
            $('#sidebar-pane-settings > a').click(CloudPebble.Settings.Show);
            $('#sidebar-pane-github > a').click(CloudPebble.GitHub.Show);
            $('#new-source-file').click(CloudPebble.Editor.Create);
            $('#new-js-file').click(CloudPebble.Editor.DoJSFile);
            init();
        },
        SetPopover: function(pane_id, title, content) {
            $('#sidebar-pane-' + pane_id).popover('destroy').popover({
                trigger: 'hover',
                title: title,
                content: content,
                html: true,
                delay: {show: 250}
            }).click(function() { $(this).popover('hide'); });
        },
        SetProjectType: function(type) {
            if(type == 'simplyjs') {
                $('.native-only').hide();
            }
        }
    };
})();<commit_msg>Hack out popovers.<commit_after>CloudPebble.Sidebar = (function() {

    var suspended_panes = {};
    var mProjectType = 'native';

    var suspend_active_pane = function() {
        var pane_id = $('#main-pane').data('pane-id');
        if(!pane_id) {
            destroy_active_pane();
            $('#pane-parent').append($('

'));
            return;
        }
        var pane = $('#main-pane');

        var suspend_function = pane.data('pane-suspend-function');
        if(suspend_function) suspend_function();

        var list_entry = $('#sidebar-pane-' + pane_id);
        if(list_entry) {
            list_entry.removeClass('active');
        }
        suspended_panes[pane_id] = pane;
        pane.detach();
        // Create a new empty one.
        var empty_pane = $('

');
        $('#pane-parent').append(empty_pane);
    };

    var destroy_active_pane = function() {
        var pane_id = $('#main-pane').data('pane-id');
        var pane = $('#main-pane');
        if(pane.data('pane-destroy-function')) {
            pane.data('pane-destroy-function')(pane);
        }
        pane.remove();
        var list_entry = $('#sidebar-pane-' + pane_id);
        if(list_entry) {
            list_entry.removeClass('active');
        }
    };

    var restore_suspended_pane = function(id) {
        var pane = suspended_panes[id] ;
        if(pane) {
            $('#main-pane').remove();
            $('#pane-parent').append(pane);
            delete suspended_panes[id];

            var list_entry = $('#sidebar-pane-' + id);
            if(list_entry) {
                list_entry.addClass('active');
            }

            if(pane.data('pane-restore-function')) {
                pane.data('pane-restore-function')();
            }

            return true;
        }
        return false;
    };

    var set_main_pane = function(pane, id, restore_function, destroy_function) {
        $('#main-pane').append(pane).data('pane-id', id);
        if(restore_function) {
            $('#main-pane').data('pane-restore-function', restore_function);
        }
        if(destroy_function) {
            $('#main-pane').data('pane-destroy-function', destroy_function);
        }
    };

    var set_active_menu_entry = function(id) {
        $('#sidebar-pane-' + id).addClass('active');
    };

    var init = function() {
    }

    return {
        SuspendActive: function() {
            suspend_active_pane();
        },
        DestroyActive: function() {
            destroy_active_pane();
        },
        Restore: function(id) {
            var restored = restore_suspended_pane(id);
            if(restored) {
                set_active_menu_entry(id);
            }
            return restored;
        },
        SetActivePane: function(pane, id, restore_function, destroy_function) {
            set_main_pane(pane, id, restore_function, destroy_function);
            set_active_menu_entry(id);
        },
        AddResource: function(resource, on_click) {
            var end = $('#end-resources-' + resource.kind);
            if(!end.length) {
                // Create an appropriate section
                var res_end = $('#end-resources');
                end = $('
');
                res_end.before(end);
            }
            var link = $('').text(resource.file_name).click(on_click);
            var li = $('
');
            li.append(link);
            end.before(li);
            return li;
        },
        AddSourceFile: function(file, on_click) {
            var end = $('#end-source-files');
            var link = $('');
            link.text(file.name + ' ');
            link.click(on_click);
            var li = $('
');
            li.append(link);
            end.before(li);
            return li;
        },
        Remove: function(id) {
            $('#sidebar-pane-' + id).remove();
        },
        SetIcon: function(pane_id, icon) {
            var a = $('#sidebar-pane-' + pane_id).find('a');
            var i = a.find('i');
            if(i.length === 0) {
                i = $('');
                a.append(i);
            }

            a.find('i').removeClass().addClass('icon-' + icon);
        },
        ClearIcon: function(pane_id) {
            $('#sidebar-pane-' + pane_id).find('a > i').remove();
        },
        Init: function() {
            $('#sidebar-pane-new-resource > a').click(CloudPebble.Resources.Create);
            $('#sidebar-pane-compile > a').click(CloudPebble.Compile.Show);
            $('#sidebar-pane-settings > a').click(CloudPebble.Settings.Show);
            $('#sidebar-pane-github > a').click(CloudPebble.GitHub.Show);
            $('#new-source-file').click(CloudPebble.Editor.Create);
            $('#new-js-file').click(CloudPebble.Editor.DoJSFile);
            init();
        },
        SetPopover: function(pane_id, title, content) {
            return;
            $('#sidebar-pane-' + pane_id).popover('destroy').popover({
                trigger: 'hover',
                title: title,
                content: content,
                html: true,
                delay: {show: 250}
            }).click(function() { $(this).popover('hide'); });
        },
        SetProjectType: function(type) {
            if(type == 'simplyjs') {
                $('.native-only').hide();
            }
        }
    };
})();<|endoftext|>


<commit_before>//insert data into MongoDB
module.exports = function(opt, columns, moduleCallback) {

  var fs = require('fs'),
    async = require('async');

  var client = require('mongodb').MongoClient,
    creds = require(opt.cfg.dirs.creds + 'mongo'),
    db,
    collection,
    Stream = require('stream'),
    split = require('split'),
    log = opt.log,
    timer = opt.timer,
    opfile = opt.opfile,
    cols = [];

  const table = opt.table.indexOf('.') > -1 ? opt.table : 'databridge.' + opt.table;
  const databaseName = table.split('.')[0];
  const collectionName = table.split('.')[1];

  async.waterfall([
      function(cb) {
        //connect to mongo
        client.connect(creds + databaseName, function(err, database) {
          if (err) return cb(err);
          db = database;
          log.group('MongoDB').log('Connected');
          cb(null);
        })
      },
      function(cb) {
        //sometimes only insert, don't delete data
        if (opt.update) {
          log.log('Insert only - not dropping existing data.');
          return cb(null);
        }
        //delete old data
        db.listCollections({
          name: collectionName
        }).toArray(function(err, collections) {
          if (err) return cb(err);
          if (collections.length) {
            db.collection(collectionName).drop(function(err, results) {
              if (err) return cb(err);
              log.log('Deleted old collection.');
              cb(null);
            })
          } else {
            cb(null)
          }
        })
      },
      function(cb) {
        //create colleciton (if not already existing)
        db.createCollection(collectionName, function(err, coll) {
          if (err) return cb(err);
          collection = coll;
          log.log('Created collection (if not existing) ' + collectionName);
          cb(null);
        })
      },
      function(cb) {
        //insert data one row at a time
        var first = true;
        var mongoWriteStream = new Stream.Writable();
        var rowCount = 0;
        var indexes = [];
        /* indexes */
        for (var i = 0; i < columns.length; i++) {
          //generate key and save for indexes
          var name = columns[i].name.replace(/_IND/ig, '');
          cols.push(name);
          if (columns[i].index) {
            indexes.push(name);
          }
        }
        /* transform stream */
        mongoWriteStream._write = function(chunk, encoding, callback) {
          //skip first (columns)
          if (first) {
            first = false;
            return callback();
          }
          //write every line
          var data = chunk.toString().split('\t');
          var doc = {};
          //parse types
          for (var i = 0; i < columns.length; i++) {
            //parse data types
            if (!isNaN(Number(data[i]))) {
              doc[name] = Number(data[i]);
            } else if (data[i] === 'true') {
              doc[name] = true;
            } else if (data[i] === 'false') {
              doc[name] = false;
            } else if (
              name.toUpperCase().indexOf('DATE') !== -1 || name.toUpperCase().indexOf('TIMESTAMP') !== -1 || name.toUpperCase().indexOf('DATETIME') !== -1
            ) {
              doc[name] = new Date(data[i]);
            } else {
              doc[name] = data[i];
            }
          }
          //skip blank lines
          if (data.length !== columns.length) return callback();
          //console.log(doc);
          collection.insertOne(doc, function(err, result) {
            if (err) throw err;
            rowCount++;
            callback();
          });

        }
        mongoWriteStream.on('error', function(err) {
          cb(err);
        });
        mongoWriteStream.on('finish', function() {
          cb(null, rowCount, indexes);
        })
        var opfileRStream = opfile.createReadStream();
        opfileRStream.pipe(split()).pipe(mongoWriteStream);
      },
      function(rowCount, indexes, cb) {
        //create indexes
        function makeIndex(i, callback) {
          log.log('makeIndex: ' + i);
          var ndex = {};
          ndex[i] = 1;
          collection.createIndex(ndex, null, function(err, results) {
            if (err) return callback(err);
            callback(null);
          })
        }
        //TODO not making indexes?? WHY??
        async.map(indexes, makeIndex, function(err, results) {
          if (err) return cb(err);

          cb(null, rowCount);
        })
      },
      function(rowCount, cb) {
        collection.count(function(err, count) {
          log.log('Success! Inserted ' + rowCount + ' documents. MongoDB says ' + databaseName + '.' + collectionName + ' now has ' + count + ' docs.');
          cb(null, count);
        })
      }
    ],
    function(err, rows) {
      db.close(true, function(err) {
        if (err) moduleCallback(err);
      });
      try {
        db.close();
      } catch (e) {
        return moduleCallback(e);
      }
      if (err) return moduleCallback(err);
      log.group('Finished destination').log(timer.str());
      moduleCallback(null, rows, cols);
    })

}
<commit_msg>fix db close error handling<commit_after>//insert data into MongoDB
module.exports = function(opt, columns, moduleCallback) {

  var fs = require('fs'),
    async = require('async');

  var client = require('mongodb').MongoClient,
    creds = require(opt.cfg.dirs.creds + 'mongo'),
    db,
    collection,
    Stream = require('stream'),
    split = require('split'),
    log = opt.log,
    timer = opt.timer,
    opfile = opt.opfile,
    cols = [];

  const table = opt.table.indexOf('.') > -1 ? opt.table : 'databridge.' + opt.table;
  const databaseName = table.split('.')[0];
  const collectionName = table.split('.')[1];

  async.waterfall([
      function(cb) {
        //connect to mongo
        client.connect(creds + databaseName, function(err, database) {
          if (err) return cb(err);
          db = database;
          log.group('MongoDB').log('Connected');
          cb(null);
        })
      },
      function(cb) {
        //sometimes only insert, don't delete data
        if (opt.update) {
          log.log('Insert only - not dropping existing data.');
          return cb(null);
        }
        //delete old data
        db.listCollections({
          name: collectionName
        }).toArray(function(err, collections) {
          if (err) return cb(err);
          if (collections.length) {
            db.collection(collectionName).drop(function(err, results) {
              if (err) return cb(err);
              log.log('Deleted old collection.');
              cb(null);
            })
          } else {
            cb(null)
          }
        })
      },
      function(cb) {
        //create colleciton (if not already existing)
        db.createCollection(collectionName, function(err, coll) {
          if (err) return cb(err);
          collection = coll;
          log.log('Created collection (if not existing) ' + collectionName);
          cb(null);
        })
      },
      function(cb) {
        //insert data one row at a time
        var first = true;
        var mongoWriteStream = new Stream.Writable();
        var rowCount = 0;
        var indexes = [];
        /* indexes */
        for (var i = 0; i < columns.length; i++) {
          //generate key and save for indexes
          var name = columns[i].name.replace(/_IND/ig, '');
          cols.push(name);
          if (columns[i].index) {
            indexes.push(name);
          }
        }
        /* transform stream */
        mongoWriteStream._write = function(chunk, encoding, callback) {
          //skip first (columns)
          if (first) {
            first = false;
            return callback();
          }
          //write every line
          var data = chunk.toString().split('\t');
          var doc = {};
          //parse types
          for (var i = 0; i < columns.length; i++) {
            //parse data types
            if (!isNaN(Number(data[i]))) {
              doc[name] = Number(data[i]);
            } else if (data[i] === 'true') {
              doc[name] = true;
            } else if (data[i] === 'false') {
              doc[name] = false;
            } else if (
              name.toUpperCase().indexOf('DATE') !== -1 || name.toUpperCase().indexOf('TIMESTAMP') !== -1 || name.toUpperCase().indexOf('DATETIME') !== -1
            ) {
              doc[name] = new Date(data[i]);
            } else {
              doc[name] = data[i];
            }
          }
          //skip blank lines
          if (data.length !== columns.length) return callback();
          //console.log(doc);
          collection.insertOne(doc, function(err, result) {
            if (err) throw err;
            rowCount++;
            callback();
          });

        }
        mongoWriteStream.on('error', function(err) {
          cb(err);
        });
        mongoWriteStream.on('finish', function() {
          cb(null, rowCount, indexes);
        })
        var opfileRStream = opfile.createReadStream();
        opfileRStream.pipe(split()).pipe(mongoWriteStream);
      },
      function(rowCount, indexes, cb) {
        //create indexes
        function makeIndex(i, callback) {
          log.log('makeIndex: ' + i);
          var ndex = {};
          ndex[i] = 1;
          collection.createIndex(ndex, null, function(err, results) {
            if (err) return callback(err);
            callback(null);
          })
        }
        //TODO not making indexes?? WHY??
        async.map(indexes, makeIndex, function(err, results) {
          if (err) return cb(err);

          cb(null, rowCount);
        })
      },
      function(rowCount, cb) {
        collection.count(function(err, count) {
          log.log('Success! Inserted ' + rowCount + ' documents. MongoDB says ' + databaseName + '.' + collectionName + ' now has ' + count + ' docs.');
          cb(null, count);
        })
      }
    ],
    function(err, rows) {
      try {
        db.close();
      } catch (e) {
        return moduleCallback(e);
      }
      if (err) return moduleCallback(err);
      log.group('Finished destination').log(timer.str());
      moduleCallback(null, rows, cols);
    })

}
<|endoftext|>


<commit_before>const project = require('../config/project.config')
const server = require('../server/main')
const debug = require('debug')('app:bin:dev-server')

//For avoidong Heroku $PORT error
server.get('/', function(request, response) {
    res.render('index.html');
}).listen(process.env.PORT || 3000, function(err) {
	console.log('****************************************')
	console.log(err)
	console.log('****************************************')

	  debug(`Server is now running on :${process.env.PORT}.`)
})
<commit_msg>testing Heroky hosting...<commit_after>const project = require('../config/project.config')
const server = require('../server/main')
const debug = require('debug')('app:bin:dev-server')

//For avoidong Heroku $PORT error
server.get('/', function(request, response) {
    response.render('index.html');
}).listen(process.env.PORT || 3000, function(err) {
	console.log('****************************************')
	console.log(err)
	console.log('****************************************')

	  debug(`Server is now running on :${process.env.PORT}.`)
})
<|endoftext|>


<commit_before>var debug = require('debug')('iot-home:middlewares:middlewares');
var Middlewares = {};

Middlewares.init = function(app, host, username) {
  app.use(function(req, res, next) {
    debug('***** ' + req.method, req.url + ' *****');
    next();
  });

  Middlewares.hueLights = require('./hueLights').init(app, host, username);
  Middlewares.misc = require('./misc').init(app);

  app.get('/', function(req, res, next) {
    res.send('Hello');
    next();
  });
};

module.exports = Middlewares;
<commit_msg>set response headers to allow access outisde of origin for ajax<commit_after>var debug = require('debug')('iot-home:middlewares:middlewares');
var Middlewares = {};

Middlewares.init = function(app, host, username) {
  app.use(function(req, res, next) {
    debug('***** ' + req.method, req.url + ' *****');
    res.setHeader('Access-Control-Allow-Origin', '*');
    next();
  });

  Middlewares.hueLights = require('./hueLights').init(app, host, username);
  Middlewares.misc = require('./misc').init(app);

  app.get('/', function(req, res, next) {
    res.send('Hello');
    next();
  });
};

module.exports = Middlewares;
<|endoftext|>


<commit_before>function createTransform (inbound, outbound, config = {}) {
  let whitelist = config.whitelist || null
  let blacklist = config.blacklist || null

  function whitelistBlacklistCheck (key) {
    if (whitelist && whitelist.indexOf(key) === -1) return true
    if (blacklist && blacklist.indexOf(key) !== -1) return true
    return false
  }

  return {
    in: (state, key) => !whitelistBlacklistCheck(key) ? inbound(state, key) : state,
    out: (state, key) => !whitelistBlacklistCheck(key) ? outbound(state, key) : state
  }
}

export default createTransform
<commit_msg>[transform] allow null inbound/outbound functions to noop<commit_after>function createTransform (inbound, outbound, config = {}) {
  let whitelist = config.whitelist || null
  let blacklist = config.blacklist || null
  inbound = inbound || (state) => state
  outbound = outbound || (state) => state

  function whitelistBlacklistCheck (key) {
    if (whitelist && whitelist.indexOf(key) === -1) return true
    if (blacklist && blacklist.indexOf(key) !== -1) return true
    return false
  }

  return {
    in: (state, key) => !whitelistBlacklistCheck(key) ? inbound(state, key) : state,
    out: (state, key) => !whitelistBlacklistCheck(key) ? outbound(state, key) : state
  }
}

export default createTransform
<|endoftext|>


<commit_before>module.exports = (function() {

// Author: Matthew Forrester <matt_at_keyboardwritescode.com>
// Copyright: 2013 Matthew Forrester
// License: MIT/BSD-style

/**
 * ## SyncIt_ServerPersist_MongoDb
 *
 * ### Upgrade Procudure v0 -> v1
 *
 *     var r = db.dat.aggregate([ {$sort: {'_id.n': 1, '_id.s': 1}}, {$project: { _id: 0, s: '$_id.s', k: '$k', b: '$b', m: '$m', r: '$r', u: '$u', o: '$o', t: '$t', j: '$j' }} ]);
 *     db.syncit.insert(r.result);
 */

"use strict";

var CommonFuncs = require('./CommonFuncs.js');
var SyncIt_Constant = require('sync-it/Constant.js');
var getIncrementalNumber = require('./getIncrementalNumberFromMongodb');

/**
 * ### SyncIt_ServerPersist_MemoryAsync()
 *
 * Constructor.
 *
 * * **@param {Function} `cloneFunc`** The function to used for cloning.
 * * **@param {MongoskinConnection} `mongoskinConnection`** Must be connected.
 * * **@param {String} `ObjectID`** The ObjectId class from MongoDB / Mongoskin
 * * **@param {String} `dataCollection`** The name of the collection to store (accepted) data in.
 * * **@param {Function} `logger`** Optional function will will log information during DB insertions.
 */
var SyncIt_ServerPersist_MongoDb = function(cloneFunc, mongoskinConnection, ObjectID, dataCollection, logger) {
	this._cloneFunc = cloneFunc;
	this._ObjectID = ObjectID;
	this._dataCollection = dataCollection;
	this._mongoskinConnection = mongoskinConnection;
	this._logger = function() {};
	if (logger !== undefined) {
		this._logger = logger;
	}
	this._mongoskinConnection.collection(dataCollection).ensureIndex(
		{s: 1, k: 1, b: 1},
		{unique: 1, sparse: false},
		function(err,name) {
			if (err) {
				throw new Error("SyncIt: MongoDB: Index:", err, name);
			}
		}
	);
};

SyncIt_ServerPersist_MongoDb.prototype._find = function(collection, query, done) {
	this._mongoskinConnection.collection(collection).find(query).toArray(function(err, items) {
		if (err) {
			throw new Error("SyncIt: MongoDB: _find:", err, collection, query);
		}
		done(err, items);
	});
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getDatasetNames()
 *
 * #### Parameters
 *
 * * **@param {Function} `done`** Signature: `function (err, status, datasetNames)`
 *   * **@param {Errorcode} `done.err`**
 *   * **@param {Array} `done.datasetNames`** The names of all Dataset
 */
SyncIt_ServerPersist_MongoDb.prototype.getDatasetNames = function(done) {
	this._mongoskinConnection.collection(this._dataCollection).aggregate(
		{ $group: {'_id': '$s'} },
		function(e, res) {
			if (e) { return done(e); }
			var r = [],
				i, l;
			for (i=0, l=res.length; i<l; i++) {
				r.push(res[i]._id);
			}
			return done(null, r);
		}
	);
};

SyncIt_ServerPersist_MongoDb.prototype._unserialize = function(rec) {
	return {
		s: rec.s,
		k: rec.k,
		b: rec.b,
		m: rec.m,
		r: rec.r,
		u: JSON.parse(rec.u),
		o: rec.o,
		t: rec.t,
		j: rec.j
	};
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getQueueitems()
 *
 * #### Parameters
 *
 * * **@param {String} `dataset`**
 * * **@param {String|null} `fromSeqId`** Where to read Queueitem from (not inclusive).
 * * **@param {Function} `done`** Signature: `done(err, status, queueitems, lastQueueitemIdentifier)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Array} `done.queueitems`** An array of Queueitem
 *   * **@param {Object} `done.lastQueueitemIdentifier`** The internal reference of that last item, passing this to this function again will lead to continual reading.
 */
SyncIt_ServerPersist_MongoDb.prototype.getQueueitems = function(dataset, fromSeqId, done) {

	var q = {s: dataset},
		maxId = null;

	if (
		(typeof fromSeqId == 'string') &&
		((fromSeqId.length == 12) || (fromSeqId.length == 24))
	) {
		q._id = {$gt: this._ObjectID.createFromHexString(fromSeqId)};
		maxId = fromSeqId;
	}

	this._find( this._dataCollection, q, function(err, items) {
		if (err) { return done(err); }
		done(
			err,
			SyncIt_Constant.Error.OK,
			items.map(function(rec) {
				if ((maxId === null) || (rec._id > maxId)) {
					maxId = rec._id;
				}
				return this._unserialize(rec);
			}.bind(this)),
			maxId === null ? null : new this._ObjectID(maxId).toHexString()
		);
	}.bind(this));

};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getDatasetDatakeyVersion()
 *
 * #### Parameters
 *
 * * **@param {String} `dataset`** The *Dataset* you want to download the update for
 * * **@param {String} `datakey`** The *Datakey* you want to download the update for
 * * **@param {Number} `version`** The *Version* of the update you want to get
 * * **@param {Function} `done`** Signature: `done(err, status, queueitems, lastQueueitemIdentifier)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Object} `responder.data`** The change
 */
SyncIt_ServerPersist_MongoDb.prototype.getDatasetDatakeyVersion = function(dataset, datakey, version, done) {

	var q = {"s": dataset, "k": datakey, "b": parseInt(version,10)-1};

	this._find( this._dataCollection, q, function(err, items) {
		if (err) {
			if (err) { return done(err); }
		}
		if ( (items === null) || (items.length === 0) ) {
			return done(err, SyncIt_Constant.Error.NO_DATA_FOUND);
		}
		var i = items[0];
		i.u = JSON.parse(i.u);
		delete i._id;
		delete i.j.m;
		done(
			err,
			SyncIt_Constant.Error.OK,
			items[0]
		);
	}.bind(this));

};

SyncIt_ServerPersist_MongoDb.prototype.getLastQueueitem = function(dataset,datakey, done) {
	return this._getLast(dataset, datakey, {}, done);
};

SyncIt_ServerPersist_MongoDb.prototype._getLast = function(dataset, datakey, projection, done) {

	this._mongoskinConnection.collection(this._dataCollection).findOne(
		{s: dataset, k: datakey},
		projection,
		{sort:[['_id', -1]]},
		function(err, result) {
			if (err) { return done(err); }
			if (
				(result === null) ||
				(Array.isArray(result) && (result.length === 0))
			) {
				return done(err, SyncIt_Constant.Error.NO_DATA_FOUND, result);
			}
			if (Object.getOwnPropertyNames(projection).length !== 0) {
				return done(err, SyncIt_Constant.Error.OK, result);
			}
			return done(err, SyncIt_Constant.Error.OK, this._unserialize(result));
		}.bind(this)
	);
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getValue()
 *
 * #### Parameters
 *
 * * **@param {String} `dataset`**
 * * **@param {String} `datakey`**
 * * **@param {Function} `done`** Signature: `function (err, status, jrec)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Jrec} `done.jrec`** The result of all the Queueitem.
 */
SyncIt_ServerPersist_MongoDb.prototype.getValue = function(dataset, datakey, done) {
	this._getLast(dataset, datakey, {j: 1}, function(err, status, res) {
		if (err) { return done(err); }
		if (res.hasOwnProperty('j') && res.j.hasOwnProperty('i')) {
			return done(err, status, res.j.i);
		}
		return done(err, status);
	});
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.push()
 *
 * #### Parameters
 *
 * * **@param {Queueitem} `queueitem`**
 * * **@param {Function} `done`** Signature: `function (err, queueitem, jrec, seqId)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Queueitem} `done.queueitem`** The Queueitem passed in (successful or not)
 *   * **@param {Jrec} `done.jrec`** If successul, a Jrec, otherwise `undefined`
 *   * **@param {seqId} `done.seqId` The sequence within the dataset.
 */
SyncIt_ServerPersist_MongoDb.prototype.push = function(queueitem, done) {

	this._logger("SyncIt: MongoDB: Push:", queueitem);

	this._getLast(
		queueitem.s,
		queueitem.k,
		{},
		function(err, status, storedQueueitem) {

			if (err) { return done(err); }

			this._logger("SyncIt: MongoDB: Existing:", storedQueueitem);

			var result = CommonFuncs.getResultingJrecBasedOnOld(
				this._cloneFunc,
				storedQueueitem,
				queueitem
			);

			this._logger("SyncIt: MongoDB: Proposed:",result);

			if (result.status) {
				return(done(err, result.status, result.resultingJrec));
			}

			var toWrite = {
				s: queueitem.s,
				k: queueitem.k,
				b: queueitem.b,
				m: queueitem.m,
				r: queueitem.r,
				u: JSON.stringify(queueitem.u),
				o: queueitem.o,
				t: queueitem.t,
				j: result.resultingJrec
			};

			this._logger("SyncIt: MongoDB: ToWrite:", toWrite);

			this._mongoskinConnection.collection(this._dataCollection).insert(
				toWrite,
				{journal: true},
				function(mongoErr, doc) {

					this._logger("SyncIt: MongoDB: Insert:", mongoErr);

					if (mongoErr) {
						if ( // dupe key means someone else inserted before us...
							mongoErr.hasOwnProperty('code') &&
							mongoErr.code == 11000
						) {
							return done(null, SyncIt_Constant.Error.TRYING_TO_ADD_QUEUEITEM_BASED_ON_OLD_VERSION);
						}
						return done(err);
					}

					return done(
						err,
						SyncIt_Constant.Error.OK,
						queueitem,
						result.resultingJrec,
						new this._ObjectID(doc._id).toHexString()
					);

				}.bind(this)
			);

		}.bind(this)
	);

};

return SyncIt_ServerPersist_MongoDb;

})();
<commit_msg>order data coming from mongodb by _id<commit_after>module.exports = (function() {

// Author: Matthew Forrester <matt_at_keyboardwritescode.com>
// Copyright: 2013 Matthew Forrester
// License: MIT/BSD-style

/**
 * ## SyncIt_ServerPersist_MongoDb
 *
 * ### Upgrade Procudure v0 -> v1
 *
 *     var r = db.dat.aggregate([ {$sort: {'_id.n': 1, '_id.s': 1}}, {$project: { _id: 0, s: '$_id.s', k: '$k', b: '$b', m: '$m', r: '$r', u: '$u', o: '$o', t: '$t', j: '$j' }} ]);
 *     db.syncit.insert(r.result);
 */

"use strict";

var CommonFuncs = require('./CommonFuncs.js');
var SyncIt_Constant = require('sync-it/Constant.js');
var getIncrementalNumber = require('./getIncrementalNumberFromMongodb');

/**
 * ### SyncIt_ServerPersist_MemoryAsync()
 *
 * Constructor.
 *
 * * **@param {Function} `cloneFunc`** The function to used for cloning.
 * * **@param {MongoskinConnection} `mongoskinConnection`** Must be connected.
 * * **@param {String} `ObjectID`** The ObjectId class from MongoDB / Mongoskin
 * * **@param {String} `dataCollection`** The name of the collection to store (accepted) data in.
 * * **@param {Function} `logger`** Optional function will will log information during DB insertions.
 */
var SyncIt_ServerPersist_MongoDb = function(cloneFunc, mongoskinConnection, ObjectID, dataCollection, logger) {
	this._cloneFunc = cloneFunc;
	this._ObjectID = ObjectID;
	this._dataCollection = dataCollection;
	this._mongoskinConnection = mongoskinConnection;
	this._logger = function() {};
	if (logger !== undefined) {
		this._logger = logger;
	}
	this._mongoskinConnection.collection(dataCollection).ensureIndex(
		{s: 1, k: 1, b: 1},
		{unique: 1, sparse: false},
		function(err,name) {
			if (err) {
				throw new Error("SyncIt: MongoDB: Index:", err, name);
			}
		}
	);
};

SyncIt_ServerPersist_MongoDb.prototype._find = function(collection, query, done) {
	this._mongoskinConnection.collection(collection).find(query).sort({_id: 1}).toArray(function(err, items) {
		if (err) {
			throw new Error("SyncIt: MongoDB: _find:", err, collection, query);
		}
		done(err, items);
	});
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getDatasetNames()
 *
 * #### Parameters
 *
 * * **@param {Function} `done`** Signature: `function (err, status, datasetNames)`
 *   * **@param {Errorcode} `done.err`**
 *   * **@param {Array} `done.datasetNames`** The names of all Dataset
 */
SyncIt_ServerPersist_MongoDb.prototype.getDatasetNames = function(done) {
	this._mongoskinConnection.collection(this._dataCollection).aggregate(
		{ $group: {'_id': '$s'} },
		function(e, res) {
			if (e) { return done(e); }
			var r = [],
				i, l;
			for (i=0, l=res.length; i<l; i++) {
				r.push(res[i]._id);
			}
			return done(null, r);
		}
	);
};

SyncIt_ServerPersist_MongoDb.prototype._unserialize = function(rec) {
	return {
		s: rec.s,
		k: rec.k,
		b: rec.b,
		m: rec.m,
		r: rec.r,
		u: JSON.parse(rec.u),
		o: rec.o,
		t: rec.t,
		j: rec.j
	};
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getQueueitems()
 *
 * #### Parameters
 *
 * * **@param {String} `dataset`**
 * * **@param {String|null} `fromSeqId`** Where to read Queueitem from (not inclusive).
 * * **@param {Function} `done`** Signature: `done(err, status, queueitems, lastQueueitemIdentifier)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Array} `done.queueitems`** An array of Queueitem
 *   * **@param {Object} `done.lastQueueitemIdentifier`** The internal reference of that last item, passing this to this function again will lead to continual reading.
 */
SyncIt_ServerPersist_MongoDb.prototype.getQueueitems = function(dataset, fromSeqId, done) {

	var q = {s: dataset},
		maxId = null;

	if (
		(typeof fromSeqId == 'string') &&
		((fromSeqId.length == 12) || (fromSeqId.length == 24))
	) {
		q._id = {$gt: this._ObjectID.createFromHexString(fromSeqId)};
		maxId = fromSeqId;
	}

	this._find( this._dataCollection, q, function(err, items) {
		if (err) { return done(err); }
		done(
			err,
			SyncIt_Constant.Error.OK,
			items.map(function(rec) {
				if ((maxId === null) || (rec._id > maxId)) {
					maxId = rec._id;
				}
				return this._unserialize(rec);
			}.bind(this)),
			maxId === null ? null : new this._ObjectID(maxId).toHexString()
		);
	}.bind(this));

};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getDatasetDatakeyVersion()
 *
 * #### Parameters
 *
 * * **@param {String} `dataset`** The *Dataset* you want to download the update for
 * * **@param {String} `datakey`** The *Datakey* you want to download the update for
 * * **@param {Number} `version`** The *Version* of the update you want to get
 * * **@param {Function} `done`** Signature: `done(err, status, queueitems, lastQueueitemIdentifier)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Object} `responder.data`** The change
 */
SyncIt_ServerPersist_MongoDb.prototype.getDatasetDatakeyVersion = function(dataset, datakey, version, done) {

	var q = {"s": dataset, "k": datakey, "b": parseInt(version,10)-1};

	this._find( this._dataCollection, q, function(err, items) {
		if (err) {
			if (err) { return done(err); }
		}
		if ( (items === null) || (items.length === 0) ) {
			return done(err, SyncIt_Constant.Error.NO_DATA_FOUND);
		}
		var i = items[0];
		i.u = JSON.parse(i.u);
		delete i._id;
		delete i.j.m;
		done(
			err,
			SyncIt_Constant.Error.OK,
			items[0]
		);
	}.bind(this));

};

SyncIt_ServerPersist_MongoDb.prototype.getLastQueueitem = function(dataset,datakey, done) {
	return this._getLast(dataset, datakey, {}, done);
};

SyncIt_ServerPersist_MongoDb.prototype._getLast = function(dataset, datakey, projection, done) {

	this._mongoskinConnection.collection(this._dataCollection).findOne(
		{s: dataset, k: datakey},
		projection,
		{sort:[['_id', -1]]},
		function(err, result) {
			if (err) { return done(err); }
			if (
				(result === null) ||
				(Array.isArray(result) && (result.length === 0))
			) {
				return done(err, SyncIt_Constant.Error.NO_DATA_FOUND, result);
			}
			if (Object.getOwnPropertyNames(projection).length !== 0) {
				return done(err, SyncIt_Constant.Error.OK, result);
			}
			return done(err, SyncIt_Constant.Error.OK, this._unserialize(result));
		}.bind(this)
	);
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.getValue()
 *
 * #### Parameters
 *
 * * **@param {String} `dataset`**
 * * **@param {String} `datakey`**
 * * **@param {Function} `done`** Signature: `function (err, status, jrec)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Jrec} `done.jrec`** The result of all the Queueitem.
 */
SyncIt_ServerPersist_MongoDb.prototype.getValue = function(dataset, datakey, done) {
	this._getLast(dataset, datakey, {j: 1}, function(err, status, res) {
		if (err) { return done(err); }
		if (res.hasOwnProperty('j') && res.j.hasOwnProperty('i')) {
			return done(err, status, res.j.i);
		}
		return done(err, status);
	});
};

/**
 * ### SyncIt_ServerPersist_MemoryAsync.push()
 *
 * #### Parameters
 *
 * * **@param {Queueitem} `queueitem`**
 * * **@param {Function} `done`** Signature: `function (err, queueitem, jrec, seqId)`
 *   * **@param {Number} `done.err`**
 *   * **@param {Number} `done.status`** See SyncIt_Constant.Error
 *   * **@param {Queueitem} `done.queueitem`** The Queueitem passed in (successful or not)
 *   * **@param {Jrec} `done.jrec`** If successul, a Jrec, otherwise `undefined`
 *   * **@param {seqId} `done.seqId` The sequence within the dataset.
 */
SyncIt_ServerPersist_MongoDb.prototype.push = function(queueitem, done) {

	this._logger("SyncIt: MongoDB: Push:", queueitem);

	this._getLast(
		queueitem.s,
		queueitem.k,
		{},
		function(err, status, storedQueueitem) {

			if (err) { return done(err); }

			this._logger("SyncIt: MongoDB: Existing:", storedQueueitem);

			var result = CommonFuncs.getResultingJrecBasedOnOld(
				this._cloneFunc,
				storedQueueitem,
				queueitem
			);

			this._logger("SyncIt: MongoDB: Proposed:",result);

			if (result.status) {
				return(done(err, result.status, result.resultingJrec));
			}

			var toWrite = {
				s: queueitem.s,
				k: queueitem.k,
				b: queueitem.b,
				m: queueitem.m,
				r: queueitem.r,
				u: JSON.stringify(queueitem.u),
				o: queueitem.o,
				t: queueitem.t,
				j: result.resultingJrec
			};

			this._logger("SyncIt: MongoDB: ToWrite:", toWrite);

			this._mongoskinConnection.collection(this._dataCollection).insert(
				toWrite,
				{journal: true},
				function(mongoErr, doc) {

					this._logger("SyncIt: MongoDB: Insert:", mongoErr);

					if (mongoErr) {
						if ( // dupe key means someone else inserted before us...
							mongoErr.hasOwnProperty('code') &&
							mongoErr.code == 11000
						) {
							return done(null, SyncIt_Constant.Error.TRYING_TO_ADD_QUEUEITEM_BASED_ON_OLD_VERSION);
						}
						return done(err);
					}

					return done(
						err,
						SyncIt_Constant.Error.OK,
						queueitem,
						result.resultingJrec,
						new this._ObjectID(doc._id).toHexString()
					);

				}.bind(this)
			);

		}.bind(this)
	);

};

return SyncIt_ServerPersist_MongoDb;

})();
<|endoftext|>


<commit_before>import {resolve} from 'path'

const pkg = require(resolve('package')),
      {dependencies = {}} = pkg,
      {devDependencies = {}} = pkg,
      {optionalDependencies = {}} = pkg,
      {peerDependencies = {}} = pkg,
      keys1 = Object.keys(dependencies),
      keys2 = Object.keys(devDependencies),
      keys3 = Object.keys(optionalDependencies),
      keys4 = Object.keys(peerDependencies),
      modules = [...keys1, ...keys2, ...keys3, ...keys4],
      browserPlugins = {
        'karma-chrome-launcher': 'Chrome',
        'karma-firefox-launcher': 'Firefox',
        'karma-ie-launcher': 'IE',
        'karma-phantomjs-launcher': 'PhantomJS',
        'karma-safari-launcher': 'Safari',
      },
      frameworkPlugins = {
        'karma-detect-browsers': 'detectBrowsers',
        'karma-jasmine': 'jasmine',
        'karma-mocha': 'mocha',
        'karma-qunit': 'qunit',
      }
let browsers = Object.keys(browserPlugins)
      .filter(moduleExists)
      .map(key => browserPlugins[key]),
    frameworks = Object.keys(frameworkPlugins)
      .filter(moduleExists)
      .map(key => frameworkPlugins[key])

// If user did not specify any browsers, use PhantomJS
if(!browsers.length && !frameworks.includes('detectBrowsers')) {
  browsers = ['PhantomJS']
}

// If user did not specify any frameworks, use Mocha
if(!frameworks.length) {
  frameworks = ['mocha']
}

export {browsers, frameworks}

function moduleExists(mod) {
  if(modules.includes(mod)) {
    try {
      require.resolve(mod)
      return true
    }
    catch(e) {
      return false
    }
  }
  return false
}
<commit_msg>Fix to adjust to babel-runtime<commit_after>import {resolve} from 'path'

const pkg = require(resolve('package')),
      {dependencies = {}} = pkg,
      {devDependencies = {}} = pkg,
      {optionalDependencies = {}} = pkg,
      {peerDependencies = {}} = pkg,
      keys1 = Object.keys(dependencies),
      keys2 = Object.keys(devDependencies),
      keys3 = Object.keys(optionalDependencies),
      keys4 = Object.keys(peerDependencies),
      modules = [...keys1, ...keys2, ...keys3, ...keys4],
      browserPlugins = {
        'karma-chrome-launcher': 'Chrome',
        'karma-firefox-launcher': 'Firefox',
        'karma-ie-launcher': 'IE',
        'karma-phantomjs-launcher': 'PhantomJS',
        'karma-safari-launcher': 'Safari',
      },
      frameworkPlugins = {
        'karma-detect-browsers': 'detectBrowsers',
        'karma-jasmine': 'jasmine',
        'karma-mocha': 'mocha',
        'karma-qunit': 'qunit',
      }
let browsers = Object.keys(browserPlugins)
      .filter(moduleExists)
      .map(key => browserPlugins[key]),
    frameworks = Object.keys(frameworkPlugins)
      .filter(moduleExists)
      .map(key => frameworkPlugins[key])

// If user did not specify any browsers, use PhantomJS
if(!browsers.length && !~frameworks.indexOf('detectBrowsers')) {
  browsers = ['PhantomJS']
}

// If user did not specify any frameworks, use Mocha
if(!frameworks.length) {
  frameworks = ['mocha']
}

export {browsers, frameworks}

function moduleExists(mod) {
  if(~modules.indexOf(mod)) {
    try {
      require.resolve(mod)
      return true
    }
    catch(e) {
      return false
    }
  }
  return false
}
<|endoftext|>


<commit_before>import RelayQuery from "react-relay/lib/RelayQuery"
import RelayMetaRoute from "react-relay/lib/RelayMetaRoute"
import GraphQL from "react-relay/lib/GraphQL"

import curry from "./util/curry"

export default curry((q, variables) => {
  return (fragment) => {
    const baseQuery =
      new GraphQL.Query(
        q.fieldName,
        (q.calls[0] && q.calls[0].value) || null,
        q.fields,
        [fragment],
        q.metadata,
        q.name
      )

    const rootQuery =
      RelayQuery.Root.create(
        baseQuery,
        RelayMetaRoute.get(q.name),
        variables || {}
      )

    return {
      root: rootQuery,
      base: baseQuery,
      variables
    }
  }
})
<commit_msg>Add query name check<commit_after>import RelayQuery from "react-relay/lib/RelayQuery"
import RelayMetaRoute from "react-relay/lib/RelayMetaRoute"
import GraphQL from "react-relay/lib/GraphQL"

import curry from "./util/curry"

export default curry((q, variables) => {
  return (fragment) => {
    if (!q.name) {
      throw new Error("Missing query name! Relei supports only named queries")
    }

    const baseQuery =
      new GraphQL.Query(
        q.fieldName,
        (q.calls[0] && q.calls[0].value) || null,
        q.fields,
        [fragment],
        q.metadata,
        q.name
      )

    const rootQuery =
      RelayQuery.Root.create(
        baseQuery,
        RelayMetaRoute.get(q.name),
        variables || {}
      )

    return {
      root: rootQuery,
      base: baseQuery,
      variables
    }
  }
})
<|endoftext|>