parent
574692551a
commit
b86b4680b6
12 changed files with 139 additions and 149 deletions
@ -1,13 +0,0 @@ |
|||||||
import * as re from './utils/re'; |
|
||||||
|
|
||||||
const includes = (blacklist: string[], url: string): boolean => { |
|
||||||
let u = new URL(url); |
|
||||||
return blacklist.some((item) => { |
|
||||||
if (!item.includes('/')) { |
|
||||||
return re.fromWildcard(item).test(u.host); |
|
||||||
} |
|
||||||
return re.fromWildcard(item).test(u.host + u.pathname); |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
export { includes }; |
|
@ -0,0 +1,39 @@ |
|||||||
|
export type BlacklistJSON = string[]; |
||||||
|
|
||||||
|
const fromWildcard = (pattern: string): RegExp => { |
||||||
|
let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; |
||||||
|
return new RegExp(regexStr); |
||||||
|
}; |
||||||
|
|
||||||
|
export default class Blacklist { |
||||||
|
constructor( |
||||||
|
private blacklist: string[], |
||||||
|
) { |
||||||
|
} |
||||||
|
|
||||||
|
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`); |
||||||
|
} |
||||||
|
} |
||||||
|
return new Blacklist(json); |
||||||
|
} |
||||||
|
|
||||||
|
toJSON(): BlacklistJSON { |
||||||
|
return this.blacklist; |
||||||
|
} |
||||||
|
|
||||||
|
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); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -1,6 +0,0 @@ |
|||||||
const fromWildcard = (pattern: string): RegExp => { |
|
||||||
let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; |
|
||||||
return new RegExp(regexStr); |
|
||||||
}; |
|
||||||
|
|
||||||
export { fromWildcard }; |
|
@ -1,49 +0,0 @@ |
|||||||
import { includes } from 'shared/blacklists'; |
|
||||||
|
|
||||||
describe("shared/blacklist", () => { |
|
||||||
it('matches by *', () => { |
|
||||||
let blacklist = ['*']; |
|
||||||
|
|
||||||
expect(includes(blacklist, 'https://github.com/abc')).to.be.true; |
|
||||||
}) |
|
||||||
|
|
||||||
it('matches by hostname', () => { |
|
||||||
let blacklist = ['github.com']; |
|
||||||
|
|
||||||
expect(includes(blacklist, 'https://github.com')).to.be.true; |
|
||||||
expect(includes(blacklist, 'https://gist.github.com')).to.be.false; |
|
||||||
expect(includes(blacklist, 'https://github.com/ueokande')).to.be.true; |
|
||||||
expect(includes(blacklist, 'https://github.org')).to.be.false; |
|
||||||
expect(includes(blacklist, 'https://google.com/search?q=github.org')).to.be.false; |
|
||||||
}) |
|
||||||
|
|
||||||
it('matches by hostname with wildcard', () => { |
|
||||||
let blacklist = ['*.github.com']; |
|
||||||
|
|
||||||
expect(includes(blacklist, 'https://github.com')).to.be.false; |
|
||||||
expect(includes(blacklist, 'https://gist.github.com')).to.be.true; |
|
||||||
}) |
|
||||||
|
|
||||||
it('matches by path', () => { |
|
||||||
let blacklist = ['github.com/abc']; |
|
||||||
|
|
||||||
expect(includes(blacklist, 'https://github.com/abc')).to.be.true; |
|
||||||
expect(includes(blacklist, 'https://github.com/abcdef')).to.be.false; |
|
||||||
expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; |
|
||||||
}) |
|
||||||
|
|
||||||
it('matches by path with wildcard', () => { |
|
||||||
let blacklist = ['github.com/abc*']; |
|
||||||
|
|
||||||
expect(includes(blacklist, 'https://github.com/abc')).to.be.true; |
|
||||||
expect(includes(blacklist, 'https://github.com/abcdef')).to.be.true; |
|
||||||
expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; |
|
||||||
}) |
|
||||||
|
|
||||||
it('matches address and port', () => { |
|
||||||
let blacklist = ['127.0.0.1:8888']; |
|
||||||
|
|
||||||
expect(includes(blacklist, 'http://127.0.0.1:8888/')).to.be.true; |
|
||||||
expect(includes(blacklist, 'http://127.0.0.1:8888/hello')).to.be.true; |
|
||||||
}) |
|
||||||
}); |
|
@ -0,0 +1,77 @@ |
|||||||
|
import Blacklist 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; |
||||||
|
}); |
||||||
|
|
||||||
|
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('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); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe('#includes', () => { |
||||||
|
it('matches by *', () => { |
||||||
|
let blacklist = new Blacklist(['*']); |
||||||
|
|
||||||
|
expect(blacklist.includes('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; |
||||||
|
}); |
||||||
|
|
||||||
|
it('matches by hostname with wildcard', () => { |
||||||
|
let blacklist = new Blacklist(['*.github.com']); |
||||||
|
|
||||||
|
expect(blacklist.includes('https://github.com')).to.be.false; |
||||||
|
expect(blacklist.includes('https://gist.github.com')).to.be.true; |
||||||
|
}) |
||||||
|
|
||||||
|
it('matches by path', () => { |
||||||
|
let blacklist = new Blacklist(['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; |
||||||
|
}) |
||||||
|
|
||||||
|
it('matches by path with wildcard', () => { |
||||||
|
let blacklist = new Blacklist(['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; |
||||||
|
}) |
||||||
|
|
||||||
|
it('matches address and port', () => { |
||||||
|
let blacklist = new Blacklist(['127.0.0.1:8888']); |
||||||
|
|
||||||
|
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; |
||||||
|
}) |
||||||
|
}) |
||||||
|
}); |
@ -1,19 +0,0 @@ |
|||||||
import * as re from 'shared/utils/re'; |
|
||||||
|
|
||||||
describe("re util", () => { |
|
||||||
it('matches by pattern', () => { |
|
||||||
let regex = re.fromWildcard('*.example.com/*'); |
|
||||||
expect('foo.example.com/bar').to.match(regex); |
|
||||||
expect('foo.example.com').not.to.match(regex); |
|
||||||
expect('example.com/bar').not.to.match(regex); |
|
||||||
|
|
||||||
regex = re.fromWildcard('example.com/*') |
|
||||||
expect('example.com/foo').to.match(regex); |
|
||||||
expect('example.com/').to.match(regex); |
|
||||||
|
|
||||||
regex = re.fromWildcard('example.com/*bar') |
|
||||||
expect('example.com/foobar').to.match(regex); |
|
||||||
expect('example.com/bar').to.match(regex); |
|
||||||
expect('example.com/foobarfoo').not.to.match(regex); |
|
||||||
}) |
|
||||||
}); |
|
Reference in new issue