### Context Mount Origin Example Source: https://github.com/podium-lib/context/blob/main/README.md Example of initializing the Context parser with a specific mount origin configuration. ```js import Context from '@podium/context'; const context = new Context({ name: 'myName', mountOrigin: { origin: 'https://example.org/', }, }); ``` -------------------------------- ### Context Mount Pathname Example Source: https://github.com/podium-lib/context/blob/main/README.md Example of initializing the Context parser with a specific mount pathname configuration. ```js import Context from '@podium/context'; const context = new Context({ name: 'myName', mountPathname: { pathname: '/my/path/name', }, }); ``` -------------------------------- ### Install @podium/context Source: https://github.com/podium-lib/context/blob/main/README.md Installs the Podium Context module using npm. This is the standard method for adding the library to a Node.js project. ```bash $ npm install @podium/context ``` -------------------------------- ### Configure Internal Parsers Source: https://github.com/podium-lib/context/blob/main/README.md Demonstrates how to pass configuration options to the built-in parsers when initializing the Context. This example shows enabling the 'debug' parser with specific options. ```javascript import Context from '@podium/context'; const context = new Context({ name: 'myName', debug: { enabled: true, }, }); ``` -------------------------------- ### Podium Context API Reference Source: https://github.com/podium-lib/context/blob/main/README.md Comprehensive documentation for the Podium Context API, detailing methods for managing context data, processing requests, and handling serialization/deserialization. This includes method signatures, parameter descriptions, return values, and usage examples. ```APIDOC Context: __constructor(options) options: Object name: string (required) - Name of the layout or service. [debug]: Object - Configuration for the debug parser. enabled: boolean - Enable/disable debug output (default: false). [locale]: Object - Configuration for the locale parser. default: string - Default locale if none is found (default: 'en-US'). [publicPathname]: Object - Configuration for the public pathname parser. [pathname]: string - The public pathname (default: '/'). .register(name, parser) Registers a Parser for a value that should be appended to the Context. Parameters: name: string (required) - Unique name of the parser. Used as the key for the parser's value in the context. parser: object (required) - The Parser to be registered. .process(HttpIncoming) Processes an incoming HTTP request, running all registered parsers in parallel and appending their results to HttpIncoming.context. Parameters: HttpIncoming: object (required) - An instance of a HttpIncoming class. Returns: Promise - A Promise which will resolve with the passed in HttpIncoming object. .serialize(headers, context, podletName) Serializes an "HTTP header like" object (HttpIncoming.context) into an HTTP header object that can be applied to HTTP requests sent to podlets. Parameters: headers: object (required) - An existing HTTP header object or empty object the context should be merged into. context: object (required) - The object produced by .middleware() and stored at res.locals.podium.context. podletName: string (optional) - The name of the podlet the context should be applied to. If a key holds a Function, the serializer will call the function with this argument. Returns: object - An HTTP header object. .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. Returns: function - Middleware function. Static API: Context.serialize(headers, context, podletName) (See instance method .serialize() for details) Context.deserialize() (See instance method .deserialize() for details) Internal Parsers: Requested By: Context header: podium-requested-by Description: 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. ``` -------------------------------- ### Serialize Context for Podlet Request Source: https://github.com/podium-lib/context/blob/main/README.md Illustrates how to use the static `Context.serialize` method to convert context data into HTTP headers suitable for requests to Podlets. It shows an example of a layout sending context with a request to a podlet. ```javascript 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); }); ``` -------------------------------- ### Register Parser with Podium Context Source: https://github.com/podium-lib/context/blob/main/README.md Demonstrates how to register a custom parser with the Podium Context instance. This allows custom data to be appended to the context during request processing. It shows the setup of a Context, registration of a custom parser, and processing an incoming HTTP request. ```javascript 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); ``` -------------------------------- ### Generate and Process Context in Layout Server Source: https://github.com/podium-lib/context/blob/main/README.md Demonstrates how to set up a Podium Context instance, process an incoming HTTP request to extract context information, and serialize that context into HTTP headers for downstream requests to Podlets. ```javascript 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); ``` -------------------------------- ### Context Constructor Source: https://github.com/podium-lib/context/blob/main/README.md Initializes the Context parser. The constructor requires a configuration object, including a 'name' property. ```js import Context from '@podium/context'; const context = new Context({ name: 'myName', }); ``` -------------------------------- ### Context Constructor and Options Source: https://github.com/podium-lib/context/blob/main/README.md Details the constructor for the Podium Context class and its available options. The 'name' option is mandatory for identifying the layout instance. ```APIDOC Context(options) Creates a new Podium context instance. Parameters: options (object): Configuration object for the context. name (string): REQUIRED. A name to identify the instance, e.g., 'myLayout'. Must be in camelCase. debug (object): Optional. Configuration for the debug parser. locale (object): Optional. Configuration for the locale parser. deviceType (object): Optional. Configuration for the device type parser. mountOrigin (object): Optional. Configuration for the mount origin parser. mountPathname (object): Optional. Configuration for the mount pathname parser. publicPathname (object): Optional. Configuration for the public pathname parser. Example: ```javascript import Context from '@podium/context'; const context = new Context({ name: 'myName' }); ``` Option Details: name: A logical, human-readable name in camelCase for the Layout instance. Passed to Podlet servers as part of the 'Requested By' context. debug: Config object passed to the debug parser. locale: Config object passed to the locale parser. deviceType: Config object passed to the device type parser. mountOrigin: Config object passed to the mount origin parser. mountPathname: Config object passed to the mount pathname parser. publicPathname: Config object passed to the public pathname parser. ``` -------------------------------- ### Context Initialization with Public Pathname Source: https://github.com/podium-lib/context/blob/main/README.md Demonstrates how to initialize a Podium Context object with custom public pathname configuration. This includes setting the 'pathname' where the proxy is mounted and a 'prefix' for namespacing. ```javascript import Context from '@podium/context'; const context = new Context({ name: 'myName', publicPathname: { pathname: '/my/custom/proxy', prefix: 'proxy', }, }); ``` -------------------------------- ### Deserialize Context from Headers Source: https://github.com/podium-lib/context/blob/main/README.md Shows how to use the static `Context.deserialize` method as middleware to parse incoming HTTP headers and populate the context object. This is typically used in a Podlet receiving requests from a layout server. ```javascript app.use(Context.deserialize()); app.get('/', (req, res) => { res.status(200).json(res.locals.podium.context); }); ``` -------------------------------- ### Context Locale Configuration Source: https://github.com/podium-lib/context/blob/main/README.md Configures the locale setting for the Context parser. This parser looks for a locale property in HttpIncoming.params.locale or uses a default. An optional configuration object can be passed to the constructor. ```APIDOC Context Locale Configuration: Arguments (optional): locale: string (default: 'en-US') - A bcp47 compliant locale String. This config object is passed on to the `locale` property on the config object in the constructor. ``` -------------------------------- ### Context Device Type Configuration Source: https://github.com/podium-lib/context/blob/main/README.md Configures the device type detection for the Context parser. It guesses the device type based on UA detection and caches results. An optional configuration object can be passed to the constructor. ```APIDOC Context Device Type Configuration: Arguments (optional): cacheSize: number (default: 10000) - Number 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. Output Values: - 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. ``` -------------------------------- ### Context Mount Origin Configuration Source: https://github.com/podium-lib/context/blob/main/README.md Configures the mount origin for the Context parser. This specifies the URL origin of the inbound request to the layout server. The value can be overridden via configuration. ```APIDOC Context Mount Origin Configuration: Arguments (optional): origin: string (default: null) - Origin string that, if present, will override the origin found on request. The value is a WHATWG URL compatible origin. ``` -------------------------------- ### Context Debug Configuration Source: https://github.com/podium-lib/context/blob/main/README.md Configures the debug mode for the Context parser. This feature uses the 'podium-debug' header to indicate layout server debug status. An optional configuration object can be passed to the constructor. ```APIDOC Context Debug Configuration: Arguments (optional): enable: boolean (default: false) - Indicates whether layout is in debug mode or not. This config object is passed on to the `debug` argument on the context object constructor. ``` -------------------------------- ### Context Mount Pathname Configuration Source: https://github.com/podium-lib/context/blob/main/README.md Configures the mount pathname for the Context parser. This specifies the URL pathname where a layout is mounted in an HTTP server. The value can be overridden via configuration. ```APIDOC Context Mount Pathname Configuration: Arguments (optional): pathname: string (default: '/') - Pathname specifying where a Layout is mounted in an HTTP server. The value is a WHATWG URL compatible pathname. ``` -------------------------------- ### Process Incoming Request with Locale Source: https://github.com/podium-lib/context/blob/main/README.md Demonstrates processing an incoming request with a specific locale. The Context parser can utilize a locale provided in the request parameters. ```js 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 ...] }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.