Skip to content

Commit

Permalink
Chunks handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vbabak committed Feb 2, 2024
1 parent d7e1b3c commit 19006dd
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 431 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ There are 2 possible options of configuration:
Piped output can be useful when formatter is installed as a dev dependency.

```bash
npx ts-node service.ts | npx @tsxper/log-stream-formatter-visual
npx ts-node service.ts | npx @tsxper/log-stream-formatter-visual [logDepth]
```

### Replace Default Formatter
Expand Down
4 changes: 1 addition & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
### Modify stdout
```bash
bun std.ts | bun ../src/std.ts
# or "npx ts-node"
# npx ts-node std.ts | npx ts-node ../src/std.ts
npx ts-node std.ts | npx ts-node ../src/std.ts 3
```

### Modify stream output
Expand Down
5 changes: 1 addition & 4 deletions examples/std.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ class A {
constructor(public a: A[]) { }
}

Logger.replaceLogStreams(process.stdout, process.stderr);
Logger.replaceLogStreams(process.stdout, process.stdout);
const logger = new Logger(4, 'std');

const log = async () => {
const pause = 500;
await new Promise(r => setTimeout(r, pause));
logger.debug('debug');
await new Promise(r => setTimeout(r, pause));
logger.log('info', [1, 2]);
await new Promise(r => setTimeout(r, pause));
logger.error('error', new Error('test'));
await new Promise(r => setTimeout(r, pause));
logger.warn('info', { a: { b: { c: { d: 'f' } } } });
await new Promise(r => setTimeout(r, pause));

const arr: A[] = [];
const obj = new A(arr);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tsxper/log-stream-formatter-visual",
"version": "1.0.2",
"version": "1.0.3",
"description": "Visual formatter for @tsxper/log-stream",
"scripts": {
"test": "jest",
Expand Down
35 changes: 26 additions & 9 deletions src/std.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/env node
import { stdin, stdout } from 'node:process';
import { argv, stdin, stdout } from 'node:process';
import { pipeline, Transform, TransformCallback } from 'node:stream';
import { LOG_LEVEL, FormatterVisual } from './FormatterVisual';
import { EOL } from 'os';

const args = argv.slice(2);
const logDepth = parseInt(args[0] || '');

type DEFAULT_FORMAT = {
t: number;
l: LOG_LEVEL;
Expand All @@ -14,20 +17,34 @@ type DEFAULT_FORMAT = {
};

const formatter = new FormatterVisual();
if (!isNaN(logDepth)) {
formatter.setDepth(logDepth);
}
pipeline(
stdin,
new Transform({
transform(chunk: Buffer, encoding: BufferEncoding, callback: TransformCallback) {
let line = chunk.toString().trim();
try {
const decoded = JSON.parse(line) as DEFAULT_FORMAT;
if (decoded.t) {
line = formatter.formatter(decoded.t, decoded.n, decoded.s, decoded.l, decoded.e || decoded.d);
const lines = chunk.toString().trim().split(`}${EOL}{`).map((l) => l.trim());
if (!lines.length) {
return callback(null, `${EOL}`);
}
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (!line) continue;
if (!line.startsWith('{')) line = `{${line}`;
if (!line.endsWith('}')) line = `${line}}`;
try {
const decoded = JSON.parse(line) as DEFAULT_FORMAT;
if (decoded.t) {
line = formatter.formatter(decoded.t, decoded.n, decoded.s, decoded.l, decoded.e || decoded.d);
}
} catch (e) {
//
}
} catch (e) {
//
lines[i] = line;
}
callback(null, `${line}${EOL}`);

callback(null, `${lines.join(EOL)}${EOL}`);
},
}),
stdout,
Expand Down
Loading

0 comments on commit 19006dd

Please sign in to comment.