diff --git a/server/src/http-fetch-helpers.ts b/server/src/http-fetch-helpers.ts index 39eabbc0d4..f1b1f1c5cd 100644 --- a/server/src/http-fetch-helpers.ts +++ b/server/src/http-fetch-helpers.ts @@ -1,11 +1,11 @@ import { once } from 'events'; import { http, https } from 'follow-redirects'; -import { IncomingHttpHeaders, IncomingMessage } from 'http'; -import { Readable } from 'stream'; +import { IncomingMessage } from 'http'; +import type { Readable } from 'stream'; export type HttpFetchResponseType = 'json' | 'text' | 'buffer' | 'readable'; export interface HttpFetchOptions { - url: string|URL; + url: string | URL; family?: 4 | 6; method?: string; headers?: HeadersInit; @@ -71,12 +71,22 @@ export async function getNpmPackageInfo(pkg: string) { url: `https://registry.npmjs.org/${pkg}`, // force ipv4 in case of busted ipv6. family: 4, + responseType: 'json', }); return body; } -export function setFetchAcceptOptions(accept: string, headers: Headers) { - headers.set('Accept', accept); +export function getHttpFetchAccept(responseType: HttpFetchResponseType) { + const { accept } = getHttpFetchParser(responseType); + return accept; +} + +export function setDefaultHttpFetchAccept(headers: Headers, responseType: HttpFetchResponseType) { + if (headers.has('Accept')) + return; + const { accept } = getHttpFetchParser(responseType); + if (accept) + headers.set('Accept', accept); } export function fetchStatusCodeOk(statusCode: number) { @@ -90,7 +100,7 @@ export function checkStatus(statusCode: number) { export interface HttpFetchResponse { statusCode: number; - headers: IncomingHttpHeaders; + headers: Headers; body: T; } @@ -106,6 +116,10 @@ export function getHttpFetchParser(responseType: HttpFetchResponseType) { return BufferParser; } +export function parseResponseType(readable: IncomingMessage, responseType: HttpFetchResponseType) { + return getHttpFetchParser(responseType).parse(readable); +} + export async function httpFetch>(options: T): Promise> { const headers = new Headers(options.headers); - const parser = getHttpFetchParser(options.responseType); + setDefaultHttpFetchAccept(headers, options.responseType); - if (parser.accept) - setFetchAcceptOptions(parser.accept, headers); + const parser = getHttpFetchParser(options.responseType); const { url } = options; const isSecure = url.toString().startsWith('https:'); @@ -144,7 +157,7 @@ export async function httpFetch { } console.log('installing package', pkg, version); - const { body: tarball } = await fetchBuffer(`${registry.versions[version].dist.tarball}`, { + const { body: tarball } = await httpFetch( { + url: `${registry.versions[version].dist.tarball}`, // force ipv4 in case of busted ipv6. family: 4, }); diff --git a/server/src/services/service-control.ts b/server/src/services/service-control.ts index c736867f49..1d0ee90dfe 100644 --- a/server/src/services/service-control.ts +++ b/server/src/services/service-control.ts @@ -1,5 +1,5 @@ import fs from 'fs'; -import { fetchJSON } from "../http-fetch-helpers"; +import { httpFetch } from "../http-fetch-helpers"; import { ScryptedRuntime } from "../runtime"; export class ServiceControl { @@ -23,7 +23,8 @@ export class ServiceControl { const webhookUpdate = process.env.SCRYPTED_WEBHOOK_UPDATE; if (webhookUpdate) { const webhookUpdateAuthorization = process.env.SCRYPTED_WEBHOOK_UPDATE_AUTHORIZATION; - await fetchJSON(webhookUpdate, { + await httpFetch({ + url: webhookUpdate, headers: { Authorization: webhookUpdateAuthorization, }