fetch.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, fetch, XMLHttpRequest, XPCNativeWrapper, wrappedJSObject */
  21. this.superFetch = this.superFetch || (() => {
  22. const superFetch = {
  23. fetch: async url => {
  24. try {
  25. return await fetch(url, { mode: "cors", credentials: "include" });
  26. } catch (error) {
  27. const responseFetch = await sendMessage({ method: "fetch", url });
  28. return {
  29. headers: { get: headerName => responseFetch.headers[headerName] },
  30. arrayBuffer: async () => {
  31. const response = await sendMessage({ method: "fetch.array", requestId: responseFetch.responseId });
  32. return new Uint8Array(response.array).buffer;
  33. }
  34. };
  35. }
  36. }
  37. };
  38. superFetch.hostFetch = async url => {
  39. const xhrPromise = new Promise((resolve, reject) => {
  40. const xhrRequest = new XMLHttpRequest();
  41. let resolveResponse, rejectResponse;
  42. xhrRequest.withCredentials = true;
  43. xhrRequest.responseType = "arraybuffer";
  44. xhrRequest.onerror = event => reject(new Error(event.details));
  45. xhrRequest.onreadystatechange = () => {
  46. if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
  47. const headers = new Map();
  48. headers.set("content-type", xhrRequest.getResponseHeader("Content-Type"));
  49. resolve({ headers, status: xhrRequest.status, arrayBuffer: () => new Promise((resolve, reject) => [resolveResponse, rejectResponse] = [resolve, reject]) });
  50. }
  51. if (xhrRequest.readyState == XMLHttpRequest.DONE) {
  52. resolveResponse(xhrRequest.response);
  53. }
  54. };
  55. xhrRequest.onerror = error => {
  56. reject(error);
  57. rejectResponse(error);
  58. };
  59. xhrRequest.open("GET", url, true);
  60. xhrRequest.send();
  61. });
  62. if (typeof XPCNativeWrapper != "undefined" && typeof wrappedJSObject != "undefined") {
  63. return xhrPromise.catch(() => XPCNativeWrapper(wrappedJSObject.fetch)(url, { mode: "cors", credentials: "include" }));
  64. } else {
  65. return xhrPromise;
  66. }
  67. };
  68. return superFetch;
  69. async function sendMessage(message) {
  70. const response = await browser.runtime.sendMessage(message);
  71. if (!response || response.error) {
  72. throw new Error(response && response.error.toString());
  73. } else {
  74. return response;
  75. }
  76. }
  77. })();