Hex-escape (\uHHHH) those UCS-2 characters that cannot be encoded as UTF-8, and the replacement character, to help you distinguish verbatim occurrences from freshly discarded data.
Generate JS(ON) code that re-creates a JS value (object, string, Date, …) as verbatim as possible on the other side of a UTF-8 stream.