this is the commonly shared codebase for all grundstein servers.
npm i --save-exact @grundstein/commons
Below are all imports possible from this library.
TODO: Add usage examples.
import commons, { fs, is, lib, log, middleware } from '@grundstein/commons'
import lib from '@grundstein/commons/lib.mjs'
import middleware from '@grundstein/commons/middleware.mjs'
commons.log.info('commons are usable now.')
exports @magic/fs, a promisified version of fs with some added functionality.
exports @magic/types, a comprehensive runtime type checking library.
serverside logging functionality, basically a fancy wrapper around console.log that outputs a json object. those logs get collected by systemd and can be viewed and managed using journalctl.
various utility functions
Looks for a directory with https certificates, and starts a nodejs https or http server depending on the existence of said certificates.
import { lib, log } from '@grundstein/commons'
const listener = lib.createServer({
// this directory has to be readable by the executing user (in prod, this is "grundstein")
certDir: '/absolute/dir/to/certs',
host: '1.2.3.4',
port: '3000',
startTime: log.hrtime(),
})
used by lib.createServer to get the https context set up.
tests if req.url is empty, includes ://, or does not start with '/', calls req.socket.destroy if malicious request is detected.
adds req.startTime field.
used to check etag values against a cache of files to use 304 headers for cached content. cache is either built on-demand or using the pre-built etags.csv file that @grundstein/prepare-static-files generates.
returns the ip address of a request. by default, this function will replace the last block of the ip address by xxx.
get a formatted date and time string
returns 'br', 'gzip', 'deflate', 'buffer' in that order, depending on contents of req.headers['accept-encoding']
returns the time elapsed between two hrtime values, as a string, normalized to seconds, milliseconds or nanoseconds, and ending with the unit.
reads config.proxyFile using fs, splits it at newlines and returns the resulting array
checks if file is truthy and has a file.buffer key.
collects data from request, response and headers, then returns a one-line json string that can be console.log(ged).
prepares the headers and body of a response, then sends it to the client and logs it.
sets content-type and Content-Encoding headers, checks if compressed file exists and accept-encoding accepts it, then sends the correct content with correct headers using lib.respond
sends a file as a stream.
first release
- add lib.etags
- lib.createServer now wraps the handler function to use denyRequest
- update dependencies
- getClientIp is much more sophisticated, looking for the ip in multiple headers and request subfields.
update dependencies
make sure that lib/createSecureContext does not try to find keys in the root keystore, only in subdirectories
- update dependencies
- lib/createServer uses localhost as default host
update dependencies
update dependencies
update dependencies
update dependencies
update dependencies
- update dependencies
- replace http and https with http2
...