# abstract-extension

> Small abstraction to help build out user defined extension messages in an RPC system

Latest version **3.1.1** (published 2020-06-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install abstract-extension
pnpm add abstract-extension
yarn add abstract-extension
bun add abstract-extension
```

## 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 | 3.1.1 |
| Published | 2020-06-15 |
| First published | 2019-10-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/abstract-extension
- Repository: https://github.com/mafintosh/abstract-extension
- Issues: https://github.com/mafintosh/abstract-extension/issues
- npm.io page: https://npm.io/package/abstract-extension

## Dependencies (1)

- [codecs](https://npm.io/package/codecs.md) ^2.0.0

## Recent versions

- 3.1.1 (latest) — 2020-06-15
- 3.1.0 — 2019-11-26
- 3.0.1 — 2019-10-13
- 3.0.0 — 2019-10-13

## README

# abstract-extension

Small abstraction to help build out user defined extension messages in an RPC system.

```
npm install abstract-extension
```

See [hypercore](https://github.com/mafintosh/hypercore) and [hypercore-protocol](https://github.com/mafintosh/hypercore-protocol)
for a full example on how to use this

## API

#### `const AbstractExtension = require('abstract-extension')`

Imports the AbstractExtension class. You should extend this and add the functionality you need.

#### `abstractExtension.destroy()`

Detroy an extension instance. Removes the message from the local pairing instance.

#### `abstractExtension.id`

The local id of the message. Send this over the wire instead of the message name after exchanging the initial message names.

#### `const bool = abstractExtension.remoteSupports()`

True if the remote also supports this message. Note that nothing bad will having from sending a message the remote does not support.

#### `const buffer = abstractExtension.encode(message)`

Encode a message to a buffer based on the message encoding.

#### `const local = AbstractExtension.createLocal(handlers)`

Create a local message pairing instance.

Whenever the messages are updated `local.onextensionupdate()` will be called if provided.

#### `const msg = local.add(name, handlers)`

Add a new message. `name` should be the string name of a message.

* `handlers.encoding` is an optional encoding for the message payload. Can be either `json`, `utf-8`, `binary` or any abstract encoding.
* `handlers.onmessage(message, context)` is called when a message has been received and pairing.
* `handlers.onerror(error, context)` is called when a message fails to decode.

#### `const list = local.names()`

Returns a sorted list of message names. You need to pass this to another remote pairing instance somehow.

#### `const remote = local.remote()`

Call this to setup remote pairing.

#### `remote.update(localNames)`

Pass the names of another instance to setup the pairing

#### `remote.onmessage(id, message, [context])`

Pair the remote id with the corresponding local message and call the onmessage handler.
Optionally pass a context object that is simply passed along to the `message.onmessage` function

## License

MIT

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