library_godot_fetch.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. let bodySize = -1;
  51. response.headers.forEach(function (value, header) {
  52. const v = value.toLowerCase().trim();
  53. const h = header.toLowerCase().trim();
  54. if (h === 'transfer-encoding' && v === 'chunked') {
  55. chunked = true;
  56. }
  57. if (h === 'content-length') {
  58. bodySize = parseInt(v, 10);
  59. }
  60. });
  61. obj.status = response.status;
  62. obj.response = response;
  63. obj.reader = response.body.getReader();
  64. obj.chunked = chunked;
  65. obj.bodySize = bodySize;
  66. },
  67. onerror: function (id, err) {
  68. GodotRuntime.error(err);
  69. const obj = IDHandler.get(id);
  70. if (!obj) {
  71. return;
  72. }
  73. obj.error = err;
  74. },
  75. create: function (method, url, headers, body) {
  76. const obj = {
  77. request: null,
  78. response: null,
  79. reader: null,
  80. error: null,
  81. done: false,
  82. reading: false,
  83. status: 0,
  84. chunks: [],
  85. bodySize: -1,
  86. };
  87. const id = IDHandler.add(obj);
  88. const init = {
  89. method: method,
  90. headers: headers,
  91. body: body,
  92. };
  93. obj.request = fetch(url, init);
  94. obj.request.then(GodotFetch.onresponse.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  95. return id;
  96. },
  97. free: function (id) {
  98. const obj = IDHandler.get(id);
  99. if (!obj) {
  100. return;
  101. }
  102. IDHandler.remove(id);
  103. if (!obj.request) {
  104. return;
  105. }
  106. // Try to abort
  107. obj.request.then(function (response) {
  108. response.abort();
  109. }).catch(function (e) { /* nothing to do */ });
  110. },
  111. read: function (id) {
  112. const obj = IDHandler.get(id);
  113. if (!obj) {
  114. return;
  115. }
  116. if (obj.reader && !obj.reading) {
  117. if (obj.done) {
  118. obj.reader = null;
  119. return;
  120. }
  121. obj.reading = true;
  122. obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  123. }
  124. },
  125. },
  126. godot_js_fetch_create__sig: 'iiiiiii',
  127. godot_js_fetch_create: function (p_method, p_url, p_headers, p_headers_size, p_body, p_body_size) {
  128. const method = GodotRuntime.parseString(p_method);
  129. const url = GodotRuntime.parseString(p_url);
  130. const headers = GodotRuntime.parseStringArray(p_headers, p_headers_size);
  131. const body = p_body_size ? GodotRuntime.heapSlice(HEAP8, p_body, p_body_size) : null;
  132. return GodotFetch.create(method, url, headers.map(function (hv) {
  133. const idx = hv.indexOf(':');
  134. if (idx <= 0) {
  135. return [];
  136. }
  137. return [
  138. hv.slice(0, idx).trim(),
  139. hv.slice(idx + 1).trim(),
  140. ];
  141. }).filter(function (v) {
  142. return v.length === 2;
  143. }), body);
  144. },
  145. godot_js_fetch_state_get__sig: 'ii',
  146. godot_js_fetch_state_get: function (p_id) {
  147. const obj = IDHandler.get(p_id);
  148. if (!obj) {
  149. return -1;
  150. }
  151. if (obj.error) {
  152. return -1;
  153. }
  154. if (!obj.response) {
  155. return 0;
  156. }
  157. if (obj.reader) {
  158. return 1;
  159. }
  160. if (obj.done) {
  161. return 2;
  162. }
  163. return -1;
  164. },
  165. godot_js_fetch_http_status_get__sig: 'ii',
  166. godot_js_fetch_http_status_get: function (p_id) {
  167. const obj = IDHandler.get(p_id);
  168. if (!obj || !obj.response) {
  169. return 0;
  170. }
  171. return obj.status;
  172. },
  173. godot_js_fetch_read_headers__sig: 'iiii',
  174. godot_js_fetch_read_headers: function (p_id, p_parse_cb, p_ref) {
  175. const obj = IDHandler.get(p_id);
  176. if (!obj || !obj.response) {
  177. return 1;
  178. }
  179. const cb = GodotRuntime.get_func(p_parse_cb);
  180. const arr = [];
  181. obj.response.headers.forEach(function (v, h) {
  182. arr.push(`${h}:${v}`);
  183. });
  184. const c_ptr = GodotRuntime.allocStringArray(arr);
  185. cb(arr.length, c_ptr, p_ref);
  186. GodotRuntime.freeStringArray(c_ptr, arr.length);
  187. return 0;
  188. },
  189. godot_js_fetch_read_chunk__sig: 'iiii',
  190. godot_js_fetch_read_chunk: function (p_id, p_buf, p_buf_size) {
  191. const obj = IDHandler.get(p_id);
  192. if (!obj || !obj.response) {
  193. return 0;
  194. }
  195. let to_read = p_buf_size;
  196. const chunks = obj.chunks;
  197. while (to_read && chunks.length) {
  198. const chunk = obj.chunks[0];
  199. if (chunk.length > to_read) {
  200. GodotRuntime.heapCopy(HEAP8, chunk.slice(0, to_read), p_buf);
  201. chunks[0] = chunk.slice(to_read);
  202. to_read = 0;
  203. } else {
  204. GodotRuntime.heapCopy(HEAP8, chunk, p_buf);
  205. to_read -= chunk.length;
  206. chunks.pop();
  207. }
  208. }
  209. if (!chunks.length) {
  210. GodotFetch.read(p_id);
  211. }
  212. return p_buf_size - to_read;
  213. },
  214. godot_js_fetch_body_length_get__sig: 'ii',
  215. godot_js_fetch_body_length_get: function (p_id) {
  216. const obj = IDHandler.get(p_id);
  217. if (!obj || !obj.response) {
  218. return -1;
  219. }
  220. return obj.bodySize;
  221. },
  222. godot_js_fetch_is_chunked__sig: 'ii',
  223. godot_js_fetch_is_chunked: function (p_id) {
  224. const obj = IDHandler.get(p_id);
  225. if (!obj || !obj.response) {
  226. return -1;
  227. }
  228. return obj.chunked ? 1 : 0;
  229. },
  230. godot_js_fetch_free__sig: 'vi',
  231. godot_js_fetch_free: function (id) {
  232. GodotFetch.free(id);
  233. },
  234. };
  235. autoAddDeps(GodotFetch, '$GodotFetch');
  236. mergeInto(LibraryManager.library, GodotFetch);