tauri.cjs 3.7 KB

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