All files / src/implementation blob.ts

96.88% Statements 62/64
85% Branches 17/20
100% Functions 11/11
96.72% Lines 59/61
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                                          5x 5x 5x 5x             5x           46x 46x 46x 30x 30x 30x 16x 1x     1x 1x   1x 15x 15x 12x   3x 3x   15x   46x 46x     5x 117x     5x 14x     5x 7x 6x 6x 6x     6x   1x         1x       56x 19x 17x     51x 17x   34x     17x   2x     5x 2x     3x     2x 2x 5x   2x 2x 2x 5x 33x     2x       5x 25x   5x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @file Provides a Blob-like wrapper for various binary types (including the
 * native Blob type). This makes it possible to upload types like ArrayBuffers,
 * making uploads possible in environments without the native Blob type.
 */
import * as fs from './fs';
import * as string from './string';
import { StringFormat } from './string';
import * as type from './type';
 
/**
 * @param opt_elideCopy If true, doesn't copy mutable input data
 *     (e.g. Uint8Arrays). Pass true only if you know the objects will not be
 *     modified after this blob's construction.
 */
export class FbsBlob {
  private data_: Blob | Uint8Array;
  private size_: number;
  private type_: string;
 
  constructor(data: Blob | Uint8Array | ArrayBuffer, opt_elideCopy?: boolean) {
    let size: number = 0;
    let blobType: string = '';
    if (type.isNativeBlob(data)) {
      this.data_ = data as Blob;
      size = (data as Blob).size;
      blobType = (data as Blob).type;
    } else if (data instanceof ArrayBuffer) {
      Iif (opt_elideCopy) {
        this.data_ = new Uint8Array(data);
      } else {
        this.data_ = new Uint8Array(data.byteLength);
        this.data_.set(new Uint8Array(data));
      }
      size = this.data_.length;
    } else Eif (data instanceof Uint8Array) {
      if (opt_elideCopy) {
        this.data_ = data as Uint8Array;
      } else {
        this.data_ = new Uint8Array(data.length);
        this.data_.set(data as Uint8Array);
      }
      size = data.length;
    }
    this.size_ = size;
    this.type_ = blobType;
  }
 
  size(): number {
    return this.size_;
  }
 
  type(): string {
    return this.type_;
  }
 
  slice(startByte: number, endByte: number): FbsBlob | null {
    if (type.isNativeBlob(this.data_)) {
      let realBlob = this.data_ as Blob;
      let sliced = fs.sliceBlob(realBlob, startByte, endByte);
      Iif (sliced === null) {
        return null;
      }
      return new FbsBlob(sliced);
    } else {
      let slice = new Uint8Array(
        (this.data_ as Uint8Array).buffer,
        startByte,
        endByte - startByte
      );
      return new FbsBlob(slice, true);
    }
  }
 
  static getBlob(...var_args: (string | FbsBlob)[]): FbsBlob | null {
    if (type.isNativeBlobDefined()) {
      var blobby: (Blob | Uint8Array | string)[] = var_args.map(function(
        val: string | FbsBlob
      ): Blob | Uint8Array | string {
        if (val instanceof FbsBlob) {
          return val.data_;
        } else {
          return val;
        }
      });
      return new FbsBlob(fs.getBlob.apply(null, blobby));
    } else {
      let uint8Arrays: Uint8Array[] = var_args.map(function(
        val: string | FbsBlob
      ): Uint8Array {
        if (type.isString(val)) {
          return string.dataFromString(StringFormat.RAW, val as string).data;
        } else {
          // Blobs don't exist, so this has to be a Uint8Array.
          return (val as FbsBlob).data_ as Uint8Array;
        }
      });
      let finalLength = 0;
      uint8Arrays.forEach(function(array: Uint8Array): void {
        finalLength += array.byteLength;
      });
      let merged = new Uint8Array(finalLength);
      let index = 0;
      uint8Arrays.forEach(function(array: Uint8Array) {
        for (let i = 0; i < array.length; i++) {
          merged[index++] = array[i];
        }
      });
      return new FbsBlob(merged, true);
    }
  }
 
  uploadData(): Blob | Uint8Array {
    return this.data_;
  }
}