fileTools.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /* eslint-disable @typescript-eslint/naming-convention */
  2. import { WebRequest } from "./webRequest.js";
  3. import { IsWindowObjectExist } from "./domManagement.js";
  4. import { Observable } from "./observable.js";
  5. import { FilesInputStore } from "./filesInputStore.js";
  6. import { RetryStrategy } from "./retryStrategy.js";
  7. import { BaseError, ErrorCodes, RuntimeError } from "./error.js";
  8. import { DecodeBase64ToBinary, DecodeBase64ToString, EncodeArrayBufferToBase64 } from "./stringTools.js";
  9. import { ShaderProcessor } from "../Engines/Processors/shaderProcessor.js";
  10. import { EngineStore } from "../Engines/engineStore.js";
  11. import { Logger } from "./logger.js";
  12. import { TimingTools } from "./timingTools.js";
  13. import { AbstractEngine } from "../Engines/abstractEngine.js";
  14. const Base64DataUrlRegEx = new RegExp(/^data:([^,]+\/[^,]+)?;base64,/i);
  15. /** @ignore */
  16. export class LoadFileError extends RuntimeError {
  17. /**
  18. * Creates a new LoadFileError
  19. * @param message defines the message of the error
  20. * @param object defines the optional web request
  21. */
  22. constructor(message, object) {
  23. super(message, ErrorCodes.LoadFileError);
  24. this.name = "LoadFileError";
  25. BaseError._setPrototypeOf(this, LoadFileError.prototype);
  26. if (object instanceof WebRequest) {
  27. this.request = object;
  28. }
  29. else {
  30. this.file = object;
  31. }
  32. }
  33. }
  34. /** @ignore */
  35. export class RequestFileError extends RuntimeError {
  36. /**
  37. * Creates a new LoadFileError
  38. * @param message defines the message of the error
  39. * @param request defines the optional web request
  40. */
  41. constructor(message, request) {
  42. super(message, ErrorCodes.RequestFileError);
  43. this.request = request;
  44. this.name = "RequestFileError";
  45. BaseError._setPrototypeOf(this, RequestFileError.prototype);
  46. }
  47. }
  48. /** @ignore */
  49. export class ReadFileError extends RuntimeError {
  50. /**
  51. * Creates a new ReadFileError
  52. * @param message defines the message of the error
  53. * @param file defines the optional file
  54. */
  55. constructor(message, file) {
  56. super(message, ErrorCodes.ReadFileError);
  57. this.file = file;
  58. this.name = "ReadFileError";
  59. BaseError._setPrototypeOf(this, ReadFileError.prototype);
  60. }
  61. }
  62. /**
  63. * @internal
  64. */
  65. export const FileToolsOptions = {
  66. /**
  67. * Gets or sets the retry strategy to apply when an error happens while loading an asset.
  68. * When defining this function, return the wait time before trying again or return -1 to
  69. * stop retrying and error out.
  70. */
  71. DefaultRetryStrategy: RetryStrategy.ExponentialBackoff(),
  72. /**
  73. * Gets or sets the base URL to use to load assets
  74. */
  75. BaseUrl: "",
  76. /**
  77. * Default behaviour for cors in the application.
  78. * It can be a string if the expected behavior is identical in the entire app.
  79. * Or a callback to be able to set it per url or on a group of them (in case of Video source for instance)
  80. */
  81. CorsBehavior: "anonymous",
  82. /**
  83. * Gets or sets a function used to pre-process url before using them to load assets
  84. * @param url
  85. * @returns the processed url
  86. */
  87. PreprocessUrl: (url) => url,
  88. /**
  89. * Gets or sets the base URL to use to load scripts
  90. * Used for both JS and WASM
  91. */
  92. ScriptBaseUrl: "",
  93. /**
  94. * Gets or sets a function used to pre-process script url before using them to load.
  95. * Used for both JS and WASM
  96. * @param url defines the url to process
  97. * @returns the processed url
  98. */
  99. ScriptPreprocessUrl: (url) => url,
  100. };
  101. /**
  102. * Removes unwanted characters from an url
  103. * @param url defines the url to clean
  104. * @returns the cleaned url
  105. */
  106. const _CleanUrl = (url) => {
  107. url = url.replace(/#/gm, "%23");
  108. return url;
  109. };
  110. /**
  111. * Sets the cors behavior on a dom element. This will add the required Tools.CorsBehavior to the element.
  112. * @param url define the url we are trying
  113. * @param element define the dom element where to configure the cors policy
  114. * @internal
  115. */
  116. export const SetCorsBehavior = (url, element) => {
  117. if (url && url.indexOf("data:") === 0) {
  118. return;
  119. }
  120. if (FileToolsOptions.CorsBehavior) {
  121. if (typeof FileToolsOptions.CorsBehavior === "string" || FileToolsOptions.CorsBehavior instanceof String) {
  122. element.crossOrigin = FileToolsOptions.CorsBehavior;
  123. }
  124. else {
  125. const result = FileToolsOptions.CorsBehavior(url);
  126. if (result) {
  127. element.crossOrigin = result;
  128. }
  129. }
  130. }
  131. };
  132. /**
  133. * Loads an image as an HTMLImageElement.
  134. * @param input url string, ArrayBuffer, or Blob to load
  135. * @param onLoad callback called when the image successfully loads
  136. * @param onError callback called when the image fails to load
  137. * @param offlineProvider offline provider for caching
  138. * @param mimeType optional mime type
  139. * @param imageBitmapOptions
  140. * @returns the HTMLImageElement of the loaded image
  141. * @internal
  142. */
  143. export const LoadImage = (input, onLoad, onError, offlineProvider, mimeType = "", imageBitmapOptions) => {
  144. const engine = EngineStore.LastCreatedEngine;
  145. if (typeof HTMLImageElement === "undefined" && !engine?._features.forceBitmapOverHTMLImageElement) {
  146. onError("LoadImage is only supported in web or BabylonNative environments.");
  147. return null;
  148. }
  149. let url;
  150. let usingObjectURL = false;
  151. if (input instanceof ArrayBuffer || ArrayBuffer.isView(input)) {
  152. if (typeof Blob !== "undefined" && typeof URL !== "undefined") {
  153. url = URL.createObjectURL(new Blob([input], { type: mimeType }));
  154. usingObjectURL = true;
  155. }
  156. else {
  157. url = `data:${mimeType};base64,` + EncodeArrayBufferToBase64(input);
  158. }
  159. }
  160. else if (input instanceof Blob) {
  161. url = URL.createObjectURL(input);
  162. usingObjectURL = true;
  163. }
  164. else {
  165. url = _CleanUrl(input);
  166. url = FileToolsOptions.PreprocessUrl(input);
  167. }
  168. const onErrorHandler = (exception) => {
  169. if (onError) {
  170. const inputText = url || input.toString();
  171. onError(`Error while trying to load image: ${inputText.indexOf("http") === 0 || inputText.length <= 128 ? inputText : inputText.slice(0, 128) + "..."}`, exception);
  172. }
  173. };
  174. if (engine?._features.forceBitmapOverHTMLImageElement) {
  175. LoadFile(url, (data) => {
  176. engine
  177. .createImageBitmap(new Blob([data], { type: mimeType }), { premultiplyAlpha: "none", ...imageBitmapOptions })
  178. .then((imgBmp) => {
  179. onLoad(imgBmp);
  180. if (usingObjectURL) {
  181. URL.revokeObjectURL(url);
  182. }
  183. })
  184. .catch((reason) => {
  185. if (onError) {
  186. onError("Error while trying to load image: " + input, reason);
  187. }
  188. });
  189. }, undefined, offlineProvider || undefined, true, (request, exception) => {
  190. onErrorHandler(exception);
  191. });
  192. return null;
  193. }
  194. const img = new Image();
  195. SetCorsBehavior(url, img);
  196. const handlersList = [];
  197. const loadHandlersList = () => {
  198. handlersList.forEach((handler) => {
  199. handler.target.addEventListener(handler.name, handler.handler);
  200. });
  201. };
  202. const unloadHandlersList = () => {
  203. handlersList.forEach((handler) => {
  204. handler.target.removeEventListener(handler.name, handler.handler);
  205. });
  206. handlersList.length = 0;
  207. };
  208. const loadHandler = () => {
  209. unloadHandlersList();
  210. onLoad(img);
  211. // Must revoke the URL after calling onLoad to avoid security exceptions in
  212. // certain scenarios (e.g. when hosted in vscode).
  213. if (usingObjectURL && img.src) {
  214. URL.revokeObjectURL(img.src);
  215. }
  216. };
  217. const errorHandler = (err) => {
  218. unloadHandlersList();
  219. onErrorHandler(err);
  220. if (usingObjectURL && img.src) {
  221. URL.revokeObjectURL(img.src);
  222. }
  223. };
  224. const cspHandler = (err) => {
  225. if (err.blockedURI !== img.src) {
  226. return;
  227. }
  228. unloadHandlersList();
  229. const cspException = new Error(`CSP violation of policy ${err.effectiveDirective} ${err.blockedURI}. Current policy is ${err.originalPolicy}`);
  230. EngineStore.UseFallbackTexture = false;
  231. onErrorHandler(cspException);
  232. if (usingObjectURL && img.src) {
  233. URL.revokeObjectURL(img.src);
  234. }
  235. img.src = "";
  236. };
  237. handlersList.push({ target: img, name: "load", handler: loadHandler });
  238. handlersList.push({ target: img, name: "error", handler: errorHandler });
  239. handlersList.push({ target: document, name: "securitypolicyviolation", handler: cspHandler });
  240. loadHandlersList();
  241. const fromBlob = url.substring(0, 5) === "blob:";
  242. const fromData = url.substring(0, 5) === "data:";
  243. const noOfflineSupport = () => {
  244. if (fromBlob || fromData || !WebRequest.IsCustomRequestAvailable) {
  245. img.src = url;
  246. }
  247. else {
  248. LoadFile(url, (data, _, contentType) => {
  249. const type = !mimeType && contentType ? contentType : mimeType;
  250. const blob = new Blob([data], { type });
  251. const url = URL.createObjectURL(blob);
  252. usingObjectURL = true;
  253. img.src = url;
  254. }, undefined, offlineProvider || undefined, true, (_request, exception) => {
  255. onErrorHandler(exception);
  256. });
  257. }
  258. };
  259. const loadFromOfflineSupport = () => {
  260. if (offlineProvider) {
  261. offlineProvider.loadImage(url, img);
  262. }
  263. };
  264. if (!fromBlob && !fromData && offlineProvider && offlineProvider.enableTexturesOffline) {
  265. offlineProvider.open(loadFromOfflineSupport, noOfflineSupport);
  266. }
  267. else {
  268. if (url.indexOf("file:") !== -1) {
  269. const textureName = decodeURIComponent(url.substring(5).toLowerCase());
  270. if (FilesInputStore.FilesToLoad[textureName] && typeof URL !== "undefined") {
  271. try {
  272. let blobURL;
  273. try {
  274. blobURL = URL.createObjectURL(FilesInputStore.FilesToLoad[textureName]);
  275. }
  276. catch (ex) {
  277. // Chrome doesn't support oneTimeOnly parameter
  278. blobURL = URL.createObjectURL(FilesInputStore.FilesToLoad[textureName]);
  279. }
  280. img.src = blobURL;
  281. usingObjectURL = true;
  282. }
  283. catch (e) {
  284. img.src = "";
  285. }
  286. return img;
  287. }
  288. }
  289. noOfflineSupport();
  290. }
  291. return img;
  292. };
  293. /**
  294. * Reads a file from a File object
  295. * @param file defines the file to load
  296. * @param onSuccess defines the callback to call when data is loaded
  297. * @param onProgress defines the callback to call during loading process
  298. * @param useArrayBuffer defines a boolean indicating that data must be returned as an ArrayBuffer
  299. * @param onError defines the callback to call when an error occurs
  300. * @returns a file request object
  301. * @internal
  302. */
  303. export const ReadFile = (file, onSuccess, onProgress, useArrayBuffer, onError) => {
  304. const reader = new FileReader();
  305. const fileRequest = {
  306. onCompleteObservable: new Observable(),
  307. abort: () => reader.abort(),
  308. };
  309. reader.onloadend = () => fileRequest.onCompleteObservable.notifyObservers(fileRequest);
  310. if (onError) {
  311. reader.onerror = () => {
  312. onError(new ReadFileError(`Unable to read ${file.name}`, file));
  313. };
  314. }
  315. reader.onload = (e) => {
  316. //target doesn't have result from ts 1.3
  317. onSuccess(e.target["result"]);
  318. };
  319. if (onProgress) {
  320. reader.onprogress = onProgress;
  321. }
  322. if (!useArrayBuffer) {
  323. // Asynchronous read
  324. reader.readAsText(file);
  325. }
  326. else {
  327. reader.readAsArrayBuffer(file);
  328. }
  329. return fileRequest;
  330. };
  331. /**
  332. * Loads a file from a url, a data url, or a file url
  333. * @param fileOrUrl file, url, data url, or file url to load
  334. * @param onSuccess callback called when the file successfully loads
  335. * @param onProgress callback called while file is loading (if the server supports this mode)
  336. * @param offlineProvider defines the offline provider for caching
  337. * @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
  338. * @param onError callback called when the file fails to load
  339. * @param onOpened
  340. * @returns a file request object
  341. * @internal
  342. */
  343. // eslint-disable-next-line @typescript-eslint/naming-convention
  344. export const LoadFile = (fileOrUrl, onSuccess, onProgress, offlineProvider, useArrayBuffer, onError, onOpened) => {
  345. if (fileOrUrl.name) {
  346. return ReadFile(fileOrUrl, onSuccess, onProgress, useArrayBuffer, onError
  347. ? (error) => {
  348. onError(undefined, error);
  349. }
  350. : undefined);
  351. }
  352. const url = fileOrUrl;
  353. // If file and file input are set
  354. if (url.indexOf("file:") !== -1) {
  355. let fileName = decodeURIComponent(url.substring(5).toLowerCase());
  356. if (fileName.indexOf("./") === 0) {
  357. fileName = fileName.substring(2);
  358. }
  359. const file = FilesInputStore.FilesToLoad[fileName];
  360. if (file) {
  361. return ReadFile(file, onSuccess, onProgress, useArrayBuffer, onError ? (error) => onError(undefined, new LoadFileError(error.message, error.file)) : undefined);
  362. }
  363. }
  364. // For a Base64 Data URL
  365. const { match, type } = TestBase64DataUrl(url);
  366. if (match) {
  367. const fileRequest = {
  368. onCompleteObservable: new Observable(),
  369. abort: () => () => { },
  370. };
  371. try {
  372. const data = useArrayBuffer ? DecodeBase64UrlToBinary(url) : DecodeBase64UrlToString(url);
  373. onSuccess(data, undefined, type);
  374. }
  375. catch (error) {
  376. if (onError) {
  377. onError(undefined, error);
  378. }
  379. else {
  380. Logger.Error(error.message || "Failed to parse the Data URL");
  381. }
  382. }
  383. TimingTools.SetImmediate(() => {
  384. fileRequest.onCompleteObservable.notifyObservers(fileRequest);
  385. });
  386. return fileRequest;
  387. }
  388. return RequestFile(url, (data, request) => {
  389. onSuccess(data, request?.responseURL, request?.getResponseHeader("content-type"));
  390. }, onProgress, offlineProvider, useArrayBuffer, onError
  391. ? (error) => {
  392. onError(error.request, new LoadFileError(error.message, error.request));
  393. }
  394. : undefined, onOpened);
  395. };
  396. /**
  397. * Loads a file from a url
  398. * @param url url to load
  399. * @param onSuccess callback called when the file successfully loads
  400. * @param onProgress callback called while file is loading (if the server supports this mode)
  401. * @param offlineProvider defines the offline provider for caching
  402. * @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
  403. * @param onError callback called when the file fails to load
  404. * @param onOpened callback called when the web request is opened
  405. * @returns a file request object
  406. * @internal
  407. */
  408. export const RequestFile = (url, onSuccess, onProgress, offlineProvider, useArrayBuffer, onError, onOpened) => {
  409. url = _CleanUrl(url);
  410. url = FileToolsOptions.PreprocessUrl(url);
  411. const loadUrl = FileToolsOptions.BaseUrl + url;
  412. let aborted = false;
  413. const fileRequest = {
  414. onCompleteObservable: new Observable(),
  415. abort: () => (aborted = true),
  416. };
  417. const requestFile = () => {
  418. let request = new WebRequest();
  419. let retryHandle = null;
  420. let onReadyStateChange;
  421. const unbindEvents = () => {
  422. if (!request) {
  423. return;
  424. }
  425. if (onProgress) {
  426. request.removeEventListener("progress", onProgress);
  427. }
  428. if (onReadyStateChange) {
  429. request.removeEventListener("readystatechange", onReadyStateChange);
  430. }
  431. request.removeEventListener("loadend", onLoadEnd);
  432. };
  433. let onLoadEnd = () => {
  434. unbindEvents();
  435. fileRequest.onCompleteObservable.notifyObservers(fileRequest);
  436. fileRequest.onCompleteObservable.clear();
  437. onProgress = undefined;
  438. onReadyStateChange = null;
  439. onLoadEnd = null;
  440. onError = undefined;
  441. onOpened = undefined;
  442. onSuccess = undefined;
  443. };
  444. fileRequest.abort = () => {
  445. aborted = true;
  446. if (onLoadEnd) {
  447. onLoadEnd();
  448. }
  449. if (request && request.readyState !== (XMLHttpRequest.DONE || 4)) {
  450. request.abort();
  451. }
  452. if (retryHandle !== null) {
  453. clearTimeout(retryHandle);
  454. retryHandle = null;
  455. }
  456. request = null;
  457. };
  458. const handleError = (error) => {
  459. const message = error.message || "Unknown error";
  460. if (onError && request) {
  461. onError(new RequestFileError(message, request));
  462. }
  463. else {
  464. Logger.Error(message);
  465. }
  466. };
  467. const retryLoop = (retryIndex) => {
  468. if (!request) {
  469. return;
  470. }
  471. request.open("GET", loadUrl);
  472. if (onOpened) {
  473. try {
  474. onOpened(request);
  475. }
  476. catch (e) {
  477. handleError(e);
  478. return;
  479. }
  480. }
  481. if (useArrayBuffer) {
  482. request.responseType = "arraybuffer";
  483. }
  484. if (onProgress) {
  485. request.addEventListener("progress", onProgress);
  486. }
  487. if (onLoadEnd) {
  488. request.addEventListener("loadend", onLoadEnd);
  489. }
  490. onReadyStateChange = () => {
  491. if (aborted || !request) {
  492. return;
  493. }
  494. // In case of undefined state in some browsers.
  495. if (request.readyState === (XMLHttpRequest.DONE || 4)) {
  496. // Some browsers have issues where onreadystatechange can be called multiple times with the same value.
  497. if (onReadyStateChange) {
  498. request.removeEventListener("readystatechange", onReadyStateChange);
  499. }
  500. if ((request.status >= 200 && request.status < 300) || (request.status === 0 && (!IsWindowObjectExist() || IsFileURL()))) {
  501. try {
  502. if (onSuccess) {
  503. onSuccess(useArrayBuffer ? request.response : request.responseText, request);
  504. }
  505. }
  506. catch (e) {
  507. handleError(e);
  508. }
  509. return;
  510. }
  511. const retryStrategy = FileToolsOptions.DefaultRetryStrategy;
  512. if (retryStrategy) {
  513. const waitTime = retryStrategy(loadUrl, request, retryIndex);
  514. if (waitTime !== -1) {
  515. // Prevent the request from completing for retry.
  516. unbindEvents();
  517. request = new WebRequest();
  518. retryHandle = setTimeout(() => retryLoop(retryIndex + 1), waitTime);
  519. return;
  520. }
  521. }
  522. const error = new RequestFileError("Error status: " + request.status + " " + request.statusText + " - Unable to load " + loadUrl, request);
  523. if (onError) {
  524. onError(error);
  525. }
  526. }
  527. };
  528. request.addEventListener("readystatechange", onReadyStateChange);
  529. request.send();
  530. };
  531. retryLoop(0);
  532. };
  533. // Caching all files
  534. if (offlineProvider && offlineProvider.enableSceneOffline) {
  535. const noOfflineSupport = (request) => {
  536. if (request && request.status > 400) {
  537. if (onError) {
  538. onError(request);
  539. }
  540. }
  541. else {
  542. requestFile();
  543. }
  544. };
  545. const loadFromOfflineSupport = () => {
  546. // TODO: database needs to support aborting and should return a IFileRequest
  547. if (offlineProvider) {
  548. offlineProvider.loadFile(FileToolsOptions.BaseUrl + url, (data) => {
  549. if (!aborted && onSuccess) {
  550. onSuccess(data);
  551. }
  552. fileRequest.onCompleteObservable.notifyObservers(fileRequest);
  553. }, onProgress
  554. ? (event) => {
  555. if (!aborted && onProgress) {
  556. onProgress(event);
  557. }
  558. }
  559. : undefined, noOfflineSupport, useArrayBuffer);
  560. }
  561. };
  562. offlineProvider.open(loadFromOfflineSupport, noOfflineSupport);
  563. }
  564. else {
  565. requestFile();
  566. }
  567. return fileRequest;
  568. };
  569. /**
  570. * Checks if the loaded document was accessed via `file:`-Protocol.
  571. * @returns boolean
  572. * @internal
  573. */
  574. export const IsFileURL = () => {
  575. return typeof location !== "undefined" && location.protocol === "file:";
  576. };
  577. /**
  578. * Test if the given uri is a valid base64 data url
  579. * @param uri The uri to test
  580. * @returns True if the uri is a base64 data url or false otherwise
  581. * @internal
  582. */
  583. export const IsBase64DataUrl = (uri) => {
  584. return Base64DataUrlRegEx.test(uri);
  585. };
  586. export const TestBase64DataUrl = (uri) => {
  587. const results = Base64DataUrlRegEx.exec(uri);
  588. if (results === null || results.length === 0) {
  589. return { match: false, type: "" };
  590. }
  591. else {
  592. const type = results[0].replace("data:", "").replace("base64,", "");
  593. return { match: true, type };
  594. }
  595. };
  596. /**
  597. * Decode the given base64 uri.
  598. * @param uri The uri to decode
  599. * @returns The decoded base64 data.
  600. * @internal
  601. */
  602. export function DecodeBase64UrlToBinary(uri) {
  603. return DecodeBase64ToBinary(uri.split(",")[1]);
  604. }
  605. /**
  606. * Decode the given base64 uri into a UTF-8 encoded string.
  607. * @param uri The uri to decode
  608. * @returns The decoded base64 data.
  609. * @internal
  610. */
  611. export const DecodeBase64UrlToString = (uri) => {
  612. return DecodeBase64ToString(uri.split(",")[1]);
  613. };
  614. /**
  615. * This will be executed automatically for UMD and es5.
  616. * If esm dev wants the side effects to execute they will have to run it manually
  617. * Once we build native modules those need to be exported.
  618. * @internal
  619. */
  620. const initSideEffects = () => {
  621. AbstractEngine._FileToolsLoadImage = LoadImage;
  622. AbstractEngine._FileToolsLoadFile = LoadFile;
  623. ShaderProcessor._FileToolsLoadFile = LoadFile;
  624. };
  625. initSideEffects();
  626. // deprecated
  627. /**
  628. * FileTools defined as any.
  629. * This should not be imported or used in future releases or in any module in the framework
  630. * @internal
  631. * @deprecated import the needed function from fileTools.ts
  632. */
  633. export let FileTools;
  634. /**
  635. * @param DecodeBase64UrlToBinary
  636. * @param DecodeBase64UrlToString
  637. * @param FileToolsOptions
  638. * @internal
  639. */
  640. export const _injectLTSFileTools = (DecodeBase64UrlToBinary, DecodeBase64UrlToString, FileToolsOptions, IsBase64DataUrl, IsFileURL, LoadFile, LoadImage, ReadFile, RequestFile, SetCorsBehavior) => {
  641. /**
  642. * Backwards compatibility.
  643. * @internal
  644. * @deprecated
  645. */
  646. FileTools = {
  647. DecodeBase64UrlToBinary,
  648. DecodeBase64UrlToString,
  649. DefaultRetryStrategy: FileToolsOptions.DefaultRetryStrategy,
  650. BaseUrl: FileToolsOptions.BaseUrl,
  651. CorsBehavior: FileToolsOptions.CorsBehavior,
  652. PreprocessUrl: FileToolsOptions.PreprocessUrl,
  653. IsBase64DataUrl,
  654. IsFileURL,
  655. LoadFile,
  656. LoadImage,
  657. ReadFile,
  658. RequestFile,
  659. SetCorsBehavior,
  660. };
  661. Object.defineProperty(FileTools, "DefaultRetryStrategy", {
  662. get: function () {
  663. return FileToolsOptions.DefaultRetryStrategy;
  664. },
  665. set: function (value) {
  666. FileToolsOptions.DefaultRetryStrategy = value;
  667. },
  668. });
  669. Object.defineProperty(FileTools, "BaseUrl", {
  670. get: function () {
  671. return FileToolsOptions.BaseUrl;
  672. },
  673. set: function (value) {
  674. FileToolsOptions.BaseUrl = value;
  675. },
  676. });
  677. Object.defineProperty(FileTools, "PreprocessUrl", {
  678. get: function () {
  679. return FileToolsOptions.PreprocessUrl;
  680. },
  681. set: function (value) {
  682. FileToolsOptions.PreprocessUrl = value;
  683. },
  684. });
  685. Object.defineProperty(FileTools, "CorsBehavior", {
  686. get: function () {
  687. return FileToolsOptions.CorsBehavior;
  688. },
  689. set: function (value) {
  690. FileToolsOptions.CorsBehavior = value;
  691. },
  692. });
  693. };
  694. _injectLTSFileTools(DecodeBase64UrlToBinary, DecodeBase64UrlToString, FileToolsOptions, IsBase64DataUrl, IsFileURL, LoadFile, LoadImage, ReadFile, RequestFile, SetCorsBehavior);
  695. //# sourceMappingURL=fileTools.js.map