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

Function decode

packages/effect/src/encoding/SchemaBinary.ts:737–831  ·  view source on GitHub ↗
(
  schema: S,
  options?: SchemaAST.ParseOptions & Options & { readonly maxFrameSize?: number | undefined }
)

Source from the content-addressed store, hash-verified

735 * @since 4.0.0
736 */
737export const decode = <S extends Schema.Constraint>(
738 schema: S,
739 options?: SchemaAST.ParseOptions & Options & { readonly maxFrameSize?: number | undefined }
740) =>
741<IE = never, Done = unknown>(): Channel.Channel<
742 Arr.NonEmptyReadonlyArray<S["Type"]>,
743 Schema.SchemaError | IE,
744 Done,
745 Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>,
746 IE,
747 Done,
748 S["DecodingServices"]
749> => {
750 // Everything here depends only on schema and options, so it is derived once
751 // per channel; the frame parser and failure stash below stay per run.
752 const { exact, exitSuccess, layout, target } = compileTarget(schema)
753 const parseOptions: SchemaAST.ParseOptions = options ?? SchemaAST.defaultParseOptions
754 const mode = compileMode(layout, options?.fingerprint)
755 const decodeTarget = exact
756 ? undefined
757 : Schema.decodeUnknownEffect(target as Schema.ConstraintDecoder<unknown>, parseOptions)
758 const decodeEncoded = decodeTarget !== undefined && exitSuccess
759 ? (value: unknown) => isSuccessExit(value) ? Effect.succeed(value) : decodeTarget(value)
760 : decodeTarget
761 return Channel.fromTransform((upstream, _scope) =>
762 Effect.sync(() => {
763 const frames = makeParser<unknown>(layout, mode, parseOptions, options?.maxFrameSize, undefined, false)
764 let stashed: Schema.SchemaError | undefined
765
766 // Decodes framed values in order. On a failure, values decoded before it
767 // are emitted first and the error fails the next pull.
768 const decodeAll = (
769 raw: ReadonlyArray<unknown>
770 ): Pull.Pull<Arr.NonEmptyReadonlyArray<S["Type"]>, Schema.SchemaError | IE, Done> => {
771 const out: Array<S["Type"]> = []
772 const loop: Effect.Effect<Arr.NonEmptyReadonlyArray<S["Type"]>, Schema.SchemaError> = Effect.suspend(() =>
773 out.length === raw.length
774 ? Effect.succeed(out as unknown as Arr.NonEmptyReadonlyArray<S["Type"]>)
775 : Effect.flatMap(decodeEncoded!(raw[out.length]), (value) => {
776 out.push(value as S["Type"])
777 return loop
778 })
779 )
780 return Effect.catch(loop, (error) => {
781 if (out.length === 0) {
782 stashed = undefined
783 return Effect.fail(error)
784 }
785 stashed = error
786 return Effect.succeed(out as unknown as Arr.NonEmptyReadonlyArray<S["Type"]>)
787 })
788 }
789
790 const feed = (
791 chunk: Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>
792 ): Pull.Pull<Arr.NonEmptyReadonlyArray<S["Type"]>, Schema.SchemaError | IE, Done> => {
793 const raw: Array<unknown> = []
794 try {

Callers 1

SchemaBinary.tsFile · 0.70

Calls 9

compileTargetFunction · 0.85
compileModeFunction · 0.85
isSuccessExitFunction · 0.85
endSyncMethod · 0.80
makeParserFunction · 0.70
failMethod · 0.65
succeedMethod · 0.45
syncMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected