refactor(hls): [PKG-HLS-03] separate menus and SDK lifecycle

This commit is contained in:
Harvey Zhao committed 2026-09-12 00:18:19 +08:00
1 parent 33f6d19b77
commit ba93b98be5
24 files changed
+5849 -382

No files matched your search

+37
View File
@@ -1,6 +1,8 @@
import assert from 'node:assert/strict'
import { Buffer } from 'node:buffer'
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import vm from 'node:vm'
import { ensureArchive, hash, readMember } from '../../refactor/scripts/releases.mjs'
import { loadPackage } from './load.js'
@@ -26,6 +28,21 @@ export async function hlsImplementations() {
assert.equal(typeof factory, 'function')
implementations.push({ name: `published-${format}`, factory })
}
for (const filename of (process.env.ARTPLAYER_TEST_HLS || '').split(path.delimiter).filter(Boolean)) {
const bytes = fs.readFileSync(filename)
let factory
if (filename.endsWith('.mjs')) {
factory = (await import(`data:text/javascript;base64,${bytes.toString('base64')}`)).default
}
else {
const context = { module: { exports: {} } }
context.exports = context.module.exports
vm.runInNewContext(bytes.toString(), context, { timeout: 5000 })
factory = context.module.exports
}
assert.equal(typeof factory, 'function')
implementations.push({ name: `artifact-${path.basename(filename)}`, factory })
}
return implementations
}
@@ -102,3 +119,23 @@ export function hlsHost() {
export const bothMenus = () => ({ quality: { control: true, setting: true }, audio: { control: true, setting: true } })
export const selections = menu => Array.from(menu.selector, item => [item.html, item.value, item.default])
export function attachHlsEvents(hls) {
const listeners = new Map()
const Events = Object.fromEntries(['MANIFEST_PARSED', 'LEVELS_UPDATED', 'LEVEL_SWITCHED', 'AUDIO_TRACKS_UPDATED', 'AUDIO_TRACK_SWITCHED', 'DESTROYING'].map(key => [key, key]))
hls.constructor = { Events }
hls.on = (name, callback) => {
if (!listeners.has(name))
listeners.set(name, new Set())
listeners.get(name).add(callback)
}
hls.off = (name, callback) => listeners.get(name)?.delete(callback)
return {
listeners,
emit(name) {
for (const callback of [...(listeners.get(name) || [])])
callback(name, {})
},
count: () => [...listeners.values()].reduce((total, entries) => total + entries.size, 0),
}
}