All files index.ts

83.93% Statements 94/112
68.75% Branches 33/48
82.76% Functions 24/29
82% Lines 82/100

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 2601x   1x 1x 1x 1x 1x               1x       1x       10x   1x                     1x       10x 10x     1x 10x 10x 10x 10x 1x     10x               10x 9x   10x 3x     10x 2x   10x   1x       1x           1x                     1x               1x                                                       1x                   1x         11x   1x   1x       11x 11x   1x 11x   11x         11x 10x     11x 2x     11x   1x                     1x       21x   1x   1x       21x 21x   1x 21x   21x         21x 19x     21x   21x 4x   21x 21x     21x   1x         1x       48x 45x     1x       1x 10x     1x 11x     1x 21x     8x 4x   4x 4x   1x   1x  
import { ThumbnailJob } from "./thumbnail";
import { Blob } from "./blob";
import { Container, Encoder, TranscodeJob, TranscodeOpts } from "./transcode";
import { SummaryJob, SummaryType } from "./summary";
import { parseApiKey } from "./utils";
import { WorkerConfig } from "./WorkerConfig";
import {
  ImageWatermark,
  ImageWatermarkOptions,
  TextWatermark,
  TextWatermarkOptions,
  Watermark,
} from "./watermark";
import { Executable } from "./Executable";
import { WorkerTarget } from "./WorkerTarget";
 
// mp4 transcoding
 
class TranscodeMp4Target extends WorkerTarget<TranscodeJob> {
  workerConfig: TranscodeJob;
 
  constructor(transcoder: TranscodeJob) {
    super(transcoder);
  }
}
 
interface TranscodeMp4Options {
  height?: number;
  width?: number;
  watermark?: Watermark;
  encoder?: Encoder;
  successUrl?: string;
  failureUrl?: string;
}
 
class TranscoderMp4 extends WorkerConfig<TranscodeMp4Target> {
  options: TranscodeMp4Options;
 
  constructor(apiKey: string, opts: TranscodeMp4Options) {
    super(apiKey, TranscodeMp4Target);
    this.options = opts;
  }
 
  getExecutable(fromConfig: string | Blob) {
    const opts = new TranscodeOpts();
    const options = this.options;
    opts.container(Container.MP4);
    if (options.encoder) {
      opts.encoder(options.encoder);
    }
 
    let config = new TranscodeJob(this.apiKey)
      .from(fromConfig)
      .webhooks({
        successUrl: options.successUrl,
        failureUrl: options.failureUrl,
      })
      .opts(opts);
 
    if (options.width) {
      config = config.width(options.width);
    }
    if (options.height) {
      config = config.height(options.height);
    }
 
    if (options.watermark) {
      config = config.watermark(options.watermark);
    }
    return config;
  }
}
 
// transcoding
 
class TranscodeWebmTarget extends WorkerTarget<TranscodeJob> {
  workerConfig: TranscodeJob;
 
  constructor(transcoder: TranscodeJob) {
    super(transcoder);
  }
}
 
interface TranscodeWebmOptions {
  height?: number;
  width?: number;
  watermark?: Watermark;
  encoder: "vp8" | "vp9";
  successUrl?: string;
  failureUrl?: string;
}
 
class TranscoderWebm extends WorkerConfig<TranscodeWebmTarget> {
  options: TranscodeWebmOptions;
 
  constructor(apiKey: string, opts: TranscodeWebmOptions) {
    super(apiKey, TranscodeWebmTarget);
    this.options = opts;
  }
 
  getExecutable(fromConfig: string | Blob) {
    const opts = new TranscodeOpts();
    opts.container(Container.WEBM);
    const options = this.options;
    if (options.encoder) {
      opts.encoder(options.encoder);
    }
 
    let config = new TranscodeJob(this.apiKey)
      .from(fromConfig)
      .webhooks({
        successUrl: options.successUrl,
        failureUrl: options.failureUrl,
      })
      .opts(opts);
 
    if (options.width) {
      config = config.width(options.width);
    }
    if (options.height) {
      config = config.height(options.height);
    }
 
    if (options.watermark) {
      config = config.watermark(options.watermark);
    }
    return config;
  }
}
 
interface ThumbnailOptions {
  width?: number;
  watermarkText?: string;
  watermark?: Watermark;
  successUrl?: string;
  failureUrl?: string;
}
 
class ThumbnailTarget extends WorkerTarget<ThumbnailJob> {
  thumbnailer: Thumbnailer;
  inputBlob: Blob;
 
  constructor(thumbnailer: ThumbnailJob) {
    super(thumbnailer);
  }
}
 
class Thumbnailer extends WorkerConfig<ThumbnailTarget> {
  options: ThumbnailOptions;
 
  constructor(apiKey: string, options: ThumbnailOptions) {
    super(apiKey, ThumbnailTarget);
    this.options = options;
  }
  getExecutable(fromConfig: string | Blob) {
    const options = this.options;
 
    let config = new ThumbnailJob(this.apiKey).from(fromConfig).webhooks({
      successUrl: options.successUrl,
      failureUrl: options.failureUrl,
    });
 
    if (options.width) {
      config = config.width(150);
    }
 
    if (options.watermark) {
      config = config.watermark(options.watermark);
    }
 
    return config;
  }
}
 
interface SummaryOptions {
  width?: number;
  watermark?: Watermark;
  format?: SummaryType;
  removeAudio?: boolean;
  successUrl?: string;
  failureUrl?: string;
}
 
class SummaryTarget extends WorkerTarget<SummaryJob> {
  summarizer: Summarizer;
 
  constructor(summarizer: SummaryJob) {
    super(summarizer);
  }
}
 
class Summarizer extends WorkerConfig<SummaryTarget> {
  options: SummaryOptions;
 
  constructor(apiKey: string, opts: SummaryOptions) {
    super(apiKey, SummaryTarget);
    this.options = opts;
  }
  getExecutable(fromConfig: string | Blob): Executable {
    const options = this.options;
 
    let config = new SummaryJob(this.apiKey).from(fromConfig).webhooks({
      successUrl: options.successUrl,
      failureUrl: options.failureUrl,
    });
 
    if (options.width) {
      config = config.width(150);
    }
 
    config = config.type(options.format ? options.format : "gif");
 
    if (options.watermark) {
      config = config.watermark(options.watermark);
    }
    options.removeAudio = !!options.removeAudio;
    Iif (options.removeAudio) {
      config = config.removeAudio(options.removeAudio);
    }
    return config;
  }
}
 
// MediaMachine
// ==============================
 
export class MediaMachine {
  apiKey: string;
 
  constructor(apiKey: string) {
    parseApiKey(apiKey);
    this.apiKey = apiKey;
  }
 
  transcodeToWebm(opts: TranscodeWebmOptions): TranscoderWebm {
    return new TranscoderWebm(this.apiKey, opts);
  }
 
  transcodeToMp4(opts: TranscodeMp4Options): TranscoderMp4 {
    return new TranscoderMp4(this.apiKey, opts);
  }
 
  thumbnail(opts: ThumbnailOptions): Thumbnailer {
    return new Thumbnailer(this.apiKey, opts);
  }
 
  summary(opts: SummaryOptions): Summarizer {
    return new Summarizer(this.apiKey, opts);
  }
 
  textWatermark(text: string, Eopts: TextWatermarkOptions = {}): TextWatermark {
    return new TextWatermark(text, opts);
  }
  imageWatermark(Iopts: ImageWatermarkOptions = {}): ImageWatermark {
    return new ImageWatermark(opts);
  }
}
 
export { Job } from "./job";