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
- Host: GitHub
- URL: https://github.com/devtin/duck-worker
- Owner: devtin
- Created: 2021-02-02T23:19:29.000Z (about 5 years ago)
- Default Branch: dev
- Last Pushed: 2021-02-03T01:49:17.000Z (about 5 years ago)
- Last Synced: 2025-09-06T10:36:03.069Z (7 months ago)
- Language: JavaScript
- Size: 111 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
duck-worker
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