All files / utils response.js

98.89% Statements 179/181
97.95% Branches 48/49
85.71% Functions 6/7
98.89% Lines 179/181

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 1822x 2x 2x 2x 2x 2x 1x 7x 7x 1x 1x 6x 6x 7x 12x 12x 12x 12x 6x 6x 6x 7x 2x 2x 2x 2x 2x 2x 2x 19x 14x 14x 14x 19x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x     22x 35x 35x 10x 10x 35x 12x 12x 2x 2x 2x 2x 2x 2x 19x 19x 12x 12x 7x 7x 7x 7x 7x 7x 7x 19x 15x 15x 12x 12x 12x 12x 12x 7x 7x 7x 2x 2x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 858x 858x 648x 648x 648x 858x 15x 15x 15x 210x 858x 15x 15x 210x 858x 36x 36x 210x 858x 18x 18x 18x 18x 192x 858x 9x 9x 9x 9x 9x 858x 183x 183x 858x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 16x 16x 16x 16x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 18x 18x 15x 15x 15x 15x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
/**
 * Generator function to stream text from response.
 *
 * @param {Response} response - response from fetch
 * @yields {string} text from the response stream.
 */
export async function * toTextStream (response) {
  const { body } = response
  if (!body) {
    return
  }
  const reader = body.getReader()
  const decoder = new TextDecoder()
  while (true) {
    // wait for next encoded chunk
    const { done, value } = await reader.read()
    // check if stream is done
    if (done) { break }
    // Decodes data chunk and yields it
    yield (decoder.decode(value))
  }
}
 
/**
 * Gets parsed LinkHeader. Returns empty LinkHeader if invalid or absent
 * @param {Response|string} responseOrHeader - response from fetch
 * @returns {LinkHeader} parser Link header
 */
export function linkHeaderOf (responseOrHeader) {
  if (responseOrHeader instanceof Response) {
    const header = responseOrHeader.headers.get('Link')
    return parseLinkHttpHeader(header)
  }
  if (typeof responseOrHeader === 'string') {
    return parseLinkHttpHeader(responseOrHeader)
  }
  return parseLinkHttpHeader(null)
}
 
/**
 * Parses "X-Has-More" as well as "Has-More" header from response to
 * detect if there is more data to fetch
 * Note: "Has-More" has higher priority than "X-Has-More"
 * @param {Response} response - response from fetch
 * @returns {boolean} parsed "Has more" response header
 */
export function parseHasMoreHeader (response) {
  if (!(response instanceof Response)) {
    return false
  }
  for (const headerName of ['Has-More', 'X-Has-More']) {
    const header = response.headers.get(headerName)
    if (header) {
      return header.toLowerCase() === 'true'
    }
  }
  return false
}
 
/**
 * Parses Link HTTP header used for pagination only
 * @param {string?} header - response from fetch
 * @returns {LinkHeader} parser Link header
 */
function parseLinkHttpHeader (header) {
  if (!header || header.length <= 0) {
    return { entries: [], byRel: {} }
  }
 
  const entries = Iterator.from(splitLinkEntries(header))
    .map(parseLinkHeaderEntry)
    .filter(result => result != null)
    .toArray()
 
  const byRel = /** @type {LinkHeader["byRel"]} */({})
  for (const entry of entries) {
    const { params: { rel } } = entry
    if (!rel) { continue }
    for (const relEntry of rel.split(' ').filter(Boolean)) {
      byRel[relEntry] ??= []
      byRel[relEntry].push(entry)
    }
  }
 
  return { entries, byRel }
}
 
/**
 * @param {string} header - header object
 */
function splitLinkEntries (header) {
  const entries = []
  let subEntry = []
 
  let current = ''
  let inQuotes = false
  let inAngleBrackets = false
  let firstAngleBrackets = true
 
  for (let i = 0, e = header.length; i < e; i++) {
    const char = header[i]
    if ((inAngleBrackets && char !== '>') || (inQuotes && char !== '"')) {
      current += char
      continue
    }
    if (char === '<' && firstAngleBrackets) {
      inAngleBrackets = true
      firstAngleBrackets = false
    }
 
    if (char === '>') {
      inAngleBrackets = false
    }
 
    if (char === '"') {
      inQuotes = !inQuotes
    }
 
    if (char === ';') {
      subEntry.push(current)
      current = ''
      continue
    }
 
    if (char === ',') {
      subEntry.push(current)
      entries.push(subEntry)
      current = ''
      subEntry = []
      firstAngleBrackets = true
    } else {
      current += char
    }
  }
 
  if (current) {
    subEntry.push(current)
    entries.push(subEntry)
  }
  return entries
}
 
/**
 * @param {string[]} headerEntry - `Link` header entry
 * @returns {LinkHeaderEntry|null} parser header entry, or null if invalid
 */
function parseLinkHeaderEntry (headerEntry) {
  const [uri, ...params] = headerEntry
  const match = uri.match(/<([^>]+)>/)
  if (!match) { return null }
 
  const url = match[1]
 
  const parsedParams = Object.fromEntries(
    Iterator.from(params)
      .map(p => p.trim())
      .filter(Boolean)
      .map(param => param.match(/^([^=]+)="?([^"]+)"?$/))
      .filter(match => match != null)
      .map(match => {
        const [,key, value] = match
        return [key, value]
      })
      .toArray(),
  )
  return { url, params: parsedParams }
}
 
/**
 * @typedef {object} LinkHeader
 *
 * @property {LinkHeaderEntry[]} entries - link entries
 * @property {{[rel: string]: LinkHeaderEntry[]}} byRel - link entries grouped by rel
 */
 
/**
 * @typedef {object} LinkHeaderEntry
 *
 * @property {string} url - link entry URI
 * @property {{[key: string]: string}} params - link entry parameters
 */