input.ts 673 B

123456789101112131415161718192021222324
  1. import { Effect } from "effect"
  2. const inputDecoder = new TextDecoder("utf-8", { fatal: true })
  3. export function handlePtyInput(
  4. handler: { onMessage: (message: string | ArrayBuffer) => void },
  5. message: string | Uint8Array,
  6. ) {
  7. if (typeof message === "string") {
  8. handler.onMessage(message)
  9. return Effect.void
  10. }
  11. return Effect.try({
  12. try: () => inputDecoder.decode(message),
  13. catch: () => new Error("invalid PTY websocket input"),
  14. }).pipe(
  15. Effect.catch(() => Effect.succeed(undefined)),
  16. Effect.flatMap((decoded) => {
  17. if (decoded === undefined) return Effect.void
  18. handler.onMessage(decoded)
  19. return Effect.void
  20. }),
  21. )
  22. }