feat: add native node.js fetch implementation to http-client example
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 10 Aug 2023 17:26:41 +0000 (19:26 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 10 Aug 2023 17:26:41 +0000 (19:26 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
13 files changed:
.eslintignore
.eslintrc.js
examples/typescript/http-client/httpd-echo.js [moved from examples/typescript/http-client/node-fetch/httpd-echo.js with 100% similarity]
examples/typescript/http-client/node-fetch/src/main.ts [deleted file]
examples/typescript/http-client/node-fetch/src/types.ts [deleted file]
examples/typescript/http-client/node-fetch/src/worker.ts [deleted file]
examples/typescript/http-client/package.json [moved from examples/typescript/http-client/node-fetch/package.json with 87% similarity]
examples/typescript/http-client/pnpm-lock.yaml [moved from examples/typescript/http-client/node-fetch/pnpm-lock.yaml with 100% similarity]
examples/typescript/http-client/src/main.ts [new file with mode: 0644]
examples/typescript/http-client/src/pool.ts [moved from examples/typescript/http-client/node-fetch/src/pool.ts with 100% similarity]
examples/typescript/http-client/src/types.ts [new file with mode: 0644]
examples/typescript/http-client/src/worker.ts [new file with mode: 0644]
examples/typescript/http-client/tsconfig.json [moved from examples/typescript/http-client/node-fetch/tsconfig.json with 100% similarity]

index f94ab6fc37e46c0075a5206d6cb80604cc056fb8..f2ad06fb108cfd829dab35f0233c84640c00846f 100644 (file)
@@ -1,3 +1,4 @@
 docs/
-outputs/
+dist/
 lib/
+outputs/
index b547ed1909badd738d831ccf1b70af4fe1001af0..02782ad45cfeb9766eddf2c8e50bfaabb22e6372 100644 (file)
@@ -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/src/main.ts b/examples/typescript/http-client/node-fetch/src/main.ts
deleted file mode 100644 (file)
index 8676092..0000000
+++ /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<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)
-}
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 (file)
index 4ac3a8f..0000000
+++ /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 (file)
index 6f0d365..0000000
+++ /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<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 }
similarity index 87%
rename from examples/typescript/http-client/node-fetch/package.json
rename to examples/typescript/http-client/package.json
index 3aa26176497a5ab826d27da13daec27d3c674c28..a1627747ecafbeb0ff16502e06427e2ed4f1daaf 100644 (file)
@@ -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/src/main.ts b/examples/typescript/http-client/src/main.ts
new file mode 100644 (file)
index 0000000..a19ed28
--- /dev/null
@@ -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<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)
+  }
+}
diff --git a/examples/typescript/http-client/src/types.ts b/examples/typescript/http-client/src/types.ts
new file mode 100644 (file)
index 0000000..e68e726
--- /dev/null
@@ -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 (file)
index 0000000..f05c8d1
--- /dev/null
@@ -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<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 }