18.2.4 • Published 4 months ago

woodland v18.2.4

Weekly downloads
83
License
BSD-3-Clause
Repository
github
Last release
4 months ago

Woodland

Lightweight HTTP framework with automatic headers. Routes can use parameter syntax, i.e. /users/:id, or RegExp syntax. Route parameters are not sanitized. If 2+ routes with parameters match a request the first route will be used to extract parameters. All HTTP methods are supported.

CORS (Cross Origin Resource Sharing) is automatically handled, and indicated with cors Boolean on the request Object for middleware.

Middleware arguments can be req, res, next or error, req, res, next. If no Error handling middleware is registered woodland will handle it.

Using the factory

import {createServer} from "node:http";
import {woodland} from "woodland";

const app = woodland({
  defaultHeaders: {
    "cache-control": "public, max-age=3600",
    "content-type": "text/plain"
  },
  time: true
});

app.get("/", (req, res) => res.send("Custom greeting at '/:user', try it out!"));
app.get("/:user", (req, res) => res.send(`Hello ${req.params.user}!`));
createServer(app.route).listen(8000);

Using the Class

import {Woodland} from "woodland";
class MyFramework extends Woodland {};

Testing

Woodland has 100% code coverage with its tests; the missing 0.22% is hard to reach conditions.

--------------|---------|----------|---------|---------|-------------------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------------------
All files     |   99.78 |    76.02 |   98.57 |     100 | 
 woodland.cjs |   99.78 |    76.02 |   98.57 |     100 | ...214,254,275-285,314-328,...
--------------|---------|----------|---------|---------|-------------------------------

Benchmark

Please benchmark woodland on your target hardware to understand the overhead which is expected to be <15% with etags disabled, or <25% with etags enabled. E.g. if http can handle 50k req/s, then woodland should handle 43k req/s.

  1. Clone repository from GitHub.
  2. Install dependencies with npm or yarn.
  3. Execute benchmark script with npm or yarn.

Results with node.js 20.8.0 & an Intel i9-12900HX (mobile) on Windows 11, with etags disabled.

> node benchmark.js

http
┌─────────┬──────┬──────┬───────┬───────┬──────────┬──────────┬───────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%   │ Avg      │ Stdev    │ Max   │
├─────────┼──────┼──────┼───────┼───────┼──────────┼──────────┼───────┤
│ Latency │ 1 ms │ 8 ms │ 42 ms │ 47 ms │ 10.81 ms │ 10.26 ms │ 88 ms │
└─────────┴──────┴──────┴───────┴───────┴──────────┴──────────┴───────┘
┌───────────┬─────────┬─────────┬───────┬───────┬─────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%   │ 97.5% │ Avg     │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼───────┼───────┼─────────┼─────────┼─────────┤
│ Req/Sec   │ 75967   │ 75967   │ 88703 │ 93823 │ 88409.6 │ 4152.76 │ 75952   │
├───────────┼─────────┼─────────┼───────┼───────┼─────────┼─────────┼─────────┤
│ Bytes/Sec │ 15.4 MB │ 15.4 MB │ 18 MB │ 19 MB │ 17.9 MB │ 841 kB  │ 15.4 MB │
└───────────┴─────────┴─────────┴───────┴───────┴─────────┴─────────┴─────────┘

woodland
┌─────────┬──────┬──────┬───────┬───────┬──────────┬──────────┬────────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%   │ Avg      │ Stdev    │ Max    │
├─────────┼──────┼──────┼───────┼───────┼──────────┼──────────┼────────┤
│ Latency │ 2 ms │ 9 ms │ 57 ms │ 67 ms │ 12.82 ms │ 13.04 ms │ 119 ms │
└─────────┴──────┴──────┴───────┴───────┴──────────┴──────────┴────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Req/Sec   │ 66687   │ 66687   │ 75263   │ 76095   │ 75041.61 │ 1482.92 │ 66667   │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Bytes/Sec │ 14.1 MB │ 14.1 MB │ 15.9 MB │ 16.1 MB │ 15.8 MB  │ 312 kB  │ 14.1 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘

API

constructor ({...})

Returns a woodland instance. Enable directory browsing & traversal with autoindex. Create an automatic x-response-time response header with time & digit. Customize etag response header with seed.

Configuration

{
  "autoindex": false,
  "cacheSize": 1000,
  "cacheTTL": 300000,
  "charset": "utf-8",
  "corsExpose": "",
  "defaultHeaders": {},
  "digit": 3,
  "etags": true,
  "indexes": [
    "index.htm",
    "index.html"
  ],
  "logging": {
    "enabled": true,
    "format": "%h %l %u %t \"%r\" %>s %b",
    "level": "info"
  },
  "origins": [
    "*"
  ],
  "silent": false,
  "time": false
}

allowed (method, uri, override = false)

Calls routes() and returns a Boolean to indicate if method is allowed for uri.

allows (uri, override = false)

Returns a String for the Allow header. Caches value, & will update cache if override is true.

always (path, fn)

Registers middleware for a route for all HTTP methods; runs first. path is a regular expression (as a string), and if not passed it defaults to /.*.

Execute ignore(fn) if you do not want the middleware included for calculating the Allow header.

ignore (fn)

Ignores fn for calculating the return of allows().

decorate (req, res)

Decorates allow, body, cors, host, ip, params, & parsed on req and error(status[, body, headers]), header(key, value), json(body[, status, headers]), locals{} & redirect(url[, perm = false]) on res.

etag (...args)

Returns a String to be used as an etag response header value.

list (method = "get", type = "array")

Returns an Array or Object of routes for the specified method.

log (msg = "", level = "debug")

Logs to stdout or stderr depending on the level, & what the minimum log level is set to.

onsend (req, res, body, status, headers)

Override to customize response body, status, or headers. Must return [body, status, headers]!

route (req, res)

Function for http.createServer() or https.createServer().

routes (uri, method, override = false)

Returns an Array of middleware for the request. Caches value, & will update cache if override is true.

serve (req, res, localFilePath, folderPath, indexes = this.indexes)

Serve static files on disk. Use a route parameter or remove folderPath from req.parsed.pathname to create localFilePath.

Without autoindex

app.use("/files/:file", (req, res) => app.serve(req, res, req.params.file, path.join(__dirname, "files")));

With autoindex

app.use("/files(/.*)?", (req, res) => app.serve(req, res, req.parsed.pathname.replace(/^\/files\/?/, ""), join(__dirname, "files")));

staticFiles (root = "/")

Serve static files on disk. Calls this.serve().

use (path = "/.*", ...fn, method = "GET")

Registers middleware for a route. path is a regular expression (as a string), and if not passed it defaults to /.*. See always() if you want the middleware to be used for all HTTP methods.

All HTTP methods are available on the prototype (partial application of the third argument), e.g. get([path,] ...fn) & options([path,] ...fn).

Command Line Interface (CLI)

When woodland is installed as a global module you can serve the contents of a folder by executing woodland in a shell. Optional parameters are --ip=127.0.0.1 & --port=8000.

Node.js v20.8.0
PS C:\Users\jason\Projects> npm install -g woodland

changed 6 packages in 1s
PS C:\Users\jason\Projects> woodland
id=woodland, hostname=localhost, ip=127.0.0.1, port=8000
127.0.0.1 -  [7/Oct/2023:15:18:18 -0400] "GET / HTTP/1.1" 200 1327
127.0.0.1 -  [7/Oct/2023:15:18:26 -0400] "GET /woodland/ HTTP/1.1" 200 2167
127.0.0.1 -  [7/Oct/2023:15:18:29 -0400] "GET /woodland/dist/ HTTP/1.1" 200 913
127.0.0.1 -  [7/Oct/2023:15:18:32 -0400] "GET /woodland/dist/woodland.js HTTP/1.1" 200 26385
127.0.0.1 -  [7/Oct/2023:15:18:47 -0400] "GET /woodland/benchmark.js HTTP/1.1" 200 1657
127.0.0.1 -  [7/Oct/2023:15:18:58 -0400] "GET /woodland/sample.js HTTP/1.1" 200 845
127.0.0.1 -  [7/Oct/2023:15:19:07 -0400] "GET /woodland/sample.js HTTP/1.1" 304 0

Event Handlers

Event Emitter syntax for the following events:

app.on("connect", (req, res) => res.header("x-custom-header", "abc-def"));

connect (req, res)

Executes after the connection has been decorated, but before the middleware executes.

error (req, res, err)

Executes after the response has been sent.

finish (req, res)

Executes after the response has been sent.

Helpers

req & res are decorated with helper functions to simplify responding.

res.error(status, body, headers)

Sends an error response.

res.header(key, value)

Shorthand of res.setHeader().

res.json(body, status = 200, headers)

Sends a JSON response.

res.last(req, res, next)

Last middleware of the route for the HTTP method as a way to "skip" to the middleware which sends a response.

res.redirect(uri, perm = false)

Sends a redirection response.

res.send(body, status = 200, headers = {})

Sends a response. Range header is ignored on stream responses.

res.set(headers = {})

Shorthand of res.setHeaders() which accepts Object, Map, or Headers instances.

res.status(arg)

Sets the response statusCode property.

Logging

Woodland defaults to Common Log Format but supports Common Log Format with Virtual Host, & NCSA extended/combined log format with an info level by default. You can change the stdout output by changing logging.format with valid placeholders.

You can disable woodland's logging by configuration with {logging: {enabled: false}}.

License

Copyright (c) 2023 Jason Mulligan

Licensed under the BSD-3 license.

18.2.4

4 months ago

18.2.3

4 months ago

18.2.2

4 months ago

18.2.1

4 months ago

18.2.0

5 months ago

18.1.7

7 months ago

18.1.6

7 months ago

18.1.5

7 months ago

18.1.4

7 months ago

18.1.2

7 months ago

18.1.1

7 months ago

18.1.0

7 months ago

18.0.9

7 months ago

18.0.8

7 months ago

18.0.7

7 months ago

18.0.6

7 months ago

18.0.5

7 months ago

18.0.4

7 months ago

18.0.3

7 months ago

18.0.2

7 months ago

18.0.1

7 months ago

18.0.0

7 months ago

18.0.10

7 months ago

18.0.11

7 months ago

18.0.12

7 months ago

18.0.13

7 months ago

18.0.14

7 months ago

18.0.15

7 months ago

18.0.16

7 months ago

17.0.2

2 years ago

17.0.1

2 years ago

17.0.0

2 years ago

16.1.3

3 years ago

16.1.2

3 years ago

16.1.1

3 years ago

16.1.0

3 years ago

16.0.8

3 years ago

16.0.7

3 years ago

16.0.6

3 years ago

16.0.5

3 years ago

16.0.4

3 years ago

16.0.3

3 years ago

16.0.2

3 years ago

16.0.1

3 years ago

16.0.0

3 years ago

15.1.12

3 years ago

15.1.11

3 years ago

15.1.10

3 years ago

15.1.9

3 years ago

15.1.8

3 years ago

15.1.7

3 years ago

15.1.6

3 years ago

15.1.5

3 years ago

15.1.4

3 years ago

15.1.3

3 years ago

15.1.2

3 years ago

15.1.1

3 years ago

15.0.9

3 years ago

15.0.11

3 years ago

15.0.10

3 years ago

15.1.0

3 years ago

15.0.8

3 years ago

15.0.7

3 years ago

15.0.6

3 years ago

15.0.5

3 years ago

15.0.4

3 years ago

15.0.3

3 years ago

15.0.2

3 years ago

15.0.1

3 years ago

15.0.0

3 years ago

14.0.0

3 years ago

13.1.4

3 years ago

13.1.3

3 years ago

13.1.2

3 years ago

13.1.1

3 years ago

13.1.0

3 years ago

13.0.3

3 years ago

12.0.0

3 years ago

13.0.2

3 years ago

13.0.0

3 years ago

13.0.1

3 years ago

12.2.1

3 years ago

12.2.0

3 years ago

12.1.0

3 years ago

11.1.2

3 years ago

11.1.1

3 years ago

11.0.1

3 years ago

11.1.0

3 years ago

10.0.0

3 years ago

11.0.0

3 years ago

9.1.8

4 years ago

9.1.6

4 years ago

9.1.5

4 years ago

9.1.4

5 years ago

9.1.3

5 years ago

9.2.0

5 years ago

9.1.2

5 years ago

9.1.1

5 years ago

9.1.0

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

8.0.3

5 years ago

8.0.2

5 years ago

8.0.1

5 years ago

8.0.0

5 years ago

7.7.0

5 years ago

7.6.6

5 years ago

7.6.5

5 years ago

7.6.4

5 years ago

7.6.3

5 years ago

7.6.2

5 years ago

7.6.1

5 years ago

7.6.0

5 years ago

7.5.1

5 years ago

7.5.0

5 years ago

7.4.2

5 years ago

7.4.1

5 years ago

7.4.0

5 years ago

7.3.40

5 years ago

7.3.39

5 years ago

7.3.38

5 years ago

7.3.37

5 years ago

7.3.36

5 years ago

7.3.35

5 years ago

7.3.34

5 years ago

7.3.33

5 years ago

7.3.32

5 years ago

7.3.31

5 years ago

7.3.30

5 years ago

7.3.29

5 years ago

7.3.28

5 years ago

7.3.27

5 years ago

7.3.26

5 years ago

7.3.25

5 years ago

7.3.24

5 years ago

7.3.23

5 years ago

7.3.22

5 years ago

7.3.21

5 years ago

7.3.20

5 years ago

7.3.19

5 years ago

7.3.18

5 years ago

7.3.17

5 years ago

7.3.16

5 years ago

7.3.15

5 years ago

7.3.14

5 years ago

7.3.13

5 years ago

7.3.12

5 years ago

7.3.11

5 years ago

7.3.10

5 years ago

7.3.9

5 years ago

7.3.8

5 years ago

7.3.7

5 years ago

7.3.6

5 years ago

7.3.5

5 years ago

7.3.4

5 years ago

7.3.3

5 years ago

7.3.2

5 years ago

7.3.1

6 years ago

7.3.0

6 years ago

7.2.12

6 years ago

7.2.11

6 years ago

7.2.10

6 years ago

7.2.9

6 years ago

7.2.8

6 years ago

7.2.7

6 years ago

7.2.6

6 years ago

7.2.5

6 years ago

7.2.4

6 years ago

7.2.3

6 years ago

7.2.2

6 years ago

7.2.1

6 years ago

7.2.0

6 years ago

7.1.13

6 years ago

7.1.12

6 years ago

7.1.11

6 years ago

7.1.9

6 years ago

7.1.8

6 years ago

7.1.7

6 years ago

7.1.6

6 years ago

7.1.5

6 years ago

7.1.4

6 years ago

7.1.3

6 years ago

7.1.2

6 years ago

7.1.1

6 years ago

7.1.0

6 years ago

7.0.4

6 years ago

7.0.3

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

5.2.2

6 years ago

5.2.1

6 years ago

5.2.0

6 years ago

5.1.8

6 years ago

5.1.7

6 years ago

6.1.2

6 years ago

6.1.1

6 years ago

6.1.0

6 years ago

6.0.4

6 years ago

6.0.3

6 years ago

6.0.2

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.1.6

6 years ago

5.1.5

6 years ago

5.1.4

6 years ago

5.1.3

6 years ago

5.1.2

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.21

6 years ago

5.0.20

6 years ago

5.0.19

6 years ago

5.0.18

6 years ago

5.0.17

6 years ago

5.0.16

6 years ago

5.0.15

6 years ago

5.0.14

6 years ago

5.0.13

6 years ago

5.0.12

6 years ago

5.0.11

6 years ago

5.0.10

6 years ago

5.0.9

6 years ago

5.0.8

6 years ago

5.0.7

6 years ago

5.0.6

6 years ago

5.0.5

6 years ago

5.0.4

6 years ago

5.0.3

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.3.11

6 years ago

4.3.10

6 years ago

4.3.9

6 years ago

4.3.8

6 years ago

4.3.7

6 years ago

4.3.6

6 years ago

4.3.5

6 years ago

4.3.4

6 years ago

4.3.3

6 years ago

4.3.2

6 years ago

4.3.1

6 years ago

4.3.0

6 years ago

4.2.5

6 years ago

4.2.4

6 years ago

4.2.3

6 years ago

4.2.2

6 years ago

4.2.1

6 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.6

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.4

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.3.8

7 years ago

1.3.7

7 years ago

1.3.6

7 years ago

1.3.5

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.9

7 years ago

1.2.8

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.22

7 years ago

1.1.21

7 years ago

1.1.20

7 years ago

1.1.19

7 years ago

1.1.18

7 years ago

1.1.17

7 years ago

1.1.16

7 years ago

1.1.15

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.17

8 years ago

1.0.16

8 years ago

1.0.15

8 years ago

1.0.14

8 years ago

1.0.13

8 years ago

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago