@podium/context
Advanced tools
Comparing version 5.0.9 to 5.0.10
@@ -0,1 +1,8 @@ | ||
## [5.0.10](https://github.com/podium-lib/context/compare/v5.0.9...v5.0.10) (2024-05-13) | ||
### Bug Fixes | ||
* generate types from JSDoc ([#240](https://github.com/podium-lib/context/issues/240)) ([015537a](https://github.com/podium-lib/context/commit/015537a6ef6963ffae7d3a68222889fc62ba5cb9)) | ||
## [5.0.9](https://github.com/podium-lib/context/compare/v5.0.8...v5.0.9) (2024-05-07) | ||
@@ -2,0 +9,0 @@ |
@@ -17,20 +17,69 @@ import decamelize from 'decamelize'; | ||
/** | ||
* Options passed on to the built-in context parsers. | ||
* | ||
* @typedef {object} PodiumContextOptions | ||
* @property {string} name | ||
* @property {object} [logger] | ||
* @property {import('./get-public-pathname.js').PodiumContextPublicPathnameParserOptions} [publicPathname={}] | ||
* @property {import('./get-mount-pathname.js').PodiumContextMountPathnameParserOptions} [mountPathname={}] | ||
* @property {import('./get-mount-origin.js').PodiumContextMountOriginParserOptions} [mountOrigin={}] | ||
* @property {import('./get-device-type.js').PodiumContextDeviceTypeParserOptions} [deviceType={}] | ||
* @property {import('./get-locale.js').PodiumContextLocaleParserOptions} [locale={}] | ||
* @property {import('./get-debug.js').PodiumContextDebugParserOptions} [debug={}] | ||
*/ | ||
/** | ||
* @typedef {object} PodiumContextParser | ||
* @property {(incoming?: import('@podium/utils').PodiumHttpIncoming) => string | ((name: string) => string) | Promise<string> | ((name: string) => Promise<string>)} parse | ||
*/ | ||
export default class PodiumContext { | ||
constructor({ | ||
name = undefined, | ||
logger = undefined, | ||
publicPathname = {}, | ||
mountPathname = {}, | ||
mountOrigin = {}, | ||
deviceType = {}, | ||
locale = {}, | ||
debug = {}, | ||
} = {}) { | ||
if (schemas.name(name).error) | ||
/** | ||
* @readonly | ||
* @type {import('abslog').ValidLogger} | ||
*/ | ||
log; | ||
/** | ||
* @readonly | ||
* @type {import('@metrics/client')} | ||
*/ | ||
metrics; | ||
/** | ||
* @readonly | ||
* @type {import('@metrics/client').MetricsHistogram} | ||
*/ | ||
histogram; | ||
/** | ||
* @readonly | ||
* @type {Map<string, PodiumContextParser>} | ||
*/ | ||
parsers; | ||
/** | ||
* @constructor | ||
* @param {PodiumContextOptions} options | ||
*/ | ||
constructor( | ||
options = { | ||
name: undefined, | ||
logger: undefined, | ||
publicPathname: {}, | ||
mountPathname: {}, | ||
mountOrigin: {}, | ||
deviceType: {}, | ||
locale: {}, | ||
debug: {}, | ||
}, | ||
) { | ||
if (schemas.name(options.name).error) | ||
throw new Error( | ||
`The value, "${name}", for the required argument "name" on the Context constructor is not defined or not valid.`, | ||
`The value, "${options.name}", for the required argument "name" on the Context constructor is not defined or not valid.`, | ||
); | ||
Object.defineProperty(this, 'log', { | ||
value: abslog(logger), | ||
value: abslog(options.logger), | ||
}); | ||
@@ -61,9 +110,15 @@ | ||
this.register('publicPathname', new PublicPathname(publicPathname)); | ||
this.register('mountPathname', new MountPathname(mountPathname)); | ||
this.register('mountOrigin', new MountOrigin(mountOrigin)); | ||
this.register('requestedBy', new RequestedBy({ name })); | ||
this.register('deviceType', new DeviceType(deviceType)); | ||
this.register('locale', new Locale(locale)); | ||
this.register('debug', new Debug(debug)); | ||
this.register( | ||
'publicPathname', | ||
new PublicPathname(options.publicPathname), | ||
); | ||
this.register( | ||
'mountPathname', | ||
new MountPathname(options.mountPathname), | ||
); | ||
this.register('mountOrigin', new MountOrigin(options.mountOrigin)); | ||
this.register('requestedBy', new RequestedBy({ name: options.name })); | ||
this.register('deviceType', new DeviceType(options.deviceType)); | ||
this.register('locale', new Locale(options.locale)); | ||
this.register('debug', new Debug(options.debug)); | ||
} | ||
@@ -75,2 +130,8 @@ | ||
/** | ||
* Register a context parser that will run on each incoming request. | ||
* | ||
* @param {string} name | ||
* @param {PodiumContextParser} parser | ||
*/ | ||
register(name, parser) { | ||
@@ -96,2 +157,8 @@ assert(name, 'You must provide a value to "name".'); | ||
/** | ||
* Process an incoming request. This will run all registered context parsers. | ||
* | ||
* @param {import('@podium/utils').HttpIncoming} incoming | ||
* @returns {Promise<import('@podium/utils').HttpIncoming>} enriched with the result of each context parser | ||
*/ | ||
process(incoming) { | ||
@@ -123,2 +190,8 @@ if ( | ||
/** | ||
* @param {object} [headers] | ||
* @param {object} [context] | ||
* @param {unknown} [name] | ||
* @returns {object} | ||
*/ | ||
static serialize(headers, context, name) { | ||
@@ -128,2 +201,5 @@ return utils.serializeContext(headers, context, name); | ||
/** | ||
* @returns {(req: { headers: Record<string, string | string[]>}, res: object, next: () => void) => void} | ||
*/ | ||
static deserialize() { | ||
@@ -136,2 +212,2 @@ return (req, res, next) => { | ||
} | ||
}; | ||
} |
import assert from 'assert'; | ||
/** | ||
* @typedef {object} PodiumContextDebugParserOptions | ||
* @property {boolean} [enabled=false] | ||
*/ | ||
export default class PodiumContextDebugParser { | ||
/** | ||
* @constructor | ||
* @param {PodiumContextDebugParserOptions} [options] | ||
*/ | ||
constructor({ enabled = false } = {}) { | ||
@@ -21,4 +30,5 @@ assert.strictEqual( | ||
parse() { | ||
// @ts-expect-error because of Object.defineProperty | ||
return this.default; | ||
} | ||
}; | ||
} |
@@ -6,3 +6,18 @@ import Bowser from 'bowser'; | ||
/** | ||
* @typedef {object} PodiumContextDeviceTypeParserOptions | ||
* @property {number} [cacheSize=10000] | ||
*/ | ||
export default class PodiumContextDeviceTypeParser { | ||
/** | ||
* @readonly | ||
* @type {import('lru-cache').LRUCache} | ||
*/ | ||
cache; | ||
/** | ||
* @constructor | ||
* @param {PodiumContextDeviceTypeParserOptions} [options] | ||
*/ | ||
constructor({ cacheSize = 10000 } = {}) { | ||
@@ -19,2 +34,3 @@ Object.defineProperty(this, 'default', { | ||
value: new LRUCache({ | ||
// @ts-expect-error because of Object.defineProperty | ||
max: this.cacheSize, | ||
@@ -36,2 +52,3 @@ }), | ||
} | ||
// @ts-expect-error because of Object.defineProperty | ||
return this.default; | ||
@@ -46,2 +63,3 @@ } | ||
if (!userAgent || userAgent === '') { | ||
// @ts-expect-error because of Object.defineProperty | ||
return this.default; | ||
@@ -69,2 +87,2 @@ } | ||
} | ||
}; | ||
} |
import assert from 'assert'; | ||
import bcp47 from 'bcp47-validate'; | ||
/** | ||
* @typedef {object} PodiumContextLocaleParserOptions | ||
* @property {string} [locale='en-US'] | ||
*/ | ||
export default class PodiumContextLocaleParser { | ||
/** | ||
* @constructor | ||
* @param {PodiumContextLocaleParserOptions} options | ||
*/ | ||
constructor({ locale = 'en-US' } = {}) { | ||
@@ -26,4 +35,5 @@ assert.strictEqual( | ||
// @ts-expect-error because of Object.defineProperty | ||
return this.locale; | ||
} | ||
}; | ||
} |
import { URL } from 'url'; | ||
/** | ||
* @typedef {object} PodiumContextMountOriginParserOptions | ||
* @property {string | null} [origin=null] | ||
*/ | ||
export default class PodiumContextMountOriginParser { | ||
/** | ||
* @constructor | ||
* @param {PodiumContextMountOriginParserOptions} options | ||
*/ | ||
constructor({ origin = null } = {}) { | ||
@@ -18,3 +27,5 @@ Object.defineProperty(this, 'defaultOrigin', { | ||
// @ts-expect-error because of Object.defineProperty | ||
if (this.defaultOrigin.hostname) { | ||
// @ts-expect-error because of Object.defineProperty | ||
({ hostname, protocol, port } = this.defaultOrigin); | ||
@@ -33,2 +44,2 @@ } | ||
} | ||
}; | ||
} |
import { pathnameBuilder } from '@podium/utils'; | ||
/** | ||
* @typedef {object} PodiumContextMountPathnameParserOptions | ||
* @property {string} [pathname='/'] | ||
*/ | ||
export default class PodiumContextMountPathnameParser { | ||
/** | ||
* @constructor | ||
* @param {PodiumContextMountPathnameParserOptions} options | ||
*/ | ||
constructor({ pathname = '/' } = {}) { | ||
@@ -15,4 +24,5 @@ Object.defineProperty(this, 'pathname', { | ||
parse() { | ||
// @ts-expect-error because of Object.defineProperty | ||
return pathnameBuilder(this.pathname); | ||
} | ||
}; | ||
} |
import { pathnameBuilder } from '@podium/utils'; | ||
/** | ||
* @typedef {object} PodiumContextPublicPathnameParserOptions | ||
* @property {string} [pathname='/'] | ||
* @property {string} [prefix='podium-resource'] | ||
*/ | ||
export default class PodiumContextPublicPathnameParser { | ||
/** | ||
* @constructor | ||
* @param {PodiumContextPublicPathnameParserOptions} options | ||
*/ | ||
constructor({ pathname = '/', prefix = 'podium-resource' } = {}) { | ||
@@ -20,4 +30,5 @@ Object.defineProperty(this, 'pathname', { | ||
return (name = '/') => | ||
// @ts-expect-error because of Object.defineProperty | ||
pathnameBuilder(this.pathname, this.prefix, name); | ||
} | ||
}; | ||
} |
import assert from 'assert'; | ||
/** | ||
* @typedef {object} PodiumContextRequestedByParserOptions | ||
* @property {string} name | ||
*/ | ||
export default class PodiumContextRequestedByParser { | ||
/** | ||
* @constructor | ||
* @param {PodiumContextRequestedByParserOptions} options | ||
*/ | ||
// @ts-expect-error | ||
constructor({ name } = {}) { | ||
@@ -17,4 +27,5 @@ assert(name, 'You must provide a value to "name".'); | ||
parse() { | ||
// @ts-expect-error because of Object.defineProperty | ||
return this.name; | ||
} | ||
}; | ||
} |
{ | ||
"name": "@podium/context", | ||
"version": "5.0.9", | ||
"version": "5.0.10", | ||
"description": "Module to generate the context which is passed on requests from a Podium Layout server to a Podium Podlet server", | ||
@@ -27,16 +27,19 @@ "type": "module", | ||
"dist", | ||
"lib" | ||
"lib", | ||
"types" | ||
], | ||
"main": "./lib/context.js", | ||
"types": "./types/context.d.ts", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test": "tap --disable-coverage --allow-empty-coverage" | ||
"test": "tap --disable-coverage --allow-empty-coverage && tsc --project tsconfig.test.json", | ||
"types": "tsc --declaration --emitDeclarationOnly" | ||
}, | ||
"author": "", | ||
"dependencies": { | ||
"@metrics/client": "2.5.2", | ||
"@podium/schemas": "5.0.1", | ||
"@podium/utils": "5.0.5", | ||
"@metrics/client": "2.5.2", | ||
"abslog": "^2.4.0", | ||
"abslog": "2.4.4", | ||
"bcp47-validate": "^1.0.0", | ||
@@ -48,10 +51,3 @@ "bowser": "^2.7.0", | ||
"devDependencies": { | ||
"eslint": "8.57.0", | ||
"eslint-config-airbnb-base": "15.0.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-plugin-import": "2.29.1", | ||
"eslint-plugin-prettier": "5.1.3", | ||
"@babel/eslint-parser": "7.24.5", | ||
"tap": "18.7.2", | ||
"prettier": "3.2.5", | ||
"@semantic-release/changelog": "6.0.3", | ||
@@ -63,5 +59,13 @@ "@semantic-release/commit-analyzer": "11.1.0", | ||
"@semantic-release/release-notes-generator": "12.1.0", | ||
"benchmark": "2.1.4", | ||
"semantic-release": "23.0.8" | ||
"@types/readable-stream": "4.0.12", | ||
"eslint": "8.57.0", | ||
"eslint-config-airbnb-base": "15.0.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-plugin-import": "2.29.1", | ||
"eslint-plugin-prettier": "5.1.3", | ||
"prettier": "3.2.5", | ||
"semantic-release": "23.0.8", | ||
"tap": "18.7.2", | ||
"typescript": "5.4.5" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73815
422
17
Updatedabslog@2.4.4