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

Function decode

packages/effect/src/unstable/encoding/Msgpack.ts:127–188  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

125 * @since 4.0.0
126 */
127export const decode = <IE = never, Done = unknown>(): Channel.Channel<
128 Arr.NonEmptyReadonlyArray<unknown>,
129 IE | MsgPackError,
130 Done,
131 Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>,
132 IE,
133 Done
134> =>
135 Channel.fromTransform((upstream, _scope) =>
136 Effect.sync(() => {
137 const unpackr = new Unpackr()
138 let incomplete: {
139 readonly bytes: Uint8Array<ArrayBuffer>
140 readonly cause: unknown
141 } | undefined = undefined
142
143 const pull = Effect.suspend((): Pull.Pull<Arr.NonEmptyReadonlyArray<unknown>, IE | MsgPackError, Done> =>
144 Pull.matchEffect(upstream, {
145 onSuccess: loop,
146 onFailure: Effect.failCause,
147 onDone: (done): Pull.Pull<never, MsgPackError, Done> =>
148 incomplete === undefined
149 ? Cause.done(done)
150 : Effect.fail(new MsgPackError({ kind: "Unpack", cause: incomplete.cause }))
151 })
152 )
153
154 function loop(chunk: Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>): Pull.Pull<
155 Arr.NonEmptyReadonlyArray<unknown>,
156 IE | MsgPackError,
157 Done
158 > {
159 const out = Arr.empty<unknown>()
160 for (let i = 0; i < chunk.length; i++) {
161 let buf = chunk[i]
162 if (incomplete !== undefined) {
163 const prev = buf
164 buf = new Uint8Array(incomplete.bytes.length + buf.length)
165 buf.set(incomplete.bytes)
166 buf.set(prev, incomplete.bytes.length)
167 incomplete = undefined
168 }
169 try {
170 out.push(...unpackr.unpackMultiple(buf))
171 } catch (cause) {
172 const error: any = cause
173 if (error.incomplete) {
174 incomplete = { bytes: buf.subarray(error.lastPosition), cause }
175 if (error.values) {
176 out.push(...error.values)
177 }
178 } else {
179 return Effect.fail(new MsgPackError({ kind: "Unpack", cause }))
180 }
181 }
182 }
183 return Arr.isReadonlyArrayNonEmpty(out) ? Effect.succeed(out) : pull
184 }

Callers 2

decodeSchemaFunction · 0.70
duplexFunction · 0.70

Calls 4

syncMethod · 0.45
doneMethod · 0.45
failMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected