<commit_before>var util = require('util');
var net = require('net');
var http = require('http');
var WebsocketServer = require('websocket').server;
var assert = require('assert');
var errors = require('./errors');
var EventEmitter = require('events').EventEmitter;
var MessageSocket = require('./message-socket.js').MessageSocket;

exports.createDaemon = function() {
    var peers = {};
    var nodes = {};
    var leaves = {};
    var routes = {};

    var daemon = new EventEmitter();

    var routeResponse = function(peer, message) {
        var route = routes[message.id];
        if (route) {
            delete routes[message.id];
            message.id = route.id;
            route.receiver.sendMessage(message);
        }
        else {
            console.log('cannot router message', message);
        }
    };

    /* publishes a notification to all subsbribers / fetchers */
    var publish = function(notification) {
        var path = notification.path;
        daemon.emit('publish', notification);
        for (var peerId in peers) {
            var peer = peers[peerId];
            var fetchers = peer.fetchers;
            for (var fetchId in fetchers) {
                if (fetchers[fetchId](path)) {
                    peer.sendMessage({
                        method: fetchId,
                        params: notification
                    });
                }
            }
        }
    };

    /* creates a match function from an array of match and unmatch
       expressions.
    */
    var matcher = function(match, unmatch) {
        return function(path) {
            for (var i = 0; i < unmatch.length; ++i) {
                if (path.match(unmatch[i])) {
                    return false;
                }
            }
            for (i = 0; i < match.length; ++i) {
                if (path.match(match[i])) {
                    return true;
                }
            }
            return false;
        };
    };

    var post = function(peer, message) {
        var notification = message.params;
        var path = notification.path;
        var event = notification.event;
        var data = notification.data;
        var leave = leaves[path];
        if (leave) {
            if (event === 'change') {
                if (data.value) {
                    leave.element.value = data.value;
                }
                if (data.schema) {
                    leave.element.schema = data.schema;
                }
            }
            publish(notification);
        }
        else {
            var error = errors.invalidParams({
                invalidPath: path
            });
            if (message.id) {
                peer.sendMessage({
                    id: message.id,
                    error: error
                });
            }
            else {
                console.log('post failed with invalid path', message);
            }
        }
    };

    var fetch = function(peer, message) {
        var params = message.params;
        var fetchId = params.id;
        var match = params.match;
        var unmatch = params.unmatch || [];
        var matchf = matcher(match, unmatch);
	var notifications = [];
        if (!peer.fetchers[fetchId]) {	    
            for (var path in nodes) {
                if (matchf(path)) {
                    var notification = {
                        method: fetchId,
                        params: {
                            path: path,
                            event: 'add',
                            data: {
                                type: 'node'
                            }
                        }
                    };
                    notifications.push(notification);
                }
            }
            notifications.sort(function(a, b) {
                return a.length - b.length;
            });
            for (path in leaves) {
                if (matchf(path)) {
                    var leave = leaves[path];
		    var notification = {
                        method: fetchId,
                        params: {
                            path: path,
                            event: 'add',
                            data: leave.element
                        }
                    };
                }
            }
        }<commit_msg>dont forget to push non nodes<commit_after>var util = require('util');
var net = require('net');
var http = require('http');
var WebsocketServer = require('websocket').server;
var assert = require('assert');
var errors = require('./errors');
var EventEmitter = require('events').EventEmitter;
var MessageSocket = require('./message-socket.js').MessageSocket;

exports.createDaemon = function() {
    var peers = {};
    var nodes = {};
    var leaves = {};
    var routes = {};

    var daemon = new EventEmitter();

    var routeResponse = function(peer, message) {
        var route = routes[message.id];
        if (route) {
            delete routes[message.id];
            message.id = route.id;
            route.receiver.sendMessage(message);
        }
        else {
            console.log('cannot router message', message);
        }
    };

    /* publishes a notification to all subsbribers / fetchers */
    var publish = function(notification) {
        var path = notification.path;
        daemon.emit('publish', notification);
        for (var peerId in peers) {
            var peer = peers[peerId];
            var fetchers = peer.fetchers;
            for (var fetchId in fetchers) {
                if (fetchers[fetchId](path)) {
                    peer.sendMessage({
                        method: fetchId,
                        params: notification
                    });
                }
            }
        }
    };

    /* creates a match function from an array of match and unmatch
       expressions.
    */
    var matcher = function(match, unmatch) {
        return function(path) {
            for (var i = 0; i < unmatch.length; ++i) {
                if (path.match(unmatch[i])) {
                    return false;
                }
            }
            for (i = 0; i < match.length; ++i) {
                if (path.match(match[i])) {
                    return true;
                }
            }
            return false;
        };
    };

    var post = function(peer, message) {
        var notification = message.params;
        var path = notification.path;
        var event = notification.event;
        var data = notification.data;
        var leave = leaves[path];
        if (leave) {
            if (event === 'change') {
                if (data.value) {
                    leave.element.value = data.value;
                }
                if (data.schema) {
                    leave.element.schema = data.schema;
                }
            }
            publish(notification);
        }
        else {
            var error = errors.invalidParams({
                invalidPath: path
            });
            if (message.id) {
                peer.sendMessage({
                    id: message.id,
                    error: error
                });
            }
            else {
                console.log('post failed with invalid path', message);
            }
        }
    };

    var fetch = function(peer, message) {
        var params = message.params;
        var fetchId = params.id;
        var match = params.match;
        var unmatch = params.unmatch || [];
        var matchf = matcher(match, unmatch);
	var notifications = [];
        if (!peer.fetchers[fetchId]) {	    
            for (var path in nodes) {
                if (matchf(path)) {
                    var notification = {
                        method: fetchId,
                        params: {
                            path: path,
                            event: 'add',
                            data: {
                                type: 'node'
                            }
                        }
                    };
                    notifications.push(notification);
                }
            }
            notifications.sort(function(a, b) {
                return a.length - b.length;
            });
            for (path in leaves) {
                if (matchf(path)) {
                    var leave = leaves[path];
		    var notification = {
                        method: fetchId,
                        params: {
                            path: path,
                            event: 'add',
                            data: leave.element
                        }
                    };
                    notifications.push(notification);
                }
            }
        }
{'fraction_non_alphanumeric': 0.10298850574712644, 'fraction_numerical': 0.0004597701149425287, 'mean_word_length': 1.0835727969348659, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4072855', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2044, 'n_words': 522}