nio.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2011 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile Core.
  6. *
  7. * SingleFile Core 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 Core 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 Core. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. (function() {
  21. singlefile.nio = {};
  22. singlefile.nio.RequestManager = function() {
  23. var cache = {}, keys = [], pendingResponseHandlers = {}, XHR_TIMEOUT = 45000;
  24. function sendResponses(key) {
  25. if (pendingResponseHandlers[key]) {
  26. pendingResponseHandlers[key].forEach(function(callback) {
  27. callback(cache[key]);
  28. });
  29. delete pendingResponseHandlers[key];
  30. }
  31. }
  32. function arrayBufferToBase64(buffer) {
  33. var binary, bytes, len, i;
  34. binary = "";
  35. bytes = new Uint8Array(buffer);
  36. len = bytes.byteLength;
  37. for (i = 0; i < len; i++) {
  38. binary += String.fromCharCode(bytes[i]);
  39. }
  40. return btoa(binary);
  41. }
  42. this.reset = function() {
  43. cache = {};
  44. keys = [];
  45. };
  46. this.send = function(url, responseHandler, characterSet, mediaTypeParam) {
  47. var xhr, timeout, key = JSON.stringify({
  48. url : url,
  49. characterSet : characterSet,
  50. mediaTypeParam : mediaTypeParam
  51. });
  52. if (cache[key])
  53. setTimeout(function() {
  54. responseHandler(cache[key]);
  55. }, 1);
  56. else if (pendingResponseHandlers[key])
  57. pendingResponseHandlers[key].push(responseHandler);
  58. else {
  59. pendingResponseHandlers[key] = [ responseHandler ];
  60. xhr = new XMLHttpRequest();
  61. xhr.onload = function() {
  62. clearTimeout(timeout);
  63. cache[key] = {
  64. url : url,
  65. status : xhr.status,
  66. mediaType : xhr.getResponseHeader("Content-Type"),
  67. content : mediaTypeParam == "base64" ? arrayBufferToBase64(xhr.response) : xhr.responseText,
  68. mediaTypeParam : mediaTypeParam
  69. };
  70. keys.push(key);
  71. sendResponses(key);
  72. };
  73. xhr.onerror = function() {
  74. sendResponses(key);
  75. };
  76. xhr.open("GET", url, true);
  77. if (mediaTypeParam == "base64") {
  78. xhr.responseType = "arraybuffer";
  79. }
  80. timeout = setTimeout(function() {
  81. xhr.abort();
  82. sendResponses(key);
  83. }, XHR_TIMEOUT);
  84. try {
  85. xhr.send(null);
  86. } catch (e) {
  87. sendResponses(key);
  88. }
  89. }
  90. };
  91. };
  92. })();