vomebook commited on
Commit ·
58fdd73
1
Parent(s): d888607
Keep reader retries on the proxied URL.
Browse filesA failed PDF, image, or media load used to fall back to the dataset host, which does not connect from networks that can reach the Space proxy.
- static/reader/core/reader-document-loader.mjs +4 -7
- static/reader/formats/reader-media-format.mjs +7 -2
- static/reader/pdf/reader-pdf-engine.mjs +3 -6
- tests/gates/test_static_contract.py +2 -3
- tests/reader/media/test_reader_media_metadata.mjs +18 -2
- tests/reader/transport/test_reader_document_loader_contract.mjs +8 -8
static/reader/core/reader-document-loader.mjs
CHANGED
|
@@ -159,13 +159,10 @@ export function createReaderDocumentLoader(readerContext) {
|
|
| 159 |
image.className = "reader-image";
|
| 160 |
image.alt = readerContext.documentState.title;
|
| 161 |
image.decoding = "async";
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
if (/** @type {ReaderLoaderError | null | undefined} */ (error)?.code === "READER_RESOURCE_LIMIT") throw error;
|
| 167 |
-
return readerContext.imageLoader.load(image, target, readerContext.readerAbortController.signal, deadline);
|
| 168 |
-
}
|
| 169 |
}
|
| 170 |
|
| 171 |
function loadChapterManifestDocument() {
|
|
|
|
| 159 |
image.className = "reader-image";
|
| 160 |
image.alt = readerContext.documentState.title;
|
| 161 |
image.decoding = "async";
|
| 162 |
+
readerContext.assertReaderActive();
|
| 163 |
+
// Do not fall through to the dataset URL. huggingface.co does not open on
|
| 164 |
+
// this network, so a failed proxy must stay on the proxied URL.
|
| 165 |
+
return readerContext.imageLoader.load(image, readerContext.readerContentUrl(target), readerContext.readerAbortController.signal, deadline);
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
|
| 168 |
function loadChapterManifestDocument() {
|
static/reader/formats/reader-media-format.mjs
CHANGED
|
@@ -98,9 +98,14 @@ export function createReaderMediaFormat(readerContext) {
|
|
| 98 |
// The runtime capability helper stringifies unknown metadata; its ambient
|
| 99 |
// declaration is narrower. This assertion preserves the original argument.
|
| 100 |
const originalMode = VoiceOfMLReader.capability(/** @type {string} */ (readerContext.resolvedReaderData?.original_extension || readerContext.extension)).mode;
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
const retryOriginal = () => {
|
| 103 |
-
if (sourceRetried ||
|
|
|
|
| 104 |
readerContext.saveProgress();
|
| 105 |
retryPosition = readerContext.documentState.restorationReady
|
| 106 |
? {time:media.currentTime, playing:!media.paused, rate:media.playbackRate} : null;
|
|
|
|
| 98 |
// The runtime capability helper stringifies unknown metadata; its ambient
|
| 99 |
// declaration is narrower. This assertion preserves the original argument.
|
| 100 |
const originalMode = VoiceOfMLReader.capability(/** @type {string} */ (readerContext.resolvedReaderData?.original_extension || readerContext.extension)).mode;
|
| 101 |
+
// A stalled proxy used to assign the dataset URL to the media element. That
|
| 102 |
+
// URL is on huggingface.co, which this network cannot open, so the retry
|
| 103 |
+
// never reaches metadata. Reload the same proxied URL instead.
|
| 104 |
+
const directSource = originalMode === mode && readerContext.validSource(readerContext.downloadUrl) ? readerContext.downloadUrl : readerContext.sourceUrl;
|
| 105 |
+
const retryUrl = readerContext.contentUrl && readerContext.contentUrl !== directSource ? readerContext.contentUrl : directSource;
|
| 106 |
const retryOriginal = () => {
|
| 107 |
+
if (sourceRetried || !readerContext.validSource(retryUrl)) return false;
|
| 108 |
+
if (retryUrl === media.src && retryUrl === directSource) return false;
|
| 109 |
readerContext.saveProgress();
|
| 110 |
retryPosition = readerContext.documentState.restorationReady
|
| 111 |
? {time:media.currentTime, playing:!media.paused, rate:media.playbackRate} : null;
|
static/reader/pdf/reader-pdf-engine.mjs
CHANGED
|
@@ -85,12 +85,9 @@ export function createReaderPdfEngine(readerContext) {
|
|
| 85 |
|
| 86 |
/** @param {PdfModule} pdfjs @param {(url: string) => PdfOptions} options @param {string} originalUrl @param {string} proxyUrl @param {(url: string) => Initial} takeInitial */
|
| 87 |
function loadPdfWithTimeout(pdfjs, options, originalUrl = readerContext.sourceUrl, proxyUrl = readerContext.contentUrl, takeInitial = (_url) => null) {
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
if (readerContext.retryableReaderProxyError(error)) throw error;
|
| 92 |
-
return readerContext.loadPdfTaskWithTimeout(pdfjs, options, originalUrl);
|
| 93 |
-
});
|
| 94 |
}
|
| 95 |
|
| 96 |
function preloadPdfEngine() {
|
|
|
|
| 85 |
|
| 86 |
/** @param {PdfModule} pdfjs @param {(url: string) => PdfOptions} options @param {string} originalUrl @param {string} proxyUrl @param {(url: string) => Initial} takeInitial */
|
| 87 |
function loadPdfWithTimeout(pdfjs, options, originalUrl = readerContext.sourceUrl, proxyUrl = readerContext.contentUrl, takeInitial = (_url) => null) {
|
| 88 |
+
// Keep a failed proxy on the proxied URL. Falling through to originalUrl
|
| 89 |
+
// opens huggingface.co directly, which does not connect on this network.
|
| 90 |
+
return readerContext.retryReaderProxy(() => readerContext.loadPdfTaskWithTimeout(pdfjs, options, proxyUrl, takeInitial(proxyUrl)));
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
function preloadPdfEngine() {
|
tests/gates/test_static_contract.py
CHANGED
|
@@ -666,9 +666,8 @@ console.log('Chapter adapter contract passed; 5 negative controls rejected');
|
|
| 666 |
self.assertIn('method: readerId ? "GET" : "HEAD", cache: "no-store"', self.js)
|
| 667 |
reader = read_reader_source(ROOT)
|
| 668 |
image_loader = re.search(r'^async function loadImageDocument\([\s\S]*?^}', reader, re.M).group()
|
| 669 |
-
assert_source_contains(self, 'return
|
| 670 |
-
|
| 671 |
-
assert_source_contains(self, 'return imageLoader.load(image, target, readerAbortController.signal, deadline)', image_loader)
|
| 672 |
|
| 673 |
def test_image_pages_prioritize_visible_pages_without_document_image_race(self):
|
| 674 |
reader = read_reader_source(ROOT)
|
|
|
|
| 666 |
self.assertIn('method: readerId ? "GET" : "HEAD", cache: "no-store"', self.js)
|
| 667 |
reader = read_reader_source(ROOT)
|
| 668 |
image_loader = re.search(r'^async function loadImageDocument\([\s\S]*?^}', reader, re.M).group()
|
| 669 |
+
assert_source_contains(self, 'return imageLoader.load(image, readerContentUrl(target), readerAbortController.signal, deadline)', image_loader)
|
| 670 |
+
self.assertNotIn('return imageLoader.load(image, target, readerAbortController.signal, deadline)', image_loader)
|
|
|
|
| 671 |
|
| 672 |
def test_image_pages_prioritize_visible_pages_without_document_image_race(self):
|
| 673 |
reader = read_reader_source(ROOT)
|
tests/reader/media/test_reader_media_metadata.mjs
CHANGED
|
@@ -48,10 +48,26 @@ test('media readiness waits for metadata and reports player timing', async () =>
|
|
| 48 |
} finally {f.restore();}
|
| 49 |
});
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
test('media source retries only once and restores position on metadata', async () => {
|
| 52 |
const f=fixture();try {
|
| 53 |
const promise=f.context.renderMedia('audio');await tick();
|
| 54 |
-
f.media.dispatchEvent(new Event('error'));assert.equal(f.media.src,'https://test/
|
| 55 |
f.media.currentTime=0;f.media.readyState=1;f.media.dispatchEvent(new Event('loadedmetadata'));await promise;
|
| 56 |
assert.equal(f.media.currentTime,4);assert.equal(f.media._readerSourceRetry,false);
|
| 57 |
f.media.dispatchEvent(new Event('error'));assert.equal(f.media.loads,1);
|
|
@@ -92,7 +108,7 @@ test('idle deadline retries original once and rearms without changing hard deadl
|
|
| 92 |
const promise=f.context.renderMedia('audio');
|
| 93 |
const hard=[...callbacks].find(([,item])=>item.delay===20000)[0];
|
| 94 |
const idle=[...callbacks].find(([,item])=>item.delay===15000)[1];idle.callback();
|
| 95 |
-
assert.equal(f.media.src,'https://test/
|
| 96 |
assert.ok(callbacks.has(hard));assert.equal([...callbacks.values()].filter(item=>item.delay===20000).length,1);
|
| 97 |
f.media.readyState=1;f.media.dispatchEvent(new Event('loadedmetadata'));await promise;
|
| 98 |
assert.equal(callbacks.size,0);
|
|
|
|
| 48 |
} finally {f.restore();}
|
| 49 |
});
|
| 50 |
|
| 51 |
+
test('stalled proxy retries the proxy instead of the blocked dataset host', async () => {
|
| 52 |
+
const f=fixture();try {
|
| 53 |
+
f.context.sourceUrl=f.context.downloadUrl='https://huggingface.co/datasets/VoiceOfML/VOMEBOOK/resolve/main/audio.mp3';
|
| 54 |
+
f.context.contentUrl='https://voiceofml-search.hf.space/api/reader-content?url=audio';
|
| 55 |
+
const promise=f.context.renderMedia('audio');await tick();
|
| 56 |
+
f.media.dispatchEvent(new Event('error'));
|
| 57 |
+
assert.equal(f.media.src,f.context.contentUrl);
|
| 58 |
+
assert.equal(f.media.src.includes('huggingface.co'),false);
|
| 59 |
+
f.media.dispatchEvent(new Event('error'));
|
| 60 |
+
assert.equal(f.media.loads,1);
|
| 61 |
+
assert.ok(f.events.some(value=>Array.isArray(value)&&value[0]==='fail'&&value[2]==='READER_MEDIA'));
|
| 62 |
+
f.media.readyState=1;f.media.dispatchEvent(new Event('loadedmetadata'));
|
| 63 |
+
await assert.rejects(promise,{name:'AbortError'});
|
| 64 |
+
} finally {f.restore();}
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
test('media source retries only once and restores position on metadata', async () => {
|
| 68 |
const f=fixture();try {
|
| 69 |
const promise=f.context.renderMedia('audio');await tick();
|
| 70 |
+
f.media.dispatchEvent(new Event('error'));assert.equal(f.media.src,'https://test/proxy');assert.equal(f.media.loads,1);
|
| 71 |
f.media.currentTime=0;f.media.readyState=1;f.media.dispatchEvent(new Event('loadedmetadata'));await promise;
|
| 72 |
assert.equal(f.media.currentTime,4);assert.equal(f.media._readerSourceRetry,false);
|
| 73 |
f.media.dispatchEvent(new Event('error'));assert.equal(f.media.loads,1);
|
|
|
|
| 108 |
const promise=f.context.renderMedia('audio');
|
| 109 |
const hard=[...callbacks].find(([,item])=>item.delay===20000)[0];
|
| 110 |
const idle=[...callbacks].find(([,item])=>item.delay===15000)[1];idle.callback();
|
| 111 |
+
assert.equal(f.media.src,'https://test/proxy');assert.equal(f.media.loads,1);
|
| 112 |
assert.ok(callbacks.has(hard));assert.equal([...callbacks.values()].filter(item=>item.delay===20000).length,1);
|
| 113 |
f.media.readyState=1;f.media.dispatchEvent(new Event('loadedmetadata'));await promise;
|
| 114 |
assert.equal(callbacks.size,0);
|
tests/reader/transport/test_reader_document_loader_contract.mjs
CHANGED
|
@@ -171,24 +171,24 @@ test("script load releases timers, handlers and resources on success, error and
|
|
| 171 |
} finally {globalThis.document = oldDocument; globalThis.setTimeout = oldSet; globalThis.clearTimeout = oldClear;}
|
| 172 |
});
|
| 173 |
|
| 174 |
-
test("image
|
| 175 |
const oldImage = globalThis.Image;
|
| 176 |
globalThis.Image = class {};
|
| 177 |
try {
|
| 178 |
const h = fixture(), calls = [];
|
|
|
|
| 179 |
h.context.imageLoader = {load: async (image, url, signal, deadline) => {
|
| 180 |
calls.push({image, url, signal, deadline});
|
| 181 |
-
if (
|
| 182 |
return image;
|
| 183 |
}};
|
| 184 |
const image = await h.loader.loadImageDocument();
|
| 185 |
-
assert.equal(image.alt, "book"); assert.equal(calls.length,
|
| 186 |
-
assert.
|
| 187 |
-
|
| 188 |
-
h.context.imageLoader.load = async () => {
|
| 189 |
await assert.rejects(h.loader.loadImageDocument(), {code:"READER_RESOURCE_LIMIT"});
|
| 190 |
-
|
| 191 |
-
h.close(); h.context.imageLoader.load = async () => {throw new Error("failed");};
|
| 192 |
await assert.rejects(h.loader.loadImageDocument(), {name:"AbortError"});
|
| 193 |
} finally {globalThis.Image = oldImage;}
|
| 194 |
});
|
|
|
|
| 171 |
} finally {globalThis.document = oldDocument; globalThis.setTimeout = oldSet; globalThis.clearTimeout = oldClear;}
|
| 172 |
});
|
| 173 |
|
| 174 |
+
test("image load stays on the proxy when the dataset host is unreachable", {timeout: 10000}, async () => {
|
| 175 |
const oldImage = globalThis.Image;
|
| 176 |
globalThis.Image = class {};
|
| 177 |
try {
|
| 178 |
const h = fixture(), calls = [];
|
| 179 |
+
h.context.sourceUrl = "https://huggingface.co/datasets/VoiceOfML/VOMEBOOK/resolve/main/page.webp";
|
| 180 |
h.context.imageLoader = {load: async (image, url, signal, deadline) => {
|
| 181 |
calls.push({image, url, signal, deadline});
|
| 182 |
+
if (url === h.context.sourceUrl) throw new Error("direct dataset request");
|
| 183 |
return image;
|
| 184 |
}};
|
| 185 |
const image = await h.loader.loadImageDocument();
|
| 186 |
+
assert.equal(image.alt, "book"); assert.equal(calls.length, 1);
|
| 187 |
+
assert.notEqual(calls[0].url, h.context.sourceUrl);
|
| 188 |
+
assert.equal(calls[0].url, h.context.readerContentUrl(h.context.sourceUrl));
|
| 189 |
+
h.context.imageLoader.load = async () => {throw Object.assign(new Error("limit"), {code:"READER_RESOURCE_LIMIT"});};
|
| 190 |
await assert.rejects(h.loader.loadImageDocument(), {code:"READER_RESOURCE_LIMIT"});
|
| 191 |
+
h.close();
|
|
|
|
| 192 |
await assert.rejects(h.loader.loadImageDocument(), {name:"AbortError"});
|
| 193 |
} finally {globalThis.Image = oldImage;}
|
| 194 |
});
|