# duck-worker

> duckfficer service worker that serves an ipc bridge for communication

Latest version **1.0.2** (published 2021-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install duck-worker
pnpm add duck-worker
yarn add duck-worker
bun add duck-worker
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-02-03 |
| First published | 2021-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 33.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Martin Rafael Gonzalez |
| Maintainers | tin_r |
| Keywords | duckfficer, service, worker |

## Links

- npm: https://www.npmjs.com/package/duck-worker
- Repository: https://github.com/devtin/duck-worker
- Homepage: https://github.com/devtin/duck-worker#readme
- Issues: https://github.com/devtin/duck-worker/issues
- npm.io page: https://npm.io/package/duck-worker

## Dependencies (7)

- [pkg-up](https://npm.io/package/pkg-up.md) ^3.1.0
- [bluebird](https://npm.io/package/bluebird.md) ^3.7.2
- [node-ipc](https://npm.io/package/node-ipc.md) ^9.1.3
- [proxy-deep](https://npm.io/package/proxy-deep.md) ^3.1.0
- [duck-storage](https://npm.io/package/duck-storage.md) 0.0.24
- [js-dir-into-json](https://npm.io/package/js-dir-into-json.md) ^2.7.0
- [duck-storage-mongodb](https://npm.io/package/duck-storage-mongodb.md) 0.0.6

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-02-03
- 1.0.1 — 2021-02-03
- 1.0.0 — 2021-02-02

## README

<div><h1>duck-worker</h1></div>

<p>
    <a href="https://www.npmjs.com/package/duck-worker" target="_blank"><img src="https://img.shields.io/npm/v/duck-worker.svg" alt="Version"></a>
<a href="http://opensource.org/licenses" target="_blank"><img src="http://img.shields.io/badge/License-MIT-brightgreen.svg"></a>
</p>

<p>
    duckfficer service worker that serves an ipc bridge for communication
</p>

## 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)


<a name="duck-worker-client-success"></a>

## duck worker client success


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

<a name="duck-worker-client-fail"></a>

## duck worker client fail


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

<a name="duck-worker-provides-ipc-interface"></a>

## 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')
})
```

<a name="loads-workers-from-folder"></a>

## 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?'
})
```


<br><a name="DuckWorkerClient"></a>

### DuckWorkerClient

<br><a name="new_DuckWorkerClient_new"></a>

#### new DuckWorkerClient(appSpace, [clientId], [workerId])

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| appSpace | <code>String</code> |  | default to local package.json->name |
| [clientId] | <code>String</code> | <code>client</code> |  |
| [workerId] | <code>String</code> | <code>worker</code> |  |

**Returns**: <code>Promise.&lt;{Object}&gt;</code> - the proxy to communicate with the worker  

<br><a name="duckWorkerIpc"></a>

### duckWorkerIpc(workers, [appName], [id]) ⇒ <code>Promise.&lt;ipc.server&gt;</code>

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| workers | <code>Object</code> |  | workers object mapping to functions |
| [appName] | <code>String</code> | <code>&lt;package.json-&gt;name&gt;</code> | the appName (defaults to project's package.json name) |
| [id] | <code>String</code> | <code>worker</code> | worker id |


<br><a name="duckWorker"></a>

### duckWorker(workerDir, [appName], [id]) ⇒ <code>exports.&lt;void&gt;</code>

| Param | Type | Description |
| --- | --- | --- |
| workerDir | <code>String</code> | the worker dir |
| [appName] | <code>String</code> | ipc appname |
| [id] | <code>String</code> | ipc id |


* * *

### License

[MIT](https://opensource.org/licenses/MIT)

&copy; 2020-present Martin Rafael Gonzalez <tin@devtin.io>

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