MCPcopy Create free account
hub / github.com/effect-app/libs / toString

Function toString

repos/effect/packages/platform-node-shared/src/NodeStream.ts:217–260  ·  view source on GitHub ↗
(
  readable: LazyArg<Readable | NodeJS.ReadableStream>,
  options?: {
    readonly onError?: (error: unknown) => E
    readonly encoding?: BufferEncoding | undefined
    readonly maxBytes?: SizeInput | undefined
  }
)

Source from the content-addressed store, hash-verified

215 * @since 4.0.0
216 */
217export const toString = <E = Cause.UnknownError>(
218 readable: LazyArg<Readable | NodeJS.ReadableStream>,
219 options?: {
220 readonly onError?: (error: unknown) => E
221 readonly encoding?: BufferEncoding | undefined
222 readonly maxBytes?: SizeInput | undefined
223 }
224): Effect.Effect<string, E> => {
225 const maxBytesNumber = options?.maxBytes ? Number(options.maxBytes) : undefined
226 const onError = options?.onError ?? defaultOnError
227 const encoding = options?.encoding ?? "utf8"
228 return Effect.callback((resume) => {
229 const stream = readable() as Readable
230 stream.setEncoding(encoding)
231
232 stream.once("error", (err) => {
233 if ("closed" in stream && !stream.closed) {
234 stream.destroy()
235 }
236 resume(Effect.fail(onError(err) as E))
237 })
238
239 let string = ""
240 let bytes = 0
241 stream.once("end", () => {
242 resume(Effect.succeed(string))
243 })
244 stream.on("data", (chunk) => {
245 string += chunk
246 bytes += Buffer.byteLength(chunk)
247 if (maxBytesNumber && bytes > maxBytesNumber) {
248 resume(Effect.fail(onError(new Error("maxBytes exceeded")) as E))
249 }
250 })
251 return Effect.sync(() => {
252 if ("closed" in stream && !stream.closed) {
253 stream.destroy()
254 }
255 })
256 })
257}
258
259/**
260 * Consumes a Node readable stream into an `ArrayBuffer`, failing through
261 * `onError` on stream errors or when `maxBytes` is exceeded and destroying the
262 * stream on interruption or failure.
263 *

Callers

nothing calls this directly

Calls 8

onceMethod · 0.80
syncMethod · 0.80
onErrorFunction · 0.70
NumberInterface · 0.50
resumeFunction · 0.50
failMethod · 0.45
succeedMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…