All files tus.ts

100% Statements 75/75
94.11% Branches 16/17
100% Functions 4/4
100% Lines 75/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x                 1x                             4x 4x 4x 4x 4x 4x 4x 4x   4x 4x   4x   4x     4x 4x 4x 7x   7x 3x 3x 3x 3x 7x 4x 4x   4x   4x 7x   7x 7x   7x 7x 7x 7x     4x 4x 4x   7x 16x     16x 16x 43x 43x     16x 9x     9x 9x 9x 9x 9x 16x   7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x   7x   7x 7x   7x 7x   4x 4x  
import debug from 'debug'
import { basename } from 'path'
import { OnSuccessPayload, Upload, UploadOptions } from 'tus-js-client'
import { stat } from 'fs/promises'
import pMap from 'p-map'
import type { Readable } from 'stream'
import type { UploadProgress } from './Transloadit'
import { AssemblyStatus } from './alphalib/types/assemblyStatus'
 
const log = debug('transloadit')
 
export interface Stream {
  path?: string
  stream: Readable
}
 
interface SendTusRequestOptions {
  streamsMap: Record<string, Stream>
  assembly: AssemblyStatus
  requestedChunkSize: number
  uploadConcurrency: number
  onProgress: (options: UploadProgress) => void
}
 
export async function sendTusRequest({
  streamsMap,
  assembly,
  requestedChunkSize,
  uploadConcurrency,
  onProgress,
}: SendTusRequestOptions) {
  const streamLabels = Object.keys(streamsMap)
 
  let totalBytes = 0
  let lastEmittedProgress = 0
 
  const sizes: Record<string, number> = {}
 
  const haveUnknownLengthStreams = streamLabels.some((label) => !streamsMap[label]!.path)
 
  // Initialize size data
  await pMap(
    streamLabels,
    async (label) => {
      const { path } = streamsMap[label]!
 
      if (path) {
        const { size } = await stat(path)
        sizes[label] = size
        totalBytes += size
      }
    },
    { concurrency: 5 }
  )
 
  const uploadProgresses: Record<string, number> = {}
 
  async function uploadSingleStream(label: string) {
    uploadProgresses[label] = 0
 
    const { stream, path } = streamsMap[label]!
    const size = sizes[label]
 
    let chunkSize = requestedChunkSize
    let uploadLengthDeferred: boolean
    const isStreamLengthKnown = !!path
    if (!isStreamLengthKnown) {
      // tus-js-client requires these options to be set for unknown size streams
      // https://github.com/tus/tus-js-client/blob/master/docs/api.md#uploadlengthdeferred
      uploadLengthDeferred = true
      if (chunkSize === Infinity) chunkSize = 50e6
    }
 
    const onTusProgress = (bytesUploaded: number): void => {
      uploadProgresses[label] = bytesUploaded
 
      // get all uploaded bytes for all files
      let uploadedBytes = 0
      for (const l of streamLabels) {
        uploadedBytes += uploadProgresses[l] ?? 0
      }
 
      // don't send redundant progress
      if (lastEmittedProgress < uploadedBytes) {
        lastEmittedProgress = uploadedBytes
        // If we have any unknown length streams, we cannot trust totalBytes
        // totalBytes should then be undefined to mimic behavior of form uploads.
        onProgress({
          uploadedBytes,
          totalBytes: haveUnknownLengthStreams ? undefined : totalBytes,
        })
      }
    }
 
    const filename = path ? basename(path) : label
 
    await new Promise<OnSuccessPayload>((resolve, reject) => {
      const tusOptions: UploadOptions = {
        endpoint: assembly.tus_url,
        metadata: {
          assembly_url: assembly.assembly_ssl_url,
          fieldname: label,
          filename,
        },
        onError: reject,
        onProgress: onTusProgress,
        onSuccess: resolve,
      }
      // tus-js-client doesn't like undefined/null
      if (size != null) tusOptions.uploadSize = size
      if (chunkSize) tusOptions.chunkSize = chunkSize
      if (uploadLengthDeferred) tusOptions.uploadLengthDeferred = uploadLengthDeferred
 
      const tusUpload = new Upload(stream, tusOptions)
 
      tusUpload.start()
    })
 
    log(label, 'upload done')
  }
 
  await pMap(streamLabels, uploadSingleStream, { concurrency: uploadConcurrency })
}