Make bufToStr in polyfill strict, and document difference with native implementation (#5285)

This commit is contained in:
Amaury Chamayou 2023-05-19 14:21:47 +01:00 коммит произвёл GitHub
Родитель 13eac37a95
Коммит 682d7fafd5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 6 добавлений и 1 удалений

Просмотреть файл

@ -603,6 +603,9 @@ export interface CCF {
/**
* Convert an ArrayBuffer into a string.
*
* Note that this function does not perform any encoding validation, and may produce
* an invalid JS string if the input is not valid UTF-8.
*/
bufToStr(v: ArrayBuffer): string;

Просмотреть файл

@ -489,8 +489,10 @@ class CCFPolyfill implements CCF {
return typedArrToArrBuf(new TextEncoder().encode(s));
}
// Note: this is stricter than CCF's bufToStr, as it will
// reject buffers that are not valid UTF-8.
bufToStr(v: ArrayBuffer): string {
return new TextDecoder().decode(v);
return new TextDecoder("utf-8", { fatal: true }).decode(v);
}
jsonCompatibleToBuf<T extends JsonCompatible<T>>(v: T): ArrayBuffer {