<commit_before>const Board = require('./board')

let equals = v => w => w[0] == v[0] && w[1] == v[1]

function hasNLiberties(board, vertex, N, visited = [], count = 0, sign = null) {
    let key = vertex.join(',')
    if (visited.includes(key)) return false
    if (sign == null) sign = board.get(vertex)

    let neighbors = board.getNeighbors(vertex)
    let freeNeighbors = []
    let friendlyNeighbors = []

    for (let i = 0; i < neighbors.length; i++) {
        let n = neighbors[i]
        let s = board.get(n)

        if (s == 0) freeNeighbors.push(n)
        else if (s == sign) friendlyNeighbors.push(n)
    }

    count += freeNeighbors.length
    if (count >= N) return true

    visited.push(key)
    return friendlyNeighbors.some(n => hasNLiberties(board, n, N, visited, count, sign))
}

function makePseudoMove(board, sign, vertex) {
    let neighbors = board.getNeighbors(vertex)
    let neighborSigns = neighbors.map(n => board.get(n))

    if (neighborSigns.every(s => s == sign)) {
        return null
    }

    board.set(vertex, sign)

    let hasEnemyNeighbors = neighborSigns.some(s => s == -sign)
    let checkCapture = false

    if (!hasNLiberties(board, vertex, 2)) {
        if (!hasEnemyNeighbors) {
            board.set(vertex, 0)
            return null
        } else {
            checkCapture = true
        }
    }

    let dead = []

    for (let i = 0; i < neighbors.length; i++) {
        let n = neighbors[i]
        if (neighborSigns[i] != -sign || hasNLiberties(board, n, 1))
            continue

        let chain = board.getChain(n)
        dead.push(...chain)
        chain.forEach(c => board.set(c, 0))
    }

    if (checkCapture && dead.length == 0) {
        board.set(vertex, 0)
        return null
    }

    return dead
}

function fixHoles(board) {
    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            let vertex = [x, y]

            if (board.get(vertex) != 0)
                continue

            let neighbors = board.getNeighbors(vertex)
            let sign = board.get(neighbors[0])
            let fix = true

            for (let i = 1; i < neighbors.length; i++) {
                let n = neighbors[i]

                if (board.get(n) != sign) {
                    fix = false
                    break
                }
            }

            if (fix) board.set(vertex, sign)
        }
    }

    return board
}

exports.guess = function(board, scoring = false, iterations = 50) {
    let map = exports.getProbabilityMap(board, iterations)
    let done = {}
    let result = []

    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            let vertex = [x, y]
            let sign = board.get(vertex)

            if (sign == 0 || vertex in done) continue

            let chain = board.getChain(vertex)
            let probability = chain.map(v => map[v]).reduce((sum, x) => sum + x) / chain.length
            let newSign = probability < 0.5 ? -1 : probability > 0.5 ? 1 : 0

            if (newSign == -sign) result.push(...chain)

            done[vertex] = true
        }
    }

    if (scoring) {
        let floating = exports.getFloatingStones(board)

        for (let v of floating) {
            if (!result.some(equals(v))) result.push(v)
        }
    }

    return result
}

exports.getFloatingStones = function(board) {
    let map = board.getAreaMap()
    let done = {}
    let result = []

    for (let i = 0; i < board.width; i++) {
        for (let j = 0; j < board.height; j++) {
            let vertex = [i, j]
            if (map[vertex] != 0 || vertex in done) continue

            let posArea = board.getConnectedComponent(vertex, [0, -1])
            let negArea = board.getConnectedComponent(vertex, [0, 1])
            let posDead = posArea.filter(v => board.get(v) == -1)
            let negDead = negArea.filter(v => board.get(v) == 1)
            let posDiff = posArea.filter(v => !posDead.some(equals(v)) && !negArea.some(equals(v)))
            let negDiff = negArea.filter(v => !negDead.some(equals(v)) && !posArea.some(equals(v)))

            let sign = 0
            let actualArea, actualDead

            if (negDiff.length <= 1 && negDead.length <= posDead.length) {
                sign--
                actualArea = negArea
                actualDead = negDead
            }

            if (posDiff.length <= 1 && posDead.length <= negDead.length) {
                sign++
                actualArea = posArea
                actualDead = posDead
            }

            if (sign == 0) {
                actualArea = board.getChain(vertex)
                actualDead = []
            }

            actualArea.forEach(v => done[v] = true)
            result.push(...actualDead)
        }
    }

    return result
}

exports.playTillEnd = function(board, sign, iterations = null) {
    if (iterations == null) iterations = board.width * board.height
    board = board.clone()

    let freeVertices = []
    let illegalVertices = []

    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            if (board.get([x, y]) != 0) continue
            freeVertices.push([x, y])
        }
    }

    let finished = {'-1': false, '1': false}

    while (iterations > 0) {
        if (freeVertices.length == 0 || finished[-sign] && finished[sign])
            break

        let madeMove = false

        while (freeVertices.length > 0) {
            let randomIndex = Math.floor(Math.random() * freeVertices.length)
            let vertex = freeVertices[randomIndex]
            let freedVertices = makePseudoMove(board, sign, vertex, false)

            freeVertices.splice(randomIndex, 1)

            if (freedVertices != null) {
                freeVertices.push(...freedVertices)

                finished[-sign] = false
                madeMove = true

                break
            } else {
                illegalVertices.push(vertex)
            }
        }

        finished[sign] = !madeMove

        freeVertices.push(...illegalVertices)
        illegalVertices.length = 0

        sign = -sign
        iterations--
    }

    return fixHoles(board).arrangement
}

exports.getProbabilityMap = function(board, iterations) {
    let pmap = []
    let nmap = []
    let result = {}

    for (let i = 0; i < iterations; i++) {
        let sign = Math.sign(Math.random() - 0.5)
        let areaMap = exports.playTillEnd(board, sign)

        for (let j = 0; j < areaMap.length; j++) {
            if (!(j in pmap)) pmap[j] = 0
            if (!(j in nmap)) nmap[j] = 0

            if (areaMap[j] < 0) nmap[j]++
            else if (areaMap[j] > 0) pmap[j]++
        }
    }

    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            let v = [x, y]
            let j = board.vertex2index(v)

            if (pmap[j] + nmap[j] == 0) result[v] = 0.5
            else result[v] = pmap[j] / (pmap[j] + nmap[j])
        }
    }

    return result
}
<commit_msg>Make hasNPseudoLiberties() more performant<commit_after>const Board = require('./board')

let equals = v => w => w[0] == v[0] && w[1] == v[1]

function hasNPseudoLiberties(board, vertex, N, visited = [], count = 0, sign = null) {
    let key = vertex.join(',')
    if (visited.includes(key)) return false
    if (sign == null) sign = board.get(vertex)

    let neighbors = board.getNeighbors(vertex)
    let friendlyNeighbors = []

    for (let i = 0; i < neighbors.length; i++) {
        let n = neighbors[i]
        let s = board.get(n)

        if (s == 0) {
            count++
            if (count >= N) return true
        } else if (s == sign) {
            friendlyNeighbors.push(n)
        }
    }

    visited.push(key)
    return friendlyNeighbors.some(n => hasNPseudoLiberties(board, n, N, visited, count, sign))
}

function makePseudoMove(board, sign, vertex) {
    let neighbors = board.getNeighbors(vertex)
    let neighborSigns = neighbors.map(n => board.get(n))
    if (neighborSigns.every(s => s == sign)) return null

    board.set(vertex, sign)

    let checkCapture = false

    if (!hasNPseudoLiberties(board, vertex, 2)) {
        if (!neighborSigns.some(s => s == -sign)) {
            board.set(vertex, 0)
            return null
        } else {
            checkCapture = true
        }
    }

    let dead = []

    for (let i = 0; i < neighbors.length; i++) {
        let n = neighbors[i]
        if (neighborSigns[i] != -sign || hasNPseudoLiberties(board, n, 1))
            continue

        let chain = board.getChain(n)
        dead.push(...chain)
        chain.forEach(c => board.set(c, 0))
    }

    if (checkCapture && dead.length == 0) {
        board.set(vertex, 0)
        return null
    }

    return dead
}

function fixHoles(board) {
    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            let vertex = [x, y]
            if (board.get(vertex) != 0) continue

            let neighbors = board.getNeighbors(vertex)
            let neighborSigns = neighbors.map(n => board.get(n))
            let fix = true

            for (let i = 1; i < neighbors.length; i++) {
                let n = neighbors[i]

                if (neighborSigns[i] != neighborSigns[0]) {
                    fix = false
                    break
                }
            }

            if (fix) board.set(vertex, neighborSigns[0])
        }
    }

    return board
}

exports.guess = function(board, scoring = false, iterations = 50) {
    let map = exports.getProbabilityMap(board, iterations)
    let done = {}
    let result = []

    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            let vertex = [x, y]
            let sign = board.get(vertex)

            if (sign == 0 || vertex in done) continue

            let chain = board.getChain(vertex)
            let probability = chain.map(v => map[v]).reduce((sum, x) => sum + x) / chain.length
            let newSign = probability < 0.5 ? -1 : probability > 0.5 ? 1 : 0

            if (newSign == -sign) result.push(...chain)

            done[vertex] = true
        }
    }

    if (scoring) {
        let floating = exports.getFloatingStones(board)

        for (let v of floating) {
            if (!result.some(equals(v))) result.push(v)
        }
    }

    return result
}

exports.getFloatingStones = function(board) {
    let map = board.getAreaMap()
    let done = {}
    let result = []

    for (let i = 0; i < board.width; i++) {
        for (let j = 0; j < board.height; j++) {
            let vertex = [i, j]
            if (map[vertex] != 0 || vertex in done) continue

            let posArea = board.getConnectedComponent(vertex, [0, -1])
            let negArea = board.getConnectedComponent(vertex, [0, 1])
            let posDead = posArea.filter(v => board.get(v) == -1)
            let negDead = negArea.filter(v => board.get(v) == 1)
            let posDiff = posArea.filter(v => !posDead.some(equals(v)) && !negArea.some(equals(v)))
            let negDiff = negArea.filter(v => !negDead.some(equals(v)) && !posArea.some(equals(v)))

            let sign = 0
            let actualArea, actualDead

            if (negDiff.length <= 1 && negDead.length <= posDead.length) {
                sign--
                actualArea = negArea
                actualDead = negDead
            }

            if (posDiff.length <= 1 && posDead.length <= negDead.length) {
                sign++
                actualArea = posArea
                actualDead = posDead
            }

            if (sign == 0) {
                actualArea = board.getChain(vertex)
                actualDead = []
            }

            actualArea.forEach(v => done[v] = true)
            result.push(...actualDead)
        }
    }

    return result
}

exports.playTillEnd = function(board, sign, iterations = null) {
    if (iterations == null) iterations = board.width * board.height
    board = board.clone()

    let freeVertices = []
    let illegalVertices = []

    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            if (board.get([x, y]) != 0) continue
            freeVertices.push([x, y])
        }
    }

    let finished = {'-1': false, '1': false}

    while (iterations > 0) {
        if (freeVertices.length == 0 || finished[-sign] && finished[sign])
            break

        let madeMove = false

        while (freeVertices.length > 0) {
            let randomIndex = Math.floor(Math.random() * freeVertices.length)
            let vertex = freeVertices[randomIndex]
            let freedVertices = makePseudoMove(board, sign, vertex, false)

            freeVertices.splice(randomIndex, 1)

            if (freedVertices != null) {
                freeVertices.push(...freedVertices)

                finished[-sign] = false
                madeMove = true

                break
            } else {
                illegalVertices.push(vertex)
            }
        }

        finished[sign] = !madeMove

        freeVertices.push(...illegalVertices)
        illegalVertices.length = 0

        sign = -sign
        iterations--
    }

    return fixHoles(board).arrangement
}

exports.getProbabilityMap = function(board, iterations) {
    let pmap = []
    let nmap = []
    let result = {}

    for (let i = 0; i < iterations; i++) {
        let sign = Math.sign(Math.random() - 0.5)
        let areaMap = exports.playTillEnd(board, sign)

        for (let j = 0; j < areaMap.length; j++) {
            if (!(j in pmap)) pmap[j] = 0
            if (!(j in nmap)) nmap[j] = 0

            if (areaMap[j] < 0) nmap[j]++
            else if (areaMap[j] > 0) pmap[j]++
        }
    }

    for (let x = 0; x < board.width; x++) {
        for (let y = 0; y < board.height; y++) {
            let v = [x, y]
            let j = board.vertex2index(v)

            if (pmap[j] + nmap[j] == 0) result[v] = 0.5
            else result[v] = pmap[j] / (pmap[j] + nmap[j])
        }
    }

    return result
}
<|endoftext|>


<commit_before>(function(global) {
	global.eg = {};

	var dependency = {
		"jQuery": {
			"url": "http://jquery.com/"
		},
		"Hammer": {
			"url": "http://hammerjs.github.io/"
		},
		"Outlayer": {
			"url": "https://github.com/metafizzy/outlayer/"
		}
	};

	// jscs:disable maximumLineLength
	var templateMessage = [
		"[egjs] The {{name}} library must be loaded before {{componentName}}.",
		"[egjs] For AMD environment (like RequireJS), \"{{name}}\" must be declared, which is required by {{componentName}}.",
        "[egjs] The {{index}} argument of {{componentName}} is missing.\n\rDownload {{name}} from [{{url}}].",
		"[egjs] The {{name}} parameter of {{componentName}} is not valid.\n\rPlease check and try again.",
        "[egjs] The {{index}} argument of {{componentName}} is undefined.\n\rPlease check and try again."
	];

	// jscs:enable maximumLineLength

	var ordinal = [ "1st", "2nd", "3rd"];

	function changeOrdinal(index) {
		return index > 2 ? (index + 1) + "th" : ordinal[index];
	}

	function replaceStr(str, obj) {
		var i;
		for (i in obj) {
			str = str.replace(new RegExp("{{" + i + "}}","gi"), obj[i]);
		}
		return str;
	}

	function checkDependency(componentName, di) {
		var i = 0;
		var l = di.length;
		var message = [];
		var paramList = [];
		var require = global.require;
		var dependencyInfo;
		var param;
		var messageInfo;
		var isString;
		var isUndefined;
		var registedDependency;
		var isNotGlobal;
		var specifiedAMD;

		for (; i < l; i++) {
			param = di[i];
			messageInfo = {
				"index": changeOrdinal(i),
				"name": param,
				"componentName": componentName
			};

			isString = typeof di[i] === "string";
			isUndefined = di[i] === undefined;
			registedDependency = isString && (dependencyInfo = dependency[di[i]]);
			isNotGlobal = isString && dependencyInfo && !global[di[i]];
			specifiedAMD = isNotGlobal && require && require.specified(di[i]);

			// Message decision flow
			//             argument
			// |--------------|--------------|
			// undefined    string    !string&&!undefined
			// |              |              |
			// msg(4)         |             (OK)
			//         defined dependency
			//                |
			// |-----------------------------|
			// |                             |
			// msg(3)                     in global
			//                               |
			//                 |------------------------------|
			//              use AMD                          (OK)
			//                 |
			//  |------------------------------|
			//  msg(2)                  require.specified
			// 	                               |
			// 	                |------------------------------|
			//                  msg(1)                  require.defined
			// 	                                               |
			//                                  |------------------------------|
			//                                  msg(0)                        (OK)

			if (!isString && !isUndefined) {
				paramList.push(param);
				continue;
			}

			if (specifiedAMD && require.defined(di[i])) {
				param = require(di[i]);
				paramList.push(param);
				continue;
			}

			if (specifiedAMD && !require.defined(di[i])) {
				messageInfo.url = dependencyInfo.url;
				message.push(replaceStr(templateMessage[0], messageInfo));
				continue;
			}

			if (isNotGlobal && require && !require.specified(di[i])) {
				messageInfo.url = dependencyInfo.url;
				message.push(replaceStr(templateMessage[1], messageInfo));
				continue;
			}

			if (isNotGlobal && !require) {
				messageInfo.url = dependencyInfo.url;
				message.push(replaceStr(templateMessage[2], messageInfo));
				continue;
			}

			if (registedDependency && global[di[i]]) {
				param = global[di[i]];
				paramList.push(param);
				continue;
			}

			if (isString && !dependencyInfo) {
				message.push(replaceStr(templateMessage[3], messageInfo));
				continue;
			}

			if (di[i] === undefined) {
				message.push(replaceStr(templateMessage[4], messageInfo));
				continue;
			}
		}

		return [paramList, message];
	}

	/**
	 * Regist module.
	 * @private
	 */
	global.eg.module = function(name, di, fp) {
		var result = checkDependency(name, di);

		if (result[1].length) {
			throw new Error(result[1].join("\n\r"));
		} else {
			fp.apply(global, result[0]);
		}
	};
})(window);<commit_msg>refactor(module): fixed namespace in module.<commit_after>(function(global, ns) {
	global[ns] = {};

	var dependency = {
		"jQuery": {
			"url": "http://jquery.com/"
		},
		"Hammer": {
			"url": "http://hammerjs.github.io/"
		},
		"Outlayer": {
			"url": "https://github.com/metafizzy/outlayer/"
		}
	};

	// jscs:disable maximumLineLength
	var templateMessage = [
		"[egjs] The {{name}} library must be loaded before {{componentName}}.",
		"[egjs] For AMD environment (like RequireJS), \"{{name}}\" must be declared, which is required by {{componentName}}.",
        "[egjs] The {{index}} argument of {{componentName}} is missing.\n\rDownload {{name}} from [{{url}}].",
		"[egjs] The {{name}} parameter of {{componentName}} is not valid.\n\rPlease check and try again.",
        "[egjs] The {{index}} argument of {{componentName}} is undefined.\n\rPlease check and try again."
	];

	// jscs:enable maximumLineLength

	var ordinal = [ "1st", "2nd", "3rd"];

	function changeOrdinal(index) {
		return index > 2 ? (index + 1) + "th" : ordinal[index];
	}

	function replaceStr(str, obj) {
		var i;
		for (i in obj) {
			str = str.replace(new RegExp("{{" + i + "}}","gi"), obj[i]);
		}
		return str;
	}

	function checkDependency(componentName, di) {
		var i = 0;
		var l = di.length;
		var message = [];
		var paramList = [];
		var require = global.require;
		var dependencyInfo;
		var param;
		var messageInfo;
		var isString;
		var isUndefined;
		var registedDependency;
		var isNotGlobal;
		var specifiedAMD;

		for (; i < l; i++) {
			param = di[i];
			messageInfo = {
				"index": changeOrdinal(i),
				"name": param,
				"componentName": componentName
			};

			isString = typeof di[i] === "string";
			isUndefined = di[i] === undefined;
			registedDependency = isString && (dependencyInfo = dependency[di[i]]);
			isNotGlobal = isString && dependencyInfo && !global[di[i]];
			specifiedAMD = isNotGlobal && require && require.specified(di[i]);

			// Message decision flow
			//             argument
			// |--------------|--------------|
			// undefined    string    !string&&!undefined
			// |              |              |
			// msg(4)         |             (OK)
			//         defined dependency
			//                |
			// |-----------------------------|
			// |                             |
			// msg(3)                     in global
			//                               |
			//                 |------------------------------|
			//              use AMD                          (OK)
			//                 |
			//  |------------------------------|
			//  msg(2)                  require.specified
			// 	                               |
			// 	                |------------------------------|
			//                  msg(1)                  require.defined
			// 	                                               |
			//                                  |------------------------------|
			//                                  msg(0)                        (OK)

			if (!isString && !isUndefined) {
				paramList.push(param);
				continue;
			}

			if (specifiedAMD && require.defined(di[i])) {
				param = require(di[i]);
				paramList.push(param);
				continue;
			}

			if (specifiedAMD && !require.defined(di[i])) {
				messageInfo.url = dependencyInfo.url;
				message.push(replaceStr(templateMessage[0], messageInfo));
				continue;
			}

			if (isNotGlobal && require && !require.specified(di[i])) {
				messageInfo.url = dependencyInfo.url;
				message.push(replaceStr(templateMessage[1], messageInfo));
				continue;
			}

			if (isNotGlobal && !require) {
				messageInfo.url = dependencyInfo.url;
				message.push(replaceStr(templateMessage[2], messageInfo));
				continue;
			}

			if (registedDependency && global[di[i]]) {
				param = global[di[i]];
				paramList.push(param);
				continue;
			}

			if (isString && !dependencyInfo) {
				message.push(replaceStr(templateMessage[3], messageInfo));
				continue;
			}

			if (di[i] === undefined) {
				message.push(replaceStr(templateMessage[4], messageInfo));
				continue;
			}
		}

		return [paramList, message];
	}

	/**
	 * Regist module.
	 * @private
	 */
	global[ns].module = function(name, di, fp) {
		var result = checkDependency(name, di);

		if (result[1].length) {
			throw new Error(result[1].join("\n\r"));
		} else {
			fp.apply(global, result[0]);
		}
	};
})(window, "eg");<|endoftext|>


<commit_before>var _ = require('lodash'),
    async = require('async'),
    sdk = require('postman-collection'),
    Requester = require('../../requester').Requester,

    /**
     * Takes a list for Form-data parameters, and "resolve"s them.
     *
     * @param request
     * @param resolver
     * @param triggers
     * @param {Function} triggers.exception
     * @param callback
     */
    resolveFiles = function (request, resolver, triggers, callback) {
        var mode = _.get(request, 'body.mode'),
            formParams = _.get(request, 'body.formdata'),
            fileData = _.get(request, 'body.file');

        if (!mode) { return callback(null, request); }

        if (mode === 'formdata') {
            return async.reduce(formParams.all(), [], function (list, param, cb) {
                var isFile = (param.type === 'file');

                if (isFile && !(resolver && _.isFunction(resolver.createReadStream))) {
                    triggers.console('warn', 'Unable to load file for upload: ' + param.src);
                    return cb(null, list);
                }

                list.push({
                    key: param.key,
                    value: isFile ? resolver.createReadStream(param.src) : param.value,
                    type: param.type,
                    src: param.src
                });
                return cb(null, list);
            }, function (err, results) {
                if (err) { return callback(err); }

                request.body.formdata = new sdk.PropertyList(sdk.FormParam, request, results);
                return callback(null, request);
            });
        }
        else if (mode === 'file') {
            request.body.file = {
                content: resolver.createReadStream(fileData.src)
            };
            return callback(null, request);
        }
        else {
            return callback(null, request);
        }
    };

module.exports = {
    init: function (done) {
        var timeout = _.min([
            _.get(this.options, 'timeout.request'),
            _.get(this.options, 'timeout.global')
        ]);

        // @todo - remove this when requester creation is offloaded to runner
        if (this.options.requester && this.options.requester.external === true) {
            this.requester = this.options.requester.instance;
            // Just so we don't break the App, pull the file resolver out of the requester
            this.options.fileResolver = _.get(this.requester, 'fileResolver');
            return done();
        }

        !_.isFinite(timeout) && (timeout = undefined);

        // @todo: get requester options and maybe store per-protocol requester
        !this.requester && (this.requester = new Requester(_.merge(this.options.requester, {
            timeout: timeout || undefined
        })));

        done();
    },

    triggers: ['beforeRequest', 'request'],

    process: {
        /**
         * @param {Object} payload
         * @param {Item} payload.item
         * @param {Object} payload.data
         * @param {Object} payload.globals
         * @param {Object} payload.environment
         * @param {Cursor} payload.coords
         * @param {Function} next
         *
         * @todo  validate payload
         */
        request: function (payload, next) {
            // @todo - resolve variables in a more graceful way
            var item = new sdk.Item(payload.item.toObjectResolved(null,
                [payload.data, payload.environment, payload.globals])),

                abortOnError = _.has(payload, 'abortOnError') ? payload.abortOnError : this.options.abortOnError,
                self = this;

            async.waterfall([
                // Process any authentication helpers in the request.
                function (cb) {
                    cb(null, item.request.authorize());
                },
                // Handle file resolution
                function (request, cb) {
                    var resolver = _.get(self.options, 'fileResolver') ||
                        // todo: remove this once apps give the options correctly
                        _.get(self.options, 'requester.fileResolver');
                    resolveFiles(request, resolver, self.triggers, cb);
                },
                // Send the request
                function (request, cb) {
                    var xhr,
                        aborted;

                    item.request = request;

                    // Trigger the beforeRequest callback
                    self.triggers.beforeRequest(null, payload.coords, item.request, payload.item, {
                        abort: function () {
                            !aborted && xhr && xhr.abort();
                            aborted = true;
                        }
                    });

                    if (aborted) {
                        return cb(new Error('runtime: request aborted'));
                    }

                    xhr = self.requester.request(item, cb, self);
                }
            ], function (err, legacyResponse, legacyRequest, response) {
                err = err || null;

                this.triggers.request(err, payload.coords, new sdk.Response(response),
                    item.request, payload.item,
                    // todo: remove these asap
                    legacyResponse, legacyRequest);

                next((err && this.options.abortOnError) ? err : null, {
                    response: response,
                    request: item.request,
                    legacyResponse: legacyResponse,
                    legacyRequest: legacyRequest
                }, err);
            }.bind(this));
        }
    }
};
<commit_msg>added a check to ensure that file path is present before trying to read it<commit_after>var _ = require('lodash'),
    async = require('async'),
    sdk = require('postman-collection'),
    Requester = require('../../requester').Requester,

    /**
     * Takes a list for Form-data parameters, and "resolve"s them.
     *
     * @param request
     * @param resolver
     * @param triggers
     * @param {Function} triggers.exception
     * @param callback
     */
    resolveFiles = function (request, resolver, triggers, callback) {
        var mode = _.get(request, 'body.mode'),
            formParams = _.get(request, 'body.formdata'),
            fileData = _.get(request, 'body.file');

        if (!mode) { return callback(null, request); }

        if (mode === 'formdata') {
            return async.reduce(formParams.all(), [], function (list, param, cb) {
                var isFile = (param.type === 'file');

                if (isFile && !(resolver && _.isFunction(resolver.createReadStream))) {
                    triggers.console('warn', 'Unable to load file for upload: ' + param.src);
                    return cb(null, list);
                }

                list.push({
                    key: param.key,
                    value: isFile ? resolver.createReadStream(param.src) : param.value,
                    type: param.type,
                    src: param.src
                });
                return cb(null, list);
            }, function (err, results) {
                if (err) { return callback(err); }

                request.body.formdata = new sdk.PropertyList(sdk.FormParam, request, results);
                return callback(null, request);
            });
        }
        else if (mode === 'file') {
            request.body.file = {
                content: fileData.src && resolver.createReadStream(fileData.src)
            };
            return callback(null, request);
        }
        else {
            return callback(null, request);
        }
    };

module.exports = {
    init: function (done) {
        var timeout = _.min([
            _.get(this.options, 'timeout.request'),
            _.get(this.options, 'timeout.global')
        ]);

        // @todo - remove this when requester creation is offloaded to runner
        if (this.options.requester && this.options.requester.external === true) {
            this.requester = this.options.requester.instance;
            // Just so we don't break the App, pull the file resolver out of the requester
            this.options.fileResolver = _.get(this.requester, 'fileResolver');
            return done();
        }

        !_.isFinite(timeout) && (timeout = undefined);

        // @todo: get requester options and maybe store per-protocol requester
        !this.requester && (this.requester = new Requester(_.merge(this.options.requester, {
            timeout: timeout || undefined
        })));

        done();
    },

    triggers: ['beforeRequest', 'request'],

    process: {
        /**
         * @param {Object} payload
         * @param {Item} payload.item
         * @param {Object} payload.data
         * @param {Object} payload.globals
         * @param {Object} payload.environment
         * @param {Cursor} payload.coords
         * @param {Function} next
         *
         * @todo  validate payload
         */
        request: function (payload, next) {
            // @todo - resolve variables in a more graceful way
            var item = new sdk.Item(payload.item.toObjectResolved(null,
                [payload.data, payload.environment, payload.globals])),

                abortOnError = _.has(payload, 'abortOnError') ? payload.abortOnError : this.options.abortOnError,
                self = this;

            async.waterfall([
                // Process any authentication helpers in the request.
                function (cb) {
                    cb(null, item.request.authorize());
                },
                // Handle file resolution
                function (request, cb) {
                    var resolver = _.get(self.options, 'fileResolver') ||
                        // todo: remove this once apps give the options correctly
                        _.get(self.options, 'requester.fileResolver');
                    resolveFiles(request, resolver, self.triggers, cb);
                },
                // Send the request
                function (request, cb) {
                    var xhr,
                        aborted;

                    item.request = request;

                    // Trigger the beforeRequest callback
                    self.triggers.beforeRequest(null, payload.coords, item.request, payload.item, {
                        abort: function () {
                            !aborted && xhr && xhr.abort();
                            aborted = true;
                        }
                    });

                    if (aborted) {
                        return cb(new Error('runtime: request aborted'));
                    }

                    xhr = self.requester.request(item, cb, self);
                }
            ], function (err, legacyResponse, legacyRequest, response) {
                err = err || null;

                this.triggers.request(err, payload.coords, new sdk.Response(response),
                    item.request, payload.item,
                    // todo: remove these asap
                    legacyResponse, legacyRequest);

                next((err && this.options.abortOnError) ? err : null, {
                    response: response,
                    request: item.request,
                    legacyResponse: legacyResponse,
                    legacyRequest: legacyRequest
                }, err);
            }.bind(this));
        }
    }
};
<|endoftext|>


<commit_before>$(document).ready(function () {
    "use strict";
    var settings = {
            me: "",
            rivals: []
        },
        ranking_table = function (data) {
            var players = $.extend([], settings.rivals).concat(settings.me),
                no_play = [],
                dj_records = [],
                rival_records = [],
                exit_on_zero = 5,
                found,
                i,
                ilen;
            no_play = $.map(data, function (element) {
                return element[2];
            });
            no_play = $.map(players, function (name, index) {
                if ($.inArray(name, no_play) < 0) {
                    players.splice(index, 1, false);
                    return name;
                }
            });
            players = $.map(players, function (name) {
                if (name) {
                    return name;
                }
            });
            dj_records.push("

Rank	Icon	DJ	Score
");
            rival_records.push("

Rank	Icon	Rival DJ	Score
");
            for (i = 0, ilen = data.length; i < ilen; i += 1) {
                if (players.length < 1) {
                    if (exit_on_zero < 1) {
                        break;
                    }
                    exit_on_zero -= 1;
                }
                dj_records.push("
" + data[i][0] + '		' + data[i][2] + "	" + data[i][3] + "
");
                found = $.inArray(data[i][2], players);
                if (found > -1) {
                    rival_records.push("
" + data[i][0] + '		' + data[i][2] + "	" + data[i][3] + "
");
                    players.splice(found, 1);
                }
            }
            for (i = 0, ilen = no_play.length; i < ilen; i += 1) {
                rival_records.push("
-		" + no_play[i] + "	-
");
            }
            return '

' + dj_records.join("") + '
	' + rival_records.join("") + "

";
        },
        load_tab = function (event, ui) {
            var chart = ui.newTab.children().text(),
                div = $("#" + chart).children(),
                name = div.first().text().replace(/\W/g, "").toLowerCase();
            if (div.last().text() === "Loading...") {
                $.ajax({
                    cache: false,
                    dataType: "json",
                    url: "./database/" + (chart.length === 2 ? "pop_" + chart : chart).toLowerCase() + "/" + name + ".json"
                }).done(function (data) {
                    div.last().empty().html(ranking_table(data.ranking));
                }).fail(function () {
                    div.last().empty().html("Unable to retrieve data.");
                });
            }
        };

    // tabs
    $("#tabs").tabs({
        active: false,
        collapsible: true,
        heightStyle: "content",
        activate: load_tab
    });
});
<commit_msg>Adjusted load_tab() to accommodate master rankings.<commit_after>$(document).ready(function () {
    "use strict";
    var settings = {
            me: "",
            rivals: []
        },
        ranking_table = function (data) {
            var players = $.extend([], settings.rivals).concat(settings.me),
                no_play = [],
                dj_records = [],
                rival_records = [],
                exit_on_zero = 5,
                found,
                i,
                ilen;
            no_play = $.map(data, function (element) {
                return element[2];
            });
            no_play = $.map(players, function (name, index) {
                if ($.inArray(name, no_play) < 0) {
                    players.splice(index, 1, false);
                    return name;
                }
            });
            players = $.map(players, function (name) {
                if (name) {
                    return name;
                }
            });
            dj_records.push("

Rank	Icon	DJ	Score
");
            rival_records.push("

Rank	Icon	Rival DJ	Score
");
            for (i = 0, ilen = data.length; i < ilen; i += 1) {
                if (players.length < 1) {
                    if (exit_on_zero < 1) {
                        break;
                    }
                    exit_on_zero -= 1;
                }
                dj_records.push("
" + data[i][0] + '		' + data[i][2] + "	" + data[i][3] + "
");
                found = $.inArray(data[i][2], players);
                if (found > -1) {
                    rival_records.push("
" + data[i][0] + '		' + data[i][2] + "	" + data[i][3] + "
");
                    players.splice(found, 1);
                }
            }
            for (i = 0, ilen = no_play.length; i < ilen; i += 1) {
                rival_records.push("
-		" + no_play[i] + "	-
");
            }
            return '

' + dj_records.join("") + '
	' + rival_records.join("") + "

";
        },
        load_tab = function (event, ui) {
            var chart = ui.newTab.children().text(),
                div = $("#" + chart).children(),
                name = div.first().text().replace(/\W/g, ""),
                url;
            if (chart.length === 2) {
                chart = "pop_" + chart;
            }
            url = "./database/" + ((name === "Master") ? (name + "/" + chart) : (chart + "/" + name)) + ".json";
            if (div.last().text() === "Loading...") {
                $.ajax({
                    cache: false,
                    dataType: "json",
                    url: url.toLowerCase()
                }).done(function (data) {
                    div.last().empty().html(ranking_table(data.ranking));
                }).fail(function () {
                    div.last().empty().html("Unable to retrieve data.");
                });
            }
        };

    // tabs
    $("#tabs").tabs({
        active: false,
        collapsible: true,
        event: "mouseover",
        heightStyle: "content",
        activate: load_tab
    });

    // accordion
    $(".accordion").accordion({
        heightStyle: "content"
    });
});
<|endoftext|>


<commit_before>"use strict"

var config = require("config")
var utils = require("../lib/utils.js")
var errorLib = require("../lib/error.js")
var db = require("../lib/db.js")
var clc = require('cli-color')
var async = require("async")
var sanitizeHtml = require('sanitize-html')
require("string_score")

exports.prepareAgents = function(cb){
	db.prepareAgents(function(){
		if (cb) cb()
	})
}

//turns on a process to keep the agent queue populated from the database
exports.populateShadowcatAgentsBuildQueue = function(cb){

	exports.shadowCatAgentsQueue = []
	var queueTimer = null
	var resumeOn = false

	console.log("Connecting to shadowcat...")

	db.returnCollectionShadowcat("bib",function(err,shadowcatBib){

		console.log("Starting Cursor...")

		// _id: 16547127
		var cursor = shadowcatBib.find({ }, { 'sc:agents' : 1, 'sc:research' : 1 }).batchSize(100)

		queueTimer = setInterval(function(){
			if (exports.shadowCatAgentsQueue.length<5000){
				if (!resumeOn){
					cursor.resume()
					resumeOn = true
				}
			}else{
				if (resumeOn){
					cursor.pause()
					resumeOn = false
				}
				//the first run?
				if (cb){
					cb()
					cb = null
				}
			}
		},500)

		cursor.on('data', function(doc) {
			if (doc['sc:research']){
				exports.shadowCatAgentsQueue.push({bnumber: doc._id, agents: doc['sc:agents']})
			}
		})

		cursor.once('end', function() {
			setTimeout(function(){

				clearTimeout(queueTimer)
				exports.shadowCatAgentsQueue.push(null)

				//console.log("populateShadowcatAgentsViaf - Done!")
				//cb(++agentId)
				//agentsDatabase.close()
			},5000)
		})

	})

}

//we creating an agent it needs to merge the source information (scAgent) with any VIAF data we have on had and a existing registry agent if it exists
exports.mergeScAgentViafRegistryAgent = function(scAgent, viaf, registryAgent,source){

	//if it has no registryAgent yet that means we are creating a new one
	if (scAgent && !registryAgent){

		var newAgent = {
			viaf: false,
			viafAll: [],
			registry: "temp" + Date.now() + Math.floor(Math.random() * (1000000 - 1)) + 1,
			nameControlled: false,
			wikidata: false,
			lcId: false,
			gettyId: false,
			dbpedia: false,
			birth: false,
			death: false,
			type: false,
			source: false,
			useCount: 0,
			altForms: [],
			nameNormalized: []
		}

		//if it has a VIAF then we need to incorporate that info
		if (viaf){

			newAgent.viaf = viaf._id
			newAgent.viafAll = [viaf._id]

			//we have the VIAF data lets fill that in
			if (viaf.lcTerm){
				newAgent.nameControlled = viaf.lcTerm
			}else if (viaf.viafTerm){
				newAgent.nameControlled = viaf.viafTerm
			}else{
				newAgent.nameControlled = scAgent.nameLocal
			}
			//lets collapse these too
			newAgent.nameControlled = newAgent.nameControlled.replace(/\s\(Spirit\)/gi,'')

			if (viaf.type) newAgent.type = viaf.type.toLowerCase()

			if (viaf.wikidataId) newAgent.wikidata = viaf.wikidataId
			if (viaf.lcId) newAgent.lcId = viaf.lcId
			if (viaf.gettyId) newAgent.gettyId = parseInt(viaf.gettyId)
			if (viaf.dbpedia) newAgent.dbpedia = viaf.dbpedia
			if (viaf.birth) newAgent.birth = viaf.birth
			if (viaf.death) newAgent.death = viaf.death

			if (viaf.lcTerm){
				var normal = utils.normalizeAndDiacritics(viaf.lcTerm).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: viaf.lcTerm, type: "viaf.lcTerm", source: "catalog", id: source})
				}
			}
			if (viaf.viafTerm){
				var normal = utils.normalizeAndDiacritics(viaf.viafTerm).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: viaf.viafTerm, type: "viaf.viafTerm", source: "catalog", id: source})

				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: viaf.nameLocal, type: "viaf.nameLocal", source: "catalog", id: source})
				}
			}

			return newAgent

		}else if (!viaf && scAgent.viaf){

			//this is a potential problem, there is a VIAF id, but we did not find it in our lookup table, record an error
			errorLib.error("Agent Serialization - Catalog - No VIAF found in lookup table", JSON.stringify(scAgent))

			//we have no VIAF info and no existing registry agent, populate the registry agent based on what we have from the scagent
			if (scAgent.nameLc){
				newAgent.nameControlled = scAgent.nameLc
			}else if (scAgent.nameViaf){
				newAgent.nameControlled = scAgent.nameViaf
			}else{
				newAgent.nameControlled = scAgent.nameLocal
			}

			if (scAgent.type) newAgent.type = scAgent.type.toLowerCase()
			//we do not trust the VIAF number
			//newAgent.viaf = scAgent.viaf

			if (scAgent.nameLc){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLc).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLc, type: "scAgent.nameLc", source: "catalog", id: source})
				}
			}
			if (scAgent.nameViaf){
				var normal = utils.normalizeAndDiacritics(scAgent.nameViaf).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameViaf, type: "scAgent.nameViaf",source: "catalog", id: source})
				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})
				}
			}

			return newAgent

		}else{

			//there is no agent and also we don't have any VIAF info, so populate what we can

			if (scAgent.nameLc){
				newAgent.nameControlled = scAgent.nameLc
			}else if (scAgent.nameViaf){
				newAgent.nameControlled = scAgent.nameViaf
			}else{
				newAgent.nameControlled = scAgent.nameLocal
			}
			newAgent.type = scAgent.type.toLowerCase()

			if (scAgent.nameLc){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLc).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLc, type: "scAgent.nameLc", source: "catalog", id: source})
				}
			}
			if (scAgent.nameViaf){
				var normal = utils.normalizeAndDiacritics(scAgent.nameViaf).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameViaf, type: "scAgent.nameViaf",source: "catalog", id: source})
				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})
				}
			}

			return newAgent

		}

	}else if (scAgent && registryAgent){

		//we have the registry agent if it already has a VIAF id then that means it was properly setup eariler so we just need to populate possibly new normalized values

		if (!registryAgent.viaf && viaf){

			//the existing registry agent does not have VIAF data yet, we can populate what we know
			if (viaf.type) registryAgent.type = viaf.type.toLowerCase()
			registryAgent.viaf = viaf._id

			newAgent.viafAll = [viaf._id]

			if (viaf.wikidataId) registryAgent.wikidata = viaf.wikidataId
			if (viaf.lcId) registryAgent.lcId = viaf.lcId
			if (viaf.gettyId) registryAgent.gettyId = parseInt(viaf.gettyId)
			if (viaf.dbpedia) registryAgent.dbpedia = viaf.dbpedia
			if (viaf.birth) registryAgent.birth = viaf.birth
			if (viaf.death) registryAgent.death = viaf.death

			if (viaf.lcTerm){
				var normal = utils.normalizeAndDiacritics(viaf.lcTerm).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: viaf.lcTerm, type: "viaf.lcTerm",source: "catalog", id: source})
				}
			}
			if (viaf.viafTerm){
				var normal = utils.normalizeAndDiacritics(viaf.viafTerm).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: viaf.viafTerm, type: "viaf.viafTerm",source: "catalog", id: source})
				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: viaf.nameLocal, type: "viaf.nameLocal",source: "catalog", id: source})
				}
			}

			return registryAgent

		}else if (registryAgent.viaf){

			//it already has VIAF information just populate any new normalized names
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})
				}
			}

			return registryAgent

		}else if (!registryAgent.viaf && !viaf){

			//This is jsut a locally matched name, add in any new local normalized names
			//it already has VIAF information just populate any new normalized names
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})

				}
			}

			return registryAgent

		}

	}

	//if it got here we have problems
	errorLib.error("Agent Serialization - Catalog - Could not serialize this agent!", JSON.stringify({"scAgent" : scAgent, "viaf" : viaf, "registryAgent" : registryAgent }))

	return false

}

<commit_msg>queue problems<commit_after>"use strict"

var config = require("config")
var utils = require("../lib/utils.js")
var errorLib = require("../lib/error.js")
var db = require("../lib/db.js")
var clc = require('cli-color')
var async = require("async")
var sanitizeHtml = require('sanitize-html')
require("string_score")

exports.prepareAgents = function(cb){
	db.prepareAgents(function(){
		if (cb) cb()
	})
}

//turns on a process to keep the agent queue populated from the database
exports.populateShadowcatAgentsBuildQueue = function(cb){

	exports.shadowCatAgentsQueue = []
	var queueTimer = null
	var resumeOn = false

	console.log("Connecting to shadowcat...")

	db.returnCollectionShadowcat("bib",function(err,shadowcatBib){

		console.log("Starting Cursor...")

		// _id: 16547127
		var cursor = shadowcatBib.find({ }, { 'sc:agents' : 1, 'sc:research' : 1 }).batchSize(100)

		queueTimer = setInterval(function(){
			console.log("CHECK",exports.shadowCatAgentsQueue.length)
			if (exports.shadowCatAgentsQueue.length<5000){
				if (!resumeOn){
					cursor.resume()
					resumeOn = true
				}
			}else{
				if (resumeOn){
					cursor.pause()
					resumeOn = false
				}
				//the first run?
				if (cb){
					cb()
					cb = null
				}
			}
		},500)

		cursor.on('data', function(doc) {
			if (doc['sc:research']){
				exports.shadowCatAgentsQueue.push({bnumber: doc._id, agents: doc['sc:agents']})
			}
		})

		cursor.once('end', function() {
			setTimeout(function(){

				clearTimeout(queueTimer)
				exports.shadowCatAgentsQueue.push(null)

				//console.log("populateShadowcatAgentsViaf - Done!")
				//cb(++agentId)
				//agentsDatabase.close()
			},5000)
		})

	})

}

//we creating an agent it needs to merge the source information (scAgent) with any VIAF data we have on had and a existing registry agent if it exists
exports.mergeScAgentViafRegistryAgent = function(scAgent, viaf, registryAgent,source){

	//if it has no registryAgent yet that means we are creating a new one
	if (scAgent && !registryAgent){

		var newAgent = {
			viaf: false,
			viafAll: [],
			registry: "temp" + Date.now() + Math.floor(Math.random() * (1000000 - 1)) + 1,
			nameControlled: false,
			wikidata: false,
			lcId: false,
			gettyId: false,
			dbpedia: false,
			birth: false,
			death: false,
			type: false,
			source: false,
			useCount: 0,
			altForms: [],
			nameNormalized: []
		}

		//if it has a VIAF then we need to incorporate that info
		if (viaf){

			newAgent.viaf = viaf._id
			newAgent.viafAll = [viaf._id]

			//we have the VIAF data lets fill that in
			if (viaf.lcTerm){
				newAgent.nameControlled = viaf.lcTerm
			}else if (viaf.viafTerm){
				newAgent.nameControlled = viaf.viafTerm
			}else{
				newAgent.nameControlled = scAgent.nameLocal
			}
			//lets collapse these too
			newAgent.nameControlled = newAgent.nameControlled.replace(/\s\(Spirit\)/gi,'')

			if (viaf.type) newAgent.type = viaf.type.toLowerCase()

			if (viaf.wikidataId) newAgent.wikidata = viaf.wikidataId
			if (viaf.lcId) newAgent.lcId = viaf.lcId
			if (viaf.gettyId) newAgent.gettyId = parseInt(viaf.gettyId)
			if (viaf.dbpedia) newAgent.dbpedia = viaf.dbpedia
			if (viaf.birth) newAgent.birth = viaf.birth
			if (viaf.death) newAgent.death = viaf.death

			if (viaf.lcTerm){
				var normal = utils.normalizeAndDiacritics(viaf.lcTerm).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: viaf.lcTerm, type: "viaf.lcTerm", source: "catalog", id: source})
				}
			}
			if (viaf.viafTerm){
				var normal = utils.normalizeAndDiacritics(viaf.viafTerm).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: viaf.viafTerm, type: "viaf.viafTerm", source: "catalog", id: source})

				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: viaf.nameLocal, type: "viaf.nameLocal", source: "catalog", id: source})
				}
			}

			return newAgent

		}else if (!viaf && scAgent.viaf){

			//this is a potential problem, there is a VIAF id, but we did not find it in our lookup table, record an error
			errorLib.error("Agent Serialization - Catalog - No VIAF found in lookup table", JSON.stringify(scAgent))

			//we have no VIAF info and no existing registry agent, populate the registry agent based on what we have from the scagent
			if (scAgent.nameLc){
				newAgent.nameControlled = scAgent.nameLc
			}else if (scAgent.nameViaf){
				newAgent.nameControlled = scAgent.nameViaf
			}else{
				newAgent.nameControlled = scAgent.nameLocal
			}

			if (scAgent.type) newAgent.type = scAgent.type.toLowerCase()
			//we do not trust the VIAF number
			//newAgent.viaf = scAgent.viaf

			if (scAgent.nameLc){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLc).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLc, type: "scAgent.nameLc", source: "catalog", id: source})
				}
			}
			if (scAgent.nameViaf){
				var normal = utils.normalizeAndDiacritics(scAgent.nameViaf).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameViaf, type: "scAgent.nameViaf",source: "catalog", id: source})
				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})
				}
			}

			return newAgent

		}else{

			//there is no agent and also we don't have any VIAF info, so populate what we can

			if (scAgent.nameLc){
				newAgent.nameControlled = scAgent.nameLc
			}else if (scAgent.nameViaf){
				newAgent.nameControlled = scAgent.nameViaf
			}else{
				newAgent.nameControlled = scAgent.nameLocal
			}
			newAgent.type = scAgent.type.toLowerCase()

			if (scAgent.nameLc){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLc).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLc, type: "scAgent.nameLc", source: "catalog", id: source})
				}
			}
			if (scAgent.nameViaf){
				var normal = utils.normalizeAndDiacritics(scAgent.nameViaf).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameViaf, type: "scAgent.nameViaf",source: "catalog", id: source})
				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (newAgent.nameNormalized.indexOf(normal) === -1 && normal !== ''){
					newAgent.nameNormalized.push(normal)
					newAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})
				}
			}

			return newAgent

		}

	}else if (scAgent && registryAgent){

		//we have the registry agent if it already has a VIAF id then that means it was properly setup eariler so we just need to populate possibly new normalized values

		if (!registryAgent.viaf && viaf){

			//the existing registry agent does not have VIAF data yet, we can populate what we know
			if (viaf.type) registryAgent.type = viaf.type.toLowerCase()
			registryAgent.viaf = viaf._id

			newAgent.viafAll = [viaf._id]

			if (viaf.wikidataId) registryAgent.wikidata = viaf.wikidataId
			if (viaf.lcId) registryAgent.lcId = viaf.lcId
			if (viaf.gettyId) registryAgent.gettyId = parseInt(viaf.gettyId)
			if (viaf.dbpedia) registryAgent.dbpedia = viaf.dbpedia
			if (viaf.birth) registryAgent.birth = viaf.birth
			if (viaf.death) registryAgent.death = viaf.death

			if (viaf.lcTerm){
				var normal = utils.normalizeAndDiacritics(viaf.lcTerm).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: viaf.lcTerm, type: "viaf.lcTerm",source: "catalog", id: source})
				}
			}
			if (viaf.viafTerm){
				var normal = utils.normalizeAndDiacritics(viaf.viafTerm).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: viaf.viafTerm, type: "viaf.viafTerm",source: "catalog", id: source})
				}
			}
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: viaf.nameLocal, type: "viaf.nameLocal",source: "catalog", id: source})
				}
			}

			return registryAgent

		}else if (registryAgent.viaf){

			//it already has VIAF information just populate any new normalized names
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})
				}
			}

			return registryAgent

		}else if (!registryAgent.viaf && !viaf){

			//This is jsut a locally matched name, add in any new local normalized names
			//it already has VIAF information just populate any new normalized names
			if (scAgent.nameLocal){
				var normal = utils.normalizeAndDiacritics(scAgent.nameLocal).trim()
				if (registryAgent.nameNormalized.indexOf(normal) === -1 && normal !== '') {
					registryAgent.nameNormalized.push(normal)
					registryAgent.altForms.push({name: scAgent.nameLocal, type: "scAgent.nameLocal",source: "catalog", id: source})

				}
			}

			return registryAgent

		}

	}

	//if it got here we have problems
	errorLib.error("Agent Serialization - Catalog - Could not serialize this agent!", JSON.stringify({"scAgent" : scAgent, "viaf" : viaf, "registryAgent" : registryAgent }))

	return false

}

<|endoftext|>


<commit_before>var util = require('util');
var path = require('path');
var Q = require('q');
var junk = require('junk');
var BaseAdapter = require(__dirname + '/../base_adapter');

var LocalAdapter = function(config, fs, glob, md5) {
    BaseAdapter.apply(this, arguments);
    this.uploadPath = config.retrieve().options.uploadPath;
    this.fs = fs;
    this.glob = glob;
    this.md5 = md5;
};

/**
 * @todo abstract this.
 */
var filterJunk = function(files) {
    return files.filter(junk.not);
};

var transposeMd5 = function(files) {
    var sumPromises = [];
    files.forEach(function(file) {
        sumPromises.push(this.md5.computeFromFile(file));
    }.bind(this));
    return Q.all(sumPromises)
        .then(function(sums) {
            return files.map(function(entry, i) {
                return {
                    name: entry,
                    hash: sums[i]
                };
            });
        });
};

util.inherits(LocalAdapter, BaseAdapter);

/**
 * Filter files already existing in the upstream.
 *
 * @param array files
 * @return array
 */
LocalAdapter.prototype.filterExisting = function(files) {
    var uploadPath = this.uploadPath;
    return Q.nfcall(this.glob, uploadPath + '/**/*', {nodir: true})
        .then(filterJunk)
        .then(transposeMd5.bind(this))
        .then(function(entries) {
            return files.filter(function(file) {
                return !entries.some(function(entry) {
                    return entry.name.substr(uploadPath.length + 1) === file.name && entry.hash === file.hash;
                });
            });
        });
};

/**
 * Upload files to the configured upload location.
 *
 * @param array files
 * @return promise
 * @todo prevent overwrite of same filename (regardless of md5)
 */
LocalAdapter.prototype.upload = function(files) {
    var fs = this.fs;
    var uploadPath = this.uploadPath;
    var removeErrored = function(name, file) {
        return file.name !== name;
    };
    var handleError = function(err) {
        throw new Error(err);
    };
    var promises = [];
    files.forEach(function(file) {
        var filePath = path.normalize(uploadPath + '/' + path.dirname(file.name));
        var promise = Q.nfcall(fs.mkdirp, filePath)
            .then(function() {
                var writer = fs.createWriteStream(path.normalize(uploadPath + '/' + file.name));
                var fd = fs.ReadStream(path.normalize(process.cwd() + '/' + file.originalName));
                writer.on('error', handleError);
                fd.on('error', handleError);
                fd.pipe(writer);
            })
            .catch(function(err) {
                files = files.filter(removeErrored.bind(null, file.name));
            });
        promises.push(promise);
    });
    return Q.all(promises).then(Q.bind(Q, files));
};

module.exports = function(container) {
    return new LocalAdapter(
        container.config,
        container.node.fs,
        container.glob,
        container.md5
    );
};
<commit_msg>Rename Q library reference.<commit_after>var util = require('util');
var path = require('path');
var q = require('q');
var junk = require('junk');
var BaseAdapter = require(__dirname + '/../base_adapter');

var LocalAdapter = function(config, fs, glob, md5) {
    BaseAdapter.apply(this, arguments);
    this.uploadPath = config.retrieve().options.uploadPath;
    this.fs = fs;
    this.glob = glob;
    this.md5 = md5;
};

/**
 * @todo abstract this.
 */
var filterJunk = function(files) {
    return files.filter(junk.not);
};

var transposeMd5 = function(files) {
    var sumPromises = [];
    files.forEach(function(file) {
        sumPromises.push(this.md5.computeFromFile(file));
    }.bind(this));
    return q.all(sumPromises)
        .then(function(sums) {
            return files.map(function(entry, i) {
                return {
                    name: entry,
                    hash: sums[i]
                };
            });
        });
};

util.inherits(LocalAdapter, BaseAdapter);

/**
 * Filter files already existing in the upstream.
 *
 * @param array files
 * @return array
 */
LocalAdapter.prototype.filterExisting = function(files) {
    var uploadPath = this.uploadPath;
    return q.nfcall(this.glob, uploadPath + '/**/*', {nodir: true})
        .then(filterJunk)
        .then(transposeMd5.bind(this))
        .then(function(entries) {
            return files.filter(function(file) {
                return !entries.some(function(entry) {
                    return entry.name.substr(uploadPath.length + 1) === file.name && entry.hash === file.hash;
                });
            });
        });
};

/**
 * Upload files to the configured upload location.
 *
 * @param array files
 * @return promise
 */
LocalAdapter.prototype.upload = function(files) {
    var fs = this.fs;
    var uploadPath = this.uploadPath;
    var removeErrored = function(name, file) {
        return file.name !== name;
    };
    var handleError = function(err) {
        throw new Error(err);
    };
    var promises = [];
    files.forEach(function(file) {
        var filePath = path.normalize(uploadPath + '/' + path.dirname(file.name));
        var promise = q.nfcall(fs.mkdirp, filePath)
            .then(function() {
                var writer = fs.createWriteStream(path.normalize(uploadPath + '/' + file.name));
                var fd = fs.ReadStream(path.normalize(process.cwd() + '/' + file.originalName));
                writer.on('error', handleError);
                fd.on('error', handleError);
                fd.pipe(writer);
            })
            .catch(function(err) {
                files = files.filter(removeErrored.bind(null, file.name));
            });
        promises.push(promise);
    });
    return q.all(promises).then(q.bind(q, files));
};

module.exports = function(container) {
    return new LocalAdapter(
        container.config,
        container.node.fs,
        container.glob,
        container.md5
    );
};
<|endoftext|>


<commit_before>var multiparty = require('multiparty');
var http = require('http');
var io = require('socket.io')(app);
var fs = require('fs');
var port = 8080;

var app = http.createServer(function(req, res) {
  if (req.method === 'POST') {
    console.log("POST received");
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
      // See https://sendgrid.com/docs/API_Reference/Webhooks/parse.html for fields
      console.log(fields.from);
      console.log(fields.text);

    });

    return;
    }
}).listen(port);

console.log('Listening on port %d', port);

io.on('connection', function (socket) {
    setInterval(function(){
        var randInt = Math.floor(Math.random() * 4);
        socket.emit('drumVote', { drumInt: randInt });
    }, 1000);

    socket.on('my other event', function (data) {
        console.log(data);
    });
});
<commit_msg>strips name properly<commit_after>var multiparty = require('multiparty');
var http = require('http');
var io = require('socket.io')(app);
var fs = require('fs');
var port = 8080;

var app = http.createServer(function(req, res) {
  if (req.method === 'POST') {
    console.log("POST received");
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
      // See https://sendgrid.com/docs/API_Reference/Webhooks/parse.html for fields
      var who = JSON.stringify(fields.from);
      who = who.substr(2, who.indexOf("<") - 3);
      console.log(who);
      console.log(fields.text);

    });

    return;
    }
}).listen(port);

console.log('Listening on port %d', port);

io.on('connection', function (socket) {
    setInterval(function(){
        var randInt = Math.floor(Math.random() * 4);
        socket.emit('drumVote', { drumInt: randInt });
    }, 1000);

    socket.on('my other event', function (data) {
        console.log(data);
    });
});
<|endoftext|>


<commit_before>L.Rectangle.Draw = L.Handler.Draw.extend({
	options: {
		shapeOptions: {
			stroke: true,
			color: '#f06eaa',
			weight: 4,
			opacity: 0.5,
			fill: true,
			fillColor: null, //same as color by default
			fillOpacity: 0.2,
			clickable: true
		}
	},

	addHooks: function () {
		L.Handler.Draw.prototype.addHooks.call(this);
		if (this._map) {
			this._map.dragging.disable();
			//TODO refactor: move cursor to styles
			this._container.style.cursor = 'crosshair';

			this._updateLabelText({ text: 'Click and drag to draw rectangle.' });

			L.DomEvent
				.addListener(this._container, 'mousedown', this._onMouseDown, this)
				.addListener(document, 'mousemove', this._onMouseMove, this);
		}
	},

	removeHooks: function () {
		L.Handler.Draw.prototype.removeHooks.call(this);
		if (this._map) {
			this._map.dragging.enable();
			//TODO refactor: move cursor to styles
			this._container.style.cursor = '';

			L.DomEvent
				.removeListener(this._container, 'mousedown', this._onMouseDown)
				.removeListener(document, 'mousemove', this._onMouseMove)
				.removeListener(document, 'mouseup', this._onMouseUp);

			// If the box element doesn't exist they must not have moved the mouse, so don't need to destroy/return
			if (this._rect) {
				this._map.removeLayer(this._rect);
				delete this._rect;
			}
		}
		this._isDrawing = false;
	},

	_onMouseDown: function (e) {
		this._isDrawing = true;

		this._updateLabelText({ text: 'Release mouse to finish drawing.' });

		this._startLatLng = this._map.mouseEventToLatLng(e);

		this._startLayerPoint = this._map.mouseEventToLayerPoint(e);

		L.DomEvent
			.addListener(document, 'mouseup', this._onMouseUp, this)
			.preventDefault(e);
	},

	_onMouseMove: function (e) {
		var layerPoint = this._map.mouseEventToLayerPoint(e),
			latlng = this._map.mouseEventToLatLng(e);

		this._updateLabelPosition(layerPoint);

		if (this._isDrawing) {
			this._updateLabelPosition(layerPoint);
			if (!this._rect) {
				this._rect = new L.Rectangle(new L.LatLngBounds(this._startLatLng, latlng), this.options.shapeOptions);
				this._map.addLayer(this._rect);
			} else {
				this._rect.setBounds(new L.LatLngBounds(this._startLatLng, latlng));
			}
		}
	},

	_onMouseUp: function (e) {
		this._endLatLng = this._map.mouseEventToLatLng(e);

		this._map.fire(
			'draw:rectangle-created',
			{ rect: new L.Rectangle(new L.LatLngBounds(this._startLatLng, this._endLatLng), this.options.shapeOptions) }
		);

		this.disable();
	}
});<commit_msg>Remove unused member<commit_after>L.Rectangle.Draw = L.Handler.Draw.extend({
	options: {
		shapeOptions: {
			stroke: true,
			color: '#f06eaa',
			weight: 4,
			opacity: 0.5,
			fill: true,
			fillColor: null, //same as color by default
			fillOpacity: 0.2,
			clickable: true
		}
	},

	addHooks: function () {
		L.Handler.Draw.prototype.addHooks.call(this);
		if (this._map) {
			this._map.dragging.disable();
			//TODO refactor: move cursor to styles
			this._container.style.cursor = 'crosshair';

			this._updateLabelText({ text: 'Click and drag to draw rectangle.' });

			L.DomEvent
				.addListener(this._container, 'mousedown', this._onMouseDown, this)
				.addListener(document, 'mousemove', this._onMouseMove, this);
		}
	},

	removeHooks: function () {
		L.Handler.Draw.prototype.removeHooks.call(this);
		if (this._map) {
			this._map.dragging.enable();
			//TODO refactor: move cursor to styles
			this._container.style.cursor = '';

			L.DomEvent
				.removeListener(this._container, 'mousedown', this._onMouseDown)
				.removeListener(document, 'mousemove', this._onMouseMove)
				.removeListener(document, 'mouseup', this._onMouseUp);

			// If the box element doesn't exist they must not have moved the mouse, so don't need to destroy/return
			if (this._rect) {
				this._map.removeLayer(this._rect);
				delete this._rect;
			}
		}
		this._isDrawing = false;
	},

	_onMouseDown: function (e) {
		this._isDrawing = true;

		this._updateLabelText({ text: 'Release mouse to finish drawing.' });

		this._startLatLng = this._map.mouseEventToLatLng(e);

		L.DomEvent
			.addListener(document, 'mouseup', this._onMouseUp, this)
			.preventDefault(e);
	},

	_onMouseMove: function (e) {
		var layerPoint = this._map.mouseEventToLayerPoint(e),
			latlng = this._map.mouseEventToLatLng(e);

		this._updateLabelPosition(layerPoint);

		if (this._isDrawing) {
			this._updateLabelPosition(layerPoint);
			if (!this._rect) {
				this._rect = new L.Rectangle(new L.LatLngBounds(this._startLatLng, latlng), this.options.shapeOptions);
				this._map.addLayer(this._rect);
			} else {
				this._rect.setBounds(new L.LatLngBounds(this._startLatLng, latlng));
			}
		}
	},

	_onMouseUp: function (e) {
		this._endLatLng = this._map.mouseEventToLatLng(e);

		this._map.fire(
			'draw:rectangle-created',
			{ rect: new L.Rectangle(new L.LatLngBounds(this._startLatLng, this._endLatLng), this.options.shapeOptions) }
		);

		this.disable();
	}
});<|endoftext|>


<commit_before>/**
 * module.js - The core of module loader
 */

var cachedModules = seajs.cache = {}

var STATUS = Module.STATUS = {
  "INITIALIZED": 1, // The module is initialized
  "FETCHING": 2,    // The module file is being fetched now
  "SAVED": 3,       // The module data has been saved to cachedModules
  "LOADED": 4,      // The module and all its dependencies are ready to compile
  "COMPILING": 5,   // The module is being compiled
  "COMPILED": 6     // The module is compiled and `module.exports` is available
}

function Module(uri, status) {
  this.uri = uri
  this.status = status || STATUS.INITIALIZED
  this.dependencies = []
  this.waitings = []

  emit("initialized", this)
}

Module.prototype.load = function(ids, callback) {
  var uris = resolve(isArray(ids) ? ids : [ids], this.uri)

  load(uris, function() {
    var exports = []

    for (var i = 0, len = uris.length; i < len; i++) {
      exports[i] = compile(cachedModules[uris[i]])
    }

    if (callback) {
      callback.apply(global, exports)
    }
  })

  return this
}

function resolve(ids, refUri) {
  if (isArray(ids)) {
    // Use `for` loop instead of `forEach` or `map` function for performance
    var ret = []
    for (var i = 0, len = ids.length; i < len; i++) {
      ret[i] = resolve(ids[i], refUri)
    }
    return ret
  }

  var data = { id: ids, refUri: refUri, id2Uri: id2Uri }
  var id = emitData("resolve", data, "id")

  return data.uri || id2Uri(id, refUri)
}

function load(uris, callback, options) {
  options = options || {}
  var unloadedUris = options.filtered ? uris : getUnloadedUris(uris)

  if (unloadedUris.length === 0) {
    callback()
    return
  }

  // Emit load event for plugins such as combo plugin
  emit("load", unloadedUris)

  var len = unloadedUris.length
  var remain = len

  for (var i = 0; i < len; i++) {
    (function(uri) {
      var mod = cachedModules[uri]
      var deps = mod.dependencies

      mod.status < STATUS.SAVED ?
          (deps.length ?
              // Load dependencies that added during module initialization
              mod.load(deps, function() {
                deps.length = 0
                fetch(uri, onFetched)
              }) :
              fetch(uri, onFetched) ) :
          onFetched()

      function onFetched() {
        // Maybe failed to fetch successfully, such as 404 error
        // In these cases, just call `cb` function directly
        if (mod.status < STATUS.SAVED) {
          done()
          return
        }

        // Break circular waiting callbacks
        if (isCircularWaiting(mod)) {
          printCircularLog(circularStack)
          circularStack.length = 0
          done()
          return
        }

        // Load all unloaded dependencies
        var waitings = mod.waitings = getUnloadedUris(mod.dependencies)
        if (waitings.length === 0) {
          done(mod)
          return
        }

        // Copy waitings to prevent modification
        load(waitings.slice(), function() {
          done(mod)
        }, { filtered: true })
      }

    })(unloadedUris[i])
  }

  function done(mod) {
    if (mod && mod.status < STATUS.LOADED) {
      mod.status = STATUS.LOADED
    }

    if (--remain === 0) {
      callback()
    }
  }
}

var fetchingList = {}
var fetchedList = {}
var callbackList = {}
var anonymousModuleMeta = null

function fetch(uri, callback) {
  cachedModules[uri].status = STATUS.FETCHING

  // Emit `fetch` event. Plugins could use this event to
  // modify uri or do other magic things
  var requestUri = emitData("fetch",
      { uri: uri, fetchedList: fetchedList },
      "requestUri") || uri

  if (fetchedList[requestUri]) {
    callback()
    return
  }

  if (fetchingList[requestUri]) {
    callbackList[requestUri].push(callback)
    return
  }

  fetchingList[requestUri] = true
  callbackList[requestUri] = [callback]

  // Emit `request` event and send request
  var charset = configData.charset
  var data = {
    uri: uri,
    requestUri: requestUri,
    callback: onRequested,
    charset: charset
  }
  var requested = emitData("request", data, "requested")

  if (!requested) {
    request(data.requestUri, onRequested, charset)
  }

  function onRequested() {
    delete fetchingList[requestUri]
    fetchedList[requestUri] = true

    // Save meta data of anonymous module
    if (anonymousModuleMeta) {
      save(uri, anonymousModuleMeta)
      anonymousModuleMeta = null
    }

    // Call callbacks
    var fn, fns = callbackList[requestUri]
    delete callbackList[requestUri]
    while ((fn = fns.shift())) fn()
  }
}

function define(id, deps, factory) {
  var argsLen = arguments.length

  // HANDLE: define(factory)
  if (argsLen === 1) {
    factory = id
    id = undefined
  }
  // HANDLE: define(id || deps, factory)
  else if (argsLen === 2) {
    factory = deps
    deps = undefined

    // HANDLE: define(deps, factory)
    if (isArray(id)) {
      deps = id
      id = undefined
    }
  }

  // Parse dependencies according to the module factory code
  if (!isArray(deps) && isFunction(factory)) {
    deps = parseDependencies(factory.toString())
  }

  var meta = { id: id, dependencies: deps, factory: factory }
  var derivedUri

  // Try to derive uri in IE6-9 for anonymous modules
  if (!id && doc.attachEvent) {
    var script = getCurrentScript()

    if (script) {
      derivedUri = script.src
    }
    else {
      log("Failed to derive: " + factory)

      // NOTE: If the id-deriving methods above is failed, then falls back
      // to use onload event to get the uri
    }
  }

  var resolvedUri = id ? resolve(id) : derivedUri

  if (resolvedUri) {
    save(resolvedUri, meta)
  }
  else {
    // Save information for "memoizing" work in the script onload event
    anonymousModuleMeta = meta
  }

}

function save(uri, meta) {
  meta.uri = uri
  uri = emitData("save", meta, "uri")

  var mod = getModule(uri)

  // Do NOT override already saved modules
  if (mod.status < STATUS.SAVED) {
    // Let anonymous module id equal to its uri
    mod.id = meta.id || uri

    // Remove duplicated dependencies
    mod.dependencies = resolve(meta.dependencies || [], uri)

    mod.factory = meta.factory
    mod.status = STATUS.SAVED
  }
}

function compile(mod) {
  // Return null when mod is invalid
  if (!mod) {
    return null
  }

  // When module is compiled, DO NOT compile it again. When module
  // is being compiled, just return `module.exports` too, for avoiding
  // circularly calling
  if (mod.status >= STATUS.COMPILING) {
    return mod.exports
  }

  emit("compile", mod)

  // Just return `null` when:
  //  1. the module file is 404
  //  2. the module file is not written with valid module format
  //  3. other error cases
  if (mod.status < STATUS.LOADED && mod.exports === undefined) {
    return null
  }

  mod.status = STATUS.COMPILING

  function require(id) {
    var child = cachedModules[require.resolve(id)]

    // Return `null` when `uri` is invalid
    if (child === undefined) {
      return null
    }

    child.parent = mod
    return compile(child)
  }

  require.async = function(ids, callback) {
    mod.load(ids, callback)
    return require
  }

  require.resolve = function(id) {
    return resolve(id, mod.uri)
  }

  var factory = mod.factory
  var exports = factory === undefined ? mod.exports : factory

  if (isFunction(factory)) {
    exports = factory(require, mod.exports = {}, mod)
  }

  mod.exports = exports === undefined ? mod.exports : exports
  mod.status = STATUS.COMPILED

  emit("compiled", mod)
  return mod.exports
}

// Helpers

function getModule(uri, status) {
  return cachedModules[uri] ||
      (cachedModules[uri] = new Module(uri, status))
}

function getUnloadedUris(uris) {
  var ret = []

  for (var i = 0, len = uris.length; i < len; i++) {
    var uri = uris[i]
    if (uri && getModule(uri).status < STATUS.LOADED) {
      ret.push(uri)
    }
  }

  return ret
}

var circularStack = []

function isCircularWaiting(mod) {
  var waitings = mod.waitings
  if (waitings.length === 0) {
    return false
  }

  circularStack.push(mod.uri)
  if (isOverlap(waitings, circularStack)) {
    cutWaitings(waitings)
    return true
  }

  for (var i = 0; i < waitings.length; i++) {
    if (isCircularWaiting(cachedModules[waitings[i]])) {
      return true
    }
  }

  circularStack.pop()
  return false
}

function isOverlap(arrA, arrB) {
  var arrC = arrA.concat(arrB)
  return  unique(arrC).length < arrC.length
}

function cutWaitings(waitings) {
  var uri = circularStack[0]

  for (var i = waitings.length - 1; i >= 0; i--) {
    if (waitings[i] === uri) {
      waitings.splice(i, 1)
      break
    }
  }
}

function printCircularLog(stack) {
  stack.push(stack[0])
  log("Circular dependencies: " + stack.join(" --> "))
}

function preload(callback) {
  var preloadMods = configData.preload
  var len = preloadMods.length

  if (len) {
    // Use splice method to copy array and empty configData.preload
    globalModule.load(preloadMods.splice(0, len), function() {
      // Allow preload modules to add new preload modules
      preload(callback)
    })
  }
  else {
    callback()
  }
}

// Public API

var globalModule = new Module(pageUri, STATUS.COMPILED)

seajs.use = function(ids, callback) {
  // Load preload modules before all other modules
  preload(function() {
    globalModule.load(ids, callback)
  })
  return seajs
}

global.define = define

<commit_msg>Expose seajs.resolve method<commit_after>/**
 * module.js - The core of module loader
 */

var cachedModules = seajs.cache = {}

var STATUS = Module.STATUS = {
  "INITIALIZED": 1, // The module is initialized
  "FETCHING": 2,    // The module file is being fetched now
  "SAVED": 3,       // The module data has been saved to cachedModules
  "LOADED": 4,      // The module and all its dependencies are ready to compile
  "COMPILING": 5,   // The module is being compiled
  "COMPILED": 6     // The module is compiled and `module.exports` is available
}

function Module(uri, status) {
  this.uri = uri
  this.status = status || STATUS.INITIALIZED
  this.dependencies = []
  this.waitings = []

  emit("initialized", this)
}

Module.prototype.load = function(ids, callback) {
  var uris = resolve(isArray(ids) ? ids : [ids], this.uri)

  load(uris, function() {
    var exports = []

    for (var i = 0, len = uris.length; i < len; i++) {
      exports[i] = compile(cachedModules[uris[i]])
    }

    if (callback) {
      callback.apply(global, exports)
    }
  })

  return this
}

function resolve(ids, refUri) {
  if (isArray(ids)) {
    // Use `for` loop instead of `forEach` or `map` function for performance
    var ret = []
    for (var i = 0, len = ids.length; i < len; i++) {
      ret[i] = resolve(ids[i], refUri)
    }
    return ret
  }

  var data = { id: ids, refUri: refUri }
  var id = emitData("resolve", data, "id")

  return data.uri || id2Uri(id, refUri)
}

function load(uris, callback, options) {
  options = options || {}
  var unloadedUris = options.filtered ? uris : getUnloadedUris(uris)

  if (unloadedUris.length === 0) {
    callback()
    return
  }

  // Emit load event for plugins such as combo plugin
  emit("load", unloadedUris)

  var len = unloadedUris.length
  var remain = len

  for (var i = 0; i < len; i++) {
    (function(uri) {
      var mod = cachedModules[uri]
      var deps = mod.dependencies

      mod.status < STATUS.SAVED ?
          (deps.length ?
              // Load dependencies that added during module initialization
              mod.load(deps, function() {
                deps.length = 0
                fetch(uri, onFetched)
              }) :
              fetch(uri, onFetched) ) :
          onFetched()

      function onFetched() {
        // Maybe failed to fetch successfully, such as 404 error
        // In these cases, just call `cb` function directly
        if (mod.status < STATUS.SAVED) {
          done()
          return
        }

        // Break circular waiting callbacks
        if (isCircularWaiting(mod)) {
          printCircularLog(circularStack)
          circularStack.length = 0
          done()
          return
        }

        // Load all unloaded dependencies
        var waitings = mod.waitings = getUnloadedUris(mod.dependencies)
        if (waitings.length === 0) {
          done(mod)
          return
        }

        // Copy waitings to prevent modification
        load(waitings.slice(), function() {
          done(mod)
        }, { filtered: true })
      }

    })(unloadedUris[i])
  }

  function done(mod) {
    if (mod && mod.status < STATUS.LOADED) {
      mod.status = STATUS.LOADED
    }

    if (--remain === 0) {
      callback()
    }
  }
}

var fetchingList = {}
var fetchedList = {}
var callbackList = {}
var anonymousModuleMeta = null

function fetch(uri, callback) {
  cachedModules[uri].status = STATUS.FETCHING

  // Emit `fetch` event. Plugins could use this event to
  // modify uri or do other magic things
  var requestUri = emitData("fetch",
      { uri: uri, fetchedList: fetchedList },
      "requestUri") || uri

  if (fetchedList[requestUri]) {
    callback()
    return
  }

  if (fetchingList[requestUri]) {
    callbackList[requestUri].push(callback)
    return
  }

  fetchingList[requestUri] = true
  callbackList[requestUri] = [callback]

  // Emit `request` event and send request
  var charset = configData.charset
  var data = {
    uri: uri,
    requestUri: requestUri,
    callback: onRequested,
    charset: charset
  }
  var requested = emitData("request", data, "requested")

  if (!requested) {
    request(data.requestUri, onRequested, charset)
  }

  function onRequested() {
    delete fetchingList[requestUri]
    fetchedList[requestUri] = true

    // Save meta data of anonymous module
    if (anonymousModuleMeta) {
      save(uri, anonymousModuleMeta)
      anonymousModuleMeta = null
    }

    // Call callbacks
    var fn, fns = callbackList[requestUri]
    delete callbackList[requestUri]
    while ((fn = fns.shift())) fn()
  }
}

function define(id, deps, factory) {
  var argsLen = arguments.length

  // HANDLE: define(factory)
  if (argsLen === 1) {
    factory = id
    id = undefined
  }
  // HANDLE: define(id || deps, factory)
  else if (argsLen === 2) {
    factory = deps
    deps = undefined

    // HANDLE: define(deps, factory)
    if (isArray(id)) {
      deps = id
      id = undefined
    }
  }

  // Parse dependencies according to the module factory code
  if (!isArray(deps) && isFunction(factory)) {
    deps = parseDependencies(factory.toString())
  }

  var meta = { id: id, dependencies: deps, factory: factory }
  var derivedUri

  // Try to derive uri in IE6-9 for anonymous modules
  if (!id && doc.attachEvent) {
    var script = getCurrentScript()

    if (script) {
      derivedUri = script.src
    }
    else {
      log("Failed to derive: " + factory)

      // NOTE: If the id-deriving methods above is failed, then falls back
      // to use onload event to get the uri
    }
  }

  var resolvedUri = id ? resolve(id) : derivedUri

  if (resolvedUri) {
    save(resolvedUri, meta)
  }
  else {
    // Save information for "memoizing" work in the script onload event
    anonymousModuleMeta = meta
  }

}

function save(uri, meta) {
  meta.uri = uri
  uri = emitData("save", meta, "uri")

  var mod = getModule(uri)

  // Do NOT override already saved modules
  if (mod.status < STATUS.SAVED) {
    // Let anonymous module id equal to its uri
    mod.id = meta.id || uri

    // Remove duplicated dependencies
    mod.dependencies = resolve(meta.dependencies || [], uri)

    mod.factory = meta.factory
    mod.status = STATUS.SAVED
  }
}

function compile(mod) {
  // Return null when mod is invalid
  if (!mod) {
    return null
  }

  // When module is compiled, DO NOT compile it again. When module
  // is being compiled, just return `module.exports` too, for avoiding
  // circularly calling
  if (mod.status >= STATUS.COMPILING) {
    return mod.exports
  }

  emit("compile", mod)

  // Just return `null` when:
  //  1. the module file is 404
  //  2. the module file is not written with valid module format
  //  3. other error cases
  if (mod.status < STATUS.LOADED && mod.exports === undefined) {
    return null
  }

  mod.status = STATUS.COMPILING

  function require(id) {
    var child = cachedModules[require.resolve(id)]

    // Return `null` when `uri` is invalid
    if (child === undefined) {
      return null
    }

    child.parent = mod
    return compile(child)
  }

  require.async = function(ids, callback) {
    mod.load(ids, callback)
    return require
  }

  require.resolve = function(id) {
    return resolve(id, mod.uri)
  }

  var factory = mod.factory
  var exports = factory === undefined ? mod.exports : factory

  if (isFunction(factory)) {
    exports = factory(require, mod.exports = {}, mod)
  }

  mod.exports = exports === undefined ? mod.exports : exports
  mod.status = STATUS.COMPILED

  emit("compiled", mod)
  return mod.exports
}

// Helpers

function getModule(uri, status) {
  return cachedModules[uri] ||
      (cachedModules[uri] = new Module(uri, status))
}

function getUnloadedUris(uris) {
  var ret = []

  for (var i = 0, len = uris.length; i < len; i++) {
    var uri = uris[i]
    if (uri && getModule(uri).status < STATUS.LOADED) {
      ret.push(uri)
    }
  }

  return ret
}

var circularStack = []

function isCircularWaiting(mod) {
  var waitings = mod.waitings
  if (waitings.length === 0) {
    return false
  }

  circularStack.push(mod.uri)
  if (isOverlap(waitings, circularStack)) {
    cutWaitings(waitings)
    return true
  }

  for (var i = 0; i < waitings.length; i++) {
    if (isCircularWaiting(cachedModules[waitings[i]])) {
      return true
    }
  }

  circularStack.pop()
  return false
}

function isOverlap(arrA, arrB) {
  var arrC = arrA.concat(arrB)
  return  unique(arrC).length < arrC.length
}

function cutWaitings(waitings) {
  var uri = circularStack[0]

  for (var i = waitings.length - 1; i >= 0; i--) {
    if (waitings[i] === uri) {
      waitings.splice(i, 1)
      break
    }
  }
}

function printCircularLog(stack) {
  stack.push(stack[0])
  log("Circular dependencies: " + stack.join(" --> "))
}

function preload(callback) {
  var preloadMods = configData.preload
  var len = preloadMods.length

  if (len) {
    // Use splice method to copy array and empty configData.preload
    globalModule.load(preloadMods.splice(0, len), function() {
      // Allow preload modules to add new preload modules
      preload(callback)
    })
  }
  else {
    callback()
  }
}

// Public API

var globalModule = new Module(pageUri, STATUS.COMPILED)

seajs.use = function(ids, callback) {
  // Load preload modules before all other modules
  preload(function() {
    globalModule.load(ids, callback)
  })
  return seajs
}

global.define = define
seajs.resolve = id2Uri

<|endoftext|>