Commit | Line | Data |
---|---|---|
ded253e2 | 1 | import axios from 'axios' |
2b099bf7 | 2 | import nodeFetch, { |
49ac634f | 3 | type RequestInfo as NodeFetchRequestInfo, |
3a502712 | 4 | type ResponseInit as NodeFetchRequestInit, |
49ac634f | 5 | } from 'node-fetch' |
ded253e2 JB |
6 | import { ThreadWorker } from 'poolifier' |
7 | ||
ef083f7b | 8 | import type { WorkerData, WorkerResponse } from './types.js' |
49ac634f | 9 | |
95e50651 | 10 | class HttpClientWorker extends ThreadWorker<WorkerData, WorkerResponse> { |
49ac634f JB |
11 | public constructor () { |
12 | super({ | |
13 | node_fetch: async (workerData?: WorkerData) => { | |
14 | const response = await nodeFetch( | |
3a502712 | 15 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
67f3f2d6 | 16 | workerData!.input as URL | NodeFetchRequestInfo, |
49ac634f JB |
17 | workerData?.init as NodeFetchRequestInit |
18 | ) | |
19 | // The response is not structured-cloneable, so we return the response text body instead. | |
20 | return { | |
3a502712 | 21 | text: await response.text(), |
49ac634f JB |
22 | } |
23 | }, | |
24 | fetch: async (workerData?: WorkerData) => { | |
25 | const response = await fetch( | |
3a502712 | 26 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
67f3f2d6 | 27 | workerData!.input as URL | RequestInfo, |
49ac634f JB |
28 | workerData?.init as RequestInit |
29 | ) | |
30 | // The response is not structured-cloneable, so we return the response text body instead. | |
31 | return { | |
3a502712 | 32 | text: await response.text(), |
49ac634f | 33 | } |
7e80208e JB |
34 | }, |
35 | axios: async (workerData?: WorkerData) => { | |
36 | const response = await axios({ | |
37 | method: 'get', | |
3a502712 | 38 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
67f3f2d6 | 39 | url: workerData!.input as string, |
3a502712 | 40 | ...workerData?.axiosRequestConfig, |
7e80208e JB |
41 | }) |
42 | return { | |
3a502712 | 43 | text: response.data, |
7e80208e | 44 | } |
3a502712 | 45 | }, |
49ac634f JB |
46 | }) |
47 | } | |
48 | } | |
49 | ||
fac9ce96 | 50 | export const httpClientWorker = new HttpClientWorker() |