forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattr-list.ts
executable file
·193 lines (174 loc) · 5.31 KB
/
attr-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { LevelDetails } from '../loader/level-details';
import type { ParsedMultivariantPlaylist } from '../loader/m3u8-parser';
import { logger } from './logger';
import { substituteVariables } from './variable-substitution';
const DECIMAL_RESOLUTION_REGEX = /^(\d+)x(\d+)$/;
const ATTR_LIST_REGEX = /(.+?)=(".*?"|.*?)(?:,|$)/g;
// adapted from https://github.com/kanongil/node-m3u8parse/blob/master/attrlist.js
export class AttrList {
[key: string]: any;
constructor(
attrs: string | Record<string, any>,
parsed?: Pick<
ParsedMultivariantPlaylist | LevelDetails,
'variableList' | 'hasVariableRefs' | 'playlistParsingError'
>,
) {
if (typeof attrs === 'string') {
attrs = AttrList.parseAttrList(attrs, parsed);
}
Object.assign(this, attrs);
}
get clientAttrs(): string[] {
return Object.keys(this).filter((attr) => attr.substring(0, 2) === 'X-');
}
decimalInteger(attrName: string): number {
const intValue = parseInt(this[attrName], 10);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return intValue;
}
hexadecimalInteger(attrName: string) {
if (this[attrName]) {
let stringValue = (this[attrName] || '0x').slice(2);
stringValue = (stringValue.length & 1 ? '0' : '') + stringValue;
const value = new Uint8Array(stringValue.length / 2);
for (let i = 0; i < stringValue.length / 2; i++) {
value[i] = parseInt(stringValue.slice(i * 2, i * 2 + 2), 16);
}
return value;
} else {
return null;
}
}
hexadecimalIntegerAsNumber(attrName: string): number {
const intValue = parseInt(this[attrName], 16);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return intValue;
}
decimalFloatingPoint(attrName: string): number {
return parseFloat(this[attrName]);
}
optionalFloat(attrName: string, defaultValue: number): number {
const value = this[attrName];
return value ? parseFloat(value) : defaultValue;
}
enumeratedString(attrName: string): string | undefined {
return this[attrName];
}
enumeratedStringList<T extends { [key: string]: boolean }>(
attrName: string,
dict: T,
): { [key in keyof T]: boolean } {
const attrValue = this[attrName];
return (attrValue ? attrValue.split(/[ ,]+/) : []).reduce(
(result: { [key in keyof T]: boolean }, identifier: string) => {
result[identifier.toLowerCase() as keyof T] = true;
return result;
},
dict,
);
}
bool(attrName: string): boolean {
return this[attrName] === 'YES';
}
decimalResolution(attrName: string):
| {
width: number;
height: number;
}
| undefined {
const res = DECIMAL_RESOLUTION_REGEX.exec(this[attrName]);
if (res === null) {
return undefined;
}
return {
width: parseInt(res[1], 10),
height: parseInt(res[2], 10),
};
}
static parseAttrList(
input: string,
parsed?: Pick<
ParsedMultivariantPlaylist | LevelDetails,
'variableList' | 'hasVariableRefs' | 'playlistParsingError'
>,
): Record<string, string> {
let match: RegExpExecArray | null;
const attrs = {};
const quote = '"';
ATTR_LIST_REGEX.lastIndex = 0;
while ((match = ATTR_LIST_REGEX.exec(input)) !== null) {
const name = match[1].trim();
let value = match[2];
const quotedString =
value.indexOf(quote) === 0 &&
value.lastIndexOf(quote) === value.length - 1;
let hexadecimalSequence = false;
if (quotedString) {
value = value.slice(1, -1);
} else {
switch (name) {
case 'IV':
case 'SCTE35-CMD':
case 'SCTE35-IN':
case 'SCTE35-OUT':
hexadecimalSequence = true;
}
}
if (parsed && (quotedString || hexadecimalSequence)) {
if (__USE_VARIABLE_SUBSTITUTION__) {
value = substituteVariables(parsed, value);
}
} else if (!hexadecimalSequence && !quotedString) {
switch (name) {
case 'CLOSED-CAPTIONS':
if (value === 'NONE') {
break;
}
// falls through
case 'ALLOWED-CPC':
case 'CLASS':
case 'ASSOC-LANGUAGE':
case 'AUDIO':
case 'BYTERANGE':
case 'CHANNELS':
case 'CHARACTERISTICS':
case 'CODECS':
case 'DATA-ID':
case 'END-DATE':
case 'GROUP-ID':
case 'ID':
case 'IMPORT':
case 'INSTREAM-ID':
case 'KEYFORMAT':
case 'KEYFORMATVERSIONS':
case 'LANGUAGE':
case 'NAME':
case 'PATHWAY-ID':
case 'QUERYPARAM':
case 'RECENTLY-REMOVED-DATERANGES':
case 'SERVER-URI':
case 'STABLE-RENDITION-ID':
case 'STABLE-VARIANT-ID':
case 'START-DATE':
case 'SUBTITLES':
case 'SUPPLEMENTAL-CODECS':
case 'URI':
case 'VALUE':
case 'VIDEO':
case 'X-ASSET-LIST':
case 'X-ASSET-URI':
// Since we are not checking tag:attribute combination, just warn rather than ignoring attribute
logger.warn(`${input}: attribute ${name} is missing quotes`);
// continue;
}
}
attrs[name] = value;
}
return attrs;
}
}