docs/
-outputs/
+dist/
lib/
+outputs/
{
files: ['examples/typescript/**/*.ts'],
rules: {
- '@typescript-eslint/no-unsafe-argument': 'off',
+ // '@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off'
}
+++ /dev/null
-import { availableParallelism } from 'poolifier'
-import { fetchPool } from './pool.js'
-import { type WorkerResponse } from './types.js'
-
-const parallelism = availableParallelism()
-const requestUrl = 'http://localhost:8080/'
-
-const fetchPoolPromises = new Set<Promise<WorkerResponse>>()
-for (let i = 0; i < availableParallelism(); i++) {
- fetchPoolPromises.add(fetchPool.execute({ url: requestUrl }))
-}
-
-try {
- const now = performance.now()
- const responses = await Promise.all(fetchPoolPromises)
- const elapsedTime = performance.now() - now
- console.info(
- `Received in ${elapsedTime.toFixed(2)}ms an array with ${
- responses.length
- } responses from ${parallelism} parallel requests made with node-fetch on ${requestUrl}:\n`,
- responses
- )
-} catch (error) {
- console.error(error)
-}
+++ /dev/null
-import { type URL } from 'node:url'
-import { type RequestInfo, type RequestInit } from 'node-fetch'
-
-export interface WorkerData {
- url: URL | RequestInfo
- init?: RequestInit
-}
-
-export interface WorkerResponse {
- text: string
-}
+++ /dev/null
-import { ThreadWorker } from 'poolifier'
-import fetch from 'node-fetch'
-import { type WorkerData, type WorkerResponse } from './types.js'
-
-class FetchWorker extends ThreadWorker<WorkerData, WorkerResponse> {
- public constructor () {
- super(async (workerData?: WorkerData) => {
- const response = await fetch(
- (workerData as WorkerData).url,
- workerData?.init
- )
- // The response is not structured-cloneable, so we return the response text body instead.
- return {
- text: await response.text()
- }
- })
- }
-}
-
-const fetchWorker = new FetchWorker()
-
-export { fetchWorker }
"pnpm": "8.6.12"
},
"scripts": {
- "build": "pnpm build:clean && npx tsc",
- "build:clean": "npx tsc --build --clean",
+ "build": "pnpm build:clean && tsc",
+ "build:clean": "tsc --build --clean",
"start": "node dist/main.js",
"start:httpd-echo": "node dist/httpd-echo.js",
"test": "echo \"Error: no test specified\" && exit 1"
--- /dev/null
+import { availableParallelism } from 'poolifier'
+import { fetchPool } from './pool.js'
+import { type WorkerResponse } from './types.js'
+
+const parallelism = availableParallelism()
+const requestUrl = 'http://localhost:8080/'
+
+for (const workerFunction of ['node_fetch', 'fetch']) {
+ const fetchPoolPromises = new Set<Promise<WorkerResponse>>()
+ for (let i = 0; i < availableParallelism(); i++) {
+ fetchPoolPromises.add(
+ fetchPool.execute({ url: requestUrl }, workerFunction)
+ )
+ }
+ try {
+ const now = performance.now()
+ const responses = await Promise.all(fetchPoolPromises)
+ const elapsedTime = performance.now() - now
+ console.info(
+ `Received in ${elapsedTime.toFixed(2)}ms an array with ${
+ responses.length
+ } responses from ${parallelism} parallel requests made with ${workerFunction} on ${requestUrl}:\n`,
+ responses
+ )
+ } catch (error) {
+ console.error(error)
+ }
+}
--- /dev/null
+import { type URL } from 'node:url'
+import {
+ type RequestInfo as NodeFetchRequestInfo,
+ type RequestInit as NodeFetchRequestInit
+} from 'node-fetch'
+
+export interface WorkerData {
+ url: URL | RequestInfo | NodeFetchRequestInfo
+ init?: RequestInit | NodeFetchRequestInit
+}
+
+export interface WorkerResponse {
+ text: string
+}
--- /dev/null
+import { ThreadWorker } from 'poolifier'
+import nodeFetch from 'node-fetch'
+import {
+ type RequestInfo as NodeFetchRequestInfo,
+ type ResponseInit as NodeFetchRequestInit
+} from 'node-fetch'
+import { type WorkerData, type WorkerResponse } from './types.js'
+
+class FetchWorker extends ThreadWorker<WorkerData, WorkerResponse> {
+ public constructor () {
+ super({
+ node_fetch: async (workerData?: WorkerData) => {
+ const response = await nodeFetch(
+ (workerData as WorkerData).url as URL | NodeFetchRequestInfo,
+ workerData?.init as NodeFetchRequestInit
+ )
+ // The response is not structured-cloneable, so we return the response text body instead.
+ return {
+ text: await response.text()
+ }
+ },
+ fetch: async (workerData?: WorkerData) => {
+ const response = await fetch(
+ (workerData as WorkerData).url 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()
+ }
+ }
+ })
+ }
+}
+
+const fetchWorker = new FetchWorker()
+
+export { fetchWorker }