# dirty-rpc

> expose functions as remote calls

Latest version **0.2.0** (published 2016-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install dirty-rpc
pnpm add dirty-rpc
yarn add dirty-rpc
bun add dirty-rpc
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2016-03-23 |
| First published | 2016-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andrei Cocorean |
| Maintainers | andreic |
| Keywords | rpc, promises, express, middleware |

## Links

- npm: https://www.npmjs.com/package/dirty-rpc
- Repository: https://github.com/andrei-cocorean/dirty-rpc
- Homepage: https://github.com/andrei-cocorean/dirty-rpc#readme
- Issues: https://github.com/andrei-cocorean/dirty-rpc/issues
- npm.io page: https://npm.io/package/dirty-rpc

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2016-03-23
- 0.1.0 — 2016-03-11

## README

dirty-rpc -- expose functions as http calls
===========================================

## Install

    npm install dirty-rpc

## Start the RPC server

```javascript
import Promise from 'bluebird'
import http from 'http'
import fs from 'fs'
import {httpListener} from 'dirty-rpc'

const listener = httpListener({

  // Any function can be a request handler as long as it accepts and returns JSON data.
  // The only exception is if the function is async, in which case it should return a promise
  // that resolves with JSON data. The RPC server will respond with the result of the promise.
  readFile (name) {
    return Promise.fromCallback(callback => fs.readFile(name, 'utf-8', callback))
  }
})

http.createServer(listener).listen(3000, 'localhost', () => {
  console.log('RPC server listening...')
})
```

or using Express:

```javascript
import express from 'express'

/* ... */

const app = express()
app.post('/', listener)
app.listen(3000, 'localhost', () => {
  console.log('RPC server listening...')
})
```

## Call a remote function

The RPC client module takes care only of serializing and deserializing rpc request and response data.
It doesn't make network requests. This keeps it independent of the transport layer and allows clients to
use the library of their choice for transmitting the data. The example below uses [request](https://www.npmjs.com/package/request).

```javascript
import request from 'request'
import Promise from 'bluebird'
import {client} from 'dirty-rpc'

function makeRequest (payloadString) {
  const reqOpts = {
    method: 'POST',
    url: 'http://localhost:3000',
    body: payloadString,
  }

  // return a promise that resolves with the rpc response data
  return Promise.fromCallback(
    callback => request(reqOpts, callback),
    {multiArgs: true}
  )
    .spread((response, body) => body)
}

// Create a proxy for the readFile function on the RPC server
const readFile = client(makeRequest, 'readFile')

// RPC proxies always return a promise
readFile('some_file.txt')
  .then(content => {
    console.log('content: ', content)
  })
  .catch(err => {
    console.log('remote err: ', err)
  })
```

# Error handling

All RPC errors result in a JSON reply of:

```json
{
  "error": {
    "message": "error message",
    "stack": "error stack trace"
  }
}
```

This is deserialized into an Error object and the client proxy rejects the promise with it. Specific error types are not preserved.

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