All files blob.ts

73.33% Statements 33/45
57.14% Branches 12/21
85.71% Functions 6/7
70% Lines 28/40

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 951x 1x 1x 1x                                             1x                 29x 9x 9x 20x 8x 8x 12x 12x 12x       29x 29x     1x 29x   12x   17x 17x 17x     1x 29x 29x     29x     1x                                               1x  
export enum Store {
  S3 = "s3",
  AZURE_BLOB = "azure",
  GOOGLE_BLOB = "gcp",
} // these are also protocol prefixes
 
export interface AWSCreds {
  accessKeyId: string;
  secretAccessKey: string;
  region: string;
  type: Store.S3;
}
 
export interface AzureCreds {
  accountName: string;
  accountKey: string;
  type: Store.AZURE_BLOB;
}
 
export interface GCPCreds {
  json: string;
  type: Store.GOOGLE_BLOB;
}
 
export type Credentials = AWSCreds | AzureCreds | GCPCreds;
 
export class Blob {
  blobStore: Store;
  blobBucket: string;
  blobKey: string;
  awsCreds?: AWSCreds;
  azureCreds?: AzureCreds;
  gcpCreds?: GCPCreds;
 
  constructor(creds: Credentials, bucket: string, key: string) {
    if (creds.type === Store.S3) {
      this.awsCreds = creds;
      this.blobStore = creds.type;
    } else if (creds.type === Store.AZURE_BLOB) {
      this.azureCreds = creds;
      this.blobStore = creds.type;
    } else Eif (creds.type === Store.GOOGLE_BLOB) {
      this.gcpCreds = creds;
      this.blobStore = creds.type;
    } else {
      throw new Error("Invalid Credential type");
    }
    this.blobBucket = bucket;
    this.blobKey = key;
  }
 
  toApiCredentials() {
    if (!!this.gcpCreds) {
      // special case, we want the JSON embedded
      return JSON.parse(this.gcpCreds.json);
    }
    const creds = this.awsCreds || this.azureCreds || undefined;
    const omitSingle = (key: string, { [key]: _, ...obj }) => obj;
    return omitSingle("type", creds);
  }
 
  toApiUrl(): string {
    const protocol = this.blobStore;
    const url = `${protocol}://${encodeURIComponent(
      this.blobBucket
    )}/${encodeURIComponent(this.blobKey)}`;
    return url;
  }
 
  toJSON() {
    const json: any = {
      store: this.blobStore,
      bucket: this.blobBucket,
      key: this.blobKey,
    };
 
    if (!!this.awsCreds) {
      delete this.awsCreds.type;
      json.awsCreds = this.awsCreds;
    }
 
    if (!!this.azureCreds) {
      delete this.azureCreds.type;
      json.azureCreds = this.azureCreds;
    }
 
    if (!!this.gcpCreds) {
      delete this.gcpCreds.type;
      json.gcpCreds = this.gcpCreds;
    }
 
    return json;
  }
}