All files tus.ts

88.49% Statements 100/113
88.23% Branches 30/34
71.42% Functions 5/7
88.49% Lines 100/113

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 1691x 1x   1x 1x   1x       1x                               32x 32x 32x 32x 32x 32x 32x 32x 32x   32x 32x   32x   32x     32x 32x 32x   41x   41x 41x     41x   41x 8x 8x 8x 8x 41x 32x 32x   32x   32x 41x   41x 41x     41x 41x   41x 41x 41x 41x     33x 33x 33x   41x 113x     113x 113x 225x 225x     113x 45x     45x 45x 45x 45x 45x 113x   41x   41x 41x 1x 1x 1x     41x           40x 40x 40x 40x 40x 40x         40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x   41x 40x 41x   40x     41x 28x       28x 28x   40x 41x   40x 41x   32x 31x  
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, UploadOptions } from 'tus-js-client'
import { Upload } from 'tus-js-client'
import type { AssemblyStatus } from './alphalib/types/assemblyStatus.ts'
import type { UploadProgress } from './Transloadit.ts'
 
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
  signal?: AbortSignal
}
 
export async function sendTusRequest({
  streamsMap,
  assembly,
  requestedChunkSize,
  uploadConcurrency,
  onProgress,
  signal,
}: 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) => {
      // Check if aborted before each operation
      if (signal?.aborted) throw new Error('Upload aborted')
 
      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, signal },
  )
 
  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>((resolvePromise, rejectPromise) => {
      if (!assembly.assembly_ssl_url) {
        rejectPromise(new Error('assembly_ssl_url is not present in the assembly status'))
        return
      }
 
      // Check if already aborted before starting
      if (signal?.aborted) {
        rejectPromise(new Error('Upload aborted'))
        return
      }
 
      // Wrap resolve/reject to clean up abort listener
      let abortHandler: (() => void) | undefined
      const resolve = (payload: OnSuccessPayload) => {
        if (abortHandler) signal?.removeEventListener('abort', abortHandler)
        resolvePromise(payload)
      }
      const reject = (err: unknown) => {
        if (abortHandler) signal?.removeEventListener('abort', abortHandler)
        rejectPromise(err)
      }
 
      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)
 
      // Handle abort signal
      if (signal) {
        abortHandler = () => {
          tusUpload.abort()
          reject(new Error('Upload aborted'))
        }
        signal.addEventListener('abort', abortHandler, { once: true })
      }
 
      tusUpload.start()
    })
 
    log(label, 'upload done')
  }
 
  await pMap(streamLabels, uploadSingleStream, { concurrency: uploadConcurrency, signal })
}