From c867c1aa476b3f00933be08cbc5f528973e37857 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 8 Apr 2021 15:05:08 +0200 Subject: fix: enable FileReader wpt and align to spec (#10063) This adds some algorithms from the whatwg mimesniff, whatwg infra, and whatwg encoding specs that FileReader needs to use internally. --- op_crates/web/08_text_encoding.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'op_crates/web/08_text_encoding.js') diff --git a/op_crates/web/08_text_encoding.js b/op_crates/web/08_text_encoding.js index 73cb38311..1fda1a816 100644 --- a/op_crates/web/08_text_encoding.js +++ b/op_crates/web/08_text_encoding.js @@ -4545,10 +4545,38 @@ fromByteArray, }; + /** + * @param {Uint8Array} bytes + */ + function decode(bytes, encoding) { + const BOMEncoding = BOMSniff(bytes); + let start = 0; + if (BOMEncoding !== null) { + encoding = BOMEncoding; + if (BOMEncoding === "UTF-8") start = 3; + else start = 2; + } + return new TextDecoder(encoding).decode(bytes.slice(start)); + } + + /** + * @param {Uint8Array} bytes + */ + function BOMSniff(bytes) { + const BOM = bytes.subarray(0, 3); + if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) { + return "UTF-8"; + } + if (BOM[0] === 0xFE && BOM[1] === 0xFF) return "UTF-16BE"; + if (BOM[0] === 0xFF && BOM[1] === 0xFE) return "UTF-16LE"; + return null; + } + window.TextEncoder = TextEncoder; window.TextDecoder = TextDecoder; window.atob = atob; window.btoa = btoa; window.__bootstrap = window.__bootstrap || {}; + window.__bootstrap.encoding = { decode }; window.__bootstrap.base64 = base64; })(this); -- cgit v1.2.3