m3u8parse
Version:
Structural parsing of Apple HTTP Live Streaming .m3u8 format
42 lines (41 loc) • 1.84 kB
JavaScript
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
});
}
return path;
};
const { Buffer } = await import(__rewriteRelativeImportExtension('node:' + 'buffer'));
const { Stream } = await import(__rewriteRelativeImportExtension('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);
}