@platformatic/itc 2.0.0-alpha.5 → 2.0.0-alpha.7
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.
- package/lib/itc.js +14 -6
- package/package.json +4 -3
- package/test/itc.test.js +17 -11
package/lib/itc.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { randomUUID } = require('node:crypto')
|
|
4
4
|
const { EventEmitter, once } = require('node:events')
|
|
5
|
+
const { Unpromise } = require('@watchable/unpromise')
|
|
5
6
|
const errors = require('./errors.js')
|
|
6
7
|
|
|
7
8
|
const PLT_ITC_REQUEST_TYPE = 'PLT_ITC_REQUEST'
|
|
@@ -18,7 +19,7 @@ class ITC extends EventEmitter {
|
|
|
18
19
|
#closePromise
|
|
19
20
|
#closeAfterCurrentRequest
|
|
20
21
|
|
|
21
|
-
constructor ({ port }) {
|
|
22
|
+
constructor ({ port, handlers }) {
|
|
22
23
|
super()
|
|
23
24
|
|
|
24
25
|
this.port = port
|
|
@@ -30,6 +31,13 @@ class ITC extends EventEmitter {
|
|
|
30
31
|
|
|
31
32
|
// Make sure the emitter handle a lot of listeners at once before raising a warning
|
|
32
33
|
this.#requestEmitter.setMaxListeners(1e3)
|
|
34
|
+
|
|
35
|
+
// Register handlers provided with the constructor
|
|
36
|
+
if (typeof handlers === 'object') {
|
|
37
|
+
for (const [name, fn] of Object.entries(handlers)) {
|
|
38
|
+
this.handle(name, fn)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
33
41
|
}
|
|
34
42
|
|
|
35
43
|
async send (name, message) {
|
|
@@ -43,7 +51,7 @@ class ITC extends EventEmitter {
|
|
|
43
51
|
|
|
44
52
|
const responsePromise = once(this.#requestEmitter, request.reqId).then(([response]) => response)
|
|
45
53
|
|
|
46
|
-
const { error, data } = await
|
|
54
|
+
const { error, data } = await Unpromise.race([responsePromise, this.#closePromise])
|
|
47
55
|
|
|
48
56
|
if (error !== null) throw error
|
|
49
57
|
return data
|
|
@@ -207,7 +215,7 @@ class ITC extends EventEmitter {
|
|
|
207
215
|
version: PLT_ITC_VERSION,
|
|
208
216
|
reqId: randomUUID(),
|
|
209
217
|
name,
|
|
210
|
-
data
|
|
218
|
+
data
|
|
211
219
|
}
|
|
212
220
|
}
|
|
213
221
|
|
|
@@ -218,7 +226,7 @@ class ITC extends EventEmitter {
|
|
|
218
226
|
reqId: request.reqId,
|
|
219
227
|
name: request.name,
|
|
220
228
|
error,
|
|
221
|
-
data
|
|
229
|
+
data
|
|
222
230
|
}
|
|
223
231
|
}
|
|
224
232
|
|
|
@@ -227,7 +235,7 @@ class ITC extends EventEmitter {
|
|
|
227
235
|
type: PLT_ITC_NOTIFICATION_TYPE,
|
|
228
236
|
version: PLT_ITC_VERSION,
|
|
229
237
|
name,
|
|
230
|
-
data
|
|
238
|
+
data
|
|
231
239
|
}
|
|
232
240
|
}
|
|
233
241
|
|
|
@@ -236,7 +244,7 @@ class ITC extends EventEmitter {
|
|
|
236
244
|
type: PLT_ITC_UNHANDLED_ERROR_TYPE,
|
|
237
245
|
version: PLT_ITC_VERSION,
|
|
238
246
|
error,
|
|
239
|
-
data: null
|
|
247
|
+
data: null
|
|
240
248
|
}
|
|
241
249
|
}
|
|
242
250
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/itc",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Matteo Collina <hello@matteocollina.com>",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"homepage": "https://github.com/platformatic/platformatic#readme",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@types/node": "^
|
|
17
|
+
"@types/node": "^22.5.0",
|
|
18
18
|
"borp": "^0.17.0",
|
|
19
19
|
"eslint": "9",
|
|
20
20
|
"neostandard": "^0.11.1",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"typescript": "^5.5.4"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@fastify/error": "^3.4.1"
|
|
25
|
+
"@fastify/error": "^3.4.1",
|
|
26
|
+
"@watchable/unpromise": "^1.0.2"
|
|
26
27
|
},
|
|
27
28
|
"scripts": {
|
|
28
29
|
"test": "npm run lint && borp --timeout=180000 --concurrency=1 --coverage && tsd",
|
package/test/itc.test.js
CHANGED
|
@@ -38,20 +38,26 @@ test('should send a request between threads', async t => {
|
|
|
38
38
|
test('should support close while replying to a message', async t => {
|
|
39
39
|
const { port1, port2 } = new MessageChannel()
|
|
40
40
|
|
|
41
|
-
const itc1 = new ITC({ port: port1 })
|
|
42
|
-
const itc2 = new ITC({ port: port2 })
|
|
43
|
-
|
|
44
41
|
const requestName = 'test-command'
|
|
45
42
|
const testRequest = { test: 'test-req-message' }
|
|
46
43
|
const testResponse = { test: 'test-res-message' }
|
|
47
44
|
|
|
48
45
|
const requests = []
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
|
|
47
|
+
const itc1 = new ITC({ port: port1 })
|
|
48
|
+
const itc2 = new ITC({
|
|
49
|
+
port: port2,
|
|
50
|
+
handlers: {
|
|
51
|
+
[requestName] (request) {
|
|
52
|
+
requests.push(request)
|
|
53
|
+
itc2.close()
|
|
54
|
+
return testResponse
|
|
55
|
+
}
|
|
56
|
+
}
|
|
53
57
|
})
|
|
54
58
|
|
|
59
|
+
itc2.handle()
|
|
60
|
+
|
|
55
61
|
itc1.listen()
|
|
56
62
|
itc2.listen()
|
|
57
63
|
|
|
@@ -360,8 +366,8 @@ test('should sanitize a request before sending', async t => {
|
|
|
360
366
|
nested: {
|
|
361
367
|
test: 'test-req-message',
|
|
362
368
|
foo: () => {},
|
|
363
|
-
bar: Symbol('test')
|
|
364
|
-
}
|
|
369
|
+
bar: Symbol('test')
|
|
370
|
+
}
|
|
365
371
|
}
|
|
366
372
|
const testResponse = { test: 'test-res-message' }
|
|
367
373
|
|
|
@@ -382,8 +388,8 @@ test('should sanitize a request before sending', async t => {
|
|
|
382
388
|
assert.deepStrictEqual(requests, [
|
|
383
389
|
{
|
|
384
390
|
test: 'test-req-message',
|
|
385
|
-
nested: { test: 'test-req-message' }
|
|
386
|
-
}
|
|
391
|
+
nested: { test: 'test-req-message' }
|
|
392
|
+
}
|
|
387
393
|
])
|
|
388
394
|
})
|
|
389
395
|
|