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

Function encode

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

Source from the content-addressed store, hash-verified

677 * @since 4.0.0
678 */
679export const encode = <S extends Schema.Constraint>(
680 schema: S,
681 options?: SchemaAST.ParseOptions & Options & { readonly maxFrameSize?: number | undefined }
682) =>
683<IE = never, Done = unknown>(): Channel.Channel<
684 Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>,
685 Schema.SchemaError | IE,
686 Done,
687 Arr.NonEmptyReadonlyArray<S["Type"]>,
688 IE,
689 Done,
690 S["EncodingServices"]
691> => {
692 // Everything here depends only on schema and options, so it is derived once
693 // per channel rather than once per run.
694 const { exact, layout, target } = compileTarget(schema)
695 const parseOptions: SchemaAST.ParseOptions = options ?? SchemaAST.defaultParseOptions
696 const mode = compileMode(layout, options?.fingerprint)
697 const write = (
698 values: ReadonlyArray<unknown>
699 ): Effect.Effect<Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>, Schema.SchemaError> => {
700 try {
701 return Effect.succeed(Arr.of(encodeFrames(layout, values, parseOptions, mode)))
702 } catch (e) {
703 if (e instanceof IssueError) return Effect.fail(new Schema.SchemaError(e.issue))
704 return Effect.die(e)
705 }
706 }
707 if (exact && parseOptions.onExcessProperty !== "error") {
708 return Channel.fromTransform((upstream, _scope) => Effect.sync(() => Effect.flatMap(upstream, write)))
709 }
710 // One schema pass per chunk brings the values to the binary-adjusted encoded
711 // side the writer expects, so the writer pass stays one sync batch.
712 const encodeValues = Schema.encodeUnknownEffect(
713 Schema.NonEmptyArray(target) as unknown as Schema.ConstraintEncoder<unknown, never>,
714 options
715 ) as (values: ReadonlyArray<unknown>) => Effect.Effect<ReadonlyArray<unknown>, Schema.SchemaError>
716 return Channel.fromTransform((upstream, _scope) =>
717 Effect.sync(() => Effect.flatMap(upstream, (chunk) => Effect.flatMap(encodeValues(chunk), write)))
718 )
719}
720
721/**
722 * Creates a channel that decodes chunks of binary frames into schema values.

Callers 1

SchemaBinary.tsFile · 0.70

Calls 3

compileTargetFunction · 0.85
compileModeFunction · 0.85
syncMethod · 0.45

Tested by

no test coverage detected