post-stream
Efficiently and conveniently send number, string, boolean, null, undefined, object, buffer or stream to a stream.
npm install --save post-stream
API
Constructor
//Create a stream controlerconfig;
config
:
readable
readable streamwritable
writable streamduplex
duplex stream (such as tcp socket)maxSize
data fragment max byte size, default 16MB. If when sending data greater thanmaxSize
will throw a error. If when receiving data greater thanmaxSize
,this data will be discard.
StaticClassProperty
serialize(data: Array): Buffer
Serilize data. Pass a array, item type can be number, string, boolean, null, undefined, object or buffer.
parse(data: Buffer): Array
Parse data. Pass a has been serialized buffer.
ClassProperty
data: EventEmiter
This is a EventEmiter
. Received data`s title is event name.
const ps = readable writable;psdata;await ps;
parseData: boolean
Parse received data. Default is true. If you set false,
then you need use PostStream.parse
to parse every received data.
const ps = readable writable;psparseData = false;psdata;await ps;
ClassMethod
send(title: string, ...data: any[]): Promise<void>
Send number, string, boolean, null, undefined, object or buffer to stream. The first argument must be a string, it is used for description the data. After that you can pass zero or more data as arguments. If a argument is object or array,it will be serialized( use JSON.stringify ). If you want send a buffer, don`t put it in object or array. This function will return a promise object.
const ps = readable writable;await ps;await ps;await ps;
send(title: string, data: stream.Readable | stream.Duplex): Promise<void>;
Send a stream`s data to stream. You can only pass one stream as second argument. Other arguments will be ignore. Receiver side will receive a buffer, data comes from the send stream. This function will return a promise object.
const ps = readable writable;await ps;await ps;
sendSerializedData(title: string, data: Buffer): Promise<void>;
Directly send using PostStream.serialize
serialized data.
const ps = readable writable;await ps;
close
close(): Promise<void>
Close readable stream and writable stream.
const ps = readable writable;await ps;
Event
'data'
Receive all data. This will be triggered when PostStream has received data. The first parameter is title, after that will be zero or more data, determined by when sending.
const ps = readableStream writableStream;ps;
'end'
This will be triggered when the be controled stream trigger end event.
'close'
This will be triggered when the be controled stream trigger close event.
'error'
This will be triggered when the be controled stream trigger error event or when parsing data error.
Attention
If you want to use pipe
to communicate with child process, you would better use two pipe
instead of one.
Because of don`t know why child send data to parent will lost one data fragment.(this is not this package`s problem)
//parent const child_process = ;const path = ;const PostStream = ; const child = child_process;const ps = readable:childstdio3 writable:childstdio4;psdata;ps;
//childconst fs = ;const PostStream = ; let readable = fs;let writable = fs;const ps = readable writable;psdata;