Hyperlinkv0.8.0-beta.28

Stream

Stream.mkUint8Arrayconsteffect/Stream.ts:11090
<E, R>(self: Stream<Uint8Array, E, R>): Effect.Effect<Uint8Array, E, R>

Concatenates the stream's Uint8Array chunks into a single Uint8Array.

Example (Joining Uint8Array chunks)

import { Console, Effect, Stream } from "effect"

const stream = Stream.make(new Uint8Array([1, 2]), new Uint8Array([3, 4]))
const program = Effect.gen(function*() {
  const bytes = yield* Stream.mkUint8Array(stream)
  yield* Console.log([...bytes])
})

Effect.runPromise(program)
// [1, 2, 3, 4]
destructors
Source effect/Stream.ts:1109030 lines
export const mkUint8Array = <E, R>(self: Stream<Uint8Array, E, R>): Effect.Effect<Uint8Array, E, R> =>
  Effect.map(
    Channel.runFold(
      self.channel,
      (): {
        bytes: number
        readonly arrays: Array<Uint8Array>
      } => ({
        bytes: 0,
        arrays: []
      }),
      (acc, chunk) => {
        for (let i = 0; i < chunk.length; i++) {
          acc.bytes += chunk[i].length
          acc.arrays.push(chunk[i])
        }
        return acc
      }
    ),
    ({ arrays, bytes }) => {
      const result = new Uint8Array(bytes)
      let offset = 0
      for (let i = 0; i < arrays.length; i++) {
        const array = arrays[i]
        result.set(array, offset)
        offset += array.length
      }
      return result
    }
  )