UNPKG

@edge-csrf/node-http

Version:

Edge-CSRF integration library for node's http module

94 lines (89 loc) 2.66 kB
import { IncomingMessage, ServerResponse } from 'http'; /** * Represents cookie options in config */ declare class CookieOptions { domain: string; httpOnly: boolean; maxAge: number | undefined; name: string; partitioned: boolean | undefined; path: string; sameSite: boolean | 'none' | 'strict' | 'lax'; secure: boolean; constructor(opts?: Partial<CookieOptions>); } /** * Represents a function to retrieve token value from a request */ type TokenValueFunction = { (request: Request): Promise<string>; }; /** * Represents token options in config */ declare class TokenOptions { readonly fieldName: string; value: TokenValueFunction | undefined; _fieldNameRegex: RegExp; constructor(opts?: Partial<TokenOptions>); } /** * Represents CsrfProtect configuration object */ declare class Config { excludePathPrefixes: string[]; ignoreMethods: string[]; saltByteLength: number; secretByteLength: number; cookie: CookieOptions; token: TokenOptions; constructor(opts?: Partial<ConfigOptions>); } /** * Represents CsrfProtect configuration options object */ interface ConfigOptions extends Omit<Config, 'cookie' | 'token'> { cookie: Partial<CookieOptions>; token: Partial<TokenOptions>; } /** * Represents a generic CSRF protection error */ declare class CsrfError extends Error { } /** * Represents token options in config */ declare class NodeHttpTokenOptions extends TokenOptions { responseHeader: string; constructor(opts?: Partial<NodeHttpTokenOptions>); } /** * Represents configuration object */ declare class NodeHttpConfig extends Config { excludePathPrefixes: string[]; token: NodeHttpTokenOptions; constructor(opts?: Partial<NodeHttpConfigOptions>); } /** * Represents configuration options object */ interface NodeHttpConfigOptions extends Omit<ConfigOptions, 'token'> { token: Partial<NodeHttpTokenOptions>; } /** * Represents signature of CSRF protect function to be used in node-http request handlers */ type NodeHttpCsrfProtect = { (request: IncomingMessage, response: ServerResponse): Promise<void>; }; /** * Create CSRF protection function for use in node-http request handlers * @param {Partial<NodeHttpConfigOptions>} opts - Configuration options * @returns {NodeHttpCsrfProtect} - The CSRF protect function * @throws {CsrfError} - An error if CSRF validation failed */ declare function createCsrfProtect(opts?: Partial<NodeHttpConfigOptions>): NodeHttpCsrfProtect; export { CsrfError, NodeHttpConfig, type NodeHttpConfigOptions, type NodeHttpCsrfProtect, NodeHttpTokenOptions, createCsrfProtect };