All files Transloadit.ts

96.42% Statements 459/476
95.45% Branches 147/154
86.04% Functions 37/43
96.42% Lines 459/476

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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 9631x                                                               1x 1x                               10x 10x   9x 9x     10x 4x   10x   9x 9x 9x 9x 9x     9x 9x 10x 9x     9x 9x     29x 29x 1x 1x 29x   40x 40x 40x   28x     28x 28x 28x 28x 3x 28x 3x 3x 28x                         1x             1x   1x 51x 1x 1x   51x 1x 1x   51x 1x 1x   48x 48x 51x 51x 51x     51x 51x   1x       1x 1x 1x             1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x   25x 1x 1x 1x 1x 1x     25x         25x 25x 1x 25x 24x 24x 25x     25x 25x   25x 25x   25x 25x 25x     24x 24x 6x 6x   1x 1x   6x 24x 24x     24x 24x 24x     25x 4x 4x 4x   23x     23x     23x 23x 23x   23x 23x 23x 23x 23x 23x 23x   23x 22x 22x 22x 22x     23x   23x 23x 23x 23x 23x 19x   23x 5x 5x 5x 5x 5x 5x 5x 5x   18x 7x 7x 7x 7x 7x 7x 7x 23x   23x 25x     25x 25x   1x 13x 13x 13x 13x 13x 13x 13x 13x 13x     13x 21x   21x 21x 18x 12x 21x 12x 12x   9x 9x 21x       9x 21x 1x 1x 8x 8x 13x               1x         1x 1x 1x   1x 1x   1x 1x                 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x                   1x                   1x 23x 23x 23x 23x 23x   23x 23x   1x 1x 1x               1x 31x 31x 31x 29x 29x 31x               1x 1x 1x 1x 1x 1x   1x 1x                 1x 1x 1x 1x 1x 1x   1x 1x               1x 1x 1x 1x 1x   1x 1x               1x 2x 2x 2x 2x   2x 2x               1x 1x 1x 1x 1x 1x   1x 1x   1x                   1x 2x 2x 2x 2x 2x   2x 2x                 1x 1x 1x 1x 1x 1x   1x 1x               1x 1x 1x 1x 1x   1x 1x               1x 2x 2x 2x 2x   2x 2x               1x 1x 1x 1x 1x 1x   1x 1x   1x                     1x 1x 1x 1x 1x 1x   1x 1x   1x 98x 98x   98x 98x   1x 102x 102x 102x 102x       1x 34x 34x 34x 34x 34x 34x 34x 34x   34x   34x 22x 22x 22x 22x   34x   34x 23x 3x 3x 23x 23x 34x       1x 63x   63x   63x 63x     1x 101x 101x 2x 2x 101x 95x 95x 101x 95x 95x 101x 95x 95x   101x 101x       1x 92x 92x 92x 92x         1x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x     94x 94x   94x 62x 62x   94x       94x 95x   95x 33x 33x 33x   95x   95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x         95x   95x 95x 95x 1x 13x 1x 1x 95x 85x 95x 10x   8x 8x       8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   10x 7x 7x   1x 1x 1x 1x   1x 95x 94x 1x                                                                                                                                                                                                                                                                                                                                
import { createHmac, randomUUID } from 'crypto'
import got, { RequiredRetryOptions, Headers, OptionsOfJSONResponseBody } from 'got'
import FormData from 'form-data'
import { constants, createReadStream } from 'fs'
import { access } from 'fs/promises'
import debug from 'debug'
import intoStream from 'into-stream'
import isStream from 'is-stream'
import * as assert from 'assert'
import pMap from 'p-map'
import { InconsistentResponseError } from './InconsistentResponseError'
import { PaginationStream } from './PaginationStream'
import { PollingTimeoutError } from './PollingTimeoutError'
import { TransloaditError } from './TransloaditError'
import { version } from '../package.json'
import { sendTusRequest, Stream } from './tus'
 
import type { Readable } from 'stream'
 
// See https://github.com/sindresorhus/got#errors
// Expose relevant errors
export {
  RequestError,
  ReadError,
  ParseError,
  UploadError,
  HTTPError,
  MaxRedirectsError,
  TimeoutError,
} from 'got'
export { InconsistentResponseError }
 
const log = debug('transloadit')
const logWarn = debug('transloadit:warn')
 
interface RequestOptions {
  urlSuffix?: string
  url?: string
  timeout?: number
  method?: 'delete' | 'get' | 'post' | 'put'
  params?: KeyVal
  fields?: Record<string, string | number>
  headers?: Headers
}
 
interface CreateAssemblyPromise extends Promise<Assembly> {
  assemblyId: string
}
 
function decorateHttpError(err: TransloaditError, body: any): TransloaditError {
  if (!body) return err
 
  let newMessage = err.message
  let newStack = err.stack
 
  // Provide a more useful message if there is one
  if (body.message && body.error) newMessage += ` ${body.error}: ${body.message}`
  else if (body.error) newMessage += ` ${body.error}`
 
  if (body.assembly_ssl_url) newMessage += ` - ${body.assembly_ssl_url}`
 
  if (typeof err.stack === 'string') {
    const indexOfMessageEnd = err.stack.indexOf(err.message) + err.message.length
    const stacktrace = err.stack.slice(indexOfMessageEnd)
    newStack = `${newMessage}${stacktrace}`
  }
 
  /* eslint-disable no-param-reassign */
  err.message = newMessage
  if (newStack != null) err.stack = newStack
  if (body.assembly_id) err.assemblyId = body.assembly_id
  if (body.error) err.transloaditErrorCode = body.error
  /* eslint-enable no-param-reassign */
 
  return err
}
 
// Not sure if this is still a problem with the API, but throw a special error type so the user can retry if needed
function checkAssemblyUrls(result: Assembly) {
  if (result.assembly_url == null || result.assembly_ssl_url == null) {
    throw new InconsistentResponseError('Server returned an incomplete assembly response (no URL)')
  }
}
 
function getHrTimeMs(): number {
  return Number(process.hrtime.bigint() / 1000000n)
}
 
function checkResult<T>(result: T | { error: string }): asserts result is T {
  // In case server returned a successful HTTP status code, but an `error` in the JSON object
  // This happens sometimes when createAssembly with an invalid file (IMPORT_FILE_ERROR)
  if (
    typeof result === 'object' &&
    result !== null &&
    'error' in result &&
    typeof result.error === 'string'
  ) {
    throw decorateHttpError(new TransloaditError('Error in response', result), result)
  }
}
 
export declare namespace Transloadit {
  interface Options {
    authKey: string
    authSecret: string
    endpoint?: string
    maxRetries?: number
    timeout?: number
    gotRetry?: RequiredRetryOptions
  }
}
 
export class Transloadit {
  private _authKey: string
  private _authSecret: string
  private _endpoint: string
  private _maxRetries: number
  private _defaultTimeout: number
  private _gotRetry: RequiredRetryOptions | number
  private _lastUsedAssemblyUrl = ''
 
  constructor(opts: Transloadit.Options) {
    if (opts?.authKey == null) {
      throw new Error('Please provide an authKey')
    }
 
    if (opts.authSecret == null) {
      throw new Error('Please provide an authSecret')
    }
 
    if (opts.endpoint && opts.endpoint.endsWith('/')) {
      throw new Error('Trailing slash in endpoint is not allowed')
    }
 
    this._authKey = opts.authKey
    this._authSecret = opts.authSecret
    this._endpoint = opts.endpoint || 'https://api2.transloadit.com'
    this._maxRetries = opts.maxRetries != null ? opts.maxRetries : 5
    this._defaultTimeout = opts.timeout != null ? opts.timeout : 60000
 
    // Passed on to got https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md
    this._gotRetry = opts.gotRetry != null ? opts.gotRetry : 0
  }
 
  getLastUsedAssemblyUrl(): string {
    return this._lastUsedAssemblyUrl
  }
 
  setDefaultTimeout(timeout: number): void {
    this._defaultTimeout = timeout
  }
 
  /**
   * Create an Assembly
   *
   * @param opts assembly options
   */
  createAssembly(opts: CreateAssemblyOptions = {}): CreateAssemblyPromise {
    const {
      params = {},
      waitForCompletion = false,
      isResumable = true,
      chunkSize: requestedChunkSize = Infinity,
      uploadConcurrency = 10,
      timeout = 24 * 60 * 60 * 1000, // 1 day
      onUploadProgress = () => {},
      onAssemblyProgress = () => {},
      files = {},
      uploads = {},
      assemblyId,
    } = opts
 
    if (!isResumable) {
      process.emitWarning(
        'Parameter value isResumable = false is deprecated. All uploads will be resumable (using TUS) in the future',
        'DeprecationWarning'
      )
    }
 
    // Keep track of how long the request took
    const startTimeMs = getHrTimeMs()
 
    // Undocumented feature to allow specifying a custom assembly id from the client
    // Not recommended for general use due to security. E.g if the user doesn't provide a cryptographically
    // secure ID, then anyone could access the assembly.
    let effectiveAssemblyId
    if (assemblyId != null) {
      effectiveAssemblyId = assemblyId
    } else {
      effectiveAssemblyId = randomUUID().replace(/-/g, '')
    }
    const urlSuffix = `/assemblies/${effectiveAssemblyId}`
 
    // We want to be able to return the promise immediately with custom data
    const promise = (async () => {
      this._lastUsedAssemblyUrl = `${this._endpoint}${urlSuffix}`
 
      await pMap(
        Object.entries(files),
        // eslint-disable-next-line no-bitwise
        async ([, path]) => access(path, constants.F_OK | constants.R_OK),
        { concurrency: 5 }
      )
 
      // Convert uploads to streams
      const streamsMap = Object.fromEntries(
        Object.entries(uploads).map(([label, value]) => {
          const isReadable = isStream.readable(value)
          if (!isReadable && isStream(value)) {
            // https://github.com/transloadit/node-sdk/issues/92
            throw new Error(`Upload named "${label}" is not a Readable stream`)
          }
 
          return [label, isStream.readable(value) ? value : intoStream(value)]
        })
      )
 
      // Wrap in object structure (so we can store whether it's a pathless stream or not)
      const allStreamsMap = Object.fromEntries<Stream>(
        Object.entries(streamsMap).map(([label, stream]) => [label, { stream }])
      )
 
      // Create streams from files too
      for (const [label, path] of Object.entries(files)) {
        const stream = createReadStream(path)
        allStreamsMap[label] = { stream, path } // File streams have path
      }
 
      const allStreams = Object.values(allStreamsMap)
 
      // Pause all streams
      allStreams.forEach(({ stream }) => stream.pause())
 
      // If any stream emits error, we want to handle this and exit with error
      const streamErrorPromise = new Promise<Assembly>((resolve, reject) => {
        allStreams.forEach(({ stream }) => stream.on('error', reject))
      })
 
      const createAssemblyAndUpload = async () => {
        const requestOpts: RequestOptions = {
          urlSuffix,
          method: 'post',
          timeout,
          params,
        }
 
        if (isResumable) {
          requestOpts.fields = {
            tus_num_expected_upload_files: allStreams.length,
          }
        }
 
        // upload as form multipart or tus?
        const formUploadStreamsMap: Record<string, Stream> = isResumable ? {} : allStreamsMap
 
        const result = await this._remoteJson<Assembly>(
          requestOpts,
          formUploadStreamsMap,
          onUploadProgress
        )
        checkResult(result)
 
        if (isResumable && Object.keys(allStreamsMap).length > 0) {
          await sendTusRequest({
            streamsMap: allStreamsMap,
            assembly: result,
            onProgress: onUploadProgress,
            requestedChunkSize,
            uploadConcurrency,
          })
        }
 
        if (!waitForCompletion) return result
        const awaitResult = await this.awaitAssemblyCompletion(result.assembly_id, {
          timeout,
          onAssemblyProgress,
          startTimeMs,
        })
        checkResult(awaitResult)
        return awaitResult
      }
 
      return Promise.race([createAssemblyAndUpload(), streamErrorPromise])
    })()
 
    // This allows the user to use or log the assemblyId even before it has been created for easier debugging
    return Object.assign(promise, { assemblyId: effectiveAssemblyId })
  }
 
  async awaitAssemblyCompletion(
    assemblyId: string,
    {
      onAssemblyProgress = () => {},
      timeout,
      startTimeMs = getHrTimeMs(),
      interval = 1000,
    }: AwaitAssemblyCompletionOptions = {}
  ): Promise<Assembly> {
    assert.ok(assemblyId)
 
    // eslint-disable-next-line no-constant-condition
    while (true) {
      const result = await this.getAssembly(assemblyId)
 
      if (
        result.ok !== 'ASSEMBLY_UPLOADING' &&
        result.ok !== 'ASSEMBLY_EXECUTING' &&
        result.ok !== 'ASSEMBLY_REPLAYING'
      ) {
        return result // Done!
      }
 
      try {
        onAssemblyProgress(result)
      } catch (err) {
        log('Caught onAssemblyProgress error', err)
      }
 
      const nowMs = getHrTimeMs()
      if (timeout != null && nowMs - startTimeMs >= timeout) {
        throw new PollingTimeoutError('Polling timed out')
      }
      await new Promise((resolve) => setTimeout(resolve, interval))
    }
  }
 
  /**
   * Cancel the assembly
   *
   * @param assemblyId assembly ID
   * @returns after the assembly is deleted
   */
  async cancelAssembly(assemblyId: string): Promise<Assembly> {
    // You may wonder why do we need to call getAssembly first:
    // If we use the default base URL (instead of the one returned in assembly_url_ssl),
    // the delete call will hang in certain cases
    // See test "should stop the assembly from reaching completion"
    const { assembly_ssl_url: url } = await this.getAssembly(assemblyId)
    const opts: RequestOptions = {
      url,
      // urlSuffix: `/assemblies/${assemblyId}`, // Cannot simply do this, see above
      method: 'delete',
    }
 
    return this._remoteJson(opts)
  }
 
  /**
   * Replay an Assembly
   *
   * @param assemblyId of the assembly to replay
   * @param optional params
   * @returns after the replay is started
   */
  async replayAssembly(assemblyId: string, params: KeyVal = {}): Promise<ReplayedAssembly> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/assemblies/${assemblyId}/replay`,
      method: 'post',
    }
    if (Object.keys(params).length > 0) requestOpts.params = params
    const result = await this._remoteJson<ReplayedAssembly>(requestOpts)
    checkResult(result)
    return result
  }
 
  /**
   * Replay an Assembly notification
   *
   * @param assemblyId of the assembly whose notification to replay
   * @param optional params
   * @returns after the replay is started
   */
  async replayAssemblyNotification(
    assemblyId: string,
    params: KeyVal = {}
  ): Promise<{ ok: string; success: boolean }> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/assembly_notifications/${assemblyId}/replay`,
      method: 'post',
    }
    if (Object.keys(params).length > 0) requestOpts.params = params
    return this._remoteJson(requestOpts)
  }
 
  /**
   * List all assembly notifications
   *
   * @param params optional request options
   * @returns the list of Assembly notifications
   */
  async listAssemblyNotifications(params: object): Promise<PaginationList<object>> {
    const requestOpts: RequestOptions = {
      urlSuffix: '/assembly_notifications',
      method: 'get',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  streamAssemblyNotifications(params: object): PaginationStream<object> {
    return new PaginationStream(async (page) => this.listAssemblyNotifications({ ...params, page }))
  }
 
  /**
   * List all assemblies
   *
   * @param params optional request options
   * @returns list of Assemblies
   */
  async listAssemblies(params?: KeyVal): Promise<PaginationList<ListedAssembly>> {
    const requestOpts: RequestOptions = {
      urlSuffix: '/assemblies',
      method: 'get',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  streamAssemblies(params: KeyVal): Readable {
    return new PaginationStream(async (page) => this.listAssemblies({ ...params, page }))
  }
 
  /**
   * Get an Assembly
   *
   * @param assemblyId the Assembly Id
   * @returns the retrieved Assembly
   */
  async getAssembly(assemblyId: string): Promise<Assembly> {
    const result = await this._remoteJson<Assembly>({
      urlSuffix: `/assemblies/${assemblyId}`,
    })
    checkAssemblyUrls(result)
    return result
  }
 
  /**
   * Create a Credential
   *
   * @param params optional request options
   * @returns when the Credential is created
   */
  async createTemplateCredential(params: object): Promise<any> {
    const requestOpts: RequestOptions = {
      urlSuffix: '/template_credentials',
      method: 'post',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * Edit a Credential
   *
   * @param credentialId the Credential ID
   * @param params optional request options
   * @returns when the Credential is edited
   */
  async editTemplateCredential(credentialId: string, params: object): Promise<any> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/template_credentials/${credentialId}`,
      method: 'put',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * Delete a Credential
   *
   * @param credentialId the Credential ID
   * @returns when the Credential is deleted
   */
  async deleteTemplateCredential(credentialId: string): Promise<any> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/template_credentials/${credentialId}`,
      method: 'delete',
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * Get a Credential
   *
   * @param credentialId the Credential ID
   * @returns when the Credential is retrieved
   */
  async getTemplateCredential(credentialId: string): Promise<any> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/template_credentials/${credentialId}`,
      method: 'get',
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * List all TemplateCredentials
   *
   * @param params optional request options
   * @returns the list of templates
   */
  async listTemplateCredentials(params?: object): Promise<any> {
    const requestOpts: RequestOptions = {
      urlSuffix: '/template_credentials',
      method: 'get',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  streamTemplateCredentials(params: object): PaginationStream<any> {
    return new PaginationStream(async (page) => this.listTemplateCredentials({ ...params, page }))
  }
 
  /**
   * Create an Assembly Template
   *
   * @param params optional request options
   * @returns when the template is created
   */
  async createTemplate(params: KeyVal = {}): Promise<TemplateResponse> {
    const requestOpts: RequestOptions = {
      urlSuffix: '/templates',
      method: 'post',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * Edit an Assembly Template
   *
   * @param templateId the template ID
   * @param params optional request options
   * @returns when the template is edited
   */
  async editTemplate(templateId: string, params: KeyVal): Promise<TemplateResponse> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/templates/${templateId}`,
      method: 'put',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * Delete an Assembly Template
   *
   * @param templateId the template ID
   * @returns when the template is deleted
   */
  async deleteTemplate(templateId: string): Promise<{ ok: string; message: string }> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/templates/${templateId}`,
      method: 'delete',
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * Get an Assembly Template
   *
   * @param templateId the template ID
   * @returns when the template is retrieved
   */
  async getTemplate(templateId: string): Promise<TemplateResponse> {
    const requestOpts: RequestOptions = {
      urlSuffix: `/templates/${templateId}`,
      method: 'get',
    }
 
    return this._remoteJson(requestOpts)
  }
 
  /**
   * List all Assembly Templates
   *
   * @param params optional request options
   * @returns the list of templates
   */
  async listTemplates(params?: KeyVal): Promise<PaginationList<ListedTemplate>> {
    const requestOpts: RequestOptions = {
      urlSuffix: '/templates',
      method: 'get',
      params: params || {},
    }
 
    return this._remoteJson(requestOpts)
  }
 
  streamTemplates(params?: KeyVal): PaginationStream<ListedTemplate> {
    return new PaginationStream(async (page) => this.listTemplates({ ...params, page }))
  }
 
  /**
   * Get account Billing details for a specific month
   *
   * @param month the date for the required billing in the format yyyy-mm
   * @returns with billing data
   * @see https://transloadit.com/docs/api/bill-date-get/
   */
  async getBill(month: string): Promise<KeyVal> {
    assert.ok(month, 'month is required')
    const requestOpts: RequestOptions = {
      urlSuffix: `/bill/${month}`,
      method: 'get',
    }
 
    return this._remoteJson(requestOpts)
  }
 
  calcSignature(params: KeyVal): { signature: string; params: string } {
    const jsonParams = this._prepareParams(params)
    const signature = this._calcSignature(jsonParams)
 
    return { signature, params: jsonParams }
  }
 
  private _calcSignature(toSign: string, algorithm = 'sha384'): string {
    return `${algorithm}:${createHmac(algorithm, this._authSecret)
      .update(Buffer.from(toSign, 'utf-8'))
      .digest('hex')}`
  }
 
  // Sets the multipart/form-data for POST, PUT and DELETE requests, including
  // the streams, the signed params, and any additional fields.
  private _appendForm(
    form: FormData,
    params: KeyVal,
    streamsMap?: Record<string, Stream>,
    fields?: Record<string, string | number>
  ): void {
    const sigData = this.calcSignature(params)
    const jsonParams = sigData.params
    const { signature } = sigData
 
    form.append('params', jsonParams)
 
    if (fields != null) {
      for (const [key, val] of Object.entries(fields)) {
        form.append(key, val)
      }
    }
 
    form.append('signature', signature)
 
    if (streamsMap) {
      Object.entries(streamsMap).forEach(([label, { stream, path }]) => {
        const options = path ? undefined : { filename: label } // https://github.com/transloadit/node-sdk/issues/86
        form.append(label, stream, options)
      })
    }
  }
 
  // Implements HTTP GET query params, handling the case where the url already
  // has params.
  private _appendParamsToUrl(url: string, params: KeyVal): string {
    const { signature, params: jsonParams } = this.calcSignature(params)
 
    const prefix = url.indexOf('?') === -1 ? '?' : '&'
 
    return `${url}${prefix}signature=${signature}&params=${encodeURIComponent(jsonParams)}`
  }
 
  // Responsible for including auth parameters in all requests
  private _prepareParams(paramsIn: KeyVal): string {
    let params = paramsIn
    if (params == null) {
      params = {}
    }
    if (params['auth'] == null) {
      params['auth'] = {}
    }
    if (params['auth'].key == null) {
      params['auth'].key = this._authKey
    }
    if (params['auth'].expires == null) {
      params['auth'].expires = this._getExpiresDate()
    }
 
    return JSON.stringify(params)
  }
 
  // We want to mock this method
  // eslint-disable-next-line class-methods-use-this
  private _getExpiresDate(): string {
    const expiresDate = new Date()
    expiresDate.setDate(expiresDate.getDate() + 1)
    return expiresDate.toISOString()
  }
 
  // Responsible for making API calls. Automatically sends streams with any POST,
  // PUT or DELETE requests. Automatically adds signature parameters to all
  // requests. Also automatically parses the JSON response.
  private async _remoteJson<T>(
    opts: RequestOptions,
    streamsMap?: Record<string, Stream>,
    onProgress: CreateAssemblyOptions['onUploadProgress'] = () => {}
  ): Promise<T> {
    const {
      urlSuffix,
      url: urlInput,
      timeout = this._defaultTimeout,
      method = 'get',
      params = {},
      fields,
      headers,
    } = opts
 
    // Allow providing either a `urlSuffix` or a full `url`
    if (!urlSuffix && !urlInput) throw new Error('No URL provided')
    let url = urlInput || `${this._endpoint}${urlSuffix}`
 
    if (method === 'get') {
      url = this._appendParamsToUrl(url, params)
    }
 
    log('Sending request', method, url)
 
    // Cannot use got.retry because we are using FormData which is a stream and can only be used once
    // https://github.com/sindresorhus/got/issues/1282
    for (let retryCount = 0; ; retryCount++) {
      let form
 
      if (method === 'post' || method === 'put' || method === 'delete') {
        form = new FormData()
        this._appendForm(form, params, streamsMap, fields)
      }
 
      const isUploadingStreams = streamsMap && Object.keys(streamsMap).length > 0
 
      const requestOpts: OptionsOfJSONResponseBody = {
        retry: this._gotRetry,
        body: form as FormData,
        timeout,
        headers: {
          'Transloadit-Client': `node-sdk:${version}`,
          'User-Agent': undefined, // Remove got's user-agent
          ...headers,
        },
        responseType: 'json',
      }
 
      // For non-file streams transfer encoding does not get set, and the uploaded files will not get accepted
      // https://github.com/transloadit/node-sdk/issues/86
      // https://github.com/form-data/form-data/issues/394#issuecomment-573595015
      if (isUploadingStreams) requestOpts.headers!['transfer-encoding'] = 'chunked'
 
      try {
        const request = got[method]<T>(url, requestOpts)
        if (isUploadingStreams) {
          request.on('uploadProgress', ({ transferred, total }) =>
            onProgress({ uploadedBytes: transferred, totalBytes: total })
          )
        }
        const { body } = await request
        return body
      } catch (err) {
        if (!(err instanceof got.HTTPError)) throw err
 
        const { statusCode, body } = err.response
        logWarn('HTTP error', statusCode, body)
 
        // check whether we should retry
        // https://transloadit.com/blog/2012/04/introducing-rate-limiting/
        if (
          !(
            statusCode === 413 &&
            typeof body === 'object' &&
            body != null &&
            'error' in body &&
            body.error === 'RATE_LIMIT_REACHED' &&
            'info' in body &&
            typeof body.info === 'object' &&
            body.info != null &&
            'retryIn' in body.info &&
            typeof body.info.retryIn === 'number' &&
            Boolean(body.info.retryIn) &&
            retryCount < this._maxRetries
          )
        ) {
          throw decorateHttpError(err, body)
        }
 
        const { retryIn: retryInSec } = body.info
        logWarn(`Rate limit reached, retrying request in approximately ${retryInSec} seconds.`)
        const retryInMs = 1000 * (retryInSec * (1 + 0.1 * Math.random()))
        await new Promise((resolve) => setTimeout(resolve, retryInMs))
        // Retry
      }
    }
  }
}
 
export interface CreateAssemblyOptions {
  params?: CreateAssemblyParams
  files?: {
    [name: string]: string
  }
  uploads?: {
    [name: string]: Readable | intoStream.Input
  }
  waitForCompletion?: boolean
  isResumable?: boolean
  chunkSize?: number
  uploadConcurrency?: number
  timeout?: number
  onUploadProgress?: (uploadProgress: UploadProgress) => void
  onAssemblyProgress?: AssemblyProgress
  assemblyId?: string
}
 
export type AssemblyProgress = (assembly: Assembly) => void
 
export interface CreateAssemblyParams {
  /** See https://transloadit.com/docs/topics/assembly-instructions/ */
  steps?: KeyVal
  template_id?: string
  notify_url?: string
  fields?: KeyVal
  allow_steps_override?: boolean
}
 
// TODO
/** Object with properties. See https://transloadit.com/docs/api/ */
export interface KeyVal {
  [key: string]: any
}
 
export interface UploadProgress {
  uploadedBytes?: number | undefined
  totalBytes?: number | undefined
}
 
/** https://transloadit.com/docs/api/assembly-status-response/#explanation-of-fields */
export interface Assembly {
  ok?: string
  message?: string
  assembly_id: string
  parent_id?: string
  account_id: string
  template_id?: string
  instance: string
  assembly_url: string
  assembly_ssl_url: string
  uppyserver_url: string
  companion_url: string
  websocket_url: string
  tus_url: string
  bytes_received: number
  bytes_expected: number
  upload_duration: number
  client_agent?: string
  client_ip?: string
  client_referer?: string
  transloadit_client: string
  start_date: string
  upload_meta_data_extracted: boolean
  warnings: any[]
  is_infinite: boolean
  has_dupe_jobs: boolean
  execution_start: string
  execution_duration: number
  queue_duration: number
  jobs_queue_duration: number
  notify_start?: any
  notify_url?: string
  notify_status?: any
  notify_response_code?: any
  notify_duration?: any
  last_job_completed?: string
  fields: KeyVal
  running_jobs: any[]
  bytes_usage: number
  executing_jobs: any[]
  started_jobs: string[]
  parent_assembly_status: any
  params: string
  template?: any
  merged_params: string
  uploads: any[]
  results: any
  build_id: string
  error?: string
  stderr?: string
  stdout?: string
  reason?: string
}
 
/** See https://transloadit.com/docs/api/assemblies-assembly-id-get/ */
export interface ListedAssembly {
  id?: string
  parent_id?: string
  account_id: string
  template_id?: string
  instance: string
  notify_url?: string
  redirect_url?: string
  files: string
  warning_count: number
  execution_duration: number
  execution_start: string
  ok?: string
  error?: string
  created: string
}
 
export interface ReplayedAssembly {
  ok?: string
  message?: string
  success: boolean
  assembly_id: string
  assembly_url: string
  assembly_ssl_url: string
  notify_url?: string
}
 
export interface ListedTemplate {
  id: string
  name: string
  encryption_version: number
  require_signature_auth: number
  last_used?: string
  created: string
  modified: string
  content: TemplateContent
}
 
export interface TemplateResponse {
  ok: string
  message: string
  id: string
  content: TemplateContent
  name: string
  require_signature_auth: number
}
 
export interface TemplateContent {
  steps: KeyVal
}
 
export interface AwaitAssemblyCompletionOptions {
  onAssemblyProgress?: AssemblyProgress
  timeout?: number
  interval?: number
  startTimeMs?: number
}
 
export interface PaginationList<T> {
  count: number
  items: T[]
}