콘텐츠로 이동

stax-xml

이 콘텐츠는 아직 번역되지 않았습니다.

stax-xml


Defined in: StaxXmlParser.ts:113

High-performance asynchronous XML parser with circular buffer queue optimization.

This parser provides memory-efficient processing of large XML files through streaming with support for pull-based parsing, custom entity handling, and namespace processing.

OPTIMIZATION: Replaces array-based queue with circular buffer for O(1) operations. Achieved improvement: ~15% on large XML files (1GB+).

The parser uses UTF-8 safe processing with Boyer-Moore-Horspool pattern search optimization and supports both single-event and batch processing modes for improved performance.

Basic usage:

const xmlContent = '<root><item>Hello</item></root>';
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(xmlContent));
controller.close();
}
});
const parser = new StaxXmlParser(stream);
for await (const event of parser) {
console.log(event.type, event);
}

new StaxXmlParser(xmlStream, options): StaxXmlParser

Defined in: StaxXmlParser.ts:206

Creates a new StaxXmlParser instance.

ReadableStream<Uint8Array<ArrayBufferLike>>

The ReadableStream containing XML data as Uint8Array chunks

StaxXmlParserOptions = {}

Configuration options for the parser

StaxXmlParser

When xmlStream is not a valid ReadableStream

const xmlData = '<root><item>content</item></root>';
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(xmlData));
controller.close();
}
});
const parser = new StaxXmlParser(stream, {
autoDecodeEntities: true,
maxBufferSize: 64 * 1024,
initialQueueCapacity: 2048
});

get XmlEventType(): object

Defined in: StaxXmlParser.ts:1176

object

readonly START_DOCUMENT: "START_DOCUMENT" = 'START_DOCUMENT'

readonly END_DOCUMENT: "END_DOCUMENT" = 'END_DOCUMENT'

readonly START_ELEMENT: "START_ELEMENT" = 'START_ELEMENT'

readonly END_ELEMENT: "END_ELEMENT" = 'END_ELEMENT'

readonly CHARACTERS: "CHARACTERS" = 'CHARACTERS'

readonly CDATA: "CDATA" = 'CDATA'

readonly ERROR: "ERROR" = 'ERROR'

nextBatch(size?): Promise<AnyXmlEvent[]>

Defined in: StaxXmlParser.ts:563

number

Promise<AnyXmlEvent[]>

batchedIterator(batchSize?): AsyncGenerator<AnyXmlEvent[]>

Defined in: StaxXmlParser.ts:582

number

AsyncGenerator<AnyXmlEvent[]>

next(): Promise<IteratorResult<AnyXmlEvent, any>>

Defined in: StaxXmlParser.ts:837

Promise<IteratorResult<AnyXmlEvent, any>>

AsyncIterator.next

[asyncIterator](): AsyncIterator<AnyXmlEvent>

Defined in: StaxXmlParser.ts:856

AsyncIterator<AnyXmlEvent>


Defined in: StaxXmlParserSync.ts:50

TRUE INCREMENTAL State Machine XML Parser

Eliminates Generator overhead while maintaining StAX pull-based API Parses exactly ONE event per next() call (true incremental)

Performance: +20.67% improvement vs Generator baseline

new StaxXmlParserSync(xml, options): StaxXmlParserSync

Defined in: StaxXmlParserSync.ts:104

string

StaxXmlParserSyncOptions = {}

StaxXmlParserSync

[iterator](): Iterator<AnyXmlEvent>

Defined in: StaxXmlParserSync.ts:122

Symbol.iterator implementation - enables for…of loops Returns self to maintain single iterator state

Iterator<AnyXmlEvent>

Iterable.[iterator]

next(): IteratorResult<AnyXmlEvent>

Defined in: StaxXmlParserSync.ts:134

Main Iterator.next() implementation - TRUE INCREMENTAL parsing

Parses exactly ONE event per call without Generator overhead Uses state machine to track parsing progress

Performance: ~10ns/event overhead (vs ~95ns for Generator)

IteratorResult<AnyXmlEvent>

Iterator.next


Defined in: StaxXmlWriter.ts:131

High-performance asynchronous XML writer implementing the StAX (Streaming API for XML) pattern.

This writer provides efficient streaming XML generation using WritableStream for handling large XML documents with automatic buffering, backpressure management, and namespace support.

This is an optimized implementation with:

  • Optimization 1: Regex caching for entity escaping
  • Optimization 2: Attribute string batching
  • Optimization 3: Early entity check before regex execution

The writer supports streaming output with configurable buffering, automatic entity encoding, pretty printing with customizable indentation, and comprehensive namespace handling.

Basic usage:

const writableStream = new WritableStream({
write(chunk) {
console.log(new TextDecoder().decode(chunk));
}
});
const writer = new StaxXmlWriter(writableStream);
await writer.writeStartElement('root');
await writer.writeElement('item', { id: '1' }, 'Hello World');
await writer.writeEndElement();
await writer.close();

With pretty printing:

const options = {
prettyPrint: true,
indentString: ' ',
autoEncodeEntities: true
};
const writer = new StaxXmlWriter(writableStream, options);

new StaxXmlWriter(stream, options): StaxXmlWriter

Defined in: StaxXmlWriter.ts:168

WritableStream<Uint8Array<ArrayBufferLike>>

StaxXmlWriterOptions = {}

StaxXmlWriter

writeStartDocument(version, encoding?): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:273

Write XML declaration

string = '1.0'

string

Promise<StaxXmlWriter>

writeEndDocument(): Promise<void>

Defined in: StaxXmlWriter.ts:298

End document (automatically close all elements)

Promise<void>

writeStartElement(localName, options?): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:319

Write start element

string

WriteElementOptions

Promise<StaxXmlWriter>

writeEndElement(): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:402

Write end element

Promise<StaxXmlWriter>

writeCharacters(text): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:438

Write text

string

Promise<StaxXmlWriter>

writeCData(cdata): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:460

Write CDATA section

string

Promise<StaxXmlWriter>

writeComment(comment): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:480

Write comment

string

Promise<StaxXmlWriter>

writeRaw(xml): Promise<StaxXmlWriter>

Defined in: StaxXmlWriter.ts:503

Write raw XML content without escaping

string

Raw XML string to write

Promise<StaxXmlWriter>

this (chainable)

flush(): Promise<void>

Defined in: StaxXmlWriter.ts:512

Manual flush

Promise<void>

getMetrics(): object

Defined in: StaxXmlWriter.ts:519

Return metrics

object

totalBytesWritten: number = 0

flushCount: number = 0

lastFlushTime: number = 0

bufferUtilization: number

averageFlushSize: number


Defined in: StaxXmlWriterSync.ts:44

A class for writing XML similar to StAX XMLStreamWriter. This is an optimized implementation with:

  • Optimization 1: Regex caching for entity escaping
  • Optimization 2: Attribute string batching
  • Optimization 3: Early entity check before regex execution

new StaxXmlWriterSync(options): StaxXmlWriterSync

Defined in: StaxXmlWriterSync.ts:70

StaxXmlWriterSyncOptions = {}

StaxXmlWriterSync

writeStartDocument(version, encoding?): this

Defined in: StaxXmlWriterSync.ts:119

Writes the XML declaration (e.g., ). Should be called only once at the very beginning of the document.

string = '1.0'

XML version (default: “1.0”)

string

Encoding (default: value set in constructor)

this

this (chainable)

Error when called in incorrect state

writeEndDocument(): void

Defined in: StaxXmlWriterSync.ts:145

Indicates the end of the document and automatically closes all open elements.

void

Promise Promise that resolves when stream is flushed

getXmlString(): string

Defined in: StaxXmlWriterSync.ts:162

Returns the written XML string. Should be called after writeEndDocument() to get the complete XML.

string

The written XML string

writeStartElement(localName, options?): this

Defined in: StaxXmlWriterSync.ts:173

Writes a start element (e.g., or prefix:element).

string

Local name of the element

WriteElementOptions

Element writing options (prefix, uri, attributes, selfClosing)

this

this (chainable)

Error when called in incorrect state

writeAttribute(localName, value, prefix?): this

Defined in: StaxXmlWriterSync.ts:265

Writes an attribute. Can only be called immediately after writeStartElement().

string

Local name of the attribute

string

Attribute value

string

Namespace prefix of the attribute (note: this implementation does not manage namespace mapping)

this

this (chainable)

Error when called in incorrect state

writeNamespace(prefix, uri): this

Defined in: StaxXmlWriterSync.ts:285

Writes a namespace declaration. Can only be called immediately after writeStartElement(). This implementation simply writes the string in the form xmlns:prefix=“uri” or xmlns=“uri”. Actual namespace validation/management logic is not included.

string

Namespace prefix

string

Namespace URI

this

this (chainable)

Error when called in incorrect state

writeCharacters(text): this

Defined in: StaxXmlWriterSync.ts:309

Writes text content.

string

Text to write

this

this (chainable)

Error when called in incorrect state

writeCData(cdata): this

Defined in: StaxXmlWriterSync.ts:332

Writes a CDATA section.

string

CDATA content

this

this (chainable)

Error when called in incorrect state (especially when containing ’]]>’ sequence)

writeComment(comment): this

Defined in: StaxXmlWriterSync.ts:359

Writes a comment.

string

Comment content

this

this (chainable)

Error when called in incorrect state (especially when containing ’—’ sequence)

writeProcessingInstruction(target, data?): this

Defined in: StaxXmlWriterSync.ts:382

Writes a processing instruction (Processing Instruction).

string

PI target

string

PI data (optional)

this

this (chainable)

Error when called in incorrect state (especially when containing ’?>’ sequence)

writeRaw(xml): this

Defined in: StaxXmlWriterSync.ts:408

Writes raw XML content without escaping

string

Raw XML string to write

this

this (chainable)

writeEndElement(): this

Defined in: StaxXmlWriterSync.ts:419

Closes the currently open element (e.g., or </prefix:element>).

this

this (chainable)

Error when called with no open elements

setPrettyPrint(enabled): this

Defined in: StaxXmlWriterSync.ts:456

Enables/disables pretty print functionality.

boolean

Whether to enable pretty print

this

this (chainable)

setIndentString(indentString): this

Defined in: StaxXmlWriterSync.ts:466

Sets the indentation string.

string

String to use for indentation (e.g., ’ ’, ‘\t’, ’ ‘)

this

this (chainable)

isPrettyPrintEnabled(): boolean

Defined in: StaxXmlWriterSync.ts:475

Returns the current pretty print setting.

boolean

Whether pretty print is enabled

getIndentString(): string

Defined in: StaxXmlWriterSync.ts:483

Returns the current indentation string.

string

Currently set indentation string

Defined in: StaxXmlParser.ts:31

Configuration options for the StaxXmlParser

optional encoding: string

Defined in: StaxXmlParser.ts:36

Text encoding for the input stream

'utf-8'

optional addEntities: object[]

Defined in: StaxXmlParser.ts:42

Additional custom entities to decode

entity: string

value: string

[]

optional autoDecodeEntities: boolean

Defined in: StaxXmlParser.ts:48

Whether to automatically decode XML entities

true

optional maxBufferSize: number

Defined in: StaxXmlParser.ts:54

Maximum buffer size in bytes

65536

optional enableBufferCompaction: boolean

Defined in: StaxXmlParser.ts:60

Whether to enable buffer compaction for memory efficiency

true

optional batchSize: number

Defined in: StaxXmlParser.ts:66

Number of events to batch together

1

optional batchTimeout: number

Defined in: StaxXmlParser.ts:72

Timeout for batch processing in milliseconds

0

optional initialQueueCapacity: number

Defined in: StaxXmlParser.ts:78

Initial event queue capacity (circular buffer size)

1024

Defined in: StaxXmlParserSync.ts:25

optional autoDecodeEntities: boolean

Defined in: StaxXmlParserSync.ts:26

optional addEntities: object[]

Defined in: StaxXmlParserSync.ts:27

entity: string

value: string


Defined in: StaxXmlWriter.ts:25

Configuration options for the StaxXmlWriter

optional encoding: string

Defined in: StaxXmlWriter.ts:30

Text encoding for the output stream

'utf-8'

optional prettyPrint: boolean

Defined in: StaxXmlWriter.ts:36

Whether to format output with indentation

false

optional indentString: string

Defined in: StaxXmlWriter.ts:42

String used for indentation when prettyPrint is true

' '

optional addEntities: object[]

Defined in: StaxXmlWriter.ts:48

Additional custom entities to encode

entity: string

value: string

[]

optional autoEncodeEntities: boolean

Defined in: StaxXmlWriter.ts:54

Whether to automatically encode XML entities

true

optional namespaces: NamespaceDeclaration[]

Defined in: StaxXmlWriter.ts:60

Namespace declarations to include

[]

optional bufferSize: number

Defined in: StaxXmlWriter.ts:66

Internal buffer size in bytes

16384

optional highWaterMark: number

Defined in: StaxXmlWriter.ts:72

WritableStream backpressure threshold

65536

optional flushThreshold: number

Defined in: StaxXmlWriter.ts:78

Automatic flush threshold (percentage of bufferSize)

0.8

optional enableAutoFlush: boolean

Defined in: StaxXmlWriter.ts:84

Whether to enable automatic flushing

true

Defined in: StaxXmlWriterSync.ts:27

optional encoding: string

Defined in: StaxXmlWriterSync.ts:28

optional prettyPrint: boolean

Defined in: StaxXmlWriterSync.ts:29

optional indentString: string

Defined in: StaxXmlWriterSync.ts:30

optional addEntities: object[]

Defined in: StaxXmlWriterSync.ts:31

entity: string

value: string

optional autoEncodeEntities: boolean

Defined in: StaxXmlWriterSync.ts:32

optional namespaces: NamespaceDeclaration[]

Defined in: StaxXmlWriterSync.ts:33


Defined in: types.ts:66

Event fired when an XML element starts

type: "START_ELEMENT"

Defined in: types.ts:67

name: string

Defined in: types.ts:68

optional localName: string

Defined in: types.ts:69

optional prefix: string

Defined in: types.ts:70

optional uri: string

Defined in: types.ts:71

attributes: Record<string, string>

Defined in: types.ts:72

optional attributesWithPrefix: Record<string, AttributeInfo>

Defined in: types.ts:73


Defined in: types.ts:84

type: "CHARACTERS"

Defined in: types.ts:85

value: string

Defined in: types.ts:86


Defined in: types.ts:89

type: "CDATA"

Defined in: types.ts:90

value: string

Defined in: types.ts:91


Defined in: types.ts:94

type: "ERROR"

Defined in: types.ts:95

error: Error

Defined in: types.ts:96


Defined in: types.ts:114

Attribute interface (for Writer)

optional prefix: string

Defined in: types.ts:115

localName: string

Defined in: types.ts:116

optional uri: string

Defined in: types.ts:117

value: string

Defined in: types.ts:118


Defined in: types.ts:354

Element writing options interface (for Writer)

optional prefix: string

Defined in: types.ts:355

optional uri: string

Defined in: types.ts:356

optional attributes: Record<string, string | AttributeInfo>

Defined in: types.ts:357

optional selfClosing: boolean

Defined in: types.ts:358

optional comment: string

Defined in: types.ts:359

XmlEventType = typeof XmlEventType[keyof typeof XmlEventType]

Defined in: types.ts:6


AnyXmlEvent = StartDocumentEvent | EndDocumentEvent | StartElementEvent | EndElementEvent | CharactersEvent | CdataEvent | ErrorEvent

Defined in: types.ts:102

Discriminated Union type for developer use

const XmlEventType: object

Defined in: types.ts:6

Enumeration of XML stream event types used by the StAX parser

readonly START_DOCUMENT: "START_DOCUMENT" = 'START_DOCUMENT'

readonly END_DOCUMENT: "END_DOCUMENT" = 'END_DOCUMENT'

readonly START_ELEMENT: "START_ELEMENT" = 'START_ELEMENT'

readonly END_ELEMENT: "END_ELEMENT" = 'END_ELEMENT'

readonly CHARACTERS: "CHARACTERS" = 'CHARACTERS'

readonly CDATA: "CDATA" = 'CDATA'

readonly ERROR: "ERROR" = 'ERROR'

isStartElement(event): event is StartElementEvent

Defined in: types.ts:297

Type guard function - Check if the event is a START_ELEMENT event

AnyXmlEvent

XML event to check

event is StartElementEvent

true if the event is a START_ELEMENT event, false otherwise


isEndElement(event): event is EndElementEvent

Defined in: types.ts:306

Type guard function - Check if the event is an END_ELEMENT event

AnyXmlEvent

XML event to check

event is EndElementEvent

true if the event is an END_ELEMENT event, false otherwise


isCharacters(event): event is CharactersEvent

Defined in: types.ts:315

Type guard function - Check if the event is a CHARACTERS event

AnyXmlEvent

XML event to check

event is CharactersEvent

true if the event is a CHARACTERS event, false otherwise


isCdata(event): event is CdataEvent

Defined in: types.ts:323

Type guard function - Check if the event is a CDATA event

AnyXmlEvent

XML event to check

event is CdataEvent

true if the event is a CDATA event, false otherwise


isError(event): event is ErrorEvent

Defined in: types.ts:331

Type guard function - Check if the event is an ERROR event

AnyXmlEvent

XML event to check

event is ErrorEvent

true if the event is an ERROR event, false otherwise


isStartDocument(event): event is StartDocumentEvent

Defined in: types.ts:339

Type guard function - Check if the event is a START_DOCUMENT event

AnyXmlEvent

XML event to check

event is StartDocumentEvent

true if the event is a START_DOCUMENT event, false otherwise


isEndDocument(event): event is EndDocumentEvent

Defined in: types.ts:347

Type guard function - Check if the event is an END_DOCUMENT event

AnyXmlEvent

XML event to check

event is EndDocumentEvent

true if the event is an END_DOCUMENT event, false otherwise