fix: polyfill readable stream async iteration for Safari PDF extraction (#25473)
This commit is contained in:
1 parent
886c132a10
commit
974b13839e
1 file changed
+35
@@ -1764,7 +1764,42 @@ export const parseJsonValue = (value: string): any => {
|
|||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type ReadableStreamWithAsyncIterator<T> = ReadableStream<T> & {
|
||||||
|
[Symbol.asyncIterator]?: () => AsyncIterableIterator<T>;
|
||||||
|
};
|
||||||
|
|
||||||
|
function ensureReadableStreamAsyncIterator() {
|
||||||
|
if (typeof ReadableStream === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prototype = ReadableStream.prototype as ReadableStreamWithAsyncIterator<unknown>;
|
||||||
|
if (prototype[Symbol.asyncIterator]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(prototype, Symbol.asyncIterator, {
|
||||||
|
value: async function* (this: ReadableStream<unknown>) {
|
||||||
|
const reader = this.getReader();
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
yield value;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
configurable: true,
|
||||||
|
writable: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function ensurePDFjsLoaded() {
|
async function ensurePDFjsLoaded() {
|
||||||
|
ensureReadableStreamAsyncIterator();
|
||||||
if (!window.pdfjsLib) {
|
if (!window.pdfjsLib) {
|
||||||
const pdfjs = await import('pdfjs-dist');
|
const pdfjs = await import('pdfjs-dist');
|
||||||
pdfjs.GlobalWorkerOptions.workerSrc = pdfWorkerUrl;
|
pdfjs.GlobalWorkerOptions.workerSrc = pdfWorkerUrl;
|
||||||
|
|||||||
Reference in new issue
Block a user