|
| 1 | +#pragma once |
| 2 | +#include <string_view> |
| 3 | + |
| 4 | +namespace webscene_native { |
| 5 | +inline constexpr std::string_view fileReaderCompatibilityScript = R"JS( |
| 6 | +// SCENETECH_FILE_READER_COMPATIBILITY_V1 |
| 7 | +(() => { |
| 8 | + if (typeof globalThis.FileReader === 'function') return; |
| 9 | + if (typeof globalThis.Blob !== 'function' || typeof globalThis.Blob.prototype.arrayBuffer !== 'function') |
| 10 | + throw new Error('Native Blob.arrayBuffer is required for FileReader'); |
| 11 | + const schedule = callback => globalThis.setTimeout(callback, 0); |
| 12 | + const failure = (message, name) => new DOMException(message, name); |
| 13 | + const bytesToBinary = bytes => { |
| 14 | + let result = ''; |
| 15 | + for (let offset = 0; offset < bytes.length; offset += 4096) |
| 16 | + result += String.fromCharCode(...bytes.subarray(offset, offset + 4096)); |
| 17 | + return result; |
| 18 | + }; |
| 19 | + const base64 = bytes => { |
| 20 | + const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 21 | + let result = ''; |
| 22 | + for (let i = 0; i < bytes.length; i += 3) { |
| 23 | + const value = (bytes[i] << 16) | ((bytes[i + 1] || 0) << 8) | (bytes[i + 2] || 0); |
| 24 | + result += alphabet[(value >>> 18) & 63] + alphabet[(value >>> 12) & 63] |
| 25 | + + (i + 1 < bytes.length ? alphabet[(value >>> 6) & 63] : '=') |
| 26 | + + (i + 2 < bytes.length ? alphabet[value & 63] : '='); |
| 27 | + } |
| 28 | + return result; |
| 29 | + }; |
| 30 | + class NativeBlobFileReader extends EventTarget { |
| 31 | + constructor() { |
| 32 | + super(); |
| 33 | + this._state = 0; this._result = null; this._error = null; |
| 34 | + this._operation = 0; this._handlers = new Map(); |
| 35 | + this.onloadstart = this.onprogress = this.onload = this.onerror = this.onabort = this.onloadend = null; |
| 36 | + } |
| 37 | + get readyState() { return this._state; } |
| 38 | + get result() { return this._result; } |
| 39 | + get error() { return this._error; } |
| 40 | + // The SDK EventTarget also invokes ontype properties. Own dispatch avoids |
| 41 | + // depending on that extension or delivering FileReader.onload twice. |
| 42 | + addEventListener(type, listener, options = {}) { |
| 43 | + if (typeof listener !== 'function' && typeof listener?.handleEvent !== 'function') return; |
| 44 | + type = String(type); |
| 45 | + const capture = typeof options === 'boolean' ? options : Boolean(options?.capture); |
| 46 | + if (options?.signal?.aborted) return; |
| 47 | + const entries = this._handlers.get(type) || []; |
| 48 | + if (entries.some(entry => entry.listener === listener && entry.capture === capture)) return; |
| 49 | + const entry = { listener, capture, once: Boolean(options?.once), removeAbort: null }; |
| 50 | + entries.push(entry); this._handlers.set(type, entries); |
| 51 | + if (options?.signal?.addEventListener) { |
| 52 | + const remove = () => this.removeEventListener(type, listener, capture); |
| 53 | + options.signal.addEventListener('abort', remove, { once: true }); |
| 54 | + entry.removeAbort = () => options.signal.removeEventListener('abort', remove); |
| 55 | + } |
| 56 | + } |
| 57 | + removeEventListener(type, listener, options = {}) { |
| 58 | + const capture = typeof options === 'boolean' ? options : Boolean(options?.capture); |
| 59 | + const entries = this._handlers.get(String(type)); |
| 60 | + const index = entries?.findIndex(entry => entry.listener === listener && entry.capture === capture) ?? -1; |
| 61 | + if (index >= 0) { entries[index].removeAbort?.(); entries.splice(index, 1); } |
| 62 | + } |
| 63 | + dispatchEvent(event) { |
| 64 | + if (!event || !event.type) throw new TypeError('dispatchEvent requires an Event'); |
| 65 | + Object.defineProperties(event, { |
| 66 | + target: { value: this, configurable: true }, currentTarget: { value: this, configurable: true } |
| 67 | + }); |
| 68 | + const invoke = listener => { |
| 69 | + try { |
| 70 | + if (typeof listener === 'function') listener.call(this, event); |
| 71 | + else listener?.handleEvent(event); |
| 72 | + } catch (error) { schedule(() => { throw error; }); } |
| 73 | + }; |
| 74 | + invoke(this['on' + event.type]); |
| 75 | + for (const entry of [...(this._handlers.get(event.type) || [])]) { |
| 76 | + if (!this._handlers.get(event.type)?.includes(entry)) continue; |
| 77 | + if (entry.once) this.removeEventListener(event.type, entry.listener, entry.capture); |
| 78 | + invoke(entry.listener); |
| 79 | + } |
| 80 | + Object.defineProperty(event, 'currentTarget', { value: null, configurable: true }); |
| 81 | + return !event.defaultPrevented; |
| 82 | + } |
| 83 | + _emit(type, loaded, total) { |
| 84 | + const event = new Event(type); |
| 85 | + Object.defineProperties(event, { |
| 86 | + lengthComputable: { value: true }, loaded: { value: loaded }, total: { value: total } |
| 87 | + }); |
| 88 | + this.dispatchEvent(event); |
| 89 | + } |
| 90 | + _read(blob, format, encoding) { |
| 91 | + if (!(blob instanceof Blob)) throw new TypeError('FileReader requires a Blob'); |
| 92 | + if (this._state === 1) throw failure('A read is already in progress', 'InvalidStateError'); |
| 93 | + const operation = ++this._operation; |
| 94 | + this._state = 1; this._result = null; this._error = null; |
| 95 | + const active = () => operation === this._operation && this._state === 1; |
| 96 | + schedule(() => { |
| 97 | + if (!active()) return; |
| 98 | + this._emit('loadstart', 0, blob.size); |
| 99 | + if (!active()) return; |
| 100 | + Promise.resolve().then(() => blob.arrayBuffer()).then(buffer => { |
| 101 | + const bytes = new Uint8Array(buffer); |
| 102 | + let result = buffer; |
| 103 | + if (format === 'text') { |
| 104 | + let decoder; |
| 105 | + const charset = /(?:^|;)\s*charset\s*=\s*["']?([^;"'\s]+)/i.exec(blob.type); |
| 106 | + try { decoder = new TextDecoder(encoding || charset?.[1] || 'utf-8'); } |
| 107 | + catch { decoder = new TextDecoder('utf-8'); } |
| 108 | + result = decoder.decode(bytes); |
| 109 | + } else if (format === 'data') result = 'data:' + (blob.type || 'application/octet-stream') + ';base64,' + base64(bytes); |
| 110 | + else if (format === 'binary') result = bytesToBinary(bytes); |
| 111 | + schedule(() => { |
| 112 | + if (!active()) return; |
| 113 | + this._emit('progress', bytes.byteLength, blob.size); |
| 114 | + if (!active()) return; |
| 115 | + this._result = result; this._state = 2; |
| 116 | + this._emit('load', bytes.byteLength, blob.size); |
| 117 | + if (operation === this._operation && this._state === 2) this._emit('loadend', bytes.byteLength, blob.size); |
| 118 | + }); |
| 119 | + }).catch(error => schedule(() => { |
| 120 | + if (!active()) return; |
| 121 | + this._error = error instanceof DOMException ? error : failure(String(error?.message || error), 'NotReadableError'); |
| 122 | + this._state = 2; this._result = null; |
| 123 | + this._emit('error', 0, blob.size); |
| 124 | + if (operation === this._operation && this._state === 2) this._emit('loadend', 0, blob.size); |
| 125 | + })); |
| 126 | + }); |
| 127 | + } |
| 128 | + readAsArrayBuffer(blob) { this._read(blob, 'array'); } |
| 129 | + readAsText(blob, encoding) { this._read(blob, 'text', encoding); } |
| 130 | + readAsDataURL(blob) { this._read(blob, 'data'); } |
| 131 | + readAsBinaryString(blob) { this._read(blob, 'binary'); } |
| 132 | + abort() { |
| 133 | + this._result = null; |
| 134 | + if (this._state !== 1) return; |
| 135 | + const operation = ++this._operation; |
| 136 | + this._state = 2; this._error = null; |
| 137 | + this._emit('abort', 0, 0); |
| 138 | + if (operation === this._operation && this._state === 2) this._emit('loadend', 0, 0); |
| 139 | + } |
| 140 | + } |
| 141 | + for (const [name, value] of Object.entries({ EMPTY: 0, LOADING: 1, DONE: 2 })) { |
| 142 | + Object.defineProperty(NativeBlobFileReader, name, { value, enumerable: true }); |
| 143 | + Object.defineProperty(NativeBlobFileReader.prototype, name, { value, enumerable: true }); |
| 144 | + } |
| 145 | + Object.defineProperty(NativeBlobFileReader.prototype, Symbol.toStringTag, { value: 'FileReader' }); |
| 146 | + Object.defineProperty(globalThis, 'FileReader', { value: NativeBlobFileReader, configurable: true, writable: true }); |
| 147 | +})(); |
| 148 | +)JS"; |
| 149 | +} |
0 commit comments