sseParser.ts ×9

Frontier kind: Code frontier

unlabeled · c_65df264e1d8c

46 tests · 3478 LOC · 19 files · introduces 0 tests · 98 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
9 ranges98 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
490 ranges3478 lines · 19 files · Browse complete extent
All tests (intent)
46 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 98 introduced LOC across 9 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/base/common/sseParser.ts 98 introduced LOC · 9 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- sseParser.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > /**
7 > * Parser for Server-Sent Events (SSE) streams according to the HTML specification.
8 > * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
9 > */
10 >
11 > /**
12 > * Represents an event dispatched from an SSE stream.
13 > */
14 > export interface ISSEEvent {
15 > /**
16 > * The event type. If not specified, the type is "message".
17 > */
18 > type: string;
19 >
20 > /**
21 > * The event data.
22 > */
23 > data: string;
24 >
25 > /**
26 > * The last event ID, used for reconnection.
27 > */
28 > id?: string;
29 >
30 > /**
31 > * Reconnection time in milliseconds.
32 > */
33 > retry?: number;
34 > }
35 >
36 > /**
37 > * Callback function type for event dispatch.
38 > */
39 > export type SSEEventHandler = (event: ISSEEvent) => void;
40 >
41 > const enum Chr {
42 > CR = 13, // '\r'
43 > LF = 10, // '\n'
44 > COLON = 58, // ':'
45 > SPACE = 32, // ' '
46 > }
47 >
48 > /**
49 > * Parser for Server-Sent Events (SSE) streams.
50 > */
51 > export class SSEParser {
52 > private dataBuffer = '';
53 > private eventTypeBuffer = '';
54 > private currentEventId?: string;
55 > private lastEventIdBuffer?: string;
56 > private reconnectionTime?: number;
57 > private buffer: Uint8Array[] = [];
58 > private endedOnCR = false;
59 > private readonly onEventHandler: SSEEventHandler;
60 > private readonly decoder: TextDecoder;
61 > /**
62 > * Creates a new SSE parser.
63 > * @param onEvent The callback to invoke when an event is dispatched.
64 > */
65 > constructor(onEvent: SSEEventHandler) {
66 this.onEventHandler = onEvent;
67 this.decoder = new TextDecoder('utf-8');
68 }
70 > /**
71 > * Gets the last event ID received by this parser.
72 > */
73 > public getLastEventId(): string | undefined {
74 return this.lastEventIdBuffer;
75 }
76 > /** sseParser.ts
77 > * Gets the reconnection time in milliseconds, if one was specified by the server.
78 > */
79 > public getReconnectionTime(): number | undefined {
80 return this.reconnectionTime;
81 }
83 > /**
84 > * Feeds a chunk of the SSE stream to the parser.
85 > * @param chunk The chunk to parse as a Uint8Array of UTF-8 encoded data.
86 > */
87 > public feed(chunk: Uint8Array): void {
88 if (chunk.length === 0) {
89 return;
125 }
126 }
127 > /** sseParser.ts
128 > * Processes a single line from the SSE stream.
129 > */
130 > private processLine(line: string): void {
131 if (!line.length) {
132 this.dispatchEvent();
160 this.processField(field, value);
161 }
162 > /** sseParser.ts
163 > * Processes a field with the given name and value.
164 > */
165 > private processField(field: string, value: string): void {
166 switch (field) {
167 case 'event':
194 }
195 }
196 > /** sseParser.ts
197 > * Dispatches the event based on the current buffer states.
198 > */
199 > private dispatchEvent(): void {
200 // If the data buffer is empty, reset the buffers and return
201 if (this.dataBuffer === '') {
231 this.reset();
232 }
233 > sseParser.ts
234 > /**
235 > * Resets the parser state.
236 > */
237 > public reset(): void {
238 this.dataBuffer = '';
239 this.eventTypeBuffer = '';
241 // Note: lastEventIdBuffer is not reset as it's used for reconnection
242 }
243 > } sseParser.ts
244
245