All files / src/utils headers.ts

87.32% Statements 62/71
79.48% Branches 31/39
100% Functions 5/5
87.32% Lines 62/71

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 152 153 154 15533x   33x 33x 33x   33x 6x 6x 6x   6x               6x           6x   6x 6x           6x 6x           33x       212x   212x 2x 210x 108x 108x     212x 185x     27x   27x       27x 2x     27x 4x     27x 7x     27x 1x     27x 4x     27x 4x     27x 8x     27x       27x       27x       27x 1x     27x 1x     27x 1x     27x 3x     27x 1x     27x 3x     27x       27x       27x       212x 114x     98x   98x                
import { Types } from 'cafe-utility'
import { EnvelopeWithBatchId, FileHeaders } from '../types'
import { BeeError } from './error'
import { convertEnvelopeToMarshaledStamp } from './stamps'
import { BatchId, PublicKey, Reference } from './typed-bytes'
 
export function readFileHeaders(headers: Record<string, string>): FileHeaders {
  const name = readContentDispositionFilename(headers['content-disposition'])
  const tagUid = readTagUid(headers['swarm-tag-uid'])
  const contentType = headers['content-type'] || undefined
 
  return {
    name,
    tagUid,
    contentType,
  }
}
 
function readContentDispositionFilename(header: string | null): string {
  Iif (!header) {
    throw new BeeError('missing content-disposition header')
  }
 
  // Regex was found here
  // https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header
  const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i)
 
  if (dispositionMatch && dispositionMatch.length > 0) {
    return dispositionMatch[1]
  }
  throw new BeeError('invalid content-disposition header')
}
 
function readTagUid(header: string | null): number | undefined {
  if (!header) {
    return undefined
  }
 
  return parseInt(header, 10)
}
 
export function prepareRequestHeaders(
  stamp: BatchId | Uint8Array | string | EnvelopeWithBatchId | null,
  nullableOptions?: unknown,
): Record<string, string> {
  const headers: Record<string, string> = {}
 
  if (isEnvelopeWithBatchId(stamp)) {
    headers['swarm-postage-stamp'] = convertEnvelopeToMarshaledStamp(stamp).toHex()
  } else if (stamp) {
    stamp = new BatchId(stamp)
    headers['swarm-postage-batch-id'] = stamp.toHex()
  }
 
  if (!nullableOptions) {
    return headers
  }
 
  const options = Types.asObject(nullableOptions)
 
  Iif (options.size) {
    headers['content-length'] = String(options.size)
  }
 
  if (options.contentType) {
    headers['content-type'] = String(options.contentType)
  }
 
  if (options.redundancyLevel) {
    headers['swarm-redundancy-level'] = String(options.redundancyLevel)
  }
 
  if (Types.isBoolean(options.act)) {
    headers['swarm-act'] = String(options.act)
  }
 
  if (Types.isBoolean(options.pin)) {
    headers['swarm-pin'] = String(options.pin)
  }
 
  if (Types.isBoolean(options.encrypt)) {
    headers['swarm-encrypt'] = options.encrypt.toString()
  }
 
  if (options.tag) {
    headers['swarm-tag'] = String(options.tag)
  }
 
  if (Types.isBoolean(options.deferred)) {
    headers['swarm-deferred-upload'] = options.deferred.toString()
  }
 
  Iif (options.redundancyStrategy) {
    headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy)
  }
 
  Iif (Types.isBoolean(options.fallback)) {
    headers['swarm-redundancy-fallback-mode'] = options.fallback.toString()
  }
 
  Iif (options.timeoutMs) {
    headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs)
  }
 
  if (options.indexDocument) {
    headers['swarm-index-document'] = String(options.indexDocument)
  }
 
  if (options.errorDocument) {
    headers['swarm-error-document'] = String(options.errorDocument)
  }
 
  if (options.actPublisher) {
    headers['swarm-act-publisher'] = new PublicKey(options.actPublisher as any).toCompressedHex()
  }
 
  if (options.actHistoryAddress) {
    headers['swarm-act-history-address'] = new Reference(options.actHistoryAddress as any).toHex()
  }
 
  if (options.actTimestamp) {
    headers['swarm-act-timestamp'] = String(options.actTimestamp)
  }
 
  if (options.actPublisher || options.actHistoryAddress || options.actTimestamp) {
    headers['swarm-act'] = 'true'
  }
 
  Iif (options.gasPrice) {
    headers['gas-price'] = String(options.gasPrice)
  }
 
  Iif (options.gasLimit) {
    headers['gas-limit'] = String(options.gasLimit)
  }
 
  return headers
}
 
function isEnvelopeWithBatchId(value: unknown): value is EnvelopeWithBatchId {
  if (!Types.isObject(value)) {
    return false
  }
 
  const envelope = value as EnvelopeWithBatchId
 
  return (
    envelope.issuer !== undefined &&
    envelope.index !== undefined &&
    envelope.signature !== undefined &&
    envelope.timestamp !== undefined &&
    envelope.batchId !== undefined
  )
}