digd.js/lib/store.json.js

101 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-11-05 16:16:27 +00:00
'use strict';
module.exports.create = function (opts) {
// opts = { filepath };
var engine = { db: null };
var db = require(opts.filepath);
engine.primaryNameservers = db.primaryNameservers;
2018-01-17 08:03:05 +00:00
engine.zoneToSoa = function (domain) {
var nameservers = domain.vanityNs || engine.primaryNameservers;
var index = Math.floor(Math.random() * nameservers.length) % nameservers.length;
var nameserver = nameservers[index];
return {
name: domain.id
, typeName: 'SOA'
, className: 'IN'
, ttl: domain.ttl || 60
// nameserver -- select an NS at random if they're all in sync
, primary: nameserver
, name_server: nameserver
// admin -- email address or domain for admin
, admin: domain.admin || ('admin.' + domain.id)
, email_addr: domain.admin || ('admin.' + domain.id)
// serial -- the version, for cache-busting of secondary nameservers. suggested format: YYYYMMDDnn
, serial: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000)
, sn: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000)
// refresh -- only used when nameservers following the DNS NOTIFY spec talk
, refresh: domain.refresh || 1800
, ref: domain.refresh || 1800
// retry -- only used when nameservers following the DNS NOTIFY spec talk
, retry: domain.retry || 600
, ret: domain.retry || 600
// expiration -- how long other nameservers should continue when the primary goes down
, expiration: domain.expiration || 2419200
, ex: domain.expiration || 2419200
// minimum -- how long to cache a non-existent domain (also the default ttl for BIND)
, minimum: domain.minimum || 5
, nx: domain.minimum || 5
};
};
2018-01-10 21:54:08 +00:00
engine.peers = {
all: function (cb) {
process.nextTick(function () {
cb(null, db.primaryNameservers.map(function (ns) {
return { name: ns };
}));
});
}
};
2018-01-10 08:14:05 +00:00
engine.zones = {
2018-01-10 08:25:56 +00:00
all: function (cb) {
2018-01-10 08:14:05 +00:00
process.nextTick(function () {
cb(null, db.domains.slice(0));
});
}
, get: function (query, cb) {
var myDomains = db.domains.filter(function (d) {
return -1 !== query.names.indexOf(d.id.toLowerCase());
});
process.nextTick(function () {
cb(null, myDomains);
});
}
2017-11-05 16:16:27 +00:00
};
2018-01-10 08:14:05 +00:00
engine.records = {
2018-01-10 08:25:56 +00:00
all: function (cb) {
2018-01-10 08:14:05 +00:00
process.nextTick(function () {
cb(null, db.records.slice(0));
});
}
, get: function (query, cb) {
var myRecords = db.records.slice(0).filter(function (r) {
2017-11-05 16:16:27 +00:00
2018-01-10 08:14:05 +00:00
if ('string' !== typeof r.name) {
return false;
}
2017-11-05 16:16:27 +00:00
2018-01-10 08:14:05 +00:00
// TODO use IN in masterquest (or implement OR)
// Only return single-level wildcard?
if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) {
return true;
}
});
process.nextTick(function () {
cb(null, myRecords);
});
}
2017-11-05 16:16:27 +00:00
};
return engine;
};