This site uses cookies for analytics. By continuing to browse this site, you agree to this use.
A JS Foundation Project

Develop a parser

Develop a parser

A parser understands more deeply a resource and exposes that information to other parts of sonarwhal.

To create one, you will need to create a new Class that extends from Parser.

import { Parser, IFetchEnd } from '../../types';
import { Sonarwhal } from '../../sonarwhal';

export default class CustomParser extends Parser {

public constructor(sonarwhal: Sonarwhal) {
super(sonarwhal, '<parser_name>');

// Replace 'resource' with the tipe you need (html, script,
// css, image, etc.)
this.sonarwhal.on('fetch::end::resource', this.onFetchEnd);

public async onFetchEnd(data: FetchEnd) {
// Your magic to detect if the parser understands the content here
// You probably want to check the contentType first
// and maybe leter use a schema if a configuration file or something else

// If there's something to share, do it via an event
await this.sonarwhal.emitAsync('parse::<parser_name>::<end | error>', data);

The way parsers receive and share information is via events. To access a resource the parser needs to subscribe in the constructor to one or more events. In most cases you will subscribe to one of the fetch::end::<resource-type> events, like fetch::end::script.

Once you have analyzed the resource, the way to share information is via events (custom or not):

await this.sonarwhal.emitAsync('parse::<parser_name>::<end | error>', data);

Make sure to document which ones you are sending so rules can use them and know what to expect.

You can always check the code of any of the official parsers for more complex scenarios.