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.0.2
  • 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 release notes

Full notes here

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.

3.2.2: bug fix: decodeURIComponent no longer called on response body.

3.2.0: Filters only apply per routing

combining routes using withRoutes no longer combines filters from each routing.

3.1.0: New backend server: NativeHttpsServer.

See here for more info

3.0.2: New client: HttpsClient.

3.0.0: Breaking change: routing paths are declared absolute, not relative.

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
    • extract building up form when asked for form() or formField()
    • servers
    • clients
  • withOptions
  • 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

Keywords

FAQs

Package last updated on 04 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