(
readable: LazyArg<Readable | NodeJS.ReadableStream>,
options?: {
readonly onError?: (error: unknown) => E
readonly encoding?: BufferEncoding | undefined
readonly maxBytes?: SizeInput | undefined
}
)
| 215 | * @since 4.0.0 |
| 216 | */ |
| 217 | export 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 | * |
nothing calls this directly
no test coverage detected
searching dependent graphs…