library_godot_fetch.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. };
  81. const id = IDHandler.add(obj);
  82. const init = {
  83. method: method,
  84. headers: headers,
  85. body: body,
  86. };
  87. obj.request = fetch(url, init);
  88. obj.request.then(GodotFetch.onresponse.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  89. return id;
  90. },
  91. free: function (id) {
  92. const obj = IDHandler.get(id);
  93. if (!obj) {
  94. return;
  95. }
  96. IDHandler.remove(id);
  97. if (!obj.request) {
  98. return;
  99. }
  100. // Try to abort
  101. obj.request.then(function (response) {
  102. response.abort();
  103. }).catch(function (e) { /* nothing to do */ });
  104. },
  105. read: function (id) {
  106. const obj = IDHandler.get(id);
  107. if (!obj) {
  108. return;
  109. }
  110. if (obj.reader && !obj.reading) {
  111. if (obj.done) {
  112. obj.reader = null;
  113. return;
  114. }
  115. obj.reading = true;
  116. obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  117. }
  118. },
  119. },
  120. godot_js_fetch_create__proxy: 'sync',
  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__proxy: 'sync',
  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__proxy: 'sync',
  162. godot_js_fetch_http_status_get__sig: 'ii',
  163. godot_js_fetch_http_status_get: function (p_id) {
  164. const obj = IDHandler.get(p_id);
  165. if (!obj || !obj.response) {
  166. return 0;
  167. }
  168. return obj.status;
  169. },
  170. godot_js_fetch_read_headers__proxy: 'sync',
  171. godot_js_fetch_read_headers__sig: 'iiii',
  172. godot_js_fetch_read_headers: function (p_id, p_parse_cb, p_ref) {
  173. const obj = IDHandler.get(p_id);
  174. if (!obj || !obj.response) {
  175. return 1;
  176. }
  177. const cb = GodotRuntime.get_func(p_parse_cb);
  178. const arr = [];
  179. obj.response.headers.forEach(function (v, h) {
  180. arr.push(`${h}:${v}`);
  181. });
  182. const c_ptr = GodotRuntime.allocStringArray(arr);
  183. cb(arr.length, c_ptr, p_ref);
  184. GodotRuntime.freeStringArray(c_ptr, arr.length);
  185. return 0;
  186. },
  187. godot_js_fetch_read_chunk__proxy: 'sync',
  188. godot_js_fetch_read_chunk__sig: 'iiii',
  189. godot_js_fetch_read_chunk: function (p_id, p_buf, p_buf_size) {
  190. const obj = IDHandler.get(p_id);
  191. if (!obj || !obj.response) {
  192. return 0;
  193. }
  194. let to_read = p_buf_size;
  195. const chunks = obj.chunks;
  196. while (to_read && chunks.length) {
  197. const chunk = obj.chunks[0];
  198. if (chunk.length > to_read) {
  199. GodotRuntime.heapCopy(HEAP8, chunk.slice(0, to_read), p_buf);
  200. chunks[0] = chunk.slice(to_read);
  201. to_read = 0;
  202. } else {
  203. GodotRuntime.heapCopy(HEAP8, chunk, p_buf);
  204. to_read -= chunk.length;
  205. chunks.pop();
  206. }
  207. }
  208. if (!chunks.length) {
  209. GodotFetch.read(p_id);
  210. }
  211. return p_buf_size - to_read;
  212. },
  213. godot_js_fetch_is_chunked__proxy: 'sync',
  214. godot_js_fetch_is_chunked__sig: 'ii',
  215. godot_js_fetch_is_chunked: function (p_id) {
  216. const obj = IDHandler.get(p_id);
  217. if (!obj || !obj.response) {
  218. return -1;
  219. }
  220. return obj.chunked ? 1 : 0;
  221. },
  222. godot_js_fetch_free__proxy: 'sync',
  223. godot_js_fetch_free__sig: 'vi',
  224. godot_js_fetch_free: function (id) {
  225. GodotFetch.free(id);
  226. },
  227. };
  228. autoAddDeps(GodotFetch, '$GodotFetch');
  229. mergeInto(LibraryManager.library, GodotFetch);