jinaga
Version:
Data management for web and mobile applications.
61 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketMessageRouter = void 0;
const CONTROL_KEYWORDS = new Set(["BOOK", "ERR", "SUB", "UNSUB", "ACK"]);
class WebSocketMessageRouter {
constructor(callbacks, controlHandler, authorizationContext) {
this.callbacks = callbacks;
this.controlHandler = controlHandler;
this.buffer = "";
this.authorizationContext = authorizationContext;
}
setAuthorizationContext(ctx) {
this.authorizationContext = ctx;
}
pushChunk(chunk) {
this.buffer += typeof chunk === "string" ? chunk : String(chunk);
this.flush();
}
flush() {
var _a;
const parts = this.buffer.split(/\r?\n/);
this.buffer = (_a = parts.pop()) !== null && _a !== void 0 ? _a : "";
for (let i = 0; i < parts.length; i++) {
const line = parts[i];
if (CONTROL_KEYWORDS.has(line)) {
const keyword = line;
const payload = [];
// Collect until blank terminator; payload lines are JSON-encoded strings
i++;
while (i < parts.length) {
const next = parts[i];
if (next === "") {
break;
}
payload.push(next);
i++;
}
// If we ran out without a blank line terminator, stash partial back into buffer
if (i >= parts.length || parts[i] !== "") {
// Reconstruct the partial frame back into buffer including keyword and payload
const remainder = [keyword, ...payload].join("\n");
this.buffer = remainder + (this.buffer ? "\n" + this.buffer : "");
return;
}
// Process complete control frame
const frame = { keyword, payload };
try {
this.controlHandler.handle(frame);
}
catch (err) {
// Swallow handler errors; protocol continues
}
continue;
}
// Not a control keyword; forward to graph line consumer
this.callbacks.onGraphLine(line);
}
}
}
exports.WebSocketMessageRouter = WebSocketMessageRouter;
//# sourceMappingURL=protocol-router.js.map