@stinkycomputing/cachearoo 1.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ name: Build and test
2
+
3
+ on:
4
+ push:
5
+
6
+ jobs:
7
+ build:
8
+ runs-on: ubuntu-latest
9
+
10
+ strategy:
11
+ matrix:
12
+ node-version: [16.x]
13
+
14
+ steps:
15
+ - uses: actions/checkout@v3
16
+
17
+ - name: Use Node.js ${{ matrix.node-version }}
18
+ uses: actions/setup-node@v3
19
+ with:
20
+ node-version: ${{ matrix.node-version }}
21
+
22
+ - name: Build
23
+ run: |
24
+ npm install
25
+ npm run build
26
+
27
+ - name: Test
28
+ run: |
29
+ npm test
@@ -0,0 +1,30 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ push:
8
+ tags:
9
+ - "v*"
10
+ jobs:
11
+ publish-npm:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+ - uses: actions/setup-node@v3
16
+ with:
17
+ node-version: 16
18
+ registry-url: https://registry.npmjs.org/
19
+ - name: Build, test and publish
20
+ run: |
21
+ npm ci
22
+ npm run build
23
+ npm publish
24
+ env:
25
+ NODE_AUTH_TOKEN: ${{secrets.STINKYDEV_NPM_TOKEN}}
26
+ - name: Release
27
+ uses: "marvinpinto/action-automatic-releases@latest"
28
+ with:
29
+ repo_token: "${{ secrets.GITHUB_TOKEN }}"
30
+ prerelease: false
package/README.md ADDED
@@ -0,0 +1,400 @@
1
+ ![main branch](https://github.com/regodev/cachearoo-js/actions/workflows/build_and_test.yml/badge.svg)
2
+
3
+ Tags starting with "v" are automatically published to npm
4
+
5
+ ### Install
6
+ ```js
7
+ npm install cachearoo
8
+ ```
9
+
10
+ # Cachearoo
11
+
12
+ ```js
13
+ const Cachearoo = require('cachearoo').Cachearoo;
14
+ ```
15
+
16
+ Communicates with Cachearoo server, maintains a persistent connection, handles data store operations and event signaling. If the persistent connection (websocket) is not connected, REST will be used. Events can be registered without having the connection active, the client will automatically register them with the server when connection is established. Works with node.js and in browser.
17
+
18
+ ## Constructor
19
+
20
+ ```js
21
+ new Cachearoo(options);
22
+ ```
23
+
24
+ ### Cachearoo Options
25
+
26
+ | Property | Type | Default | Description |
27
+ |--------------|---------|-----------|----------------------------------------------------|
28
+ | host | string | 127.0.0.1 | Cachearoo Server host or ip |
29
+ | port | number | 4300 | Cachearoo Server port |
30
+ | path | string | | Path to api if behind proxy |
31
+ | apiKey | string | | Secret key to access API (if set in server) |
32
+ | secure | boolean | false | Use TLS |
33
+ | enablePing | boolean | false | |
34
+ | pingInterval | number | 5000 | Time in milliseconds |
35
+ | clientId | string | | Name of client, visible in admin UI status section |
36
+ | bucket | string | | Default bucket for operations |
37
+
38
+ ## Methods
39
+
40
+
41
+ ### read(key, [opts])
42
+ Returns promise with read object. If empty string is passed as key, an array of all objects in bucket will be returned.
43
+
44
+ ***options (optional)***
45
+
46
+ | Property | Type | Default | Description |
47
+ |-----------|---------|---------|----------------------------------------|
48
+ | bucket | string | null | Optionally override default bucket |
49
+ | forceHttp | boolean | false | Force request to be sent over REST api |
50
+ | async | boolean | true | |
51
+
52
+ ***Example***
53
+
54
+ ```js
55
+ const cro = new Cachearoo({ bucket: 'myBucket' });
56
+ cro.read('myKey')
57
+ .then(value => console.log(value));
58
+ ```
59
+
60
+ ### list([opts])
61
+ List objects in bucket. Returns promise with array.
62
+
63
+ ***options (optional)***
64
+
65
+ | Property | Type | Default | Description |
66
+ |-----------|---------|---------|----------------------------------------|
67
+ | bucket | string | null | Optionally override default bucket |
68
+ | keysOnly | boolean | true | Set to false to retrieve object content |
69
+ | forceHttp | boolean | false | Force request to be sent over REST api |
70
+ | async | boolean | true | |
71
+
72
+ ***Example***
73
+
74
+ ```js
75
+ const cro = new Cachearoo({ bucket: 'myBucket' });
76
+ cro.list()
77
+ .then(objects => console.log(objects));
78
+ ```
79
+
80
+ ### write(key, data, [opts])
81
+ Returns promise
82
+
83
+ ***options (optional)***
84
+
85
+ | Property | Type | Default | Description |
86
+ |--------------|---------|---------|-----------------------------------------------------------------------------------------------|
87
+ | bucket | string | null | Optionally override default bucket |
88
+ | forceHttp | boolean | false | Force request to be sent over REST api |
89
+ | async | boolean | true | |
90
+ | failIfExists | boolean | false | Operation will fail if key already exists in data store |
91
+ | expire | string | | Set expiration for object, possible values: 'session' - Delete object when client disconnects |
92
+
93
+ ***Example***
94
+
95
+ ```js
96
+ const cro = new Cachearoo({ bucket: 'myBucket' });
97
+ cro.write('myKey', { text: 'hello!' })
98
+ .then(() => console.log('Written!');
99
+ ```
100
+
101
+ ### patch(key, patch, [opts])
102
+ Returns promise. [JSON patch](http://jsonpatch.com/) existing object.
103
+
104
+ ***options (optional)***
105
+
106
+ | Property | Type | Default | Description |
107
+ |--------------|---------|---------|-----------------------------------------------------------------------------------------------|
108
+ | bucket | string | null | Optionally override default bucket |
109
+ | removeDataFromReply | boolean | false | Optionally remove patch result from reply |
110
+ | forceHttp | boolean | false | Force request to be sent over REST api |
111
+ | async | boolean | true | |
112
+
113
+
114
+ ***Example***
115
+
116
+ ```js
117
+ const cro = new Cachearoo({ bucket: 'myBucket' });
118
+ cro.patch('myKey', [{ op: 'replace', path: '/text', value: 'hey!' }])
119
+ .then(() => console.log('Patched!');
120
+ ```
121
+
122
+ ### remove(key, [opts])
123
+ Returns promise. Removes an object from data store.
124
+
125
+ ***options (optional)***
126
+
127
+ | Property | Type | Default | Description |
128
+ |--------------|---------|---------|-----------------------------------------------------------------------------------------------|
129
+ | bucket | string | null | Optionally override default bucket |
130
+ | forceHttp | boolean | false | Force request to be sent over REST api |
131
+ | async | boolean | true | |
132
+
133
+
134
+ ***Example***
135
+
136
+ ```js
137
+ const cro = new Cachearoo({ bucket: 'myBucket' });
138
+ cro.remove('myKey')
139
+ .then(() => console.log('Removed!');
140
+ ```
141
+ ### Properties
142
+
143
+ Connection: PersistentConnection
144
+
145
+ # PersistentConnection
146
+ PersistentConnection is automatically created in the Cachearoo instance as property `connection`
147
+
148
+ ## Methods
149
+
150
+ ### addListener(bucket, key, sendValues, callbackFn(eventObject))
151
+ Add listener for specific bucket and key. Wildcard * can be used to listen to all events in a bucket. If * is used for bucket, events will be triggered for all buckets.
152
+
153
+ **eventObject**
154
+
155
+ | Property | Type | Description |
156
+ |----------|--------|----------------------------------------------------------|
157
+ | bucket | string | bucket of event origin |
158
+ | key | string | key of event origin |
159
+ | value | object | written object if sendEvent was true in addListener call |
160
+
161
+
162
+ ***Example***
163
+
164
+ ```js
165
+ const cro = new Cachearoo({ bucket: 'myBucket' });
166
+ cro.connection.addListener('myBucket', 'myKey', true, (event) => {
167
+ console.log(`Event received for
168
+ key ${event.key} in
169
+ bucket ${event.bucket},
170
+ value=${event.value}`);
171
+ });
172
+ ```
173
+
174
+
175
+ ### signalEvent(bucket, key, value)
176
+ Send data to Cachearoo only to be propagated as an event to other clients.
177
+
178
+ ***Example***
179
+
180
+ ```js
181
+ const cro = new Cachearoo({ bucket: 'myBucket' });
182
+ cro.connection.signalEvent(
183
+ 'myEventBucket',
184
+ 'myEventKey',
185
+ { message: 'hello yall!' }
186
+ });
187
+ ```
188
+
189
+ ## Events (callbacks)
190
+ ### onConnect(persistentConnection)
191
+ Callback triggered when connection is established.
192
+
193
+ ***Example***
194
+
195
+ ```js
196
+ const cro = new Cachearoo({ bucket: 'myBucket' });
197
+ cro.connection.onConnect = (serverId) => {
198
+ console.log(`Client connected to server with id ${serverId}`);
199
+ };
200
+ ```
201
+
202
+ ### onDisconnect()
203
+ Callback triggered when connection is closed.
204
+
205
+ ***Example***
206
+
207
+ ```js
208
+ const cro = new Cachearoo({ bucket: 'myBucket' });
209
+ cro.connection.onDisconnect = () => {
210
+ console.log('Client disconnected');
211
+ };
212
+ ```
213
+
214
+ ### onPong()
215
+ Callback triggered when pong packet is received from server.
216
+
217
+ ***Example***
218
+
219
+ ```js
220
+ const cro = new Cachearoo({ bucket: 'myBucket' });
221
+ cro.connection.onPong = () => {
222
+ console.log('Server is responding!');
223
+ };
224
+ ```
225
+
226
+ # Messaging.RequestReply
227
+ ```js
228
+ const RequestReply = require('cachearoo').Messaging.RequestReply;
229
+ ```
230
+ Thin abstraction for sending and receiving messages between two clients connected to same Cachearoo. Cachearoo messaging is using the signalEvent method of PersistentConnection.
231
+
232
+ # Requestor
233
+ Requestor is used to send messages to a receiver. Request will fail if no response is received within timeoutInMs or if no progress update is received within progressTimeoutInMs.
234
+
235
+ ## Constructor
236
+
237
+ ```js
238
+ new RequestReply.Requestor(CachearooInstance, channel, timeoutInMs, progressTimeoutInMs);
239
+ ```
240
+
241
+ ## Methods
242
+
243
+ ### request(msg)
244
+ Returns promise with reply.
245
+
246
+ ***Example***
247
+
248
+ ```js
249
+ const cro = new Cachearoo();
250
+
251
+ // We cannot send request before we are connected
252
+ cro.connection.onConnect = (serverId) => {
253
+ // fail if response > 30 sec or if progress is not received < every 3 sec
254
+ const requestor = new Requestor(bap, 'my-channel', 30000, 3000);
255
+ requestor.request({ msg: 'hello?' })
256
+ .then(reply => console.log(`Got reply! ${reply}`);
257
+ .catch(err => console.error(err));
258
+ };
259
+ ```
260
+
261
+ # Replier
262
+ Replier is used to receive and reply to messages.
263
+
264
+ ## Constructor
265
+
266
+ ```js
267
+ new RequestReply.Replier(CachearooInstance, channel);
268
+ ```
269
+
270
+ ## Events
271
+
272
+ ### onMessage(msg, replyFn(err, obj), progressFn(obj))
273
+ Called when a message is received. Reply is sent by calling replyFn with either an error or some data. In case of lengthy processing before reply can be sent, replier can call progressFn to avoid timeout in requestor.
274
+
275
+ ***Example***
276
+
277
+ ```js
278
+ const cro = new Cachearoo();
279
+
280
+ const replier = new Replier(bap, 'my-channel');
281
+ replier.onMessage = (msg, reply, progress) => {
282
+ console.log(`Got request: ${msg}`);
283
+ // send progress after 2 sec, and reply after 5 sec
284
+ setTimeout(() => progress({ someprop: 'some value' }), 2000);
285
+ setTimeout(() => reply(null, { someProperty: 'some value' }), 5000);
286
+ };
287
+ ```
288
+
289
+ # Messaging.CompetingConsumers
290
+ ```js
291
+ const CompetingConsumers = require('cachearoo').Messaging.CompetingConsumers;
292
+ ```
293
+ This pattern is used when one producer generates jobs that many listening consumers can claim. Only one of the consumers can claim each job.
294
+
295
+ #Producer
296
+ Producer creates jobs.
297
+
298
+ ## Constructor
299
+
300
+ ```js
301
+ new CompetingConsumers.Producer(CachearooInstance, channel, timeoutInMs, progressTimeoutInMs);
302
+ ```
303
+
304
+ ## Methods
305
+
306
+ ### addJob(job, progressFn?, timoutInMs?, progressTimeoutInMs?)
307
+ Returns promise, resolved if consumer processed it successfully. Consumer can optionally call a progress callback (passed in progressFn) during its job processing.
308
+
309
+ ***Example***
310
+
311
+ ```js
312
+ const cro = new Cachearoo();
313
+
314
+ const producer = new Producer(bap, 'render-jobs', 10000);
315
+ cro.connection.onConnect = (serverId) => {
316
+ producer.addJob({ someProperty: 'value' }, (progress) => console.log(progress))
317
+ .then((data) => {
318
+ console.log(`Job done: ${data.result}`);
319
+ })
320
+ .catch(err => console.error(err));
321
+ }
322
+ ```
323
+
324
+ # CompetingConsumer
325
+ Consumer of jobs.
326
+
327
+ ## Constructor
328
+
329
+ ```js
330
+ new CompetingConsumers.CompetingConsumer(CachearooInstance, channel);
331
+ ```
332
+
333
+ ## Events
334
+
335
+ ### onJobQuery(job, callback(errno, worker))
336
+ Called when a job is requested by producer. errno should have one of values:
337
+
338
+ ```js
339
+ const JOB_NOT_SUPPORTED = 100;
340
+ const NO_WORKER_AVAILABLE = 200;
341
+ const JOB_OK = null;
342
+ ```
343
+
344
+ Worker object must implement following:
345
+
346
+ ```js
347
+ class Worker {
348
+ constructor(id) {
349
+ this.id = id;
350
+ this.available = true;
351
+ }
352
+
353
+ work(job, done, progress) {
354
+ // do things
355
+ // signal progress with progress();
356
+ done(err, result);
357
+ }
358
+
359
+ release() {
360
+ this.available = true;
361
+ }
362
+ }
363
+ ```
364
+
365
+
366
+ ***Example***
367
+
368
+ ```js
369
+ class MyWorker {
370
+ constructor(id) {
371
+ this.id = id;
372
+ this.available = true;
373
+ }
374
+
375
+ work(job, done, progress) {
376
+ console.log('working..');
377
+ // send progress every sec
378
+ setTimeout(() => progress({ someProperty: 'value' }), 1000);
379
+ // signal done after 10 sec
380
+ setTimeout(() => done(null, { someProperty: 'value' }), 10000);
381
+ }
382
+
383
+ release() {
384
+ this.available = true;
385
+ }
386
+ }
387
+
388
+ const de = new Cachearoo();
389
+
390
+ const consumer = new CompetingConsumer(de, 'my-channel');
391
+ const worker = new MyWorker('myworker1');
392
+
393
+ consumer.onJobQuery = (job, callback) => {
394
+ if (worker.available) {
395
+ callback(null, worker);
396
+ } else {
397
+ callback(200); // no worker available
398
+ }
399
+ };
400
+ ```
@@ -0,0 +1,19 @@
1
+ import { Cachearoo, CachearooSettings, RequestOptions } from './libs/cro';
2
+ import { CompetingConsumer, Producer } from './libs/cro.msg.competing-consumers';
3
+ import { Requestor, Replier } from './libs/cro.msg.request-reply';
4
+ export declare namespace Messaging {
5
+ const RequestReply: {
6
+ Requestor: typeof Requestor;
7
+ Replier: typeof Replier;
8
+ };
9
+ const CompetingConsumers: {
10
+ CompetingConsumer: typeof CompetingConsumer;
11
+ Producer: typeof Producer;
12
+ };
13
+ const Errors: {
14
+ JOB_NOT_SUPPORTED: number;
15
+ NO_WORKER_AVAILABLE: number;
16
+ };
17
+ }
18
+ export { Requestor, Replier } from './libs/cro.msg.request-reply';
19
+ export { Cachearoo, CachearooSettings, RequestOptions };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestOptions = exports.CachearooSettings = exports.Cachearoo = exports.Replier = exports.Requestor = exports.Messaging = void 0;
4
+ const fetch = require("node-fetch-commonjs");
5
+ global.fetch = fetch;
6
+ const cro_1 = require("./libs/cro");
7
+ Object.defineProperty(exports, "Cachearoo", { enumerable: true, get: function () { return cro_1.Cachearoo; } });
8
+ Object.defineProperty(exports, "CachearooSettings", { enumerable: true, get: function () { return cro_1.CachearooSettings; } });
9
+ Object.defineProperty(exports, "RequestOptions", { enumerable: true, get: function () { return cro_1.RequestOptions; } });
10
+ const cro_msg_competing_consumers_1 = require("./libs/cro.msg.competing-consumers");
11
+ const cro_msg_request_reply_1 = require("./libs/cro.msg.request-reply");
12
+ const cro_msg_competing_consumers_2 = require("./libs/cro.msg.competing-consumers");
13
+ var Messaging;
14
+ (function (Messaging) {
15
+ Messaging.RequestReply = {
16
+ Requestor: cro_msg_request_reply_1.Requestor,
17
+ Replier: cro_msg_request_reply_1.Replier,
18
+ };
19
+ Messaging.CompetingConsumers = {
20
+ CompetingConsumer: cro_msg_competing_consumers_1.CompetingConsumer,
21
+ Producer: cro_msg_competing_consumers_1.Producer,
22
+ };
23
+ Messaging.Errors = {
24
+ JOB_NOT_SUPPORTED: cro_msg_competing_consumers_2.JOB_NOT_SUPPORTED,
25
+ NO_WORKER_AVAILABLE: cro_msg_competing_consumers_2.NO_WORKER_AVAILABLE,
26
+ };
27
+ })(Messaging = exports.Messaging || (exports.Messaging = {}));
28
+ ;
29
+ var cro_msg_request_reply_2 = require("./libs/cro.msg.request-reply");
30
+ Object.defineProperty(exports, "Requestor", { enumerable: true, get: function () { return cro_msg_request_reply_2.Requestor; } });
31
+ Object.defineProperty(exports, "Replier", { enumerable: true, get: function () { return cro_msg_request_reply_2.Replier; } });
@@ -0,0 +1,19 @@
1
+ import { Cachearoo, CachearooSettings, RequestOptions } from './libs/cro';
2
+ import { CompetingConsumer, Producer } from './libs/cro.msg.competing-consumers';
3
+ import { Requestor, Replier } from './libs/cro.msg.request-reply';
4
+ export declare namespace Messaging {
5
+ const RequestReply: {
6
+ Requestor: typeof Requestor;
7
+ Replier: typeof Replier;
8
+ };
9
+ const CompetingConsumers: {
10
+ CompetingConsumer: typeof CompetingConsumer;
11
+ Producer: typeof Producer;
12
+ };
13
+ const Errors: {
14
+ JOB_NOT_SUPPORTED: number;
15
+ NO_WORKER_AVAILABLE: number;
16
+ };
17
+ }
18
+ export { Requestor, Replier } from './libs/cro.msg.request-reply';
19
+ export { Cachearoo, CachearooSettings, RequestOptions };