1.0.14 • Published 1 year ago
nv-client-simple-stream v1.0.14
nv-client-simple-stream
- nv-client-simple-stream
install
- npm install nv-client-simple-stream
usage
http
client
const {Client} = require("nv-client-simple-stream").http;
//new Client((url,query={},hash=''))
server is a simple echo-server
init
var c = new Client("http://127.0.0.1:8000/a/b/c")
/*
{
hostname: '127.0.0.1',
port: '8000',
path: '/a/b/c/',
method: 'POST',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Content-Type': 'application/octet-stream',
'X-Body-Type': 'buffer'
},
keepalive: true
}
*/
simple_send_with_file
var r = await c.simple_send_with_file("./index.js")
>r
{
err: null,
headers: {
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:05:53 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
},
body: <Buffer 6d 6f 64 75 6c 65 2e 65 78 70 6f 72 74 73 20 3d 20 7b 0a 20 20 20 20 68 74 74 70 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 6c 69 65 6e 74 3a 72 65 71 75 ... 281 more bytes>,
local: { address: '127.0.0.1', family: 'IPv4', port: 33916 },
peer: { address: '127.0.0.1', family: 'IPv4', port: 8000 }
}
simple_send_with_read_stream
var read$ = fs.createReadStream("./index.js")
var r = await c.simple_send_with_read_stream(read$)
> r
{
err: null,
headers: {
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:07:50 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
},
body: <Buffer 6d 6f 64 75 6c 65 2e 65 78 70 6f 72 74 73 20 3d 20 7b 0a 20 20 20 20 68 74 74 70 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 6c 69 65 6e 74 3a 72 65 71 75 ... 281 more bytes>,
local: { address: '127.0.0.1', family: 'IPv4', port: 33916 },
peer: { address: '127.0.0.1', family: 'IPv4', port: 8000 }
}
>
simple
var r = await c.simple('ABCDE')
> r
{
err: null,
headers: {
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:10:07 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
},
body: <Buffer 41 42 43 44 45>,
local: { address: '127.0.0.1', family: 'IPv4', port: 33916 },
peer: { address: '127.0.0.1', family: 'IPv4', port: 8000 }
}
>
send_with_file_and_manual_recv
function recv_chunk_handler(chunk,creq,cres) {
console.log("received chunk: ",chunk.length)
}
var r = await c.send_with_file_and_manual_recv("./index.js",recv_chunk_handler)
>
received chunk: 331
> r
{
err: null,
headers: {
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:32:23 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
},
local: { address: '127.0.0.1', family: 'IPv4', port: 33918 },
peer: { address: '127.0.0.1', family: 'IPv4', port: 8000 }
}
>
send_with_read_stream_and_manual_recv
function recv_chunk_handler(chunk,creq,cres) {
console.log("received chunk: ",chunk.length)
}
var read$ = fs.createReadStream("/mnt/sdb/NVNODE/node/out/Release/node")
var r = await c.send_with_read_stream_and_manual_recv(read$,recv_chunk_handler)
/*
....
received chunk: 12
received chunk: 60
received chunk: 65450
received chunk: 26
received chunk: 60
received chunk: 65464
received chunk: 12
received chunk: 60
received chunk: 65450
received chunk: 26
received chunk: 60
received chunk: 65464
....
> r
{
err: null,
headers: {
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:36:46 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
},
local: { address: '127.0.0.1', family: 'IPv4', port: 33918 },
peer: { address: '127.0.0.1', family: 'IPv4', port: 8000 }
}
>
Release# netstat -an | egrep tcp | egrep ESTABLISHED | egrep 8000
tcp 0 0 127.0.0.1:33918 127.0.0.1:8000 ESTABLISHED
tcp6 0 0 127.0.0.1:8000 127.0.0.1:33918 ESTABLISHED
Release#
*/
manual
function recv_chunk_handler(chunk,creq,cres) {
console.log("received chunk: ",chunk.length)
}
var {headp,bodyp,send,end} = c.manual(recv_chunk_handler)
> headp
Promise { <pending> }
> bodyp
Promise { <pending> }
> send.toString()
"async function send(chunk,encoding='utf8') {return(await _send(creq,chunk))}"
>
> end.toString()
'async function end() {return(await _end(creq))}'
>
var r = await send('ABCD')
> received chunk: 4 //body chunk handler triggered
> headp
Promise {
{
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:54:30 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
}
}
>
> var r = await send('ABCD')
> received chunk: 4
> var r = await send('XTz')
> received chunk: 3
> bodyp
Promise { <pending> }
>
> var r = await end()
> r
true
> bodyp
Promise {
{
err: null,
headers: {
'content-type': 'application/octet-stream',
'x-body-type': 'buffer',
date: 'Sat, 07 Aug 2021 09:55:22 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=4294',
'transfer-encoding': 'chunked'
},
local: { address: '127.0.0.1', family: 'IPv4', port: 33924 },
peer: { address: '127.0.0.1', family: 'IPv4', port: 8000 }
}
}
>
https
- todo
http2
- todo
METHODS
http
c.options_
c.url_
c.method_
c.disable_keepalive c.enable_keepalive c.keepalive_
c.headers_
c.body_type_
c.simple(str_or_buffer,encode='utf8',keepalive=true,with_rqrs=false)
c.simple_send_with_file(file_name,keepalive=true,with_rqrs=false)
c.simple_send_with_read_stream(read_stream$,keepalive=true,with_rqrs=false)
c.send_with_file_and_manual_recv(fn,recv_chunk_handler=(chunk,creq,cres)=>{},keepalive,with_rqrs=false)
c.send_with_read_stream_and_manual_recv(read$,recv_chunk_handler=(chunk,creq,cres)=>{},keepalive,with_rqrs=false)
c.send_with_sb_and_manual_recv(
str_or_buffer,
recv_chunk_handler=(chunk,creq,cres)=>{},
encode='utf8',
keepalive,
with_rqrs=false
)
c.manual(recv_chunk_handler=(chunk,creq,cres)=>{},keepalive,with_rqrs=false)
c.set_method_to_post
c.set_method_to_get
c.set_method_to_head
c.set_method_to_connect
c.set_method_to_delete
c.set_method_to_options
c.set_method_to_patch
c.set_method_to_put
c.set_method_to_trace
https
const {Client} = require("nv-client-simple-stream").https;
//new Client((url,query={},hash=''))
init
var c = new Client("https://192.168.1.103:28443")
var r = await c.simple('ABCDE')
http2
- todo
API
http
_simple_creq(that,keepalive=true,with_rqrs=false);
_manual_creq(that,recv_chunk_handler,keepalive=true,with_rqrs=false);
https
_simple_creq(that,keepalive=false,with_rqrs=false);
_manual_creq(that,recv_chunk_handler,keepalive=false,with_rqrs=false);
one_time_get(u,query,hash)
http2
- todo
LICENSE
- ISC