All files / src/modules bytes.ts

80% Statements 20/25
50% Branches 3/6
75% Functions 3/4
80% Lines 20/25

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 11933x     33x 33x 33x   33x 33x   33x                   33x           54x                     54x   54x                             33x                                             33x         72x       72x           72x                 33x         2x       2x           2x    
import { Optional, Types } from 'cafe-utility'
import type { BeeRequestOptions, DownloadOptions, RedundantUploadOptions, ReferenceInformation } from '../types'
import { UploadResult } from '../types'
import { Bytes } from '../utils/bytes'
import { prepareRequestHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { ResourceLocator } from '../utils/resource-locator'
import { makeTagUid, prepareDownloadOptions } from '../utils/type'
import { BatchId, Reference } from '../utils/typed-bytes'
 
const endpoint = 'bytes'
 
/**
 * Upload data to a Bee node
 *
 * @param requestOptions Options for making requests
 * @param data            Data to be uploaded
 * @param postageBatchId  Postage BatchId that will be assigned to uploaded data
 * @param options         Additional options like tag, encryption, pinning
 */
export async function upload(
  requestOptions: BeeRequestOptions,
  data: string | Uint8Array,
  postageBatchId: BatchId,
  options?: RedundantUploadOptions,
): Promise<UploadResult> {
  const response = await http<unknown>(requestOptions, {
    url: endpoint,
    method: 'post',
    responseType: 'json',
    data,
    headers: {
      'content-type': 'application/octet-stream',
      ...prepareRequestHeaders(postageBatchId, options),
    },
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return {
    reference: new Reference(Types.asHexString(body.reference)),
    tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
    historyAddress: response.headers['swarm-act-history-address']
      ? Optional.of(new Reference(response.headers['swarm-act-history-address']))
      : Optional.empty(),
  }
}
 
/**
 * Requests content length for a reference
 *
 * @param requestOptions Options for making requests
 * @param hash Bee content reference
 */
export async function head(
  requestOptions: BeeRequestOptions,
  reference: Reference | Uint8Array | string,
): Promise<ReferenceInformation> {
  reference = new Reference(reference)
 
  const response = await http<void>(requestOptions, {
    url: `${endpoint}/${reference}`,
    method: 'head',
    responseType: 'json',
  })
 
  return {
    contentLength: parseInt(response.headers['content-length'] as string),
  }
}
 
/**
 * Download data as a byte array
 *
 * @param requestOptions Options for making requests
 * @param hash Bee content reference
 */
export async function download(
  requestOptions: BeeRequestOptions,
  resource: ResourceLocator,
  options?: DownloadOptions,
): Promise<Bytes> {
  Iif (options) {
    options = prepareDownloadOptions(options)
  }
 
  const response = await http<unknown>(requestOptions, {
    responseType: 'arraybuffer',
    url: `${endpoint}/${resource}`,
    headers: prepareRequestHeaders(null, options),
  })
 
  return new Bytes(response.data as ArrayBuffer)
}
 
/**
 * Download data as a readable stream
 *
 * @param requestOptions Options for making requests
 * @param hash Bee content reference
 */
export async function downloadReadable(
  requestOptions: BeeRequestOptions,
  resource: ResourceLocator,
  options?: DownloadOptions,
): Promise<ReadableStream<Uint8Array>> {
  Iif (options) {
    options = prepareDownloadOptions(options)
  }
 
  const response = await http<ReadableStream<Uint8Array>>(requestOptions, {
    responseType: 'stream',
    url: `${endpoint}/${resource}`,
    headers: prepareRequestHeaders(null, options),
  })
 
  return response.data
}