UNPKG

tedious

Version:

A TDS driver, for connecting to MS SQLServer databases.

69 lines (68 loc) • 2.63 kB
import { type InternalConnectionOptions } from '../connection'; import { type Token } from './token'; import { type ColumnMetadata } from './colmetadata-token-parser'; import { type Result } from './helpers'; export type ParserOptions = Pick<InternalConnectionOptions, 'useUTC' | 'lowerCaseGuids' | 'tdsVersion' | 'useColumnNames' | 'columnNameReplacer' | 'camelCaseColumns'>; /** * Parses a token that is short enough to simply be parsed again from its * start if it is not fully available yet. */ type TokenParser = (buf: Buffer, offset: number, options: ParserOptions) => Result<Token | undefined>; /** * Parses a token that can grow arbitrarily large, and thus needs to be parsed * incrementally. * * The reader starts parsing right after the token's type byte and keeps its * progress across calls: if `read` throws a `NotEnoughDataError`, it is * called again once more data has arrived. */ export interface TokenReader { read(parser: Parser): Token; } /** * A push-based parser for the tokens contained in a TDS token stream. * * Data is pushed into the parser as it arrives via `write`, and parsed tokens * are pulled out via `read`. Tokens are parsed lazily, one at a time, so any * effects of handling a token (e.g. the TDS version negotiated via the * `LOGINACK` token) apply to the parsing of all tokens that follow it. */ declare class Parser { options: ParserOptions; colMetadata: ColumnMetadata[]; buffer: Buffer; position: number; pending: Buffer[]; pendingLength: number; bytesNeeded: number; tokenReader: TokenReader | undefined; constructor(options: ParserOptions, colMetadata?: ColumnMetadata[]); /** * Parse all tokens from the given chunks of data. */ static parseTokens(iterable: AsyncIterable<Buffer> | Iterable<Buffer>, options: ParserOptions, colMetadata?: ColumnMetadata[]): AsyncGenerator<Token, void, undefined>; /** * Add a chunk of data to be parsed. */ write(chunk: Buffer): void; /** * Parse the next token, or return `undefined` if more data needs to be * written first. */ read(): Token | undefined; /** * Signal that all data was written. * * Throws if the data ended in the middle of a token. */ end(): void; /** * Append the pending chunks to the unconsumed data, if that makes enough * data available for parsing to make progress. */ appendPending(): boolean; readToken(): Token | undefined; parseToken(parser: TokenParser): Token | undefined; startTokenReader(reader: TokenReader): Token; } export default Parser;