tus-js-client Resumable Upload Client

4.3.1 · active · verified Tue Apr 21

tus-js-client is a robust, pure JavaScript client library that implements the tus resumable upload protocol, enabling reliable and resilient file transfers even across network interruptions. The current stable version is 4.3.1, with the project maintaining an active release cycle that includes frequent patch updates for bug fixes (e.g., bundling issues) and minor versions for feature enhancements like expanded IETF draft support. Major version bumps, such as v4.0.0, primarily serve to update minimum Node.js environment requirements rather than introducing breaking API changes. Key differentiators include its comprehensive support for the tus protocol, cross-platform compatibility across browsers, Node.js environments (requiring Node.js >=18), and Web Workers, alongside native TypeScript type definitions for an improved developer experience. It provides a foundational solution for developers needing to handle large file uploads with progress tracking, pause/resume functionality, and error recovery.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic file upload using the `Upload` class, including progress tracking, success, and error handling, with support for resuming previous uploads. This example is adaptable for both Node.js (using `fs.createReadStream`) and browser environments.

import { Upload } from 'tus-js-client';
import fs from 'fs'; // For Node.js environment

// In a browser, file would be from an <input type="file"> event
// In Node.js, you might read a file from the filesystem
const filePath = './path/to/your/file.txt';
const file = fs.createReadStream(filePath); // Or a Blob/File object in browser

const upload = new Upload(file, {
  endpoint: 'https://master.tus.io/files/',
  retryDelays: [0, 1000, 3000, 5000],
  metadata: {
    filename: 'my-document.txt',
    filetype: 'text/plain'
  },
  headers: { // Example of custom headers
    'X-Requested-With': 'XMLHttpRequest'
  },
  onProgress: (bytesUploaded, bytesTotal) => {
    const percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);
    console.log(`Uploaded ${bytesUploaded} of ${bytesTotal} bytes (${percentage}%)`);
  },
  onSuccess: () => {
    console.log('Upload finished:', upload.url);
  },
  onError: (error) => {
    console.error('Failed to upload:', error);
    if (error.originalRequest) {
        console.error('Response status:', error.originalRequest.getStatus());
        console.error('Response text:', error.originalRequest.getResponseText());
    }
  }
});

// Check if there are any previous uploads to resume
upload.findPreviousUploads().then(previousUploads => {
  if (previousUploads.length) {
    upload.resumeFromPreviousUpload(previousUploads[0]);
  }
  upload.start();
});

view raw JSON →