Skip to content
Open
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/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async function fetchBinaryData(url) {
`Failed to fetch file "${url}" with "${response.statusText}".`
);
}
return new Uint8Array(await response.arrayBuffer());
return response.bytes();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/display/cmap_reader_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
async _fetch(url) {
const data = await fetchData(
url,
/* type = */ this.isCompressed ? "arraybuffer" : "text"
/* type = */ this.isCompressed ? "bytes" : "text"
);
return data instanceof ArrayBuffer
? new Uint8Array(data)
: stringToBytes(data);
return data instanceof Uint8Array ? data : stringToBytes(data);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ async function fetchData(url, type = "text") {
throw new Error(response.statusText);
}
switch (type) {
case "arraybuffer":
return response.arrayBuffer();
case "blob":
return response.blob();
case "bytes":
return response.bytes();
case "json":
return response.json();
}
Expand All @@ -58,15 +58,17 @@ async function fetchData(url, type = "text") {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open("GET", url, /* async = */ true);
request.responseType = type;
request.responseType = type === "bytes" ? "arraybuffer" : type;

request.onreadystatechange = () => {
if (request.readyState !== XMLHttpRequest.DONE) {
return;
}
if (request.status === 200 || request.status === 0) {
switch (type) {
case "arraybuffer":
case "bytes":
resolve(new Uint8Array(request.response));
return;
case "blob":
case "json":
resolve(request.response);
Expand Down
3 changes: 1 addition & 2 deletions src/display/standard_fontdata_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
* @ignore
*/
async _fetch(url) {
const data = await fetchData(url, /* type = */ "arraybuffer");
return new Uint8Array(data);
return fetchData(url, /* type = */ "bytes");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/display/wasm_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class DOMWasmFactory extends BaseWasmFactory {
* @ignore
*/
async _fetch(url) {
const data = await fetchData(url, /* type = */ "arraybuffer");
return new Uint8Array(data);
return fetchData(url, /* type = */ "bytes");
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,18 @@ if (
};
}

// See https://developer.mozilla.org/en-US/docs/Web/API/Response/bytes#browser_compatibility
if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("SKIP_BABEL") &&
typeof Response.prototype.bytes !== "function"
) {
Response.prototype.bytes = async function () {
return new Uint8Array(await this.arrayBuffer());
};
}

// TODO: Remove this once Safari 17.4 is the lowest supported version.
if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("SKIP_BABEL") &&
Expand Down
3 changes: 1 addition & 2 deletions test/unit/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class DefaultFileReaderFactory {
if (isNodeJS) {
return fetchDataNode(params.path);
}
const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer");
return new Uint8Array(data);
return fetchDataDOM(params.path, /* type = */ "bytes");
}
}

Expand Down