All files tus.ts

92.3% Statements 84/91
80% Branches 16/20
100% Functions 4/4
92.3% Lines 84/91

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 124 125 126 127 128 129 130 131 132 133 134 135 136 1371x 1x   1x 1x 1x       1x                             4x 4x 4x 4x 4x 4x 4x 4x   4x 4x   4x   4x     4x 4x 4x 7x 7x     7x   7x 3x 3x 3x 3x 7x 4x 4x   4x   4x 7x   7x 7x     7x 7x   7x 7x 7x 7x     4x 4x 4x   7x 23x     23x 23x 68x 68x     23x 9x     9x 9x 9x 9x 9x 23x   7x   7x 7x         7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x   7x   7x 7x   7x 7x   4x 4x  
import { stat } from 'node:fs/promises'
import { basename } from 'node:path'
import type { Readable } from 'node:stream'
import debug from 'debug'
import pMap from 'p-map'
import { type OnSuccessPayload, Upload, type UploadOptions } from 'tus-js-client'
import type { AssemblyStatus } from './alphalib/types/assemblyStatus.js'
import type { UploadProgress } from './Transloadit.js'
 
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 streamInfo = streamsMap[label]
      if (!streamInfo) {
        throw new Error(`Stream info not found for label: ${label}`)
      }
      const { path } = streamInfo
 
      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 streamInfo = streamsMap[label]
    if (!streamInfo) {
      throw new Error(`Stream info not found for label: ${label}`)
    }
    const { stream, path } = streamInfo
    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 === Number.POSITIVE_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) => {
      if (!assembly.assembly_ssl_url) {
        reject(new Error('assembly_ssl_url is not present in the assembly status'))
        return
      }
 
      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 })
}