SPB Git

Repository Public

sse.ts 3.4 KB 104 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : SPIDER — Universal Web Intelligence Engine8 * File             : sdk/typescript/src/sse.ts9 * Module           : Sdk10 * Repository       : https://git.spboucher.ai/spider11 * Project Website  : Not specified12 * Created          : 2026-12-0413 * Last Modified    : 2026-12-0414 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : ISC34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/** Incremental parser of a text/event-stream body. */44import type { JobEvent } from "./types.js";4546export class SSEParser {47  #buffer = "";48  #type = "";49  #data: string[] = [];50  #id: string | null = null;5152  /** Id of the last event seen, to resume with `Last-Event-ID`. */53  get lastEventId(): string | null {54    return this.#id;55  }5657  /** Feed a decoded chunk; returns the events it completes. */58  feed(chunk: string): JobEvent[] {59    this.#buffer += chunk;60    const out: JobEvent[] = [];61    for (;;) {62      const m = /\r\n|\n|\r/.exec(this.#buffer);63      if (!m) break;64      // a trailing "\r" may be the first half of "\r\n": wait for the next chunk65      if (m[0] === "\r" && m.index === this.#buffer.length - 1) break;66      const line = this.#buffer.slice(0, m.index);67      this.#buffer = this.#buffer.slice(m.index + m[0].length);68      const ev = this.#line(line);69      if (ev) out.push(ev);70    }71    return out;72  }7374  #line(line: string): JobEvent | null {75    if (line === "") {76      const raw = this.#data.join("\n");77      const type = this.#type;78      this.#data = [];79      this.#type = "";80      if (!raw && !type) return null; // keep-alive, a bare id or an empty data field81      let data: unknown = raw;82      if (raw) {83        try {84          data = JSON.parse(raw);85        } catch {86          data = raw;87        }88      } else {89        data = null;90      }91      return { id: this.#id, type: type || "message", data };92    }93    if (line.startsWith(":")) return null;94    const i = line.indexOf(":");95    const name = i === -1 ? line : line.slice(0, i);96    let value = i === -1 ? "" : line.slice(i + 1);97    if (value.startsWith(" ")) value = value.slice(1);98    if (name === "event") this.#type = value;99    else if (name === "data") this.#data.push(value);100    else if (name === "id" && !value.includes("\0")) this.#id = value;101    return null;102  }103}104
Files main
Menu

Theme