etcd3-mock
Version:
147 lines (146 loc) • 4.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PutBuilder = exports.SingleRangeBuilder = exports.MultiRangeBuilder = void 0;
const etcd3_1 = require("etcd3");
const RPC = require("etcd3/lib/rpc");
const util_1 = require("etcd3/lib/util");
const emptyBuffer = Buffer.from([]);
function assertWithin(map, value, thing) {
if (!(value in map)) {
const keys = Object.keys(map).join('" "');
throw new Error(`Unexpected "${value}" in ${thing}. Possible values are: "${keys}"`);
}
}
class MultiRangeBuilder extends util_1.PromiseWrap {
constructor(store, namespace) {
super();
this.store = store;
this.namespace = namespace;
this.request = {};
this.prefix(emptyBuffer);
}
prefix(value) {
return this.inRange(etcd3_1.Range.prefix(value));
}
inRange(r) {
const range = etcd3_1.Range.from(r);
this.request.key = range.start;
this.request.range_end = range.end;
return this;
}
all() {
return this.prefix('');
}
limit(count) {
this.request.limit = isFinite(count) ? count : 0;
return this;
}
sort(target, order) {
assertWithin(RPC.SortTarget, target, 'sort order in client.get().sort(...)');
assertWithin(RPC.SortOrder, order, 'sort order in client.get().sort(...)');
this.request.sort_target = RPC.SortTarget[target];
this.request.sort_order = RPC.SortOrder[order];
return this;
}
count() {
this.request.count_only = true;
return this.exec().then(res => Number(res.count));
}
keys(encoding = 'utf8') {
this.request.keys_only = true;
return this.exec().then(res => {
return res.kvs.map(kv => kv.key.toString(encoding));
});
}
keyBuffers() {
this.request.keys_only = true;
return this.exec().then(res => {
return res.kvs.map(kv => kv.key);
});
}
json() {
return this.mapValues(buf => JSON.parse(buf.toString()));
}
strings(encoding = 'utf8') {
return this.mapValues(buf => buf.toString(encoding));
}
numbers() {
return this.mapValues(buf => Number(buf.toString()));
}
buffers() {
return this.mapValues(b => b);
}
exec() {
return this.store.range(this.namespace.applyToRequest(this.request), {}).then(res => {
for (const kv of res.kvs) {
kv.key = this.namespace.unprefix(kv.key);
}
return res;
});
}
createPromise() {
return this.strings();
}
mapValues(iterator) {
return this.exec().then(res => {
const output = {};
for (const kv of res.kvs) {
output[kv.key.toString()] = iterator(kv.value);
}
return output;
});
}
}
exports.MultiRangeBuilder = MultiRangeBuilder;
class SingleRangeBuilder {
constructor(store, namespace, key) {
this.store = store;
this.namespace = namespace;
this.request = {};
this.request.key = util_1.toBuffer(key);
}
json() {
return this.string().then(JSON.parse);
}
string(encoding = 'utf8') {
return this.exec().then(res => res.kvs.length === 0 ? null : res.kvs[0].value.toString(encoding));
}
number() {
return this.string().then(value => (value === null ? null : Number(value)));
}
buffer() {
return this.exec().then(res => (res.kvs.length === 0 ? null : res.kvs[0].value));
}
exists() {
this.request.keys_only = true;
return this.exec().then(r => r.count !== '0');
}
exec() {
return this.store.range(this.namespace.applyToRequest(this.request), {});
}
then() {
return this.exec();
}
}
exports.SingleRangeBuilder = SingleRangeBuilder;
class PutBuilder extends util_1.PromiseWrap {
constructor(store, key) {
super();
this.store = store;
this.request = {};
this.request.key = util_1.toBuffer(key);
return this;
}
value(value) {
this.request.value = util_1.toBuffer(value);
return this;
}
exec() {
this.store.setCache(this.request.key, this.request.value);
}
createPromise() {
this.exec();
return Promise.resolve(null);
}
}
exports.PutBuilder = PutBuilder;