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

Function feedSync

packages/effect/src/encoding/SchemaBinary.ts:514–631  ·  view source on GitHub ↗
(chunk)

Source from the content-addressed store, hash-verified

512
513 const self: Parser<T> = {
514 feedSync(chunk) {
515 if (stashed !== undefined) return takeStashed()
516 if (spent) return failSync("parser is spent")
517 // Complete frames in a fresh chunk parse in place; only partial frames
518 // are ever copied into the buffer.
519 const direct = bufferEnd === bufferStart
520 let buf: Uint8Array
521 let pos: number
522 let end: number
523 if (direct) {
524 buf = chunk
525 pos = 0
526 end = chunk.length
527 } else {
528 if (chunk.length > 0) {
529 const remaining = bufferEnd - bufferStart
530 const required = remaining + chunk.length
531 if (required > buffer.length) {
532 const next = new Uint8Array(Math.max(256, buffer.length * 2, required))
533 next.set(buffer.subarray(bufferStart, bufferEnd))
534 buffer = next
535 bufferStart = 0
536 bufferEnd = remaining
537 } else if (bufferStart > 0) {
538 buffer.copyWithin(0, bufferStart, bufferEnd)
539 bufferStart = 0
540 bufferEnd = remaining
541 }
542 buffer.set(chunk, bufferEnd)
543 bufferEnd += chunk.length
544 }
545 buf = buffer
546 pos = bufferStart
547 end = bufferEnd
548 }
549 const out: Array<T> = []
550 const done = (): Array<T> => {
551 if (direct) {
552 if (pos < end) stashTail(chunk, pos)
553 } else {
554 bufferStart = pos
555 if (bufferStart === bufferEnd) bufferStart = bufferEnd = 0
556 }
557 return out
558 }
559 const fail = (expected: string, input?: unknown): Array<T> => {
560 spent = true
561 const error = new Schema.SchemaError(new SchemaIssue.InvalidValue({ expected }, input, parseOptions))
562 release()
563 if (out.length === 0) throw error
564 stashed = error
565 return out
566 }
567 while (true) {
568 // Use bigint only for frame lengths wider than six varint groups.
569 let frameLen = 0
570 let headerLen = -1
571 const buffered = end - pos

Callers

nothing calls this directly

Calls 12

takeStashedFunction · 0.85
decodeFrameBodyFunction · 0.85
beginFrameMethod · 0.80
failSyncFunction · 0.70
doneFunction · 0.70
failFunction · 0.70
releaseFunction · 0.70
setMethod · 0.65
resetMethod · 0.65
pushMethod · 0.65
BigIntInterface · 0.50
NumberFunction · 0.50

Tested by

no test coverage detected