"build": "pnpm build:clean && tsc",
"build:clean": "tsc --build --clean",
"start": "node dist/main.js",
- "start:httpd-echo": "node dist/httpd-echo.js",
+ "start:httpd-echo": "node httpd-echo.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
const parallelism = availableParallelism()
const requestUrl = 'http://localhost:8080/'
-for (const workerFunction of ['node_fetch', 'fetch']) {
+for (const workerFunction of ['node_fetch', 'fetch', 'axios']) {
const fetchPoolPromises = new Set<Promise<WorkerResponse>>()
for (let i = 0; i < availableParallelism(); i++) {
fetchPoolPromises.add(
- fetchPool.execute({ url: requestUrl }, workerFunction)
+ fetchPool.execute({ input: requestUrl }, workerFunction)
)
}
try {
type RequestInfo as NodeFetchRequestInfo,
type RequestInit as NodeFetchRequestInit
} from 'node-fetch'
+import { type AxiosRequestConfig } from 'axios'
export interface WorkerData {
- url: URL | RequestInfo | NodeFetchRequestInfo
+ input: URL | RequestInfo | NodeFetchRequestInfo
init?: RequestInit | NodeFetchRequestInit
+ axiosRequestConfig?: AxiosRequestConfig
}
export interface WorkerResponse {
type RequestInfo as NodeFetchRequestInfo,
type ResponseInit as NodeFetchRequestInit
} from 'node-fetch'
+import axios from 'axios'
import { type WorkerData, type WorkerResponse } from './types.js'
class HttpClientWorker extends ThreadWorker<WorkerData, WorkerResponse> {
super({
node_fetch: async (workerData?: WorkerData) => {
const response = await nodeFetch(
- (workerData as WorkerData).url as URL | NodeFetchRequestInfo,
+ (workerData as WorkerData).input as URL | NodeFetchRequestInfo,
workerData?.init as NodeFetchRequestInit
)
// The response is not structured-cloneable, so we return the response text body instead.
},
fetch: async (workerData?: WorkerData) => {
const response = await fetch(
- (workerData as WorkerData).url as URL | RequestInfo,
+ (workerData as WorkerData).input as URL | RequestInfo,
workerData?.init as RequestInit
)
// The response is not structured-cloneable, so we return the response text body instead.
return {
text: await response.text()
}
+ },
+ axios: async (workerData?: WorkerData) => {
+ const response = await axios({
+ method: 'get',
+ url: (workerData as WorkerData).input as string,
+ ...workerData?.axiosRequestConfig
+ })
+ return {
+ text: response.data
+ }
}
})
}