All files / src block-blob-client.ts

88.46% Statements 23/26
81.48% Branches 22/27
100% Functions 8/8
88.46% Lines 23/26

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                                                  4x             17x 17x             6x             5x 2x     3x       4x 4x     4x       4x           2x           2x 1x     1x       1x       1x 1x       1x 1x             5x 1x             3x    
import { StorageTransport, storageRequestError } from './internal/storage-request'
import type { StorageClientOptions, StorageCredential } from './internal/storage-request'
 
export interface BlobGetPropertiesResponse {
  etag?: string
  lastModified?: Date
  lastAccessed?: Date
  createdOn?: Date
  metadata: Record<string, string>
}
 
export interface BlobDeleteIfExistsResponse {
  succeeded: boolean
  errorCode?: string
}
 
export interface BlobUploadOptions {
  /** Defaults to `application/octet-stream`. */
  contentType?: string
}
 
export interface BlobDeleteOptions {
  deleteSnapshots?: 'include' | 'only'
}
 
const metadataPrefix = 'x-ms-meta-'
 
export class BlockBlobClient {
  public readonly url: string
  private readonly transport: StorageTransport
 
  public constructor(url: string, credential?: StorageCredential, options?: StorageClientOptions) {
    this.url = url
    this.transport = new StorageTransport(credential, options)
  }
 
  public async upload(
    body: string | BufferSource | Blob,
    options: BlobUploadOptions = {},
  ): Promise<Response> {
    const response = await this.transport.send('PUT', this.url, {
      headers: {
        'x-ms-blob-type': 'BlockBlob',
        'Content-Type': options.contentType ?? 'application/octet-stream',
      },
      body,
    })
    if (!response.ok) {
      throw storageRequestError('Blob upload', response)
    }
 
    return response
  }
 
  public async download(offset = 0, count?: number): Promise<Response> {
    const end = count === undefined ? '' : String(offset + count - 1)
    const response = await this.transport.send('GET', this.url, {
      headers: offset > 0 || count !== undefined ? { Range: `bytes=${offset}-${end}` } : {},
    })
    Iif (!response.ok) {
      throw storageRequestError('Blob download', response)
    }
 
    return response
  }
 
  public async deleteIfExists(
    options: BlobDeleteOptions = {},
  ): Promise<BlobDeleteIfExistsResponse> {
    const response = await this.transport.send('DELETE', this.url, {
      headers:
        options.deleteSnapshots === undefined
          ? {}
          : { 'x-ms-delete-snapshots': options.deleteSnapshots },
    })
    if (response.status === 404) {
      return { succeeded: false, errorCode: 'BlobNotFound' }
    }
 
    Iif (!response.ok) {
      throw storageRequestError('Blob delete', response)
    }
 
    return { succeeded: true }
  }
 
  public async getProperties(): Promise<BlobGetPropertiesResponse> {
    const response = await this.transport.send('HEAD', this.url)
    Iif (!response.ok) {
      throw storageRequestError('Blob getProperties', response)
    }
 
    const { headers } = response
    return {
      etag: headers.get('etag') ?? undefined,
      lastModified: parseDate(headers.get('last-modified')),
      lastAccessed: parseDate(headers.get('x-ms-last-access-time')),
      createdOn: parseDate(headers.get('x-ms-creation-time')),
      metadata: Object.fromEntries(
        Array.from(headers)
          .filter(([name]) => name.startsWith(metadataPrefix))
          .map(([name, value]) => [name.slice(metadataPrefix.length), value]),
      ),
    }
  }
}
 
function parseDate(value: string | null): Date | undefined {
  return value === null ? undefined : new Date(value)
}