@tauri-apps/plugin-shell
Advanced tools
Comparing version 2.0.0-alpha.0 to 2.0.0-alpha.1
/** | ||
* Access the system shell. | ||
* Allows you to spawn child processes and manage files and URLs using their default application. | ||
* | ||
* ## Security | ||
* | ||
* This API has a scope configuration that forces you to restrict the programs and arguments that can be used. | ||
* | ||
* ### Restricting access to the {@link open | `open`} API | ||
* | ||
* On the configuration object, `open: true` means that the {@link open} API can be used with any URL, | ||
* as the argument is validated with the `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+` regex. | ||
* You can change that regex by changing the boolean value to a string, e.g. `open: ^https://github.com/`. | ||
* | ||
* ### Restricting access to the {@link Command | `Command`} APIs | ||
* | ||
* The plugin configuration object has a `scope` field that defines an array of CLIs that can be used. | ||
* Each CLI is a configuration object `{ name: string, cmd: string, sidecar?: bool, args?: boolean | Arg[] }`. | ||
* | ||
* - `name`: the unique identifier of the command, passed to the {@link Command.create | Command.create function}. | ||
* If it's a sidecar, this must be the value defined on `tauri.conf.json > tauri > bundle > externalBin`. | ||
* - `cmd`: the program that is executed on this configuration. If it's a sidecar, this value is ignored. | ||
* - `sidecar`: whether the object configures a sidecar or a system program. | ||
* - `args`: the arguments that can be passed to the program. By default no arguments are allowed. | ||
* - `true` means that any argument list is allowed. | ||
* - `false` means that no arguments are allowed. | ||
* - otherwise an array can be configured. Each item is either a string representing the fixed argument value | ||
* or a `{ validator: string }` that defines a regex validating the argument value. | ||
* | ||
* #### Example scope configuration | ||
* | ||
* CLI: `git commit -m "the commit message"` | ||
* | ||
* Configuration: | ||
* ```json | ||
* { | ||
* "plugins": { | ||
* "shell": { | ||
* "scope": [ | ||
* { | ||
* "name": "run-git-commit", | ||
* "cmd": "git", | ||
* "args": ["commit", "-m", { "validator": "\\S+" }] | ||
* } | ||
* ] | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* Usage: | ||
* ```typescript | ||
* import { Command } from '@tauri-apps/plugin-shell' | ||
* Command.create('run-git-commit', ['commit', '-m', 'the commit message']) | ||
* ``` | ||
* | ||
* Trying to execute any API with a program not configured on the scope results in a promise rejection due to denied access. | ||
* | ||
* @module | ||
*/ | ||
declare global { | ||
interface Window { | ||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>; | ||
__TAURI__: { | ||
transformCallback: <T>(cb: (payload: T) => void) => number; | ||
}; | ||
} | ||
} | ||
/** | ||
* @since 2.0.0 | ||
@@ -70,0 +3,0 @@ */ |
@@ -0,1 +1,5 @@ | ||
var f$1=Object.defineProperty;var g=(a,b)=>{for(var c in b)f$1(a,c,{get:b[c],enumerable:!0});};var e=(a,b,c)=>{if(!b.has(a))throw TypeError("Cannot "+c)};var h$1=(a,b,c)=>(e(a,b,"read from private field"),c?c.call(a):b.get(a)),i$1=(a,b,c)=>{if(b.has(a))throw TypeError("Cannot add the same private member more than once");b instanceof WeakSet?b.add(a):b.set(a,c);},j=(a,b,c,d)=>(e(a,b,"write to private field"),d?d.call(a,c):b.set(a,c),c); | ||
var h={};g(h,{Channel:()=>o,PluginListener:()=>a,addPluginListener:()=>m,convertFileSrc:()=>y,invoke:()=>u,transformCallback:()=>s});function f(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function s(n,e=!1){let t=f(),r=`_${t}`;return Object.defineProperty(window,r,{value:c=>(e&&Reflect.deleteProperty(window,r),n?.(c)),writable:!1,configurable:!0}),t}var i,o=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0;i$1(this,i,()=>{});this.id=s(e=>{h$1(this,i).call(this,e);});}set onmessage(e){j(this,i,e);}get onmessage(){return h$1(this,i)}toJSON(){return `__CHANNEL__:${this.id}`}};i=new WeakMap;var a=class{constructor(e,t,r){this.plugin=e,this.event=t,this.channelId=r;}async unregister(){return u(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function m(n,e,t){let r=new o;return r.onmessage=t,u(`plugin:${n}|register_listener`,{event:e,handler:r}).then(()=>new a(n,e,r.id))}async function u(n,e={},t){return new Promise((r,c)=>{let g=s(d=>{r(d),Reflect.deleteProperty(window,`_${_}`);},!0),_=s(d=>{c(d),Reflect.deleteProperty(window,`_${g}`);},!0);window.__TAURI_IPC__({cmd:n,callback:g,error:_,payload:e,options:t});})}function y(n,e="asset"){return window.__TAURI__.convertFileSrc(n,e)} | ||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy | ||
@@ -5,2 +9,61 @@ // SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Access the system shell. | ||
* Allows you to spawn child processes and manage files and URLs using their default application. | ||
* | ||
* ## Security | ||
* | ||
* This API has a scope configuration that forces you to restrict the programs and arguments that can be used. | ||
* | ||
* ### Restricting access to the {@link open | `open`} API | ||
* | ||
* On the configuration object, `open: true` means that the {@link open} API can be used with any URL, | ||
* as the argument is validated with the `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+` regex. | ||
* You can change that regex by changing the boolean value to a string, e.g. `open: ^https://github.com/`. | ||
* | ||
* ### Restricting access to the {@link Command | `Command`} APIs | ||
* | ||
* The plugin configuration object has a `scope` field that defines an array of CLIs that can be used. | ||
* Each CLI is a configuration object `{ name: string, cmd: string, sidecar?: bool, args?: boolean | Arg[] }`. | ||
* | ||
* - `name`: the unique identifier of the command, passed to the {@link Command.create | Command.create function}. | ||
* If it's a sidecar, this must be the value defined on `tauri.conf.json > tauri > bundle > externalBin`. | ||
* - `cmd`: the program that is executed on this configuration. If it's a sidecar, this value is ignored. | ||
* - `sidecar`: whether the object configures a sidecar or a system program. | ||
* - `args`: the arguments that can be passed to the program. By default no arguments are allowed. | ||
* - `true` means that any argument list is allowed. | ||
* - `false` means that no arguments are allowed. | ||
* - otherwise an array can be configured. Each item is either a string representing the fixed argument value | ||
* or a `{ validator: string }` that defines a regex validating the argument value. | ||
* | ||
* #### Example scope configuration | ||
* | ||
* CLI: `git commit -m "the commit message"` | ||
* | ||
* Configuration: | ||
* ```json | ||
* { | ||
* "plugins": { | ||
* "shell": { | ||
* "scope": [ | ||
* { | ||
* "name": "run-git-commit", | ||
* "cmd": "git", | ||
* "args": ["commit", "-m", { "validator": "\\S+" }] | ||
* } | ||
* ] | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* Usage: | ||
* ```typescript | ||
* import { Command } from '@tauri-apps/plugin-shell' | ||
* Command.create('run-git-commit', ['commit', '-m', 'the commit message']) | ||
* ``` | ||
* | ||
* Trying to execute any API with a program not configured on the scope results in a promise rejection due to denied access. | ||
* | ||
* @module | ||
*/ | ||
/** | ||
* Spawns a process. | ||
@@ -10,3 +73,3 @@ * | ||
* @param program The name of the scoped command. | ||
* @param onEvent Event handler. | ||
* @param onEventHandler Event handler. | ||
* @param args Program arguments. | ||
@@ -18,11 +81,13 @@ * @param options Configuration for the process spawn. | ||
*/ | ||
async function execute(onEvent, program, args = [], options) { | ||
async function execute(onEventHandler, program, args = [], options) { | ||
if (typeof args === "object") { | ||
Object.freeze(args); | ||
} | ||
return window.__TAURI_INVOKE__("plugin:shell|execute", { | ||
const onEvent = new o(); | ||
onEvent.onmessage = onEventHandler; | ||
return u("plugin:shell|execute", { | ||
program, | ||
args, | ||
options, | ||
onEventFn: window.__TAURI__.transformCallback(onEvent), | ||
onEvent, | ||
}); | ||
@@ -219,3 +284,3 @@ } | ||
async write(data) { | ||
return window.__TAURI_INVOKE__("plugin:shell|stdin_write", { | ||
return u("plugin:shell|stdin_write", { | ||
pid: this.pid, | ||
@@ -234,3 +299,3 @@ // correctly serialize Uint8Arrays | ||
async kill() { | ||
return window.__TAURI_INVOKE__("plugin:shell|kill", { | ||
return u("plugin:shell|kill", { | ||
cmd: "killChild", | ||
@@ -416,3 +481,3 @@ pid: this.pid, | ||
async function open(path, openWith) { | ||
return window.__TAURI_INVOKE__("plugin:shell|open", { | ||
return u("plugin:shell|open", { | ||
path, | ||
@@ -419,0 +484,0 @@ with: openWith, |
{ | ||
"name": "@tauri-apps/plugin-shell", | ||
"version": "2.0.0-alpha.0", | ||
"version": "2.0.0-alpha.1", | ||
"license": "MIT or APACHE-2.0", | ||
@@ -27,3 +27,3 @@ "authors": [ | ||
"dependencies": { | ||
"@tauri-apps/api": "2.0.0-alpha.4" | ||
"@tauri-apps/api": "2.0.0-alpha.6" | ||
}, | ||
@@ -30,0 +30,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
# Shell | ||
![plugin-shell](https://github.com/tauri-apps/plugins-workspace/raw/v2/plugins/shell/banner.png) | ||
@@ -3,0 +3,0 @@ Access the system shell. Allows you to spawn child processes and manage files and URLs using their default application. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49068
1243
+ Added@tauri-apps/api@2.0.0-alpha.6(transitive)
- Removed@tauri-apps/api@2.0.0-alpha.4(transitive)