Skip to content

Re-factor how the GenericL10n class fetches localization-data #17277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ class PartialEvaluator {
`fetchStandardFontData: failed to fetch file "${url}" with "${response.statusText}".`
);
} else {
data = await response.arrayBuffer();
data = new Uint8Array(await response.arrayBuffer());
}
} else {
// Get the data on the main-thread instead.
Expand Down
48 changes: 33 additions & 15 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class DOMCanvasFactory extends BaseCanvasFactory {
}
}

async function fetchData(url, asTypedArray = false) {
async function fetchData(url, type = "text") {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
isValidFetchUrl(url, document.baseURI)
Expand All @@ -396,29 +396,35 @@ async function fetchData(url, asTypedArray = false) {
if (!response.ok) {
throw new Error(response.statusText);
}
return asTypedArray
? new Uint8Array(await response.arrayBuffer())
: stringToBytes(await response.text());
switch (type) {
case "arraybuffer":
return response.arrayBuffer();
case "json":
return response.json();
}
return response.text();
}

// The Fetch API is not supported.
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open("GET", url, /* asTypedArray = */ true);
request.open("GET", url, /* async = */ true);
request.responseType = type;

if (asTypedArray) {
request.responseType = "arraybuffer";
}
request.onreadystatechange = () => {
if (request.readyState !== XMLHttpRequest.DONE) {
return;
}
if (request.status === 200 || request.status === 0) {
let data;
if (asTypedArray && request.response) {
data = new Uint8Array(request.response);
} else if (!asTypedArray && request.responseText) {
data = stringToBytes(request.responseText);
switch (type) {
case "arraybuffer":
case "json":
data = request.response;
break;
default:
data = request.responseText;
break;
}
if (data) {
resolve(data);
Expand All @@ -437,8 +443,17 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
* @ignore
*/
_fetchData(url, compressionType) {
return fetchData(url, /* asTypedArray = */ this.isCompressed).then(data => {
return { cMapData: data, compressionType };
return fetchData(
url,
/* type = */ this.isCompressed ? "arraybuffer" : "text"
).then(data => {
return {
cMapData:
data instanceof ArrayBuffer
? new Uint8Array(data)
: stringToBytes(data),
compressionType,
};
});
}
}
Expand All @@ -448,7 +463,9 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
* @ignore
*/
_fetchData(url) {
return fetchData(url, /* asTypedArray = */ true);
return fetchData(url, /* type = */ "arraybuffer").then(data => {
return new Uint8Array(data);
});
}
}

Expand Down Expand Up @@ -993,6 +1010,7 @@ export {
DOMFilterFactory,
DOMStandardFontDataFactory,
DOMSVGFactory,
fetchData,
getColorValues,
getCurrentTransform,
getCurrentTransformInverse,
Expand Down
2 changes: 2 additions & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
} from "./display/api.js";
import {
DOMSVGFactory,
fetchData,
getFilenameFromUrl,
getPdfFilenameFromUrl,
getXfaPageViewport,
Expand Down Expand Up @@ -92,6 +93,7 @@ export {
createValidAbsoluteUrl,
DOMSVGFactory,
FeatureTest,
fetchData,
getDocument,
getFilenameFromUrl,
getPdfFilenameFromUrl,
Expand Down
2 changes: 2 additions & 0 deletions test/unit/pdf_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from "../../src/display/api.js";
import {
DOMSVGFactory,
fetchData,
getFilenameFromUrl,
getPdfFilenameFromUrl,
getXfaPageViewport,
Expand Down Expand Up @@ -78,6 +79,7 @@ const expectedAPI = Object.freeze({
createValidAbsoluteUrl,
DOMSVGFactory,
FeatureTest,
fetchData,
getDocument,
getFilenameFromUrl,
getPdfFilenameFromUrl,
Expand Down
9 changes: 5 additions & 4 deletions web/genericl10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { FluentBundle, FluentResource } from "fluent-bundle";
import { DOMLocalization } from "fluent-dom";
import { fetchData } from "pdfjs-lib";
import { L10n } from "./l10n.js";

/**
Expand Down Expand Up @@ -71,8 +72,8 @@ class GenericL10n extends L10n {
return null;
}
const url = new URL(path, baseURL);
const data = await fetch(url);
const text = await data.text();
const text = await fetchData(url, /* type = */ "text");

const resource = new FluentResource(text);
const bundle = new FluentBundle(lang);
const errors = bundle.addResource(resource);
Expand All @@ -84,8 +85,8 @@ class GenericL10n extends L10n {

static async #getPaths() {
const { href } = document.querySelector(`link[type="application/l10n"]`);
const data = await fetch(href);
const paths = await data.json();
const paths = await fetchData(href, /* type = */ "json");

return { baseURL: href.replace(/[^/]*$/, "") || "./", paths };
}
}
Expand Down
2 changes: 2 additions & 0 deletions web/pdfjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
createValidAbsoluteUrl,
DOMSVGFactory,
FeatureTest,
fetchData,
getDocument,
getFilenameFromUrl,
getPdfFilenameFromUrl,
Expand Down Expand Up @@ -80,6 +81,7 @@ export {
createValidAbsoluteUrl,
DOMSVGFactory,
FeatureTest,
fetchData,
getDocument,
getFilenameFromUrl,
getPdfFilenameFromUrl,
Expand Down