<commit_before>const EosKnowledge = imports.gi.EosKnowledge;
const EosKnowledgeSearch = imports.EosKnowledgeSearch;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const WebKit2 = imports.gi.WebKit2;

function register_webkit_uri_handlers (article_render_callback) {
    let security_manager = WebKit2.WebContext.get_default().get_security_manager();
    security_manager.register_uri_scheme_as_local('ekn');
    EosKnowledge.private_register_global_uri_scheme('ekn', (req) => {
        _load_ekn_assets(req, article_render_callback);
    });
    security_manager.register_uri_scheme_as_local('resource');
    EosKnowledge.private_register_global_uri_scheme('resource', _load_gresource_assets);
}

function _load_ekn_assets (req, article_render_callback) {
    try {
        EosKnowledgeSearch.Engine.get_default().get_object_by_id(req.get_uri(), function (err, model) {
            if (model instanceof EosKnowledgeSearch.ArticleObjectModel) {
                let html = article_render_callback(model);
                let stream = Gio.MemoryInputStream.new_from_data(html, -1, null);
                req.finish(stream, -1, 'text/html; charset=utf-8');
            } else {
                let file = Gio.File.new_for_uri(model.content_uri);
                req.finish(file.read(null), -1, null);
            }
        });
    } catch (error) {
        printerr(error);
        printerr(error.stack);
        req.finish_error(new Gio.IOErrorEnum({ message: error.message, code: 0 }));
    }
}

function _load_gresource_assets (req) {
    try {
        let file = Gio.File.new_for_uri(req.get_uri());
        req.finish(file.read(null), -1, null);
    } catch (error) {
        printerr(error);
        printerr(error.stack);<commit_msg>Create input stream from bytes, not data<commit_after>const ByteArray = imports.byteArray;
const EosKnowledge = imports.gi.EosKnowledge;
const EosKnowledgeSearch = imports.EosKnowledgeSearch;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const WebKit2 = imports.gi.WebKit2;

function register_webkit_uri_handlers (article_render_callback) {
    let security_manager = WebKit2.WebContext.get_default().get_security_manager();
    security_manager.register_uri_scheme_as_local('ekn');
    EosKnowledge.private_register_global_uri_scheme('ekn', (req) => {
        _load_ekn_assets(req, article_render_callback);
    });
    security_manager.register_uri_scheme_as_local('resource');
    EosKnowledge.private_register_global_uri_scheme('resource', _load_gresource_assets);
}

function _load_ekn_assets (req, article_render_callback) {
    try {
        EosKnowledgeSearch.Engine.get_default().get_object_by_id(req.get_uri(), function (err, model) {
            if (model instanceof EosKnowledgeSearch.ArticleObjectModel) {
                let html = article_render_callback(model);
                let bytes = ByteArray.fromString(html).toGBytes();
                let stream = Gio.MemoryInputStream.new_from_bytes(bytes);
                req.finish(stream, -1, 'text/html; charset=utf-8');
            } else {
                let file = Gio.File.new_for_uri(model.content_uri);
                req.finish(file.read(null), -1, null);
            }
        });
    } catch (error) {
        printerr(error);
        printerr(error.stack);
        req.finish_error(new Gio.IOErrorEnum({ message: error.message, code: 0 }));
    }
}

function _load_gresource_assets (req) {
    try {
        let file = Gio.File.new_for_uri(req.get_uri());
        req.finish(file.read(null), -1, null);
    } catch (error) {
        printerr(error);
        printerr(error.stack);
{'fraction_non_alphanumeric': 0.11473596903511198, 'fraction_numerical': 0.004700027647221454, 'mean_word_length': 3.3122765196662693, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '496115', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1145, 'n_words': 179}