Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove axios package #1314

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ async function createExampleConfigFile() {

try {
const resp = await sendHttpRequest({ url })
await writeFile(outputFilePath, resp.data, { encoding: 'utf8' })
await writeFile(outputFilePath, resp.data as string, { encoding: 'utf8' })
} catch {
const ymlConfig = `
probes:
- id: '1'
requests:
- url: http://example.com

db_limit:
max_db_size: 1000000000
deleted_data: 1
Expand Down
6 changes: 4 additions & 2 deletions src/components/config/parser/insomnia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import type { Config } from '../../../interfaces/config'
import yml from 'js-yaml'
import { compile as compileTemplate } from 'handlebars'
import type { AxiosRequestHeaders, Method } from 'axios'

import Joi from 'joi'
import { Method } from '../../../interfaces/http-client'

const envValidator = Joi.object({
scheme: Joi.array().items(Joi.string()),
Expand Down Expand Up @@ -145,13 +146,14 @@ function mapInsomniaRequestToConfig(req: unknown) {
// eslint-disable-next-line camelcase
const url = compileTemplate(res.url)({ base_url: baseUrl })
const authorization = getAuthorizationHeader(res)
let headers: AxiosRequestHeaders | undefined
let headers: { [key: string]: unknown } | undefined
if (authorization)
headers = {
authorization,
}
if (res.headers) {
if (headers === undefined) headers = {}

for (const h of res.headers) {
headers[h.name] = h.value
}
Expand Down
16 changes: 8 additions & 8 deletions src/components/config/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ export async function parseByType(
const config = isUrl(source)
? await getConfigFileFromUrl(source)
: await readFile(source, { encoding: 'utf8' })
const isEmpty = config.length === 0
const isEmpty = (config as string).length === 0

if (isEmpty) {
throw new Error(`Failed to read ${source}, the file is empty.`)
}

const extension = path.extname(source)

if (type === 'har') return parseHarFile(config)
if (type === 'text') return parseConfigFromText(config)
if (type === 'postman') return parseConfigFromPostman(config)
if (type === 'sitemap') return parseConfigFromSitemap(config)
if (type === 'har') return parseHarFile(config as string)
if (type === 'text') return parseConfigFromText(config as string)
if (type === 'postman') return parseConfigFromPostman(config as string)
if (type === 'sitemap') return parseConfigFromSitemap(config as string)
if (type === 'insomnia')
return parseInsomnia(config, extension.replace('.', ''))
return parseInsomnia(config as string, extension.replace('.', ''))

return parseConfigByExt({
config,
config: config as string,
extension,
source,
})
}

async function getConfigFileFromUrl(url: string) {
const config = await fetchConfigFile(url)
const isEmpty = config.length === 0
const isEmpty = (config as string).length === 0

if (isEmpty) {
throw new Error(
Expand Down
156 changes: 156 additions & 0 deletions src/components/http-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**********************************************************************************
* MIT License *
* *
* Copyright (c) 2021 Hyperjump Technology *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**********************************************************************************/

import { ReadableStream } from 'node:stream/web'
import {
HttpClientHeaderList,
HttpClientHeaders,
HttpClientResponseType,
HttpClientRequestOptions,
} from '../../interfaces/http-client'
import { Agent, type HeadersInit } from 'undici'

export class HttpClientResponse {
_fetchResponse: Response
_headers: HttpClientHeaderList | null = null
_isStreamResponseType: boolean
_data: unknown

constructor(
fetchResponse: Response,
isStreamResponseType: boolean,
data?: unknown
) {
this._fetchResponse = fetchResponse
this._isStreamResponseType = isStreamResponseType
this._data = data
}

get headers(): HttpClientHeaders {
if (!this._headers) {
this._headers = new HttpClientHeaderList()

for (const [k, v] of this._fetchResponse.headers.entries()) {
this._headers!.set(k, v)
}
}

return this._headers
}

get ok(): boolean {
return this._fetchResponse.ok
}

get status(): number {
return this._fetchResponse.status
}

get statusText(): string {
return this._fetchResponse.statusText
}

get type(): HttpClientResponseType {
return this._fetchResponse.type
}

json(): Promise<unknown> {
if (this._data) return Promise.resolve(this._data)
return this._fetchResponse.json()
}

text(): Promise<string> {
if (this._data) return Promise.resolve(this._data as string)
return this._fetchResponse.text()
}

// static error (): Response;

get data(): ReadableStream | unknown {
if (this._isStreamResponseType) {
const reader = this._fetchResponse.body?.getReader()
return new ReadableStream({
start(controller) {
return pump()

function pump() {
if (!reader) return

return reader
.read()
.then(({ done, value }: { done: any; value?: any }): any => {

Check warning on line 102 in src/components/http-client/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 102 in src/components/http-client/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 102 in src/components/http-client/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
// When no more data needs to be consumed, close the stream
if (done) {
controller.close()
return
}

// Enqueue the next data chunk into our target stream
controller.enqueue(value)
return pump()
})
}
},
})
}

return this._data
}
}

export const httpClient = async (
url: string,
requestOptions: HttpClientRequestOptions,
isStreamResponseType: boolean = false
): Promise<HttpClientResponse> => {
const fetchResponse = await fetch(url, {
body: requestOptions.body === '' ? undefined : requestOptions.body,
redirect: requestOptions.redirect,
dispatcher:
requestOptions.allowUnauthorizedSsl === undefined
? undefined
: new Agent({
connect: {
rejectUnauthorized: !requestOptions.allowUnauthorizedSsl,
keepAlive: requestOptions.keepAlive,
},
}),

headers: requestOptions.headers as HeadersInit,
keepalive: requestOptions.keepAlive,
method: requestOptions.method,
signal: requestOptions.signal,
})

let data: unknown = ''
if (!isStreamResponseType) {
try {
data = await fetchResponse.json()
} catch {
data = await fetchResponse.text()
}
}

return new HttpClientResponse(fetchResponse, isStreamResponseType, data)
}
1 change: 1 addition & 0 deletions src/components/probe/prober/http/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ describe('probingHTTP', () => {
)
const request = {
url: 'https://example.com/redirect-1',
followRedirects: 2,
} as RequestConfig

// act
Expand Down
Loading
Loading