fetch.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 receivedResponse, receivedError;
  42. let resolveResponse = response => receivedResponse = response;
  43. let rejectResponse = error => receivedError = error;
  44. xhrRequest.withCredentials = true;
  45. xhrRequest.responseType = "arraybuffer";
  46. xhrRequest.onerror = event => reject(new Error(event.details));
  47. xhrRequest.onreadystatechange = () => {
  48. if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
  49. const headers = new Map();
  50. headers.set("content-type", xhrRequest.getResponseHeader("Content-Type"));
  51. resolve({
  52. headers, status: xhrRequest.status, arrayBuffer: () => new Promise((resolve, reject) => {
  53. if (receivedError) {
  54. reject(receivedError);
  55. } else if (receivedResponse) {
  56. resolve(receivedResponse);
  57. } else {
  58. [resolveResponse, rejectResponse] = [resolve, reject];
  59. }
  60. })
  61. });
  62. }
  63. if (xhrRequest.readyState == XMLHttpRequest.DONE) {
  64. resolveResponse(xhrRequest.response);
  65. }
  66. };
  67. xhrRequest.onerror = error => {
  68. reject(error);
  69. if (rejectResponse) {
  70. rejectResponse(error);
  71. }
  72. };
  73. xhrRequest.open("GET", url, true);
  74. xhrRequest.send();
  75. });
  76. if (typeof XPCNativeWrapper != "undefined" && typeof wrappedJSObject != "undefined") {
  77. return xhrPromise.catch(() => XPCNativeWrapper(wrappedJSObject.fetch)(url, { mode: "cors", credentials: "include" }));
  78. } else {
  79. return xhrPromise;
  80. }
  81. };
  82. return superFetch;
  83. async function sendMessage(message) {
  84. const response = await browser.runtime.sendMessage(message);
  85. if (!response || response.error) {
  86. throw new Error(response && response.error.toString());
  87. } else {
  88. return response;
  89. }
  90. }
  91. })();