Gathering detailed insights and metrics for @hishprorg/assumenda-tempora-eius
Gathering detailed insights and metrics for @hishprorg/assumenda-tempora-eius
Gathering detailed insights and metrics for @hishprorg/assumenda-tempora-eius
Gathering detailed insights and metrics for @hishprorg/assumenda-tempora-eius
npm install @hishprorg/assumenda-tempora-eius
Typescript
Module System
Node Version
NPM Version
69.8
Supply Chain
99.5
Quality
84.9
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
50%
3
Compared to previous month
Last year
0%
12
Compared to previous year
30
Node streams2 over Primus: added back-pressure!
stream.Duplex
. Do this on both sides of a Primus connection.write
returns true
when the receiver is full.read
and readable
to exert back-pressure on the sender.The API is described here.
Exerting backpressure over Primus:
1var Primus = require('primus'), 2 PrimusDuplex = require('@hishprorg/assumenda-tempora-eius').PrimusDuplex, 3 primus = Primus.createServer({ port: 9000 }), 4 Socket = primus.Socket, 5 assert = require('assert'), 6 read_a = false; 7 8primus.once('connection', function (spark) 9{ 10 var spark_duplex = new PrimusDuplex(spark, 11 { 12 highWaterMark: 2 13 }); 14 15 assert.equal(spark_duplex.write('ab'), false); 16 17 spark_duplex.on('drain', function () 18 { 19 assert(read_a); 20 }); 21}); 22 23var client_duplex = new PrimusDuplex(new Socket('http://localhost:9000'), 24{ 25 highWaterMark: 1 26}); 27 28client_duplex.once('readable', function () 29{ 30 assert.equal(this.read().toString(), 'a'); 31 read_a = true; 32 assert.equal(this.read(), null); 33 34 this.once('readable', function () 35 { 36 assert.equal(this.read().toString(), 'b'); 37 primus.end(); 38 console.log('done') 39 }); 40});
Piping data over Primus:
1var Primus = require('primus'), 2 PrimusDuplex = require('@hishprorg/assumenda-tempora-eius').PrimusDuplex, 3 primus = Primus.createServer({ port: 9000 }), 4 Socket = primus.Socket, 5 assert = require('assert'), 6 crypto = require('crypto'), 7 tmp = require('tmp'), 8 fs = require('fs'); 9 10primus.once('connection', function (spark) 11{ 12 var spark_duplex = new PrimusDuplex(spark); 13 14 tmp.tmpName(function (err, random_file) 15 { 16 assert.ifError(err); 17 18 var random_buf = crypto.randomBytes(1024 * 1024); 19 20 fs.writeFile(random_file, random_buf, function (err) 21 { 22 assert.ifError(err); 23 24 tmp.tmpName(function (err, out_file) 25 { 26 assert.ifError(err); 27 28 var random_stream = fs.createReadStream(random_file), 29 out_stream = fs.createWriteStream(out_file); 30 31 out_stream.on('finish', function () 32 { 33 fs.readFile(out_file, function (err, out_buf) 34 { 35 assert.ifError(err); 36 assert.deepEqual(out_buf, random_buf); 37 38 fs.unlink(random_file, function (err) 39 { 40 assert.ifError(err); 41 fs.unlink(out_file, function (err) 42 { 43 assert.ifError(err); 44 primus.end(); 45 console.log('done'); 46 }); 47 }); 48 }); 49 }); 50 51 spark_duplex.pipe(out_stream); 52 random_stream.pipe(spark_duplex); 53 }); 54 }); 55 }); 56}); 57 58var client_duplex = new PrimusDuplex(new Socket('http://localhost:9000')); 59client_duplex.pipe(client_duplex);
1npm install @hishprorg/assumenda-tempora-eius
Node client to Node server:
1grunt test
Browser client to Node server (requires PhantomJS):
1grunt test-browser
1grunt coverage
c8 results are available here.
Coveralls page is here.
1grunt lint
PrimusDuplex
inherits from stream.Duplex
so you can call any method from stream.Readable
and stream.Writable
.
Extra constructor options and an additional parameter to readable.read
are described below.
Source: index.js
Creates a new
PrimusDuplex
object which exerts back-pressure over a Primus connection.
Both sides of a Primus connection must use PrimusDuplex
— create one for your Primus client and one for your spark as soon as you have them.
Parameters:
{Object} msg_stream
The Primus client or spark you wish to exert back-pressure over.{Object} [options]
Configuration options. This is passed onto stream.Duplex
and can contain the following extra properties:
{Function} [encode_data(chunk, encoding, start, end, internal)]
Optional encoding function for data passed to writable.write
. chunk
and encoding
are as described in the writable.write
documentation. The difference is that encode_data
is synchronous (it must return the encoded data) and it should only encode data between the start
and end
positions in chunk
. Defaults to a function which does chunk.toString('base64', start, end)
. Note that PrimusDuplex
may also pass some internal data through this function (always with chunk
as a Buffer
, encoding=null
and internal=true
).
{Function} [decode_data(chunk, internal)]
Optional decoding function for data received on the Primus connection. The type of chunk
will depend on how the peer PrimusDuplex
encoded it. Defaults to a function which does Buffer.from(chunk, 'base64')
. If the data can't be decoded, return null
(and optionally call this.emit
to emit an error). Note that PrimusDuplex
may also pass some internal data through this function (always with internal=true
) — in which case you must return a Buffer
.
{Integer} [max_write_size]
Maximum number of bytes to write onto the Primus connection at once, regardless of how many bytes the peer is free to receive. Defaults to 0 (no limit).
{Boolean} [check_read_overflow]
Whether to check if more data than expected is being received. If true
and the high-water mark for reading is exceeded then the PrimusDuplex
object emits an error
event. This should not normally occur unless you add data yourself using readable.unshift
— in which case you should set check_read_overflow
to false
. Defaults to true
.
Go: TOC
See
readable.read
.PrimusDuplex
adds an extra optional parameter,send_status
.
Parameters:
{Number} [size]
Optional argument to specify how much data to read. Defaults to undefined
(you can also specify null
) which means return all the data available.{Boolean} [send_status]
Every time you call read
, a status message is sent to the peer PrimusDuplex
indicating how much space is left in the internal buffer to receive new data. To prevent deadlock, these status messages are always sent — they aren't subject to back-pressure. Normally this is fine because status messages are small. However, if your application reads data one byte at a time, for example, you may wish to control when status messages are sent. To stop a status message being sent when you call read
, pass send_status
as false
. send_status
defaults to true
. To force a status message to be sent without reading any data, call read(0)
.Go: TOC | PrimusDuplex.prototype
—generated by apidox—
No vulnerabilities found.
No security vulnerabilities found.