-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure first-class support for transpiled & ts code in transports (#1381
- Loading branch information
Showing
22 changed files
with
611 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
const { realImport, realRequire } = require('real-require') | ||
|
||
module.exports = loadTransportStreamBuilder | ||
|
||
/** | ||
* Loads & returns a function to build transport streams | ||
* @param {string} target | ||
* @returns {function(object): Promise<import('stream').Writable>} | ||
* @throws {Error} In case the target module does not export a function | ||
*/ | ||
async function loadTransportStreamBuilder (target) { | ||
let fn | ||
try { | ||
const toLoad = 'file://' + target | ||
|
||
if (toLoad.endsWith('.ts') || toLoad.endsWith('.cts')) { | ||
// TODO: add support for the TSM modules loader ( https://github.com/lukeed/tsm ). | ||
if (process[Symbol.for('ts-node.register.instance')]) { | ||
realRequire('ts-node/register') | ||
} else if (process.env && process.env.TS_NODE_DEV) { | ||
realRequire('ts-node-dev') | ||
} | ||
// TODO: Support ES imports once tsc, tap & ts-node provide better compatibility guarantees. | ||
fn = realRequire(decodeURIComponent(target)) | ||
} else { | ||
fn = (await realImport(toLoad)) | ||
} | ||
} catch (error) { | ||
// 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) | ||
} else { | ||
throw error | ||
} | ||
} | ||
|
||
// Depending on how the default export is performed, and on how the code is | ||
// transpiled, we may find cases of two nested "default" objects. | ||
// See https://github.com/pinojs/pino/issues/1243#issuecomment-982774762 | ||
if (typeof fn === 'object') fn = fn.default | ||
if (typeof fn === 'object') fn = fn.default | ||
if (typeof fn !== 'function') throw Error('exported worker is not a function') | ||
|
||
return fn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
completelyUnrelatedProperty: 'Just a very incorrect transport worker implementation' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as fs from 'fs' | ||
import { once } from 'events' | ||
import { Transform } from 'stream' | ||
|
||
async function run (opts: { destination?: fs.PathLike }): Promise<Transform> { | ||
if (!opts.destination) throw new Error('kaboom') | ||
const stream = fs.createWriteStream(opts.destination) | ||
await once(stream, 'open') | ||
const t = new Transform({ | ||
transform (chunk, enc, cb) { | ||
setImmediate(cb, null, chunk.toString().toUpperCase()) | ||
} | ||
}) | ||
t.pipe(stream) | ||
return t | ||
} | ||
|
||
export default run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as fs from 'fs' | ||
import { once } from 'events' | ||
|
||
async function run (opts: { destination?: fs.PathLike }): Promise<fs.WriteStream> { | ||
if (!opts.destination) throw new Error('kaboom') | ||
const stream = fs.createWriteStream(opts.destination, { encoding: 'utf8' }) | ||
await once(stream, 'open') | ||
return stream | ||
} | ||
|
||
export default run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env node | ||
|
||
const execa = require('execa') | ||
const fs = require('fs') | ||
|
||
const existsSync = fs.existsSync | ||
const stat = fs.promises.stat | ||
|
||
// Hardcoded parameters | ||
const esVersions = ['es5', 'es6', 'es2017', 'esnext'] | ||
const filesToTranspile = ['to-file-transport.ts'] | ||
|
||
async function transpile () { | ||
process.chdir(__dirname) | ||
|
||
const runner = (process.env.npm_config_user_agent || '').match(/yarn/) | ||
? 'yarn' | ||
: 'npx' | ||
|
||
for (const sourceFileName of filesToTranspile) { | ||
const sourceStat = await stat(sourceFileName) | ||
|
||
for (const esVersion of esVersions) { | ||
const intermediateFileName = sourceFileName.replace(/\.ts$/, '.js') | ||
const targetFileName = sourceFileName.replace(/\.ts$/, `.${esVersion}.cjs`) | ||
|
||
const shouldTranspile = !existsSync(targetFileName) || (await stat(targetFileName)).mtimeMs < sourceStat.mtimeMs | ||
|
||
if (shouldTranspile) { | ||
await execa(runner, ['tsc', '--target', esVersion, '--module', 'commonjs', sourceFileName]) | ||
await execa('mv', [intermediateFileName, targetFileName]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
transpile().catch(err => { | ||
process.exitCode = 1 | ||
throw err | ||
}) |
15 changes: 15 additions & 0 deletions
15
test/fixtures/ts/transport-exit-immediately-with-async-dest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pino from '../../..' | ||
import { join } from 'path' | ||
|
||
const transport = pino.transport({ | ||
target: join(__dirname, 'to-file-transport-with-transform.ts'), | ||
options: { | ||
destination: process.argv[2] | ||
} | ||
}) | ||
const logger = pino(transport) | ||
|
||
logger.info('Hello') | ||
logger.info('World') | ||
|
||
process.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pino from '../../..' | ||
|
||
const transport = pino.transport({ | ||
target: 'pino/file' | ||
}) | ||
const logger = pino(transport) | ||
|
||
logger.info('Hello') | ||
|
||
process.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pino from '../../..' | ||
|
||
const transport = pino.transport({ | ||
target: 'pino/file' | ||
}) | ||
const logger = pino(transport) | ||
|
||
transport.on('ready', function () { | ||
logger.info('Hello') | ||
process.exit(0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { join } from 'path' | ||
import pino from '../../..' | ||
|
||
const transport = pino.transport({ | ||
target: join(__dirname, 'transport-worker.ts') | ||
}) | ||
const logger = pino(transport) | ||
logger.info('Hello') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pino from '../../..' | ||
|
||
const transport = pino.transport({ | ||
target: 'pino/file', | ||
options: { destination: '1' } | ||
}) | ||
const logger = pino(transport) | ||
logger.info('Hello') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Writable } from 'stream' | ||
|
||
export default (): Writable => { | ||
const myTransportStream = new Writable({ | ||
autoDestroy: true, | ||
write (chunk, _enc, cb) { | ||
console.log(chunk.toString()) | ||
cb() | ||
}, | ||
defaultEncoding: 'utf8' | ||
}) | ||
|
||
return myTransportStream | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PathLike } from 'fs' | ||
|
||
export declare function watchFileCreated(filename: PathLike): Promise<void> | ||
export declare function watchForWrite(filename: PathLike, testString: string): Promise<void> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.