Compare commits

..

1 Commits

Author SHA1 Message Date
Vitaliy Filippov bf7dd6646d Move base64 handling into antietcd.js 2024-06-08 17:20:47 +03:00
7 changed files with 25 additions and 64 deletions

View File

@ -100,9 +100,6 @@ Specify &lt;ca&gt; = &lt;cert&gt; if your certificate is self-signed.</dd>
<dt>--ws_keepalive_interval 30000</dt>
<dd>Client websocket ping (keepalive) interval in milliseconds</dd>
<dt>--use_base64 1</dt>
<dd>Use base64 encoding of keys and values, like in etcd (enabled by default).</dd>
</dl>
### Persistence

View File

@ -43,8 +43,6 @@ HTTP:
Require TLS client certificates signed by <ca> or by default CA to connect.
--ws_keepalive_interval 30000
Client websocket ping (keepalive) interval in milliseconds
--use_base64 1
Use base64 encoding of keys and values, like in etcd (enabled by default).
Persistence:
@ -107,14 +105,7 @@ function parse()
options[arg.substr(2)] = process.argv[++i];
}
}
if ('stale_read' in options)
{
options['stale_read'] = options['stale_read'] === '1' || options['stale_read'] === 'yes' || options['stale_read'] === 'true';
}
if ('use_base64' in options)
{
options['use_base64'] = options['use_base64'] === '1' || options['use_base64'] === 'yes' || options['use_base64'] === 'true';
}
if (options['persist_filter'])
{
options['persist_filter'] = require(options['persist_filter'])(options);

View File

@ -22,14 +22,6 @@ class AntiEtcd extends EventEmitter
constructor(cfg)
{
super();
if (!('use_base64' in cfg))
{
cfg.use_base64 = 1;
}
if (!('stale_read' in cfg))
{
cfg.stale_read = 1;
}
this.clients = {};
this.client_id = 1;
this.etctree = new EtcTree(true);
@ -140,11 +132,11 @@ class AntiEtcd extends EventEmitter
if (!this.cluster)
{
// Run deletion compaction without followers
const mod_revision = this.etctree.mod_revision;
if (mod_revision - this.etctree.compact_revision > (this.cfg.compact_revisions||1000)*2)
const mod_revision = this.antietcd.etctree.mod_revision;
if (mod_revision - this.antietcd.etctree.compact_revision > (this.cfg.compact_revisions||1000)*2)
{
const revision = mod_revision - (this.cfg.compact_revisions||1000);
this.etctree.compact(revision);
this.antietcd.etctree.compact(revision);
}
}
}
@ -419,7 +411,7 @@ class AntiEtcd extends EventEmitter
// public watch API
async create_watch(params, callback)
{
const watch = this.etctree.api_create_watch(this._encodeWatch(params), (msg) => callback(this._encodeMsg(msg)));
const watch = this.etctree.api_create_watch({ ...params, watch_id: null }, callback);
if (!watch.created)
{
throw new RequestError(400, 'Requested watch revision is compacted', { compact_revision: watch.compact_revision });
@ -542,7 +534,15 @@ class AntiEtcd extends EventEmitter
const create_request = msg.create_request;
if (!create_request.watch_id || !client.watches[create_request.watch_id])
{
const watch = this.etctree.api_create_watch(this._encodeWatch(create_request), (msg) => socket.send(JSON.stringify(this._encodeMsg(msg))));
const req = { ...create_request, watch_id: null };
if (this.cfg.use_base64)
{
if (req.key != null)
req.key = de64(req.key);
if (req.range_end != null)
req.range_end = de64(req.range_end);
}
const watch = this.etctree.api_create_watch(req, (msg) => this.sendToSocket(socket, msg));
if (!watch.created)
{
socket.send(JSON.stringify({ result: { header: { revision: this.etctree.mod_revision }, watch_id: create_request.watch_id, ...watch } }));
@ -579,35 +579,6 @@ class AntiEtcd extends EventEmitter
}
}
_encodeWatch(create_request)
{
const req = { ...create_request, watch_id: null };
if (this.cfg.use_base64)
{
if (req.key != null)
req.key = de64(req.key);
if (req.range_end != null)
req.range_end = de64(req.range_end);
}
return req;
}
_encodeMsg(msg)
{
if (this.cfg.use_base64 && msg.result && msg.result.events)
{
return { ...msg, result: { ...msg.result, events: msg.result.events.map(ev => ({
...ev,
kv: !ev.kv ? ev.kv : {
...ev.kv,
key: b64(ev.kv.key),
value: b64(ev.kv.value),
},
})) } };
}
return msg;
}
_unsubscribeClient(client_id)
{
if (!this.clients[client_id])

View File

@ -8,7 +8,7 @@ const zlib = require('zlib');
const stableStringify = require('./stable-stringify.js');
const EtcTree = require('./etctree.js');
const { runCallbacks } = require('./common.js');
const { de64, runCallbacks } = require('./common.js');
class AntiPersistence
{
@ -60,18 +60,20 @@ class AntiPersistence
if (ev.lease)
{
// Values with lease are never persisted
if (this.prev_value[ev.key] !== undefined)
const key = de64(ev.key);
if (this.prev_value[key] !== undefined)
{
delete this.prev_value[ev.key];
delete this.prev_value[key];
changed = true;
}
}
else
{
const filtered = this.cfg.persist_filter(ev.key, ev.value == null ? undefined : ev.value);
if (!EtcTree.eq(filtered, this.prev_value[ev.key]))
const key = de64(ev.key);
const filtered = this.cfg.persist_filter(key, ev.value == null ? undefined : de64(ev.value));
if (!EtcTree.eq(filtered, this.prev_value[key]))
{
this.prev_value[ev.key] = filtered;
this.prev_value[key] = filtered;
changed = true;
}
}

View File

@ -22,7 +22,7 @@ function b64(k)
{
if (k == null) // null or undefined
return k;
return Buffer.from(k).toString('base64');
return this.use_base64 ? Buffer.from(k).toString('base64') : k;
}
function runCallbacks(obj, key, new_value)

View File

@ -573,7 +573,7 @@ class EtcTree
});
this.active_immediate.push(imm);
}
return { header: { revision: this.mod_revision }, watch_id, created: true };
return { watch_id, created: true };
}
_get_modified(events, cur, prefix, min_rev)

View File

@ -1,6 +1,6 @@
{
"name": "antietcd",
"version": "1.0.1",
"version": "1.0.0",
"description": "Simplistic etcd replacement based on TinyRaft",
"main": "antietcd.js",
"scripts": {