@transmitlive/m3u8-parser 4.7.2-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/CONTRIBUTING.md +30 -0
  2. package/LICENSE +13 -0
  3. package/README.md +388 -0
  4. package/dist/m3u8-parser.cjs.js +1563 -0
  5. package/dist/m3u8-parser.es.js +1549 -0
  6. package/dist/m3u8-parser.js +1748 -0
  7. package/dist/m3u8-parser.min.js +2 -0
  8. package/index.html +15 -0
  9. package/package.json +100 -0
  10. package/scripts/karma.conf.js +12 -0
  11. package/scripts/rollup.config.js +47 -0
  12. package/src/index.js +19 -0
  13. package/src/line-stream.js +35 -0
  14. package/src/parse-stream.js +619 -0
  15. package/src/parser.js +748 -0
  16. package/test/fixtures/integration/absoluteUris.js +31 -0
  17. package/test/fixtures/integration/absoluteUris.m3u8 +12 -0
  18. package/test/fixtures/integration/allowCache.js +165 -0
  19. package/test/fixtures/integration/allowCache.m3u8 +58 -0
  20. package/test/fixtures/integration/allowCacheInvalid.js +21 -0
  21. package/test/fixtures/integration/allowCacheInvalid.m3u8 +10 -0
  22. package/test/fixtures/integration/alternateAudio.js +56 -0
  23. package/test/fixtures/integration/alternateAudio.m3u8 +9 -0
  24. package/test/fixtures/integration/alternateVideo.js +48 -0
  25. package/test/fixtures/integration/alternateVideo.m3u8 +8 -0
  26. package/test/fixtures/integration/brightcove.js +57 -0
  27. package/test/fixtures/integration/brightcove.m3u8 +9 -0
  28. package/test/fixtures/integration/byteRange.js +161 -0
  29. package/test/fixtures/integration/byteRange.m3u8 +56 -0
  30. package/test/fixtures/integration/dateTime.js +27 -0
  31. package/test/fixtures/integration/dateTime.m3u8 +12 -0
  32. package/test/fixtures/integration/diff-init-key.js +164 -0
  33. package/test/fixtures/integration/diff-init-key.m3u8 +57 -0
  34. package/test/fixtures/integration/disallowCache.js +21 -0
  35. package/test/fixtures/integration/disallowCache.m3u8 +10 -0
  36. package/test/fixtures/integration/disc-sequence.js +32 -0
  37. package/test/fixtures/integration/disc-sequence.m3u8 +15 -0
  38. package/test/fixtures/integration/discontinuity.js +59 -0
  39. package/test/fixtures/integration/discontinuity.m3u8 +26 -0
  40. package/test/fixtures/integration/domainUris.js +31 -0
  41. package/test/fixtures/integration/domainUris.m3u8 +12 -0
  42. package/test/fixtures/integration/empty.js +5 -0
  43. package/test/fixtures/integration/empty.m3u8 +0 -0
  44. package/test/fixtures/integration/emptyAllowCache.js +21 -0
  45. package/test/fixtures/integration/emptyAllowCache.m3u8 +10 -0
  46. package/test/fixtures/integration/emptyMediaSequence.js +31 -0
  47. package/test/fixtures/integration/emptyMediaSequence.m3u8 +14 -0
  48. package/test/fixtures/integration/emptyPlaylistType.js +40 -0
  49. package/test/fixtures/integration/emptyPlaylistType.m3u8 +16 -0
  50. package/test/fixtures/integration/emptyTargetDuration.js +57 -0
  51. package/test/fixtures/integration/emptyTargetDuration.m3u8 +10 -0
  52. package/test/fixtures/integration/encrypted.js +61 -0
  53. package/test/fixtures/integration/encrypted.m3u8 +28 -0
  54. package/test/fixtures/integration/event.js +41 -0
  55. package/test/fixtures/integration/event.m3u8 +16 -0
  56. package/test/fixtures/integration/extXPlaylistTypeInvalidPlaylist.js +15 -0
  57. package/test/fixtures/integration/extXPlaylistTypeInvalidPlaylist.m3u8 +8 -0
  58. package/test/fixtures/integration/extinf.js +165 -0
  59. package/test/fixtures/integration/extinf.m3u8 +57 -0
  60. package/test/fixtures/integration/fmp4.js +44 -0
  61. package/test/fixtures/integration/fmp4.m3u8 +14 -0
  62. package/test/fixtures/integration/headerOnly.js +5 -0
  63. package/test/fixtures/integration/headerOnly.m3u8 +1 -0
  64. package/test/fixtures/integration/invalidAllowCache.js +21 -0
  65. package/test/fixtures/integration/invalidAllowCache.m3u8 +10 -0
  66. package/test/fixtures/integration/invalidMediaSequence.js +31 -0
  67. package/test/fixtures/integration/invalidMediaSequence.m3u8 +14 -0
  68. package/test/fixtures/integration/invalidPlaylistType.js +40 -0
  69. package/test/fixtures/integration/invalidPlaylistType.m3u8 +16 -0
  70. package/test/fixtures/integration/invalidTargetDuration.js +164 -0
  71. package/test/fixtures/integration/invalidTargetDuration.m3u8 +57 -0
  72. package/test/fixtures/integration/liveMissingSegmentDuration.js +25 -0
  73. package/test/fixtures/integration/liveMissingSegmentDuration.m3u8 +9 -0
  74. package/test/fixtures/integration/liveStart30sBefore.js +54 -0
  75. package/test/fixtures/integration/liveStart30sBefore.m3u8 +22 -0
  76. package/test/fixtures/integration/llhls-byte-range.js +253 -0
  77. package/test/fixtures/integration/llhls-byte-range.m3u8 +66 -0
  78. package/test/fixtures/integration/llhls-delta-byte-range.js +149 -0
  79. package/test/fixtures/integration/llhls-delta-byte-range.m3u8 +30 -0
  80. package/test/fixtures/integration/llhls.js +214 -0
  81. package/test/fixtures/integration/llhls.m3u8 +56 -0
  82. package/test/fixtures/integration/llhlsDelta.js +186 -0
  83. package/test/fixtures/integration/llhlsDelta.m3u8 +50 -0
  84. package/test/fixtures/integration/manifestExtTTargetdurationNegative.js +14 -0
  85. package/test/fixtures/integration/manifestExtTTargetdurationNegative.m3u8 +5 -0
  86. package/test/fixtures/integration/manifestExtXEndlistEarly.js +35 -0
  87. package/test/fixtures/integration/manifestExtXEndlistEarly.m3u8 +14 -0
  88. package/test/fixtures/integration/manifestNoExtM3u.js +15 -0
  89. package/test/fixtures/integration/manifestNoExtM3u.m3u8 +4 -0
  90. package/test/fixtures/integration/master-fmp4.js +465 -0
  91. package/test/fixtures/integration/master-fmp4.m3u8 +76 -0
  92. package/test/fixtures/integration/master.js +57 -0
  93. package/test/fixtures/integration/master.m3u8 +10 -0
  94. package/test/fixtures/integration/media.js +31 -0
  95. package/test/fixtures/integration/media.m3u8 +12 -0
  96. package/test/fixtures/integration/mediaSequence.js +31 -0
  97. package/test/fixtures/integration/mediaSequence.m3u8 +14 -0
  98. package/test/fixtures/integration/missingEndlist.js +19 -0
  99. package/test/fixtures/integration/missingEndlist.m3u8 +6 -0
  100. package/test/fixtures/integration/missingExtinf.js +27 -0
  101. package/test/fixtures/integration/missingExtinf.m3u8 +11 -0
  102. package/test/fixtures/integration/missingMediaSequence.js +31 -0
  103. package/test/fixtures/integration/missingMediaSequence.m3u8 +13 -0
  104. package/test/fixtures/integration/missingSegmentDuration.js +31 -0
  105. package/test/fixtures/integration/missingSegmentDuration.m3u8 +11 -0
  106. package/test/fixtures/integration/multipleAudioGroups.js +89 -0
  107. package/test/fixtures/integration/multipleAudioGroups.m3u8 +17 -0
  108. package/test/fixtures/integration/multipleAudioGroupsCombinedMain.js +88 -0
  109. package/test/fixtures/integration/multipleAudioGroupsCombinedMain.m3u8 +17 -0
  110. package/test/fixtures/integration/multipleTargetDurations.js +28 -0
  111. package/test/fixtures/integration/multipleTargetDurations.m3u8 +8 -0
  112. package/test/fixtures/integration/multipleVideo.js +74 -0
  113. package/test/fixtures/integration/multipleVideo.m3u8 +16 -0
  114. package/test/fixtures/integration/negativeMediaSequence.js +31 -0
  115. package/test/fixtures/integration/negativeMediaSequence.m3u8 +14 -0
  116. package/test/fixtures/integration/playlist.js +165 -0
  117. package/test/fixtures/integration/playlist.m3u8 +57 -0
  118. package/test/fixtures/integration/playlistMediaSequenceHigher.js +16 -0
  119. package/test/fixtures/integration/playlistMediaSequenceHigher.m3u8 +8 -0
  120. package/test/fixtures/integration/start.js +36 -0
  121. package/test/fixtures/integration/start.m3u8 +13 -0
  122. package/test/fixtures/integration/streamInfInvalid.js +24 -0
  123. package/test/fixtures/integration/streamInfInvalid.m3u8 +6 -0
  124. package/test/fixtures/integration/twoMediaSequences.js +31 -0
  125. package/test/fixtures/integration/twoMediaSequences.m3u8 +15 -0
  126. package/test/fixtures/integration/versionInvalid.js +16 -0
  127. package/test/fixtures/integration/versionInvalid.m3u8 +8 -0
  128. package/test/fixtures/integration/whiteSpace.js +31 -0
  129. package/test/fixtures/integration/whiteSpace.m3u8 +13 -0
  130. package/test/fixtures/integration/zeroDuration.js +16 -0
  131. package/test/fixtures/integration/zeroDuration.m3u8 +7 -0
  132. package/test/line-stream.test.js +80 -0
  133. package/test/parse-stream.test.js +903 -0
  134. package/test/parser.test.js +884 -0
@@ -0,0 +1,1549 @@
1
+ /*! @name @transmitlive/m3u8-parser @version 4.7.2-beta.6 @license Apache-2.0 */
2
+ import _inheritsLoose from '@babel/runtime/helpers/inheritsLoose';
3
+ import Stream from '@videojs/vhs-utils/es/stream.js';
4
+ import _extends from '@babel/runtime/helpers/extends';
5
+ import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
6
+ import decodeB64ToUint8Array from '@videojs/vhs-utils/es/decode-b64-to-uint8-array.js';
7
+
8
+ /**
9
+ * A stream that buffers string input and generates a `data` event for each
10
+ * line.
11
+ *
12
+ * @class LineStream
13
+ * @extends Stream
14
+ */
15
+
16
+ var LineStream = /*#__PURE__*/function (_Stream) {
17
+ _inheritsLoose(LineStream, _Stream);
18
+
19
+ function LineStream() {
20
+ var _this;
21
+
22
+ _this = _Stream.call(this) || this;
23
+ _this.buffer = '';
24
+ return _this;
25
+ }
26
+ /**
27
+ * Add new data to be parsed.
28
+ *
29
+ * @param {string} data the text to process
30
+ */
31
+
32
+
33
+ var _proto = LineStream.prototype;
34
+
35
+ _proto.push = function push(data) {
36
+ var nextNewline;
37
+ this.buffer += data;
38
+ nextNewline = this.buffer.indexOf('\n');
39
+
40
+ for (; nextNewline > -1; nextNewline = this.buffer.indexOf('\n')) {
41
+ this.trigger('data', this.buffer.substring(0, nextNewline));
42
+ this.buffer = this.buffer.substring(nextNewline + 1);
43
+ }
44
+ };
45
+
46
+ return LineStream;
47
+ }(Stream);
48
+
49
+ var TAB = String.fromCharCode(0x09);
50
+
51
+ var parseByterange = function parseByterange(byterangeString) {
52
+ // optionally match and capture 0+ digits before `@`
53
+ // optionally match and capture 0+ digits after `@`
54
+ var match = /([0-9.]*)?@?([0-9.]*)?/.exec(byterangeString || '');
55
+ var result = {};
56
+
57
+ if (match[1]) {
58
+ result.length = parseInt(match[1], 10);
59
+ }
60
+
61
+ if (match[2]) {
62
+ result.offset = parseInt(match[2], 10);
63
+ }
64
+
65
+ return result;
66
+ };
67
+ /**
68
+ * "forgiving" attribute list psuedo-grammar:
69
+ * attributes -> keyvalue (',' keyvalue)*
70
+ * keyvalue -> key '=' value
71
+ * key -> [^=]*
72
+ * value -> '"' [^"]* '"' | [^,]*
73
+ */
74
+
75
+
76
+ var attributeSeparator = function attributeSeparator() {
77
+ var key = '[^=]*';
78
+ var value = '"[^"]*"|[^,]*';
79
+ var keyvalue = '(?:' + key + ')=(?:' + value + ')';
80
+ return new RegExp('(?:^|,)(' + keyvalue + ')');
81
+ };
82
+ /**
83
+ * Parse attributes from a line given the separator
84
+ *
85
+ * @param {string} attributes the attribute line to parse
86
+ */
87
+
88
+
89
+ var parseAttributes = function parseAttributes(attributes) {
90
+ // split the string using attributes as the separator
91
+ var attrs = attributes.split(attributeSeparator());
92
+ var result = {};
93
+ var i = attrs.length;
94
+ var attr;
95
+
96
+ while (i--) {
97
+ // filter out unmatched portions of the string
98
+ if (attrs[i] === '') {
99
+ continue;
100
+ } // split the key and value
101
+
102
+
103
+ attr = /([^=]*)=(.*)/.exec(attrs[i]).slice(1); // trim whitespace and remove optional quotes around the value
104
+
105
+ attr[0] = attr[0].replace(/^\s+|\s+$/g, '');
106
+ attr[1] = attr[1].replace(/^\s+|\s+$/g, '');
107
+ attr[1] = attr[1].replace(/^['"](.*)['"]$/g, '$1');
108
+ result[attr[0]] = attr[1];
109
+ }
110
+
111
+ return result;
112
+ };
113
+ /**
114
+ * A line-level M3U8 parser event stream. It expects to receive input one
115
+ * line at a time and performs a context-free parse of its contents. A stream
116
+ * interpretation of a manifest can be useful if the manifest is expected to
117
+ * be too large to fit comfortably into memory or the entirety of the input
118
+ * is not immediately available. Otherwise, it's probably much easier to work
119
+ * with a regular `Parser` object.
120
+ *
121
+ * Produces `data` events with an object that captures the parser's
122
+ * interpretation of the input. That object has a property `tag` that is one
123
+ * of `uri`, `comment`, or `tag`. URIs only have a single additional
124
+ * property, `line`, which captures the entirety of the input without
125
+ * interpretation. Comments similarly have a single additional property
126
+ * `text` which is the input without the leading `#`.
127
+ *
128
+ * Tags always have a property `tagType` which is the lower-cased version of
129
+ * the M3U8 directive without the `#EXT` or `#EXT-X-` prefix. For instance,
130
+ * `#EXT-X-MEDIA-SEQUENCE` becomes `media-sequence` when parsed. Unrecognized
131
+ * tags are given the tag type `unknown` and a single additional property
132
+ * `data` with the remainder of the input.
133
+ *
134
+ * @class ParseStream
135
+ * @extends Stream
136
+ */
137
+
138
+
139
+ var ParseStream = /*#__PURE__*/function (_Stream) {
140
+ _inheritsLoose(ParseStream, _Stream);
141
+
142
+ function ParseStream() {
143
+ var _this;
144
+
145
+ _this = _Stream.call(this) || this;
146
+ _this.customParsers = [];
147
+ _this.tagMappers = [];
148
+ _this.lineNumber = 0;
149
+ return _this;
150
+ }
151
+ /**
152
+ * Parses an additional line of input.
153
+ *
154
+ * @param {string} line a single line of an M3U8 file to parse
155
+ */
156
+
157
+
158
+ var _proto = ParseStream.prototype;
159
+
160
+ _proto.push = function push(line) {
161
+ var _this2 = this;
162
+
163
+ var match;
164
+ var event;
165
+ this.lineNumber = this.lineNumber + 1; // strip whitespace
166
+
167
+ line = line.trim();
168
+
169
+ if (line.length === 0) {
170
+ // ignore empty lines
171
+ return;
172
+ } // URIs
173
+
174
+
175
+ if (line[0] !== '#') {
176
+ this.trigger('data', {
177
+ type: 'uri',
178
+ uri: line
179
+ });
180
+ return;
181
+ } // map tags
182
+
183
+
184
+ var newLines = this.tagMappers.reduce(function (acc, mapper) {
185
+ var mappedLine = mapper(line); // skip if unchanged
186
+
187
+ if (mappedLine === line) {
188
+ return acc;
189
+ }
190
+
191
+ return acc.concat([mappedLine]);
192
+ }, [line]);
193
+ newLines.forEach(function (newLine) {
194
+ for (var i = 0; i < _this2.customParsers.length; i++) {
195
+ if (_this2.customParsers[i].call(_this2, newLine)) {
196
+ return;
197
+ }
198
+ } // Comments
199
+
200
+
201
+ if (newLine.indexOf('#EXT') !== 0) {
202
+ _this2.trigger('data', {
203
+ type: 'comment',
204
+ text: newLine.slice(1)
205
+ });
206
+
207
+ return;
208
+ } // strip off any carriage returns here so the regex matching
209
+ // doesn't have to account for them.
210
+
211
+
212
+ newLine = newLine.replace('\r', ''); // Tags
213
+
214
+ match = /^#EXTM3U/.exec(newLine);
215
+
216
+ if (match) {
217
+ _this2.trigger('data', {
218
+ type: 'tag',
219
+ tagType: 'm3u'
220
+ });
221
+
222
+ return;
223
+ }
224
+
225
+ match = /^#EXTINF:?([0-9\.]*)?,?(.*)?$/.exec(newLine);
226
+
227
+ if (match) {
228
+ event = {
229
+ type: 'tag',
230
+ tagType: 'inf'
231
+ };
232
+
233
+ if (match[1]) {
234
+ event.duration = parseFloat(match[1]);
235
+ }
236
+
237
+ if (match[2]) {
238
+ event.title = match[2];
239
+ }
240
+
241
+ _this2.trigger('data', event);
242
+
243
+ return;
244
+ }
245
+
246
+ match = /^#EXT-X-TARGETDURATION:?([0-9.]*)?/.exec(newLine);
247
+
248
+ if (match) {
249
+ event = {
250
+ type: 'tag',
251
+ tagType: 'targetduration'
252
+ };
253
+
254
+ if (match[1]) {
255
+ event.duration = parseInt(match[1], 10);
256
+ }
257
+
258
+ _this2.trigger('data', event);
259
+
260
+ return;
261
+ }
262
+
263
+ match = /^#EXT-X-VERSION:?([0-9.]*)?/.exec(newLine);
264
+
265
+ if (match) {
266
+ event = {
267
+ type: 'tag',
268
+ tagType: 'version'
269
+ };
270
+
271
+ if (match[1]) {
272
+ event.version = parseInt(match[1], 10);
273
+ }
274
+
275
+ _this2.trigger('data', event);
276
+
277
+ return;
278
+ }
279
+
280
+ match = /^#EXT-X-MEDIA-SEQUENCE:?(\-?[0-9.]*)?/.exec(newLine);
281
+
282
+ if (match) {
283
+ event = {
284
+ type: 'tag',
285
+ tagType: 'media-sequence'
286
+ };
287
+
288
+ if (match[1]) {
289
+ event.number = parseInt(match[1], 10);
290
+ }
291
+
292
+ _this2.trigger('data', event);
293
+
294
+ return;
295
+ }
296
+
297
+ match = /^#EXT-X-DISCONTINUITY-SEQUENCE:?(\-?[0-9.]*)?/.exec(newLine);
298
+
299
+ if (match) {
300
+ event = {
301
+ type: 'tag',
302
+ tagType: 'discontinuity-sequence'
303
+ };
304
+
305
+ if (match[1]) {
306
+ event.number = parseInt(match[1], 10);
307
+ }
308
+
309
+ _this2.trigger('data', event);
310
+
311
+ return;
312
+ }
313
+
314
+ match = /^#EXT-X-PLAYLIST-TYPE:?(.*)?$/.exec(newLine);
315
+
316
+ if (match) {
317
+ event = {
318
+ type: 'tag',
319
+ tagType: 'playlist-type'
320
+ };
321
+
322
+ if (match[1]) {
323
+ event.playlistType = match[1];
324
+ }
325
+
326
+ _this2.trigger('data', event);
327
+
328
+ return;
329
+ }
330
+
331
+ match = /^#EXT-X-BYTERANGE:?(.*)?$/.exec(newLine);
332
+
333
+ if (match) {
334
+ event = _extends(parseByterange(match[1]), {
335
+ type: 'tag',
336
+ tagType: 'byterange'
337
+ });
338
+
339
+ _this2.trigger('data', event);
340
+
341
+ return;
342
+ }
343
+
344
+ match = /^#EXT-X-ALLOW-CACHE:?(YES|NO)?/.exec(newLine);
345
+
346
+ if (match) {
347
+ event = {
348
+ type: 'tag',
349
+ tagType: 'allow-cache'
350
+ };
351
+
352
+ if (match[1]) {
353
+ event.allowed = !/NO/.test(match[1]);
354
+ }
355
+
356
+ _this2.trigger('data', event);
357
+
358
+ return;
359
+ }
360
+
361
+ match = /^#EXT-X-MAP:?(.*)$/.exec(newLine);
362
+
363
+ if (match) {
364
+ event = {
365
+ type: 'tag',
366
+ tagType: 'map'
367
+ };
368
+
369
+ if (match[1]) {
370
+ var attributes = parseAttributes(match[1]);
371
+
372
+ if (attributes.URI) {
373
+ event.uri = attributes.URI;
374
+ }
375
+
376
+ if (attributes.BYTERANGE) {
377
+ event.byterange = parseByterange(attributes.BYTERANGE);
378
+ }
379
+ }
380
+
381
+ _this2.trigger('data', event);
382
+
383
+ return;
384
+ }
385
+
386
+ match = /^#EXT-X-STREAM-INF:?(.*)$/.exec(newLine);
387
+
388
+ if (match) {
389
+ event = {
390
+ type: 'tag',
391
+ tagType: 'stream-inf'
392
+ };
393
+
394
+ if (match[1]) {
395
+ event.attributes = parseAttributes(match[1]);
396
+
397
+ if (event.attributes.RESOLUTION) {
398
+ var split = event.attributes.RESOLUTION.split('x');
399
+ var resolution = {};
400
+
401
+ if (split[0]) {
402
+ resolution.width = parseInt(split[0], 10);
403
+ }
404
+
405
+ if (split[1]) {
406
+ resolution.height = parseInt(split[1], 10);
407
+ }
408
+
409
+ event.attributes.RESOLUTION = resolution;
410
+ }
411
+
412
+ if (event.attributes.BANDWIDTH) {
413
+ event.attributes.BANDWIDTH = parseInt(event.attributes.BANDWIDTH, 10);
414
+ }
415
+
416
+ if (event.attributes['PROGRAM-ID']) {
417
+ event.attributes['PROGRAM-ID'] = parseInt(event.attributes['PROGRAM-ID'], 10);
418
+ }
419
+ }
420
+
421
+ _this2.trigger('data', event);
422
+
423
+ return;
424
+ }
425
+
426
+ match = /^#EXT-X-MEDIA:?(.*)$/.exec(newLine);
427
+
428
+ if (match) {
429
+ event = {
430
+ type: 'tag',
431
+ tagType: 'media'
432
+ };
433
+
434
+ if (match[1]) {
435
+ event.attributes = parseAttributes(match[1]);
436
+ }
437
+
438
+ _this2.trigger('data', event);
439
+
440
+ return;
441
+ }
442
+
443
+ match = /^#EXT-X-ENDLIST/.exec(newLine);
444
+
445
+ if (match) {
446
+ _this2.trigger('data', {
447
+ type: 'tag',
448
+ tagType: 'endlist'
449
+ });
450
+
451
+ return;
452
+ }
453
+
454
+ match = /^#EXT-X-DISCONTINUITY/.exec(newLine);
455
+
456
+ if (match) {
457
+ _this2.trigger('data', {
458
+ type: 'tag',
459
+ tagType: 'discontinuity'
460
+ });
461
+
462
+ return;
463
+ }
464
+
465
+ match = /^#EXT-X-PROGRAM-DATE-TIME:?(.*)$/.exec(newLine);
466
+
467
+ if (match) {
468
+ event = {
469
+ type: 'tag',
470
+ tagType: 'program-date-time'
471
+ };
472
+
473
+ if (match[1]) {
474
+ event.dateTimeString = match[1];
475
+ event.dateTimeObject = new Date(match[1]);
476
+ }
477
+
478
+ _this2.trigger('data', event);
479
+
480
+ return;
481
+ }
482
+
483
+ match = /^#EXT-X-KEY:?(.*)$/.exec(newLine);
484
+
485
+ if (match) {
486
+ event = {
487
+ type: 'tag',
488
+ tagType: 'key'
489
+ };
490
+
491
+ if (match[1]) {
492
+ event.attributes = parseAttributes(match[1]); // parse the IV string into a Uint32Array
493
+
494
+ if (event.attributes.IV) {
495
+ if (event.attributes.IV.substring(0, 2).toLowerCase() === '0x') {
496
+ event.attributes.IV = event.attributes.IV.substring(2);
497
+ }
498
+
499
+ event.attributes.IV = event.attributes.IV.match(/.{8}/g);
500
+ event.attributes.IV[0] = parseInt(event.attributes.IV[0], 16);
501
+ event.attributes.IV[1] = parseInt(event.attributes.IV[1], 16);
502
+ event.attributes.IV[2] = parseInt(event.attributes.IV[2], 16);
503
+ event.attributes.IV[3] = parseInt(event.attributes.IV[3], 16);
504
+ event.attributes.IV = new Uint32Array(event.attributes.IV);
505
+ }
506
+ }
507
+
508
+ _this2.trigger('data', event);
509
+
510
+ return;
511
+ }
512
+
513
+ match = /^#EXT-X-START:?(.*)$/.exec(newLine);
514
+
515
+ if (match) {
516
+ event = {
517
+ type: 'tag',
518
+ tagType: 'start'
519
+ };
520
+
521
+ if (match[1]) {
522
+ event.attributes = parseAttributes(match[1]);
523
+ event.attributes['TIME-OFFSET'] = parseFloat(event.attributes['TIME-OFFSET']);
524
+ event.attributes.PRECISE = /YES/.test(event.attributes.PRECISE);
525
+ }
526
+
527
+ _this2.trigger('data', event);
528
+
529
+ return;
530
+ }
531
+
532
+ match = /^#EXT-X-CUE-OUT-CONT:?(.*)?$/.exec(newLine);
533
+
534
+ if (match) {
535
+ event = {
536
+ type: 'tag',
537
+ tagType: 'cue-out-cont'
538
+ };
539
+
540
+ if (match[1]) {
541
+ event.data = match[1];
542
+ } else {
543
+ event.data = '';
544
+ }
545
+
546
+ _this2.trigger('data', event);
547
+
548
+ return;
549
+ }
550
+
551
+ match = /^#EXT-X-CUE-OUT:?(.*)?$/.exec(newLine);
552
+
553
+ if (match) {
554
+ event = {
555
+ type: 'tag',
556
+ tagType: 'cue-out'
557
+ };
558
+
559
+ if (match[1]) {
560
+ event.data = match[1];
561
+ } else {
562
+ event.data = '';
563
+ }
564
+
565
+ _this2.trigger('data', event);
566
+
567
+ return;
568
+ }
569
+
570
+ match = /^#EXT-X-CUE-IN:?(.*)?$/.exec(newLine);
571
+
572
+ if (match) {
573
+ event = {
574
+ type: 'tag',
575
+ tagType: 'cue-in'
576
+ };
577
+
578
+ if (match[1]) {
579
+ event.data = match[1];
580
+ } else {
581
+ event.data = '';
582
+ }
583
+
584
+ _this2.trigger('data', event);
585
+
586
+ return;
587
+ }
588
+
589
+ match = /^#EXT-X-SKIP:(.*)$/.exec(newLine);
590
+
591
+ if (match && match[1]) {
592
+ event = {
593
+ type: 'tag',
594
+ tagType: 'skip'
595
+ };
596
+ event.attributes = parseAttributes(match[1]);
597
+
598
+ if (event.attributes.hasOwnProperty('SKIPPED-SEGMENTS')) {
599
+ event.attributes['SKIPPED-SEGMENTS'] = parseInt(event.attributes['SKIPPED-SEGMENTS'], 10);
600
+ }
601
+
602
+ if (event.attributes.hasOwnProperty('RECENTLY-REMOVED-DATERANGES')) {
603
+ event.attributes['RECENTLY-REMOVED-DATERANGES'] = event.attributes['RECENTLY-REMOVED-DATERANGES'].split(TAB);
604
+ }
605
+
606
+ _this2.trigger('data', event);
607
+
608
+ return;
609
+ }
610
+
611
+ match = /^#EXT-X-PART:(.*)$/.exec(newLine);
612
+
613
+ if (match && match[1]) {
614
+ event = {
615
+ type: 'tag',
616
+ tagType: 'part'
617
+ };
618
+ event.attributes = parseAttributes(match[1]);
619
+ ['DURATION'].forEach(function (key) {
620
+ if (event.attributes.hasOwnProperty(key)) {
621
+ event.attributes[key] = parseFloat(event.attributes[key]);
622
+ }
623
+ });
624
+ ['INDEPENDENT', 'GAP'].forEach(function (key) {
625
+ if (event.attributes.hasOwnProperty(key)) {
626
+ event.attributes[key] = /YES/.test(event.attributes[key]);
627
+ }
628
+ });
629
+
630
+ if (event.attributes.hasOwnProperty('BYTERANGE')) {
631
+ event.attributes.byterange = parseByterange(event.attributes.BYTERANGE);
632
+ }
633
+
634
+ _this2.trigger('data', event);
635
+
636
+ return;
637
+ }
638
+
639
+ match = /^#EXT-X-SERVER-CONTROL:(.*)$/.exec(newLine);
640
+
641
+ if (match && match[1]) {
642
+ event = {
643
+ type: 'tag',
644
+ tagType: 'server-control'
645
+ };
646
+ event.attributes = parseAttributes(match[1]);
647
+ ['CAN-SKIP-UNTIL', 'PART-HOLD-BACK', 'HOLD-BACK'].forEach(function (key) {
648
+ if (event.attributes.hasOwnProperty(key)) {
649
+ event.attributes[key] = parseFloat(event.attributes[key]);
650
+ }
651
+ });
652
+ ['CAN-SKIP-DATERANGES', 'CAN-BLOCK-RELOAD'].forEach(function (key) {
653
+ if (event.attributes.hasOwnProperty(key)) {
654
+ event.attributes[key] = /YES/.test(event.attributes[key]);
655
+ }
656
+ });
657
+
658
+ _this2.trigger('data', event);
659
+
660
+ return;
661
+ }
662
+
663
+ match = /^#EXT-X-PART-INF:(.*)$/.exec(newLine);
664
+
665
+ if (match && match[1]) {
666
+ event = {
667
+ type: 'tag',
668
+ tagType: 'part-inf'
669
+ };
670
+ event.attributes = parseAttributes(match[1]);
671
+ ['PART-TARGET'].forEach(function (key) {
672
+ if (event.attributes.hasOwnProperty(key)) {
673
+ event.attributes[key] = parseFloat(event.attributes[key]);
674
+ }
675
+ });
676
+
677
+ _this2.trigger('data', event);
678
+
679
+ return;
680
+ }
681
+
682
+ match = /^#EXT-X-PRELOAD-HINT:(.*)$/.exec(newLine);
683
+
684
+ if (match && match[1]) {
685
+ event = {
686
+ type: 'tag',
687
+ tagType: 'preload-hint'
688
+ };
689
+ event.attributes = parseAttributes(match[1]);
690
+ ['BYTERANGE-START', 'BYTERANGE-LENGTH'].forEach(function (key) {
691
+ if (event.attributes.hasOwnProperty(key)) {
692
+ event.attributes[key] = parseInt(event.attributes[key], 10);
693
+ var subkey = key === 'BYTERANGE-LENGTH' ? 'length' : 'offset';
694
+ event.attributes.byterange = event.attributes.byterange || {};
695
+ event.attributes.byterange[subkey] = event.attributes[key]; // only keep the parsed byterange object.
696
+
697
+ delete event.attributes[key];
698
+ }
699
+ });
700
+
701
+ _this2.trigger('data', event);
702
+
703
+ return;
704
+ }
705
+
706
+ match = /^#EXT-X-RENDITION-REPORT:(.*)$/.exec(newLine);
707
+
708
+ if (match && match[1]) {
709
+ event = {
710
+ type: 'tag',
711
+ tagType: 'rendition-report'
712
+ };
713
+ event.attributes = parseAttributes(match[1]);
714
+ ['LAST-MSN', 'LAST-PART'].forEach(function (key) {
715
+ if (event.attributes.hasOwnProperty(key)) {
716
+ event.attributes[key] = parseInt(event.attributes[key], 10);
717
+ }
718
+ });
719
+
720
+ _this2.trigger('data', event);
721
+
722
+ return;
723
+ } // unknown tag type
724
+
725
+
726
+ _this2.trigger('data', {
727
+ type: 'tag',
728
+ data: newLine.slice(4)
729
+ });
730
+ });
731
+ }
732
+ /**
733
+ * Add a parser for custom headers
734
+ *
735
+ * @param {Object} options a map of options for the added parser
736
+ * @param {RegExp} options.expression a regular expression to match the custom header
737
+ * @param {string} options.customType the custom type to register to the output
738
+ * @param {Function} [options.dataParser] function to parse the line into an object
739
+ * @param {boolean} [options.segment] should tag data be attached to the segment object
740
+ */
741
+ ;
742
+
743
+ _proto.addParser = function addParser(_ref) {
744
+ var _this3 = this;
745
+
746
+ var expression = _ref.expression,
747
+ customType = _ref.customType,
748
+ dataParser = _ref.dataParser,
749
+ segment = _ref.segment;
750
+
751
+ if (typeof dataParser !== 'function') {
752
+ dataParser = function dataParser(line) {
753
+ return line;
754
+ };
755
+ }
756
+
757
+ this.customParsers.push(function (line) {
758
+ var match = expression.exec(line);
759
+
760
+ if (match) {
761
+ _this3.trigger('data', {
762
+ type: 'custom',
763
+ data: dataParser(line),
764
+ customType: customType,
765
+ segment: segment
766
+ });
767
+
768
+ return true;
769
+ }
770
+ });
771
+ }
772
+ /**
773
+ * Add a custom header mapper
774
+ *
775
+ * @param {Object} options
776
+ * @param {RegExp} options.expression a regular expression to match the custom header
777
+ * @param {Function} options.map function to translate tag into a different tag
778
+ */
779
+ ;
780
+
781
+ _proto.addTagMapper = function addTagMapper(_ref2) {
782
+ var expression = _ref2.expression,
783
+ map = _ref2.map;
784
+
785
+ var mapFn = function mapFn(line) {
786
+ if (expression.test(line)) {
787
+ return map(line);
788
+ }
789
+
790
+ return line;
791
+ };
792
+
793
+ this.tagMappers.push(mapFn);
794
+ };
795
+
796
+ return ParseStream;
797
+ }(Stream);
798
+
799
+ var camelCase = function camelCase(str) {
800
+ return str.toLowerCase().replace(/-(\w)/g, function (a) {
801
+ return a[1].toUpperCase();
802
+ });
803
+ };
804
+
805
+ var camelCaseKeys = function camelCaseKeys(attributes) {
806
+ var result = {};
807
+ Object.keys(attributes).forEach(function (key) {
808
+ result[camelCase(key)] = attributes[key];
809
+ });
810
+ return result;
811
+ }; // set SERVER-CONTROL hold back based upon targetDuration and partTargetDuration
812
+ // we need this helper because defaults are based upon targetDuration and
813
+ // partTargetDuration being set, but they may not be if SERVER-CONTROL appears before
814
+ // target durations are set.
815
+
816
+
817
+ var setHoldBack = function setHoldBack(manifest) {
818
+ var serverControl = manifest.serverControl,
819
+ targetDuration = manifest.targetDuration,
820
+ partTargetDuration = manifest.partTargetDuration;
821
+
822
+ if (!serverControl) {
823
+ return;
824
+ }
825
+
826
+ var tag = '#EXT-X-SERVER-CONTROL';
827
+ var hb = 'holdBack';
828
+ var phb = 'partHoldBack';
829
+ var minTargetDuration = targetDuration && targetDuration * 3;
830
+ var minPartDuration = partTargetDuration && partTargetDuration * 2;
831
+
832
+ if (targetDuration && !serverControl.hasOwnProperty(hb)) {
833
+ serverControl[hb] = minTargetDuration;
834
+ this.trigger('info', {
835
+ message: tag + " defaulting HOLD-BACK to targetDuration * 3 (" + minTargetDuration + ")."
836
+ });
837
+ }
838
+
839
+ if (minTargetDuration && serverControl[hb] < minTargetDuration) {
840
+ this.trigger('warn', {
841
+ message: tag + " clamping HOLD-BACK (" + serverControl[hb] + ") to targetDuration * 3 (" + minTargetDuration + ")"
842
+ });
843
+ serverControl[hb] = minTargetDuration;
844
+ } // default no part hold back to part target duration * 3
845
+
846
+
847
+ if (partTargetDuration && !serverControl.hasOwnProperty(phb)) {
848
+ serverControl[phb] = partTargetDuration * 3;
849
+ this.trigger('info', {
850
+ message: tag + " defaulting PART-HOLD-BACK to partTargetDuration * 3 (" + serverControl[phb] + ")."
851
+ });
852
+ } // if part hold back is too small default it to part target duration * 2
853
+
854
+
855
+ if (partTargetDuration && serverControl[phb] < minPartDuration) {
856
+ this.trigger('warn', {
857
+ message: tag + " clamping PART-HOLD-BACK (" + serverControl[phb] + ") to partTargetDuration * 2 (" + minPartDuration + ")."
858
+ });
859
+ serverControl[phb] = minPartDuration;
860
+ }
861
+ };
862
+ /**
863
+ * A parser for M3U8 files. The current interpretation of the input is
864
+ * exposed as a property `manifest` on parser objects. It's just two lines to
865
+ * create and parse a manifest once you have the contents available as a string:
866
+ *
867
+ * ```js
868
+ * var parser = new m3u8.Parser();
869
+ * parser.push(xhr.responseText);
870
+ * ```
871
+ *
872
+ * New input can later be applied to update the manifest object by calling
873
+ * `push` again.
874
+ *
875
+ * The parser attempts to create a usable manifest object even if the
876
+ * underlying input is somewhat nonsensical. It emits `info` and `warning`
877
+ * events during the parse if it encounters input that seems invalid or
878
+ * requires some property of the manifest object to be defaulted.
879
+ *
880
+ * @class Parser
881
+ * @extends Stream
882
+ */
883
+
884
+
885
+ var Parser = /*#__PURE__*/function (_Stream) {
886
+ _inheritsLoose(Parser, _Stream);
887
+
888
+ function Parser() {
889
+ var _this;
890
+
891
+ _this = _Stream.call(this) || this;
892
+ _this.lineStream = new LineStream();
893
+ _this.parseStream = new ParseStream();
894
+
895
+ _this.lineStream.pipe(_this.parseStream);
896
+ /* eslint-disable consistent-this */
897
+
898
+
899
+ var self = _assertThisInitialized(_this);
900
+ /* eslint-enable consistent-this */
901
+
902
+
903
+ var uris = [];
904
+ var currentUri = {}; // if specified, the active EXT-X-MAP definition
905
+
906
+ var currentMap; // if specified, the active decryption key
907
+
908
+ var _key;
909
+
910
+ var hasParts = false;
911
+
912
+ var noop = function noop() {};
913
+
914
+ var defaultMediaGroups = {
915
+ 'AUDIO': {},
916
+ 'VIDEO': {},
917
+ 'CLOSED-CAPTIONS': {},
918
+ 'SUBTITLES': {}
919
+ }; // This is the Widevine UUID from DASH IF IOP. The same exact string is
920
+ // used in MPDs with Widevine encrypted streams.
921
+
922
+ var widevineUuid = 'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed'; // group segments into numbered timelines delineated by discontinuities
923
+
924
+ var currentTimeline = 0; // the manifest is empty until the parse stream begins delivering data
925
+
926
+ _this.manifest = {
927
+ allowCache: true,
928
+ discontinuityStarts: [],
929
+ segments: []
930
+ }; // keep track of the last seen segment's byte range end, as segments are not required
931
+ // to provide the offset, in which case it defaults to the next byte after the
932
+ // previous segment
933
+
934
+ var lastByterangeEnd = 0; // keep track of the last seen part's byte range end.
935
+
936
+ var lastPartByterangeEnd = 0; // track where next segment starts
937
+
938
+ var nextSegmentLineNumberStart = 0;
939
+
940
+ _this.on('end', function () {
941
+ // only add preloadSegment if we don't yet have a uri for it.
942
+ // and we actually have parts/preloadHints
943
+ if (currentUri.uri || !currentUri.parts && !currentUri.preloadHints) {
944
+ return;
945
+ }
946
+
947
+ if (!currentUri.map && currentMap) {
948
+ currentUri.map = currentMap;
949
+ }
950
+
951
+ if (!currentUri.key && _key) {
952
+ currentUri.key = _key;
953
+ }
954
+
955
+ if (!currentUri.timeline && typeof currentTimeline === 'number') {
956
+ currentUri.timeline = currentTimeline;
957
+ }
958
+
959
+ _this.manifest.preloadSegment = currentUri;
960
+ }); // update the manifest with the m3u8 entry from the parse stream
961
+
962
+
963
+ _this.parseStream.on('data', function (entry) {
964
+ var mediaGroup;
965
+ var rendition; // starting a new segment
966
+
967
+ if (!Object.keys(currentUri).length) {
968
+ nextSegmentLineNumberStart = this.lineNumber;
969
+ }
970
+
971
+ ({
972
+ tag: function tag() {
973
+ // switch based on the tag type
974
+ (({
975
+ version: function version() {
976
+ if (entry.version) {
977
+ this.manifest.version = entry.version;
978
+ }
979
+ },
980
+ 'allow-cache': function allowCache() {
981
+ this.manifest.allowCache = entry.allowed;
982
+
983
+ if (!('allowed' in entry)) {
984
+ this.trigger('info', {
985
+ message: 'defaulting allowCache to YES'
986
+ });
987
+ this.manifest.allowCache = true;
988
+ }
989
+ },
990
+ byterange: function byterange() {
991
+ var byterange = {};
992
+
993
+ if ('length' in entry) {
994
+ currentUri.byterange = byterange;
995
+ byterange.length = entry.length;
996
+
997
+ if (!('offset' in entry)) {
998
+ /*
999
+ * From the latest spec (as of this writing):
1000
+ * https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.2
1001
+ *
1002
+ * Same text since EXT-X-BYTERANGE's introduction in draft 7:
1003
+ * https://tools.ietf.org/html/draft-pantos-http-live-streaming-07#section-3.3.1)
1004
+ *
1005
+ * "If o [offset] is not present, the sub-range begins at the next byte
1006
+ * following the sub-range of the previous media segment."
1007
+ */
1008
+ entry.offset = lastByterangeEnd;
1009
+ }
1010
+ }
1011
+
1012
+ if ('offset' in entry) {
1013
+ currentUri.byterange = byterange;
1014
+ byterange.offset = entry.offset;
1015
+ }
1016
+
1017
+ lastByterangeEnd = byterange.offset + byterange.length;
1018
+ },
1019
+ endlist: function endlist() {
1020
+ this.manifest.endList = true;
1021
+ },
1022
+ inf: function inf() {
1023
+ if (!('mediaSequence' in this.manifest)) {
1024
+ this.manifest.mediaSequence = 0;
1025
+ this.trigger('info', {
1026
+ message: 'defaulting media sequence to zero'
1027
+ });
1028
+ }
1029
+
1030
+ if (!('discontinuitySequence' in this.manifest)) {
1031
+ this.manifest.discontinuitySequence = 0;
1032
+ this.trigger('info', {
1033
+ message: 'defaulting discontinuity sequence to zero'
1034
+ });
1035
+ }
1036
+
1037
+ if (entry.duration > 0) {
1038
+ currentUri.duration = entry.duration;
1039
+ }
1040
+
1041
+ if (entry.duration === 0) {
1042
+ currentUri.duration = 0.01;
1043
+ this.trigger('info', {
1044
+ message: 'updating zero segment duration to a small value'
1045
+ });
1046
+ }
1047
+
1048
+ this.manifest.segments = uris;
1049
+ },
1050
+ key: function key() {
1051
+ if (!entry.attributes) {
1052
+ this.trigger('warn', {
1053
+ message: 'ignoring key declaration without attribute list'
1054
+ });
1055
+ return;
1056
+ } // clear the active encryption key
1057
+
1058
+
1059
+ if (entry.attributes.METHOD === 'NONE') {
1060
+ _key = null;
1061
+ return;
1062
+ }
1063
+
1064
+ if (!entry.attributes.URI) {
1065
+ this.trigger('warn', {
1066
+ message: 'ignoring key declaration without URI'
1067
+ });
1068
+ return;
1069
+ }
1070
+
1071
+ if (entry.attributes.KEYFORMAT === 'com.apple.streamingkeydelivery') {
1072
+ this.manifest.contentProtection = this.manifest.contentProtection || {}; // TODO: add full support for this.
1073
+
1074
+ this.manifest.contentProtection['com.apple.fps.1_0'] = {
1075
+ attributes: entry.attributes
1076
+ };
1077
+ return;
1078
+ }
1079
+
1080
+ if (entry.attributes.KEYFORMAT === 'com.microsoft.playready') {
1081
+ this.manifest.contentProtection = this.manifest.contentProtection || {}; // TODO: add full support for this.
1082
+
1083
+ this.manifest.contentProtection['com.microsoft.playready'] = {
1084
+ uri: entry.attributes.URI
1085
+ };
1086
+ return;
1087
+ } // check if the content is encrypted for Widevine
1088
+ // Widevine/HLS spec: https://storage.googleapis.com/wvdocs/Widevine_DRM_HLS.pdf
1089
+
1090
+
1091
+ if (entry.attributes.KEYFORMAT === widevineUuid) {
1092
+ var VALID_METHODS = ['SAMPLE-AES', 'SAMPLE-AES-CTR', 'SAMPLE-AES-CENC'];
1093
+
1094
+ if (VALID_METHODS.indexOf(entry.attributes.METHOD) === -1) {
1095
+ this.trigger('warn', {
1096
+ message: 'invalid key method provided for Widevine'
1097
+ });
1098
+ return;
1099
+ }
1100
+
1101
+ if (entry.attributes.METHOD === 'SAMPLE-AES-CENC') {
1102
+ this.trigger('warn', {
1103
+ message: 'SAMPLE-AES-CENC is deprecated, please use SAMPLE-AES-CTR instead'
1104
+ });
1105
+ }
1106
+
1107
+ if (entry.attributes.URI.substring(0, 23) !== 'data:text/plain;base64,') {
1108
+ this.trigger('warn', {
1109
+ message: 'invalid key URI provided for Widevine'
1110
+ });
1111
+ return;
1112
+ }
1113
+
1114
+ if (!(entry.attributes.KEYID && entry.attributes.KEYID.substring(0, 2) === '0x')) {
1115
+ this.trigger('warn', {
1116
+ message: 'invalid key ID provided for Widevine'
1117
+ });
1118
+ return;
1119
+ } // if Widevine key attributes are valid, store them as `contentProtection`
1120
+ // on the manifest to emulate Widevine tag structure in a DASH mpd
1121
+
1122
+
1123
+ this.manifest.contentProtection = this.manifest.contentProtection || {};
1124
+ this.manifest.contentProtection['com.widevine.alpha'] = {
1125
+ attributes: {
1126
+ schemeIdUri: entry.attributes.KEYFORMAT,
1127
+ // remove '0x' from the key id string
1128
+ keyId: entry.attributes.KEYID.substring(2)
1129
+ },
1130
+ // decode the base64-encoded PSSH box
1131
+ pssh: decodeB64ToUint8Array(entry.attributes.URI.split(',')[1])
1132
+ };
1133
+ return;
1134
+ }
1135
+
1136
+ if (!entry.attributes.METHOD) {
1137
+ this.trigger('warn', {
1138
+ message: 'defaulting key method to AES-128'
1139
+ });
1140
+ } // setup an encryption key for upcoming segments
1141
+
1142
+
1143
+ _key = {
1144
+ method: entry.attributes.METHOD || 'AES-128',
1145
+ uri: entry.attributes.URI
1146
+ };
1147
+
1148
+ if (typeof entry.attributes.IV !== 'undefined') {
1149
+ _key.iv = entry.attributes.IV;
1150
+ }
1151
+ },
1152
+ 'media-sequence': function mediaSequence() {
1153
+ if (!isFinite(entry.number)) {
1154
+ this.trigger('warn', {
1155
+ message: 'ignoring invalid media sequence: ' + entry.number
1156
+ });
1157
+ return;
1158
+ }
1159
+
1160
+ this.manifest.mediaSequence = entry.number;
1161
+ },
1162
+ 'discontinuity-sequence': function discontinuitySequence() {
1163
+ if (!isFinite(entry.number)) {
1164
+ this.trigger('warn', {
1165
+ message: 'ignoring invalid discontinuity sequence: ' + entry.number
1166
+ });
1167
+ return;
1168
+ }
1169
+
1170
+ this.manifest.discontinuitySequence = entry.number;
1171
+ currentTimeline = entry.number;
1172
+ },
1173
+ 'playlist-type': function playlistType() {
1174
+ if (!/VOD|EVENT/.test(entry.playlistType)) {
1175
+ this.trigger('warn', {
1176
+ message: 'ignoring unknown playlist type: ' + entry.playlist
1177
+ });
1178
+ return;
1179
+ }
1180
+
1181
+ this.manifest.playlistType = entry.playlistType;
1182
+ },
1183
+ map: function map() {
1184
+ currentMap = {};
1185
+
1186
+ if (entry.uri) {
1187
+ currentMap.uri = entry.uri;
1188
+ }
1189
+
1190
+ if (entry.byterange) {
1191
+ currentMap.byterange = entry.byterange;
1192
+ }
1193
+
1194
+ if (_key) {
1195
+ currentMap.key = _key;
1196
+ }
1197
+ },
1198
+ 'stream-inf': function streamInf() {
1199
+ this.manifest.playlists = uris;
1200
+ this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
1201
+
1202
+ if (!entry.attributes) {
1203
+ this.trigger('warn', {
1204
+ message: 'ignoring empty stream-inf attributes'
1205
+ });
1206
+ return;
1207
+ }
1208
+
1209
+ if (!currentUri.attributes) {
1210
+ currentUri.attributes = {};
1211
+ }
1212
+
1213
+ _extends(currentUri.attributes, entry.attributes);
1214
+ },
1215
+ media: function media() {
1216
+ this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
1217
+
1218
+ if (!(entry.attributes && entry.attributes.TYPE && entry.attributes['GROUP-ID'] && entry.attributes.NAME)) {
1219
+ this.trigger('warn', {
1220
+ message: 'ignoring incomplete or missing media group'
1221
+ });
1222
+ return;
1223
+ } // find the media group, creating defaults as necessary
1224
+
1225
+
1226
+ var mediaGroupType = this.manifest.mediaGroups[entry.attributes.TYPE];
1227
+ mediaGroupType[entry.attributes['GROUP-ID']] = mediaGroupType[entry.attributes['GROUP-ID']] || {};
1228
+ mediaGroup = mediaGroupType[entry.attributes['GROUP-ID']]; // collect the rendition metadata
1229
+
1230
+ rendition = {
1231
+ default: /yes/i.test(entry.attributes.DEFAULT)
1232
+ };
1233
+
1234
+ if (rendition.default) {
1235
+ rendition.autoselect = true;
1236
+ } else {
1237
+ rendition.autoselect = /yes/i.test(entry.attributes.AUTOSELECT);
1238
+ }
1239
+
1240
+ if (entry.attributes.LANGUAGE) {
1241
+ rendition.language = entry.attributes.LANGUAGE;
1242
+ }
1243
+
1244
+ if (entry.attributes.URI) {
1245
+ rendition.uri = entry.attributes.URI;
1246
+ }
1247
+
1248
+ if (entry.attributes['INSTREAM-ID']) {
1249
+ rendition.instreamId = entry.attributes['INSTREAM-ID'];
1250
+ }
1251
+
1252
+ if (entry.attributes.CHARACTERISTICS) {
1253
+ rendition.characteristics = entry.attributes.CHARACTERISTICS;
1254
+ }
1255
+
1256
+ if (entry.attributes.FORCED) {
1257
+ rendition.forced = /yes/i.test(entry.attributes.FORCED);
1258
+ } // insert the new rendition
1259
+
1260
+
1261
+ mediaGroup[entry.attributes.NAME] = rendition;
1262
+ },
1263
+ discontinuity: function discontinuity() {
1264
+ currentTimeline += 1;
1265
+ currentUri.discontinuity = true;
1266
+ this.manifest.discontinuityStarts.push(uris.length);
1267
+ },
1268
+ 'program-date-time': function programDateTime() {
1269
+ if (typeof this.manifest.dateTimeString === 'undefined') {
1270
+ // PROGRAM-DATE-TIME is a media-segment tag, but for backwards
1271
+ // compatibility, we add the first occurence of the PROGRAM-DATE-TIME tag
1272
+ // to the manifest object
1273
+ // TODO: Consider removing this in future major version
1274
+ this.manifest.dateTimeString = entry.dateTimeString;
1275
+ this.manifest.dateTimeObject = entry.dateTimeObject;
1276
+ }
1277
+
1278
+ currentUri.dateTimeString = entry.dateTimeString;
1279
+ currentUri.dateTimeObject = entry.dateTimeObject;
1280
+ },
1281
+ targetduration: function targetduration() {
1282
+ if (!isFinite(entry.duration) || entry.duration < 0) {
1283
+ this.trigger('warn', {
1284
+ message: 'ignoring invalid target duration: ' + entry.duration
1285
+ });
1286
+ return;
1287
+ }
1288
+
1289
+ this.manifest.targetDuration = entry.duration;
1290
+ setHoldBack.call(this, this.manifest);
1291
+ },
1292
+ start: function start() {
1293
+ if (!entry.attributes || isNaN(entry.attributes['TIME-OFFSET'])) {
1294
+ this.trigger('warn', {
1295
+ message: 'ignoring start declaration without appropriate attribute list'
1296
+ });
1297
+ return;
1298
+ }
1299
+
1300
+ this.manifest.start = {
1301
+ timeOffset: entry.attributes['TIME-OFFSET'],
1302
+ precise: entry.attributes.PRECISE
1303
+ };
1304
+ },
1305
+ 'cue-out': function cueOut() {
1306
+ currentUri.cueOut = entry.data;
1307
+ },
1308
+ 'cue-out-cont': function cueOutCont() {
1309
+ currentUri.cueOutCont = entry.data;
1310
+ },
1311
+ 'cue-in': function cueIn() {
1312
+ currentUri.cueIn = entry.data;
1313
+ },
1314
+ 'skip': function skip() {
1315
+ this.manifest.skip = camelCaseKeys(entry.attributes);
1316
+ this.warnOnMissingAttributes_('#EXT-X-SKIP', entry.attributes, ['SKIPPED-SEGMENTS']);
1317
+ },
1318
+ 'part': function part() {
1319
+ var _this2 = this;
1320
+
1321
+ hasParts = true; // parts are always specifed before a segment
1322
+
1323
+ var segmentIndex = this.manifest.segments.length;
1324
+ var part = camelCaseKeys(entry.attributes);
1325
+ currentUri.parts = currentUri.parts || [];
1326
+ currentUri.parts.push(part);
1327
+
1328
+ if (part.byterange) {
1329
+ if (!part.byterange.hasOwnProperty('offset')) {
1330
+ part.byterange.offset = lastPartByterangeEnd;
1331
+ }
1332
+
1333
+ lastPartByterangeEnd = part.byterange.offset + part.byterange.length;
1334
+ }
1335
+
1336
+ var partIndex = currentUri.parts.length - 1;
1337
+ this.warnOnMissingAttributes_("#EXT-X-PART #" + partIndex + " for segment #" + segmentIndex, entry.attributes, ['URI', 'DURATION']);
1338
+
1339
+ if (this.manifest.renditionReports) {
1340
+ this.manifest.renditionReports.forEach(function (r, i) {
1341
+ if (!r.hasOwnProperty('lastPart')) {
1342
+ _this2.trigger('warn', {
1343
+ message: "#EXT-X-RENDITION-REPORT #" + i + " lacks required attribute(s): LAST-PART"
1344
+ });
1345
+ }
1346
+ });
1347
+ }
1348
+ },
1349
+ 'server-control': function serverControl() {
1350
+ var attrs = this.manifest.serverControl = camelCaseKeys(entry.attributes);
1351
+
1352
+ if (!attrs.hasOwnProperty('canBlockReload')) {
1353
+ attrs.canBlockReload = false;
1354
+ this.trigger('info', {
1355
+ message: '#EXT-X-SERVER-CONTROL defaulting CAN-BLOCK-RELOAD to false'
1356
+ });
1357
+ }
1358
+
1359
+ setHoldBack.call(this, this.manifest);
1360
+
1361
+ if (attrs.canSkipDateranges && !attrs.hasOwnProperty('canSkipUntil')) {
1362
+ this.trigger('warn', {
1363
+ message: '#EXT-X-SERVER-CONTROL lacks required attribute CAN-SKIP-UNTIL which is required when CAN-SKIP-DATERANGES is set'
1364
+ });
1365
+ }
1366
+ },
1367
+ 'preload-hint': function preloadHint() {
1368
+ // parts are always specifed before a segment
1369
+ var segmentIndex = this.manifest.segments.length;
1370
+ var hint = camelCaseKeys(entry.attributes);
1371
+ var isPart = hint.type && hint.type === 'PART';
1372
+ currentUri.preloadHints = currentUri.preloadHints || [];
1373
+ currentUri.preloadHints.push(hint);
1374
+
1375
+ if (hint.byterange) {
1376
+ if (!hint.byterange.hasOwnProperty('offset')) {
1377
+ // use last part byterange end or zero if not a part.
1378
+ hint.byterange.offset = isPart ? lastPartByterangeEnd : 0;
1379
+
1380
+ if (isPart) {
1381
+ lastPartByterangeEnd = hint.byterange.offset + hint.byterange.length;
1382
+ }
1383
+ }
1384
+ }
1385
+
1386
+ var index = currentUri.preloadHints.length - 1;
1387
+ this.warnOnMissingAttributes_("#EXT-X-PRELOAD-HINT #" + index + " for segment #" + segmentIndex, entry.attributes, ['TYPE', 'URI']);
1388
+
1389
+ if (!hint.type) {
1390
+ return;
1391
+ } // search through all preload hints except for the current one for
1392
+ // a duplicate type.
1393
+
1394
+
1395
+ for (var i = 0; i < currentUri.preloadHints.length - 1; i++) {
1396
+ var otherHint = currentUri.preloadHints[i];
1397
+
1398
+ if (!otherHint.type) {
1399
+ continue;
1400
+ }
1401
+
1402
+ if (otherHint.type === hint.type) {
1403
+ this.trigger('warn', {
1404
+ message: "#EXT-X-PRELOAD-HINT #" + index + " for segment #" + segmentIndex + " has the same TYPE " + hint.type + " as preload hint #" + i
1405
+ });
1406
+ }
1407
+ }
1408
+ },
1409
+ 'rendition-report': function renditionReport() {
1410
+ var report = camelCaseKeys(entry.attributes);
1411
+ this.manifest.renditionReports = this.manifest.renditionReports || [];
1412
+ this.manifest.renditionReports.push(report);
1413
+ var index = this.manifest.renditionReports.length - 1;
1414
+ var required = ['LAST-MSN', 'URI'];
1415
+
1416
+ if (hasParts) {
1417
+ required.push('LAST-PART');
1418
+ }
1419
+
1420
+ this.warnOnMissingAttributes_("#EXT-X-RENDITION-REPORT #" + index, entry.attributes, required);
1421
+ },
1422
+ 'part-inf': function partInf() {
1423
+ this.manifest.partInf = camelCaseKeys(entry.attributes);
1424
+ this.warnOnMissingAttributes_('#EXT-X-PART-INF', entry.attributes, ['PART-TARGET']);
1425
+
1426
+ if (this.manifest.partInf.partTarget) {
1427
+ this.manifest.partTargetDuration = this.manifest.partInf.partTarget;
1428
+ }
1429
+
1430
+ setHoldBack.call(this, this.manifest);
1431
+ }
1432
+ })[entry.tagType] || noop).call(self);
1433
+ },
1434
+ uri: function uri() {
1435
+ currentUri.uri = entry.uri;
1436
+ currentUri.lineNumberStart = nextSegmentLineNumberStart;
1437
+ currentUri.lineNumberEnd = this.parseStream.lineNumber;
1438
+ uris.push(currentUri); // if no explicit duration was declared, use the target duration
1439
+
1440
+ if (this.manifest.targetDuration && !('duration' in currentUri)) {
1441
+ this.trigger('warn', {
1442
+ message: 'defaulting segment duration to the target duration'
1443
+ });
1444
+ currentUri.duration = this.manifest.targetDuration;
1445
+ } // annotate with encryption information, if necessary
1446
+
1447
+
1448
+ if (_key) {
1449
+ currentUri.key = _key;
1450
+ }
1451
+
1452
+ currentUri.timeline = currentTimeline; // annotate with initialization segment information, if necessary
1453
+
1454
+ if (currentMap) {
1455
+ currentUri.map = currentMap;
1456
+ } // reset the last byterange end as it needs to be 0 between parts
1457
+
1458
+
1459
+ lastPartByterangeEnd = 0; // prepare for the next URI
1460
+
1461
+ currentUri = {};
1462
+ },
1463
+ comment: function comment() {// comments are not important for playback
1464
+ },
1465
+ custom: function custom() {
1466
+ // if this is segment-level data attach the output to the segment
1467
+ if (entry.segment) {
1468
+ currentUri.custom = currentUri.custom || {};
1469
+ currentUri.custom[entry.customType] = entry.data; // if this is manifest-level data attach to the top level manifest object
1470
+ } else {
1471
+ this.manifest.custom = this.manifest.custom || {};
1472
+ this.manifest.custom[entry.customType] = entry.data;
1473
+ }
1474
+ }
1475
+ })[entry.type].call(self);
1476
+ });
1477
+
1478
+ return _this;
1479
+ }
1480
+
1481
+ var _proto = Parser.prototype;
1482
+
1483
+ _proto.warnOnMissingAttributes_ = function warnOnMissingAttributes_(identifier, attributes, required) {
1484
+ var missing = [];
1485
+ required.forEach(function (key) {
1486
+ if (!attributes.hasOwnProperty(key)) {
1487
+ missing.push(key);
1488
+ }
1489
+ });
1490
+
1491
+ if (missing.length) {
1492
+ this.trigger('warn', {
1493
+ message: identifier + " lacks required attribute(s): " + missing.join(', ')
1494
+ });
1495
+ }
1496
+ }
1497
+ /**
1498
+ * Parse the input string and update the manifest object.
1499
+ *
1500
+ * @param {string} chunk a potentially incomplete portion of the manifest
1501
+ */
1502
+ ;
1503
+
1504
+ _proto.push = function push(chunk) {
1505
+ this.lineStream.push(chunk);
1506
+ }
1507
+ /**
1508
+ * Flush any remaining input. This can be handy if the last line of an M3U8
1509
+ * manifest did not contain a trailing newline but the file has been
1510
+ * completely received.
1511
+ */
1512
+ ;
1513
+
1514
+ _proto.end = function end() {
1515
+ // flush any buffered input
1516
+ this.lineStream.push('\n');
1517
+ this.trigger('end');
1518
+ }
1519
+ /**
1520
+ * Add an additional parser for non-standard tags
1521
+ *
1522
+ * @param {Object} options a map of options for the added parser
1523
+ * @param {RegExp} options.expression a regular expression to match the custom header
1524
+ * @param {string} options.type the type to register to the output
1525
+ * @param {Function} [options.dataParser] function to parse the line into an object
1526
+ * @param {boolean} [options.segment] should tag data be attached to the segment object
1527
+ */
1528
+ ;
1529
+
1530
+ _proto.addParser = function addParser(options) {
1531
+ this.parseStream.addParser(options);
1532
+ }
1533
+ /**
1534
+ * Add a custom header mapper
1535
+ *
1536
+ * @param {Object} options
1537
+ * @param {RegExp} options.expression a regular expression to match the custom header
1538
+ * @param {Function} options.map function to translate tag into a different tag
1539
+ */
1540
+ ;
1541
+
1542
+ _proto.addTagMapper = function addTagMapper(options) {
1543
+ this.parseStream.addTagMapper(options);
1544
+ };
1545
+
1546
+ return Parser;
1547
+ }(Stream);
1548
+
1549
+ export { LineStream, ParseStream, Parser };