Repository: pinojs/pino (js)

Issue:
Error: "fn is not a function" while setting up a pino transports since v9.2.1
Pino `v9.3.0` throws errors

```
#10 23.21 5e4f8d756c2d9898b115a52167c82923990c1c73 (Node v20.11.1)
#10 23.22 node:events:496
#10 23.22       throw er; // Unhandled 'error' event
#10 23.22       ^
#10 23.22 
#10 23.22 TypeError: fn is not a function
#10 23.22     at /snapshot/dist/pino-worker.js:10
#10 23.22     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
#10 23.22     at async Promise.all (index 0)
#10 23.22     at async module.exports (/snapshot/dist/pino-worker.js:10713:15)
#10 23.22     at async start (/snapshot/dist/thread-stream-worker.js:134:17)
#10 23.22 Emitted 'error' event on ThreadStream instance at:
#10 23.22     at Immediate.<anonymous> (/snapshot/dist/containerbase-cli.js:6481:17)
#10 23.22     at process.processImmediate (node:internal/timers:4
```

Update PR
- https://github.com/containerbase/base/pull/2942

Logger init code
- https://github.com/containerbase/base/blob/main/src/cli/utils/logger.ts

The code will be transpiled with `esbuild` and then packaged with `@yao-pkg/pkg`
- https://github.com/containerbase/base/blob/main/tools/esbuild.js

Seems to be related to #1383

Pull request description:
fix: no early return in `loadTransportStreamBuilder`
Don't return early in `loadTransportStreamBuilder`, it'll return undefined instead if the expected function.
I'll also fixed the type annotation to be a promise. 😉 

- closes #2012
- regression of #1990

Patch:
diff --git a/lib/transport-stream.js b/lib/transport-stream.js
index d1f1fd1..22cb37e 100644
--- a/lib/transport-stream.js
+++ b/lib/transport-stream.js
@@ -7,7 +7,7 @@ module.exports = loadTransportStreamBuilder
 /**
  * Loads & returns a function to build transport streams
  * @param {string} target
- * @returns {function(object): Promise<import('node:stream').Writable>}
+ * @returns {Promise<function(object): Promise<import('node:stream').Writable>>}
  * @throws {Error} In case the target module does not export a function
  */
 async function loadTransportStreamBuilder (target) {
@@ -31,7 +31,6 @@ async function loadTransportStreamBuilder (target) {
     // See this PR for details: https://github.com/pinojs/thread-stream/pull/34
     if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) {
       fn = realRequire(target)
-      return
     } else if (error.code === undefined || error.code === 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING') {
       // When bundled with pkg, an undefined error is thrown when called with realImport
       // When bundled with pkg and using node v20, an ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING error is thrown when called with realImport


Test patch:
diff --git a/test/transport-stream.test.js b/test/transport-stream.test.js
new file mode 100644
index 0000000..488aceb
--- /dev/null
+++ b/test/transport-stream.test.js
@@ -0,0 +1,26 @@
+'use strict'
+
+const { test } = require('tap')
+
+test('should import', async (t) => {
+  t.plan(2)
+  const mockRealRequire = (target) => {
+    return {
+      default: {
+        default: () => {
+          t.equal(target, 'pino-pretty')
+          return Promise.resolve()
+        }
+      }
+    }
+  }
+  const mockRealImport = async () => { await Promise.resolve(); throw Object.assign(new Error(), { code: 'ERR_MODULE_NOT_FOUND' }) }
+
+  /** @type {typeof import('../lib/transport-stream.js')} */
+  const loadTransportStreamBuilder = t.mock('../lib/transport-stream.js', { 'real-require': { realRequire: mockRealRequire, realImport: mockRealImport } })
+
+  const fn = await loadTransportStreamBuilder('pino-pretty')
+
+  t.resolves(fn())
+  t.end()
+})