An open API service indexing awesome lists of open source software.

https://github.com/devtin/duck-worker

duckfficer service worker that serves an ipc bridge for communication
https://github.com/devtin/duck-worker

Last synced: 7 months ago
JSON representation

duckfficer service worker that serves an ipc bridge for communication

Awesome Lists containing this project

README

          

duck-worker


Version


duckfficer service worker that serves an ipc bridge for communication

## Installation

```sh
$ npm i duck-worker --save
# or
$ yarn add duck-worker
```

## Features

- [duck worker client success](#duck-worker-client-success)
- [duck worker client fail](#duck-worker-client-fail)
- [duck worker provides ipc interface](#duck-worker-provides-ipc-interface)
- [loads workers from folder](#loads-workers-from-folder)

## duck worker client success

```js
const client = await new DuckWorkerClient()
t.is(await client.workerA('some payload'), 'received: some payload')
```

## duck worker client fail

```js
const client = await new DuckWorkerClient()
await t.throwsAsync(() => client.workerB('some error'), {
instanceOf: Error,
message: 'some error'
})
```

## duck worker provides ipc interface

```js
ipc.connectTo('worker')
return new Promise((resolve) => {
ipc.of['worker'].on('pong', () => {
t.pass()
resolve()
})
ipc.of['worker'].emit('ping')
})
```

## loads workers from folder

```js
await t.throwsAsync(() => client.taskA('something'), {
message: 'got: something'
})

t.like(await client.taskB({ name: 'Olivia' }), {
output: 'name is: Olivia'
})

const r = await client.nameSpace.taskC('what is love?')

t.like(r.errorsThrown[0], {
payload: 'what is love?'
})
```


### DuckWorkerClient


#### new DuckWorkerClient(appSpace, [clientId], [workerId])

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| appSpace | String | | default to local package.json->name |
| [clientId] | String | client | |
| [workerId] | String | worker | |

**Returns**: Promise.<{Object}> - the proxy to communicate with the worker


### duckWorkerIpc(workers, [appName], [id]) ⇒ Promise.<ipc.server>

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| workers | Object | | workers object mapping to functions |
| [appName] | String | <package.json->name> | the appName (defaults to project's package.json name) |
| [id] | String | worker | worker id |


### duckWorker(workerDir, [appName], [id]) ⇒ exports.<void>

| Param | Type | Description |
| --- | --- | --- |
| workerDir | String | the worker dir |
| [appName] | String | ipc appname |
| [id] | String | ipc id |

* * *

### License

[MIT](https://opensource.org/licenses/MIT)

© 2020-present Martin Rafael Gonzalez