@platformatic/itc 2.70.0 → 2.71.0-alpha.1
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/index.d.ts +7 -4
- package/index.test-d.ts +2 -7
- package/lib/itc.js +33 -24
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events'
|
|
2
2
|
import { MessagePort } from 'node:worker_threads'
|
|
3
3
|
|
|
4
|
+
export type Handler = ((data: any) => any) | ((data: any) => Promise<any>)
|
|
5
|
+
|
|
4
6
|
export interface ITCConstructorOptions {
|
|
5
|
-
port: MessagePort
|
|
7
|
+
port: MessagePort
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export class ITC extends EventEmitter {
|
|
9
11
|
constructor (options: ITCConstructorOptions)
|
|
10
12
|
|
|
11
|
-
send (name: string, message: any): Promise<any>
|
|
12
|
-
notify (name: string, message: any): void
|
|
13
|
-
handle (message: string, handler:
|
|
13
|
+
send (name: string, message: any, options?: Record<string, any>): Promise<any>
|
|
14
|
+
notify (name: string, message: any, options?: Record<string, any>): void
|
|
15
|
+
handle (message: string, handler: Handler): void
|
|
16
|
+
getHandler (message: string): Handler | undefined
|
|
14
17
|
listen (): void
|
|
15
18
|
close (): void
|
|
16
19
|
}
|
package/index.test-d.ts
CHANGED
|
@@ -13,7 +13,7 @@ expectType<ITC>(itc)
|
|
|
13
13
|
expectType<Promise<any>>(itc.send('testMessage', { key: 'value' }))
|
|
14
14
|
expectType<void>(itc.notify('testMessage', { key: 'value' }))
|
|
15
15
|
expectType<void>(
|
|
16
|
-
itc.handle('testMessage', async
|
|
16
|
+
itc.handle('testMessage', async data => {
|
|
17
17
|
return { key: 'value' }
|
|
18
18
|
})
|
|
19
19
|
)
|
|
@@ -23,15 +23,10 @@ expectType<void>(itc.close())
|
|
|
23
23
|
expectError(itc.send(123, { key: 'value' })) // send name must be a string
|
|
24
24
|
expectError(itc.notify(123, { key: 'value' })) // send name must be a string
|
|
25
25
|
expectError(
|
|
26
|
-
itc.handle(123, async
|
|
26
|
+
itc.handle(123, async data => {
|
|
27
27
|
return { key: 'value' }
|
|
28
28
|
})
|
|
29
29
|
) // handle message must be a string
|
|
30
|
-
expectError(
|
|
31
|
-
itc.handle('testMessage', (data) => {
|
|
32
|
-
return 'string'
|
|
33
|
-
})
|
|
34
|
-
) // handler must return a Promise
|
|
35
30
|
|
|
36
31
|
itc.on('unhandledError', (error: Error) => {
|
|
37
32
|
expectType<Error>(error)
|
package/lib/itc.js
CHANGED
|
@@ -47,7 +47,7 @@ function generateRequest (name, data) {
|
|
|
47
47
|
version: PLT_ITC_VERSION,
|
|
48
48
|
reqId: randomUUID(),
|
|
49
49
|
name,
|
|
50
|
-
data
|
|
50
|
+
data
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
@@ -58,7 +58,7 @@ function generateResponse (request, error, data) {
|
|
|
58
58
|
reqId: request.reqId,
|
|
59
59
|
name: request.name,
|
|
60
60
|
error,
|
|
61
|
-
data
|
|
61
|
+
data
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -67,7 +67,7 @@ function generateNotification (name, data) {
|
|
|
67
67
|
type: PLT_ITC_NOTIFICATION_TYPE,
|
|
68
68
|
version: PLT_ITC_VERSION,
|
|
69
69
|
name,
|
|
70
|
-
data
|
|
70
|
+
data
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
@@ -80,8 +80,8 @@ function generateUnhandledErrorResponse (error) {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
function sanitize (data) {
|
|
84
|
-
if (!data || typeof data !== 'object') {
|
|
83
|
+
function sanitize (data, transferList) {
|
|
84
|
+
if (!data || typeof data !== 'object' || transferList?.includes(data) || data instanceof Error) {
|
|
85
85
|
return data
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -101,7 +101,7 @@ function sanitize (data) {
|
|
|
101
101
|
continue
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
sanitized.push(value && typeof value === 'object' ? sanitize(value) : value)
|
|
104
|
+
sanitized.push(value && typeof value === 'object' ? sanitize(value, transferList) : value)
|
|
105
105
|
}
|
|
106
106
|
} else {
|
|
107
107
|
sanitized = {}
|
|
@@ -113,7 +113,7 @@ function sanitize (data) {
|
|
|
113
113
|
continue
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
sanitized[key] = value && typeof value === 'object' ? sanitize(value) : value
|
|
116
|
+
sanitized[key] = value && typeof value === 'object' ? sanitize(value, transferList) : value
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
|
|
@@ -176,7 +176,11 @@ class ITC extends EventEmitter {
|
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
|
|
179
|
+
getHandler (message) {
|
|
180
|
+
return this.#handlers.get(message)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async send (name, message, options) {
|
|
180
184
|
if (!this.#listening) {
|
|
181
185
|
throw new errors.SendBeforeListen()
|
|
182
186
|
}
|
|
@@ -185,8 +189,7 @@ class ITC extends EventEmitter {
|
|
|
185
189
|
this._enableKeepAlive()
|
|
186
190
|
|
|
187
191
|
const request = generateRequest(name, message)
|
|
188
|
-
|
|
189
|
-
this._send(request)
|
|
192
|
+
this._send(request, options)
|
|
190
193
|
|
|
191
194
|
const responsePromise = once(this.#requestEmitter, request.reqId).then(([response]) => response)
|
|
192
195
|
|
|
@@ -199,8 +202,8 @@ class ITC extends EventEmitter {
|
|
|
199
202
|
}
|
|
200
203
|
}
|
|
201
204
|
|
|
202
|
-
async notify (name, message) {
|
|
203
|
-
this._send(generateNotification(name, message))
|
|
205
|
+
async notify (name, message, options) {
|
|
206
|
+
this._send(generateNotification(name, message), options)
|
|
204
207
|
}
|
|
205
208
|
|
|
206
209
|
handle (message, handler) {
|
|
@@ -213,14 +216,16 @@ class ITC extends EventEmitter {
|
|
|
213
216
|
}
|
|
214
217
|
this.#listening = true
|
|
215
218
|
|
|
216
|
-
this._setupListener(message => {
|
|
219
|
+
this._setupListener((message, context) => {
|
|
220
|
+
context ??= {}
|
|
221
|
+
|
|
217
222
|
const messageType = message.type
|
|
218
223
|
if (messageType === PLT_ITC_REQUEST_TYPE) {
|
|
219
|
-
this.#handleRequest(message)
|
|
224
|
+
this.#handleRequest(message, context)
|
|
220
225
|
return
|
|
221
226
|
}
|
|
222
227
|
if (messageType === PLT_ITC_RESPONSE_TYPE) {
|
|
223
|
-
this.#handleResponse(message)
|
|
228
|
+
this.#handleResponse(message, context)
|
|
224
229
|
return
|
|
225
230
|
}
|
|
226
231
|
if (messageType === PLT_ITC_NOTIFICATION_TYPE) {
|
|
@@ -253,8 +258,9 @@ class ITC extends EventEmitter {
|
|
|
253
258
|
this.port.on('message', listener)
|
|
254
259
|
}
|
|
255
260
|
|
|
256
|
-
_send (request) {
|
|
257
|
-
|
|
261
|
+
_send (request, options) {
|
|
262
|
+
const transferList = options?.transferList || []
|
|
263
|
+
this.port.postMessage(sanitize(request, transferList), transferList)
|
|
258
264
|
}
|
|
259
265
|
|
|
260
266
|
_createClosePromise () {
|
|
@@ -266,7 +272,7 @@ class ITC extends EventEmitter {
|
|
|
266
272
|
this.port?.close?.()
|
|
267
273
|
}
|
|
268
274
|
|
|
269
|
-
async #handleRequest (raw) {
|
|
275
|
+
async #handleRequest (raw, context) {
|
|
270
276
|
let request = null
|
|
271
277
|
let handler = null
|
|
272
278
|
let response = null
|
|
@@ -278,7 +284,7 @@ class ITC extends EventEmitter {
|
|
|
278
284
|
handler = this.#handlers.get(request.name)
|
|
279
285
|
|
|
280
286
|
if (handler) {
|
|
281
|
-
const result = await handler(request.data)
|
|
287
|
+
const result = await handler(request.data, context)
|
|
282
288
|
response = generateResponse(request, null, result)
|
|
283
289
|
} else {
|
|
284
290
|
if (this.#throwOnMissingHandler) {
|
|
@@ -304,24 +310,27 @@ class ITC extends EventEmitter {
|
|
|
304
310
|
this.#handling = false
|
|
305
311
|
}
|
|
306
312
|
|
|
307
|
-
this._send(response)
|
|
313
|
+
this._send(response, context)
|
|
308
314
|
|
|
309
315
|
if (this.#closeAfterCurrentRequest) {
|
|
310
316
|
this.close()
|
|
311
317
|
}
|
|
312
318
|
}
|
|
313
319
|
|
|
314
|
-
#handleResponse (response) {
|
|
320
|
+
#handleResponse (response, context) {
|
|
315
321
|
try {
|
|
316
322
|
response = parseResponse(response)
|
|
317
323
|
} catch (error) {
|
|
318
324
|
response = generateUnhandledErrorResponse(error)
|
|
319
|
-
this._send(response)
|
|
325
|
+
this._send(response, context)
|
|
320
326
|
return
|
|
321
327
|
}
|
|
322
328
|
|
|
323
|
-
|
|
324
|
-
|
|
329
|
+
this._emitResponse(response)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
_emitResponse (response) {
|
|
333
|
+
this.#requestEmitter.emit(response.reqId, response)
|
|
325
334
|
}
|
|
326
335
|
|
|
327
336
|
_enableKeepAlive () {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/itc",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.71.0-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@watchable/unpromise": "^1.0.2"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
-
"test": "npm run lint && borp --timeout=
|
|
29
|
+
"test": "npm run lint && borp --timeout=1200000 --concurrency=1 --coverage && tsd",
|
|
30
30
|
"lint": "eslint"
|
|
31
31
|
}
|
|
32
32
|
}
|