Commit | Line | Data |
---|---|---|
ded253e2 | 1 | import axios from 'axios' |
2b099bf7 | 2 | import nodeFetch, { |
49ac634f JB |
3 | type RequestInfo as NodeFetchRequestInfo, |
4 | type ResponseInit as NodeFetchRequestInit | |
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( | |
67f3f2d6 | 15 | workerData!.input as URL | NodeFetchRequestInfo, |
49ac634f JB |
16 | workerData?.init as NodeFetchRequestInit |
17 | ) | |
18 | // The response is not structured-cloneable, so we return the response text body instead. | |
19 | return { | |
20 | text: await response.text() | |
21 | } | |
22 | }, | |
23 | fetch: async (workerData?: WorkerData) => { | |
24 | const response = await fetch( | |
67f3f2d6 | 25 | workerData!.input as URL | RequestInfo, |
49ac634f JB |
26 | workerData?.init as RequestInit |
27 | ) | |
28 | // The response is not structured-cloneable, so we return the response text body instead. | |
29 | return { | |
30 | text: await response.text() | |
31 | } | |
7e80208e JB |
32 | }, |
33 | axios: async (workerData?: WorkerData) => { | |
34 | const response = await axios({ | |
35 | method: 'get', | |
67f3f2d6 | 36 | url: workerData!.input as string, |
7e80208e JB |
37 | ...workerData?.axiosRequestConfig |
38 | }) | |
39 | return { | |
40 | text: response.data | |
41 | } | |
49ac634f JB |
42 | } |
43 | }) | |
44 | } | |
45 | } | |
46 | ||
fac9ce96 | 47 | export const httpClientWorker = new HttpClientWorker() |