tauri.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /** @ignore */
  5. function uid() {
  6. return window.crypto.getRandomValues(new Uint32Array(1))[0];
  7. }
  8. /**
  9. * Transforms a callback function to a string identifier that can be passed to the backend.
  10. * The backend uses the identifier to `eval()` the callback.
  11. *
  12. * @return A unique identifier associated with the callback function.
  13. *
  14. * @since 1.0.0
  15. */
  16. function transformCallback(callback, once = false) {
  17. const identifier = uid();
  18. const prop = `_${identifier}`;
  19. Object.defineProperty(window, prop, {
  20. value: (result) => {
  21. if (once) {
  22. Reflect.deleteProperty(window, prop);
  23. }
  24. return callback === null || callback === void 0 ? void 0 : callback(result);
  25. },
  26. writable: false,
  27. configurable: true
  28. });
  29. return identifier;
  30. }
  31. /**
  32. * Sends a message to the backend.
  33. * @example
  34. * ```typescript
  35. * import { invoke } from '@tauri-apps/api/tauri';
  36. * await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });
  37. * ```
  38. *
  39. * @param cmd The command name.
  40. * @param args The optional arguments to pass to the command.
  41. * @return A promise resolving or rejecting to the backend response.
  42. *
  43. * @since 1.0.0
  44. */
  45. async function invoke(cmd, args = {}) {
  46. return new Promise((resolve, reject) => {
  47. const callback = transformCallback((e) => {
  48. resolve(e);
  49. Reflect.deleteProperty(window, `_${error}`);
  50. }, true);
  51. const error = transformCallback((e) => {
  52. reject(e);
  53. Reflect.deleteProperty(window, `_${callback}`);
  54. }, true);
  55. window.__TAURI_IPC__({
  56. cmd,
  57. callback,
  58. error,
  59. ...args
  60. });
  61. });
  62. }
  63. /**
  64. * Convert a device file path to an URL that can be loaded by the webview.
  65. * Note that `asset:` and `https://asset.localhost` must be added to [`tauri.security.csp`](https://tauri.app/v1/api/config/#securityconfig.csp) in `tauri.conf.json`.
  66. * Example CSP value: `"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"` to use the asset protocol on image sources.
  67. *
  68. * Additionally, `asset` must be added to [`tauri.allowlist.protocol`](https://tauri.app/v1/api/config/#allowlistconfig.protocol)
  69. * in `tauri.conf.json` and its access scope must be defined on the `assetScope` array on the same `protocol` object.
  70. * For example:
  71. * ```json
  72. * {
  73. * "tauri": {
  74. * "allowlist": {
  75. * "protocol": {
  76. * "asset": true,
  77. * "assetScope": ["$APPDATA/assets/*"]
  78. * }
  79. * }
  80. * }
  81. * }
  82. * ```
  83. *
  84. * @param filePath The file path.
  85. * @param protocol The protocol to use. Defaults to `asset`. You only need to set this when using a custom protocol.
  86. * @example
  87. * ```typescript
  88. * import { appDataDir, join } from '@tauri-apps/api/path';
  89. * import { convertFileSrc } from '@tauri-apps/api/tauri';
  90. * const appDataDirPath = await appDataDir();
  91. * const filePath = await join(appDataDirPath, 'assets/video.mp4');
  92. * const assetUrl = convertFileSrc(filePath);
  93. *
  94. * const video = document.getElementById('my-video');
  95. * const source = document.createElement('source');
  96. * source.type = 'video/mp4';
  97. * source.src = assetUrl;
  98. * video.appendChild(source);
  99. * video.load();
  100. * ```
  101. *
  102. * @return the URL that can be used as source on the webview.
  103. *
  104. * @since 1.0.0
  105. */
  106. function convertFileSrc(filePath, protocol = 'asset') {
  107. return window.__TAURI__.convertFileSrc(filePath, protocol);
  108. }
  109. export { convertFileSrc, invoke, transformCallback };