Skip to content

Async element handler throws - Cannot read properties of undefined (reading 'asyncify_get_state') #7

@paed01

Description

@paed01

Hi, and thank you for a fantastic library. Immensely helpful when unit testing our worker before deploying to Cloudflare.

According to the official docs async element handlers are supported. But when we attempt to use one during testing we get an error.

This snippet:

import { HTMLRewriter } from 'htmlrewriter'

const rewriter = new HTMLRewriter()

async function main() {
    rewriter.on('a', {
        async element(element) {
            await new Promise((resolve) => {
                element.setAttribute('href', 'https://www.baidu.com');
                resolve();
            });
        },
    })
    const res = await rewriter.transform(
        new Response('<a href="https://www.google.com">google</a>'),
    )
    console.log(await res.text())
}

main()

throws:

file:///app/node_modules/htmlrewriter/dist/asyncify.js:77
    if (wasm.asyncify_get_state() === State.REWINDING) {
             ^

TypeError: Cannot read properties of undefined (reading 'asyncify_get_state')
    at awaitPromise (file:///app/node_modules/htmlrewriter/dist/asyncify.js:77:14)
    at imports.wbg.__wbg_awaitPromise_39a1101fd8518869 (file:///app/node_modules/htmlrewriter/dist/html_rewriter.js:814:9)
    at wasm://wasm/0036341a:wasm-function[250]:0x7da04
    at wasm://wasm/0036341a:wasm-function[70]:0x32ee6
    at wasm://wasm/0036341a:wasm-function[47]:0x18162
    at wasm://wasm/0036341a:wasm-function[86]:0x3bd2c
    at wasm://wasm/0036341a:wasm-function[185]:0x6109a
    at wasm://wasm/0036341a:wasm-function[160]:0x5a5c1
    at wasm://wasm/0036341a:wasm-function[386]:0x92305
    at HTMLRewriter.write (file:///app/node_modules/htmlrewriter/dist/html_rewriter.js:665:14)

Are we doing something wrong or are async handlers not supported by this package?

Best regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions