nv-easy-fetch ======================= - a simple fetch server/client for beginner - can directly send/recv any-javascript-value: excpet function-like | weak-like | stream-like - you MUST use the server/client together, it NOT compatible with other serve
npm install nv-easy-fetch
nv-easy-fetch
=======================
- a simple fetch server/client for beginner
- can directly send/recv any-javascript-value: excpet function-like | weak-like | stream-like
- you MUST use the server/client together, it NOT compatible with other server/client
install
=======
- npm install nv-easy-fetch
splitted
--------
usage
=====
const {
usage,
bw_client,
nd_client,
creat_srv_using_nodejs,
creat_srv_using_uwebsocket
} = require("nv-easy-fetch");
example
-------
const jsval_fetch_from_url = async(url) => {
if(window.jsval_fetch) {
} else {
let code = await (await fetch(url)).text();
let script = document.createElement("script");
script.innerText = code;
document.head.appendChild(script);
}
return(window.jsval_fetch)
}
const {get,post} = await jsval_fetch_from_url("http://192.168.1.103:7777/");
await get("http://192.168.1.103:7777/", headers={},extra={})
await post("http://192.168.1.103:7777/", new Map([[123,new Set()],[undefined,NaN]]), headers={},extra={})
const {get,post} = nd_client;
var r = await post("../___usock___#", new Map([[1,2],[3,4]]), headers={},tls_options={rejectUnauthorized:false})
// 【when using unix_sock , need end/split with "#" such as "x/y#" "x/y#/a/b/c/d...." 】
var r = await post("http://127.0.0.1:7777/",new Map([[1,2],[3,4]]), headers={},tls_options={rejectUnauthorized:false})
var r = await post("https://127.0.0.1:7443/",[new Date, 12345678901234567890n,new Set()],headers={},tls_options={rejectUnauthorized:false});
const creat_srv = creat_srv_using_nodejs;
//for example: we reply a complicate jsvalue:
var TST_DATA_FOR_REPLY = [
{circular:null},
undefined,null,true,false,
"str",1.1,123456789n,new Date,/a-z/g,
new Set([1,2,3,4,5,6]),
new Map([[11,22],[33,44]]),
new Uint8Array([1,2,3,4]),
new ArrayBuffer(10),
new DataView(new ArrayBuffer(8, { maxByteLength: 16 })),
new Error("1",{cause:"xxx"}),new AggregateError([new Error(),new Error()]),
];
TST_DATA_FOR_REPLY[0].circular = TST_DATA_FOR_REPLY;
//write your server handle:
var ahandle = async (recved_data, recved_headers, from_client) => {
console.log({recved_data, recved_headers,from_client});
if(recved_data instanceof Map) {
return(TST_DATA_FOR_REPLY);
} else {
throw(new Error("must-be-map"))
}
};
// we create 4 server:
var srv_using_usock = await creat_srv("../___usock___",ahandle);
var srv_using_http = await creat_srv("http://127.0.0.1:7777/",ahandle);
var srv_using_unsafe_https = await creat_srv("https://127.0.0.1:7443/",ahandle);
var srv_using_https = await creat_srv(
https://127.0.0.1:7443/?,
rejectUnauthorized &
key = test/fixtures/keys/agent2-key.pem &
cert = test/fixtures/keys/agent2-cert.pem
ahandle
);
// from client:
//on server:
{
recved_data: Map(2) { 1 => 2, 3 => 4 },
recved_headers: {
'content-type': 'application/octet-stream',
'---dtype---': 'v8ser',
'---bytsz---': '13',
host: '127.0.0.1:7777',
connection: 'close',
'transfer-encoding': 'chunked'
},
from_client: {
address: '127.0.0.1',
family: 'IPv4',
port: 33724,
url: '/',
method: 'POST',
httpVersion: '1.1'
}
}
*/
var e = await post("http://127.0.0.1:7777/", 1234567890n)
console.log(e instanceof Error) //true
var r = await post("../___usock___#",new Map([[1,2],[3,4]]));
IF
need end/split with a "#":
such as :
/xx/yy/ww/usock#/a/b/c/d
../usock#
var e = await post("../___usock___#", 1234567890n)
console.log(e instanceof Error) //true
const creat_srv = creat_srv_using_uwebsocket;
//the api is a little from nodejs server
// refer to below
METHODS
========
APIS
=======
usage.how_to_use_browser_client()
const jsval_fetch_from_url = async(url) => {
if(window.jsval_fetch) {
} else {
let code = await (await fetch(url)).text();
let script = document.createElement("script");
script.innerText = code;
document.head.appendChild(script);
}
return(window.jsval_fetch)
}
const {get,post} = await jsval_fetch_from_url(
await get(
await post(
$3
usage.how_to_use_nodejs_client()
//npm install nv-jsval-fetch-nd
const {get,post} = require("nv-jsval-fetch-nd");
await get (
await post(
IF
need end/split with a "#":
such as :
/xx/yy/ww/usock#/a/b/c/d
../usock#
usage.how_to_use_srv_with_nodejs_builtin()
//npm install nv-jsval-fetch-srv-api-if-using-nodejs-builtin
String@is_file_path 【will be treated as using unix_socket】;
String@is_srv_launch_url::EXAMPLES :
IF USING HTTP:
http://:
http://
http://
IF USING HTTPS:
IF USING UNSAFE HTTPS: 【NONEED provide key AND cert】
https://:
https://
https://
IF USING SAFE HTTPS: 【MUST provide key AND cert】
https://:
https://
https://
;
JsValhandle : async(
received_data : @any_jsval_except
received_headers : Dict
client : Dict
)=> {
IF return(reply_data: @any_jsval_except
IF throw(reply_error: Error) THEN will send reply_error to client
},
creat_srv(
listen_on : String@is_file_path | String@is_srv_launch_url,
jsval_handle : JsValhandle, 【jsval handle】
other_handle : async(res,req)=>{}, 【
//normal handle
//the default normal handle,will reply a jsval_fetch.js javascript for browser
res.writeHead(200,'OK',{'Content-Type': 'application/javascript'});
res.end(BWAPI_CODE);
】
chown_to_user : String|undefined 【
this argument ONLY make sense IF the
IF you NOT use nginx:
just let it be undefined,
ELSE
its a uname such as "www-data"
】,
key_buf : ArrayBuffer | Buffer | Array
IF this is NOT undefined:
the
will using this as ssl-key
】,
cert_buf : ArrayBuffer | Buffer | Array
IF this is NOT undefined:
the
will using this as ssl-cert
】,
) -> Promise
``typescript
//npm install nv-jsval-fetch-srv-api-if-using-uwebsocket
String@is_file_path 【will be treated as using unix_socket】;
String@is_srv_launch_url::EXAMPLES :
IF USING HTTP:
http://:
http://
http://
IF USING HTTPS:
IF USING UNSAFE HTTPS: 【NONEED provide key AND cert】
https://:
https://
https://
IF USING SAFE HTTPS: 【MUST provide key AND cert】
https://:
https://
https://
;
JsValhandle : async(
received_data : @any_jsval_except
received_headers : Dict
client : Dict
)=> {
IF return(reply_data: @any_jsval_except
IF throw(reply_error: Error) THEN will send reply_error to client
};
OtherHandleForUwebsocket : async(url, mthd, reqd:Dict, res:
//its different with nodejs,
//the res is res of uWebsocket refer to its documents
//the default normal handle,will reply a jsval_fetch.js javascript for browser
res.writeStatus('200 OK');
res.writeHeader('Content-Type','application/javascript');
res.end(BWAPI_CODE);
};
creat_srv(
listen_on : String@is_file_path | String@is_srv_launch_url,
jsval_handle : JsValhandle, 【jsval handle】
other_handle : OtherHandleForUwebsocket, 【uwebsocket original handle】
chown_to_user : String|undefined 【
this argument ONLY make sense IF the
IF you NOT use nginx:
just let it be undefined,
ELSE
its a uname such as "www-data"
】,
) -> Promise
``
LICENSE
=======
- ISC