m3u8parse
Version:
Structural parsing of Apple HTTP Live Streaming .m3u8 format
66 lines (65 loc) • 2.05 kB
JavaScript
import { Attr } from './attr-types.js';
const deserialize = {
[Attr.BigInt](stringValue) {
const intValue = BigInt(stringValue);
if (/^\s*0[^\d]/.test(stringValue)) {
throw new SyntaxError('Representation is not decimal integer compatible');
}
return intValue;
},
[Attr.HexInt](stringValue) {
const intValue = BigInt(stringValue);
if (!/^\s*0x/.test(stringValue)) {
throw new SyntaxError('Representation is not hexadecimal integer compatible');
}
return intValue;
},
[Attr.Int](stringValue) {
const intValue = parseInt(stringValue, 10);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Number.POSITIVE_INFINITY;
}
return intValue;
},
[Attr.HexNo](stringValue) {
const intValue = parseInt(stringValue, 16);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Number.POSITIVE_INFINITY;
}
return intValue;
},
[Attr.Float](stringValue) {
return parseFloat(stringValue);
},
[Attr.SignedFloat](stringValue) {
return parseFloat(stringValue);
},
[Attr.String](stringValue) {
return stringValue.slice(1, -1);
},
[Attr.Enum](stringValue) {
return stringValue;
},
[Attr.List](stringValue) {
const list = deserialize[Attr.String](stringValue);
return list.split(',');
},
[Attr.Resolution](stringValue) {
const res = /^(\d+)x(\d+)$/.exec(stringValue);
if (res === null) {
return undefined;
}
return { width: parseInt(res[1], 10), height: parseInt(res[2], 10) };
},
[Attr.Byterange](stringValue) {
const res = /^"?(\d+)(?:@(\d+))?"?$/.exec(stringValue);
if (res === null) {
return undefined;
}
return {
offset: res[2] !== undefined ? parseInt(res[2], 10) : undefined,
length: parseInt(res[1], 10)
};
}
};
export default deserialize;