Skip to content

Asynchronous event processing

Currently the Rule API allows you to listen for events and you must respond synchronously:

Example (TypeScript):

export default class MyRule extends Rule {
  public override setup(): void {
    this.on('tag:ready', (event: TagReadyEvent) => {
...

However some rule testing requires network access and other asynchronous functions. And those are now incompatible with HTML-validate.

Here is the new use case we could support:

export default class MyRule extends Rule {
  public override setup(): void {
    this.on('tag:ready', async (event: TagReadyEvent) => {
      const response = await fetch(event.target.getAttribute('href').value);
      // validate that response
...

We should carefully think of this use case breaks other usability of the project. And that will help us decide if this changes in scope or not. For example, today with everything runs synchronously, there is an implicit guarantee that the program will run quickly. And that means this program is well suited to run inside of an ID like VS Code in real time while you're typing HTML.

If this changes allowed, the program may possibly be less well suited for these kinds of use cases.

Here are the kinds of rules I have implemented that could benefit from this change. And also here are the workarounds I am using.

  1. External link checking
  2. External API lookup (kind of like Dependabot for your jsDelivr includes)

The workaround is that I'm using execSync with cURL each time. This means it will not work in the browser.

Additionally, because this is synchronous, it means I cannot easily use HTTP server mocking. For example, I first start up a mock server to respond to HTTP requests how I want, then I go ahead and run my code and then it interacts with my mock server. Right now, this produces a deadlock because you cannot wait on the asynchronous server while you are executing a synchronous request. The only reason that I need this is to mop my test cases and unit testing for my rules. Maybe this is too niche of a use case.

Maybe all of this is a niche use case so I'm hoping to open a discussion with other interested parties.