@reptilbud/etcd3-temp
Version:
Hapiness module for etcd3
211 lines • 8.63 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "@hapiness/core", "rxjs", "../interfaces", "../etcd3.extension"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@hapiness/core");
const rxjs_1 = require("rxjs");
const interfaces_1 = require("../interfaces");
// import { Etcd3Manager } from '../managers';
const etcd3_extension_1 = require("../etcd3.extension");
let Etcd3Service = class Etcd3Service {
constructor(_manager) {
this._manager = _manager;
let basePath = '/';
if (this._manager.config.basePath) {
basePath = this._manager.config.basePath;
if (!this._manager.config.basePath.endsWith('/')) {
basePath = `${basePath}/`;
}
}
this._basePath = basePath;
this._client = this._manager.client.namespace(this._basePath);
}
/**
*
* @returns {string} The value of the base path
*
*/
get basePath() {
return this._basePath;
}
/**
*
* Retrieve the client without basePath consideration
*
* @returns {Namespace} the client for the namespace
*
*/
get client() {
return this._client;
}
/**
*
* Retrieve the client without basePath consideration
*
* @returns {Etcd3} the normal client (without namespace consideration)
*
*/
etcd3Client() {
return this._manager.client;
}
/******************************************************************************************
*
* KV operations
*
******************************************************************************************/
/**
*
* Get the value stored at path `key`.
*
* @param {string} key The key you want to retrieve the value
* @param {ResponseFormat} format The format you want for the result (default is string)
*
* @returns {string | object | number | Buffer | null | Error} The value of the object stored
*
*/
get(key, format = interfaces_1.ResponseFormat.String) {
const promise = this.client.get(key);
switch (format) {
case interfaces_1.ResponseFormat.String:
return rxjs_1.Observable.fromPromise(promise.string());
case interfaces_1.ResponseFormat.Json:
return rxjs_1.Observable.fromPromise(promise.json());
case interfaces_1.ResponseFormat.Buffer:
return rxjs_1.Observable.fromPromise(promise.buffer());
default:
}
return rxjs_1.Observable.throw(new Error('Format not supported'));
}
/**
*
* Append the value `value` at path `key`.
*
* @param {string} key The key you want to retrieve the value
* @param {string | Buffer | number} value The format you want for the result (default is string)
*
* @returns {IPutResponse} The result of the operation
*
*/
put(key, value) {
if (!value) {
return rxjs_1.Observable.throw(new Error('"value" should not be null nor undefined'));
}
let _value;
if (typeof value === 'object') {
try {
const tmp = JSON.parse(JSON.stringify(value));
if (tmp.type !== 'Buffer') {
_value = JSON.stringify(value);
}
}
catch (err) {
return rxjs_1.Observable.throw(new Error('Unknown type of "value"'));
}
}
else {
_value = value;
}
return rxjs_1.Observable.fromPromise(this.client.put(key).value(_value).exec());
}
/******************************************************************************************
*
* Watch operations
*
******************************************************************************************/
/**
*
* Create a watcher for a specific key.
*
* @param {string} key The key you want to watch
*
* @returns {Watcher} The watcher instance created
*
*/
createWatcher(key) {
return rxjs_1.Observable.fromPromise(this.client.watch().key(key).create());
}
/******************************************************************************************
*
* Lock operations
*
******************************************************************************************/
/**
*
* Create and acquire a lock for a key `key` specifying a ttl.
* It will automatically contact etcd to keep the connection live.
* When the connection is broken (end of process or lock released),
* the TTL is the time after when the lock will be released.
*
* @param {string} key The key
* @param {number} ttl The TTL value in seconds. Default value is 1
*
* @returns {Lock} The lock instance created
*
*/
acquireLock(key, ttl = 1) {
return rxjs_1.Observable.fromPromise(this.client.lock(key).ttl(ttl || 1).acquire());
}
/******************************************************************************************
*
* Lease Operations
*
******************************************************************************************/
/**
*
* Create a lease object with a ttl.
* The lease is automatically keeping alive until it is close.
*
* @param {number} ttl The TTL value in seconds. Default value is 1
*
* @returns {Lease} The lease instance created
*
*/
createLease(ttl = 1) {
return rxjs_1.Observable.of(this.client.lease(ttl || 1));
}
/**
*
* Create a lease object with a ttl and attach directly a key-value to it.
* The lease is automatically keeping alive until it is close.
*
* NOTE: Once the lease is closed, the key-value will be destroyed by etcd.
*
* @param {string} key The key where to store the value
* @param {string | Buffer | number} value The value that will be stored at `key` path
* @param {number} ttl The TTL value in seconds. Default value is 1
*
* @returns {Lease} The lease instance created
*
*/
createLeaseWithValue(key, value, ttl = 1) {
const lease = this.client.lease(ttl || 1);
return rxjs_1.Observable.fromPromise(lease.put(key).value(value).exec().then(_ => lease));
}
};
Etcd3Service = __decorate([
core_1.Injectable(),
__param(0, core_1.Inject(etcd3_extension_1.Etcd3Ext)),
__metadata("design:paramtypes", [Object])
], Etcd3Service);
exports.Etcd3Service = Etcd3Service;
});
//# sourceMappingURL=etcd3.service.js.map