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

Function makeParser

packages/effect/src/encoding/SchemaBinary.ts:468–660  ·  view source on GitHub ↗
(
  layout: Layout,
  mode: Mode,
  parseOptions: SchemaAST.ParseOptions,
  maxFrameSize: number | undefined,
  decodeEncoded: ((value: unknown) => unknown) | undefined,
  dictionary: boolean
)

Source from the content-addressed store, hash-verified

466// Frame parser over a compiled layout. `decodeEncoded` runs the target schema
467// pass on each framed value; `undefined` yields the raw binary-decoded values.
468function makeParser<T>(
469 layout: Layout,
470 mode: Mode,
471 parseOptions: SchemaAST.ParseOptions,
472 maxFrameSize: number | undefined,
473 decodeEncoded: ((value: unknown) => unknown) | undefined,
474 dictionary: boolean
475): Parser<T> {
476 const dict: DictRead | undefined = dictionary ? [] : undefined
477 let buffer = new Uint8Array(0)
478 let bufferStart = 0
479 let bufferEnd = 0
480 let stashed: Schema.SchemaError | undefined
481 let spent = false
482 const body = new Reader()
483 const indexSignatures = new IndexSignatureCache(parseOptions, PARSER_INDEX_SIGNATURE_CACHE_SIZE)
484
485 const release = () => {
486 body.release()
487 indexSignatures.clear()
488 buffer = EMPTY_READER_BUFFER
489 bufferStart = bufferEnd = 0
490 }
491
492 const failSync = (expected: string, input?: unknown): never => {
493 throw new Schema.SchemaError(new SchemaIssue.InvalidValue({ expected }, input, parseOptions))
494 }
495
496 const takeStashed = (): never => {
497 const error = stashed!
498 stashed = undefined
499 throw error
500 }
501
502 // Copy the unconsumed tail of a directly parsed chunk into the buffer.
503 const stashTail = (chunk: Uint8Array, start: number) => {
504 const remaining = chunk.length - start
505 if (remaining > buffer.length) {
506 buffer = new Uint8Array(Math.max(256, remaining))
507 }
508 buffer.set(start === 0 ? chunk : chunk.subarray(start), 0)
509 bufferStart = 0
510 bufferEnd = remaining
511 }
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

Callers 2

parserFunction · 0.70
decodeFunction · 0.70

Calls 4

feedSyncMethod · 0.80
endSyncMethod · 0.80
failMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected