velocious 1.0.562 → 1.0.563
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/README.md +3 -1
- package/build/configuration-types.js +1 -0
- package/build/configuration.js +44 -3
- package/build/http-server/client/websocket-session.js +3 -3
- package/build/http-server/client-delivery-queue.js +168 -0
- package/build/http-server/server-client.js +9 -0
- package/build/http-server/websocket-events-host.js +42 -12
- package/build/http-server/worker-handler/in-process.js +53 -14
- package/build/http-server/worker-handler/index.js +88 -18
- package/build/http-server/worker-handler/worker-thread.js +2 -2
- package/build/src/configuration-types.d.ts +8 -0
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +2 -1
- package/build/src/configuration.d.ts +34 -1
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +40 -4
- package/build/src/http-server/client/websocket-session.js +4 -4
- package/build/src/http-server/client-delivery-queue.d.ts +121 -0
- package/build/src/http-server/client-delivery-queue.d.ts.map +1 -0
- package/build/src/http-server/client-delivery-queue.js +152 -0
- package/build/src/http-server/server-client.d.ts +6 -0
- package/build/src/http-server/server-client.d.ts.map +1 -1
- package/build/src/http-server/server-client.js +10 -1
- package/build/src/http-server/websocket-events-host.d.ts +15 -4
- package/build/src/http-server/websocket-events-host.d.ts.map +1 -1
- package/build/src/http-server/websocket-events-host.js +39 -13
- package/build/src/http-server/worker-handler/in-process.d.ts +19 -1
- package/build/src/http-server/worker-handler/in-process.d.ts.map +1 -1
- package/build/src/http-server/worker-handler/in-process.js +50 -14
- package/build/src/http-server/worker-handler/index.d.ts +36 -4
- package/build/src/http-server/worker-handler/index.d.ts.map +1 -1
- package/build/src/http-server/worker-handler/index.js +82 -18
- package/build/src/http-server/worker-handler/worker-thread.js +3 -3
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/configuration-types.js +1 -0
- package/src/configuration.js +44 -3
- package/src/http-server/client/websocket-session.js +3 -3
- package/src/http-server/client-delivery-queue.js +168 -0
- package/src/http-server/server-client.js +9 -0
- package/src/http-server/websocket-events-host.js +42 -12
- package/src/http-server/worker-handler/in-process.js +53 -14
- package/src/http-server/worker-handler/index.js +88 -18
- package/src/http-server/worker-handler/worker-thread.js +2 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import Client from "../client/index.js"
|
|
4
|
+
import ClientDeliveryQueue from "../client-delivery-queue.js"
|
|
4
5
|
import dispatchChannelSubscribers from "./channel-subscriber-dispatch.js"
|
|
5
6
|
import Logger from "../../logger.js"
|
|
6
7
|
import websocketEventsHost from "../websocket-events-host.js"
|
|
@@ -23,7 +24,7 @@ export default class VelociousHttpServerInProcessHandler {
|
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Narrows the runtime value to the documented type.
|
|
26
|
-
* @type {Record<number, {httpClient: Client, serverClient: import("../server-client.js").default}>} */
|
|
27
|
+
* @type {Record<number, {deliveryQueue: ClientDeliveryQueue, httpClient: Client, serverClient: import("../server-client.js").default}>} */
|
|
27
28
|
this.clients = {}
|
|
28
29
|
|
|
29
30
|
/** @type {Set<Promise<void>>} */
|
|
@@ -56,25 +57,36 @@ export default class VelociousHttpServerInProcessHandler {
|
|
|
56
57
|
remoteAddress: serverClient.remoteAddress
|
|
57
58
|
})
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
const {maxBytes, maxFrames} = this.configuration.getWebsocketOutboundQueueLimits()
|
|
61
|
+
const deliveryQueue = new ClientDeliveryQueue({
|
|
62
|
+
clientCount,
|
|
63
|
+
maxBytes,
|
|
64
|
+
maxFrames,
|
|
65
|
+
onOverflow: (error) => {
|
|
66
|
+
deliveryQueue.destroy()
|
|
67
|
+
serverClient.destroy(error)
|
|
68
|
+
this._reportOutboundQueueOverflow({clientCount, error})
|
|
69
|
+
}
|
|
70
|
+
})
|
|
67
71
|
|
|
68
|
-
httpClient.events.on("output", (output) => {
|
|
72
|
+
httpClient.events.on("output", (output, {websocketFrame = false} = {}) => {
|
|
69
73
|
if (output !== null && output !== undefined) {
|
|
70
|
-
|
|
74
|
+
const delivery = () => serverClient.send(output)
|
|
75
|
+
const queued = websocketFrame
|
|
76
|
+
? deliveryQueue.enqueueFrame({
|
|
77
|
+
byteLength: typeof output === "string" ? Buffer.byteLength(output) : output.byteLength,
|
|
78
|
+
delivery
|
|
79
|
+
})
|
|
80
|
+
: deliveryQueue.enqueueControl(delivery)
|
|
81
|
+
|
|
82
|
+
void queued.catch((error) => {
|
|
71
83
|
this.logger.error(() => ["Failed to deliver client output", {clientCount}, error])
|
|
72
84
|
})
|
|
73
85
|
}
|
|
74
86
|
})
|
|
75
87
|
|
|
76
88
|
httpClient.events.on("file", ({filePath, sendBody, settle}) => {
|
|
77
|
-
void
|
|
89
|
+
void deliveryQueue.enqueueControl(async () => {
|
|
78
90
|
await settle(await serverClient.sendFile(filePath, sendBody))
|
|
79
91
|
}).catch((error) => {
|
|
80
92
|
this.logger.error(() => ["Failed to deliver file response", {clientCount, filePath}, error])
|
|
@@ -83,11 +95,12 @@ export default class VelociousHttpServerInProcessHandler {
|
|
|
83
95
|
})
|
|
84
96
|
|
|
85
97
|
httpClient.events.on("close", () => {
|
|
86
|
-
void
|
|
98
|
+
void deliveryQueue.enqueueControl(() => serverClient.end())
|
|
87
99
|
.finally(() => delete this.clients[clientCount])
|
|
88
100
|
})
|
|
89
101
|
|
|
90
102
|
serverClient.events.on("close", () => {
|
|
103
|
+
deliveryQueue.destroy()
|
|
91
104
|
const cleanup = httpClient.abortPendingFileResponses()
|
|
92
105
|
.catch((error) => {
|
|
93
106
|
this.logger.warn("Failed to abort file responses after client close", error)
|
|
@@ -100,7 +113,7 @@ export default class VelociousHttpServerInProcessHandler {
|
|
|
100
113
|
this.pendingClientCloseCleanups.add(cleanup)
|
|
101
114
|
})
|
|
102
115
|
|
|
103
|
-
this.clients[clientCount] = {httpClient, serverClient}
|
|
116
|
+
this.clients[clientCount] = {deliveryQueue, httpClient, serverClient}
|
|
104
117
|
|
|
105
118
|
// Create a message-port shim so ServerClient.onSocketData can route data
|
|
106
119
|
// to the in-process HTTP Client without needing a real worker thread.
|
|
@@ -118,6 +131,24 @@ export default class VelociousHttpServerInProcessHandler {
|
|
|
118
131
|
serverClient.listen()
|
|
119
132
|
}
|
|
120
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Reports a per-client outbound queue overflow.
|
|
136
|
+
* @param {object} args - Overflow details.
|
|
137
|
+
* @param {number} args.clientCount - Affected client.
|
|
138
|
+
* @param {Error} args.error - Overflow error.
|
|
139
|
+
* @returns {void}
|
|
140
|
+
*/
|
|
141
|
+
_reportOutboundQueueOverflow({clientCount, error}) {
|
|
142
|
+
const errorPayload = {
|
|
143
|
+
context: {clientCount, websocketOutboundQueueOverflow: true, workerCount: this.workerCount},
|
|
144
|
+
error
|
|
145
|
+
}
|
|
146
|
+
const errorEvents = this.configuration.getErrorEvents()
|
|
147
|
+
|
|
148
|
+
errorEvents.emit("framework-error", errorPayload)
|
|
149
|
+
errorEvents.emit("all-error", {...errorPayload, errorType: "framework-error"})
|
|
150
|
+
}
|
|
151
|
+
|
|
121
152
|
/**
|
|
122
153
|
* Runs stop.
|
|
123
154
|
* @returns {Promise<void>} */
|
|
@@ -158,6 +189,14 @@ export default class VelociousHttpServerInProcessHandler {
|
|
|
158
189
|
return this.configuration._broadcastToChannelLocal(channel, broadcastParams, body, {eventId})
|
|
159
190
|
}
|
|
160
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Gets the configuration-wide V2 broadcast target shared by in-process handlers.
|
|
194
|
+
* @returns {import("../../configuration.js").default} - Shared configuration target.
|
|
195
|
+
*/
|
|
196
|
+
websocketV2BroadcastDispatchKey() {
|
|
197
|
+
return this.configuration
|
|
198
|
+
}
|
|
199
|
+
|
|
161
200
|
/**
|
|
162
201
|
* Runs dispatch websocket event.
|
|
163
202
|
* @param {object} args - Options object.
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import {ensureError} from "typanic"
|
|
4
4
|
import Logger from "../../logger.js"
|
|
5
|
+
import ClientDeliveryQueue from "../client-delivery-queue.js"
|
|
5
6
|
import {Worker} from "worker_threads"
|
|
6
7
|
import websocketEventsHost from "../websocket-events-host.js"
|
|
7
8
|
|
|
@@ -59,7 +60,7 @@ export default class VelociousHttpServerWorker {
|
|
|
59
60
|
* @type {Map<number, {resolve: (snapshot: Record<string, ?>) => void}>} */
|
|
60
61
|
this._debugSnapshotRequests = new Map()
|
|
61
62
|
|
|
62
|
-
/** @type {Map<number,
|
|
63
|
+
/** @type {Map<number, ClientDeliveryQueue>} */
|
|
63
64
|
this._clientDeliveryQueues = new Map()
|
|
64
65
|
}
|
|
65
66
|
|
|
@@ -117,6 +118,7 @@ export default class VelociousHttpServerWorker {
|
|
|
117
118
|
if (!this.clients[clientCount] && !this._clientDeliveryQueues.has(clientCount)) return
|
|
118
119
|
|
|
119
120
|
delete this.clients[clientCount]
|
|
121
|
+
this._clientDeliveryQueues.get(clientCount)?.destroy()
|
|
120
122
|
this._clientDeliveryQueues.delete(clientCount)
|
|
121
123
|
this.worker?.postMessage({command: "clientAbort", clientCount})
|
|
122
124
|
}
|
|
@@ -163,6 +165,10 @@ export default class VelociousHttpServerWorker {
|
|
|
163
165
|
_closeAllClients() {
|
|
164
166
|
const clients = Object.values(this.clients)
|
|
165
167
|
this.clients = {}
|
|
168
|
+
const deliveryQueues = this._clientDeliveryQueues
|
|
169
|
+
this._clientDeliveryQueues = new Map()
|
|
170
|
+
|
|
171
|
+
for (const queue of deliveryQueues.values()) queue.destroy()
|
|
166
172
|
|
|
167
173
|
for (const client of clients) {
|
|
168
174
|
try {
|
|
@@ -182,6 +188,7 @@ export default class VelociousHttpServerWorker {
|
|
|
182
188
|
* @param {string} [data.filePath] - File path.
|
|
183
189
|
* @param {boolean} [data.sendBody] - Whether to send the file body.
|
|
184
190
|
* @param {number} [data.transferId] - File transfer id.
|
|
191
|
+
* @param {boolean} [data.websocketFrame] - Whether output is a completed WebSocket frame.
|
|
185
192
|
* @param {string} [data.channel] - Channel name.
|
|
186
193
|
* @param {number} [data.requestId] - Debug request id.
|
|
187
194
|
* @param {Record<string, ?>} [data.snapshot] - Worker debug snapshot.
|
|
@@ -219,7 +226,11 @@ export default class VelociousHttpServerWorker {
|
|
|
219
226
|
if (output !== null && output !== undefined) {
|
|
220
227
|
const outputLength = typeof output === "string" ? output.length : output.byteLength
|
|
221
228
|
|
|
222
|
-
|
|
229
|
+
const delivery = data.websocketFrame === true
|
|
230
|
+
? this.enqueueClientFrame(client, output)
|
|
231
|
+
: this.enqueueClientControl(client, () => client.send(output))
|
|
232
|
+
|
|
233
|
+
void delivery.then(() => {
|
|
223
234
|
this.logger.debug(() => ["Client output delivered", {
|
|
224
235
|
clientCount,
|
|
225
236
|
outputLength,
|
|
@@ -243,7 +254,7 @@ export default class VelociousHttpServerWorker {
|
|
|
243
254
|
return
|
|
244
255
|
}
|
|
245
256
|
|
|
246
|
-
void this.
|
|
257
|
+
void this.enqueueClientControl(client, async () => {
|
|
247
258
|
const result = await client.sendFile(filePath, sendBody !== false)
|
|
248
259
|
|
|
249
260
|
this.worker?.postMessage({command: "clientFileResult", result, transferId})
|
|
@@ -260,7 +271,7 @@ export default class VelociousHttpServerWorker {
|
|
|
260
271
|
return
|
|
261
272
|
}
|
|
262
273
|
|
|
263
|
-
void this.
|
|
274
|
+
void this.enqueueClientControl(client, () => client.end())
|
|
264
275
|
.finally(() => delete this.clients[client.clientCount])
|
|
265
276
|
} else if (command == "debugSnapshot") {
|
|
266
277
|
const {requestId, snapshot} = data
|
|
@@ -289,7 +300,12 @@ export default class VelociousHttpServerWorker {
|
|
|
289
300
|
throw new Error("Worker websocket v2-broadcast channel must be a string")
|
|
290
301
|
}
|
|
291
302
|
|
|
292
|
-
websocketEventsHost.broadcastV2({
|
|
303
|
+
websocketEventsHost.broadcastV2({
|
|
304
|
+
body,
|
|
305
|
+
broadcastParams: broadcastParams || {},
|
|
306
|
+
channel,
|
|
307
|
+
configuration: this.configuration
|
|
308
|
+
})
|
|
293
309
|
} else {
|
|
294
310
|
throw new Error(`Unknown command: ${command}`)
|
|
295
311
|
}
|
|
@@ -297,24 +313,70 @@ export default class VelociousHttpServerWorker {
|
|
|
297
313
|
|
|
298
314
|
/**
|
|
299
315
|
* Preserves socket output ordering for one client.
|
|
300
|
-
* @param {
|
|
316
|
+
* @param {import("../server-client.js").default} client - Client instance.
|
|
317
|
+
* @param {string | Uint8Array} output - Complete output buffer.
|
|
318
|
+
* @returns {Promise<void>} - Queued delivery.
|
|
319
|
+
*/
|
|
320
|
+
enqueueClientFrame(client, output) {
|
|
321
|
+
const byteLength = typeof output === "string" ? Buffer.byteLength(output) : output.byteLength
|
|
322
|
+
|
|
323
|
+
return this._deliveryQueueFor(client).enqueueFrame({
|
|
324
|
+
byteLength,
|
|
325
|
+
delivery: () => client.send(output)
|
|
326
|
+
})
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Preserves ordering for a delivery that retains no complete output frame.
|
|
331
|
+
* @param {import("../server-client.js").default} client - Client instance.
|
|
301
332
|
* @param {() => Promise<void>} delivery - Delivery operation.
|
|
302
333
|
* @returns {Promise<void>} - Queued delivery.
|
|
303
334
|
*/
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
.catch(() => {})
|
|
308
|
-
.then(delivery)
|
|
309
|
-
|
|
310
|
-
this._clientDeliveryQueues.set(clientCount, queued)
|
|
311
|
-
const clearQueue = () => {
|
|
312
|
-
if (this._clientDeliveryQueues.get(clientCount) === queued) this._clientDeliveryQueues.delete(clientCount)
|
|
313
|
-
}
|
|
335
|
+
enqueueClientControl(client, delivery) {
|
|
336
|
+
return this._deliveryQueueFor(client).enqueueControl(delivery)
|
|
337
|
+
}
|
|
314
338
|
|
|
315
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Gets or creates one client's delivery queue.
|
|
341
|
+
* @param {import("../server-client.js").default} client - Client instance.
|
|
342
|
+
* @returns {ClientDeliveryQueue} - Client-owned delivery queue.
|
|
343
|
+
*/
|
|
344
|
+
_deliveryQueueFor(client) {
|
|
345
|
+
const existing = this._clientDeliveryQueues.get(client.clientCount)
|
|
346
|
+
if (existing) return existing
|
|
347
|
+
|
|
348
|
+
const {maxBytes, maxFrames} = this.configuration.getWebsocketOutboundQueueLimits()
|
|
349
|
+
const queue = new ClientDeliveryQueue({
|
|
350
|
+
clientCount: client.clientCount,
|
|
351
|
+
maxBytes,
|
|
352
|
+
maxFrames,
|
|
353
|
+
onOverflow: (error) => {
|
|
354
|
+
queue.destroy()
|
|
355
|
+
client.destroy(error)
|
|
356
|
+
this._reportOutboundQueueOverflow({clientCount: client.clientCount, error})
|
|
357
|
+
}
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
this._clientDeliveryQueues.set(client.clientCount, queue)
|
|
361
|
+
return queue
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Reports a per-client outbound queue overflow.
|
|
366
|
+
* @param {object} args - Overflow details.
|
|
367
|
+
* @param {number} args.clientCount - Affected client.
|
|
368
|
+
* @param {Error} args.error - Overflow error.
|
|
369
|
+
* @returns {void}
|
|
370
|
+
*/
|
|
371
|
+
_reportOutboundQueueOverflow({clientCount, error}) {
|
|
372
|
+
const errorPayload = {
|
|
373
|
+
context: {clientCount, websocketOutboundQueueOverflow: true, workerCount: this.workerCount},
|
|
374
|
+
error
|
|
375
|
+
}
|
|
376
|
+
const errorEvents = this.configuration.getErrorEvents()
|
|
316
377
|
|
|
317
|
-
|
|
378
|
+
errorEvents.emit("framework-error", errorPayload)
|
|
379
|
+
errorEvents.emit("all-error", {...errorPayload, errorType: "framework-error"})
|
|
318
380
|
}
|
|
319
381
|
|
|
320
382
|
/**
|
|
@@ -410,6 +472,14 @@ export default class VelociousHttpServerWorker {
|
|
|
410
472
|
this.worker.postMessage({body, broadcastParams, channel, command: "websocketV2Broadcast", eventId, createdAt})
|
|
411
473
|
}
|
|
412
474
|
|
|
475
|
+
/**
|
|
476
|
+
* Gets this worker's isolated V2 broadcast target.
|
|
477
|
+
* @returns {VelociousHttpServerWorker} - This worker handler.
|
|
478
|
+
*/
|
|
479
|
+
websocketV2BroadcastDispatchKey() {
|
|
480
|
+
return this
|
|
481
|
+
}
|
|
482
|
+
|
|
413
483
|
/**
|
|
414
484
|
* Runs register with events host.
|
|
415
485
|
* @returns {void} */
|
|
@@ -154,8 +154,8 @@ export default class VelociousHttpServerWorkerHandlerWorkerThread {
|
|
|
154
154
|
remoteAddress
|
|
155
155
|
})
|
|
156
156
|
|
|
157
|
-
client.events.on("output", (output) => {
|
|
158
|
-
this.parentPort.postMessage({command: "clientOutput", clientCount, output})
|
|
157
|
+
client.events.on("output", (output, {websocketFrame = false} = {}) => {
|
|
158
|
+
this.parentPort.postMessage({command: "clientOutput", clientCount, output, websocketFrame})
|
|
159
159
|
})
|
|
160
160
|
|
|
161
161
|
client.events.on("file", ({filePath, sendBody, settle}) => {
|