<commit_before>var Store = require('jfs');
var db = require('monk');
var redis = require('redis');

var uuid = require('uuid/v4');

var Promise = require('bluebird');

module.exports = function(config) {
	if (!config) {
		throw new Error('You must pass a config to botkit-interactive-storage');
	}

	if (!config.mongo_uri && !config.path && !config.redis_uri) {
		throw new Error('You must pass a mongo_uri, redis_uri, or path property');
	}

	if ((config.mongo_uri && config.path) || (config.redis_uri && config.path)) {
		throw new Error('Please pass 1 of mongo_uri, redis_uri, or path property.');
	}

	var isLocal = config.path != null;
	var isMongo = config.mongo_uri != null;

	var unwrapFromList = function(cb) {
		return function(err, data) {
			if (err) {
				cb(err, data);
			} else {
				cb(err, data[0]);
			}
		};
	};

	var msg_db;
	if (isLocal) {
		msg_db = new Store(config.path + "/interactive-storage", { saveId: 'id' });
	}
	else if (isMongo) {
		msg_db = db(config.mongo_uri).get('interactive-storage');
	}
	else {
		msg_db = redis.createClient({
			url: config.redis_uri
		});

		msg_db.on('error', function (err) {
			console.log('Redis error: ' + err);
		});
	}

	var messages = {
		save: function(msg, cb) {
			if (cb) {
				if (!msg.id) {
					msg.id = uuid();
				}

				if (isLocal) {
					msg_db.save(msg.id, msg, function() {
						cb(null, msg.id);
					});
				}
				else if (isMongo) {
					msg_db.insert(msg, function(err, result) {
						cb(null, result.id);
					});
				}
				else {
					try {
						msg_db.set('interactive:' + msg.id, JSON.stringify(msg), function(err, result) {
							cb(null, msg.id);
						});
					}
					catch(ex) {
						try {
							setTimeout(function() {
								msg_db.set('interactive:' + msg.id, JSON.stringify(msg), 'EX', config.ttl || -1, function(err, result) {
									cb(null, msg.id);
								});
							}, 5000);
						}
						catch(ex) {
							cb(ex, null);
						}
					}
				}
			}
			else {
				return new Promise(function(resolve, reject) {
					if (!msg.id) {
						msg.id = uuid();
					}

					if (isLocal) {
						msg_db.save(msg.id, msg, function() {
							resolve(msg.id);
						});
					}
					else if (isMongo) {
						msg_db.insert(msg, function(err, result) {
							resolve(result.id);
						});
					}
					else {
						try {
							msg_db.set('interactive:' + msg.id, JSON.stringify(msg), function(err, result) {
								resolve(msg.id);
							});
						}
						catch(ex) {
							try {
								setTimeout(function() {
									msg_db.set('interactive:' + msg.id, JSON.stringify(msg), 'EX', config.ttl || -1, function(err, result) {
										resolve(msg.id);
									});
								}, 5000);
							}
							catch(ex) {
								reject(ex);
							}
						}
					}
				});
			}
		},
		get: function(id, cb) {
			if (cb) {
				if (isLocal) {
					msg_db.get(id, function(err, result) {
						msg_db.delete(id, function(err) {
							cb(null, result);
						});
					});
				}
				else if (isMongo) {
					msg_db.find({ id: id }, unwrapFromList(function(err, result) {
						msg_db.remove({ id: id }, function(err) {
							cb(null, result);
						});
					}));
				}
				else {
					try {
						msg_db.get('interactive:' + id, function(err, result) {
							cb(null, JSON.parse(result));
						});
					}
					catch(ex) {
						try {
							setTimeout(function() {
								msg_db.get('interactive:' + id, function(err, result) {
									cb(null, JSON.parse(result));
								});
							}, 5000);
						}
						catch(ex) {
							cb(ex, null);
						}
					}
				}
			}
			else {
				return new Promise(function(resolve, reject) {
					if (isLocal) {
						msg_db.get(id, function(err, result) {
							msg_db.delete(id, function(err) {
								resolve(result);
							});
						});
					}
					else if (isMongo) {
						msg_db.find({ id: id }, unwrapFromList(function(err, result) {
							msg_db.remove({ id: id }, function(err) {
								resolve(result);
							});
						}));
					}
					else {
						try {
							msg_db.get('interactive:' + id, function(err, result) {
								resolve(JSON.parse(result));
							});
						}
						catch(ex) {
							try {
								setTimeout(function() {
									msg_db.get('interactive:' + id, function(err, result) {
										resolve(JSON.parse(result));
									});
								}, 5000);
							}
							catch(ex) {
								reject(ex);
							}
						}
					}
				});
			}
		}
	};

	return messages;
};<commit_msg>Add TTL to Redis<commit_after>var Store = require('jfs');
var db = require('monk');
var redis = require('redis');

var uuid = require('uuid/v4');

var Promise = require('bluebird');

module.exports = function(config) {
	if (!config) {
		throw new Error('You must pass a config to botkit-interactive-storage');
	}

	if (!config.mongo_uri && !config.path && !config.redis_uri) {
		throw new Error('You must pass a mongo_uri, redis_uri, or path property');
	}

	if ((config.mongo_uri && config.path) || (config.redis_uri && config.path)) {
		throw new Error('Please pass 1 of mongo_uri, redis_uri, or path property.');
	}

	var isLocal = config.path != null;
	var isMongo = config.mongo_uri != null;

	var unwrapFromList = function(cb) {
		return function(err, data) {
			if (err) {
				cb(err, data);
			} else {
				cb(err, data[0]);
			}
		};
	};

	var msg_db;
	if (isLocal) {
		msg_db = new Store(config.path + "/interactive-storage", { saveId: 'id' });
	}
	else if (isMongo) {
		msg_db = db(config.mongo_uri).get('interactive-storage');
	}
	else {
		msg_db = redis.createClient({
			url: config.redis_uri
		});

		msg_db.on('error', function (err) {
			console.log('Redis error: ' + err);
		});
	}

	var messages = {
		save: function(msg, cb) {
			if (cb) {
				if (!msg.id) {
					msg.id = uuid();
				}

				if (isLocal) {
					msg_db.save(msg.id, msg, function() {
						cb(null, msg.id);
					});
				}
				else if (isMongo) {
					msg_db.insert(msg, function(err, result) {
						cb(null, result.id);
					});
				}
				else {
					try {
						msg_db.set('interactive:' + msg.id, JSON.stringify(msg), 'EX', config.ttl || -1, function(err, result) {
							cb(null, msg.id);
						});
					}
					catch(ex) {
						try {
							setTimeout(function() {
								msg_db.set('interactive:' + msg.id, JSON.stringify(msg), 'EX', config.ttl || -1, function(err, result) {
									cb(null, msg.id);
								});
							}, 5000);
						}
						catch(ex) {
							cb(ex, null);
						}
					}
				}
			}
			else {
				return new Promise(function(resolve, reject) {
					if (!msg.id) {
						msg.id = uuid();
					}

					if (isLocal) {
						msg_db.save(msg.id, msg, function() {
							resolve(msg.id);
						});
					}
					else if (isMongo) {
						msg_db.insert(msg, function(err, result) {
							resolve(result.id);
						});
					}
					else {
						try {
							msg_db.set('interactive:' + msg.id, JSON.stringify(msg), 'EX', config.ttl || -1, function(err, result) {
								resolve(msg.id);
							});
						}
						catch(ex) {
							try {
								setTimeout(function() {
									msg_db.set('interactive:' + msg.id, JSON.stringify(msg), 'EX', config.ttl || -1, function(err, result) {
										resolve(msg.id);
									});
								}, 5000);
							}
							catch(ex) {
								reject(ex);
							}
						}
					}
				});
			}
		},
		get: function(id, cb) {
			if (cb) {
				if (isLocal) {
					msg_db.get(id, function(err, result) {
						msg_db.delete(id, function(err) {
							cb(null, result);
						});
					});
				}
				else if (isMongo) {
					msg_db.find({ id: id }, unwrapFromList(function(err, result) {
						msg_db.remove({ id: id }, function(err) {
							cb(null, result);
						});
					}));
				}
				else {
					try {
						msg_db.get('interactive:' + id, function(err, result) {
							cb(null, JSON.parse(result));
						});
					}
					catch(ex) {
						try {
							setTimeout(function() {
								msg_db.get('interactive:' + id, function(err, result) {
									cb(null, JSON.parse(result));
								});
							}, 5000);
						}
						catch(ex) {
							cb(ex, null);
						}
					}
				}
			}
			else {
				return new Promise(function(resolve, reject) {
					if (isLocal) {
						msg_db.get(id, function(err, result) {
							msg_db.delete(id, function(err) {
								resolve(result);
							});
						});
					}
					else if (isMongo) {
						msg_db.find({ id: id }, unwrapFromList(function(err, result) {
							msg_db.remove({ id: id }, function(err) {
								resolve(result);
							});
						}));
					}
					else {
						try {
							msg_db.get('interactive:' + id, function(err, result) {
								resolve(JSON.parse(result));
							});
						}
						catch(ex) {
							try {
								setTimeout(function() {
									msg_db.get('interactive:' + id, function(err, result) {
										resolve(JSON.parse(result));
									});
								}, 5000);
							}
							catch(ex) {
								reject(ex);
							}
						}
					}
				});
			}
		}
	};

	return messages;
};<|endoftext|>


<commit_before>export function parseQS(query) {
  return !query ? {} : query
    .replace(/^\?/, '')
    .split('&')
    .reduce((acc, criteria) => {
      let [k, v] = criteria.split('=');
      if (/\[\]$/.test(k)) {
        k = k.replace('[]', '');
        acc[k] = acc[k] || [];
        acc[k].push(v);
      } else {
        acc[k] = v || '';
      }
      return acc;
    }, {});
}

export function formatQS(query) {
  return Object
    .keys(query)
    .map(k => Array.isArray(query[k])
      ? query[k].map(v => `${k}[]=${v}`).join('&')
      : `${k}=${query[k]}`)
    .join('&');
}

export function parse(url, options) {
  let parsed = document.createElement('a');
  if (options && 'noDecodeWholeURL' in options && options.noDecodeWholeURL) {
    parsed.href = url;
  } else {
    parsed.href = decodeURIComponent(url);
  }
  return {
    protocol: (parsed.protocol || '').replace(/:$/, ''),
    hostname: parsed.hostname,
    port: +parsed.port,
    pathname: parsed.pathname.replace(/^(?!\/)/, '/'),
    search: parseQS(parsed.search || ''),
    hash: (parsed.hash || '').replace(/^#/, ''),
    host: parsed.host
  };
}

export function format(obj) {
  return (obj.protocol || 'http') + '://' +
         (obj.host ||
          obj.hostname + (obj.port ? `:${obj.port}` : '')) +
         (obj.pathname || '') +
         (obj.search ? `?${formatQS(obj.search || '')}` : '') +
         (obj.hash ? `#${obj.hash}` : '');
}
<commit_msg>fixes bug for IE when invalid value passed to parse (#1657)<commit_after>export function parseQS(query) {
  return !query ? {} : query
    .replace(/^\?/, '')
    .split('&')
    .reduce((acc, criteria) => {
      let [k, v] = criteria.split('=');
      if (/\[\]$/.test(k)) {
        k = k.replace('[]', '');
        acc[k] = acc[k] || [];
        acc[k].push(v);
      } else {
        acc[k] = v || '';
      }
      return acc;
    }, {});
}

export function formatQS(query) {
  return Object
    .keys(query)
    .map(k => Array.isArray(query[k])
      ? query[k].map(v => `${k}[]=${v}`).join('&')
      : `${k}=${query[k]}`)
    .join('&');
}

export function parse(url, options) {
  let parsed = document.createElement('a');
  if (options && 'noDecodeWholeURL' in options && options.noDecodeWholeURL) {
    parsed.href = url;
  } else {
    parsed.href = decodeURIComponent(url);
  }
  return {
    protocol: (parsed.protocol || '').replace(/:$/, ''),
    hostname: parsed.hostname,
    port: +parsed.port,
    pathname: parsed.pathname.replace(/^(?!\/)/, '/'),
    search: parseQS(parsed.search || ''),
    hash: (parsed.hash || '').replace(/^#/, ''),
    host: parsed.host || window.location.host
  };
}

export function format(obj) {
  return (obj.protocol || 'http') + '://' +
         (obj.host ||
          obj.hostname + (obj.port ? `:${obj.port}` : '')) +
         (obj.pathname || '') +
         (obj.search ? `?${formatQS(obj.search || '')}` : '') +
         (obj.hash ? `#${obj.hash}` : '');
}
<|endoftext|>


<commit_before>(function ($) {

    var contentButton = [],
        contentTop = [],
        content = [],
        lastScrollTop = 0,
        scrollDir = '',
        itemClass = '',
        itemHover = '',
        menuSize = null,
        stickyHeight = 0,
        stickyMarginB = 0,
        currentMarginT = 0,
        topMargin = 0;

    $(window).scroll(function (event) {
        var st = $(this).scrollTop();
        if (st > lastScrollTop) {
            scrollDir = 'down';
        } else {
            scrollDir = 'up';
        }
        lastScrollTop = st;
    });

    $.fn.stickUp = function (options) {
        // adding a class to users div
        $(this).addClass('stuckMenu');
        //getting options
        var objn = 0;
        if (options != null) {
            for (var o in options.parts) {
                if (options.parts.hasOwnProperty(o)) {
                    content[objn] = options.parts[objn];
                    objn++;
                }
            }
            if (objn == 0) {
                console.log('error:needs arguments');
            }

            itemClass = options.itemClass;
            itemHover = options.itemHover;
            if (options.topMargin != null) {
                if (options.topMargin == 'auto') {
                    topMargin = parseInt($('.stuckMenu').css('margin-top'));
                } else {
                    if (isNaN(options.topMargin) && options.topMargin.search("px") > 0) {
                        topMargin = parseInt(options.topMargin.replace("px", ""));
                    } else if (!isNaN(parseInt(options.topMargin))) {
                        topMargin = parseInt(options.topMargin);
                    } else {
                        console.log("incorrect argument, ignored.");
                        topMargin = 0;
                    }
                }
            } else {
                topMargin = 0;
            }
            menuSize = $('.' + itemClass).size();
        }
        stickyHeight = parseInt($(this).height());
        stickyMarginB = parseInt($(this).css('margin-bottom'));
        currentMarginT = parseInt($(this).next().closest('div').css('margin-top'));
        vartop = parseInt($(this).offset().top);
        //$(this).find('*').removeClass(itemHover);

        return this;
    }
    $(document).on('scroll', function () {
        varscroll = parseInt($(document).scrollTop());
        if (menuSize != null) {
            for (var i = 0; i < menuSize; i++) {
                contentTop[i] = $('#' + content[i] + '').offset().top;

                function bottomView(i) {
                    contentView = $('#' + content[i] + '').height() * .4;
                    testView = contentTop[i] - contentView;
                    //console.log(varscroll);
                    if (varscroll > testView) {
                        $('.' + itemClass).removeClass(itemHover);
                        $('.' + itemClass + ':eq(' + i + ')').addClass(itemHover);
                    } else if (varscroll < 50) {
                        $('.' + itemClass).removeClass(itemHover);
                        $('.' + itemClass + ':eq(0)').addClass(itemHover);
                    }
                }
                if (scrollDir == 'down' && varscroll > contentTop[i] - 50 && varscroll < contentTop[i] + 50) {
                    $('.' + itemClass).removeClass(itemHover);
                    $('.' + itemClass + ':eq(' + i + ')').addClass(itemHover);
                }
                if (scrollDir == 'up') {
                    bottomView(i);
                }
            }
        }

        if (vartop < varscroll + topMargin) {
            $('.stuckMenu').addClass('isStuck');
            $('.stuckMenu').next().closest('div').css({
                'margin-top': stickyHeight + stickyMarginB + currentMarginT + 'px'
            }, 10);
            $('.stuckMenu').css("position", "fixed");
            $('.isStuck').css({
                top: '0px'
            }, 10, function () {

            });
        };

        if (varscroll + topMargin < vartop) {
            $('.stuckMenu').removeClass('isStuck');
            $('.stuckMenu').next().closest('div').css({
                'margin-top': currentMarginT + 'px'
            }, 10);
            $('.stuckMenu').css("position", "relative");
        };

    });

})(jQuery);
<commit_msg>performance | scrollevent cleanup<commit_after>(function ($) {

    var contentButton = [],
        contentTop = [],
        content = [],
        lastScrollTop = 0,
        scrollDir = '',
        itemClass = '',
        itemHover = '',
        menuSize = null,
        stickyHeight = 0,
        stickyMarginB = 0,
        currentMarginT = 0,
        topMargin = 0;

    $(window).scroll(function (event) {
        var st = $(this).scrollTop();
        if (st > lastScrollTop) {
            scrollDir = 'down';
        } else {
            scrollDir = 'up';
        }
        lastScrollTop = st;
    });

    $.fn.stickUp = function (options) {
        // adding a class to users div
        $(this).addClass('stuckMenu');
        //getting options
        var objn = 0;
        if (options != null) {
            for (var o in options.parts) {
                if (options.parts.hasOwnProperty(o)) {
                    content[objn] = options.parts[objn];
                    objn++;
                }
            }
            if (objn == 0) {
                console.log('error:needs arguments');
            }

            itemClass = options.itemClass;
            itemHover = options.itemHover;
            if (options.topMargin != null) {
                if (options.topMargin == 'auto') {
                    topMargin = parseInt($('.stuckMenu').css('margin-top'));
                } else {
                    if (isNaN(options.topMargin) && options.topMargin.search("px") > 0) {
                        topMargin = parseInt(options.topMargin.replace("px", ""));
                    } else if (!isNaN(parseInt(options.topMargin))) {
                        topMargin = parseInt(options.topMargin);
                    } else {
                        console.log("incorrect argument, ignored.");
                        topMargin = 0;
                    }
                }
            } else {
                topMargin = 0;
            }
            menuSize = $('.' + itemClass).size();
        }
        stickyHeight = parseInt($(this).height());
        stickyMarginB = parseInt($(this).css('margin-bottom'));
        currentMarginT = parseInt($(this).next().closest('div').css('margin-top'));
        vartop = parseInt($(this).offset().top);
        //$(this).find('*').removeClass(itemHover);

        return this;
    }
    $(document).on('scroll', function () {
        varscroll = parseInt($(document).scrollTop());
        if (menuSize != null) {
            for (var i = 0; i < menuSize; i++) {
                contentTop[i] = $('#' + content[i] + '').offset().top;

                function bottomView(i) {
                    contentView = $('#' + content[i] + '').height() * .4;
                    testView = contentTop[i] - contentView;
                    //console.log(varscroll);
                    if (varscroll > testView) {
                        $('.' + itemClass).removeClass(itemHover);
                        $('.' + itemClass + ':eq(' + i + ')').addClass(itemHover);
                    } else if (varscroll < 50) {
                        $('.' + itemClass).removeClass(itemHover);
                        $('.' + itemClass + ':eq(0)').addClass(itemHover);
                    }
                }
                if (scrollDir == 'down' && varscroll > contentTop[i] - 50 && varscroll < contentTop[i] + 50) {
                    $('.' + itemClass).removeClass(itemHover);
                    $('.' + itemClass + ':eq(' + i + ')').addClass(itemHover);
                }
                if (scrollDir == 'up') {
                    bottomView(i);
                }
            }
        }

        if (vartop < varscroll + topMargin) {
            if(!$('.stuckMenu').hasClass('isStuck')){
                $('.stuckMenu').addClass('isStuck');
                $('.stuckMenu').next().closest('div').css({
                   'margin-top': stickyHeight + stickyMarginB + currentMarginT + 'px'
                }, 10);
                $('.stuckMenu').css("position", "fixed");
                $('.isStuck').css({
                    top: '0px'
                }, 10, function () {

                });
            }
        }
        if (varscroll + topMargin < vartop && $('.stuckMenu').hasClass('isStuck')) {
            $('.stuckMenu').removeClass('isStuck');
            $('.stuckMenu').next().closest('div').css({
                'margin-top': currentMarginT + 'px'
            }, 10);
            $('.stuckMenu').css("position", "relative");
        };

    });

})(jQuery);
<|endoftext|>


<commit_before>define(function(require, exports, module) {
    main.consumes = [
        "Plugin", "menus", "commands", "tabManager", "settings",
        "preferences", "save", "ui"
    ];
    main.provides = ["ace.stripws"];
    return main;

    function main(options, imports, register) {
        var Plugin   = imports.Plugin;
        var menus    = imports.menus;
        var commands = imports.commands;
        var tabs     = imports.tabManager;
        var settings = imports.settings;
        var prefs    = imports.preferences;
        var save     = imports.save;
        var ui       = imports.ui;

        var whitespaceUtil = require("ace/ext/whitespace");

        /***** Initialization *****/

        var plugin = new Plugin("Ajax.org", main.consumes);
        // var emit   = plugin.getEmitter();

        var disabled = false;

        var loaded = false;
        function load(){
            if (loaded) return false;
            loaded = true;

            commands.addCommand({
                name        : "stripws",
                hint        : "strip whitespace at the end of each line",
                exec        : function(){
                    stripws();
                },
                isAvailable : function (editor){
                    return editor && tabs.focussedTab &&
                        typeof tabs.focussedTab.path == "string";
                }
            }, plugin);

            menus.addItemByPath("Tools/Strip Trailing Space", new ui.item({
                command : "stripws"
            }), 100, plugin);

            menus.addItemByPath("Tools/~", new ui.divider(), 200, plugin);

            save.on("beforeSave", function (e) {
                var shouldStrip = settings.getBool("user/general/@stripws");
                if (!shouldStrip)
                    return;
                stripws(e.document.tab);
            }, plugin);

            settings.on("read", function(e) {
                settings.setDefaults("user/general", [["stripws", "false"]]);
            }, plugin);

            prefs.add({
               "File" : {
                    position : 150,
                    "Whitespace" : {
                        position : 500,
                        "On Save, Strip Whitespace" : {
                            type     : "checkbox",
                            position : 900,
                            path     : "user/general/@stripws"
                        }
                    }
               }
            }, plugin);
        }

        /***** Methods *****/

        function stripws(tab) {
            tab = tab || tabs.focussedTab;
            if (!tab || !tab.path || disabled)
                return;

            var session = tab.document.getSession().session;
            whitespaceUtil.trimTrailingSpace(session, true);
            session.$syncInformUndoManager();
        }

        /***** Lifecycle *****/

        plugin.on("load", function(){
            load();
        });
        plugin.on("enable", function(){
            disabled = false;
        });
        plugin.on("disable", function(){
            disabled = true;
        });
        plugin.on("unload", function(){
            loaded = false;
        });

        /***** Register and define API *****/

        /**
         * Strips trailing whitespace from lines just before a file is saved.
         * @singleton
         **/
        plugin.freezePublicAPI({
            /*
             * Strips whitespace at the end of each line in the given page
             * @param {Tab} tab The tab to strip the whitespace from
             * If not provided, the currently focussed tab will be used instead
             */
            strpws: stripws
        });

        register(null, {
            "ace.stripws": plugin
        });
    }
});

<commit_msg>Apply scripts/apply-conventions.sh<commit_after>define(function(require, exports, module) {
    main.consumes = [
        "Plugin", "menus", "commands", "tabManager", "settings",
        "preferences", "save", "ui"
    ];
    main.provides = ["ace.stripws"];
    return main;

    function main(options, imports, register) {
        var Plugin = imports.Plugin;
        var menus = imports.menus;
        var commands = imports.commands;
        var tabs = imports.tabManager;
        var settings = imports.settings;
        var prefs = imports.preferences;
        var save = imports.save;
        var ui = imports.ui;

        var whitespaceUtil = require("ace/ext/whitespace");

        /***** Initialization *****/

        var plugin = new Plugin("Ajax.org", main.consumes);
        // var emit = plugin.getEmitter();

        var disabled = false;

        var loaded = false;
        function load(){
            if (loaded) return false;
            loaded = true;

            commands.addCommand({
                name: "stripws",
                hint: "strip whitespace at the end of each line",
                exec: function(){
                    stripws();
                },
                isAvailable: function (editor) {
                    return editor && tabs.focussedTab &&
                        typeof tabs.focussedTab.path == "string";
                }
            }, plugin);

            menus.addItemByPath("Tools/Strip Trailing Space", new ui.item({
                command: "stripws"
            }), 100, plugin);

            menus.addItemByPath("Tools/~", new ui.divider(), 200, plugin);

            save.on("beforeSave", function (e) {
                var shouldStrip = settings.getBool("user/general/@stripws");
                if (!shouldStrip)
                    return;
                stripws(e.document.tab);
            }, plugin);

            settings.on("read", function(e) {
                settings.setDefaults("user/general", [["stripws", "false"]]);
            }, plugin);

            prefs.add({
               "File" : {
                    position: 150,
                    "Whitespace" : {
                        position: 500,
                        "On Save, Strip Whitespace" : {
                            type: "checkbox",
                            position: 900,
                            path: "user/general/@stripws"
                        }
                    }
               }
            }, plugin);
        }

        /***** Methods *****/

        function stripws(tab) {
            tab = tab || tabs.focussedTab;
            if (!tab || !tab.path || disabled)
                return;

            var session = tab.document.getSession().session;
            whitespaceUtil.trimTrailingSpace(session, true);
            session.$syncInformUndoManager();
        }

        /***** Lifecycle *****/

        plugin.on("load", function(){
            load();
        });
        plugin.on("enable", function(){
            disabled = false;
        });
        plugin.on("disable", function(){
            disabled = true;
        });
        plugin.on("unload", function(){
            loaded = false;
        });

        /***** Register and define API *****/

        /**
         * Strips trailing whitespace from lines just before a file is saved.
         * @singleton
         **/
        plugin.freezePublicAPI({
            /*
             * Strips whitespace at the end of each line in the given page
             * @param {Tab} tab The tab to strip the whitespace from
             * If not provided, the currently focussed tab will be used instead
             */
            strpws: stripws
        });

        register(null, {
            "ace.stripws": plugin
        });
    }
});

<|endoftext|>


<commit_before>var util = require('util')
var abstract = require('abstract-leveldown')
var wrap = require('level-option-wrap')

var concat = function(prefix, key) {
  if (typeof key === 'string' && key.length) return prefix+key
  if (Buffer.isBuffer(key) && key.length) return Buffer.concat([new Buffer(prefix), key])
  return key
}

var SubIterator = function(ite, prefix) {
  this.iterator = ite
  this.prefix = prefix
}

SubIterator.prototype.next = function(cb) {
  var self = this
  this.iterator.next(cb && function(err, key, value) {
    if (err) return cb(err)
    if (key) key = key.slice(self.prefix.length)
    cb.apply(null, arguments)
  })
}

SubIterator.prototype.end = function(cb) {
  this.iterator.end(cb)
}

var SubDown = function(db, prefix, separator) {
  if (!(this instanceof SubDown)) return new SubDown(db, prefix, separator)

  if (!prefix) prefix = ''
  if (!separator) separator = '!'
  if (prefix[0] === separator) prefix = prefix.slice(1)
  if (prefix[prefix.length-1] === separator) prefix = prefix.slice(0, -1)

  this.db = db
  this.leveldown = null
  this.prefix = separator+prefix+separator

  var self = this

  this._wrap = {
    gt: function(x) {
      return concat(self.prefix, (x || ''))
    },
    lt: function(x) {
      return concat(self.prefix, (x || '\xff'))
    }
  }

  abstract.AbstractLevelDOWN.call(this, 'no-location')
}

util.inherits(SubDown, abstract.AbstractLevelDOWN)

SubDown.prototype.type = 'subdown'

SubDown.prototype._open = function(opts, cb) {
  if (this.db.isOpen()) {
    if (this.db.db.type === 'subdown' && this.db.db.prefix) {
      this.prefix = this.db.db.prefix.slice(0, -1) + this.prefix
      this.leveldown = this.db.db.leveldown
    } else {
      this.leveldown = this.db.db
    }
    return cb()
  }

  this.db.on('open', this.open.bind(this, opts, cb))
}

SubDown.prototype.close = function() {
  this.leveldown.close.apply(this.leveldown, arguments)
}

SubDown.prototype.setDb = function() {
  this.leveldown.setDb.apply(this.leveldown, arguments)
}

SubDown.prototype.put = function(key, value, opts, cb) {
  this.leveldown.put(concat(this.prefix, key), value, opts, cb)
}

SubDown.prototype.get = function(key, opts, cb) {
  this.leveldown.get(concat(this.prefix, key), opts, cb)
}

SubDown.prototype.del = function(key, opts, cb) {
  this.leveldown.del(concat(this.prefix, key), opts, cb)
}

SubDown.prototype.batch =
SubDown.prototype._batch = function(operations, opts, cb) {
  if (arguments.length === 0) return new abstract.AbstractChainedBatch(this)
  if (!Array.isArray(operations)) return this.leveldown.batch.apply(null, arguments)

  var subops = new Array(operations.length)
  for (var i = 0; i < operations.length; i++) {
    var o = operations[i]
    subops[i] = {type:o.type, key:concat(this.prefix, o.key), value:o.value}
  }

  this.leveldown.batch(subops, opts, cb)
}

SubDown.prototype.approximateSize = function(start, end, cb) {
  this.leveldown.approximateSize.apply(this.leveldown, arguments)
}

SubDown.prototype.getProperty = function() {
  return this.leveldown.getProperty.apply(this.leveldown, arguments)
}

SubDown.prototype.destroy = function() {
  return this.leveldown.destroy.apply(this.leveldown, arguments)
}

SubDown.prototype.repair = function() {
  return this.leveldown.repair.apply(this.leveldown, arguments)
}

var extend = function(xopts, opts) {
  xopts.keys = opts.keys
  xopts.values = opts.values
  xopts.createIfMissing = opts.createIfMissing
  xopts.errorIfExists = opts.errorIfExists
  xopts.keyEncoding = opts.keyEncoding
  xopts.valueEncoding = opts.valueEncoding
  xopts.compression = opts.compression
  xopts.db = opts.db
  xopts.limit = opts.limit
  xopts.keyAsBuffer = opts.keyAsBuffer
  xopts.valueAsBuffer = opts.valueAsBuffer
  xopts.reverse = opts.reverse
  return xopts
}

var fixRange = function(opts) {
  return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start:opts.end, end:opts.start}
}

SubDown.prototype.iterator = function(opts) {
  if (!opts) opts = {}
  var xopts = extend(wrap(fixRange(opts), this._wrap), opts)
  return new SubIterator(this.leveldown.iterator(xopts), this.prefix)
}

module.exports = SubDown<commit_msg>fix iterator issue<commit_after>var util = require('util')
var abstract = require('abstract-leveldown')
var wrap = require('level-option-wrap')

var concat = function(prefix, key, len) {
  if (!len) len = key.length
  if (typeof key === 'string' && len) return prefix+key
  if (Buffer.isBuffer(key) && len) return Buffer.concat([new Buffer(prefix), key])
  return key
}

var SubIterator = function(ite, prefix) {
  this.iterator = ite
  this.prefix = prefix
}

SubIterator.prototype.next = function(cb) {
  var self = this
  this.iterator.next(cb && function(err, key, value) {
    if (err) return cb(err)
    if (key) key = key.slice(self.prefix.length)
    cb.apply(null, arguments)
  })
}

SubIterator.prototype.end = function(cb) {
  this.iterator.end(cb)
}

var SubDown = function(db, prefix, separator) {
  if (!(this instanceof SubDown)) return new SubDown(db, prefix, separator)

  if (!prefix) prefix = ''
  if (!separator) separator = '!'
  if (prefix[0] === separator) prefix = prefix.slice(1)
  if (prefix[prefix.length-1] === separator) prefix = prefix.slice(0, -1)

  this.db = db
  this.leveldown = null
  this.prefix = separator+prefix+separator

  var self = this

  this._wrap = {
    gt: function(x) {
      return concat(self.prefix, x || '', 1)
    },
    lt: function(x) {
      return concat(self.prefix, x || '\xff', 1)
    }
  }

  abstract.AbstractLevelDOWN.call(this, 'no-location')
}

util.inherits(SubDown, abstract.AbstractLevelDOWN)

SubDown.prototype.type = 'subdown'

SubDown.prototype._open = function(opts, cb) {
  if (this.db.isOpen()) {
    if (this.db.db.type === 'subdown' && this.db.db.prefix) {
      this.prefix = this.db.db.prefix.slice(0, -1) + this.prefix
      this.leveldown = this.db.db.leveldown
    } else {
      this.leveldown = this.db.db
    }
    return cb()
  }

  this.db.on('open', this.open.bind(this, opts, cb))
}

SubDown.prototype.close = function() {
  this.leveldown.close.apply(this.leveldown, arguments)
}

SubDown.prototype.setDb = function() {
  this.leveldown.setDb.apply(this.leveldown, arguments)
}

SubDown.prototype.put = function(key, value, opts, cb) {
  this.leveldown.put(concat(this.prefix, key), value, opts, cb)
}

SubDown.prototype.get = function(key, opts, cb) {
  this.leveldown.get(concat(this.prefix, key), opts, cb)
}

SubDown.prototype.del = function(key, opts, cb) {
  this.leveldown.del(concat(this.prefix, key), opts, cb)
}

SubDown.prototype.batch =
SubDown.prototype._batch = function(operations, opts, cb) {
  if (arguments.length === 0) return new abstract.AbstractChainedBatch(this)
  if (!Array.isArray(operations)) return this.leveldown.batch.apply(null, arguments)

  var subops = new Array(operations.length)
  for (var i = 0; i < operations.length; i++) {
    var o = operations[i]
    subops[i] = {type:o.type, key:concat(this.prefix, o.key), value:o.value}
  }

  this.leveldown.batch(subops, opts, cb)
}

SubDown.prototype.approximateSize = function(start, end, cb) {
  this.leveldown.approximateSize.apply(this.leveldown, arguments)
}

SubDown.prototype.getProperty = function() {
  return this.leveldown.getProperty.apply(this.leveldown, arguments)
}

SubDown.prototype.destroy = function() {
  return this.leveldown.destroy.apply(this.leveldown, arguments)
}

SubDown.prototype.repair = function() {
  return this.leveldown.repair.apply(this.leveldown, arguments)
}

var extend = function(xopts, opts) {
  xopts.keys = opts.keys
  xopts.values = opts.values
  xopts.createIfMissing = opts.createIfMissing
  xopts.errorIfExists = opts.errorIfExists
  xopts.keyEncoding = opts.keyEncoding
  xopts.valueEncoding = opts.valueEncoding
  xopts.compression = opts.compression
  xopts.db = opts.db
  xopts.limit = opts.limit
  xopts.keyAsBuffer = opts.keyAsBuffer
  xopts.valueAsBuffer = opts.valueAsBuffer
  xopts.reverse = opts.reverse
  return xopts
}

var fixRange = function(opts) {
  return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start:opts.end, end:opts.start}
}

SubDown.prototype.iterator = function(opts) {
  if (!opts) opts = {}
  var xopts = extend(wrap(fixRange(opts), this._wrap), opts)
  return new SubIterator(this.leveldown.iterator(xopts), this.prefix)
}

module.exports = SubDown<|endoftext|>


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

var jpp = require('json-path-processor'),
    promise = require('promise'),

promise_all = function (tasks) {
    var values = [],
        keys = [],
        I,
        all = 0;

    for (I in tasks) {
        if (tasks.hasOwnProperty(I)) {
            all++;
            values.push(tasks[I]);
            keys.push(I);
        }
    }
    return promise.all(values).then(function (V) {
        var ret = {};

        for (I=0;I<all;I++) {
            ret[keys[I]] = V[I];
        }

        return ret;
    });
},

subtask = function (tasks) {
    var typeOf = typeof tasks,
        P,
        resolved = false,
        rejected = false,
        result;

    switch (typeOf) {
    case 'object':
        P = promise_all(tasks);
        break;
    case 'function':
        P = new promise(function (resolve, reject) {
            if (resolved || rejected) {
                return result;
            }

            tasks(function () {
                result = arguments;
                resolve.apply(null, result);
            }, function (X) {
                result = arguments;
                reject.apply(null, result);
            });
        });
        break;
    case 'array':
        P = new promise(tasks);
        break;
    default:
        P = promise.resolve(tasks);
    }
    this.then = P.then;
},

SUBTASK = function(tasks) {
    return new subtask(tasks);
};

subtask.prototype.pick = function (path) {
    return this.then(function (O) {
        return jpp(O, path);
    });
};

SUBTASK.isSubtask = function (O) {
    return O instanceof subtask;
};

SUBTASK.before = function (taskCreator, doFunc, This) {
    return function () {
        var result = doFunc.apply(This || this, [taskCreator, arguments]);

        if (result instanceof subtask) {
            return result;
        }

        if (result) {
            return SUBTASK(result);
        }

        return taskCreator.apply(This || this, arguments) || SUBTASK();
    };
};

SUBTASK.after = function (taskCreator, doFunc, This) {
    return function () {
        var task = taskCreator.apply(This || this, arguments) || SUBTASK();
        return doFunc.apply(This || this, [task, arguments]) || task;
    };
};

module.exports = SUBTASK;
<commit_msg>it seens promise already cached result, no need these logic<commit_after>/*jslint node: true */
'use strict';

var jpp = require('json-path-processor'),
    promise = require('promise'),

promise_all = function (tasks) {
    var values = [],
        keys = [],
        I,
        all = 0;

    for (I in tasks) {
        if (tasks.hasOwnProperty(I)) {
            all++;
            values.push(tasks[I]);
            keys.push(I);
        }
    }
    return promise.all(values).then(function (V) {
        var ret = {};

        for (I=0;I<all;I++) {
            ret[keys[I]] = V[I];
        }

        return ret;
    });
},

subtask = function (tasks) {
    var typeOf = typeof tasks,
        P;

    switch (typeOf) {
    case 'object':
        P = promise_all(tasks);
        break;
    case 'function':
    case 'array':
        P = new promise(tasks);
        break;
    default:
        P = promise.resolve(tasks);
    }
    this.then = P.then;
},

SUBTASK = function(tasks) {
    return new subtask(tasks);
};

subtask.prototype.pick = function (path) {
    return this.then(function (O) {
        return jpp(O, path);
    });
};

SUBTASK.isSubtask = function (O) {
    return O instanceof subtask;
};

SUBTASK.before = function (taskCreator, doFunc, This) {
    return function () {
        var result = doFunc.apply(This || this, [taskCreator, arguments]);

        if (result instanceof subtask) {
            return result;
        }

        if (result) {
            return SUBTASK(result);
        }

        return taskCreator.apply(This || this, arguments) || SUBTASK();
    };
};

SUBTASK.after = function (taskCreator, doFunc, This) {
    return function () {
        var task = taskCreator.apply(This || this, arguments) || SUBTASK();
        return doFunc.apply(This || this, [task, arguments]) || task;
    };
};

module.exports = SUBTASK;
<|endoftext|>


<commit_before>
const ASSET_PREFIX = "asset-cache-v";
const ASSET_CACHE = ASSET_PREFIX + versionHash;
const COVER_ART_CACHE = "cover-art-cache";
const COVER_ART_HOSTNAME = "coverartarchive.org";
const THIRD_PARTY_ASSET_CACHE = "asset-cache-3rd-party";
const IS_DEVELOPMENT = location.hostname === "vm";

const delay = function(ms) {
    return new Promise(function(resolve) {
        setTimeout(resolve, ms);
    });
};

const isCors = function(url) {
    return new URL(url).origin !== location.origin;
};

self.addEventListener("install", function(e) {
    if (IS_DEVELOPMENT) return;

    var assetsCached = caches.open(ASSET_CACHE).then(function (cache) {
        return Promise.all(assets.map(function loop(url) {
            url = new URL(url, location.origin + (location.pathname || "")).toString();

            if (isCors(url)) {
                return cache.add(url);
            }

            return (function loop(retries) {
                return fetch(url, {
                    credentials: "include",
                    mode: "no-cors",
                    cache: "reload"
                }).then(function(response) {
                    if (response.status != 200) {
                        throw new Error("http");
                    }
                    return cache.put(url, response);
                }).catch(function(e) {
                    if (retries <= 5) {
                        return delay(1000).then(function() {
                            return loop(url, retries + 1);
                        });
                    } else {
                        throw e;
                    }
                });
            })(0);
        }));
    });
    e.waitUntil(assetsCached);
}, false);

self.addEventListener("activate", function(e) {
    var oldAssetsRemoved = caches.keys().then(function(keys) {
        return Promise.all(keys.filter(function(key) {
            // Never delete cover art.
            if (key === COVER_ART_CACHE) {
                return false;
            // Delete everything in development
            } else if (IS_DEVELOPMENT) {
                return true;
            // In production, only delete old assets.
            } else if (key !== ASSET_CACHE && key.indexOf(ASSET_PREFIX) >= 0) {
                return true;
            } else {
                return false;
            }
        }).map(function(key) {
            return caches.delete(key);
        }));
    });
    e.waitUntil(oldAssetsRemoved);
}, false);

var rfonts = /\.(woff2?)$/
self.addEventListener("fetch", function(e) {
    var request = e.request;

    if (request.method !== "GET") {
        return;
    }

    var requestURL = new URL(e.request.url);
    var isCors = location.origin !== requestURL.origin;
    var isHttp = requestURL.protocol.toLowerCase().indexOf("http") >= 0;
    var isCoverArt = COVER_ART_HOSTNAME === requestURL.hostname;
    var isQuery = requestURL.search && requestURL.search.length > 1 && isCors;
    var isCorsFont = isCors && rfonts.test(request.url);

    if (!isHttp || (!isCoverArt && (isCorsFont || isQuery))) {
        return;
    }

    // Cache coverart even in development.
    if (IS_DEVELOPMENT && !isCoverArt) {
        return;
    }

    var result = caches.match(e.request).then(function(response) {
        if (response) return response;

        var fetchRequest = e.request.clone();
        return fetch(fetchRequest, {mode: "no-cors"}).then(function(response) {
            var cacheName = null;
            if (!IS_DEVELOPMENT && response.type === "basic" && response.status < 300) {
                cacheName = ASSET_CACHE;
            } else if (COVER_ART_HOSTNAME === requestURL.hostname) {
                cacheName = COVER_ART_CACHE;
            }

            if (cacheName) {
                var responseToCache = response.clone();
                caches.open(cacheName).then(function(cache) {
                    cache.put(fetchRequest, responseToCache)
                });
            }
            return response;
        }).catch(function(e) {
            console.log(e.message);
        });
    });

    e.respondWith(result);
}, false);

self.addEventListener('message', function(e) {
    if (e.data.action === 'skipWaiting') {
        self.skipWaiting();
    }
});

self.addEventListener("notificationclick", function(event) {
    event.waitUntil(clients.matchAll({type: "window"}).then(function(clientList) {
        for (var i = 0; i < clientList.length; i++) {
            var client = clientList[i];
            if ('focus' in client) {
                return client.postMessage({
                    eventType: "swEvent",
                    type: "notificationClick",
                    action: event.action,
                    data: event.notification.data,
                    tag: event.notification.tag
                });
            }
        }
    }));
});
<commit_msg>clone cors request<commit_after>
const ASSET_PREFIX = "asset-cache-v";
const ASSET_CACHE = ASSET_PREFIX + versionHash;
const COVER_ART_CACHE = "cover-art-cache";
const COVER_ART_HOSTNAME = "coverartarchive.org";
const THIRD_PARTY_ASSET_CACHE = "asset-cache-3rd-party";
const IS_DEVELOPMENT = location.hostname === "vm";

const delay = function(ms) {
    return new Promise(function(resolve) {
        setTimeout(resolve, ms);
    });
};

const isCors = function(url) {
    return new URL(url).origin !== location.origin;
};

self.addEventListener("install", function(e) {
    if (IS_DEVELOPMENT) return;

    var assetsCached = caches.open(ASSET_CACHE).then(function (cache) {
        return Promise.all(assets.map(function loop(url) {
            url = new URL(url, location.origin + (location.pathname || "")).toString();

            if (isCors(url)) {
                return cache.add(url);
            }

            return (function loop(retries) {
                return fetch(url, {
                    credentials: "include",
                    mode: "no-cors",
                    cache: "reload"
                }).then(function(response) {
                    if (response.status != 200) {
                        throw new Error("http");
                    }
                    return cache.put(url, response);
                }).catch(function(e) {
                    if (retries <= 5) {
                        return delay(1000).then(function() {
                            return loop(url, retries + 1);
                        });
                    } else {
                        throw e;
                    }
                });
            })(0);
        }));
    });
    e.waitUntil(assetsCached);
}, false);

self.addEventListener("activate", function(e) {
    var oldAssetsRemoved = caches.keys().then(function(keys) {
        return Promise.all(keys.filter(function(key) {
            // Never delete cover art.
            if (key === COVER_ART_CACHE) {
                return false;
            // Delete everything in development
            } else if (IS_DEVELOPMENT) {
                return true;
            // In production, only delete old assets.
            } else if (key !== ASSET_CACHE && key.indexOf(ASSET_PREFIX) >= 0) {
                return true;
            } else {
                return false;
            }
        }).map(function(key) {
            return caches.delete(key);
        }));
    });
    e.waitUntil(oldAssetsRemoved);
}, false);

var rfonts = /\.(woff2?)$/
self.addEventListener("fetch", function(e) {
    var request = e.request;

    if (request.method !== "GET") {
        return;
    }

    var requestURL = new URL(e.request.url);
    var isCors = location.origin !== requestURL.origin;
    var isHttp = requestURL.protocol.toLowerCase().indexOf("http") >= 0;
    var isCoverArt = COVER_ART_HOSTNAME === requestURL.hostname;
    var isQuery = requestURL.search && requestURL.search.length > 1 && isCors;
    var isCorsFont = isCors && rfonts.test(request.url);

    if (isCors && requestURL.hostname === "api.acoustid.org") {
        var fetchRequest = e.request.clone();
        return fetch(fetchRequest, {mode: "cors"});
    }

    if (!isHttp || (!isCoverArt && (isCorsFont || isQuery))) {
        return;
    }

    // Cache coverart even in development.
    if (IS_DEVELOPMENT && !isCoverArt) {
        return;
    }

    var result = caches.match(e.request).then(function(response) {
        if (response) return response;

        var fetchRequest = e.request.clone();
        return fetch(fetchRequest, {mode: "no-cors"}).then(function(response) {
            var cacheName = null;
            if (!IS_DEVELOPMENT && response.type === "basic" && response.status < 300) {
                cacheName = ASSET_CACHE;
            } else if (COVER_ART_HOSTNAME === requestURL.hostname) {
                cacheName = COVER_ART_CACHE;
            }

            if (cacheName) {
                var responseToCache = response.clone();
                caches.open(cacheName).then(function(cache) {
                    cache.put(fetchRequest, responseToCache)
                });
            }
            return response;
        }).catch(function(e) {
            console.log(e.message);
        });
    });

    e.respondWith(result);
}, false);

self.addEventListener('message', function(e) {
    if (e.data.action === 'skipWaiting') {
        self.skipWaiting();
    }
});

self.addEventListener("notificationclick", function(event) {
    event.waitUntil(clients.matchAll({type: "window"}).then(function(clientList) {
        for (var i = 0; i < clientList.length; i++) {
            var client = clientList[i];
            if ('focus' in client) {
                return client.postMessage({
                    eventType: "swEvent",
                    type: "notificationClick",
                    action: event.action,
                    data: event.notification.data,
                    tag: event.notification.tag
                });
            }
        }
    }));
});
<|endoftext|>


<commit_before>var elasticsearch = require('elasticsearch');
var Q = require('q');

var cip = require('./lib/cip-methods.js');
var asset_mapping = require('./lib/asset-mapping.js');

var client = new elasticsearch.Client();

// XXX: Change this to do a full sync
var sync_all = false;

function create_index() {
    var deferred = Q.defer();

    client.indices.create({
        index: 'assets'
    }).then(function(resp) {
        console.log('Index created');
        deferred.resolve(resp);
    }, function(resp) {
        deferred.reject(resp);
    });

    return deferred.promise;
}

function handle_results(catalog, items) {
   if(items !== undefined && items.length > 0) {
        for(var i=0; i < items.length; ++i) {
            var formatted_result = asset_mapping.format_result(items[i].fields);
            var es_id = catalog.alias + '-' + formatted_result['id'];

            if(formatted_result['modification_time'] != undefined) {
                re = new RegExp('\\d+');
                var re_result = re.exec(formatted_result['modification_time']);
                if(re_result && re_result.length > 0) {
                    formatted_result['modification_time'] = parseInt(re_result[0]);
                }
            }

            if(formatted_result['categories'] != undefined) {
                formatted_result['categories_int'] = [];
                for(var j=0; j < formatted_result['categories'].length; ++j) {
                    formatted_result['categories_int'].push(formatted_result['categories'][j].id);
                }
            }
            formatted_result['catalog'] = catalog.alias;

            client.index({
                index: 'assets',
                type: 'asset',
                id: es_id,
                body: formatted_result
            }).then(function(resp) {
                console.log('Indexed ' + resp._id);
            }, function(resp) {
                console.log('Error indexing ' + resp._id);
            });
        }
    }
}

function get_result(result, i) {
    var deferred = Q.defer();

    result.get(100, i, function(returnvalue) {
        handle_results(result.catalog, returnvalue);
        deferred.resolve();
    });

    return deferred.promise;
}

function handle_catalog(nm, catalog) {
    var deferred = Q.defer();
    var promises = [];

    if(catalog.alias !== undefined) {
        cip.get_recent_assets(nm, catalog, sync_all ? '2003-12-24' : '$today-2d', function(result) {
            for(var i=0; i < result.total_rows; i=i+100) {
                promises.push(get_result(result, i));
            }

            Q.all(promises).then(function() {
                deferred.resolve();
            });
        });
    } else {
        deferred.resolve();
    }

    return deferred.promise;
}

create_index().then(function() {
    console.log("Index created");
}, function() {
    console.log("Failed, index probably already exists");
});

cip.init_session().then(function(nm) {
    cip.get_catalogs(nm).then(function(catalogs) {
        var promises = [];

        for(var i=0; i < catalogs.length; ++i) {
            promises.push(handle_catalog(nm, catalogs[i]));
        }

        Q.all(promises).then(function() {
            process.exit(0);
        });
    });
});
<commit_msg>Made sync work with categories in path<commit_after>var elasticsearch = require('elasticsearch');
var Q = require('q');

var cip = require('./lib/cip-methods.js');
var cip_categories = require('./lib/cip-categories.js');

var asset_mapping = require('./lib/asset-mapping.js');

var client = new elasticsearch.Client();

// XXX: Change this to do a full sync
var sync_all = false;
var categories = {};

function create_index() {
    var deferred = Q.defer();

    client.indices.create({
        index: 'assets'
    }).then(function(resp) {
        console.log('Index created');
        deferred.resolve(resp);
    }, function(resp) {
        deferred.reject(resp);
    });

    return deferred.promise;
}

function handle_results(catalog, items) {
   if(items !== undefined && items.length > 0) {
        for(var i=0; i < items.length; ++i) {
            var formatted_result = asset_mapping.format_result(items[i].fields);
            var es_id = catalog.alias + '-' + formatted_result['id'];

            if(formatted_result['modification_time'] != undefined) {
                re = new RegExp('\\d+');
                var re_result = re.exec(formatted_result['modification_time']);
                if(re_result && re_result.length > 0) {
                    formatted_result['modification_time'] = parseInt(re_result[0]);
                }
            }

            if(formatted_result['categories'] != undefined) {
                formatted_result['categories_int'] = [];

                for(var j=0; j < formatted_result['categories'].length; ++j) {
                    if(formatted_result['categories'][j].path.indexOf('$Categories') != 0)
                        continue;

                    var path = categories[catalog.alias].get_path(formatted_result['categories'][j].id);
                    if(path) {
                        for(var k=0; k < path.length; k++) {
                            formatted_result['categories_int'].push(path[k].id);
                        }
                    } else {
                        debugger;
                    }
                }
            }
            formatted_result['catalog'] = catalog.alias;

            client.index({
                index: 'assets',
                type: 'asset',
                id: es_id,
                body: formatted_result
            }).then(function(resp) {
                console.log('Indexed ' + resp._id);
            }, function(resp) {
                console.log('Error indexing ' + resp._id);
            });
        }
    }
}

function get_result(result, i) {
    var deferred = Q.defer();

    result.get(100, i, function(returnvalue) {
        handle_results(result.catalog, returnvalue);
        deferred.resolve();
    });

    return deferred.promise;
}

function handle_catalog(nm, catalog) {
    var deferred = Q.defer();
    var promises = [];

    if(catalog.alias !== undefined) {
        cip.get_recent_assets(nm, catalog, sync_all ? '2003-12-24' : '$today-2d', function(result) {
            for(var i=0; i < result.total_rows; i=i+100) {
                promises.push(get_result(result, i));
            }

            Q.all(promises).then(function() {
                deferred.resolve();
            });
        });
    } else {
        deferred.resolve();
    }

    return deferred.promise;
}

cip_categories.load_categories().then(function(result) {
    for(var i=0; i < result.length; ++i) {
        categories[result[i].id] = result[i];
    }
}).then(function() {
    create_index().then(function() {
        console.log("Index created");
    }, function() {
        console.log("Failed, index probably already exists");
    });
}).then(function() {
    cip.init_session().then(function(nm) {
        cip.get_catalogs(nm).then(function(catalogs) {
            var promises = [];

            for(var i=0; i < catalogs.length; ++i) {
                promises.push(handle_catalog(nm, catalogs[i]));
            }

            Q.all(promises).then(function() {
                process.exit(0);
            });
        });
    });
});
<|endoftext|>


<commit_before>require('proof')(module, function (body, assert) {
    var options = {}
    options.precompiler = require('./require')
    options.directory = 't/generated'
    body(require('..').createPacketizer(options), assert)
})
<commit_msg>Remove `t/proof.js`.<commit_after><|endoftext|>


<commit_before>// Copyright 2012, 2013 Patrick Wang <kk1fff@patrickz.net>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var EventEmitter   = require('events').EventEmitter,
    fsQueue        = require('./fs-sync.js'),
    util           = require('./util.js'),
    photoprocessor = require('./photoprocessor.js'),
    template       = require('./template-interface.js'),
    config         = require('./config.js').getConfig(),
    tagMap = {};

function resolveTag() {
  // Sort and remove duplicated item.
  function resolveSingleTag(photo) {
    var prev = null, result = [];
    photo.sort();
    photo.forEach(function(p) {
      if (p != prev) {
        result.push(p);
        prev = p;
      }
    });
    return result;
  }

  Object.keys(tagMap).forEach(function(tag) {
    var photos = tagMap[tag];
    tagMap[tag] = resolveSingleTag(photos);
  });
  return tagMap;
}

function getTagName(tag) {
  return "tag-" + util.getSafeName(tag);
}

function getTagUrl(tag) {
  return config.httpPrefix + "/" + getTagName(tag) + ".html";
}

function generateTagList() {
  var ret = [];

  resolveTag();
  Object.keys(tagMap).forEach(function(tag) {
    ret.push({
      title: tag,
      photos: photoprocessor.getPhotoInfos(tagMap[tag]),
      name: getTagName(tag),
      tagUrl: getTagUrl(tag)
    });
  });
  return ret;
}

exports.getTagName = getTagName;

exports.getTagUrl = getTagUrl;

// For photo page usage
exports.getSimpleProperties = function getSimpleProperties(tagTitleList) {
  var ret = [];
  tagTitleList.forEach(function(t) {
    ret.push({
      title: t,
      name: getTagName(t),
      tagUrl: getTagUrl(t)
    });
  });
  return ret;
}

exports.generateTagPages = function generateTagPages(albums) {
  var tags = generateTagList(),
      generating = 0,
      e = new EventEmitter();

  function onPageGenerated(err) {
    generating--;
    if (err) {
      e.emit('error', err);
    }

    if (generating == 0) {
      e.emit('ok');
    }
  }

  tags.forEach(function(tagItem) {
    var generatingPage = template.generatePage(
      'tag.html',
      {
        tag: tagItem,
        tags: tags,
        page: {
          title: tagItem.title,
          enableTagListOnSidebar: true,
          enableAlbumListOnSidebar: true
        },
        albums: albums
      });
    generating++;
    generatingPage.on('ok', function(page) {
      console.log('Page is generated: ' + tagItem.title);
      if (config.debug) console.log('Page is generated: ' + page);
      fsQueue.writeFile(config.outputDir + "/" + tagItem.name + ".html", page, 'utf8', function(err) {
        onPageGenerated(err);
      });
    });
    generatingPage.on('error', function(err) {
      onPageGenerated(err);
    });
  });
  return e;
}

// Add a photo into a tag.
exports.addTag = function addTag(photoFileName, tag) {
  tagMap[tag] = tagMap[tag] || [];
  tagMap[tag].push(photoFileName);
}

exports.printTags = function printTags() {
  console.log(JSON.stringify(tagMap));
}

exports.getTags = function getTags() {
  return generateTagList();
}
<commit_msg>Sort tag before return.<commit_after>// Copyright 2012, 2013 Patrick Wang <kk1fff@patrickz.net>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var EventEmitter   = require('events').EventEmitter,
    fsQueue        = require('./fs-sync.js'),
    util           = require('./util.js'),
    photoprocessor = require('./photoprocessor.js'),
    template       = require('./template-interface.js'),
    config         = require('./config.js').getConfig(),
    tagMap = {};

function resolveTag() {
  // Sort and remove duplicated item.
  function resolveSingleTag(photo) {
    var prev = null, result = [];
    photo.sort();
    photo.forEach(function(p) {
      if (p != prev) {
        result.push(p);
        prev = p;
      }
    });
    return result;
  }

  Object.keys(tagMap).forEach(function(tag) {
    var photos = tagMap[tag];
    tagMap[tag] = resolveSingleTag(photos);
  });
  return tagMap;
}

function getTagName(tag) {
  return "tag-" + util.getSafeName(tag);
}

function getTagUrl(tag) {
  return config.httpPrefix + "/" + getTagName(tag) + ".html";
}

function generateTagList() {
  var ret = [];

  resolveTag();
  Object.keys(tagMap).forEach(function(tag) {
    ret.push({
      title: tag,
      photos: photoprocessor.getPhotoInfos(tagMap[tag]),
      name: getTagName(tag),
      tagUrl: getTagUrl(tag)
    });
  });

  ret.sort(function (a, b) {
    return a.title < b.title ? -1 : 1;
  });

  return ret;
}

exports.getTagName = getTagName;

exports.getTagUrl = getTagUrl;

// For photo page usage
exports.getSimpleProperties = function getSimpleProperties(tagTitleList) {
  var ret = [];
  tagTitleList.forEach(function(t) {
    ret.push({
      title: t,
      name: getTagName(t),
      tagUrl: getTagUrl(t)
    });
  });
  return ret;
}

exports.generateTagPages = function generateTagPages(albums) {
  var tags = generateTagList(),
      generating = 0,
      e = new EventEmitter();

  function onPageGenerated(err) {
    generating--;
    if (err) {
      e.emit('error', err);
    }

    if (generating == 0) {
      e.emit('ok');
    }
  }

  tags.forEach(function(tagItem) {
    var generatingPage = template.generatePage(
      'tag.html',
      {
        tag: tagItem,
        tags: tags,
        page: {
          title: tagItem.title,
          enableTagListOnSidebar: true,
          enableAlbumListOnSidebar: true
        },
        albums: albums
      });
    generating++;
    generatingPage.on('ok', function(page) {
      console.log('Page is generated: ' + tagItem.title);
      if (config.debug) console.log('Page is generated: ' + page);
      fsQueue.writeFile(config.outputDir + "/" + tagItem.name + ".html", page, 'utf8', function(err) {
        onPageGenerated(err);
      });
    });
    generatingPage.on('error', function(err) {
      onPageGenerated(err);
    });
  });
  return e;
}

// Add a photo into a tag.
exports.addTag = function addTag(photoFileName, tag) {
  tagMap[tag] = tagMap[tag] || [];
  tagMap[tag].push(photoFileName);
}

exports.printTags = function printTags() {
  console.log(JSON.stringify(tagMap));
}

exports.getTags = function getTags() {
  return generateTagList();
}
<|endoftext|>


<commit_before>var assert = require('assert');
var dc = require('../');
describe('destroy-circular', function() {
  it ('should destroy circular references', function() {
    var obj = {};
    var child = {parent: obj};
    obj.child = child;

    var destroyed = dc(obj);
    assert.equal(typeof destroyed, 'object');
    assert.equal(destroyed.child.parent, '[Circular]');
  });
  it ('should not affect the original object', function() {
    var obj = {};
    var child = {parent: obj};
    obj.child = child;

    var destroyed = dc(obj);
    assert(destroyed != obj);
    assert(obj.child.parent == obj);
  });
  it('should only destroy parent references', function() {
    var obj = {};
    var common = { thing: obj };
    obj.one = { firstThing: common };
    obj.two = { secondThing: common };

    var d = dc(obj);
    assert(typeof d.one.firstThing == 'object')
    assert(typeof d.two.secondThing == 'object')
    assert.equal(d.one.firstThing.thing, '[Circular]')
    assert.equal(d.two.secondThing.thing, '[Circular]')
  });
})
<commit_msg>add test using arrays<commit_after>var assert = require('assert');
var dc = require('../');
describe('destroy-circular', function() {
  it ('should destroy circular references', function() {
    var obj = {};
    var child = {parent: obj};
    obj.child = child;

    var destroyed = dc(obj);
    assert.equal(typeof destroyed, 'object');
    assert.equal(destroyed.child.parent, '[Circular]');
  });
  it ('should not affect the original object', function() {
    var obj = {};
    var child = {parent: obj};
    obj.child = child;

    var destroyed = dc(obj);
    assert(destroyed != obj);
    assert(obj.child.parent == obj);
  });
  it('should only destroy parent references', function() {
    var obj = {};
    var common = { thing: obj };
    obj.one = { firstThing: common };
    obj.two = { secondThing: common };

    var d = dc(obj);
    assert(typeof d.one.firstThing == 'object')
    assert(typeof d.two.secondThing == 'object')
    assert.equal(d.one.firstThing.thing, '[Circular]')
    assert.equal(d.two.secondThing.thing, '[Circular]')
  });
  it('should work on arrays', function() {
    var obj = {};
    var common = [obj];
    var x = [common];
    var y = [['test'], common];
    y[0][1] = y;
    obj.a = {x:x};
    obj.b = {y:y};

    var d = dc(obj);
    assert(Array.isArray(d.a.x));
    assert.equal(d.a.x[0][0], '[Circular]');
    assert.equal(d.b.y[0][0], 'test');
    assert.equal(d.b.y[1][0], '[Circular]');
    assert.equal(d.b.y[0][1], '[Circular]');
  });
})
<|endoftext|>


<commit_before>"use strict";
/*jshint mocha: true*/
/*eslint-env mocha*/
/*global assert*/

/*eslint-disable max-nested-callbacks*/

describe("fs", function () {

    var testFolder = {
        data: {
            "file.bin": {
                _size: 256
            }
        }
    };

    function notImplemented (path, eventsHandler) {
        gpf.events.fire(gpf.events.EVENT_ERROR, {
            error: gpf.error.notImplemented()
        }, eventsHandler);
        return path; // so that it is used
    }

    function checkFileInfo (item, filePath) {
        var fileName = filePath.split("/").pop(),
            fileInfo;
        if (undefined === item._fileInfo) {
            fileInfo = item._fileInfo = {
                fileName: fileName,
                filePath: filePath,
                createdDateTime: new Date(),
                modifiedDateTime: new Date()
            };
            if (item._size) {
                fileInfo.type = gpf.fs.TYPE_FILE;
                fileInfo.size = item._size;
                delete item._size;
            } else {
                fileInfo.type = gpf.fs.TYPE_FOLDER;
                fileInfo.size = 0;
            }
        }
    }

    function getFromPath (path) {
        var parts = path.split("/"),
            item = testFolder,
            filePath,
            fileName;
        if (parts.shift() !== "test") {
            return undefined;
        }
        filePath = "test";
        while (0 < parts.length) {
            fileName = parts[0];
            if ("object" === typeof item[fileName]) {
                parts.shift();
                item = item[fileName];
                filePath = filePath + "/" + fileName;
                checkFileInfo(item, filePath);
            } else {
                return undefined;
            }
        }
        return item;
    }

    function getChildrenList (filePath) {
        var item = getFromPath(filePath),
            fileName,
            result = [];
        if (undefined !== item) {
            for (fileName in item) {
                if (item.hasOwnProperty(fileName) && "_fileInfo" !== fileName) {
                    checkFileInfo(item[fileName], filePath + "/" + fileName);
                    result.push(filePath + "/" + fileName);
                }
            }
        }
        return result;
    }

    var testStorage = {

        getInfo: function (path, eventsHandler) {
            var file = getFromPath(path),
                fileInfo;
            if (undefined === file) {
                fileInfo = {
                    type: gpf.fs.TYPE_NOT_FOUND
                };
            }  else {
                fileInfo = file._fileInfo;
            }
            gpf.events.fire(gpf.events.EVENT_READY, {info: fileInfo}, eventsHandler);
        },

        readAsBinaryStream: notImplemented,
        writeAsBinaryStream: notImplemented,
        close: notImplemented,

        explore: function (path, eventsHandler) {
            // Force error to check error handlin
            gpf.events.fire(gpf.events.EVENT_READY, {
                enumerator: gpf.internals._gpfFsExploreEnumerator(this, getChildrenList(path))
            }, eventsHandler);
        },

        createFolder: notImplemented,
        deleteFile: notImplemented,
        deleteFolder: notImplemented

    };

    if (gpf.internals) {

        describe("(internal)", function () {

            describe("_gpfFsExploreEnumerator", function () {

                it("helps building explore functions", function (done) {
                    testStorage.explore("test/data", function (event) {
                        try {
                            assert(gpf.events.EVENT_READY === event.type);
                            var enumerator = event.get("enumerator");
                            assert(true === gpf.interfaces.isImplementedBy(enumerator, gpf.interfaces.IEnumerator));
                            done();
                        } catch (e) {
                            done(e);
                        }
                    });
                });

                it("supports reset");

                it("forwards errors");

                it("automates calling getInfo from file path", function (done) {
                    var results = [];
                    function endEnumCallback (event) {
                        try {
                            assert(gpf.events.EVENT_END_OF_DATA === event.type);
                            assert(1 === results.length);
                            done();
                        } catch (e) {
                            done(e);
                        }
                    }
                    testStorage.explore("test/data", function (event) {
                        try {
                            assert(gpf.events.EVENT_READY === event.type);
                            var enumerator = event.get("enumerator");
                            gpf.interfaces.IEnumerator.each(enumerator, function (fileInfo) {
                                try {
                                    assert("number" === typeof fileInfo.type);
                                    assert("string" === typeof fileInfo.fileName);
                                    assert("string" === typeof fileInfo.filePath);
                                    assert("number" === typeof fileInfo.size);
                                    assert(null === fileInfo.createdDateTime
                                           || fileInfo.createdDateTime instanceof Date);
                                    assert(null === fileInfo.modifiedDateTime
                                           || fileInfo.modifiedDateTime instanceof Date);
                                    results.push(fileInfo);
                                } catch (e) {
                                    done(e);
                                }

                            }, endEnumCallback);
                        } catch (e) {
                            done(e);
                        }
                    });
                });

            });

            describe("_gpfFsBuildFindMethod", function () {

                // var _gpfFsBuildFindMethod = gpf.internals._gpfFsBuildFindMethod;

                it("exists");

            });

        });

    }

    if (null === gpf.fs.host()) {
        return; // No host
    }

    describe("Data generator", function () {

        it("file.bin", function (done) {
            var fs = gpf.fs.host();
            fs.getInfo("test/data/file.bin", function (infoEvent) {
                if (infoEvent.type === gpf.events.EVENT_READY && infoEvent.get("info").type === gpf.fs.TYPE_FILE) {
                    done(); // Already exists
                    return;
                }
                // File does not exist, generates
                var count = 0,
                    wStream;
                function loop (event) {
                    assert(!event || gpf.events.EVENT_READY === event.type);
                    if (256 === count) {
                        fs.close(wStream, function () {});
                        done();
                    }
                    wStream.write([count++], loop);
                }
                // Current path is always root of gpf-js
                fs.writeAsBinaryStream("test/data/file.bin", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    wStream = event.get("stream");
                    loop();
                });
            });
        });

    });

    describe("gpf.fs.host", function () {

        var gpfI = gpf.interfaces,
            iFs;

        beforeEach(function () {
            iFs = gpf.fs.host();
        });

        it("gives access to a IFileStorage interface", function () {
            assert(null !== iFs);
            if (null !== iFs) {
                assert(gpfI.isImplementedBy(iFs, gpfI.IFileStorage));
            }
        });

        describe("getInfo", function () {

            function checkInfo (info) {
                assert(null !== info);
                assert("object" === typeof info);
                assert("number" === typeof info.type);
                assert("string" === typeof info.fileName);
                assert("string" === typeof info.filePath);
                assert("number" === typeof info.size);
                assert("object" === typeof info.createdDateTime);
                assert("object" === typeof info.modifiedDateTime);
            }

            it("tells if a file exists", function (done) {
                iFs.getInfo("src/fs.js", function (event) {
                    assert(event.type === gpf.events.EVENT_READY);
                    var info = event.get("info");
                    checkInfo(info);
                    assert(info.type === gpf.fs.TYPE_FILE);
                    assert(info.size > 0);
                    done();
                });
            });

            it("tells if a directory exists", function (done) {
                iFs.getInfo("src", function (event) {
                    assert(event.type === gpf.events.EVENT_READY);
                    var info = event.get("info");
                    checkInfo(info);
                    assert(info.type === gpf.fs.TYPE_DIRECTORY);
                    // size is not relevant
                    done();
                });
            });

            it("tells if a path does not exists", function (done) {
                iFs.getInfo("null", function (event) {
                    assert(event.type === gpf.events.EVENT_READY);
                    var info = event.get("info");
                    assert(null !== info);
                    assert("object" === typeof info);
                    assert(info.type === gpf.fs.TYPE_NOT_FOUND);
                    done();
                });
            });

        });

        function _close (stream) {
            iFs.close(stream, function () {}); // ignore
        }

        describe("readAsBinaryStream", function () {

            it("reads binary files", function (done) {
                iFs.readAsBinaryStream("test/data/file.bin", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    var rStream = event.get("stream");
                    rStream.read(1, function (readEvent) {
                        assert(gpf.events.EVENT_DATA === readEvent.type);
                        var buffer = readEvent.get("buffer");
                        assert(1 === buffer.length);
                        assert(0 === buffer[0]);
                        _close(rStream);
                        done();
                    });
                });
            });

        });

        describe("writeAsBinaryStream", function () {

            it("writes binary files", function (done) {
                iFs.writeAsBinaryStream("tmp/test/fs_" + gpf.host() + ".bin", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    var wStream = event.get("stream");
                    wStream.write([0, 34, 75, 0, 128, 255], function (writeEvent) {
                        assert(gpf.events.EVENT_READY === writeEvent.type);
                        _close(wStream);
                        done();
                    });
                });
            });

        });

        describe("explore", function () {

            it("fails on a file", function (done) {
                iFs.explore("src/fs.js", function (event) {
                    assert(gpf.events.EVENT_ERROR === event.type);
                    done();
                });
            });

            it("gets an IEnumerator on a folder", function (done) {
                iFs.explore("src", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    var enumerator = event.get("enumerator");
                    assert(null !== enumerator);
                    assert(gpf.interfaces.isImplementedBy(enumerator, gpf.interfaces.IEnumerator));
                    done();
                });
            });

        });

    });

});
<commit_msg>testing promise wrapper<commit_after>"use strict";
/*jshint mocha: true*/
/*eslint-env mocha*/
/*global assert*/

/*eslint-disable max-nested-callbacks*/

describe("fs", function () {

    var testFolder = {
        data: {
            "file.bin": {
                _size: 256
            }
        }
    };

    function notImplemented (path, eventsHandler) {
        gpf.events.fire(gpf.events.EVENT_ERROR, {
            error: gpf.error.notImplemented()
        }, eventsHandler);
        return path; // so that it is used
    }

    function checkFileInfo (item, filePath) {
        var fileName = filePath.split("/").pop(),
            fileInfo;
        if (undefined === item._fileInfo) {
            fileInfo = item._fileInfo = {
                fileName: fileName,
                filePath: filePath,
                createdDateTime: new Date(),
                modifiedDateTime: new Date()
            };
            if (item._size) {
                fileInfo.type = gpf.fs.TYPE_FILE;
                fileInfo.size = item._size;
                delete item._size;
            } else {
                fileInfo.type = gpf.fs.TYPE_FOLDER;
                fileInfo.size = 0;
            }
        }
    }

    function getFromPath (path) {
        var parts = path.split("/"),
            item = testFolder,
            filePath,
            fileName;
        if (parts.shift() !== "test") {
            return undefined;
        }
        filePath = "test";
        while (0 < parts.length) {
            fileName = parts[0];
            if ("object" === typeof item[fileName]) {
                parts.shift();
                item = item[fileName];
                filePath = filePath + "/" + fileName;
                checkFileInfo(item, filePath);
            } else {
                return undefined;
            }
        }
        return item;
    }

    function getChildrenList (filePath) {
        var item = getFromPath(filePath),
            fileName,
            result = [];
        if (undefined !== item) {
            for (fileName in item) {
                if (item.hasOwnProperty(fileName) && "_fileInfo" !== fileName) {
                    checkFileInfo(item[fileName], filePath + "/" + fileName);
                    result.push(filePath + "/" + fileName);
                }
            }
        }
        return result;
    }

    var testStorage = {

        getInfo: function (path, eventsHandler) {
            var file = getFromPath(path),
                fileInfo;
            if (undefined === file) {
                fileInfo = {
                    type: gpf.fs.TYPE_NOT_FOUND
                };
            }  else {
                fileInfo = file._fileInfo;
            }
            gpf.events.fire(gpf.events.EVENT_READY, {info: fileInfo}, eventsHandler);
        },

        readAsBinaryStream: notImplemented,
        writeAsBinaryStream: notImplemented,
        close: notImplemented,

        explore: gpf.events.wrap(function (path, eventsHandler) {
            // Force error to check error handlin
            gpf.events.fire(gpf.events.EVENT_READY, {
                enumerator: gpf.internals._gpfFsExploreEnumerator(this, getChildrenList(path))
            }, eventsHandler);
        }),

        createFolder: notImplemented,
        deleteFile: notImplemented,
        deleteFolder: notImplemented

    };

    if (gpf.internals) {

        describe("(internal)", function () {

            describe("_gpfFsExploreEnumerator", function () {

                it("helps building explore functions", function (done) {
                    testStorage.explore("test/data", function (event) {
                        try {
                            assert(gpf.events.EVENT_READY === event.type);
                            var enumerator = event.get("enumerator");
                            assert(true === gpf.interfaces.isImplementedBy(enumerator, gpf.interfaces.IEnumerator));
                            done();
                        } catch (e) {
                            done(e);
                        }
                    });
                });

                it("supports reset"/*, function (done) {
                    testStorage.explore("test/data", function (event) {
                        try {
                            assert(gpf.events.EVENT_READY === event.type);
                            var enumerator = event.get("enumerator");

                            assert(true === gpf.interfaces.isImplementedBy(enumerator, gpf.interfaces.IEnumerator));
                            done();
                        } catch (e) {
                            done(e);
                        }
                    });

                }*/);

                it("forwards errors");

                it("automates calling getInfo from file path", function (done) {
                    var results = [];
                    function endEnumCallback (event) {
                        try {
                            assert(gpf.events.EVENT_END_OF_DATA === event.type);
                            assert(1 === results.length);
                            done();
                        } catch (e) {
                            done(e);
                        }
                    }
                    testStorage.explore("test/data", function (event) {
                        try {
                            assert(gpf.events.EVENT_READY === event.type);
                            var enumerator = event.get("enumerator");
                            gpf.interfaces.IEnumerator.each(enumerator, function (fileInfo) {
                                try {
                                    assert("number" === typeof fileInfo.type);
                                    assert("string" === typeof fileInfo.fileName);
                                    assert("string" === typeof fileInfo.filePath);
                                    assert("number" === typeof fileInfo.size);
                                    assert(null === fileInfo.createdDateTime
                                           || fileInfo.createdDateTime instanceof Date);
                                    assert(null === fileInfo.modifiedDateTime
                                           || fileInfo.modifiedDateTime instanceof Date);
                                    results.push(fileInfo);
                                } catch (e) {
                                    done(e);
                                }

                            }, endEnumCallback);
                        } catch (e) {
                            done(e);
                        }
                    });
                });

            });

            describe("_gpfFsBuildFindMethod", function () {

                // var _gpfFsBuildFindMethod = gpf.internals._gpfFsBuildFindMethod;

                it("exists");

            });

        });

    }

    if (null === gpf.fs.host()) {
        return; // No host
    }

    describe("Data generator", function () {

        it("file.bin", function (done) {
            var fs = gpf.fs.host();
            fs.getInfo("test/data/file.bin", function (infoEvent) {
                if (infoEvent.type === gpf.events.EVENT_READY && infoEvent.get("info").type === gpf.fs.TYPE_FILE) {
                    done(); // Already exists
                    return;
                }
                // File does not exist, generates
                var count = 0,
                    wStream;
                function loop (event) {
                    assert(!event || gpf.events.EVENT_READY === event.type);
                    if (256 === count) {
                        fs.close(wStream, function () {});
                        done();
                    }
                    wStream.write([count++], loop);
                }
                // Current path is always root of gpf-js
                fs.writeAsBinaryStream("test/data/file.bin", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    wStream = event.get("stream");
                    loop();
                });
            });
        });

    });

    describe("gpf.fs.host", function () {

        var gpfI = gpf.interfaces,
            iFs;

        beforeEach(function () {
            iFs = gpf.fs.host();
        });

        it("gives access to a IFileStorage interface", function () {
            assert(null !== iFs);
            if (null !== iFs) {
                assert(gpfI.isImplementedBy(iFs, gpfI.IFileStorage));
            }
        });

        describe("getInfo", function () {

            function checkInfo (info) {
                assert(null !== info);
                assert("object" === typeof info);
                assert("number" === typeof info.type);
                assert("string" === typeof info.fileName);
                assert("string" === typeof info.filePath);
                assert("number" === typeof info.size);
                assert("object" === typeof info.createdDateTime);
                assert("object" === typeof info.modifiedDateTime);
            }

            it("tells if a file exists", function (done) {
                iFs.getInfo("src/fs.js", function (event) {
                    assert(event.type === gpf.events.EVENT_READY);
                    var info = event.get("info");
                    checkInfo(info);
                    assert(info.type === gpf.fs.TYPE_FILE);
                    assert(info.size > 0);
                    done();
                });
            });

            it("tells if a directory exists", function (done) {
                iFs.getInfo("src", function (event) {
                    assert(event.type === gpf.events.EVENT_READY);
                    var info = event.get("info");
                    checkInfo(info);
                    assert(info.type === gpf.fs.TYPE_DIRECTORY);
                    // size is not relevant
                    done();
                });
            });

            it("tells if a path does not exists", function (done) {
                iFs.getInfo("null", function (event) {
                    assert(event.type === gpf.events.EVENT_READY);
                    var info = event.get("info");
                    assert(null !== info);
                    assert("object" === typeof info);
                    assert(info.type === gpf.fs.TYPE_NOT_FOUND);
                    done();
                });
            });

        });

        function _close (stream) {
            iFs.close(stream, function () {}); // ignore
        }

        describe("readAsBinaryStream", function () {

            it("reads binary files", function (done) {
                iFs.readAsBinaryStream("test/data/file.bin", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    var rStream = event.get("stream");
                    rStream.read(1, function (readEvent) {
                        assert(gpf.events.EVENT_DATA === readEvent.type);
                        var buffer = readEvent.get("buffer");
                        assert(1 === buffer.length);
                        assert(0 === buffer[0]);
                        _close(rStream);
                        done();
                    });
                });
            });

        });

        describe("writeAsBinaryStream", function () {

            it("writes binary files", function (done) {
                iFs.writeAsBinaryStream("tmp/test/fs_" + gpf.host() + ".bin", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    var wStream = event.get("stream");
                    wStream.write([0, 34, 75, 0, 128, 255], function (writeEvent) {
                        assert(gpf.events.EVENT_READY === writeEvent.type);
                        _close(wStream);
                        done();
                    });
                });
            });

        });

        describe("explore", function () {

            it("fails on a file", function (done) {
                iFs.explore("src/fs.js", function (event) {
                    assert(gpf.events.EVENT_ERROR === event.type);
                    done();
                });
            });

            it("gets an IEnumerator on a folder", function (done) {
                iFs.explore("src", function (event) {
                    assert(gpf.events.EVENT_READY === event.type);
                    var enumerator = event.get("enumerator");
                    assert(null !== enumerator);
                    assert(gpf.interfaces.isImplementedBy(enumerator, gpf.interfaces.IEnumerator));
                    done();
                });
            });

        });

    });

});
<|endoftext|>