@prexjs/utils
v1.0.0
Published
Core utility functions for Prex and Express applications including ETag generation, trust proxy compilation, query parsing, and MIME normalization.
Readme
@prexjs/utils
Core utility functions for Prex and Express applications including ETag generation, trust proxy compilation, query parsing, and MIME normalization.
@prexjs/utils provides shared helper functions and internal compilation utilities used across @prexjs/prex, @prexjs/application, @prexjs/request, and @prexjs/response.
Features
- ETag Generators: Strong and weak ETag hashing (
etag,wetag,compileETag). - Proxy Trust Compilation: Compiles IP addresses, subnets, hop counts, or trust predicates using
@prexjs/proxy-address. - Query String Compilers: Supports simple (
node:querystring) and extended (qs) query string parsers. - Header & MIME Helpers: Normalizes content types, inspects media type accept parameters, and sets character encodings.
- HTTP Methods List: Complete list of supported HTTP methods recognized by Node.js.
Installation
Install using your preferred package manager:
# npm
npm install @prexjs/utils
# pnpm
pnpm add @prexjs/utils
# Yarn
yarn add @prexjs/utils
# Bun
bun add @prexjs/utilsQuick start
import {
compileTrust,
compileQueryParser,
compileETag,
setCharset,
} from '@prexjs/utils';
// Compile proxy trust helper
const trust = compileTrust('loopback, 10.0.0.0/8');
console.log(trust('127.0.0.1', 0)); // true
// Compile query parser
const parseQuery = compileQueryParser('extended');
console.log(parseQuery('user[name]=Alice&user[role]=admin'));
// Set charset on Content-Type
const contentType = setCharset('text/html', 'utf-8');
console.log(contentType); // 'text/html; charset=utf-8'API
compileTrust(val)
Compiles a proxy trust setting into a predicate function (addr: string, i: number) => boolean. Accepts booleans, numbers (hop count), IP addresses, CIDR ranges, or custom functions.
compileETag(val)
Returns an ETag generator function. Accepts 'strong', 'weak', booleans, or custom functions.
compileQueryParser(val)
Returns a query string parsing function. Accepts 'simple', 'extended', booleans, or custom functions.
setCharset(type, charset)
Appends or replaces the charset parameter on an existing MIME type string.
normalizeType(type) / normalizeTypes(types)
Normalizes shorthand extensions (e.g. 'json') to full MIME type descriptors (e.g. 'application/json').
Compatibility
- Node.js >= 18.0.0
- Supports both ESM and CommonJS
- TypeScript 5.0+
Related Packages
@prexjs/prex: The core Prex framework.@prexjs/etag: Low-level ETag creation.@prexjs/proxy-address: Proxy address resolution.
