tus-js-client
repository·main·Indexed 25 days ago
https://github.com/tus/tus-js-clientA 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.
What's inside tus-js-client
- 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.
Embed tus-js-client using a script tag
mainIf 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>- Unminified:
Run the React Native Demo
mainThis demo demonstrates how to use
tus-js-clientwithin a React Native environment. While the example is built using Expo for ease of development,tus-js-clientdoes 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/reactnativedirectory and execute the following commands:npm install npm startImport tus-js-client in your project
mainDepending on your environment and bundler support, you can load the package using CommonJS or ES Modules.
CommonJS:
var tus = require('tus-js-client')ES Modules:
import * as tus from 'tus-js-client'Runtime requirements and Promise polyfilling
mainThe JavaScript environment must support Promises. If you are targeting older browsers that do not support Promises, you must load a polyfill (like
core-js) before loadingtus-js-client.require('core-js/features/promise') var tus = require('tus-js-client')Basic workflow for using tus-js-client
mainThe standard lifecycle for an upload in
tus-js-clientfollows these steps:- Obtain the file (e.g., via an
<input type="file">element). - Create a new
tus.Uploadinstance with the file and configuration options. - Fetch previous uploads using
tus.Upload#findPreviousUploadsto check if the upload can be resumed. - Resume if a previous upload is found using
tus.Upload#resumeFromPreviousUpload. - Start the upload using
tus.Upload#start. - Pause the upload if necessary using
tus.Upload#abort(this stops current transfers immediately). - Unpause/Resume by calling
tus.Upload#startagain.
- Obtain the file (e.g., via an
Release tus-js-client to NPM
mainReleases are automated via GitHub Actions. To trigger a release, create a tag matching the
v*pattern that includes the same version as defined inpackage.json.Standard Releases:
- Run
npm versionlocally and push the resulting commit and tag to GitHub. - Or, edit
package.jsondirectly 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 inpackage.json(e.g.,5.0.0-pre1) and create a tag in the desired branch.- Run
Locate tus upload implementation in React Native Demo
mainIn the React Native demo application, the core logic for performing tus uploads is implemented in theApp.jsfile.Install tus-js-client via NPM
mainThe recommended way to install
tus-js-clientis using a package manager likenpmoryarn. This allows you to use the library in modern web development workflows with bundlers.$ npm install --save tus-js-clientRun the Apache Cordova demo
mainThis demo demonstrates how to use
tus-js-clientwithin an Apache Cordova application. It utilizes thecordova-plugin-cameraandcordova-plugin-fileplugins to access the device's photo library via the native file system.To run the demo, you must first build the
tus-js-clientdistribution 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 androidRun tests for tus-js-client
mainTests are implemented using Jasmine and are located in the
test/directory. Note: You must rebuild the library (viayarn run buildoryarn 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.htmldirectly in a browser using thefile:///protocol. No web server is required.
- Node.js: Run via
Build and watch tus-js-client source files
mainUse 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