library_godot_fetch.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**************************************************************************/
  2. /* library_godot_fetch.js */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. const GodotFetch = {
  31. $GodotFetch__deps: ['$IDHandler', '$GodotRuntime'],
  32. $GodotFetch: {
  33. onread: function (id, result) {
  34. const obj = IDHandler.get(id);
  35. if (!obj) {
  36. return;
  37. }
  38. if (result.value) {
  39. obj.chunks.push(result.value);
  40. }
  41. obj.reading = false;
  42. obj.done = result.done;
  43. },
  44. onresponse: function (id, response) {
  45. const obj = IDHandler.get(id);
  46. if (!obj) {
  47. return;
  48. }
  49. let chunked = false;
  50. response.headers.forEach(function (value, header) {
  51. const v = value.toLowerCase().trim();
  52. const h = header.toLowerCase().trim();
  53. if (h === 'transfer-encoding' && v === 'chunked') {
  54. chunked = true;
  55. }
  56. });
  57. obj.status = response.status;
  58. obj.response = response;
  59. obj.reader = response.body.getReader();
  60. obj.chunked = chunked;
  61. },
  62. onerror: function (id, err) {
  63. GodotRuntime.error(err);
  64. const obj = IDHandler.get(id);
  65. if (!obj) {
  66. return;
  67. }
  68. obj.error = err;
  69. },
  70. create: function (method, url, headers, body) {
  71. const obj = {
  72. request: null,
  73. response: null,
  74. reader: null,
  75. error: null,
  76. done: false,
  77. reading: false,
  78. status: 0,
  79. chunks: [],
  80. bodySize: -1,
  81. };
  82. const id = IDHandler.add(obj);
  83. const init = {
  84. method: method,
  85. headers: headers,
  86. body: body,
  87. };
  88. obj.request = fetch(url, init);
  89. obj.request.then(GodotFetch.onresponse.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  90. return id;
  91. },
  92. free: function (id) {
  93. const obj = IDHandler.get(id);
  94. if (!obj) {
  95. return;
  96. }
  97. IDHandler.remove(id);
  98. if (!obj.request) {
  99. return;
  100. }
  101. // Try to abort
  102. obj.request.then(function (response) {
  103. response.abort();
  104. }).catch(function (e) { /* nothing to do */ });
  105. },
  106. read: function (id) {
  107. const obj = IDHandler.get(id);
  108. if (!obj) {
  109. return;
  110. }
  111. if (obj.reader && !obj.reading) {
  112. if (obj.done) {
  113. obj.reader = null;
  114. return;
  115. }
  116. obj.reading = true;
  117. obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  118. }
  119. },
  120. },
  121. godot_js_fetch_create__sig: 'iiiiiii',
  122. godot_js_fetch_create: function (p_method, p_url, p_headers, p_headers_size, p_body, p_body_size) {
  123. const method = GodotRuntime.parseString(p_method);
  124. const url = GodotRuntime.parseString(p_url);
  125. const headers = GodotRuntime.parseStringArray(p_headers, p_headers_size);
  126. const body = p_body_size ? GodotRuntime.heapSlice(HEAP8, p_body, p_body_size) : null;
  127. return GodotFetch.create(method, url, headers.map(function (hv) {
  128. const idx = hv.indexOf(':');
  129. if (idx <= 0) {
  130. return [];
  131. }
  132. return [
  133. hv.slice(0, idx).trim(),
  134. hv.slice(idx + 1).trim(),
  135. ];
  136. }).filter(function (v) {
  137. return v.length === 2;
  138. }), body);
  139. },
  140. godot_js_fetch_state_get__sig: 'ii',
  141. godot_js_fetch_state_get: function (p_id) {
  142. const obj = IDHandler.get(p_id);
  143. if (!obj) {
  144. return -1;
  145. }
  146. if (obj.error) {
  147. return -1;
  148. }
  149. if (!obj.response) {
  150. return 0;
  151. }
  152. if (obj.reader) {
  153. return 1;
  154. }
  155. if (obj.done) {
  156. return 2;
  157. }
  158. return -1;
  159. },
  160. godot_js_fetch_http_status_get__sig: 'ii',
  161. godot_js_fetch_http_status_get: function (p_id) {
  162. const obj = IDHandler.get(p_id);
  163. if (!obj || !obj.response) {
  164. return 0;
  165. }
  166. return obj.status;
  167. },
  168. godot_js_fetch_read_headers__sig: 'iiii',
  169. godot_js_fetch_read_headers: function (p_id, p_parse_cb, p_ref) {
  170. const obj = IDHandler.get(p_id);
  171. if (!obj || !obj.response) {
  172. return 1;
  173. }
  174. const cb = GodotRuntime.get_func(p_parse_cb);
  175. const arr = [];
  176. obj.response.headers.forEach(function (v, h) {
  177. arr.push(`${h}:${v}`);
  178. });
  179. const c_ptr = GodotRuntime.allocStringArray(arr);
  180. cb(arr.length, c_ptr, p_ref);
  181. GodotRuntime.freeStringArray(c_ptr, arr.length);
  182. return 0;
  183. },
  184. godot_js_fetch_read_chunk__sig: 'iiii',
  185. godot_js_fetch_read_chunk: function (p_id, p_buf, p_buf_size) {
  186. const obj = IDHandler.get(p_id);
  187. if (!obj || !obj.response) {
  188. return 0;
  189. }
  190. let to_read = p_buf_size;
  191. const chunks = obj.chunks;
  192. while (to_read && chunks.length) {
  193. const chunk = obj.chunks[0];
  194. if (chunk.length > to_read) {
  195. GodotRuntime.heapCopy(HEAP8, chunk.slice(0, to_read), p_buf);
  196. chunks[0] = chunk.slice(to_read);
  197. to_read = 0;
  198. } else {
  199. GodotRuntime.heapCopy(HEAP8, chunk, p_buf);
  200. to_read -= chunk.length;
  201. chunks.pop();
  202. }
  203. }
  204. if (!chunks.length) {
  205. GodotFetch.read(p_id);
  206. }
  207. return p_buf_size - to_read;
  208. },
  209. godot_js_fetch_body_length_get__sig: 'ii',
  210. godot_js_fetch_body_length_get: function (p_id) {
  211. const obj = IDHandler.get(p_id);
  212. if (!obj || !obj.response) {
  213. return -1;
  214. }
  215. return obj.bodySize;
  216. },
  217. godot_js_fetch_is_chunked__sig: 'ii',
  218. godot_js_fetch_is_chunked: function (p_id) {
  219. const obj = IDHandler.get(p_id);
  220. if (!obj || !obj.response) {
  221. return -1;
  222. }
  223. return obj.chunked ? 1 : 0;
  224. },
  225. godot_js_fetch_free__sig: 'vi',
  226. godot_js_fetch_free: function (id) {
  227. GodotFetch.free(id);
  228. },
  229. };
  230. autoAddDeps(GodotFetch, '$GodotFetch');
  231. mergeInto(LibraryManager.library, GodotFetch);