@mulmobridge/chat-service 1.0.3 → 1.2.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.
- package/dist/index.d.ts +7 -0
- package/dist/index.js +36 -0
- package/dist/socket.js +15 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,13 @@ export interface ChatService {
|
|
|
14
14
|
* before `attachSocket`: the message is queued and flushes on
|
|
15
15
|
* the next bridge connection for that transport. */
|
|
16
16
|
pushToBridge: PushFn;
|
|
17
|
+
/** Subscribe an IN-PROCESS bridge to this transport's pushes (#3080).
|
|
18
|
+
* Returns the unsubscribe function. An in-process bridge is always live, so
|
|
19
|
+
* its pushes are never queued. */
|
|
20
|
+
registerInProcessBridge(transportId: string, handler: (event: {
|
|
21
|
+
chatId: string;
|
|
22
|
+
message: string;
|
|
23
|
+
}) => void): () => void;
|
|
17
24
|
}
|
|
18
25
|
export declare function createChatService(deps: ChatServiceDeps): ChatService;
|
|
19
26
|
export type { Attachment, ChatServiceDeps, StartChatFn, StartChatParams, OnSessionEventFn } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -60,7 +60,42 @@ export function createChatService(deps) {
|
|
|
60
60
|
// The queue is shared with the socket layer so any pushes
|
|
61
61
|
// enqueued during the pre-attach window flush on first connect.
|
|
62
62
|
let livePush = null;
|
|
63
|
+
// In-process bridges (#3080). Keyed by transportId, at most one each: two
|
|
64
|
+
// bridges claiming one transportId is what #3079 exists to stop, and the
|
|
65
|
+
// registry refuses the second rather than silently fanning a reply out twice.
|
|
66
|
+
const inProcessBridges = new Map();
|
|
67
|
+
const registerInProcessBridge = (transportId, handler) => {
|
|
68
|
+
if (inProcessBridges.has(transportId)) {
|
|
69
|
+
throw new Error(`an in-process bridge is already registered for transport "${transportId}"`);
|
|
70
|
+
}
|
|
71
|
+
inProcessBridges.set(transportId, handler);
|
|
72
|
+
return () => {
|
|
73
|
+
if (inProcessBridges.get(transportId) === handler)
|
|
74
|
+
inProcessBridges.delete(transportId);
|
|
75
|
+
};
|
|
76
|
+
};
|
|
63
77
|
const pushToBridge = (transportId, chatId, message) => {
|
|
78
|
+
// An in-process bridge is in this very process, so it is live by
|
|
79
|
+
// construction — deliver and return rather than consulting the socket
|
|
80
|
+
// layer, whose miss path would queue a message that has been delivered.
|
|
81
|
+
const inProcess = inProcessBridges.get(transportId);
|
|
82
|
+
if (inProcess) {
|
|
83
|
+
try {
|
|
84
|
+
// Runs on WHATEVER stack called `pushToBridge` — a route handler, a
|
|
85
|
+
// scheduler tick. A socket bridge cannot reach us here because its
|
|
86
|
+
// handler runs in its own process; an in-process one can, so the
|
|
87
|
+
// isolation has to be written rather than assumed.
|
|
88
|
+
inProcess({ chatId, message });
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
logger.error("chat-service", "in-process bridge push handler threw", {
|
|
92
|
+
transportId,
|
|
93
|
+
chatId,
|
|
94
|
+
error: err instanceof Error ? err.message : String(err),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
64
99
|
if (livePush) {
|
|
65
100
|
livePush(transportId, chatId, message);
|
|
66
101
|
return;
|
|
@@ -152,6 +187,7 @@ export function createChatService(deps) {
|
|
|
152
187
|
livePush = handle.pushToBridge;
|
|
153
188
|
},
|
|
154
189
|
pushToBridge,
|
|
190
|
+
registerInProcessBridge,
|
|
155
191
|
};
|
|
156
192
|
}
|
|
157
193
|
export { writeFileAtomic } from "./atomic-write.js";
|
package/dist/socket.js
CHANGED
|
@@ -45,6 +45,15 @@ export const CHAT_SOCKET_EVENTS = {
|
|
|
45
45
|
/** server → bridge streaming text chunk (Phase C of #268). */
|
|
46
46
|
textChunk: "textChunk",
|
|
47
47
|
};
|
|
48
|
+
// Hard limits to prevent oversized payloads from bridges (DoS /
|
|
49
|
+
// accidental misconfiguration). Express's JSON body limit (50 MB)
|
|
50
|
+
// is the outer gate; these are tighter, attachment-specific caps.
|
|
51
|
+
const MAX_ATTACHMENT_COUNT = 10;
|
|
52
|
+
const MAX_ATTACHMENT_TOTAL_BYTES = 20 * 1024 * 1024; // 20 MB base64
|
|
53
|
+
/** Transport ceiling for one socket frame. Headroom over the attachment
|
|
54
|
+
* budget for the rest of the payload (text, ids, options) so the
|
|
55
|
+
* application-level cap is what a bridge actually hits. */
|
|
56
|
+
const MAX_SOCKET_PAYLOAD_BYTES = MAX_ATTACHMENT_TOTAL_BYTES + 4 * 1024 * 1024;
|
|
48
57
|
export function bridgeRoom(transportId) {
|
|
49
58
|
return `bridge:${transportId}`;
|
|
50
59
|
}
|
|
@@ -55,6 +64,12 @@ export function attachChatSocket(server, deps) {
|
|
|
55
64
|
// Loopback-only deployment; skip long-polling negotiation for
|
|
56
65
|
// the same reason `/ws/pubsub` does (#311).
|
|
57
66
|
transports: ["websocket"],
|
|
67
|
+
// Socket.IO's default is 1 MB — below the attachment budget
|
|
68
|
+
// `parseAttachments` enforces, so a bridge shipping a 900 KB image
|
|
69
|
+
// had its connection closed before the ack instead of a reply.
|
|
70
|
+
// `parseAttachments` stays the real gate; this only has to be
|
|
71
|
+
// wide enough not to pre-empt it.
|
|
72
|
+
maxHttpBufferSize: MAX_SOCKET_PAYLOAD_BYTES,
|
|
58
73
|
});
|
|
59
74
|
io.use((socket, next) => {
|
|
60
75
|
const result = validateHandshake(socket.handshake.auth, tokenProvider);
|
|
@@ -232,11 +247,6 @@ function parseMessagePayload(payload) {
|
|
|
232
247
|
const attachments = parseAttachments(payload.attachments);
|
|
233
248
|
return { ok: true, externalChatId, text, attachments };
|
|
234
249
|
}
|
|
235
|
-
// Hard limits to prevent oversized payloads from bridges (DoS /
|
|
236
|
-
// accidental misconfiguration). Express's JSON body limit (50 MB)
|
|
237
|
-
// is the outer gate; these are tighter, attachment-specific caps.
|
|
238
|
-
const MAX_ATTACHMENT_COUNT = 10;
|
|
239
|
-
const MAX_ATTACHMENT_TOTAL_BYTES = 20 * 1024 * 1024; // 20 MB base64
|
|
240
250
|
export function parseAttachments(raw) {
|
|
241
251
|
if (!Array.isArray(raw) || raw.length === 0)
|
|
242
252
|
return undefined;
|
package/package.json
CHANGED