Skip to content

Commit 8ba83e7

Browse files
committed
Start using Response.prototype.bytes() in the code-base
In all cases where we currently use `Response.prototype.arrayBuffer()` the result is immediately wrapped in a `Uint8Array`, which can be avoided by instead using the newer `Response.prototype.bytes()` method; see https://developer.mozilla.org/en-US/docs/Web/API/Response/bytes
1 parent 7077b2a commit 8ba83e7

7 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/core/core_utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async function fetchBinaryData(url) {
129129
`Failed to fetch file "${url}" with "${response.statusText}".`
130130
);
131131
}
132-
return new Uint8Array(await response.arrayBuffer());
132+
return response.bytes();
133133
}
134134

135135
/**

src/display/cmap_reader_factory.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
6464
async _fetch(url) {
6565
const data = await fetchData(
6666
url,
67-
/* type = */ this.isCompressed ? "arraybuffer" : "text"
67+
/* type = */ this.isCompressed ? "bytes" : "text"
6868
);
69-
return data instanceof ArrayBuffer
70-
? new Uint8Array(data)
71-
: stringToBytes(data);
69+
return data instanceof Uint8Array ? data : stringToBytes(data);
7270
}
7371
}
7472

src/display/display_utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ async function fetchData(url, type = "text") {
4848
return response.arrayBuffer();
4949
case "blob":
5050
return response.blob();
51+
case "bytes":
52+
return response.bytes();
5153
case "json":
5254
return response.json();
5355
}
@@ -58,14 +60,17 @@ async function fetchData(url, type = "text") {
5860
return new Promise((resolve, reject) => {
5961
const request = new XMLHttpRequest();
6062
request.open("GET", url, /* async = */ true);
61-
request.responseType = type;
63+
request.responseType = type === "bytes" ? "arraybuffer" : type;
6264

6365
request.onreadystatechange = () => {
6466
if (request.readyState !== XMLHttpRequest.DONE) {
6567
return;
6668
}
6769
if (request.status === 200 || request.status === 0) {
6870
switch (type) {
71+
case "bytes":
72+
resolve(new Uint8Array(request.response));
73+
return;
6974
case "arraybuffer":
7075
case "blob":
7176
case "json":

src/display/standard_fontdata_factory.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
5757
* @ignore
5858
*/
5959
async _fetch(url) {
60-
const data = await fetchData(url, /* type = */ "arraybuffer");
61-
return new Uint8Array(data);
60+
return fetchData(url, /* type = */ "bytes");
6261
}
6362
}
6463

src/display/wasm_factory.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class DOMWasmFactory extends BaseWasmFactory {
5555
* @ignore
5656
*/
5757
async _fetch(url) {
58-
const data = await fetchData(url, /* type = */ "arraybuffer");
59-
return new Uint8Array(data);
58+
return fetchData(url, /* type = */ "bytes");
6059
}
6160
}
6261

src/shared/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,18 @@ if (
12481248
};
12491249
}
12501250

1251+
// See https://developer.mozilla.org/en-US/docs/Web/API/Response/bytes#browser_compatibility
1252+
if (
1253+
typeof PDFJSDev !== "undefined" &&
1254+
!PDFJSDev.test("SKIP_BABEL") &&
1255+
typeof Response.prototype.bytes !== "function"
1256+
) {
1257+
Response.prototype.bytes = async function () {
1258+
return new Uint8Array(await this.arrayBuffer());
1259+
};
1260+
}
1261+
1262+
// TODO: Remove this once Safari 17.4 is the lowest supported version.
12511263
if (
12521264
typeof PDFJSDev !== "undefined" &&
12531265
!PDFJSDev.test("SKIP_BABEL") &&

test/unit/test_utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class DefaultFileReaderFactory {
4141
if (isNodeJS) {
4242
return fetchDataNode(params.path);
4343
}
44-
const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer");
45-
return new Uint8Array(data);
44+
return fetchDataDOM(params.path, /* type = */ "bytes");
4645
}
4746
}
4847

0 commit comments

Comments
 (0)