mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-02 20:37:21 +02:00
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
This commit is contained in:
parent
7077b2a998
commit
8ba83e73fa
@ -129,7 +129,7 @@ async function fetchBinaryData(url) {
|
|||||||
`Failed to fetch file "${url}" with "${response.statusText}".`
|
`Failed to fetch file "${url}" with "${response.statusText}".`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return new Uint8Array(await response.arrayBuffer());
|
return response.bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -64,11 +64,9 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
|
|||||||
async _fetch(url) {
|
async _fetch(url) {
|
||||||
const data = await fetchData(
|
const data = await fetchData(
|
||||||
url,
|
url,
|
||||||
/* type = */ this.isCompressed ? "arraybuffer" : "text"
|
/* type = */ this.isCompressed ? "bytes" : "text"
|
||||||
);
|
);
|
||||||
return data instanceof ArrayBuffer
|
return data instanceof Uint8Array ? data : stringToBytes(data);
|
||||||
? new Uint8Array(data)
|
|
||||||
: stringToBytes(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -48,6 +48,8 @@ async function fetchData(url, type = "text") {
|
|||||||
return response.arrayBuffer();
|
return response.arrayBuffer();
|
||||||
case "blob":
|
case "blob":
|
||||||
return response.blob();
|
return response.blob();
|
||||||
|
case "bytes":
|
||||||
|
return response.bytes();
|
||||||
case "json":
|
case "json":
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
@ -58,7 +60,7 @@ async function fetchData(url, type = "text") {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const request = new XMLHttpRequest();
|
const request = new XMLHttpRequest();
|
||||||
request.open("GET", url, /* async = */ true);
|
request.open("GET", url, /* async = */ true);
|
||||||
request.responseType = type;
|
request.responseType = type === "bytes" ? "arraybuffer" : type;
|
||||||
|
|
||||||
request.onreadystatechange = () => {
|
request.onreadystatechange = () => {
|
||||||
if (request.readyState !== XMLHttpRequest.DONE) {
|
if (request.readyState !== XMLHttpRequest.DONE) {
|
||||||
@ -66,6 +68,9 @@ async function fetchData(url, type = "text") {
|
|||||||
}
|
}
|
||||||
if (request.status === 200 || request.status === 0) {
|
if (request.status === 200 || request.status === 0) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
case "bytes":
|
||||||
|
resolve(new Uint8Array(request.response));
|
||||||
|
return;
|
||||||
case "arraybuffer":
|
case "arraybuffer":
|
||||||
case "blob":
|
case "blob":
|
||||||
case "json":
|
case "json":
|
||||||
|
|||||||
@ -57,8 +57,7 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
|
|||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
async _fetch(url) {
|
async _fetch(url) {
|
||||||
const data = await fetchData(url, /* type = */ "arraybuffer");
|
return fetchData(url, /* type = */ "bytes");
|
||||||
return new Uint8Array(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -55,8 +55,7 @@ class DOMWasmFactory extends BaseWasmFactory {
|
|||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
async _fetch(url) {
|
async _fetch(url) {
|
||||||
const data = await fetchData(url, /* type = */ "arraybuffer");
|
return fetchData(url, /* type = */ "bytes");
|
||||||
return new Uint8Array(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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 (
|
if (
|
||||||
typeof PDFJSDev !== "undefined" &&
|
typeof PDFJSDev !== "undefined" &&
|
||||||
!PDFJSDev.test("SKIP_BABEL") &&
|
!PDFJSDev.test("SKIP_BABEL") &&
|
||||||
|
|||||||
@ -41,8 +41,7 @@ class DefaultFileReaderFactory {
|
|||||||
if (isNodeJS) {
|
if (isNodeJS) {
|
||||||
return fetchDataNode(params.path);
|
return fetchDataNode(params.path);
|
||||||
}
|
}
|
||||||
const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer");
|
return fetchDataDOM(params.path, /* type = */ "bytes");
|
||||||
return new Uint8Array(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user