5.1.0 • Published 2 months ago

@podium/context v5.1.0

Weekly downloads
300
License
MIT
Repository
github
Last release
2 months ago

Podium Context v5

Module to generate the context which is passed on requests from a Podium Layout server to a Podium Podlet server.

Dependencies GitHub Actions status Known Vulnerabilities

This module is intended for internal use in Podium and is not a module an end user would use directly. End users will typically interact with this module through higher level modules such as the @podium/layout module.

Installation

$ npm install @podium/context

Example

Generate a context which can be passed on to an http request to a Podlet:

import { HttpIncoming } from '@podium/utils';
import Context from '@podium/context';
import http from 'http';

// Set up a context with the name 'myLayout'
const context = new Context({ name: 'myLayout' });

const server = http.createServer(async (req, res) => {
    // Create a HttpIncoming object
    const incoming = new HttpIncoming(req, res);

    // Run context parsers on the request
    const incom = await context.process(incoming);

    // Serialize the context into an object that can be
    // passed on as HTTP headers on each HTTP request
    const headers = Context.serialize({}, incom.context);

    [ ... snip ...]
});

server.listen(8080);

Description

The Podium Context is used to provide key information from a Layout server to a Podlet server. This is done by a defined set of HTTP headers which is applied to all requests from a Layout server to a Podlet server.

This module handles generating wanted key information in the Layout server and seralizing it into HTTP headers which are passed on to requests to the Podlet servers where the HTTP headers are once again parsed back into a key / value object with the key information.

There are three parts in this module:

  • Parsers
  • Middleware to run parsers
  • Serializing / deserializing

Each part works as follow:

Parsers

Parsers operate on inbound requests to a layout server. Each parser is handed an HttpIncoming object for each request. Upon execution a parser builds a value which will be applied as part of the context and then appended to all requests made to podlet servers.

This module comes with a set of built in parsers which will always be applied.

It's also possible to write custom parsers and append them to the process of constructing the context.

Processing

There is a .process() method which takes an HttpIncoming object and then runs it through all registered parsers in parallel.

The result of each parser is stored in an object which is set on the .context property of the HttpIncoming object. This object is "HTTP header like" and can be serialized into headers on an HTTP request to a Podlet.

Serializing / deserializing

These are static methods used to serialize and deserialize the "HTTP header like" object from HttpIncoming.context into HTTP headers on the HTTP request to a Podlet and then back into a object on HttpIncoming.context in the Podlet server.

Constructor

Creates a new Podium context instance.

import Context from '@podium/context';
const context = new Context({ name: 'myName' });

The constructor takes the following arguments:

options

optiondefaulttyperequireddetails
namenullstringtrue
debugnullobjectfalseSee parser options
localenullobjectfalseSee parser options
deviceTypenullobjectfalseSee parser options
mountOriginnullobjectfalseSee parser options
mountPathnamenullobjectfalseSee parser options
publicPathnamenullobjectfalseSee parser options

name

A name as a String to identify the instance. This should be a logical and human readable name related to the Layout this instance is appended too. This name is passed on to the Podlet servers as part of the Requested By context.

The name value must be in camelCase.

Example

const context = new Context({
    name: 'myLayout';
});

debug

Config object passed on to the debug parser. See the parser docs.

locale

Config object passed on to the locale parser. See the parser docs.

deviceType

Config object passed on to the device type parser. See the parser docs.

mountOrigin

Config object passed on to the mount origin parser. See the parser docs.

mountPathname

Config object passed on to the mount pathname parser. See the parser docs.

publicPathname

Config object passed on to the public pathname parser. See the parser docs.

API

The Context instance has the following API:

.register(name, parser)

Register a Parser for a value that should be appended to the Context.

This method takes the following arguments:

optiondefaulttyperequireddetails
namenullstringtrueUnique name of the parser. Used as the key for the parser's value in the context
parsernullobjecttrueThe Parser to be registered

Example:

import { HttpIncoming } from '@podium/utils';
import Context from '@podium/context';
import Parser from 'my-custom-parser';
import http from 'http';

// Set up a context and register the custom parser
const context = new Context({ name: 'myLayout' });
context.register('myStuff', new Parser('someConfig'));

const server = http.createServer(async (req, res) => {
    const incoming = new HttpIncoming(req, res);
    const incom = await context.process(incoming);
    // incom.context will now hold the following object:
    // {
    //     'podium-debug': 'false',
    //     'podium-locale': 'no-NO'
    //     'podium-my-stuff': 'value from custom parser'
    // }
});

server.listen(8080);

.process(HttpIncoming)

Metod for processing a incoming http request. It runs all parsers in parallel and append the result of each parser to HttpIncoming.context.

This will execute all built in parsers as well as all externally registered (through the .register() method) parsers.

Returns a Promise which will resolve with the passed in HttpIncoming object.

HttpIncoming (required)

An instance of a HttpIncoming class.

Static API

The Context constructor has the following static API:

.serialize(headers, context, podletName)

Takes an "HTTP header like" object produced by .process() (the HttpIncoming.context object) and serializes it into an HTTP header object which can be applied to HTTP requests sent to podlets.

The object stored at HttpIncoming.context is "HTTP header-ish" because the value of each key can be either a String or a Function. If a key holds a Function the serializer will call the function with the podletName argument.

The method takes the following arguments:

optiondefaulttyperequireddetails
headersnullobjecttrueAn existing HTTP header object or empty object the context should be merged into
contextnullobjecttrueThe object produced by .middleware() and stored at res.locals.podium.context
podletNamenullstringfalseThe name of the podlet the context should be applied to

Example: layout sends context with a request to a podlet

const server = http.createServer(async (req, res) => {
    const incoming = new HttpIncoming(req, res);
    const incom = await context.process(incoming);

    const headers = Context.serialize({}, incom.context, 'somePodlet');
    request({
        headers: headers,
        method: 'GET',
        url: 'http://some.podlet.finn.no/',
    }).pipe(res);
});

.deserialize()

Connect compatible middleware which will parse HTTP headers on inbound requests and turn Podium context headers into a context object stored at res.locals.podium.context.

Example: podlet receives request from a layout server

app.use(Context.deserialize());

app.get('/', (req, res) => {
    res.status(200).json(res.locals.podium.context);
});

Internal parsers

This module comes with a set of default parsers which will be applied when .process() is run.

Each of these parsers can be configured through the constructor's options object by passing an options object for the specific parser (see constructor options).

Example of passing options to the built in debug parser:

import Context from '@podium/context';
const context = new Context({
    name: 'myName',
    debug: {
        enabled: true,
    },
});

The following parsers are applied by default:

Requested By

Context header: podium-requested-by

Each layout must have a given name to make it more easily human identifiable. This name value is then passed on from the layout to any podlets in the podium-requested-by context header which is generated by running this parser.

arguments (required)

The parser takes a string (required) as the first argument to the constructor.

Example:

import Context from '@podium/context';
const context = new Context({
    name: 'myName',
});

Debug

Context header: podium-debug

Indicates to podlets when the layout server is in debug mode.

arguments (optional)

This parser takes an optional config object with the following properties:

optiondefaulttyperequireddetails
enablefalsebooleanfalseIndicates whether layout is in debug mode or not

This config object is passed on to the debug argument on the context object constructor.

Locale

Context header: podium-locale

Locale of the requesting browser. When executed by .process(), this parser will look for a locale property at HttpIncoming.params.locale. If found, this value will be used. If not found, the default locale will be used.

const context = new Context({
    name: 'myName',
});
const app = express();

app.get(await (req, res) => {
    const incoming = new HttpIncoming(req, res, {
        locale: 'nb-NO',
    });
    const incom = await context.process(incoming);

    [ ... snip ...]
});

arguments (optional)

This parser takes an optional config object with the following properties:

optiondefaulttyperequireddetails
localeen-USstringfalseA bcp47 compliant locale String

This config object is passed on to the locale property on the config object in the constructor.

Device Type

Context header: podium-device-type

A guess at the device type of the requesting browser. Guessing is done by UA detection and is not guaranteed to be accurate.

The output value will be one of the following strings:

  • desktop: The device requesting the podlet is probably a desktop computer or something with a large screen. This is the default if we're not able to determine anything more detailed.
  • tablet: The device is probably a tablet of some sort, or a device with a smaller screen than a desktop.
  • mobile: The device is probably a phone of some sort, or a device with a smaller screen than a tablet.

This module will internally cache its result and the matching UA string in an LRU cache for faster lookup.

arguments (optional)

This parser takes an optional config object with the following properties:

optiondefaulttyperequireddetails
cacheSize10000numberfalseNumber of UA Strings to keep in the LRU cache

This config object is passed on to the deviceType property on the config object in the constructor.

Mount Origin

Context header: podium-mount-origin

URL origin of the inbound request to the layout server. The parser will try to parse this value from inbound requests to the layout server. It is also possible to override the value using config.

The value is a WHATWG URL compatible origin (illustrated overview).

arguments (optional)

This parser takes an optional config object with the following properties:

optiondefaulttyperequireddetails
originnullstringfalseOrigin string that, if present, will override the origin found on request

Example:

import Context from '@podium/context';
const context = new Context({
    name: 'myName',
    mountOrigin: {
        origin: 'https://example.org/',
    },
});

Mount Pathname

Context header: podium-mount-pathname

URL pathname specifying where a layout is mounted in an HTTP server.

The value is a WHATWG URL compatible pathname (illustrated overview).

arguments (optional)

This parser takes an optional config object with the following properties:

optiondefaulttyperequireddetails
pathname'/'stringfalsePathname specifying where a Layout is mounted in an HTTP server

Example:

import Context from '@podium/context';
const context = new Context({
    name: 'myName',
    mountPathname: {
        pathname: '/my/path/name',
    },
});

Public Pathname

Context header: podium-public-pathname

URL pathname indicating where a layout server has mounted a proxy to proxy public traffic to podlets.

The full public pathname is built up joining pathname and prefix where pathname is the pathname to where the proxy is mounted into the HTTP server and prefix is a namespace isolating the proxy from other routes defined under the same pathname.

Often pathname will be the same value as mount pathname.

The value is a WHATWG URL compatible pathname (illustrated overview).

arguments (optional)

This parser takes an optional config object with the following properties:

optiondefaulttyperequireddetails
pathname'/'stringfalsePathname where a Proxy is mounted in a HTTP server
prefix'podium-resource'stringfalseNamespace used to isolate the proxy from other routes under the same pathname

Example:

import Context from '@podium/context';
const context = new Context({
    name: 'myName',
    publicPathname: {
        pathname: '/my/custom/proxy',
        prefix: 'proxy',
    },
});

License

Copyright (c) 2019 FINN.no

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

5.1.0

2 months ago

5.0.29

2 months ago

5.0.28

2 months ago

5.0.27

3 months ago

5.0.20

5 months ago

5.0.21

5 months ago

5.0.22

5 months ago

5.0.23

5 months ago

5.0.24

4 months ago

5.0.25

4 months ago

5.0.26

3 months ago

5.0.15

6 months ago

5.0.16

6 months ago

5.0.17

6 months ago

5.0.18

5 months ago

5.0.19

5 months ago

5.0.14

6 months ago

5.0.13

6 months ago

5.0.10

6 months ago

5.0.11

6 months ago

5.0.12

6 months ago

5.0.9

7 months ago

5.0.8

7 months ago

5.0.7

7 months ago

5.0.6

7 months ago

5.0.5

7 months ago

5.0.4

8 months ago

5.1.0-beta.1

8 months ago

5.0.3

10 months ago

5.0.2

10 months ago

5.0.1

12 months ago

5.0.0

12 months ago

5.0.0-next.7

12 months ago

4.1.76

1 year ago

4.1.77

1 year ago

4.1.78

1 year ago

4.1.79

1 year ago

4.1.80

1 year ago

4.1.81

1 year ago

4.1.75

2 years ago

5.0.0-next.6

3 years ago

4.1.74

3 years ago

4.1.70

3 years ago

4.1.71

3 years ago

4.1.72

3 years ago

4.1.73

3 years ago

4.1.69

3 years ago

4.1.63

3 years ago

4.1.64

3 years ago

4.1.65

3 years ago

4.1.66

3 years ago

4.1.60

3 years ago

4.1.61

3 years ago

4.1.62

3 years ago

4.1.67

3 years ago

4.1.68

3 years ago

4.1.59

3 years ago

4.1.57

3 years ago

4.1.58

3 years ago

4.1.55

3 years ago

4.1.56

3 years ago

4.1.53

3 years ago

4.1.54

3 years ago

4.1.52

3 years ago

4.1.50

3 years ago

4.1.51

3 years ago

5.0.0-next.5

4 years ago

5.0.0-next.4

4 years ago

4.1.41

4 years ago

4.1.42

4 years ago

4.1.43

4 years ago

4.1.44

4 years ago

4.1.40

4 years ago

4.1.49

4 years ago

4.1.45

4 years ago

4.1.46

4 years ago

4.1.47

4 years ago

4.1.48

4 years ago

4.1.38

4 years ago

4.1.39

4 years ago

4.1.35

4 years ago

4.1.36

4 years ago

4.1.37

4 years ago

4.1.33

4 years ago

4.1.34

4 years ago

4.1.32

4 years ago

4.1.31

4 years ago

4.1.30

4 years ago

4.1.29

4 years ago

4.1.27

4 years ago

4.1.28

4 years ago

4.1.25

4 years ago

4.1.26

4 years ago

4.1.22

4 years ago

4.1.23

4 years ago

4.1.24

4 years ago

4.1.21

4 years ago

4.1.20

4 years ago

4.1.19

4 years ago

4.1.18

4 years ago

4.1.17

4 years ago

4.1.16

4 years ago

4.1.15

4 years ago

4.1.13

4 years ago

4.1.14

4 years ago

4.1.12

4 years ago

4.1.11

4 years ago

4.1.9

4 years ago

4.1.10

4 years ago

4.1.8

4 years ago

4.1.7

4 years ago

5.0.0-next.3

4 years ago

5.0.0-next.2

4 years ago

4.1.6

5 years ago

4.1.5

5 years ago

4.1.4

5 years ago

4.1.3

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.0-next.2

6 years ago

4.0.0-next

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

3.0.0-beta.6

6 years ago

3.0.0-beta.5

6 years ago

3.0.0-beta.4

6 years ago

3.0.0-beta.3

6 years ago

3.0.0-beta.2

6 years ago

3.0.0-beta.1

6 years ago