MCPcopy
hub / github.com/Effect-TS/effect / feed

Function feed

packages/experimental/src/Sse.ts:120–189  ·  view source on GitHub ↗
(chunk: string)

Source from the content-addressed store, hash-verified

118 }
119
120 function feed(chunk: string): void {
121 buffer = buffer ? buffer + chunk : chunk
122
123 // Strip any UTF8 byte order mark (BOM) at the start of the stream.
124 // Note that we do not strip any non - UTF8 BOM, as eventsource streams are
125 // always decoded as UTF8 as per the specification.
126 if (isFirstChunk && hasBom(buffer)) {
127 buffer = buffer.slice(BOM.length)
128 }
129
130 isFirstChunk = false
131
132 // Set up chunk-specific processing state
133 const length = buffer.length
134 let position = 0
135 let discardTrailingNewline = false
136
137 // Read the current buffer byte by byte
138 while (position < length) {
139 // EventSource allows for carriage return + line feed, which means we
140 // need to ignore a linefeed character if the previous character was a
141 // carriage return
142 // @todo refactor to reduce nesting, consider checking previous byte?
143 // @todo but consider multiple chunks etc
144 if (discardTrailingNewline) {
145 if (buffer[position] === "\n") {
146 ++position
147 }
148 discardTrailingNewline = false
149 }
150
151 let lineLength = -1
152 let fieldLength = startingFieldLength
153 let character: string
154
155 for (let index = startingPosition; lineLength < 0 && index < length; ++index) {
156 character = buffer[index]
157 if (character === ":" && fieldLength < 0) {
158 fieldLength = index - position
159 } else if (character === "\r") {
160 discardTrailingNewline = true
161 lineLength = index - position
162 } else if (character === "\n") {
163 lineLength = index - position
164 }
165 }
166
167 if (lineLength < 0) {
168 startingPosition = length - position
169 startingFieldLength = fieldLength
170 break
171 } else {
172 startingPosition = 0
173 startingFieldLength = -1
174 }
175
176 parseEventStreamLine(buffer, position, fieldLength, lineLength)
177

Callers

nothing calls this directly

Calls 2

hasBomFunction · 0.85
parseEventStreamLineFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…