MCPcopy
hub / github.com/Effect-TS/effect / toUint8Array

Function toUint8Array

packages/platform-node-shared/src/internal/stream.ts:73–108  ·  view source on GitHub ↗
(
  readable: LazyArg<Readable | NodeJS.ReadableStream>,
  options: {
    readonly onFailure: (error: unknown) => E
    readonly maxBytes?: SizeInput | undefined
  }
)

Source from the content-addressed store, hash-verified

71
72/** @internal */
73export const toUint8Array = <E>(
74 readable: LazyArg<Readable | NodeJS.ReadableStream>,
75 options: {
76 readonly onFailure: (error: unknown) => E
77 readonly maxBytes?: SizeInput | undefined
78 }
79): Effect.Effect<Uint8Array, E> => {
80 const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined
81 return Effect.acquireUseRelease(
82 Effect.sync(readable),
83 (stream) =>
84 Effect.async((resume) => {
85 let buffer = Buffer.alloc(0)
86 let bytes = 0
87 stream.once("error", (err) => {
88 resume(Effect.fail(options.onFailure(err)))
89 })
90 stream.once("end", () => {
91 resume(Effect.succeed(buffer))
92 })
93 stream.on("data", (chunk) => {
94 buffer = Buffer.concat([buffer, chunk])
95 bytes += chunk.length
96 if (maxBytesNumber && bytes > maxBytesNumber) {
97 resume(Effect.fail(options.onFailure(new Error("maxBytes exceeded"))))
98 }
99 })
100 }),
101 (stream) =>
102 Effect.sync(() => {
103 if ("closed" in stream && !stream.closed) {
104 stream.destroy()
105 }
106 })
107 )
108}
109
110/** @internal */
111export const fromDuplex = <IE, E, I = string | Uint8Array<ArrayBufferLike>, O = Uint8Array<ArrayBufferLike>>(

Callers

nothing calls this directly

Calls 5

NumberInterface · 0.85
syncMethod · 0.80
destroyMethod · 0.80
failMethod · 0.65
resumeFunction · 0.50

Tested by

no test coverage detected