const isDecodeError = (err: unknown) =>
isUnknownException(err) &&
err.cause instanceof TypeError &&
"code" in err.cause &&
err.cause.code === "ERR_ENCODING_INVALID_ENCODED_DATA";
// Use a decoder instead of Stream.decodeString because we want to
// ignore the file if it is not valid UTF-8.
const decoder = new TextDecoder("utf-8", { fatal: true });
const decodeFile = (filePath: string) =>
fs.stream(filePath).pipe(
Stream.mapEffect((bytes) =>
Effect.try(() => decoder.decode(bytes)),
),
Stream.mkString,
Effect.asSome,
Effect.catchIf(isDecodeError, () => Effect.succeedNone),
);
const isDecodeError = (err: unknown) =>
isUnknownException(err) &&
err.cause instanceof TypeError &&
"code" in err.cause &&
err.cause.code === "ERR_ENCODING_INVALID_ENCODED_DATA";
// Use a decoder instead of Stream.decodeString because we want to
// ignore the file if it is not valid UTF-8.
const decoder = new TextDecoder("utf-8", { fatal: true });
const decodeFile = (filePath: string) =>
fs.stream(filePath).pipe(
Stream.mapEffect((bytes) =>
Effect.try(() => decoder.decode(bytes)),
),
Stream.mkString,
Effect.asSome,
Effect.catchIf(isDecodeError, () => Effect.succeedNone),
);