<commit_before>"use strict";

var Purger    = require('htcp-purge');
var P         = require('bluebird');
var HTTPError = require('hyperswitch').HTTPError;

var EventService = function(options) {
    this.options = options;

    if (options && options.purge) {
        this.purger = new Purger({ routes: [ options.purge ] });
    }
};

EventService.prototype.emitEvent = function(hyper, req) {
    var self = this;
    if (this.purger) {
        P.try(function() {
            if (!Array.isArray(req.body)) {
                throw new HTTPError({
                    status: 400,
                    body: {
                        type: 'events',
                        description: 'Invalid request for event service.'
                    }
                });
            }

            return self.purger.purge(req.body.map(function(event) {
                if (!event.meta || !event.meta.uri) {
                    hyper.log('error/events/purge', {
                        message: 'Invalid event URI',
                        event: event
                    });
                } else {
                    var uri = event.meta.uri.toString();
                    if (!/^https?:/.test(uri)) {
                        uri = 'http:' + uri;
                    }
                    return uri;
                }
            })
            .filter(function(event) { return !!event; }));
        })
        .catch(function(e) {
            hyper.log('error/events/purge', e);
        });
    }
    return P.resolve({ status: 200 });
};

module.exports = function(options) {
    var es = new EventService(options);

    return {
        spec: {
            paths: {
                '/': {
                    post: {
                        operationId: 'emitEvent'
                    }
                }
            }
        },
        operations: {
            emitEvent: es.emitEvent.bind(es)
        }
    };
};<commit_msg>Check that a uri is proto-relative<commit_after>"use strict";

var Purger    = require('htcp-purge');
var P         = require('bluebird');
var HTTPError = require('hyperswitch').HTTPError;

var EventService = function(options) {
    this.options = options;

    if (options && options.purge) {
        this.purger = new Purger({ routes: [ options.purge ] });
    }
};

EventService.prototype.emitEvent = function(hyper, req) {
    var self = this;
    if (this.purger) {
        P.try(function() {
            if (!Array.isArray(req.body)) {
                throw new HTTPError({
                    status: 400,
                    body: {
                        type: 'events',
                        description: 'Invalid request for event service.'
                    }
                });
            }

            return self.purger.purge(req.body.map(function(event) {
                if (!event.meta || !event.meta.uri || /^\/\/:/.test(event.meta.uri)) {
                    hyper.log('error/events/purge', {
                        message: 'Invalid event URI',
                        event: event
                    });
                } else {
                    var uri = event.meta.uri.toString();
                    if (!/^https?:/.test(uri)) {
                        uri = 'http:' + uri;
                    }
                    return uri;
                }
            })
            .filter(function(event) { return !!event; }));
        })
        .catch(function(e) {
            hyper.log('error/events/purge', e);
        });
    }
    return P.resolve({ status: 200 });
};

module.exports = function(options) {
    var es = new EventService(options);

    return {
        spec: {
            paths: {
                '/': {
                    post: {
                        operationId: 'emitEvent'
                    }
                }
            }
        },
        operations: {
            emitEvent: es.emitEvent.bind(es)
        }
    };
};
{'fraction_non_alphanumeric': 0.13016845329249618, 'fraction_numerical': 0.0030627871362940277, 'mean_word_length': 1.1275787187839306, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4959279', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1015, 'n_words': 200}