tus-js-client

repository·main·Indexed 25 days ago

https://github.com/tus/tus-js-client

A pure JavaScript client for the tus resumable upload protocol, version 5.0.0-pre2. It enables file uploads that can be paused and resumed across browsers, Node.js, React Native, and Apache Cordova. The library provides the tus.Upload class for managing upload lifecycles, support for parallel uploads via the concatenation extension, and customizable interfaces for URL storage, HTTP stacks, and file readers.

Tokens
11.2K
Snippets
22
Records
55
Agent score
82%

What's inside tus-js-client

  1. Get started with tus-js-client

    main
    tus-js-client is a pure JavaScript client for the tus resumable upload protocol. It enables resumable file uploads, meaning uploads can be interrupted (due to network issues, server outages, or user action) and resumed without re-uploading previously sent data. It is compatible with browsers, Node.js, React Native, and Apache Cordova.
  2. Embed tus-js-client using a script tag

    main

    If you are not using a web bundler, you can download the prebuilt scripts and include them directly in your HTML. It is recommended to use the minified version for production.

    • Unminified: https://cdn.jsdelivr.net/npm/tus-js-client@latest/dist/tus.js
    • Minified: https://cdn.jsdelivr.net/npm/tus-js-client@latest/dist/tus.min.js
    <script src="./tus.min.js"></script>
    <script>
      var upload = new tus.Upload(...);
    </script>
  3. Run the React Native Demo

    main

    This demo demonstrates how to use tus-js-client within a React Native environment. While the example is built using Expo for ease of development, tus-js-client does not depend on Expo and can be used in any React Native project as it only relies on standard React Native APIs.

    To run the demo, navigate to the demos/reactnative directory and execute the following commands:

    npm install
    npm start
  4. Basic workflow for using tus-js-client

    main

    The standard lifecycle for an upload in tus-js-client follows these steps:

    1. Obtain the file (e.g., via an <input type="file"> element).
    2. Create a new tus.Upload instance with the file and configuration options.
    3. Fetch previous uploads using tus.Upload#findPreviousUploads to check if the upload can be resumed.
    4. Resume if a previous upload is found using tus.Upload#resumeFromPreviousUpload.
    5. Start the upload using tus.Upload#start.
    6. Pause the upload if necessary using tus.Upload#abort (this stops current transfers immediately).
    7. Unpause/Resume by calling tus.Upload#start again.
  5. Release tus-js-client to NPM

    main

    Releases are automated via GitHub Actions. To trigger a release, create a tag matching the v* pattern that includes the same version as defined in package.json.

    Standard Releases:

    • Run npm version locally and push the resulting commit and tag to GitHub.
    • Or, edit package.json directly on GitHub and manually create a release to generate the tag.

    Pre-releases: To create a pre-release from a branch other than main, include a suffix in the version in package.json (e.g., 5.0.0-pre1) and create a tag in the desired branch.

  6. Run the Apache Cordova demo

    main

    This demo demonstrates how to use tus-js-client within an Apache Cordova application. It utilizes the cordova-plugin-camera and cordova-plugin-file plugins to access the device's photo library via the native file system.

    To run the demo, you must first build the tus-js-client distribution files, then execute the Cordova command for your target platform.

    # 1. Build the tus-js-client files
    cd ../..
    npm run dist
    
    # 2. Execute the Cordova demo
    cordova run android
  7. Run tests for tus-js-client

    main

    Tests are implemented using Jasmine and are located in the test/ directory. Note: You must rebuild the library (via yarn run build or yarn run watch) before running tests.

    Available testing environments:

    • Node.js: Run via yarn run test-node.
    • Puppeteer (Automated browser): Run via yarn run test-puppeteer.
    • Browser: Open test/SpecRunner.html directly in a browser using the file:/// protocol. No web server is required.
  8. Build and watch tus-js-client source files

    main

    Use the following commands to build the library bundle and test scripts, or to enable a watch mode that rebuilds artifacts automatically when files change.

    # Build the library bundle and all test scripts
    yarn run build
    
    # Watch source files and rebuild when files get changes
    yarn run watch