File size: 10,709 Bytes
adb87a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | import { __classPrivateFieldSet, __classPrivateFieldGet } from './external/tslib/tslib.es6.js';
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
var _Channel_onmessage, _Channel_nextMessageIndex, _Channel_pendingMessages, _Channel_messageEndIndex, _Resource_rid;
/**
* Invoke your custom commands.
*
* This package is also accessible with `window.__TAURI__.core` when [`app.withGlobalTauri`](https://v2.tauri.app/reference/config/#withglobaltauri) in `tauri.conf.json` is set to `true`.
* @module
*/
/**
* A key to be used to implement a special function
* on your types that define how your type should be serialized
* when passing across the IPC.
* @example
* Given a type in Rust that looks like this
* ```rs
* #[derive(serde::Serialize, serde::Deserialize)
* enum UserId {
* String(String),
* Number(u32),
* }
* ```
* `UserId::String("id")` would be serialized into `{ String: "id" }`
* and so we need to pass the same structure back to Rust
* ```ts
* import { SERIALIZE_TO_IPC_FN } from "@tauri-apps/api/core"
*
* class UserIdString {
* id
* constructor(id) {
* this.id = id
* }
*
* [SERIALIZE_TO_IPC_FN]() {
* return { String: this.id }
* }
* }
*
* class UserIdNumber {
* id
* constructor(id) {
* this.id = id
* }
*
* [SERIALIZE_TO_IPC_FN]() {
* return { Number: this.id }
* }
* }
*
* type UserId = UserIdString | UserIdNumber
* ```
*
*/
// if this value changes, make sure to update it in:
// 1. ipc.js
// 2. process-ipc-message-fn.js
const SERIALIZE_TO_IPC_FN = '__TAURI_TO_IPC_KEY__';
/**
* Stores the callback in a known location, and returns an identifier that can be passed to the backend.
* The backend uses the identifier to `eval()` the callback.
*
* @return An unique identifier associated with the callback function.
*
* @since 1.0.0
*/
function transformCallback(
// TODO: Make this not optional in v3
callback, once = false) {
return window.__TAURI_INTERNALS__.transformCallback(callback, once);
}
class Channel {
constructor(onmessage) {
_Channel_onmessage.set(this, void 0);
// the index is used as a mechanism to preserve message order
_Channel_nextMessageIndex.set(this, 0);
_Channel_pendingMessages.set(this, []);
_Channel_messageEndIndex.set(this, void 0);
__classPrivateFieldSet(this, _Channel_onmessage, onmessage || (() => { }), "f");
this.id = transformCallback((rawMessage) => {
const index = rawMessage.index;
if ('end' in rawMessage) {
if (index == __classPrivateFieldGet(this, _Channel_nextMessageIndex, "f")) {
this.cleanupCallback();
}
else {
__classPrivateFieldSet(this, _Channel_messageEndIndex, index, "f");
}
return;
}
const message = rawMessage.message;
// Process the message if we're at the right order
if (index == __classPrivateFieldGet(this, _Channel_nextMessageIndex, "f")) {
__classPrivateFieldGet(this, _Channel_onmessage, "f").call(this, message);
__classPrivateFieldSet(this, _Channel_nextMessageIndex, __classPrivateFieldGet(this, _Channel_nextMessageIndex, "f") + 1, "f");
// process pending messages
while (__classPrivateFieldGet(this, _Channel_nextMessageIndex, "f") in __classPrivateFieldGet(this, _Channel_pendingMessages, "f")) {
const message = __classPrivateFieldGet(this, _Channel_pendingMessages, "f")[__classPrivateFieldGet(this, _Channel_nextMessageIndex, "f")];
__classPrivateFieldGet(this, _Channel_onmessage, "f").call(this, message);
// eslint-disable-next-line @typescript-eslint/no-array-delete
delete __classPrivateFieldGet(this, _Channel_pendingMessages, "f")[__classPrivateFieldGet(this, _Channel_nextMessageIndex, "f")];
__classPrivateFieldSet(this, _Channel_nextMessageIndex, __classPrivateFieldGet(this, _Channel_nextMessageIndex, "f") + 1, "f");
}
if (__classPrivateFieldGet(this, _Channel_nextMessageIndex, "f") === __classPrivateFieldGet(this, _Channel_messageEndIndex, "f")) {
this.cleanupCallback();
}
}
// Queue the message if we're not
else {
// eslint-disable-next-line security/detect-object-injection
__classPrivateFieldGet(this, _Channel_pendingMessages, "f")[index] = message;
}
});
}
cleanupCallback() {
window.__TAURI_INTERNALS__.unregisterCallback(this.id);
}
set onmessage(handler) {
__classPrivateFieldSet(this, _Channel_onmessage, handler, "f");
}
get onmessage() {
return __classPrivateFieldGet(this, _Channel_onmessage, "f");
}
[(_Channel_onmessage = new WeakMap(), _Channel_nextMessageIndex = new WeakMap(), _Channel_pendingMessages = new WeakMap(), _Channel_messageEndIndex = new WeakMap(), SERIALIZE_TO_IPC_FN)]() {
return `__CHANNEL__:${this.id}`;
}
toJSON() {
// eslint-disable-next-line security/detect-object-injection
return this[SERIALIZE_TO_IPC_FN]();
}
}
class PluginListener {
constructor(plugin, event, channelId) {
this.plugin = plugin;
this.event = event;
this.channelId = channelId;
}
async unregister() {
return invoke(`plugin:${this.plugin}|remove_listener`, {
event: this.event,
channelId: this.channelId
});
}
}
/**
* Adds a listener to a plugin event.
*
* @returns The listener object to stop listening to the events.
*
* @since 2.0.0
*/
async function addPluginListener(plugin, event, cb) {
const handler = new Channel(cb);
try {
await invoke(`plugin:${plugin}|register_listener`, {
event,
handler
});
return new PluginListener(plugin, event, handler.id);
}
catch {
// TODO(v3): remove this fallback
// note: we must try with camelCase here for backwards compatibility
await invoke(`plugin:${plugin}|registerListener`, { event, handler });
return new PluginListener(plugin, event, handler.id);
}
}
/**
* Get permission state for a plugin.
*
* This should be used by plugin authors to wrap their actual implementation.
*/
async function checkPermissions(plugin) {
return invoke(`plugin:${plugin}|check_permissions`);
}
/**
* Request permissions.
*
* This should be used by plugin authors to wrap their actual implementation.
*/
async function requestPermissions(plugin) {
return invoke(`plugin:${plugin}|request_permissions`);
}
/**
* Sends a message to the backend.
* @example
* ```typescript
* import { invoke } from '@tauri-apps/api/core';
* await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });
* ```
*
* @param cmd The command name.
* @param args The optional arguments to pass to the command.
* @param options The request options.
* @return A promise resolving or rejecting to the backend response.
*
* @since 1.0.0
*/
async function invoke(cmd, args = {}, options) {
return window.__TAURI_INTERNALS__.invoke(cmd, args, options);
}
/**
* Convert a device file path to an URL that can be loaded by the webview.
* Note that `asset:` and `http://asset.localhost` must be added to [`app.security.csp`](https://v2.tauri.app/reference/config/#csp-1) in `tauri.conf.json`.
* Example CSP value: `"csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost"` to use the asset protocol on image sources.
*
* Additionally, `"enable" : "true"` must be added to [`app.security.assetProtocol`](https://v2.tauri.app/reference/config/#assetprotocolconfig)
* in `tauri.conf.json` and its access scope must be defined on the `scope` array on the same `assetProtocol` object.
*
* @param filePath The file path.
* @param protocol The protocol to use. Defaults to `asset`. You only need to set this when using a custom protocol.
* @example
* ```typescript
* import { appDataDir, join } from '@tauri-apps/api/path';
* import { convertFileSrc } from '@tauri-apps/api/core';
* const appDataDirPath = await appDataDir();
* const filePath = await join(appDataDirPath, 'assets/video.mp4');
* const assetUrl = convertFileSrc(filePath);
*
* const video = document.getElementById('my-video');
* const source = document.createElement('source');
* source.type = 'video/mp4';
* source.src = assetUrl;
* video.appendChild(source);
* video.load();
* ```
*
* @return the URL that can be used as source on the webview.
*
* @since 1.0.0
*/
function convertFileSrc(filePath, protocol = 'asset') {
return window.__TAURI_INTERNALS__.convertFileSrc(filePath, protocol);
}
/**
* A rust-backed resource stored through `tauri::Manager::resources_table` API.
*
* The resource lives in the main process and does not exist
* in the Javascript world, and thus will not be cleaned up automatically
* except on application exit. If you want to clean it up early, call {@linkcode Resource.close}
*
* @example
* ```typescript
* import { Resource, invoke } from '@tauri-apps/api/core';
* export class DatabaseHandle extends Resource {
* static async open(path: string): Promise<DatabaseHandle> {
* const rid: number = await invoke('open_db', { path });
* return new DatabaseHandle(rid);
* }
*
* async execute(sql: string): Promise<void> {
* await invoke('execute_sql', { rid: this.rid, sql });
* }
* }
* ```
*/
class Resource {
get rid() {
return __classPrivateFieldGet(this, _Resource_rid, "f");
}
constructor(rid) {
_Resource_rid.set(this, void 0);
__classPrivateFieldSet(this, _Resource_rid, rid, "f");
}
/**
* Destroys and cleans up this resource from memory.
* **You should not call any method on this object anymore and should drop any reference to it.**
*/
async close() {
return invoke('plugin:resources|close', {
rid: this.rid
});
}
}
_Resource_rid = new WeakMap();
function isTauri() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
return !!(globalThis || window).isTauri;
}
export { Channel, PluginListener, Resource, SERIALIZE_TO_IPC_FN, addPluginListener, checkPermissions, convertFileSrc, invoke, isTauri, requestPermissions, transformCallback };
|