Effect CommunityEC
Effect Community16mo ago
6 replies
Jonathan Clem

Enhancing `Stream.decodeText` with `{ fatal: true }` Option for Better UTF-8 Validation

It would be really useful for Stream.decodeText to accept the { fatal: true } option from TextDecoder. Right now, I have to do this to filter out non-UTF-8 files:

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),
  );
Was this page helpful?