<commit_before>var Dnsd = require("dnsd");
var Client = require("ssb-client");
var Pull = require("pull-stream");
var Paramap = require("pull-paramap");









var Config;
try {
    Config = require("./config.js");
} catch (err) {
    console.error("\nCouldn't find './config.js'. Using defaults");
    Config = {
        port: 53053,
        host: '127.0.0.1',
        ready: function () {
            console.log('Server running at %s:%s', Config.host, Config.port);
        },
    };
}

var log = {
    req: function (req) {
        var q = req.question[0];
        console.log({
            name: q.name,
            type: q.type,
            class: q.class,
        });
    },
};

var answer = function (sbot, req, res) {
    console.log();
    log.req(req);

    var q = req.question;

    if (!q.length) {
        console.log("invalid question");
        res.end();
    }

    var name = q[0].name;
    var type = q[0].type;

    Pull(sbot.messagesByType({
        type: 'ssb-dns',
    }),
    Paramap(function getAvatar(msg, cb) {
        cb(null, msg);
    }),
    Pull.drain(function printMessage(msg) {
        var text = msg.value.content.text;

        var result;
        try {
            result = JSON.parse(text);
        } catch (err) {
            throw new Error("Invalid record!");
        }
        if (name && type && result.name === name && result.type === type && result.value) {
            console.log("%s (%s) => %s", name, result.type, result.value);
            res.answer.push({
                name: result.name,
                type: result.type,
                data: result.value,
                ttl: 500,
            });
            res.end()
        }
    }));
};

var createServer = function (sbot, port, host, cb) {
    Dnsd.createServer(function(req, res) {
        answer(sbot, req, res);
    }).listen(port, host, cb);
};

Client(function (err, sbot) {
    if (err) {
        console.error(err);
        return void process.exit(1);
    }
    var port = 53053;
    var host = '127.0.0.1';
    createServer(sbot, Config.port, Config.host, Config.ready);
});
<commit_msg>assume the record is json, not a string<commit_after>var Dnsd = require("dnsd");
var Client = require("ssb-client");
var Pull = require("pull-stream");
var Paramap = require("pull-paramap");

var Config;
try {
    Config = require("./config.js");
} catch (err) {
    console.error("\nCouldn't find './config.js'. Using defaults");
    Config = require("./config.dist.js");
}

var log = {
    req: function (req) {
        var q = req.question[0];
        console.log({
            name: q.name,
            type: q.type,
            class: q.class,
        });
    },
};

var answer = function (sbot, req, res) {
    console.log();
    log.req(req);

    var q = req.question;

    if (!q.length) {
        console.log("invalid question");
        res.end();
    }

    var name = q[0].name;
    var type = q[0].type;

    Pull(sbot.messagesByType({
        type: 'ssb-dns',
    }),
    Paramap(function getAvatar(msg, cb) {
        cb(null, msg);
    }),
    Pull.drain(function printMessage(msg) {
        var record = msg.value.content.record;

        if (typeof(record) !== 'object') { return; }

        if (name && type &&
            record.name === name &&
            record.type === type &&
            record.value) {
            console.log("%s (%s) => %s", name, record.type, record.value);

            // returns the first matching record found
            // TODO support multiple returned values
            res.answer.push({
                name: record.name,
                type: record.type,
                data: record.value,
                ttl: 500,
            });
            res.end()
        }
    }));
};

var createServer = function (sbot, port, host, cb) {
    Dnsd.createServer(function(req, res) {
        answer(sbot, req, res);
    }).listen(port, host, cb);
};

Client(function (err, sbot) {
    if (err) {
        console.error(err);
        return void process.exit(1);
    }
    createServer(sbot, Config.port, Config.host, Config.ready);
});

{'fraction_non_alphanumeric': 0.16251830161054173, 'fraction_numerical': 0.008784773060029283, 'mean_word_length': 1.8504867872044506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '543658', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1293, 'n_words': 306}