1 Star 0 Fork 6

websalon / trzsz.js

forked from trzsz / trzsz.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

trzsz.js

Making webshell and terminal supports trzsz ( trz / tsz ), which similar to ( rz / sz ), and compatible with tmux.

Website: https://trzsz.github.io/js  中文文档:https://trzsz.github.io/cn/js

MIT License npmjs trzsz

Please check https://trzsz.github.io for more information of trzsz.

trzsz.js is the js version of trzsz, supports webshell running in browser, terminal built with electron, etc.

Getting Started

  • Install the module

    npm install trzsz

    or

    yarn add trzsz
  • Use in Node.js

    import { TrzszFilter } from "trzsz";

    or

    const { TrzszFilter } = require("trzsz");
  • Use in browser

    <script src="node_modules/trzsz/lib/trzsz.js"></script>
  • Create TrzszFilter object for each connection.

    const trzszFilter = new TrzszFilter({
      // The trzsz options, see below
    });
  • Generally, the output of the server is forwarded to the terminal. Pass the output through TrzszFilter.

    const trzszFilter = new TrzszFilter({
      // The output will be forwarded back by TrzszFilter, unless the user runs ( trz / tsz ) on the server.
      writeToTerminal: (data) => terminal.write(typeof data === "string" ? data : new Uint8Array(data)),
    });
    
    // forward the output to TrzszFilter
    webSocket.addEventListener("message", (ev) => trzszFilter.processServerOutput(ev.data));
  • Generally, the user input is forwarded to the server. Pass the user input through TrzszFilter.

    const trzszFilter = new TrzszFilter({
      // The user input will be forwarded back by TrzszFilter, unless there are files being transferred.
      sendToServer: (data) => webSocket.send(data),
    });
    
    // forward the user input to TrzszFilter
    terminal.onData((data) => trzszFilter.processTerminalInput(data));
    // forward binary input to TrzszFilter
    terminal.onBinary((data) => trzszFilter.processBinaryInput(data));
  • Let TrzszFilter know the terminal columns for rendering progress bar.

    const trzszFilter = new TrzszFilter({
      // initialize the terminal columns
      terminalColumns: terminal.cols,
    });
    
    // reset the terminal columns
    terminal.onResize((size) => trzszFilter.setTerminalColumns(size.cols));
  • If the remote server is a Windows shell, such as cmd and PowerShell.

    const trzszFilter = new TrzszFilter({
      // There is a windows shell
      isWindowsShell: true,
    });
  • If running in Node.js and TrzszFilter can require('fs'), chooseSendFiles and chooseSaveDirectory are required. If running in web browser, they will be ignored. Note that they are async functions.

    const trzszFilter = new TrzszFilter({
      // call on the user runs trz ( upload files ) on the server and no error on require('fs').
      chooseSendFiles: async (directory) => {
        // if `directory` is `true`, allow to choose multiple directories and files.
        // otherwise, only allow to choose multiple files.
        // return `undefined` if the user cancels.
        // return an array of file paths choosed by the user.
        return ["/path/to/file1", "/path/to/file2", "/path/to/directory3"];
      },
      // call on the user runs tsz ( download files ) on the server and no error on require('fs').
      chooseSaveDirectory: async () => {
        // return `undefined` if the user cancels.
        // return a directory path choosed by the user.
        return "/path/to/directory";
      },
    });
  • Support dragging files or directories to upload.

    terminalHtmlElement.addEventListener("dragover", (event) => event.preventDefault());
    terminalHtmlElement.addEventListener("drop", (event) => {
      event.preventDefault();
      trzszFilter
        .uploadFiles(event.dataTransfer.items)
        .then(() => console.log("upload success"))
        .catch((err) => console.log(err));
    });
  • TrzszAddon is a wrapper for TrzszFilter. If you are using xterm-addon-attach, just replace AttachAddon with TrzszAddon.

    import { Terminal } from "xterm";
    import { TrzszAddon } from "trzsz";
    
    const terminal = new Terminal();
    const trzszAddon = new TrzszAddon(webSocket);
    terminal.loadAddon(trzszAddon);

Examples

Screenshot

upload and download files in web browser

browser example

upload and download files in electron app

electron example

Contact

Feel free to email the author lonnywong@qq.com, or create an issue. Welcome to join the QQ group: 318578930.

Want to buy the author a drink 🍺 ?

sponsor wechat qrcode sponsor alipay qrcode

MIT License Copyright (c) 2023 Lonny Wong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

让 webshell 等 js 终端支持 trzsz ( trz / tsz ),和 lrzsz ( rz / sz ) 类似,并且有进度条和支持目录传输。 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/websalon/trzsz.js.git
git@gitee.com:websalon/trzsz.js.git
websalon
trzsz.js
trzsz.js
main

搜索帮助