New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

http4js

Package Overview
Dependencies
Maintainers
2
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http4js

A lightweight HTTP toolkit

  • 4.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
343
decreased by-37.52%
Maintainers
2
Weekly downloads
 
Created
Source

http4js

A lightweight HTTP framework for Typescript / JS, with zero dependencies

*** read the docs ***

Latest features

Full Release Notes here

4.1.1: Fix: HttpClient was not streaming out

See streaming docs for more info

4.1.0: streaming by default

NativeHttpServer and HttpClient stream in and out by default. A handle on the stream is provided by req.bodyStream() and a res is streamed out if a Res(200, BodyOf(readable)) is provided, i.e. a Readable stream body.

4.0.0: ! Breaking change: drop support for Koa and Express backends

In order to evolve the core library faster support for Express and Koa backends has been dropped. Happy to add back later.

Use http4js in your project

To install:

npm install --save http4js

or

yarn add http4js

Example

An example server and client


//define our routes
const routing = routes('GET', ".*", async (req: Req) => {
    console.log(req);
    return ResOf(Status.OK, 'OK');
})

//add csrf token header to every request and vary gzip to every response
const headerFilter = (handler: HttpHandler) => {
    return async (req: Req) => {
        const response = await handler(req.withHeader(Headers.X_CSRF_TOKEN, Math.random()))
        return response.withHeader(Headers.VARY, "gzip");
    }
};

routing
    .withFilter(headerFilter)
    .asServer() // starts on port 3000 by default
    .start();

//make an http request to our server and log the response
HttpClient(ReqOf(Method.GET, "http://localhost:3000/any/path"))

// output
Req {
  headers: 
   { host: 'localhost:3000',
     connection: 'close',
     'content-type': 'application/x-www-form-urlencoded',
     'x-csrf-token': 0.8369821184747923 },
  queries: {},
  pathParams: {},
  form: {},
  method: 'GET',
  uri: 
   Uri {
     matches: {},
     asNativeNodeRequest: 
      Url {
        ...,
        protocol: 'http:',
        host: 'localhost:3000',
        port: '3000',
        pathname: '/any/path',
        path: '/any/path',
        href: 'http://localhost:3000/any/path' } },
  body: '' }


Contributing

I'd be very happy if you'd like to contribute :)

To run:

git clone git@github.com:TomShacham/http4js.git && \ 
cd http4js && \
yarn && \
yarn build && \
yarn test

History and Design

http4js is a port of http4k.

The concept is called Server as a Function (SaaF).

Early ideas and influence from Daniel Bodart's Utterly Idle

To dos

  • streaming
    • clean up BodyOf leak
    • servers, https O
    • clients, https O
  • refactor req and res to not use clone and instead construct new self
  • extract Form
  • example app
  • withOptions on withPost
  • convenience response methods eg ok()
  • generalise routing to an interface, use totallylazy to implement new types of routing
  • chain withHeaders calls on an http client
  • client side httpclient (from stu)
  • update example app
  • reversible routing
    • what happens if names conflict?
    • what if multiple handlers match by path

Running HTTPS Server tests

We need our own certs to run an HTTPS server locally.

These Commands get you most of the way, I altered them slightly for this script, that may work for you

./create-ssl-certs.sh

If not, follow these Instructions to create your own certificates in order to run an HTTPS server locally.

Then run

yarn test-ssl

Sanity Testing Streaming

Create a big file

cat /dev/urandom | base64 >> bigfile.txt
# wait ...
# ^C

Start up a server and stream the file

get('/bigfile', async() => ResOf(200, BodyOf(fs.createReadStream('./bigfile.txt'))))
    .asServer()
    .start();

Check the memory of usage of the process.

  • If we are not streaming, then the whole file will be read into memory before the server responds, using lots of memory.
  • If we are streaming then the memory usage should be much lower.

Keywords

FAQs

Package last updated on 08 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc