lowlighter / libs

🍱 Collection of carefully crafted TypeScript standalone libraries. Minimal, unbloated, convenient.
https://jsr.io/@libs
MIT License
118 stars 11 forks source link

feat(xml): implement async parsing #49

Open lowlighter opened 5 months ago

lowlighter commented 5 months ago

Currently we support ReaderSync but it's be nice to be able to pass a async Reader

/** Asynchronous reader. */
type Reader = { read(buffer: Uint8Array): Promise<Nullable<number>> }

/** Mocked reader */
class MockReader implements Reader {
  constructor(string: string) {
    this.#data = new TextEncoder().encode(string)
  }
  readonly #data
  #cursor = 0
  async read(buffer: Uint8Array) {
    const bytes = this.#data.slice(this.#cursor, this.#cursor + buffer.length)
    buffer.set(bytes)
    this.#cursor = Math.min(this.#cursor + bytes.length, this.#data.length)
    return bytes.length || null
  }
}

This however may require a bit of work since we need to patch xml/wasm_xml_parser/src/lib.rs to support asynchronous reading, possibly using tokio ?