standardjs run

This commit is contained in:
boyska 2021-11-19 18:08:09 +01:00
parent c0b11c7218
commit 9fc6c6a74b
4 changed files with 108 additions and 116 deletions

View file

@ -1,34 +1,34 @@
module.exports = function (config) { module.exports = function (config) {
config.set({ config.set({
frameworks: ['mocha'], frameworks: ['mocha'],
// plugins: ['karma-webpack', 'karma-mocha', 'karma-chai-as-promised'], // plugins: ['karma-webpack', 'karma-mocha', 'karma-chai-as-promised'],
webpack: { webpack: {
// karma watches the test entry points // karma watches the test entry points
// Do NOT specify the entry option // Do NOT specify the entry option
// webpack watches dependencies // webpack watches dependencies
// webpack configuration // webpack configuration
}, },
preprocessors: { preprocessors: {
'test/**/*.js': ['webpack'], 'test/**/*.js': ['webpack'],
'radiomanifest.js': ['webpack'] 'radiomanifest.js': ['webpack']
}, },
files: [ files: [
'radiomanifest.js', 'radiomanifest.js',
'test/**/*.js' 'test/**/*.js'
], ],
reporters: ['progress'], reporters: ['progress'],
port: 9876, // karma web server port port: 9876, // karma web server port
colors: true, colors: true,
logLevel: config.LOG_INFO, logLevel: config.LOG_INFO,
browsers: ['ChromeHeadless', 'FirefoxHeadless'], browsers: ['ChromeHeadless', 'FirefoxHeadless'],
autoWatch: false, autoWatch: false,
concurrency: Infinity, concurrency: Infinity,
customLaunchers: { customLaunchers: {
FirefoxHeadless: { FirefoxHeadless: {
base: 'Firefox', base: 'Firefox',
flags: ['-headless'], flags: ['-headless']
}, }
}, }
}) })
} }

View file

@ -26,137 +26,134 @@ class RadioManifest {
} }
} }
function getStreaminfoUrl (siteurl) {
function getStreaminfoUrl(siteurl) { return siteurl + '/streaminfo.json' // XXX: improve this logic
return siteurl + '/streaminfo.json'; // XXX: improve this logic
} }
function getManifestUrl(siteurl) { function getManifestUrl (siteurl) {
return siteurl + '/radiomanifest.xml'; // XXX: improve this logic return siteurl + '/radiomanifest.xml' // XXX: improve this logic
} }
function parseRadioManifest(xml) { function parseRadioManifest (xml) {
var res = xml.evaluate('/radio-manifest/streaming/source', xml) let res = xml.evaluate('/radio-manifest/streaming/source', xml)
var sources = [] const sources = []
while(true) { while (true) {
var src = res.iterateNext() const src = res.iterateNext()
if(src === null) break; if (src === null) break
if(!src.hasAttribute("priority")) { if (!src.hasAttribute('priority')) {
src.setAttribute("priority", "0") src.setAttribute('priority', '0')
} else if(parseInt(src.getAttribute("priority"), 10) < 0) { } else if (parseInt(src.getAttribute('priority'), 10) < 0) {
continue; continue
} }
sources.push(src) sources.push(src)
} }
sources.sort(function cmp(a,b) { sources.sort(function cmp (a, b) {
return parseInt(a.getAttribute("priority", 10)) < parseInt(b.getAttribute("priority", 10)); return parseInt(a.getAttribute('priority', 10)) < parseInt(b.getAttribute('priority', 10))
}) })
res = xml.evaluate('/radio-manifest/schedule', xml) res = xml.evaluate('/radio-manifest/schedule', xml)
var scheduleEl = res.iterateNext() const scheduleEl = res.iterateNext()
var schedule = null let schedule = null
if(scheduleEl !== null) { if (scheduleEl !== null) {
schedule = scheduleEl.getAttribute("url") schedule = scheduleEl.getAttribute('url')
} }
res = xml.evaluate('/radio-manifest/shows', xml) res = xml.evaluate('/radio-manifest/shows', xml)
var showsEl = res.iterateNext() const showsEl = res.iterateNext()
var shows = null let shows = null
if(showsEl !== null) { if (showsEl !== null) {
shows = showsEl.getAttribute("src") shows = showsEl.getAttribute('src')
} }
var manifest = new Radio(sources, schedule, shows) const manifest = new Radio(sources, schedule, shows)
return manifest return manifest
} }
function Radio(sources, schedule, shows) { function Radio (sources, schedule, shows) {
this.streaming = new RadioStreaming(sources) this.streaming = new RadioStreaming(sources)
this.schedule = schedule this.schedule = schedule
this.shows = shows this.shows = shows
this.name = "" this.name = ''
} }
Radio.prototype.getStreaming = function() { Radio.prototype.getStreaming = function () {
return this.streaming return this.streaming
} }
Radio.prototype.setName = function(name) { Radio.prototype.setName = function (name) {
this.name = name this.name = name
} }
function RadioStreaming(sources) { function RadioStreaming (sources) {
this.sources = sources this.sources = sources
} }
RadioStreaming.prototype.getOptions = function() { RadioStreaming.prototype.getOptions = function () {
return this.sources.map(function(x) { return this.sources.map(function (x) {
return x.getAttribute('name') return x.getAttribute('name')
}) })
} }
RadioStreaming.prototype.getSource = function (name) { RadioStreaming.prototype.getSource = function (name) {
if(name === undefined) { if (name === undefined) {
} }
var s = this.sources.find(function(x) { const s = this.sources.find(function (x) {
return x.getAttribute('name') === name return x.getAttribute('name') === name
}) })
if(s === undefined) return s if (s === undefined) return s
return s.getAttribute('src') return s.getAttribute('src')
} }
async function get(siteurl, options) { async function get (siteurl, options) {
let resp = await fetch(getManifestUrl(siteurl)); let resp = await fetch(getManifestUrl(siteurl))
let text = await resp.text() let text = await resp.text()
var parser = new DOMParser(); const parser = new DOMParser()
var dom = parser.parseFromString(text, 'text/xml') const dom = parser.parseFromString(text, 'text/xml')
var manifest = parseRadioManifest(dom) const manifest = parseRadioManifest(dom)
resp = null resp = null
try { try {
resp = await fetch(getStreaminfoUrl(siteurl)); resp = await fetch(getStreaminfoUrl(siteurl))
text = await resp.text() text = await resp.text()
var data = JSON.parse(text) const data = JSON.parse(text)
var name = data["icy-name"] const name = data['icy-name']
if(name !== undefined) { if (name !== undefined) {
manifest.setName(name) manifest.setName(name)
} }
} catch (e) { } catch (e) {
if(e instanceof TypeError && e.message.startsWith('NetworkError')) { if (e instanceof TypeError && e.message.startsWith('NetworkError')) {
// the fetch has failed // the fetch has failed
true true
} else if(e instanceof SyntaxError && e.message.startsWith('JSON.parse')) { } else if (e instanceof SyntaxError && e.message.startsWith('JSON.parse')) {
true true
} } else {
else {
console.error('Error', e) console.error('Error', e)
throw e throw e
} }
} }
// XXX: in base alle options fai fetch anche di altra roba // XXX: in base alle options fai fetch anche di altra roba
return manifest return manifest
} }
function parseM3U(body) { function parseM3U (body) {
body.split("\n").filter((e) => { body.split('\n').filter((e) => {
if(e.startsWith("#")) { if (e.startsWith('#')) {
return false return false
} else { } else {
try { new URL(e); return true } try { new URL(e); return true } catch { return false }
catch {return false}
} }
}) })
} }
module.exports = { module.exports = {
get: get, get: get,
objs: { objs: {
Radio: Radio, Radio: Radio,
RadioStreaming: RadioStreaming RadioStreaming: RadioStreaming
}, },
parsers: { parsers: {
M3U: parseM3U, M3U: parseM3U,
radioManifest: parseRadioManifest, radioManifest: parseRadioManifest
} }
} }

View file

@ -5,9 +5,7 @@ chai.use(require('chai-as-promised'))
const expect = chai.expect const expect = chai.expect
describe('radiomanifest.js', () => { describe('radiomanifest.js', () => {
describe('Get a radiomanifest', () => { describe('Get a radiomanifest', () => {
it('should return a Promise', () => { it('should return a Promise', () => {
const p = radiomanifest.get('http://omstring') const p = radiomanifest.get('http://omstring')
expect(p instanceof Promise).to.be.eql(true) expect(p instanceof Promise).to.be.eql(true)
@ -17,7 +15,6 @@ describe('radiomanifest.js', () => {
const p = radiomanifest.get('http://invalidurl') const p = radiomanifest.get('http://invalidurl')
expect(p).to.eventually.be.rejected expect(p).to.eventually.be.rejected
}) })
}) })
describe('streaming', () => { describe('streaming', () => {
@ -25,7 +22,4 @@ describe('radiomanifest.js', () => {
}) })
}) })
}) })

15
ui.js
View file

@ -1,10 +1,11 @@
async function fai() { // const radiomanifest = require('radiomanifest.js')
var radio = await get("https://www.ondarossa.info/") async function fai () {
// var radio = await get("https://boyska.degenerazione.xyz/radiomanifest/examples/empty/") const radio = await radiomanifest.get('https://www.ondarossa.info/')
console.log('radio?', radio) // var radio = await get("https://boyska.degenerazione.xyz/radiomanifest/examples/empty/")
var s = radio.getStreaming() console.log('radio?', radio)
console.log(s.getOptions()) const s = radio.getStreaming()
console.log(s.getSource(s.getOptions()[0])) console.log(s.getOptions())
console.log(s.getSource(s.getOptions()[0]))
} }
fai() fai()