# @eyecone/blastmedia-registry

> ---

Latest version **1.0.2** (published 2016-09-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install @eyecone/blastmedia-registry
pnpm add @eyecone/blastmedia-registry
yarn add @eyecone/blastmedia-registry
bun add @eyecone/blastmedia-registry
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-09-30 |
| First published | 2016-09-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Maintainers | eyecone |

## Links

- npm: https://www.npmjs.com/package/@eyecone/blastmedia-registry
- npm.io page: https://npm.io/package/@eyecone/blastmedia-registry

## Dependencies (2)

- [node-etcd](https://npm.io/package/node-etcd.md) ^5.0.3
- [@eyecone/cb2prom](https://npm.io/package/@eyecone/cb2prom.md) ^1.0.2

## Recent versions

- 1.0.2 (latest) — 2016-09-30
- 1.0.1 — 2016-09-27
- 1.0.0 — 2016-09-27

## README

## Registry
#### Module
---

The registry is a shared environment holding configurations in a key-value fashion.

Atm, it uses `Etcd` (& `node-etcd`). Etcd needs to be installed & accessible (remember firewalls!).

---

## Methods

### .createRegistryValue(key, [options])

returns a `RegistryValue` watching the `key`

### .createRegistryValues(map)

returns an `Object` containing `RegistryValue` properties.

```javascript
const rethinkdbData = registry.createRegistryValues({
  hostname  : '/blastmedia/databases/rethinkdb/hostname',
  port      : '/blastmedia/databases/rethinkdb/port',
  password  : '/blastmedia/databases/rethinkdb/password',
  database  : '/blastmedia/databases/rethinkdb/services/shorturls/database',
  table     : '/blastmedia/databases/rethinkdb/services/shorturls/table'
});
console.log(rethinkdbData.hostname);
```

### .registerService(name, hostname, port, [serviceInfos = {}], [registryPath = '/blastmedia/services/${name}'])

### .getService(name, callback)

### .getServices([raw = false])

---

The class wraps the `node-etcd` methods & allows to use promises if no callback is specified.

## Basic usage *from `node-etcd`*

```javascript
var Registry = require('@eyecone/blastmedia-registry');
var registry = new Registry();
registry.set("key", "value");
registry.get("key", console.log);
```

Callbacks follows the default (error, result) nodejs convention:

```javascript
function callback(err, res) {
    console.log("Error: ", err);
    console.log("Return: ", res);
}
registry.get("key", callback);
// Error: null
// Return: { action: 'get', node: { key: '/key', value: 'value', modifiedIndex: 4, createdIndex: 4 } }
```

## Methods

### Registry(hosts = ['127.0.0.1:2379'], [options])

Create a new etcd client for a single host etcd setup

```javascript
registry = new Registry();
registry = new Registry("127.0.0.1:2379");
registry = new Registry("http://127.0.0.1:2379");
registry = new Registry("https://127.0.0.1:2379");
registry = new Registry(["http://127.0.0.1:2379"]);
```

### Registry(hosts, [options])

Create a new etcd client for a clustered etcd setup. Client will connect to
servers in random order. On failure it will try the next server. When all
servers have failed it will callback with error. If it suspects the cluster is
in leader election mode it will retry up to 3 times with exp backoff. Number of
retries can be controlled by adding `{ maxRetries: x }` as an option to requests.

```javascript
registry = new Registry(['127.0.0.1:2379','192.168.1.1:2379']);
registry = new Registry(['http://127.0.0.1:2379','http://192.168.1.1:2379']);
```

### .set(key, value = null, [options], [callback])

Set key to value, or create key/directory.

```javascript
registry.set("key");
registry.set("key", "value");
registry.set("key", "value", console.log);
registry.set("key", "value", { ttl: 60 }, console.log);
registry.set("key", "value", { maxRetries: 3 }, console.log);
```

Available options include:

- `ttl` (time to live in seconds)
- `prevValue` (previous value, for compare and swap)
- `prevExist` (existance test, for compare and swap)
- `prevIndex` (previous index, for compare and swap)

Will create a directory when used without value (value=null): `registry.set("directory/");`

### .compareAndSwap(key, value, oldvalue, [options], [callback])

Convenience method for test and set (set with {prevValue: oldvalue})

```javascript
registry.compareAndSwap("key", "newvalue", "oldvalue");
registry.compareAndSwap("key", "newValue", "oldValue", options, console.log);
```

Alias: `.testAndSet()`

### .get(key, [options], [callback])

Get a key or path.

```javascript
registry.get("key", console.log);
registry.get("key", { recursive: true }, console.log);
```

Available options include:

- `recursive` (bool, list all values in directory recursively)
- `wait` (bool, wait for changes to key)
- `waitIndex` (wait for changes after given index)

### .del(key, [options], [callback])

Delete a key or path

```javascript
registry.del("key");
registry.del("key", console.log);
registry.del("key/", { recursive: true }, console.log);
```

Available options include:

- `recursive` (bool, delete recursively)

Alias: `.delete()`

### .compareAndDelete(key, oldvalue, [options], [callback])

Convenience method for test and delete (delete with {prevValue: oldvalue})

```javascript
registry.compareAndDelete("key", "oldvalue");
registry.compareAndDelete("key", "oldValue", options, console.log);
```

Alias: `.testAndDelete()`

### .mkdir(dir, [options], [callback])

Create a directory

```javascript
registry.mkdir("dir");
registry.mkdir("dir", console.log);
registry.mkdir("dir/", options, console.log);
```

### .rmdir(dir, [options], [callback])

Remove a directory

```javascript
registry.rmdir("dir");
registry.rmdir("dir", console.log);
registry.rmdir("dir/", { recursive: true }, console.log);
```

Available options include:

- `recursive` (bool, delete recursively)

### .create(path, value, [options], [callback])

Atomically create in-order keys.

```javascript
registry.create("queue", "first")
registry.create("queue", "next", console.log)
```

Alias: `.post()`

### .watch(key, [options], [callback])

This is a convenience method for get with `{wait: true}`.

```javascript
registry.watch("key");
registry.watch("key", console.log);
```

The watch command is pretty low level, it does not handle reconnects or
timeouts (Etcd will disconnect you after 5 minutes). Use the `.watcher()` below
if you do not wish to handle this yourself.

### .watchIndex(key, index, [options], callback)

This is a convenience method for get with `{wait: true, waitIndex: index}`.

```javascript
registry.watchIndex("key", 7, console.log);
```

See `.watch()` above.

### .watcher(key, [index], [options])

Returns an eventemitter for watching for changes on a key

```javascript
watcher = registry.watcher("key");
watcher.on("change", console.log); // Triggers on all changes
watcher.on("set", console.log);    // Triggers on specific changes (set ops)
watcher.on("delete", console.log); // Triggers on delete.
watcher2 = registry.watcher("key", null, {recursive: true});
watcher2.on("error", console.log);
```

You can cancel a watcher by calling `.stop()`.

Signals:
- `change` - emitted on value change
- `reconnect` - emitted on reconnect
- `error` - emitted on invalid content
- `<etcd action>` - the etcd action that triggered the watcher (ex: set, delete).
- `stop` - watcher was canceled.
- `resync` - watcher lost sync (server cleared and outdated the index).

It will handle reconnects and timeouts for you, it will also resync (best
effort) if it loses sync with Etcd (Etcd only keeps 1000 items in its event
history - for high frequency setups it's possible to fall behind).

Use the `.watch()` command in you need more direct control.

### .raw(method, key, value, options, callback)

Bypass the API and do raw queries.
Method must be one of: PUT, GET, POST, PATCH, DELETE

```javascript
registry.raw("GET", "v2/stats/leader", null, {}, callback)
registry.raw("PUT", "v2/keys/key", "value", {}, callback)
```

Remember to provide the full path, without any leading '/'

### .machines(callback)

Returns information about etcd nodes in the cluster

```javascript
registry.machines(console.log);
```

### .leader(callback)

Return the leader in the cluster

```javascript
registry.leader(console.log);
```

### .leaderStats(callback)

Return statistics about cluster leader

```javascript
registry.leaderStats(console.log);
```

### .selfStats(callback)

Return statistics about connected etcd node

```javascript
registry.selfStats(console.log);
```

## Synchronous operations

The library supports a set of basic synchronous/blocking operations that can be useful during
program startup (used like [fs.readFileSync](http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options)).

Synchronous functions perform the etcd request immediately (blocking) and return the following:

```javascript
{
  err // Error message or null if request completed successfully
  body // Body of the message or null if error
  headers // Headers from the response
}
```

### .setSync(key, value = null, [options])

Synchronously set key to value, or create key/directory.

```javascript
registry.setSync("key");
registry.setSync("key", "value");
registry.setSync("key", "value", { ttl: 60 });
registry.setSync("key", "value", { maxRetries: 3 });
```

Same options and function as .set().

### .getSync(key, [options])

Get a key or path.

```javascript
registry.getSync("key");
registry.getSync("key", { recursive: true });
```

### .delSync(key, [options])

Synchronously delete a key or path

```javascript
registry.delSync("key");
registry.delSync("key/", { recursive: true });
```

The available options are the same as .del() above.

### .mkdirSync(dir, [options])

Synchronously create a directory

```javascript
registry.mkdirSync("dir");
registry.mkdirSync("dir/", options);
```

### .rmdirSync(dir, [options])

Synchronously remove a directory

```javascript
registry.rmdirSync("dir");
registry.rmdirSync("dir/", { recursive: true });
```

The available options are the same as .rmdir() above.

---
_Source: https://npm.io/package/@eyecone/blastmedia-registry · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
