cry-ebus2 2.2.5 → 3.0.0

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.
Files changed (103) hide show
  1. package/dist/EbusConfiguration.d.ts +1 -0
  2. package/dist/EbusConfiguration.d.ts.map +1 -1
  3. package/dist/broker.d.ts +1 -0
  4. package/dist/broker.d.ts.map +1 -1
  5. package/dist/broker.js +38 -6
  6. package/dist/broker.js.map +1 -1
  7. package/dist/clientBlocking.js +5 -5
  8. package/dist/clientBlocking.js.map +1 -1
  9. package/dist/clientNonBlocking.d.ts +1 -0
  10. package/dist/clientNonBlocking.d.ts.map +1 -1
  11. package/dist/clientNonBlocking.js +12 -5
  12. package/dist/clientNonBlocking.js.map +1 -1
  13. package/dist/consts.d.ts.map +1 -1
  14. package/dist/consts.js +2 -1
  15. package/dist/consts.js.map +1 -1
  16. package/dist/delayWorker.js +2 -2
  17. package/dist/delayWorker.js.map +1 -1
  18. package/dist/echoWorker.js +1 -1
  19. package/dist/echoWorker.js.map +1 -1
  20. package/dist/errorWorker.js +1 -1
  21. package/dist/errorWorker.js.map +1 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/loadConfiguration.js +1 -1
  25. package/dist/loadConfiguration.js.map +1 -1
  26. package/dist/pub.js +2 -2
  27. package/dist/pub.js.map +1 -1
  28. package/dist/rejectWorker.js +1 -1
  29. package/dist/rejectWorker.js.map +1 -1
  30. package/dist/run/all.js +1 -1
  31. package/dist/run/all.js.map +1 -1
  32. package/dist/run/broker.js +1 -1
  33. package/dist/run/broker.js.map +1 -1
  34. package/dist/run/call.js +3 -3
  35. package/dist/run/call.js.map +1 -1
  36. package/dist/run/calling.js +2 -2
  37. package/dist/run/calling.js.map +1 -1
  38. package/dist/run/callingBurst.js +2 -2
  39. package/dist/run/callingBurst.js.map +1 -1
  40. package/dist/run/delay.js +1 -1
  41. package/dist/run/delay.js.map +1 -1
  42. package/dist/run/echo.js +1 -1
  43. package/dist/run/echo.js.map +1 -1
  44. package/dist/run/error.js +1 -1
  45. package/dist/run/error.js.map +1 -1
  46. package/dist/run/ping.js +3 -3
  47. package/dist/run/ping.js.map +1 -1
  48. package/dist/run/pub.js +2 -2
  49. package/dist/run/pub.js.map +1 -1
  50. package/dist/run/reject.js +1 -1
  51. package/dist/run/reject.js.map +1 -1
  52. package/dist/run/sub.js +1 -1
  53. package/dist/run/sub.js.map +1 -1
  54. package/dist/run/workers.js +38 -9
  55. package/dist/run/workers.js.map +1 -1
  56. package/dist/serialize.d.ts.map +1 -1
  57. package/dist/serialize.js +12 -14
  58. package/dist/serialize.js.map +1 -1
  59. package/dist/sub.js +2 -2
  60. package/dist/sub.js.map +1 -1
  61. package/dist/test/config.js +3 -3
  62. package/dist/test/config.js.map +1 -1
  63. package/dist/test/echo.js +15 -21
  64. package/dist/test/echo.js.map +1 -1
  65. package/dist/test/pack.js +16 -12
  66. package/dist/test/pack.js.map +1 -1
  67. package/dist/test/performance.js +9 -9
  68. package/dist/test/performance.js.map +1 -1
  69. package/dist/test/pub.js.map +1 -1
  70. package/dist/test/reject.js +7 -7
  71. package/dist/test/reject.js.map +1 -1
  72. package/dist/test/services.js +9 -9
  73. package/dist/test/services.js.map +1 -1
  74. package/dist/test/stress.js +1 -1
  75. package/dist/test/stress.js.map +1 -1
  76. package/dist/test/timing.js +14 -14
  77. package/dist/test/timing.js.map +1 -1
  78. package/dist/types.d.ts +3 -1
  79. package/dist/types.d.ts.map +1 -1
  80. package/dist/types.js +2 -0
  81. package/dist/types.js.map +1 -1
  82. package/dist/worker.d.ts +1 -1
  83. package/dist/worker.d.ts.map +1 -1
  84. package/dist/worker.js +29 -23
  85. package/dist/worker.js.map +1 -1
  86. package/package.json +13 -12
  87. package/src/EbusConfiguration.ts +1 -0
  88. package/src/broker.ts +37 -2
  89. package/src/clientNonBlocking.ts +8 -0
  90. package/src/consts.ts +2 -1
  91. package/src/index.ts +0 -3
  92. package/src/run/all.ts +0 -2
  93. package/src/run/call.ts +1 -1
  94. package/src/run/ping.ts +1 -1
  95. package/src/run/workers.ts +47 -11
  96. package/src/serialize.ts +9 -10
  97. package/src/src/notepack.d.ts +2 -0
  98. package/src/test/echo.ts +15 -18
  99. package/src/test/pack.ts +9 -8
  100. package/src/test/performance.ts +13 -12
  101. package/src/test/pub.ts +1 -3
  102. package/src/types.ts +2 -0
  103. package/src/worker.ts +38 -24
@@ -1,14 +1,50 @@
1
- import { DelayWorker } from "../delayWorker";
2
- import { EchoWorker } from "../echoWorker";
3
- import { ErrorWorker } from "../errorWorker";
4
- import { RejectWorker } from "../rejectWorker";
5
1
 
6
- import { config } from "dotenv"
7
- import { Sub } from "../sub";
8
- config()
2
+ import { objStrToJson } from "cry-helpers";
3
+ import { config } from "dotenv";
4
+ import { Client } from "../client";
5
+ import { program } from "commander"
6
+ import { EbusRequestOptions } from "../EbusRequestOptions";
7
+ import { exit } from "process";
9
8
 
10
- new EchoWorker().start()
11
- new DelayWorker().start()
12
- new RejectWorker().start()
13
- new ErrorWorker().start()
9
+ config();
14
10
 
11
+ program.version('0.0.2');
12
+
13
+ let client = new Client()
14
+
15
+ function exitProgram(message: string) {
16
+ console.error(message)
17
+ exit(2)
18
+ }
19
+
20
+ program
21
+ .description('calls a worker')
22
+ .option('-w, --workers', 'get workers (default)', true)
23
+ .option('-s, --services', 'get services instead of workers')
24
+ .option('-r, --restart <whom>', 'restart all workers')
25
+ .action(async (params) => {
26
+
27
+ try {
28
+
29
+ if (program.restart) {
30
+ console.log(`request restart of \x1b[32m${program.restart}\x1b[0m workers`)
31
+ await client.restartAllWorkers(program.restart);
32
+ }
33
+ else if (program.services) {
34
+ let res = await client.services()
35
+ console.dir(res)
36
+ } else {
37
+ let res = await client.workers()
38
+ console.dir(res)
39
+ }
40
+
41
+ } catch (err: any) {
42
+ console.error(`\x1b[31munexpected error\x1b[0m ${err.message}`)
43
+ console.log(err)
44
+ process.exit(1)
45
+ }
46
+
47
+ process.exit(0)
48
+ })
49
+
50
+ program.parse(process.argv);
package/src/serialize.ts CHANGED
@@ -1,27 +1,26 @@
1
+
1
2
  import { serialize } from "cry-helpers";
2
- import { deflate, inflate } from "pako";
3
+ import { deflateRaw, inflateRaw } from "pako";
3
4
  import { EbusRequestOptions } from "./EbusRequestOptions";
4
-
5
+ import notepack from "notepack.io"
5
6
 
6
7
  export function pack(data: any, opts: Partial<EbusRequestOptions>)
7
8
  {
8
9
  if (opts.prepacked) return data;
9
10
  if (!opts.raw) {
10
- data = serialize.pack(data)
11
- if (opts.zip) data = deflate(data, { to: "string" })
12
- } else {
13
- if (opts.zip) data = deflate( Uint8Array.from(data) )
11
+ data = serialize.encode(data);
12
+ data = notepack.encode(data);
14
13
  }
14
+ if (opts.zip) data = deflateRaw(data)
15
15
  return data
16
16
  }
17
17
 
18
18
  export function unpack<T>(data: any, opts: Partial<EbusRequestOptions>): T | undefined {
19
19
  if (data === undefined) return undefined;
20
+ if (opts?.zip) data = inflateRaw(data)
20
21
  if (!opts.raw) {
21
- if (opts.zip) data = inflate(data, { to: "string" })
22
- data = serialize.unpack(data)
23
- } else {
24
- if (opts.zip) data = Buffer.from(inflate(data))
22
+ data = notepack.decode(Buffer.from(data))
23
+ data = serialize.decode(data)
25
24
  }
26
25
  return data as T
27
26
  }
@@ -0,0 +1,2 @@
1
+
2
+ declare module "notepack.io"
package/src/test/echo.ts CHANGED
@@ -7,7 +7,7 @@ import { Broker, DelayWorker, unpack, pack } from "../index";
7
7
 
8
8
  describe('echo', async function () {
9
9
 
10
- this.timeout(4000)
10
+ this.timeout(60000)
11
11
 
12
12
  new Broker().start()
13
13
  new EchoWorker().start()
@@ -15,14 +15,17 @@ describe('echo', async function () {
15
15
 
16
16
  let client = new Client()
17
17
 
18
+ console.log('started');
19
+
20
+
18
21
  it('should return passed-in object', async () => {
19
- let rep = await client.request<{ delay: number }>("delay", { delay: 500 })
20
- expect(rep?.delay).to.eq(500)
22
+ let rep = await client.request<{ delay: number }>("delay", { delay: 50 })
23
+ expect(rep?.delay).to.eq(50)
21
24
  });
22
25
 
23
26
  it('should return passed-in object zipped', async () => {
24
- let rep = await client.request<{ delay: number }>("delay", { delay: 500 }, { zip: true })
25
- expect(rep?.delay).to.eq(500)
27
+ let rep = await client.request<{ delay: number }>("delay", { delay: 50 }, { zip: true })
28
+ expect(rep?.delay).to.eq(50)
26
29
  });
27
30
 
28
31
 
@@ -35,24 +38,24 @@ describe('echo', async function () {
35
38
  it('should return passed-in object raw and zipped', async () => {
36
39
  let load = Buffer.from("ABC")
37
40
  let rep = await client.request<{ delay: number }>("echo", load, { raw: true, zip: true })
38
- expect(rep?.toString()).to.eq("ABC")
41
+ expect(Buffer.from(rep as any).toString()).to.eq("ABC")
39
42
  });
40
43
 
44
+
41
45
  it('should return unpacked object', async () => {
42
46
  let load = Buffer.from("ABC")
43
47
  let res = (await client.requestUpacked<{ delay: number }>("echo", load, { raw: true, zip: true, })) as unknown as EbusUnpackedResult<string>
44
- let rep = unpack<string>(res.data,res.opts)
45
- expect(rep?.toString()).to.eq("ABC")
48
+ let rep = unpack<string>(res.data, res.opts)
49
+ expect(Buffer.from(rep as any).toString()).to.eq("ABC")
46
50
  });
47
51
 
48
-
49
52
  it('should accept externally prepacked object', async () => {
50
53
  let load = { a: { b: 2 } }
51
54
  let loadPrepacked = pack(load,{ zip: true })
52
55
  let rep = (await client.request<{ a: { b:2 } }>("echo", loadPrepacked, { prepacked:true, zip: true, receiveTimeout:30000 }))
53
56
  expect(rep!.a.b).to.eq(2)
54
57
  });
55
-
58
+
56
59
  it('should accept externally prepacked object', async () => {
57
60
  let load = { a: { b: 2 } }
58
61
  let loadPrepacked = pack(load, { zip: true })
@@ -60,15 +63,9 @@ describe('echo', async function () {
60
63
  let rep = unpack < { a: { b: 2 } }>(res.data, res.opts)
61
64
  expect(rep!.a.b).to.eq(2)
62
65
  });
66
+
63
67
 
64
- it('should accept externally prepacked raw object', async () => {
65
- let load = { a: { b: 2 } }
66
- let loadPrepacked = pack(load, { zip: true })
67
- let res = (await client.requestUpacked<{ a: { b: 2 } }>("echo", loadPrepacked, { prepacked: true, zip: true, receiveTimeout: 30000 })) as unknown as EbusUnpackedResult<{ a: { b: 2 } }>
68
- let rep = unpack<{ a: { b: 2 } }>(res.data, res.opts)
69
- expect(rep!.a.b).to.eq(2)
70
- });
71
-
68
+
72
69
  it('exists', () => {
73
70
  process.exit()
74
71
  })
package/src/test/pack.ts CHANGED
@@ -1,9 +1,8 @@
1
1
 
2
2
  import { expect } from "chai";
3
- import { EbusRequestOptions } from "../EbusRequestOptions";
4
3
  import { unpack, pack } from "../index";
5
4
 
6
- describe('stress', async function () {
5
+ describe('pack', async function () {
7
6
 
8
7
  let o = () => [{ o: 1, a: ["ABC"] }];
9
8
 
@@ -16,20 +15,23 @@ describe('stress', async function () {
16
15
  expect(o1[0].a[0]).to.eq("ABC")
17
16
  expect(o2[0].a[0]).to.eq("ABC")
18
17
  });
19
-
20
18
  it('should unpack serialized, unzipped', async () => {
21
- let opts = {}
22
- expect(unpack<any>(pack(o(), opts), opts)[0].a[0]).to.eq("ABC")
19
+ let opts = { zip: true }
20
+ let packed = pack(o(), opts)
21
+ let unpacked = unpack<any>(packed, opts)
22
+ expect(unpacked[0].a[0]).to.eq("ABC")
23
23
  });
24
24
  it('should unpack serialized, zipped', async () => {
25
25
  let opts = { zip: true }
26
- expect(unpack<any>(pack(o(), opts), opts)[0].a[0]).to.eq("ABC")
26
+ let packed = pack(o(), opts)
27
+ let unpacked = unpack<any>(packed, opts)
28
+ expect(unpacked[0].a[0]).to.eq("ABC")
27
29
  });
28
30
  it('should unpack serialized, zipped', async () => {
29
31
  let opts = { zip: true, raw: true }
30
32
  let buff1 = Buffer.from( JSON.stringify(o()) )
31
33
  let buff2 = Buffer.from( JSON.stringify(o()) )
32
- let res = unpack<any>(pack(buff1, opts), opts)
34
+ let res = Buffer.from(unpack<any>(pack(buff1, opts), opts))
33
35
  expect(res.toString()).to.equal(buff2.toString())
34
36
  });
35
37
  it('should unpack unserialized, zipped', async () => {
@@ -37,7 +39,6 @@ describe('stress', async function () {
37
39
  let buff1 = Buffer.from(JSON.stringify(o()))
38
40
  let buff2 = Buffer.from(JSON.stringify(o()))
39
41
  let res = unpack<any>(pack(buff1, opts), opts)
40
-
41
42
  for (let i = 0; i < buff2.length; i++)
42
43
  expect(res[i]).to.equal(buff2[i])
43
44
 
@@ -4,16 +4,16 @@ import { EbusRequestOptions } from "../EbusRequestOptions";
4
4
  import { EchoWorker } from "../echoWorker";
5
5
  import { Broker, DelayWorker, unpack, pack } from "../index";
6
6
 
7
- describe('stress', async function () {
8
-
9
- this.timeout(10000)
7
+ describe('performance', async function () {
10
8
 
9
+ this.timeout(160000)
11
10
  new Broker().start()
12
- new DelayWorker().start()
13
11
  new EchoWorker().start()
12
+ new DelayWorker().start()
13
+
14
14
  let client = new Client()
15
15
 
16
- let requestSizes = [1, 10, 100, 1000, 10000]
16
+ let requestSizes = [1, 10, 100, 1000, 10000, 100000]
17
17
 
18
18
  function sizedRq(size: number) {
19
19
  let bigRequest = Array(size)
@@ -29,7 +29,8 @@ describe('stress', async function () {
29
29
 
30
30
  async function measure(name: string, opts: Partial<EbusRequestOptions>) {
31
31
  let sizedRequests = requestSizes.map(r => sizedRq(r))
32
- let line : any[] = [ name ]
32
+ let line: any[] = [name]
33
+
33
34
  for (let req of sizedRequests) {
34
35
  let start = new Date().valueOf()
35
36
  let rep = await client.request<any>("delay", { delay: 10, req }, opts)
@@ -41,13 +42,13 @@ describe('stress', async function () {
41
42
  printLine(line)
42
43
  }
43
44
 
44
- it('should measue raw', async () => {
45
-
45
+ it('should measure raw', async () => {
46
+
46
47
  printLine([ 'opts', ...requestSizes ])
47
- //await measure("-raw -zipped",{ raw:false, zip: false, receiveTimeout: 4000 })
48
- await measure("+raw -zipped",{ raw:true, zip: false, receiveTimeout: 4000 })
49
- //await measure("-raw +zipped",{ raw:false, zip: true, receiveTimeout: 4000 })
50
- await measure("+raw +zipped",{ raw:true, zip: true, receiveTimeout: 4000 })
48
+ await measure("-raw -zipped",{ raw:false, zip: false, receiveTimeout: 4000 })
49
+ // await measure("+raw -zipped",{ raw:true, zip: false, receiveTimeout: 4000 })
50
+ await measure("-raw +zipped",{ raw:false, zip: true, receiveTimeout: 4000 })
51
+ // await measure("+raw +zipped",{ raw:true, zip: true, receiveTimeout: 4000 })
51
52
 
52
53
  });
53
54
 
package/src/test/pub.ts CHANGED
@@ -1,9 +1,7 @@
1
- import { expect } from "chai"
2
- import { Worker, worker } from "cluster";
3
1
  import { Client } from "../client";
4
2
  import { EchoWorker } from "../echoWorker";
5
3
  import { ErrorWorker } from "../errorWorker";
6
- import { Broker, DelayWorker, unpack, pack } from "../index";
4
+ import { Broker, DelayWorker } from "../index";
7
5
  import { RejectWorker } from "../rejectWorker";
8
6
  import { Sub } from "../sub";
9
7
 
package/src/types.ts CHANGED
@@ -19,4 +19,6 @@ export enum Message {
19
19
  Subscribe = "\x12",
20
20
  Publish = "\x13",
21
21
  Shutdown = "\x14",
22
+ ShutdownAndRestart = "\x15",
23
+ RestartAllWorkers = "\x16",
22
24
  }
package/src/worker.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Log , serialize} from "cry-helpers"
2
- import { Dealer, Request } from "zeromq"
2
+ import { Dealer } from "zeromq"
3
3
  import { EbusConfiguration } from "./EbusConfiguration"
4
4
  import { EbusRequestOptions } from "./EbusRequestOptions"
5
5
  import { loadConfiguration } from "./loadConfiguration"
@@ -97,27 +97,39 @@ export class Worker {
97
97
 
98
98
  try {
99
99
 
100
- if (type.toString() === Message.Shutdown) {
101
-
102
- log.status(`worker for ${this.service} received broker shutdown signal`)
103
- break;
104
-
105
- } else
106
-
107
- if (type.toString() === Message.Heartbeat) {
108
- log.trace(`worker for ${this.service} received heartbeat from broker`)
109
-
110
- } else if (type.toString() === Message.Request) {
111
-
112
- this.processing=true
113
- await this.processRequest(reqId, optBuff, type, req, client)
114
- this.processing=false
100
+ switch (type.toString()) {
101
+ case Message.Shutdown:
102
+
103
+ log.status(`worker for ${this.service} received broker shutdown signal`)
104
+ break;
105
+
106
+ case Message.Heartbeat:
107
+
108
+ log.trace(`worker for ${this.service} received heartbeat from broker`)
109
+ break;
110
+
111
+ case Message.ShutdownAndRestart:
112
+
113
+ log.status(`worker for ${this.service} received ShutdownAndRestart from broker`)
114
+ process.exit(1)
115
+ break;
116
+
117
+ case Message.Request:
118
+
119
+ this.processing = true
120
+ await this.processRequest(reqId, optBuff, type, req, client)
121
+ this.processing = false
122
+ break;
123
+
124
+ default:
125
+
126
+ log.error(`worker for ${this.service} cannot process message type ${type}`, [blank1, header, type, client, blank2, reqId, optBuff, req])
127
+ log.error([blank1, header, type, client, blank2, reqId, optBuff, req])
115
128
 
116
- } else {
117
-
118
- log.error(`worker for ${this.service} cannot process message type ${type}`)
119
129
  }
120
130
 
131
+
132
+
121
133
  } catch (err) {
122
134
  console.error(`error in worker for '${this.service}'`)
123
135
  console.error(err)
@@ -151,14 +163,14 @@ export class Worker {
151
163
  }
152
164
 
153
165
  // to be implemented at the actual worker
154
- public async onError(err:any)
166
+ public async onError(err:any, req: any, opts: any)
155
167
  {
156
168
  console.log('worker/onError - should override');
157
169
 
158
170
  }
159
171
 
160
172
  private async processRequest(reqId: Buffer, optBuff: Buffer, type: Buffer, req: Buffer, client: Buffer) {
161
-
173
+
162
174
  let opts = serialize.unpack(optBuff) as Partial<EbusRequestOptions>
163
175
  log.debug(`worker for ${this.service} received request ${type} with opts`, opts)
164
176
 
@@ -176,11 +188,13 @@ export class Worker {
176
188
  rep = await Promise.race(promises)
177
189
  if (rep instanceof EbusRejectError && rep.reason === "timeout") {
178
190
  log.warn(`request expired at worker for '${this.service}' after ${timeout} ms with opts ${serialize.pack(opts)}`)
191
+ console.log('opts',opts);
192
+
179
193
  log.warn(unpack(req, opts))
180
194
  responseMessageType = Message.Timeout
181
- this.onError("timeout");
195
+ this.onError("timeout", unpack(req, opts), opts);
182
196
  }
183
- } catch (err) {
197
+ } catch (err:any) {
184
198
  rep = err
185
199
  if (!(rep instanceof EbusRejectError)) {
186
200
  rep = new EbusRejectError("error", `error in worker for '${this.service}' process method: '${err.message}'`, err);
@@ -193,7 +207,7 @@ export class Worker {
193
207
  if (rep instanceof EbusRejectError && rep.reason === "error")
194
208
  responseMessageType = Message.Error
195
209
 
196
- this.onError(err);
210
+ this.onError(err, unpack(req, opts), opts );
197
211
  }
198
212
 
199
213
  let data = rep;