All files / src container-client.ts

61.11% Statements 22/36
37.5% Branches 6/16
81.81% Functions 9/11
56.25% Lines 18/32

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                                          2x                 22x 22x 22x 22x       14x   18x 3x     14x 11x       2x 2x       2x       2x                                                               4x       4x 4x 5x        
import { BlockBlobClient } from './block-blob-client'
import type { BlobDeleteIfExistsResponse } from './block-blob-client'
import { listNames } from './internal/blob-listing'
import type { ListOptions } from './internal/paging'
import { StorageTransport, storageRequestError } from './internal/storage-request'
import type { StorageClientOptions, StorageCredential } from './internal/storage-request'
import { appendUrlPath } from './internal/storage-url'
 
export interface BlobItem {
  name: string
}
 
export interface ListBlobsFlatSegment {
  segment: { blobItems: BlobItem[] }
  continuationToken: string
}
 
export interface ContainerCreateIfNotExistsResponse {
  succeeded: boolean
}
 
const containerQuery = { restype: 'container' }
 
export class ContainerClient {
  public readonly url: string
  private readonly credential: StorageCredential | undefined
  private readonly options: StorageClientOptions | undefined
  private readonly transport: StorageTransport
 
  public constructor(url: string, credential?: StorageCredential, options?: StorageClientOptions) {
    this.url = url
    this.credential = credential
    this.options = options
    this.transport = new StorageTransport(credential, options)
  }
 
  public getBlockBlobClient(blobName: string): BlockBlobClient {
    const segments = blobName.split('/')
    // URLs resolve dot segments, which would address a blob outside this container.
    if (segments.some((segment) => segment === '.' || segment === '..')) {
      throw new TypeError(`Blob name must not contain "." or ".." path segments: ${blobName}`)
    }
 
    const path = segments.map((segment) => encodeURIComponent(segment)).join('/')
    return new BlockBlobClient(appendUrlPath(this.url, path), this.credential, this.options)
  }
 
  public async createIfNotExists(): Promise<ContainerCreateIfNotExistsResponse> {
    const response = await this.transport.send('PUT', this.url, { query: containerQuery })
    Iif (response.status === 409) {
      return { succeeded: false }
    }
 
    Iif (!response.ok) {
      throw storageRequestError('Container create', response)
    }
 
    return { succeeded: true }
  }
 
  public async deleteIfExists(): Promise<BlobDeleteIfExistsResponse> {
    const response = await this.transport.send('DELETE', this.url, { query: containerQuery })
    if (response.status === 404) {
      return { succeeded: false, errorCode: 'ContainerNotFound' }
    }
 
    if (!response.ok) {
      throw storageRequestError('Container delete', response)
    }
 
    return { succeeded: true }
  }
 
  public async exists(): Promise<boolean> {
    const response = await this.transport.send('HEAD', this.url, { query: containerQuery })
    if (response.status === 404) {
      return false
    }
 
    if (!response.ok) {
      throw storageRequestError('Container exists check', response)
    }
 
    return true
  }
 
  public listBlobsFlat(): {
    byPage: (options?: ListOptions) => AsyncIterable<ListBlobsFlatSegment>
  } {
    return { byPage: (options) => this.listBlobPages(options) }
  }
 
  private async *listBlobPages(options?: ListOptions): AsyncGenerator<ListBlobsFlatSegment> {
    const pages = listNames(this.transport, this.url, 'Blob', containerQuery, options)
    for await (const { names, continuationToken } of pages) {
      yield { segment: { blobItems: names.map((name) => ({ name })) }, continuationToken }
    }
  }
}