diff --git a/lib/transport.js b/lib/transport.js index 8b5b48aba..55d83b500 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -71,6 +71,8 @@ function flush (stream) { stream.flushSync() } +const bundlerOverrides = structuredClone(Object.prototype.hasOwnProperty.call(globalThis, '__bundlerPathsOverrides') ? globalThis.__bundlerPathsOverrides : {}) + function transport (fullOptions) { const { pipeline, targets, levels, dedupe, worker = {}, caller = getCallers(), sync = false } = fullOptions @@ -81,9 +83,6 @@ function transport (fullOptions) { // Backwards compatibility const callers = typeof caller === 'string' ? [caller] : caller - // This will be eventually modified by bundlers - const bundlerOverrides = '__bundlerPathsOverrides' in globalThis ? globalThis.__bundlerPathsOverrides : {} - let target = fullOptions.target if (target && targets) { diff --git a/test/transport/bundlers-override-target.test.js b/test/transport/bundlers-override-target.test.js new file mode 100644 index 000000000..ab634ddc5 --- /dev/null +++ b/test/transport/bundlers-override-target.test.js @@ -0,0 +1,38 @@ +'use strict' + +const test = require('node:test') +const assert = require('node:assert') +const os = require('node:os') +const { join } = require('node:path') +const { readFile } = require('node:fs').promises + +// Set bundler overrides BEFORE loading pino +globalThis.__bundlerPathsOverrides = { + foobar: join(__dirname, '..', 'fixtures', 'to-file-transport.js') +} + +const { watchFileCreated, file } = require('../helper') +const pino = require('../../pino') + +const { pid } = process +const hostname = os.hostname() + +test('pino.transport with destination overridden by bundler', async (t) => { + const destination = file() + const transport = pino.transport({ + target: 'foobar', + options: { destination } + }) + t.after(transport.end.bind(transport)) + const instance = pino(transport) + instance.info('hello') + await watchFileCreated(destination) + const result = JSON.parse(await readFile(destination)) + delete result.time + assert.deepEqual(result, { + pid, + hostname, + level: 30, + msg: 'hello' + }) +}) diff --git a/test/transport/bundlers-override-worker-mjs.test.js b/test/transport/bundlers-override-worker-mjs.test.js new file mode 100644 index 000000000..ec156f666 --- /dev/null +++ b/test/transport/bundlers-override-worker-mjs.test.js @@ -0,0 +1,42 @@ +'use strict' + +const test = require('node:test') +const assert = require('node:assert') +const os = require('node:os') +const { join } = require('node:path') +const { readFile } = require('node:fs').promises + +// Set bundler overrides BEFORE loading pino +globalThis.__bundlerPathsOverrides = { + 'pino-worker': join(__dirname, '..', '..', 'lib/worker.js') +} + +const { watchFileCreated, file } = require('../helper') +const pino = require('../../pino') + +const { pid } = process +const hostname = os.hostname() + +test('pino.transport with worker destination overridden by bundler and mjs transport', async (t) => { + const destination = file() + const transport = pino.transport({ + targets: [ + { + target: join(__dirname, '..', 'fixtures', 'ts', 'to-file-transport.es2017.cjs'), + options: { destination } + } + ] + }) + t.after(transport.end.bind(transport)) + const instance = pino(transport) + instance.info('hello') + await watchFileCreated(destination) + const result = JSON.parse(await readFile(destination)) + delete result.time + assert.deepEqual(result, { + pid, + hostname, + level: 30, + msg: 'hello' + }) +}) diff --git a/test/transport/bundlers-override-worker.test.js b/test/transport/bundlers-override-worker.test.js new file mode 100644 index 000000000..50b6cf4c1 --- /dev/null +++ b/test/transport/bundlers-override-worker.test.js @@ -0,0 +1,42 @@ +'use strict' + +const test = require('node:test') +const assert = require('node:assert') +const os = require('node:os') +const { join } = require('node:path') +const { readFile } = require('node:fs').promises + +// Set bundler overrides BEFORE loading pino +globalThis.__bundlerPathsOverrides = { + 'pino-worker': join(__dirname, '..', '..', 'lib/worker.js') +} + +const { watchFileCreated, file } = require('../helper') +const pino = require('../../pino') + +const { pid } = process +const hostname = os.hostname() + +test('pino.transport with worker destination overridden by bundler', async (t) => { + const destination = file() + const transport = pino.transport({ + targets: [ + { + target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'), + options: { destination } + } + ] + }) + t.after(transport.end.bind(transport)) + const instance = pino(transport) + instance.info('hello') + await watchFileCreated(destination) + const result = JSON.parse(await readFile(destination)) + delete result.time + assert.deepEqual(result, { + pid, + hostname, + level: 30, + msg: 'hello' + }) +}) diff --git a/test/transport/bundlers-support.test.js b/test/transport/bundlers-support.test.js deleted file mode 100644 index 45ddfd531..000000000 --- a/test/transport/bundlers-support.test.js +++ /dev/null @@ -1,99 +0,0 @@ -'use strict' - -const test = require('node:test') -const assert = require('node:assert') -const os = require('node:os') -const { join } = require('node:path') -const { readFile } = require('node:fs').promises - -const { watchFileCreated, file } = require('../helper') -const pino = require('../../pino') - -const { pid } = process -const hostname = os.hostname() - -test('pino.transport with destination overridden by bundler', async (t) => { - globalThis.__bundlerPathsOverrides = { - foobar: join(__dirname, '..', 'fixtures', 'to-file-transport.js') - } - - const destination = file() - const transport = pino.transport({ - target: 'foobar', - options: { destination } - }) - t.after(transport.end.bind(transport)) - const instance = pino(transport) - instance.info('hello') - await watchFileCreated(destination) - const result = JSON.parse(await readFile(destination)) - delete result.time - assert.deepEqual(result, { - pid, - hostname, - level: 30, - msg: 'hello' - }) - - globalThis.__bundlerPathsOverrides = undefined -}) - -test('pino.transport with worker destination overridden by bundler', async (t) => { - globalThis.__bundlerPathsOverrides = { - 'pino-worker': join(__dirname, '..', '..', 'lib/worker.js') - } - - const destination = file() - const transport = pino.transport({ - targets: [ - { - target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'), - options: { destination } - } - ] - }) - t.after(transport.end.bind(transport)) - const instance = pino(transport) - instance.info('hello') - await watchFileCreated(destination) - const result = JSON.parse(await readFile(destination)) - delete result.time - assert.deepEqual(result, { - pid, - hostname, - level: 30, - msg: 'hello' - }) - - globalThis.__bundlerPathsOverrides = undefined -}) - -test('pino.transport with worker destination overridden by bundler and mjs transport', async (t) => { - globalThis.__bundlerPathsOverrides = { - 'pino-worker': join(__dirname, '..', '..', 'lib/worker.js') - } - - const destination = file() - const transport = pino.transport({ - targets: [ - { - target: join(__dirname, '..', 'fixtures', 'ts', 'to-file-transport.es2017.cjs'), - options: { destination } - } - ] - }) - t.after(transport.end.bind(transport)) - const instance = pino(transport) - instance.info('hello') - await watchFileCreated(destination) - const result = JSON.parse(await readFile(destination)) - delete result.time - assert.deepEqual(result, { - pid, - hostname, - level: 30, - msg: 'hello' - }) - - globalThis.__bundlerPathsOverrides = undefined -})