-
Notifications
You must be signed in to change notification settings - Fork 132
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
base: master
Are you sure you want to change the base?
Add index.d.ts
#343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||
check(url: string, source_url: string, request_type: string, debug: true): BlockerResult; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Line 103 in dc2a4fa
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, after some reading (1, 2), my understanding is that This seems correct to me because it's describing the shape of Perhaps there is an issue with using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can use |
||||
ALL: string; | ||||
COSMETIC_ONLY: string; | ||||
NETWORK_ONLY: string; | ||||
}; | ||||
|
||||
export function uBlockResources( | ||||
web_accessible_resource_dir: string, | ||||
redirect_resources_path: string, | ||||
scriptlets_path?: string, | ||||
): Resource[]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"rust" | ||
], | ||
"main": "js/index.js", | ||
"types": "js/index.d.ts", | ||
"author": "Anton Lazarev <[email protected]>", | ||
"contributors": [ | ||
"Andrius Aucinas" | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
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