Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: std writable streams #7

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import "@denosaurs/log/transforms/text_encoder_stream";

import { ConsoleReadableStream } from "@denosaurs/log";

import { getStdoutWritableStream } from "@denosaurs/log/writables/stdout";
import { StdoutWritableStream } from "@denosaurs/log/writables/std";

import { JsonStringifyStream } from "@std/json";

Expand All @@ -118,7 +118,7 @@ stream
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stdout
.pipeTo(getStdoutWritableStream());
.pipeTo(new StdoutWritableStream());

// Log some messages
console.log("Hello, world!");
Expand All @@ -143,8 +143,10 @@ import "@denosaurs/log/transforms/text_encoder_stream";

import { ConsoleReadableStream } from "@denosaurs/log";

import { getStderrWritableStream } from "@denosaurs/log/writables/stderr";
import { getStdoutWritableStream } from "@denosaurs/log/writables/stdout";
import {
StderrWritableStream,
StdoutWritableStream,
} from "@denosaurs/log/writables/std";

import { OmitLogLevelStream } from "@denosaurs/log/transforms/omit";
import { PickLogLevelStream } from "@denosaurs/log/transforms/pick";
Expand All @@ -164,7 +166,7 @@ a
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stdout
.pipeTo(getStdoutWritableStream());
.pipeTo(new StdoutWritableStream());

b
// Pick only the error logs
Expand All @@ -174,7 +176,7 @@ b
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stderr
.pipeTo(getStderrWritableStream());
.pipeTo(new StderrWritableStream());

// Log some messages
console.error("This is going to stderr");
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"@denosaurs/log/transforms/pick": "../transforms/pick.ts",
"@denosaurs/log/transforms/text_encoder_stream": "../transforms/text_encoder_stream.ts",
"@denosaurs/log/writables/console": "../writables/console.ts",
"@denosaurs/log/writables/stderr": "../writables/stderr.ts",
"@denosaurs/log/writables/stdout": "../writables/stdout.ts",
"@denosaurs/log/writables/std": "../writables/std.ts",
"@std/json": "jsr:@std/json",
"bole": "npm:bole",
"bunyan": "npm:bunyan",
Expand Down
5 changes: 2 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@denosaurs/log",
"version": "0.0.11",
"version": "0.0.12",
"exports": {
".": "./mod.ts",
"./transforms/filter": "./transforms/filter.ts",
Expand All @@ -10,8 +10,7 @@
"./transforms/redact": "./transforms/redact.ts",
"./transforms/text_encoder_stream": "./transforms/text_encoder_stream.ts",
"./writables/console": "./writables/console.ts",
"./writables/stderr": "./writables/stderr.ts",
"./writables/stdout": "./writables/stdout.ts"
"./writables/std": "./writables/std.ts"
},
"lock": false,
"tasks": {
Expand Down
3 changes: 1 addition & 2 deletions examples/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"@denosaurs/log/transforms/redact": "../transforms/redact.ts",
"@denosaurs/log/transforms/text_encoder_stream": "../transforms/text_encoder_stream.ts",
"@denosaurs/log/writables/console": "../writables/console.ts",
"@denosaurs/log/writables/stderr": "../writables/stderr.ts",
"@denosaurs/log/writables/stdout": "../writables/stdout.ts",
"@denosaurs/log/writables/std": "../writables/std.ts",
"@std/json": "jsr:@std/json"
},
"tasks": {
Expand Down
4 changes: 2 additions & 2 deletions examples/json/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "@denosaurs/log/transforms/text_encoder_stream";

import { ConsoleReadableStream } from "@denosaurs/log";

import { getStdoutWritableStream } from "@denosaurs/log/writables/stdout";
import { StdoutWritableStream } from "@denosaurs/log/writables/std";

import { JsonStringifyStream } from "@std/json";

Expand All @@ -15,7 +15,7 @@ stream
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stdout
.pipeTo(getStdoutWritableStream());
.pipeTo(new StdoutWritableStream());

// Log some messages
console.log("Hello, world!");
Expand Down
10 changes: 6 additions & 4 deletions examples/tee/tee.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import "@denosaurs/log/transforms/text_encoder_stream";

import { ConsoleReadableStream } from "@denosaurs/log";

import { getStdoutWritableStream } from "@denosaurs/log/writables/stdout";
import { getStderrWritableStream } from "@denosaurs/log/writables/stderr";
import {
StderrWritableStream,
StdoutWritableStream,
} from "@denosaurs/log/writables/std";

import { OmitLogLevelStream } from "@denosaurs/log/transforms/omit";
import { PickLogLevelStream } from "@denosaurs/log/transforms/pick";
Expand All @@ -24,7 +26,7 @@ a
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stdout
.pipeTo(getStdoutWritableStream());
.pipeTo(new StdoutWritableStream());

b
// Pick only the error logs
Expand All @@ -34,7 +36,7 @@ b
// Encode the output to an UTF-8 byte stream
.pipeThrough(new TextEncoderStream())
// Pipe the output to stderr
.pipeTo(getStderrWritableStream());
.pipeTo(new StderrWritableStream());

// Log some messages
console.error("This is going to stderr");
Expand Down
58 changes: 0 additions & 58 deletions utils/std.ts

This file was deleted.

115 changes: 115 additions & 0 deletions writables/std.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* # StdoutWritableStream and StderrWritableStream
*
* Cross-runtime writable streams for the standard output and standard error.
*
* @example
* ```ts
* import { ConsoleReadableStream } from "@denosaurs/log";
* import { StdoutWritableStream } from "@denosaurs/log/writables/std";
* import { JsonStringifyStream } from "@std/json";
*
* // Capture logs from the console
* const stream = new ConsoleReadableStream();
* stream
* // Stringify the logs to JSON
* .pipeThrough(new JsonStringifyStream())
* // Encode the output to an UTF-8 byte stream
* .pipeThrough(new TextEncoderStream())
* // Pipe the output to stdout
* .pipeTo(new StdoutWritableStream());
* ```
*
* @module
*/

import { originalConsole } from "../utils/original_console.ts";
import { environment, isNode } from "../utils/runtime.ts";

let nodeWritableToWeb: (
stream: unknown,
) => WritableStream<Uint8Array> | undefined;
if (isNode) {
nodeWritableToWeb = (await import("node:stream")).Writable.toWeb as (
stream: unknown,
) => WritableStream<Uint8Array>;
}

/**
* A writable stream for standard output or error.
*/
export class StdWritableStream extends WritableStream<Uint8Array> {
constructor(stream: "stdout" | "stderr") {
let sink: UnderlyingSink<Uint8Array>;
switch (environment) {
case "deno":
sink = {
write: async (chunk) => {
await globalThis.Deno[stream].write(chunk);
},
}
break;
case "bun":
// Once https://github.com/oven-sh/bun/issues/3927 is completed we can use the node code for bun.
sink = {
write: async (chunk) => {
// @ts-expect-error: The type checking environment is deno, the bun types are not available
await globalThis.Bun.write(globalThis.Bun[stream], chunk);
},
};
break;
case "node":
// @ts-expect-error: The type checking environment is deno, the node types are not available
sink = nodeWritableToWeb!(globalThis.process[stream]);
break;
case "browser":
case "unknown": {
const decoder = new TextDecoder();
let buffer = "";
const write = stream === "stdout" ? originalConsole.log : originalConsole.error;

sink = {
write: (chunk) => {
buffer += decoder.decode(chunk);
const lines = buffer.split("\n");
if (lines.length > 1) {
buffer = lines.pop() ?? "";
for (const line of lines) {
write(line);
}
}
},
close: () => {
if (buffer.length > 0) {
const lines = buffer.split("\n");
for (const line of lines) {
write(line);
}
}
},
};
break;
}
}

super(sink);
}
}

/**
* A writable stream for standard output.
*/
export class StdoutWritableStream extends StdWritableStream {
constructor() {
super("stdout");
}
}

/**
* A writable stream for standard error.
*/
export class StderrWritableStream extends StdWritableStream {
constructor() {
super("stderr");
}
}
8 changes: 0 additions & 8 deletions writables/stderr.ts

This file was deleted.

8 changes: 0 additions & 8 deletions writables/stdout.ts

This file was deleted.

Loading