duck-worker
duckfficer service worker that serves an ipc bridge for communication
Installation
$ npm i duck-worker --save
# or
$ yarn add duck-worker
Features
- duck worker client success
- duck worker client fail
- duck worker provides ipc interface
- loads workers from folder
duck worker client success
const client = await new DuckWorkerClient()
t.is(await client.workerA('some payload'), 'received: some payload')
duck worker client fail
const client = await new DuckWorkerClient()
await t.throwsAsync(() => client.workerB('some error'), {
instanceOf: Error,
message: 'some error'
})
duck worker provides ipc interface
ipc.connectTo('worker')
return new Promise((resolve) => {
ipc.of['worker'].on('pong', () => {
t.pass()
resolve()
})
ipc.of['worker'].emit('ping')
})
loads workers from folder
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
Promise.<ipc.server>
duckWorkerIpc(workers, [appName], [id]) ⇒ 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 |
exports.<void>
duckWorker(workerDir, [appName], [id]) ⇒ Param | Type | Description |
---|---|---|
workerDir | String |
the worker dir |
[appName] | String |
ipc appname |
[id] | String |
ipc id |
License
© 2020-present Martin Rafael Gonzalez tin@devtin.io