From: Jérôme Benoit Date: Thu, 10 Aug 2023 17:26:41 +0000 (+0200) Subject: feat: add native node.js fetch implementation to http-client example X-Git-Tag: v2.6.23~21 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=49ac634fc18b93a4fcfadf425047196c20223f77;p=poolifier.git feat: add native node.js fetch implementation to http-client example Signed-off-by: Jérôme Benoit --- diff --git a/.eslintignore b/.eslintignore index f94ab6fc..f2ad06fb 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ docs/ -outputs/ +dist/ lib/ +outputs/ diff --git a/.eslintrc.js b/.eslintrc.js index b547ed19..02782ad4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -115,7 +115,7 @@ module.exports = defineConfig({ { 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' } diff --git a/examples/typescript/http-client/node-fetch/httpd-echo.js b/examples/typescript/http-client/httpd-echo.js similarity index 100% rename from examples/typescript/http-client/node-fetch/httpd-echo.js rename to examples/typescript/http-client/httpd-echo.js diff --git a/examples/typescript/http-client/node-fetch/src/main.ts b/examples/typescript/http-client/node-fetch/src/main.ts deleted file mode 100644 index 86760929..00000000 --- a/examples/typescript/http-client/node-fetch/src/main.ts +++ /dev/null @@ -1,25 +0,0 @@ -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>() -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) -} diff --git a/examples/typescript/http-client/node-fetch/src/types.ts b/examples/typescript/http-client/node-fetch/src/types.ts deleted file mode 100644 index 4ac3a8ff..00000000 --- a/examples/typescript/http-client/node-fetch/src/types.ts +++ /dev/null @@ -1,11 +0,0 @@ -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 -} diff --git a/examples/typescript/http-client/node-fetch/src/worker.ts b/examples/typescript/http-client/node-fetch/src/worker.ts deleted file mode 100644 index 6f0d3651..00000000 --- a/examples/typescript/http-client/node-fetch/src/worker.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { ThreadWorker } from 'poolifier' -import fetch from 'node-fetch' -import { type WorkerData, type WorkerResponse } from './types.js' - -class FetchWorker extends ThreadWorker { - 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 } diff --git a/examples/typescript/http-client/node-fetch/package.json b/examples/typescript/http-client/package.json similarity index 87% rename from examples/typescript/http-client/node-fetch/package.json rename to examples/typescript/http-client/package.json index 3aa26176..a1627747 100644 --- a/examples/typescript/http-client/node-fetch/package.json +++ b/examples/typescript/http-client/package.json @@ -10,8 +10,8 @@ "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" diff --git a/examples/typescript/http-client/node-fetch/pnpm-lock.yaml b/examples/typescript/http-client/pnpm-lock.yaml similarity index 100% rename from examples/typescript/http-client/node-fetch/pnpm-lock.yaml rename to examples/typescript/http-client/pnpm-lock.yaml diff --git a/examples/typescript/http-client/src/main.ts b/examples/typescript/http-client/src/main.ts new file mode 100644 index 00000000..a19ed280 --- /dev/null +++ b/examples/typescript/http-client/src/main.ts @@ -0,0 +1,28 @@ +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>() + 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) + } +} diff --git a/examples/typescript/http-client/node-fetch/src/pool.ts b/examples/typescript/http-client/src/pool.ts similarity index 100% rename from examples/typescript/http-client/node-fetch/src/pool.ts rename to examples/typescript/http-client/src/pool.ts diff --git a/examples/typescript/http-client/src/types.ts b/examples/typescript/http-client/src/types.ts new file mode 100644 index 00000000..e68e7260 --- /dev/null +++ b/examples/typescript/http-client/src/types.ts @@ -0,0 +1,14 @@ +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 +} diff --git a/examples/typescript/http-client/src/worker.ts b/examples/typescript/http-client/src/worker.ts new file mode 100644 index 00000000..f05c8d1e --- /dev/null +++ b/examples/typescript/http-client/src/worker.ts @@ -0,0 +1,38 @@ +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 { + 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 } diff --git a/examples/typescript/http-client/node-fetch/tsconfig.json b/examples/typescript/http-client/tsconfig.json similarity index 100% rename from examples/typescript/http-client/node-fetch/tsconfig.json rename to examples/typescript/http-client/tsconfig.json