# binder-module

> Base class for a Binder module

Latest version **1.0.5** (published 2016-06-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install binder-module
pnpm add binder-module
yarn add binder-module
bun add binder-module
```

## 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.5 |
| Published | 2016-06-20 |
| First published | 2016-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andrew Osheroff |
| Maintainers | andrewosh |
| Keywords | binder, module |

## Links

- npm: https://www.npmjs.com/package/binder-module
- Repository: https://github.com/binder-project/binder-module
- Issues: https://github.com/binder-project/binder-module/issues
- npm.io page: https://npm.io/package/binder-module

## Dependencies (12)

- [hat](https://npm.io/package/hat.md) 0.0.3
- [pm2](https://npm.io/package/pm2.md) ^1.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.0.0
- [express](https://npm.io/package/express.md) ^4.13.3
- [inherits](https://npm.io/package/inherits.md) ^2.0.1
- [jsonfile](https://npm.io/package/jsonfile.md) ^2.2.3
- [binder-db](https://npm.io/package/binder-db.md) ^1.0.0
- [commander](https://npm.io/package/commander.md) ^2.9.0
- [body-parser](https://npm.io/package/body-parser.md) ^1.14.2
- [string-format](https://npm.io/package/string-format.md) ^0.5.0
- [binder-logging](https://npm.io/package/binder-logging.md) ^1.0.0
- [binder-protocol](https://npm.io/package/binder-protocol.md) ^1.0.0

## Recent versions

- 1.0.5 (latest) — 2016-06-20
- 1.0.4 — 2016-06-17
- 1.0.3 — 2016-05-11
- 1.0.2 — 2016-04-21
- 1.0.1 — 2016-03-30
- 1.0.0 — 2016-03-27

## README

# binder-module
Base class for a PM2-managed, Express-based Binder module

An instance of `BinderModule` implements a subset of the API defined in
[`binder-protocol`](https:/github.com/binder-project/binder-protocol), with HTTP handlers defined
for each implemented endpoint.

A `BinderModule` is initialized with a list of components it will implement (currently chosen from
`build`, `registry`, and `deploy`). Once the desired API subset has been declared, the
`_makeBinderAPI` method is responsible for binding instance methods (HTTP handlers) to their appropriate API
endpoints.

`_makeOtherRoutes` lets you define other HTTP handlers external to the Binder API that will also be
registered on the Express app at launch time.

### install

`binder-module` is designed to make the lifecycles of independent Binder servers simpler to manage,
but it has limited use outside of that context.

If you're modifying or extending the Binder API, and you'd like to make another module, from within
that module's directory:
```
npm install binder-module --save
```
Then extend your existing module with
```
inherits(YourBinderModule, BinderModule)
```

### usage

The correct behavior for every Binder API endpoint is described in [the Binder protocol
file](https://github.com/binder-project/binder-protocol/blob/master/index.js). Handlers are
registered in `_makeBinderAPI` as a list of mappings from handler name to instance method:
```
BinderBuild.prototype._makeBinderAPI = function () {
  return {
    statusAll: this._getAllBuilds.bind(this),
    status: this._getBuild.bind(this),
    start: this._startBuild.bind(this),
    fetch: this._fetchTemplate.bind(this),
    fetchAll: this._fetchAllTemplates.bind(this)
  }
}
```

Each handler is passed a reference to an API object, when provides success/error handlers matching
the success/error conditions defined in the protocol file:

```
BinderBuild.prototype._getBuild = function (api) {
  var self = this
  if (!this.buildInfo) {
    return api._noBuildInfo()
  }
  console.log('api.params: {0}'.format(JSON.stringify(api.params)))
  this.buildInfo.findOne({ name: api.params['image-name'] }, function (err, info) {
    if (err) {
      return api._badQuery({ error: err })
    }
    if (!info || (info === {})) {
      return api._noBuildInfo()
    }
    return api._success({
      'name': info.name,
      'start-time': info.startTime,
      'status': info.status,
      'phase': info.phase,
      'repository': info.repo,
      'error': info.error
    })
  })
}
```

In this example from `binder-build`, the input/output properties of the `noBuildInfo` and `badQuery`
error conditions are defined in the protocol file, as is the return value for the `success`
condition. Additionally, the value of `api.params` is type-checked prior to the `_getBuild` method
being invoked, and error handling for bad parameters are all handled in a single place.

### examples

Currently, the
[`binder-build`](https://github.com/binder-project/binder-build/blob/master/lib/server.js) and
[`binder-deploy-kubernetes`](https://github.com/binder-project/binder-deploy-kubernetes/blob/master/lib/server.js)
modules implement instances of `BinderModule` in their `lib/server.js`  files.

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