parent
8eddcc1785
commit
9ff80fcac3
3 changed files with 227 additions and 70 deletions
@ -1,39 +1,117 @@ |
||||
export type BlacklistJSON = string[]; |
||||
export type BlacklistItemJSON = string | { |
||||
url: string, |
||||
keys: string[], |
||||
}; |
||||
|
||||
export type BlacklistJSON = BlacklistItemJSON[]; |
||||
|
||||
const fromWildcard = (pattern: string): RegExp => { |
||||
const regexFromWildcard = (pattern: string): RegExp => { |
||||
let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; |
||||
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 { |
||||
public readonly pattern: string; |
||||
|
||||
private regex: RegExp; |
||||
|
||||
public readonly partial: boolean; |
||||
|
||||
public readonly keys: string[]; |
||||
|
||||
private constructor( |
||||
pattern: string, |
||||
partial: boolean, |
||||
keys: string[] |
||||
) { |
||||
this.pattern = pattern; |
||||
this.regex = regexFromWildcard(pattern); |
||||
this.partial = partial; |
||||
this.keys = keys; |
||||
} |
||||
|
||||
static fromJSON(raw: any): BlacklistItem { |
||||
if (typeof raw === 'string') { |
||||
return new BlacklistItem(raw, false, []); |
||||
} else if (typeof raw === 'object' && raw !== null) { |
||||
if (!('url' in raw)) { |
||||
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 { |
||||
if (!this.partial) { |
||||
return this.pattern; |
||||
} |
||||
return { url: this.pattern, keys: this.keys }; |
||||
} |
||||
|
||||
matches(url: URL): boolean { |
||||
return this.pattern.includes('/') |
||||
? this.regex.test(url.host + url.pathname) |
||||
: this.regex.test(url.host); |
||||
} |
||||
|
||||
includeKey(url: URL, keys: string): boolean { |
||||
if (!this.matches(url)) { |
||||
return false; |
||||
} |
||||
return !this.partial || this.keys.includes(keys); |
||||
} |
||||
} |
||||
|
||||
export default class Blacklist { |
||||
constructor( |
||||
private blacklist: string[], |
||||
private blacklist: BlacklistItem[], |
||||
) { |
||||
} |
||||
|
||||
static fromJSON(json: any): Blacklist { |
||||
if (!Array.isArray(json)) { |
||||
throw new TypeError(`"blacklist" is not an array of string`); |
||||
} |
||||
for (let x of json) { |
||||
if (typeof x !== 'string') { |
||||
throw new TypeError(`"blacklist" is not an array of string`); |
||||
} |
||||
throw new TypeError('blacklist is not an array: ' + JSON.stringify(json)); |
||||
} |
||||
return new Blacklist(json); |
||||
let items = Array.from(json).map(item => BlacklistItem.fromJSON(item)); |
||||
return new Blacklist(items); |
||||
} |
||||
|
||||
toJSON(): BlacklistJSON { |
||||
return this.blacklist; |
||||
return this.blacklist.map(item => item.toJSON()); |
||||
} |
||||
|
||||
includes(url: string): boolean { |
||||
let u = new URL(url); |
||||
return this.blacklist.some((item) => { |
||||
if (!item.includes('/')) { |
||||
return fromWildcard(item).test(u.host); |
||||
} |
||||
return fromWildcard(item).test(u.host + u.pathname); |
||||
}); |
||||
includesEntireBlacklist(url: URL): boolean { |
||||
return this.blacklist.some(item => !item.partial && item.matches(url)); |
||||
} |
||||
|
||||
includeKey(url: URL, key: string) { |
||||
return this.blacklist.some(item => item.includeKey(url, key)); |
||||
} |
||||
} |
||||
|
@ -1,77 +1,155 @@ |
||||
import Blacklist from '../../../src/shared/settings/Blacklist'; |
||||
import Blacklist, { BlacklistItem } from '../../../src/shared/settings/Blacklist'; |
||||
import { expect } from 'chai'; |
||||
|
||||
describe('Blacklist', () => { |
||||
describe('fromJSON', () => { |
||||
it('returns empty array by empty settings', () => { |
||||
let blacklist = Blacklist.fromJSON([]); |
||||
expect(blacklist.toJSON()).to.be.empty; |
||||
describe('BlacklistItem', () => { |
||||
describe('#fromJSON', () => { |
||||
it('parses string pattern', () => { |
||||
let item = BlacklistItem.fromJSON('example.com'); |
||||
expect(item.pattern).to.equal('example.com'); |
||||
expect(item.partial).to.be.false; |
||||
}); |
||||
|
||||
it('returns blacklist by valid settings', () => { |
||||
let blacklist = Blacklist.fromJSON([ |
||||
'github.com', |
||||
'circleci.com', |
||||
]); |
||||
|
||||
expect(blacklist.toJSON()).to.deep.equal([ |
||||
'github.com', |
||||
'circleci.com', |
||||
]); |
||||
it('parses partial blacklist item', () => { |
||||
let item = BlacklistItem.fromJSON({ url: 'example.com', keys: ['j', 'k']}); |
||||
expect(item.pattern).to.equal('example.com'); |
||||
expect(item.partial).to.be.true; |
||||
expect(item.keys).to.deep.equal(['j', 'k']); |
||||
}); |
||||
|
||||
it('throws a TypeError by invalid settings', () => { |
||||
expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON({})).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON([1,2,3])).to.throw(TypeError); |
||||
it('throws a TypeError', () => { |
||||
expect(() => BlacklistItem.fromJSON(null)).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON(100)).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({url: 'google.com'})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({keys: ['a']})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({url: 'google.com', keys: 10})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({url: 'google.com', keys: ['a', 'b', 3]})).to.throw(TypeError); |
||||
}); |
||||
}); |
||||
|
||||
describe('#includes', () => { |
||||
it('matches by *', () => { |
||||
let blacklist = new Blacklist(['*']); |
||||
|
||||
expect(blacklist.includes('https://github.com/abc')).to.be.true; |
||||
describe('#matches', () => { |
||||
it('matches by "*"', () => { |
||||
let item = BlacklistItem.fromJSON('*'); |
||||
expect(item.matches(new URL('https://github.com/abc'))).to.be.true; |
||||
}); |
||||
|
||||
it('matches by hostname', () => { |
||||
let blacklist = new Blacklist(['github.com']); |
||||
|
||||
expect(blacklist.includes('https://github.com')).to.be.true; |
||||
expect(blacklist.includes('https://gist.github.com')).to.be.false; |
||||
expect(blacklist.includes('https://github.com/ueokande')).to.be.true; |
||||
expect(blacklist.includes('https://github.org')).to.be.false; |
||||
expect(blacklist.includes('https://google.com/search?q=github.org')).to.be.false; |
||||
let item = BlacklistItem.fromJSON('github.com'); |
||||
expect(item.matches(new URL('https://github.com'))).to.be.true; |
||||
expect(item.matches(new URL('https://gist.github.com'))).to.be.false; |
||||
expect(item.matches(new URL('https://github.com/ueokande'))).to.be.true; |
||||
expect(item.matches(new URL('https://github.org'))).to.be.false; |
||||
expect(item.matches(new URL('https://google.com/search?q=github.org'))).to.be.false; |
||||
}); |
||||
|
||||
it('matches by hostname with wildcard', () => { |
||||
let blacklist = new Blacklist(['*.github.com']); |
||||
let item = BlacklistItem.fromJSON('*.github.com'); |
||||
|
||||
expect(blacklist.includes('https://github.com')).to.be.false; |
||||
expect(blacklist.includes('https://gist.github.com')).to.be.true; |
||||
}) |
||||
expect(item.matches(new URL('https://github.com'))).to.be.false; |
||||
expect(item.matches(new URL('https://gist.github.com'))).to.be.true; |
||||
}); |
||||
|
||||
it('matches by path', () => { |
||||
let blacklist = new Blacklist(['github.com/abc']); |
||||
let item = BlacklistItem.fromJSON('github.com/abc'); |
||||
|
||||
expect(blacklist.includes('https://github.com/abc')).to.be.true; |
||||
expect(blacklist.includes('https://github.com/abcdef')).to.be.false; |
||||
expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; |
||||
}) |
||||
expect(item.matches(new URL('https://github.com/abc'))).to.be.true; |
||||
expect(item.matches(new URL('https://github.com/abcdef'))).to.be.false; |
||||
expect(item.matches(new URL('https://gist.github.com/abc'))).to.be.false; |
||||
}); |
||||
|
||||
it('matches by path with wildcard', () => { |
||||
let blacklist = new Blacklist(['github.com/abc*']); |
||||
let item = BlacklistItem.fromJSON('github.com/abc*'); |
||||
|
||||
expect(blacklist.includes('https://github.com/abc')).to.be.true; |
||||
expect(blacklist.includes('https://github.com/abcdef')).to.be.true; |
||||
expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; |
||||
}) |
||||
expect(item.matches(new URL('https://github.com/abc'))).to.be.true; |
||||
expect(item.matches(new URL('https://github.com/abcdef'))).to.be.true; |
||||
expect(item.matches(new URL('https://gist.github.com/abc'))).to.be.false; |
||||
}); |
||||
|
||||
it('matches address and port', () => { |
||||
let blacklist = new Blacklist(['127.0.0.1:8888']); |
||||
let item = BlacklistItem.fromJSON('127.0.0.1:8888'); |
||||
|
||||
expect(item.matches(new URL('http://127.0.0.1:8888/'))).to.be.true; |
||||
expect(item.matches(new URL('http://127.0.0.1:8888/hello'))).to.be.true; |
||||
}); |
||||
|
||||
expect(blacklist.includes('http://127.0.0.1:8888/')).to.be.true; |
||||
expect(blacklist.includes('http://127.0.0.1:8888/hello')).to.be.true; |
||||
it('matches with partial blacklist', () => { |
||||
let item = BlacklistItem.fromJSON({ url: 'google.com', keys: ['j', 'k'] }); |
||||
|
||||
expect(item.matches(new URL('https://google.com'))).to.be.true; |
||||
expect(item.matches(new URL('https://yahoo.com'))).to.be.false; |
||||
}) |
||||
}) |
||||
}); |
||||
|
||||
describe('#includesPartialKeys', () => { |
||||
it('matches with partial keys', () => { |
||||
let item = BlacklistItem.fromJSON({url: 'google.com', keys: ['j', 'k']}); |
||||
|
||||
expect(item.includeKey(new URL('http://google.com/maps'), 'j')).to.be.true; |
||||
expect(item.includeKey(new URL('http://google.com/maps'), 'z')).to.be.false; |
||||
expect(item.includeKey(new URL('http://maps.google.com/'), 'j')).to.be.false; |
||||
}) |
||||
}); |
||||
}); |
||||
|
||||
describe('Blacklist', () => { |
||||
describe('#fromJSON', () => { |
||||
it('parses string list', () => { |
||||
let blacklist = Blacklist.fromJSON(['example.com', 'example.org']); |
||||
expect(blacklist.toJSON()).to.deep.equals([ |
||||
'example.com', 'example.org', |
||||
]); |
||||
}); |
||||
|
||||
it('parses mixed blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON([ |
||||
{ url: 'example.com', keys: ['j', 'k']}, |
||||
'example.org', |
||||
]); |
||||
expect(blacklist.toJSON()).to.deep.equals([ |
||||
{ url: 'example.com', keys: ['j', 'k']}, |
||||
'example.org', |
||||
]); |
||||
}); |
||||
|
||||
it('parses empty blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON([]); |
||||
expect(blacklist.toJSON()).to.deep.equals([]); |
||||
}); |
||||
|
||||
it('throws a TypeError', () => { |
||||
expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON(100)).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON({})).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON([100])).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON([{}])).to.throw(TypeError); |
||||
}) |
||||
}); |
||||
|
||||
describe('#includesEntireBlacklist', () => { |
||||
it('matches a url with entire blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON(['google.com', '*.github.com']); |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://google.com'))).to.be.true; |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://github.com'))).to.be.false; |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://gist.github.com'))).to.be.true; |
||||
}); |
||||
|
||||
it('does not matches with partial blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON(['google.com', { url: 'yahoo.com', keys: ['j', 'k'] }]); |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://google.com'))).to.be.true; |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://yahoo.com'))).to.be.false; |
||||
}); |
||||
}); |
||||
|
||||
describe('#includesKeys', () => { |
||||
it('matches with entire blacklist or keys in the partial blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON([ |
||||
'google.com', |
||||
{ url: 'github.com', keys: ['j', 'k'] }, |
||||
]); |
||||
|
||||
expect(blacklist.includeKey(new URL('https://google.com'), 'j')).to.be.true; |
||||
expect(blacklist.includeKey(new URL('https://github.com'), 'j')).to.be.true; |
||||
expect(blacklist.includeKey(new URL('https://github.com'), 'a')).to.be.false; |
||||
}); |
||||
}); |
||||
}); |
||||
|
Reference in new issue