m3u8parse
Version:
Structural parsing of Apple HTTP Live Streaming .m3u8 format
34 lines (33 loc) • 1.32 kB
JavaScript
const { Buffer } = await import('node:' + 'buffer');
const { Stream } = await import('node:' + 'stream');
import { M3U8Parser, PlaylistType } from './parser.js';
import parseString from './index.js';
export * from './index.js';
const parseStream = async function (stream, options) {
const parser = new M3U8Parser(options);
stream.setEncoding('utf-8');
let saved = '';
for await (const input of stream) {
const lines = (saved + input).split(/\r?\n/);
let i;
for (i = 0; i < lines.length - 1; ++i) {
parser.feed(lines[i]);
}
saved = lines[i];
}
parser.feed(saved);
return parser.finalize(options.type);
};
export default function (input, options = {}) {
if (!(input instanceof Stream) && typeof input !== 'string' && !Buffer.isBuffer(input)) {
throw new TypeError('Passed input must be a stream, string, or buffer');
}
if (options.type && (options.type !== PlaylistType.Main && options.type !== PlaylistType.Media)) {
throw new TypeError(`Passed type must be "${PlaylistType.Main}" or "${PlaylistType.Media}"`);
}
if (input instanceof Stream) {
return parseStream(input, options);
}
input = Buffer.isBuffer(input) ? input.toString('utf-8') : input;
return parseString(input, options);
}