Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
@farar/nodes
Advanced tools
Nodes provides a framework for building type-safe data transformation graphs using Node.js streams.
Nodes provides a framework for building type-safe data transformation graphs using Node.js streams.
Nodes provides an intuitive framework for constructing data transformation graphs using native Node.js streams. You can use the built-in library of commonly used data transformation Node
classes or implement your own.
npm install @farar/nodes
A Node
is a component of a graph-like data transformation pipeline. Each Node
is responsible for transforming its input into an output that can be consumed by its connected Node
instances. By connecting Node
instances into a network, sophisticated graph-like data transformation pipelines can be constructed.
Please see the Streams Logger implementation.
new Nodes.Node<InT, OutT>(stream, options)
stream
<stream.Writable | stream.Readable>
An instance of a Writable
, Readable
, Duplex
, or Transform
Node.js stream.options
<NodeOptions>
errorHandler
<(err: Error, ...params: Array<unknown>) => void>
An optional error handler that will be used in the event of an internal Error.public node.connect(...nodes)
<Array<T>>
An array of Node<OutT, unknown>
to be connected to this Node
.Returns: <Node<InT, OutT>>
public node.disconnect(...nodes)
<Array<T>>
An array of Node<OutT, unknown>
to be disconnected from this Node
.Returns: <Node<InT, OutT>>
protected node[$write](data)
<InT>
Data to write to the writable side of the stream.Returns: <Promise<void>>
Config.errorHandler <ErrorHandler>
An optional error handler. Default: console.error
Config.verbose <boolean>
Optional announcement of activities to stdout. Default: false
Config.getConfig()
Returns: <ConfigOptions>
An object that contains the current configuration settings.
In order to implement a data transformation Node
, extend the Node
class and pass a Node.js stream.Writable
implementation to the super's constructor.
For example, the following StringToNumber
implementation will convert a numeric string to a number.
NB:
writableObjectMode
andreadableObjectMode
are both set to true in this example; hence, the Node.js stream implementation will handle the input and output as objects. It's important thatwritableObjectMode
andreadableObjectMode
accurately reflect the input and output types of yourNode
.
import * as stream from "node:stream";
import { Node } from "@farar/nodes";
export class StringToNumber extends Node<string, number> {
constructor(options: stream.TransformOptions) {
super(
new stream.Transform({
...options,
...{
writableObjectMode: true,
readableObjectMode: true,
transform: (
chunk: string,
encoding: BufferEncoding,
callback: stream.TransformCallback
) => {
try {
const result = parseFloat(chunk.toString());
callback(null, result);
} catch (err) {
if (err instanceof Error) {
callback(err);
}
}
},
},
})
);
}
}
In this hypothetical example a type-safe Node
is constructed from a net.Socket
. The resulting Node
instance can be used in a data transformation graph.
import * as net from "node:net";
import { once } from "node:events";
net.createServer((socket: net.Socket) => socket.pipe(socket)).listen(3000);
const socket = net.createConnection({ port: 3000 });
await once(socket, "connect");
const socketHandler = new Node<Buffer, Buffer>(socket);
The Node
class has a $write
method that respects backpressue; when a stream is draining it will queue messages until a drain
event is emitted by the Node's stream. Your application can optionally monitor the size of the queue and respond appropriately.
If you have a stream that is backpressuring, you can increase the high water mark on the stream in order to mitigate drain events.
Node
instances (unless you know what you are doing!).Reusing the same Node
instance can result in unexpected phenomena. If the same Node
instance is used in different locations in your graph, you need to think carefully about the resulting edges that are connected to both the input and the output of the Node
instance. Most of the time if you need to use the same class of Node
more than once, it's advisable to create a new instance for each use.
Nodes may be used in diverse contexts, each with unique requirements. Nodes should never throw if the API is used in accordance with the documentation. However, "phenomena happens" sometimes, hence you may choose to handle errors accordingly.
Nodes defaults to logging its errors to process.stderr
. If your application requires that errors throw you may set an errorHandler
on the Config
object that does that.
Node
errors to be thrown.import { Config } from "@farar/nodes";
Config.errorHandler = (err: Error) => {
throw err;
};
npm install && npm update
npm test
FAQs
Nodes provides a framework for building type-safe data transformation graphs using Node.js streams.
The npm package @farar/nodes receives a total of 17 weekly downloads. As such, @farar/nodes popularity was classified as not popular.
We found that @farar/nodes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.