MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseLine

Function parseLine

packages/effect/src/encoding/Sse.ts:376–414  ·  view source on GitHub ↗
(line: string)

Source from the content-addressed store, hash-verified

374 }
375
376 function parseLine(line: string) {
377 if (line.length === 0) {
378 // End of event. Clear the state first so a throwing `onParse` cannot
379 // leak it into the next event
380 const named = eventName !== undefined && eventName !== ""
381 const event = eventName || "message"
382 const eventData = data
383 eventName = undefined
384 data = ""
385 if (eventData.length > 0 || (dispatchEmptyNamedEvents && named)) {
386 onParse({
387 _tag: "Event",
388 id: lastEventId,
389 event,
390 data: eventData.slice(0, -1) // remove trailing newline
391 })
392 }
393 return
394 }
395
396 const colon = line.indexOf(":")
397 let field = line
398 let value = ""
399 if (colon >= 0) {
400 field = line.slice(0, colon)
401 value = line.slice(line.charCodeAt(colon + 1) === SPACE ? colon + 2 : colon + 1)
402 }
403
404 if (field === "data") {
405 data += value + "\n"
406 } else if (field === "event") {
407 eventName = value
408 } else if (field === "id" && !value.includes("\u0000")) {
409 lastEventId = value
410 } else if (field === "retry" && /^\d+$/.test(value)) {
411 const retry = parseInt(value, 10)
412 onParse(new Retry({ duration: Duration.millis(retry), lastEventId }))
413 }
414 }
415}
416
417const BOM = "\uFEFF"

Callers 1

feedFunction · 0.70

Calls 1

sliceMethod · 0.80

Tested by

no test coverage detected