All files / src/modules bzz.ts

96.77% Statements 30/31
64.28% Branches 9/14
100% Functions 4/4
96.77% Lines 30/31

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15233x                     33x 33x 33x 33x   33x 33x 33x   33x                     33x             18x 1x     1x     18x                 18x   18x                               33x           5x           5x         5x                   33x           1x   1x           1x         1x                           33x           6x 6x   6x   6x                
import { Optional, Types } from 'cafe-utility'
import { Readable } from 'stream'
import {
  BeeRequestOptions,
  Collection,
  CollectionUploadOptions,
  DownloadOptions,
  FileData,
  FileUploadOptions,
  UploadResult,
} from '../types'
import { Bytes } from '../utils/bytes'
import { assertCollection } from '../utils/collection'
import { prepareRequestHeaders, readFileHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { ResourceLocator } from '../utils/resource-locator'
import { uploadTar } from '../utils/tar-uploader'
import { isReadable, makeTagUid } from '../utils/type'
import { BatchId, Reference } from '../utils/typed-bytes'
 
const bzzEndpoint = 'bzz'
 
/**
 * Upload single file
 *
 * @param requestOptions Options for making requests
 * @param data Files data
 * @param postageBatchId  Postage BatchId that will be assigned to uploaded data
 * @param name Name that will be attached to the uploaded file. Wraps the data into manifest with set index document.
 * @param options
 */
export async function uploadFile(
  requestOptions: BeeRequestOptions,
  data: string | Uint8Array | Readable | ArrayBuffer,
  postageBatchId: BatchId,
  name?: string,
  options?: FileUploadOptions,
): Promise<UploadResult> {
  if (isReadable(data) && !options?.contentType) {
    Iif (!options) {
      options = {}
    }
    options.contentType = 'application/octet-stream'
  }
 
  const response = await http<unknown>(requestOptions, {
    method: 'post',
    url: bzzEndpoint,
    data,
    headers: prepareRequestHeaders(postageBatchId, options),
    params: { name },
    responseType: 'json',
  })
 
  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(),
  }
}
 
/**
 * Download single file as a buffer
 *
 * @param requestOptions Options for making requests
 * @param hash Bee file or collection hash
 * @param path If hash is collection then this defines path to a single file in the collection
 */
export async function downloadFile(
  requestOptions: BeeRequestOptions,
  resource: ResourceLocator,
  path = '',
  options?: DownloadOptions,
): Promise<FileData<Bytes>> {
  const response = await http<ArrayBuffer>(requestOptions, {
    method: 'GET',
    responseType: 'arraybuffer',
    url: `${bzzEndpoint}/${resource}/${path}`,
    headers: prepareRequestHeaders(null, options),
  })
  const file = {
    ...readFileHeaders(response.headers as Record<string, string>),
    data: new Bytes(response.data),
  }
 
  return file
}
 
/**
 * Download single file as a readable stream
 *
 * @param requestOptions Options for making requests
 * @param hash Bee file or collection hash
 * @param path If hash is collection then this defines path to a single file in the collection
 */
export async function downloadFileReadable(
  requestOptions: BeeRequestOptions,
  reference: Reference,
  path = '',
  options?: DownloadOptions,
): Promise<FileData<ReadableStream<Uint8Array>>> {
  reference = new Reference(reference)
 
  const response = await http<ReadableStream<Uint8Array>>(requestOptions, {
    method: 'GET',
    responseType: 'stream',
    url: `${bzzEndpoint}/${reference}/${path}`,
    headers: prepareRequestHeaders(null, options),
  })
  const file = {
    ...readFileHeaders(response.headers as Record<string, string>),
    data: response.data,
  }
 
  return file
}
 
/*******************************************************************************************************************/
 
// Collections
 
/**
 * Upload collection
 * @param requestOptions Options for making requests
 * @param collection Collection of Uint8Array buffers to upload
 * @param postageBatchId  Postage BatchId that will be assigned to uploaded data
 * @param options
 */
export async function uploadCollection(
  requestOptions: BeeRequestOptions,
  collection: Collection,
  postageBatchId: BatchId,
  options?: CollectionUploadOptions,
): Promise<UploadResult> {
  assertCollection(collection)
  const response = await uploadTar(requestOptions, collection, 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(),
  }
}