|
|
@ -1,4 +1,23 @@ |
|
|
|
import Key from './Key'; |
|
|
|
import Key from './Key'; |
|
|
|
|
|
|
|
import Validator from './Validator'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ItemSchema = { |
|
|
|
|
|
|
|
anyOf: [ |
|
|
|
|
|
|
|
{ type: 'string' }, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
type: 'object', |
|
|
|
|
|
|
|
properties: { |
|
|
|
|
|
|
|
url: { type: 'string' }, |
|
|
|
|
|
|
|
keys: { |
|
|
|
|
|
|
|
type: 'array', |
|
|
|
|
|
|
|
items: { type: 'string', minLength: 1 }, |
|
|
|
|
|
|
|
minItems: 1, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
required: ['url', 'keys'], |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export type BlacklistItemJSON = string | { |
|
|
|
export type BlacklistItemJSON = string | { |
|
|
|
url: string, |
|
|
|
url: string, |
|
|
@ -12,18 +31,6 @@ const regexFromWildcard = (pattern: string): RegExp => { |
|
|
|
return new RegExp(regexStr); |
|
|
|
return new RegExp(regexStr); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const isArrayOfString = (raw: any): boolean => { |
|
|
|
|
|
|
|
if (!Array.isArray(raw)) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (let x of Array.from(raw)) { |
|
|
|
|
|
|
|
if (typeof x !== 'string') { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class BlacklistItem { |
|
|
|
export class BlacklistItem { |
|
|
|
public readonly pattern: string; |
|
|
|
public readonly pattern: string; |
|
|
|
|
|
|
|
|
|
|
@ -47,30 +54,11 @@ export class BlacklistItem { |
|
|
|
this.keyEntities = this.keys.map(Key.fromMapKey); |
|
|
|
this.keyEntities = this.keys.map(Key.fromMapKey); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static fromJSON(raw: any): BlacklistItem { |
|
|
|
static fromJSON(json: unknown): BlacklistItem { |
|
|
|
if (typeof raw === 'string') { |
|
|
|
let obj = new Validator<BlacklistItemJSON>(ItemSchema).validate(json); |
|
|
|
return new BlacklistItem(raw, false, []); |
|
|
|
return typeof obj === 'string' |
|
|
|
} else if (typeof raw === 'object' && raw !== null) { |
|
|
|
? new BlacklistItem(obj, false, []) |
|
|
|
if (!('url' in raw)) { |
|
|
|
: new BlacklistItem(obj.url, true, obj.keys); |
|
|
|
throw new TypeError( |
|
|
|
|
|
|
|
`missing field "url" of blacklist item: ${JSON.stringify(raw)}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (typeof raw.url !== 'string') { |
|
|
|
|
|
|
|
throw new TypeError( |
|
|
|
|
|
|
|
`invalid field "url" of blacklist item: ${JSON.stringify(raw)}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!('keys' in raw)) { |
|
|
|
|
|
|
|
throw new TypeError( |
|
|
|
|
|
|
|
`missing field "keys" of blacklist item: ${JSON.stringify(raw)}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!isArrayOfString(raw.keys)) { |
|
|
|
|
|
|
|
throw new TypeError( |
|
|
|
|
|
|
|
`invalid field "keys" of blacklist item: ${JSON.stringify(raw)}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return new BlacklistItem(raw.url as string, true, raw.keys as string[]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
throw new TypeError( |
|
|
|
|
|
|
|
`invalid format of blacklist item: ${JSON.stringify(raw)}`); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
toJSON(): BlacklistItemJSON { |
|
|
|
toJSON(): BlacklistItemJSON { |
|
|
@ -103,11 +91,11 @@ export default class Blacklist { |
|
|
|
) { |
|
|
|
) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static fromJSON(json: any): Blacklist { |
|
|
|
static fromJSON(json: unknown): Blacklist { |
|
|
|
if (!Array.isArray(json)) { |
|
|
|
if (!Array.isArray(json)) { |
|
|
|
throw new TypeError('blacklist is not an array: ' + JSON.stringify(json)); |
|
|
|
throw new TypeError('blacklist is not an array'); |
|
|
|
} |
|
|
|
} |
|
|
|
let items = Array.from(json).map(item => BlacklistItem.fromJSON(item)); |
|
|
|
let items = json.map(o => BlacklistItem.fromJSON(o)); |
|
|
|
return new Blacklist(items); |
|
|
|
return new Blacklist(items); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|