library_godot_fetch.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // `body` can be null per spec (for example, in cases where the request method is HEAD).
  60. // As of the time of writing, Chromium (127.0.6533.72) does not follow the spec but Firefox (131.0.3) does.
  61. // See godotengine/godot#76825 for more information.
  62. // See Chromium revert (of the change to follow the spec):
  63. // https://chromium.googlesource.com/chromium/src/+/135354b7bdb554cd03c913af7c90aceead03c4d4
  64. obj.reader = response.body?.getReader();
  65. obj.chunked = chunked;
  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. };
  86. const id = IDHandler.add(obj);
  87. const init = {
  88. method: method,
  89. headers: headers,
  90. body: body,
  91. };
  92. obj.request = fetch(url, init);
  93. obj.request.then(GodotFetch.onresponse.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  94. return id;
  95. },
  96. free: function (id) {
  97. const obj = IDHandler.get(id);
  98. if (!obj) {
  99. return;
  100. }
  101. IDHandler.remove(id);
  102. if (!obj.request) {
  103. return;
  104. }
  105. // Try to abort
  106. obj.request.then(function (response) {
  107. response.abort();
  108. }).catch(function (e) { /* nothing to do */ });
  109. },
  110. read: function (id) {
  111. const obj = IDHandler.get(id);
  112. if (!obj) {
  113. return;
  114. }
  115. if (obj.reader && !obj.reading) {
  116. if (obj.done) {
  117. obj.reader = null;
  118. return;
  119. }
  120. obj.reading = true;
  121. obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
  122. } else if (obj.reader == null && obj.response.body == null) {
  123. // Emulate a stream closure to maintain the request lifecycle.
  124. obj.reading = true;
  125. GodotFetch.onread(id, { value: undefined, done: true });
  126. }
  127. },
  128. },
  129. godot_js_fetch_create__proxy: 'sync',
  130. godot_js_fetch_create__sig: 'iiiiiii',
  131. godot_js_fetch_create: function (p_method, p_url, p_headers, p_headers_size, p_body, p_body_size) {
  132. const method = GodotRuntime.parseString(p_method);
  133. const url = GodotRuntime.parseString(p_url);
  134. const headers = GodotRuntime.parseStringArray(p_headers, p_headers_size);
  135. const body = p_body_size ? GodotRuntime.heapSlice(HEAP8, p_body, p_body_size) : null;
  136. return GodotFetch.create(method, url, headers.map(function (hv) {
  137. const idx = hv.indexOf(':');
  138. if (idx <= 0) {
  139. return [];
  140. }
  141. return [
  142. hv.slice(0, idx).trim(),
  143. hv.slice(idx + 1).trim(),
  144. ];
  145. }).filter(function (v) {
  146. return v.length === 2;
  147. }), body);
  148. },
  149. godot_js_fetch_state_get__proxy: 'sync',
  150. godot_js_fetch_state_get__sig: 'ii',
  151. godot_js_fetch_state_get: function (p_id) {
  152. const obj = IDHandler.get(p_id);
  153. if (!obj) {
  154. return -1;
  155. }
  156. if (obj.error) {
  157. return -1;
  158. }
  159. if (!obj.response) {
  160. return 0;
  161. }
  162. // If the reader is nullish, but there is no body, and the request is not marked as done,
  163. // the same status should be returned as though the request is currently being read
  164. // so that the proper lifecycle closure can be handled in `read()`.
  165. if (obj.reader || (obj.response.body == null && !obj.done)) {
  166. return 1;
  167. }
  168. if (obj.done) {
  169. return 2;
  170. }
  171. return -1;
  172. },
  173. godot_js_fetch_http_status_get__proxy: 'sync',
  174. godot_js_fetch_http_status_get__sig: 'ii',
  175. godot_js_fetch_http_status_get: function (p_id) {
  176. const obj = IDHandler.get(p_id);
  177. if (!obj || !obj.response) {
  178. return 0;
  179. }
  180. return obj.status;
  181. },
  182. godot_js_fetch_read_headers__proxy: 'sync',
  183. godot_js_fetch_read_headers__sig: 'iiii',
  184. godot_js_fetch_read_headers: function (p_id, p_parse_cb, p_ref) {
  185. const obj = IDHandler.get(p_id);
  186. if (!obj || !obj.response) {
  187. return 1;
  188. }
  189. const cb = GodotRuntime.get_func(p_parse_cb);
  190. const arr = [];
  191. obj.response.headers.forEach(function (v, h) {
  192. arr.push(`${h}:${v}`);
  193. });
  194. const c_ptr = GodotRuntime.allocStringArray(arr);
  195. cb(arr.length, c_ptr, p_ref);
  196. GodotRuntime.freeStringArray(c_ptr, arr.length);
  197. return 0;
  198. },
  199. godot_js_fetch_read_chunk__proxy: 'sync',
  200. godot_js_fetch_read_chunk__sig: 'iiii',
  201. godot_js_fetch_read_chunk: function (p_id, p_buf, p_buf_size) {
  202. const obj = IDHandler.get(p_id);
  203. if (!obj || !obj.response) {
  204. return 0;
  205. }
  206. let to_read = p_buf_size;
  207. const chunks = obj.chunks;
  208. while (to_read && chunks.length) {
  209. const chunk = obj.chunks[0];
  210. if (chunk.length > to_read) {
  211. GodotRuntime.heapCopy(HEAP8, chunk.slice(0, to_read), p_buf);
  212. chunks[0] = chunk.slice(to_read);
  213. to_read = 0;
  214. } else {
  215. GodotRuntime.heapCopy(HEAP8, chunk, p_buf);
  216. to_read -= chunk.length;
  217. chunks.pop();
  218. }
  219. }
  220. if (!chunks.length) {
  221. GodotFetch.read(p_id);
  222. }
  223. return p_buf_size - to_read;
  224. },
  225. godot_js_fetch_is_chunked__proxy: 'sync',
  226. godot_js_fetch_is_chunked__sig: 'ii',
  227. godot_js_fetch_is_chunked: function (p_id) {
  228. const obj = IDHandler.get(p_id);
  229. if (!obj || !obj.response) {
  230. return -1;
  231. }
  232. return obj.chunked ? 1 : 0;
  233. },
  234. godot_js_fetch_free__proxy: 'sync',
  235. godot_js_fetch_free__sig: 'vi',
  236. godot_js_fetch_free: function (id) {
  237. GodotFetch.free(id);
  238. },
  239. };
  240. autoAddDeps(GodotFetch, '$GodotFetch');
  241. mergeInto(LibraryManager.library, GodotFetch);