# nv-server-simple-stream

> nv-server-simple-stream ======================= - nv-server-simple-stream - simple http server - support unix-socket - support dynamic change handler - support manual mode - for testbed using

Latest version **1.0.36** (published 2022-09-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install nv-server-simple-stream
pnpm add nv-server-simple-stream
yarn add nv-server-simple-stream
bun add nv-server-simple-stream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.36 |
| Published | 2022-09-19 |
| First published | 2021-08-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 51.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ihgazni2 |

## Links

- npm: https://www.npmjs.com/package/nv-server-simple-stream
- npm.io page: https://npm.io/package/nv-server-simple-stream

## Dependencies (11)

- [nanoid](https://npm.io/package/nanoid.md) ^3.1.23
- [lzw-stream](https://npm.io/package/lzw-stream.md) ^1.0.0
- [range-parser](https://npm.io/package/range-parser.md) ^1.2.1
- [content-range](https://npm.io/package/content-range.md) ^2.0.0
- [nv-string-basic](https://npm.io/package/nv-string-basic.md) ^1.0.31
- [nv-facutil-basic](https://npm.io/package/nv-facutil-basic.md) ^1.2.14
- [nv-file-compress](https://npm.io/package/nv-file-compress.md) ^1.0.3
- [nv-facutil-stream](https://npm.io/package/nv-facutil-stream.md) ^1.0.11
- [nv-facutil-promise](https://npm.io/package/nv-facutil-promise.md) ^1.0.29
- [content-type-parser](https://npm.io/package/content-type-parser.md) ^1.0.2
- [nv-facutil-simple-url](https://npm.io/package/nv-facutil-simple-url.md) ^0.0.1

## Recent versions

- 1.0.36 (latest) — 2022-09-19
- 1.0.35 — 2022-09-19
- 1.0.33 — 2022-08-08
- 1.0.32 — 2022-03-01
- 1.0.31 — 2022-03-01
- 1.0.30 — 2022-03-01
- 1.0.29 — 2022-03-01
- 1.0.28 — 2022-02-28
- 1.0.26 — 2022-02-19
- 1.0.25 — 2022-02-15
- 1.0.24 — 2022-02-09
- 1.0.23 — 2022-02-08
- 1.0.22 — 2022-02-08
- 1.0.21 — 2022-02-08
- 1.0.20 — 2022-02-08
- … 18 more at https://npm.io/package/nv-server-simple-stream/versions

## README

nv-server-simple-stream
=======================
- nv-server-simple-stream    
- simple  http server
- support unix-socket
- support dynamic change handler
- support manual mode
- for testbed using

install
=======
- npm install nv-server-simple-stream 

usage
=====

      const S = require("nv-server-simple-stream").http.simple;
    
example
-------

###  

     //await S.creat(port_or_unix_sock,handler/*default is empty for manual mode*/,opts)
     var srv = await S.creat(8888)


### cfg

    > S.DFLT_CFG()
    {
      opts: {
        IncomingMessage: [Function: IncomingMessage],
        ServerResponse: [Function: ServerResponse],
        insecureHTTPParser: false,
        maxHeaderSize: 16384
      }
      //----------used when port is undefined
      unix_sock: '___usock___',
      dirname: './',
    }
    >

###  set handler

        srv.handler_ = async (req,res) => {
            console.log('AAA')
            let r = await S.reply_with_json(res,{rslt:'recved'});
            return(r)
        }

        ////on client

         # curl http://127.0.0.1:8888/a/b?xx=77
         # {"rslt":"recved"}

###  change handler without stopping server

        srv.handler_ = async (req,res) => {
            console.log('BBB')
            let r = await S.reply_with_json(res,{rslt:'recved-BBB'});
            return(r)
        }

        # curl http://127.0.0.1:8888/a/b?xx=77
        # {"rslt":"recved-BBB"}
        
###  check request content
        
        
            srv.handler_ = async (req,res) => {
                console.log('CCC')
                let D = await S.extract_req(req);          //----------------->
                console.log(D)
                let r = await S.reply_with_json(res,{rslt:'recved'});
                return(r)
            } 

             # curl http://127.0.0.1:8888/a/b?xx=77

            /*
            > CCC
            {
              remoteAddress: '::ffff:127.0.0.1',
              remotePort: undefined,
              host: '127.0.0.1:8888',
              hostname: '127.0.0.1',
              port: '8888',
              method: 'GET',
              path: '/a/b?xx=77',
              headers: {
                host: '127.0.0.1:8888',
                'user-agent': 'curl/7.58.0',
                accept: '*/*'
              },
              body: <Buffer >
            }
            */


###         validate request


        srv.handler_ = async (req,res) => {
            console.log('DDD')
            let D = await S.extract_req(req);
            let r;
            if(D.path.includes('77')) {
                r = await S.reply_with_json(res,{rslt:'recved'});
            } else {
                r = await S.reply_with_json(res,{err:'wrong-path'}); //--------------------->
                await S.teardown(req)
            }
            return(r)
        }

        /*
        # curl http://127.0.0.1:8888/a/b?xx=77
        {"rslt":"recved"}


        # curl http://127.0.0.1:8888/a/b?xx=999  //----------------------
        {"err":"wrong-path"}            //------------------------------------------------->
        nv-data-tree-repr#

        */


###  Manual Mode

      //---must set the handler to a empty function:

      srv.handler_ = async (req,res) => {}


      ##  on client
      /*
        nv-data-tree-repr# curl -v http://127.0.0.1:8888/a/b?xx=999
        *   Trying 127.0.0.1...
        * TCP_NODELAY set
        * Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
        > GET /a/b?xx=999 HTTP/1.1
        > Host: 127.0.0.1:8888
        > User-Agent: curl/7.58.0
        > Accept: */*
        >

        //---------------------stucked now wait for server

        */
        

         //on server side, using srv.lst_req_ to check the request ,its a getter

             D = await S.extract_req(srv.lst_req_);
 
            {
              remoteAddress: '::ffff:127.0.0.1',
              remotePort: 48868,
              host: '127.0.0.1:8888',
              hostname: '127.0.0.1',
              port: '8888',
              method: 'GET',
              path: '/a/b?xx=999',
              headers: {
                host: '127.0.0.1:8888',
                'user-agent': 'curl/7.58.0',
                accept: '*/*'
              },
              body: <Buffer >
            }


        // test send with a stream
           var rs$ = fs.createReadStream("./tst-stream") 
           await S.reply_with_stream(srv.lst_res_,rs$)


        #on client
        
        /*
        < HTTP/1.1 200 OK
        < Content-Type: application/octet-stream
        < Date: Tue, 15 Feb 2022 16:24:39 GMT
        < Connection: keep-alive
        < Keep-Alive: timeout=5
        < Transfer-Encoding: chunked
        <
        111122222
        333344444
        * Connection #0 to host 127.0.0.1 left intact

        */ 


###  encoding AND dynamic switch callback handle

    const S = require("nv-server-simple-stream").http.simple;


    (async ()=>{
        var srv = await S.creat(8888);
        ////
        srv.handler_ = async (req,res) => {
            //// init handle
            await S.reply_with_html(res,'<html></html>');   //初次請求handler


            //// switch handle after init                   //切換handler
            srv.handler_ = async (req,res) => {
                let D = await S.extract_req(req);
                let reply_with_stream_with_content_encoding = S.creat_reply_with_stream_with_content_encoding(D);
                let rs$ = fs.createReadStream("./package-lock.json");
                let r = await reply_with_stream_with_content_encoding(res,rs$);
                return(r)
            }
        }
    })();

    //nginx http:8888->https:8888    for test 'br; which supported by chrome only over https

    var res0=await fetch("https://192.168.116.129:8888")  // first get <html></html>


    var res=await fetch("https://192.168.116.129:8888")
    rslt = await fmt_res(res)
    data = await concat_achunk(rslt)

    /*
        Connection: keep-alive
        Content-Encoding: br
        Content-Type: application/octet-stream
        Date: Tue, 01 Mar 2022 03:44:26 GMT
        Server: nginx/1.14.0 (Ubuntu)
        Transfer-Encoding: chunked

        rslt = await fmt_res(res)
        data = await concat_achunk(rslt)

        var te = new TextDecoder('utf8')
        JSON.parse(te.decode(data.body))

        {name: 'simple-server', version: '1.0.0', lockfileVersion: 1, requires: true, dependencies: {…}}


    */


METHODS
=======

### getter/setter  for dynamic change handle

      srv.handler_     

### getter:  last request AND reply

      //be used in manual mode when handler is empty function 
      srv.lst_req_ 
      srv.lst_res_




API
====

- async creat(sock\_or\_port,handler=DFLT\_HANDLER ,cfg=DFLT\_CFG())

- async extract\_req(req)

- set\_reply\_head\_to\_bytes(res,code=200,msg='OK',opts={})
- set\_reply\_head\_to\_html(res,code=200,msg='OK',opts={})
- set\_reply\_head\_to\_json(res,code=200,msg='OK',opts={})

- async send\_reply\_raw\_body(res,data)
- async send\_reply\_json\_body(res,J)
- async send\_reply\_stream\_body(res,$rs)

- async end\_reply(res)


- async reply\_with\_json (res,J,code=200,msg='OK',opts={})
- async reply\_with\_bytes (res,data,code=200,msg='OK',opts={})
- async reply\_with\_html(res,data,code=200,msg='OK',opts={})
- async reply\_with\_stream (res,rs$,code=200,msg='OK',opts={})


- async teardown(req,err='not-match')


####

        {
          DFLT_CFG: [Function: DFLT_CFG],
          DFLT_HANDLER: [AsyncFunction: DFLT_HANDLER],
          set_reply_head_to_bytes: [Function: set_reply_head_to_bytes],
          set_reply_head_to_br_bytes: [Function: set_reply_head_to_br_bytes],
          set_reply_head_to_gzip_bytes: [Function: set_reply_head_to_gzip_bytes],
          set_reply_head_to_compress_bytes: [Function: set_reply_head_to_compress_bytes],
          set_reply_head_to_deflate_bytes: [Function: set_reply_head_to_deflate_bytes],
          send_reply_stream_body_with_content_encoding: [AsyncFunction: send_reply_stream_body_with_content_encoding],
          creat_reply_with_stream_with_content_encoding: [Function: creat_reply_with_stream_with_content_encoding],
        }

LICENSE
=======
- ISC

---
_Source: https://npm.io/package/nv-server-simple-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
