webRequest.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /** @internal */
  2. // eslint-disable-next-line @typescript-eslint/naming-convention
  3. function createXMLHttpRequest() {
  4. // If running in Babylon Native, then defer to the native XMLHttpRequest, which has the same public contract
  5. if (typeof _native !== "undefined" && _native.XMLHttpRequest) {
  6. return new _native.XMLHttpRequest();
  7. }
  8. else {
  9. return new XMLHttpRequest();
  10. }
  11. }
  12. /**
  13. * Extended version of XMLHttpRequest with support for customizations (headers, ...)
  14. */
  15. export class WebRequest {
  16. constructor() {
  17. this._xhr = createXMLHttpRequest();
  18. this._requestURL = "";
  19. }
  20. /**
  21. * This function can be called to check if there are request modifiers for network requests
  22. * @returns true if there are any custom requests available
  23. */
  24. static get IsCustomRequestAvailable() {
  25. return Object.keys(WebRequest.CustomRequestHeaders).length > 0 || WebRequest.CustomRequestModifiers.length > 0;
  26. }
  27. _injectCustomRequestHeaders() {
  28. if (this._shouldSkipRequestModifications(this._requestURL)) {
  29. return;
  30. }
  31. for (const key in WebRequest.CustomRequestHeaders) {
  32. const val = WebRequest.CustomRequestHeaders[key];
  33. if (val) {
  34. this._xhr.setRequestHeader(key, val);
  35. }
  36. }
  37. }
  38. _shouldSkipRequestModifications(url) {
  39. return WebRequest.SkipRequestModificationForBabylonCDN && (url.includes("preview.babylonjs.com") || url.includes("cdn.babylonjs.com"));
  40. }
  41. /**
  42. * Gets or sets a function to be called when loading progress changes
  43. */
  44. get onprogress() {
  45. return this._xhr.onprogress;
  46. }
  47. set onprogress(value) {
  48. this._xhr.onprogress = value;
  49. }
  50. /**
  51. * Returns client's state
  52. */
  53. get readyState() {
  54. return this._xhr.readyState;
  55. }
  56. /**
  57. * Returns client's status
  58. */
  59. get status() {
  60. return this._xhr.status;
  61. }
  62. /**
  63. * Returns client's status as a text
  64. */
  65. get statusText() {
  66. return this._xhr.statusText;
  67. }
  68. /**
  69. * Returns client's response
  70. */
  71. get response() {
  72. return this._xhr.response;
  73. }
  74. /**
  75. * Returns client's response url
  76. */
  77. get responseURL() {
  78. return this._xhr.responseURL;
  79. }
  80. /**
  81. * Returns client's response as text
  82. */
  83. get responseText() {
  84. return this._xhr.responseText;
  85. }
  86. /**
  87. * Gets or sets the expected response type
  88. */
  89. get responseType() {
  90. return this._xhr.responseType;
  91. }
  92. set responseType(value) {
  93. this._xhr.responseType = value;
  94. }
  95. /**
  96. * Gets or sets the timeout value in milliseconds
  97. */
  98. get timeout() {
  99. return this._xhr.timeout;
  100. }
  101. set timeout(value) {
  102. this._xhr.timeout = value;
  103. }
  104. addEventListener(type, listener, options) {
  105. this._xhr.addEventListener(type, listener, options);
  106. }
  107. removeEventListener(type, listener, options) {
  108. this._xhr.removeEventListener(type, listener, options);
  109. }
  110. /**
  111. * Cancels any network activity
  112. */
  113. abort() {
  114. this._xhr.abort();
  115. }
  116. /**
  117. * Initiates the request. The optional argument provides the request body. The argument is ignored if request method is GET or HEAD
  118. * @param body defines an optional request body
  119. */
  120. send(body) {
  121. if (WebRequest.CustomRequestHeaders) {
  122. this._injectCustomRequestHeaders();
  123. }
  124. this._xhr.send(body);
  125. }
  126. /**
  127. * Sets the request method, request URL
  128. * @param method defines the method to use (GET, POST, etc..)
  129. * @param url defines the url to connect with
  130. */
  131. open(method, url) {
  132. for (const update of WebRequest.CustomRequestModifiers) {
  133. if (this._shouldSkipRequestModifications(url)) {
  134. return;
  135. }
  136. update(this._xhr, url);
  137. }
  138. // Clean url
  139. url = url.replace("file:http:", "http:");
  140. url = url.replace("file:https:", "https:");
  141. this._requestURL = url;
  142. this._xhr.open(method, url, true);
  143. }
  144. /**
  145. * Sets the value of a request header.
  146. * @param name The name of the header whose value is to be set
  147. * @param value The value to set as the body of the header
  148. */
  149. setRequestHeader(name, value) {
  150. this._xhr.setRequestHeader(name, value);
  151. }
  152. /**
  153. * Get the string containing the text of a particular header's value.
  154. * @param name The name of the header
  155. * @returns The string containing the text of the given header name
  156. */
  157. getResponseHeader(name) {
  158. return this._xhr.getResponseHeader(name);
  159. }
  160. }
  161. /**
  162. * Custom HTTP Request Headers to be sent with XMLHttpRequests
  163. * i.e. when loading files, where the server/service expects an Authorization header
  164. */
  165. WebRequest.CustomRequestHeaders = {};
  166. /**
  167. * Add callback functions in this array to update all the requests before they get sent to the network
  168. */
  169. WebRequest.CustomRequestModifiers = new Array();
  170. /**
  171. * If set to true, requests to Babylon.js CDN requests will not be modified
  172. */
  173. WebRequest.SkipRequestModificationForBabylonCDN = true;
  174. //# sourceMappingURL=webRequest.js.map