68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
import 'colors'
|
|
import 'tagol'
|
|
import fs from 'fs'
|
|
|
|
const empty = o=> typeof o == 'string' ? o.trim() != '' : o.length > 0
|
|
export const domains = []
|
|
|
|
export default function( file, watch = true )
|
|
{
|
|
load( file )
|
|
|
|
watch && fs.watchFile( file, (curr, prev) => {
|
|
log `The domain list file changed at: ${curr.mtime}s`
|
|
domains.splice(0)
|
|
load( file )
|
|
})
|
|
|
|
return domains
|
|
}
|
|
|
|
function load( path )
|
|
{
|
|
log `Loading the domains file ${path.yellow}s...`
|
|
|
|
parse( fs.readFileSync(path, 'utf-8') )
|
|
.map( o=> domains.push(o) )
|
|
}
|
|
|
|
var currentDomain
|
|
function transform( line )
|
|
{
|
|
let res
|
|
if( line.length == 1 )
|
|
{
|
|
currentDomain = line.pop()
|
|
return ''
|
|
}
|
|
if( line.length && line[0][line[0].length-1] == '.' )
|
|
{
|
|
line[0] = line[0] == '.'
|
|
? currentDomain
|
|
: line[0] + currentDomain
|
|
// line[0] = line[0].replace(/\.$/g, '') + currentDomain
|
|
// line[0] = line[0].substring(0,line[0].length-1)
|
|
// line.splice(1,0,currentDomain)
|
|
}
|
|
//line.test = line[0][0] == line[0][line[0].length-1] == '/'
|
|
line.test = ( res = /^\/(.*)\/$/.exec(line[0]) )
|
|
? new RegExp( res[1] )
|
|
: new RegExp( '^' + line[0].replace(/\*/g, '(.*?)') + '$' )
|
|
line.resolve = ( subDomain, domain, tld, match, path='PPP' )=> eval('`'+line[1]+'`')
|
|
return line
|
|
}
|
|
|
|
function parse( text )
|
|
{
|
|
return text
|
|
// .replace(/\/\/.*?$/mg,'') // cut out comments
|
|
.replace(/(^|\s)#.*?$/mg,'$1') // cut out comments
|
|
.split('\n')
|
|
.filter( empty )
|
|
.map( line=> line.split(/\s+/).filter(empty) )
|
|
.map( transform )
|
|
.filter( empty )
|
|
|
|
}
|
|
|