Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index.d.ts #343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions js/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/// <reference lib="es2020" />

declare interface MimeType {
mime: string;
}

export interface Resource {
name: string;
aliases: string[];
kind: MimeType | "template";
content: string;
dependencies?: string[];
permission?: number;
}

export interface FilterListMetadata {
homepage?: string;
title?: string;
expires?: number;
redirect?: string;
}

export interface ParseOptions {
format: typeof FilterFormat;
rule_types: typeof RuleTypes;
}

declare interface BlockerResult {
matched: boolean,
important: boolean,
redirect?: string,
rewritten_url?: string,
exception?: string,
filter?: string,
}

export class Engine {
constructor(rules: FilterSet, debug: boolean);
addResource(resource: Resource): boolean;
check(url: string, source_url: string, request_type: string, debug?: false): boolean;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, request_type is enum in Rust. So, I think it should be enum here too.

https://docs.rs/adblock/latest/adblock/request/enum.RequestType.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, although the enum isn't currently used in any public-facing Rust API. Those functions just accept string arguments. I do want to change that but it'll have to be left to a breaking change in the future.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can deal with union type here:

type RequestType =
  | 'beacon'
  | 'csp'
  | 'document'
  | 'dtd'
  | 'fetch'
  | 'font'
  | 'image'
  | 'media'
  | 'object'
  | 'other'
  | 'ping'
  | 'script'
  | 'stylesheet'
  | 'subdocument'
  | 'websocket'
  | 'xlst'
  | 'xmlhttprequest';

For reference see different implementations from other adblockers:

https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/tsurlfilter/src/request-type.ts#L6-L39

https://github.com/ghostery/adblocker/blob/0eb03a3f412c0764469f7561cf5f734eb4df4c66/packages/adblocker/src/request.ts#L58-L71

check(url: string, source_url: string, request_type: string, debug: true): BlockerResult;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can pass an object (aka named parameters) instead of passing 4 parameters separately, and then we will have a better user experience.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like that's a breaking change as well, so I'll leave it as-is for now

clearTags(): null;
deserialize(serialized_handle: ArrayBuffer): null;
enableTag(tag: string): null;
getResource(name: string): Resource;
serializeCompressed(): ArrayBuffer;
serializeRaw(): ArrayBuffer;
tagExists(tag: string): boolean;
useResources(resources: Resource[]): null;
}

type CbType =
'block' |
'block-cookies' |
'css-display-none' |
'ignore-previous-rules' |
'make-https';

declare interface CbAction {
type: CbType,
selector?: string,
}

type CbResourceType =
'document' |
'image' |
'style-sheet' |
'script' |
'font' |
'raw' |
'svg-document' |
'media' |
'popup';

type CbLoadType =
'first-party' |
'third-party';

declare interface CbTrigger {
'url-filter': string,
'url-filter-is-case-sensitive'?: boolean,
'if-domain'?: string[],
'unless-domain'?: string[],
'resource-type'?: Record<string, CbResourceType>,
'load-type'?: CbLoadType[],
'if-top-url'?: string[],
'unless-top-url'?: string[],
}

declare interface CbRule {
action: CbAction,
trigger: CbTrigger,
}

declare interface ContentBlockingConversionResult {
content_blocking_rules: CbRule[],
filters_used: string[],
}

export class FilterSet {
constructor(debug: boolean);
addFilter(filter: string, opts?: ParseOptions): null;
addFilters(rules: string[], opts?: ParseOptions): FilterListMetadata;
intoContentBlocking(): ContentBlockingConversionResult | undefined;
}

export const FilterFormat: {
HOSTS: string;
STANDARD: string;
};

export const RuleTypes: {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be an enum or union type. I do not know which will be compatible with the Rust library. At least it is enum here: https://docs.rs/adblock/latest/adblock/lists/enum.RuleTypes.html

For now, it is an object with all required fields and it can not be used to parse only some types of rules

addFilters(rules: string[], opts?: ParseOptions): FilterListMetadata;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, after some reading (1, 2), my understanding is that enums shouldn't be declared in a .d.ts file.

This seems correct to me because it's describing the shape of (await import('adblock-rs')).RuleTypes, which basically behaves like a struct with those 3 fields.

Perhaps there is an issue with using rule_types: typeof RuleTypes in ParseOptions instead? I think it needs to be "field of" rather than typeof, but I don't know how to express that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can use keyof typeof RyleTypes or union type like here:
#343 (comment)

ALL: string;
COSMETIC_ONLY: string;
NETWORK_ONLY: string;
};

export function uBlockResources(
web_accessible_resource_dir: string,
redirect_resources_path: string,
scriptlets_path?: string,
): Resource[];
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"rust"
],
"main": "js/index.js",
"types": "js/index.d.ts",
"author": "Anton Lazarev <[email protected]>",
"contributors": [
"Andrius Aucinas"
Expand Down
Loading