library_godot_fetch.js 7.4 KB

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