All files / rusted-chromium api.ts

100% Statements 31/31
100% Branches 11/11
100% Functions 11/11
100% Lines 28/28

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    9x       9x     9x   9x     13x   5x   8x     9x   9x   9x 2x               9x 3x         9x 2x   2x     2x   2x 1x   1x   2x       9x 4x   4x       3x     9x 2x 2x    
import { Response as NodeFetchResponse } from 'node-fetch'
 
import { RESOLVE_VERSION } from './commons/loggerTexts'
import type { IMetadataResponse } from './interfaces/interfaces'
import type { IOSSettings } from './interfaces/os.interfaces'
import type { IListStore } from './interfaces/store.interfaces'
import { spinner } from './log/spinner'
 
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const fetch = require('node-fetch')
 
const CHROMIUM_TAGS_URL = 'https://chromium.googlesource.com/chromium/src/+refs'
 
function checkStatus(response: NodeFetchResponse) {
    if (!response.ok) {
        // TODO: check of response.error is correct
        throw new Error(`Status Code: ${response.status} ${(response as (NodeFetchResponse & { error: string })).error}`)
    }
    return response
}
 
const toJson = (response: Response): Promise<unknown> => response.json()
 
const toText = (response: Response): Promise<string> => response.text()
 
export async function fetchLocalStore(url: string): Promise<IListStore> {
    return fetch(url)
        .then(checkStatus)
        .then(toJson)
}
 
/**
 * Fetch all chromium tags (containing the version) via googlesource url
 */
export async function fetchChromiumTags(): Promise<string> {
    return fetch(CHROMIUM_TAGS_URL)
        .then(checkStatus)
        .then(toText)
}
 
export async function fetchBranchPosition(version: string): Promise<string> {
    spinner.start(RESOLVE_VERSION)
 
    return fetch(`https://omahaproxy.appspot.com/deps.json?version=${version}`)
        .then(checkStatus)
        .then(toJson)
        .then((response: { chromium_base_position?: string }) => response.chromium_base_position)
        .then((resolvedVersion: string | undefined) => {
            if (resolvedVersion) {
                spinner.success()
            } else {
                spinner.error()
            }
            return resolvedVersion
        })
}
 
export async function fetchChromeUrl(branchPosition: string, osSettings: IOSSettings): Promise<string | undefined> {
    const snapshotUrl = `https://www.googleapis.com/storage/v1/b/chromium-browser-snapshots/o?delimiter=/&prefix=${osSettings.url}/${branchPosition}/&fields=items(kind,mediaLink,metadata,name,size,updated),kind,prefixes,nextPageToken`
    // TODO: adjust field in request
    const chromeMetadataResponse: IMetadataResponse = await fetch(snapshotUrl)
        .then(checkStatus)
        .then(toJson)
 
    return chromeMetadataResponse.items?.find(item => item.name === `${osSettings.url}/${branchPosition}/chrome-${osSettings.filename}.zip`)?.mediaLink
}
 
export async function fetchChromeZipFile(url: string): Promise<NodeFetchResponse> {
    const response = await fetch(url)
    return checkStatus(response)
}