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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. credentials: 'include',
  88. };
  89. obj.request = fetch(url, init);
  90. obj.request.then(GodotFetch.onresponse.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  91. return id;
  92. },
  93. free: function (id) {
  94. const obj = IDHandler.get(id);
  95. if (!obj) {
  96. return;
  97. }
  98. IDHandler.remove(id);
  99. if (!obj.request) {
  100. return;
  101. }
  102. // Try to abort
  103. obj.request.then(function (response) {
  104. response.abort();
  105. }).catch(function (e) { /* nothing to do */ });
  106. },
  107. read: function (id) {
  108. const obj = IDHandler.get(id);
  109. if (!obj) {
  110. return;
  111. }
  112. if (obj.reader && !obj.reading) {
  113. if (obj.done) {
  114. obj.reader = null;
  115. return;
  116. }
  117. obj.reading = true;
  118. obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  119. }
  120. },
  121. },
  122. godot_js_fetch_create__sig: 'iiiiiii',
  123. godot_js_fetch_create: function (p_method, p_url, p_headers, p_headers_size, p_body, p_body_size) {
  124. const method = GodotRuntime.parseString(p_method);
  125. const url = GodotRuntime.parseString(p_url);
  126. const headers = GodotRuntime.parseStringArray(p_headers, p_headers_size);
  127. const body = p_body_size ? GodotRuntime.heapSlice(HEAP8, p_body, p_body_size) : null;
  128. return GodotFetch.create(method, url, headers.map(function (hv) {
  129. const idx = hv.indexOf(':');
  130. if (idx <= 0) {
  131. return [];
  132. }
  133. return [
  134. hv.slice(0, idx).trim(),
  135. hv.slice(idx + 1).trim(),
  136. ];
  137. }).filter(function (v) {
  138. return v.length === 2;
  139. }), body);
  140. },
  141. godot_js_fetch_state_get__sig: 'ii',
  142. godot_js_fetch_state_get: function (p_id) {
  143. const obj = IDHandler.get(p_id);
  144. if (!obj) {
  145. return -1;
  146. }
  147. if (obj.error) {
  148. return -1;
  149. }
  150. if (!obj.response) {
  151. return 0;
  152. }
  153. if (obj.reader) {
  154. return 1;
  155. }
  156. if (obj.done) {
  157. return 2;
  158. }
  159. return -1;
  160. },
  161. godot_js_fetch_http_status_get__sig: 'ii',
  162. godot_js_fetch_http_status_get: function (p_id) {
  163. const obj = IDHandler.get(p_id);
  164. if (!obj || !obj.response) {
  165. return 0;
  166. }
  167. return obj.status;
  168. },
  169. godot_js_fetch_read_headers__sig: 'iiii',
  170. godot_js_fetch_read_headers: function (p_id, p_parse_cb, p_ref) {
  171. const obj = IDHandler.get(p_id);
  172. if (!obj || !obj.response) {
  173. return 1;
  174. }
  175. const cb = GodotRuntime.get_func(p_parse_cb);
  176. const arr = [];
  177. obj.response.headers.forEach(function (v, h) {
  178. arr.push(`${h}:${v}`);
  179. });
  180. const c_ptr = GodotRuntime.allocStringArray(arr);
  181. cb(arr.length, c_ptr, p_ref);
  182. GodotRuntime.freeStringArray(c_ptr, arr.length);
  183. return 0;
  184. },
  185. godot_js_fetch_read_chunk__sig: 'iiii',
  186. godot_js_fetch_read_chunk: function (p_id, p_buf, p_buf_size) {
  187. const obj = IDHandler.get(p_id);
  188. if (!obj || !obj.response) {
  189. return 0;
  190. }
  191. let to_read = p_buf_size;
  192. const chunks = obj.chunks;
  193. while (to_read && chunks.length) {
  194. const chunk = obj.chunks[0];
  195. if (chunk.length > to_read) {
  196. GodotRuntime.heapCopy(HEAP8, chunk.slice(0, to_read), p_buf);
  197. chunks[0] = chunk.slice(to_read);
  198. to_read = 0;
  199. } else {
  200. GodotRuntime.heapCopy(HEAP8, chunk, p_buf);
  201. to_read -= chunk.length;
  202. chunks.pop();
  203. }
  204. }
  205. if (!chunks.length) {
  206. GodotFetch.read(p_id);
  207. }
  208. return p_buf_size - to_read;
  209. },
  210. godot_js_fetch_body_length_get__sig: 'ii',
  211. godot_js_fetch_body_length_get: function (p_id) {
  212. const obj = IDHandler.get(p_id);
  213. if (!obj || !obj.response) {
  214. return -1;
  215. }
  216. return obj.bodySize;
  217. },
  218. godot_js_fetch_is_chunked__sig: 'ii',
  219. godot_js_fetch_is_chunked: function (p_id) {
  220. const obj = IDHandler.get(p_id);
  221. if (!obj || !obj.response) {
  222. return -1;
  223. }
  224. return obj.chunked ? 1 : 0;
  225. },
  226. godot_js_fetch_free__sig: 'vi',
  227. godot_js_fetch_free: function (id) {
  228. GodotFetch.free(id);
  229. },
  230. };
  231. autoAddDeps(GodotFetch, '$GodotFetch');
  232. mergeInto(LibraryManager.library, GodotFetch);