@ac-kit/format-hls 0.0.0-stage → 0.1.0

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 (148) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/LICENSE +28 -0
  3. package/README.md +145 -2
  4. package/dist/_date-range-fields.d.ts +14 -0
  5. package/dist/_date-range-fields.d.ts.map +1 -0
  6. package/dist/_date-range-fields.js +53 -0
  7. package/dist/_date-range-fields.js.map +1 -0
  8. package/dist/_key-scope.d.ts +8 -0
  9. package/dist/_key-scope.d.ts.map +1 -0
  10. package/dist/_key-scope.js +14 -0
  11. package/dist/_key-scope.js.map +1 -0
  12. package/dist/_temporal.d.ts +3 -0
  13. package/dist/_temporal.d.ts.map +1 -0
  14. package/dist/_temporal.js +14 -0
  15. package/dist/_temporal.js.map +1 -0
  16. package/dist/attribute/_print-attribute.d.ts +10 -0
  17. package/dist/attribute/_print-attribute.d.ts.map +1 -0
  18. package/dist/attribute/_print-attribute.js +31 -0
  19. package/dist/attribute/_print-attribute.js.map +1 -0
  20. package/dist/attribute/_scan-attribute-list.d.ts +24 -0
  21. package/dist/attribute/_scan-attribute-list.d.ts.map +1 -0
  22. package/dist/attribute/_scan-attribute-list.js +147 -0
  23. package/dist/attribute/_scan-attribute-list.js.map +1 -0
  24. package/dist/attribute/hls-attribute.d.ts +38 -0
  25. package/dist/attribute/hls-attribute.d.ts.map +1 -0
  26. package/dist/attribute/hls-attribute.js +42 -0
  27. package/dist/attribute/hls-attribute.js.map +1 -0
  28. package/dist/document/hls-document.d.ts +52 -0
  29. package/dist/document/hls-document.d.ts.map +1 -0
  30. package/dist/document/hls-document.js +2 -0
  31. package/dist/document/hls-document.js.map +1 -0
  32. package/dist/document/parse-hls-document.d.ts +14 -0
  33. package/dist/document/parse-hls-document.d.ts.map +1 -0
  34. package/dist/document/parse-hls-document.js +79 -0
  35. package/dist/document/parse-hls-document.js.map +1 -0
  36. package/dist/document/print-hls-document.d.ts +10 -0
  37. package/dist/document/print-hls-document.d.ts.map +1 -0
  38. package/dist/document/print-hls-document.js +28 -0
  39. package/dist/document/print-hls-document.js.map +1 -0
  40. package/dist/errors.d.ts +92 -0
  41. package/dist/errors.d.ts.map +1 -0
  42. package/dist/errors.js +67 -0
  43. package/dist/errors.js.map +1 -0
  44. package/dist/hls-playlist-queries.d.ts +16 -0
  45. package/dist/hls-playlist-queries.d.ts.map +1 -0
  46. package/dist/hls-playlist-queries.js +28 -0
  47. package/dist/hls-playlist-queries.js.map +1 -0
  48. package/dist/index.d.ts +21 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +21 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/limits.d.ts +48 -0
  53. package/dist/limits.d.ts.map +1 -0
  54. package/dist/limits.js +24 -0
  55. package/dist/limits.js.map +1 -0
  56. package/dist/live/apply-hls-delta-update.d.ts +49 -0
  57. package/dist/live/apply-hls-delta-update.d.ts.map +1 -0
  58. package/dist/live/apply-hls-delta-update.js +298 -0
  59. package/dist/live/apply-hls-delta-update.js.map +1 -0
  60. package/dist/model/hls-key.d.ts +29 -0
  61. package/dist/model/hls-key.d.ts.map +1 -0
  62. package/dist/model/hls-key.js +2 -0
  63. package/dist/model/hls-key.js.map +1 -0
  64. package/dist/model/hls-media-playlist.d.ts +202 -0
  65. package/dist/model/hls-media-playlist.d.ts.map +1 -0
  66. package/dist/model/hls-media-playlist.js +2 -0
  67. package/dist/model/hls-media-playlist.js.map +1 -0
  68. package/dist/model/hls-multivariant-playlist.d.ts +142 -0
  69. package/dist/model/hls-multivariant-playlist.d.ts.map +1 -0
  70. package/dist/model/hls-multivariant-playlist.js +2 -0
  71. package/dist/model/hls-multivariant-playlist.js.map +1 -0
  72. package/dist/model/hls-playlist.d.ts +77 -0
  73. package/dist/model/hls-playlist.d.ts.map +1 -0
  74. package/dist/model/hls-playlist.js +2 -0
  75. package/dist/model/hls-playlist.js.map +1 -0
  76. package/dist/parse/_attribute-reader.d.ts +106 -0
  77. package/dist/parse/_attribute-reader.d.ts.map +1 -0
  78. package/dist/parse/_attribute-reader.js +266 -0
  79. package/dist/parse/_attribute-reader.js.map +1 -0
  80. package/dist/parse/_context.d.ts +87 -0
  81. package/dist/parse/_context.d.ts.map +1 -0
  82. package/dist/parse/_context.js +179 -0
  83. package/dist/parse/_context.js.map +1 -0
  84. package/dist/parse/_interpret-media.d.ts +18 -0
  85. package/dist/parse/_interpret-media.d.ts.map +1 -0
  86. package/dist/parse/_interpret-media.js +677 -0
  87. package/dist/parse/_interpret-media.js.map +1 -0
  88. package/dist/parse/_interpret-multivariant.d.ts +9 -0
  89. package/dist/parse/_interpret-multivariant.d.ts.map +1 -0
  90. package/dist/parse/_interpret-multivariant.js +435 -0
  91. package/dist/parse/_interpret-multivariant.js.map +1 -0
  92. package/dist/parse/_media-validation.d.ts +14 -0
  93. package/dist/parse/_media-validation.d.ts.map +1 -0
  94. package/dist/parse/_media-validation.js +50 -0
  95. package/dist/parse/_media-validation.js.map +1 -0
  96. package/dist/parse/_prepare-lines.d.ts +12 -0
  97. package/dist/parse/_prepare-lines.d.ts.map +1 -0
  98. package/dist/parse/_prepare-lines.js +118 -0
  99. package/dist/parse/_prepare-lines.js.map +1 -0
  100. package/dist/parse/_sink.d.ts +30 -0
  101. package/dist/parse/_sink.d.ts.map +1 -0
  102. package/dist/parse/_sink.js +53 -0
  103. package/dist/parse/_sink.js.map +1 -0
  104. package/dist/parse/_values.d.ts +31 -0
  105. package/dist/parse/_values.d.ts.map +1 -0
  106. package/dist/parse/_values.js +89 -0
  107. package/dist/parse/_values.js.map +1 -0
  108. package/dist/parse/_variables.d.ts +34 -0
  109. package/dist/parse/_variables.d.ts.map +1 -0
  110. package/dist/parse/_variables.js +65 -0
  111. package/dist/parse/_variables.js.map +1 -0
  112. package/dist/parse/parse-hls-date-time.d.ts +13 -0
  113. package/dist/parse/parse-hls-date-time.d.ts.map +1 -0
  114. package/dist/parse/parse-hls-date-time.js +36 -0
  115. package/dist/parse/parse-hls-date-time.js.map +1 -0
  116. package/dist/parse/parse-hls-playlist.d.ts +58 -0
  117. package/dist/parse/parse-hls-playlist.d.ts.map +1 -0
  118. package/dist/parse/parse-hls-playlist.js +119 -0
  119. package/dist/parse/parse-hls-playlist.js.map +1 -0
  120. package/dist/parse-stream.d.ts +19 -0
  121. package/dist/parse-stream.d.ts.map +1 -0
  122. package/dist/parse-stream.js +54 -0
  123. package/dist/parse-stream.js.map +1 -0
  124. package/dist/print/print-hls-playlist.d.ts +39 -0
  125. package/dist/print/print-hls-playlist.d.ts.map +1 -0
  126. package/dist/print/print-hls-playlist.js +504 -0
  127. package/dist/print/print-hls-playlist.js.map +1 -0
  128. package/dist/print-stream.d.ts +11 -0
  129. package/dist/print-stream.d.ts.map +1 -0
  130. package/dist/print-stream.js +13 -0
  131. package/dist/print-stream.js.map +1 -0
  132. package/dist/resolve/_href.d.ts +13 -0
  133. package/dist/resolve/_href.d.ts.map +1 -0
  134. package/dist/resolve/_href.js +26 -0
  135. package/dist/resolve/_href.js.map +1 -0
  136. package/dist/resolve/hls-resolve-diagnostic.d.ts +26 -0
  137. package/dist/resolve/hls-resolve-diagnostic.d.ts.map +1 -0
  138. package/dist/resolve/hls-resolve-diagnostic.js +2 -0
  139. package/dist/resolve/hls-resolve-diagnostic.js.map +1 -0
  140. package/dist/resolve/resolve-hls-segments.d.ts +74 -0
  141. package/dist/resolve/resolve-hls-segments.d.ts.map +1 -0
  142. package/dist/resolve/resolve-hls-segments.js +179 -0
  143. package/dist/resolve/resolve-hls-segments.js.map +1 -0
  144. package/dist/resolve/resolve-hls-variants.d.ts +105 -0
  145. package/dist/resolve/resolve-hls-variants.d.ts.map +1 -0
  146. package/dist/resolve/resolve-hls-variants.js +162 -0
  147. package/dist/resolve/resolve-hls-variants.js.map +1 -0
  148. package/package.json +63 -3
@@ -0,0 +1,677 @@
1
+ import { mergeDateRangeFields } from "../_date-range-fields.js";
2
+ import { HlsLimitError } from "../errors.js";
3
+ import { createHeaderState, expectNoValue, interpretHeaderTag, isDuplicate, readAttributes, readTagInteger, unknownTag, } from "./_context.js";
4
+ import { byteRangeEnd, checkDateRangeTimeline, checkProgramDate, checkSequenceBounds, compareSegmentResources, } from "./_media-validation.js";
5
+ import { isSetAsideSignal, SetAsideSignal } from "./_sink.js";
6
+ import { isHexadecimalSequence, readByteRange, readDecimalFloat, splitList, } from "./_values.js";
7
+ import { parseHlsDateTime } from "./parse-hls-date-time.js";
8
+ const KEY_METHODS_ = [
9
+ "NONE",
10
+ "AES-128",
11
+ "SAMPLE-AES",
12
+ "SAMPLE-AES-CTR",
13
+ "AES-256-GCM",
14
+ ];
15
+ const KEY_FORMAT_VERSIONS_ = /^\d{1,20}(?:\/\d{1,20})*$/u;
16
+ const SIGNED_DECIMAL_ = /^-?\d+(?:\.\d+)?$/u;
17
+ const URI_SCHEME_ = /^[A-Za-z][A-Za-z0-9+.-]*:/u;
18
+ function emptyBlock_() {
19
+ return {
20
+ discontinuity: false,
21
+ programDateTime: null,
22
+ gap: false,
23
+ extinf: null,
24
+ byteRange: null,
25
+ keyTags: [],
26
+ parts: [],
27
+ dateRanges: [],
28
+ unknownTags: [],
29
+ partEnd: null,
30
+ };
31
+ }
32
+ function isBlockEmpty_(block) {
33
+ return (!block.discontinuity &&
34
+ block.programDateTime === null &&
35
+ !block.gap &&
36
+ block.extinf === null &&
37
+ block.byteRange === null &&
38
+ block.parts.length === 0 &&
39
+ block.dateRanges.length === 0 &&
40
+ block.unknownTags.length === 0);
41
+ }
42
+ /**
43
+ * Interprets the lines after `#EXTM3U` as a media playlist, in one pass.
44
+ * O(lines + attributes) time and memory: keys are kept as declarations, so
45
+ * nothing is copied per segment.
46
+ */
47
+ export function interpretMediaPlaylist(context, lines) {
48
+ const { sink } = context;
49
+ const report = sink.violation.bind(sink);
50
+ const header = createHeaderState();
51
+ const seen = header.seen;
52
+ const segments = [];
53
+ const segmentLines = [];
54
+ const preloadHints = [];
55
+ const renditionReports = [];
56
+ const dateRangeIds = new Map();
57
+ const explicitDates = new Set();
58
+ let targetDuration = null;
59
+ let mediaSequence = 0;
60
+ let discontinuitySequence = 0;
61
+ let discontinuities = 0;
62
+ let endList = false;
63
+ let endListLine = 0;
64
+ let playlistType = null;
65
+ let iFramesOnly = false;
66
+ let partInf = null;
67
+ let serverControl = null;
68
+ let skip = null;
69
+ let anyProgramDateTime = false;
70
+ let firstDateRangeLine = 0;
71
+ let map = null;
72
+ let bitrate = null;
73
+ let block = emptyBlock_();
74
+ let previousRange = null;
75
+ /** Reports an unsafe byte position, leaving the exclusive end unknown. */
76
+ const rangeEnd = (offset, length, line, what) => {
77
+ const end = byteRangeEnd(offset, length);
78
+ if (end === null) {
79
+ sink.violation("unsafe-integer", line, 1, `${what} ends past 2^53-1`);
80
+ }
81
+ return end;
82
+ };
83
+ const misplacedAfterSegments = (tag) => {
84
+ if (segments.length > 0) {
85
+ sink.violation("misplaced-tag", tag.line, 1, `${tag.name} must appear before the first segment`);
86
+ }
87
+ };
88
+ /** §4.4.4.9: these tags apply to the first part too, so they come before it. */
89
+ const beforeParts = (tag) => {
90
+ if (block.parts.length > 0) {
91
+ sink.violation("misplaced-tag", tag.line, 1, `${tag.name} must appear before the first EXT-X-PART of its segment; it is ignored`);
92
+ throw new SetAsideSignal();
93
+ }
94
+ };
95
+ const readPart = (tag) => {
96
+ const part = readPart_(context, tag);
97
+ const range = part.byteRange;
98
+ if (range === null) {
99
+ block.partEnd = null;
100
+ return part;
101
+ }
102
+ const previous = block.parts.at(-1);
103
+ if (range.offset !== null) {
104
+ block.partEnd = rangeEnd(range.offset, range.length, tag.line, "EXT-X-PART BYTERANGE");
105
+ return part;
106
+ }
107
+ const continues = previous !== undefined &&
108
+ previous.byteRange !== null &&
109
+ compareSegmentResources(previous.uri, part.uri, context.baseUrl ?? undefined);
110
+ if (continues === false) {
111
+ sink.violation("byterange-without-previous", tag.line, tag.valueColumn, "EXT-X-PART BYTERANGE has no offset and the previous part of its segment is not a sub-range of the same resource");
112
+ block.partEnd = rangeEnd(0, range.length, tag.line, "EXT-X-PART BYTERANGE");
113
+ return { ...part, byteRange: { length: range.length, offset: 0 } };
114
+ }
115
+ block.partEnd =
116
+ continues === true && block.partEnd !== null
117
+ ? rangeEnd(block.partEnd, range.length, tag.line, "EXT-X-PART BYTERANGE")
118
+ : null;
119
+ return part;
120
+ };
121
+ const interpretTag = (tag) => {
122
+ if (interpretHeaderTag(context, header, tag, "media")) {
123
+ return;
124
+ }
125
+ switch (tag.name) {
126
+ case "EXT-X-TARGETDURATION": {
127
+ const value = readTagInteger(context, tag);
128
+ if (!isDuplicate(context, seen, tag)) {
129
+ targetDuration = value;
130
+ }
131
+ return;
132
+ }
133
+ case "EXT-X-MEDIA-SEQUENCE": {
134
+ const value = readTagInteger(context, tag);
135
+ misplacedAfterSegments(tag);
136
+ if (!isDuplicate(context, seen, tag)) {
137
+ mediaSequence = value;
138
+ }
139
+ return;
140
+ }
141
+ case "EXT-X-DISCONTINUITY-SEQUENCE": {
142
+ const value = readTagInteger(context, tag);
143
+ misplacedAfterSegments(tag);
144
+ if (discontinuities > 0 || block.discontinuity) {
145
+ sink.violation("misplaced-tag", tag.line, 1, "EXT-X-DISCONTINUITY-SEQUENCE must appear before any EXT-X-DISCONTINUITY");
146
+ }
147
+ if (!isDuplicate(context, seen, tag)) {
148
+ discontinuitySequence = value;
149
+ }
150
+ return;
151
+ }
152
+ case "EXT-X-ENDLIST": {
153
+ expectNoValue(context, tag);
154
+ if (!isDuplicate(context, seen, tag)) {
155
+ endList = true;
156
+ endListLine = tag.line;
157
+ }
158
+ return;
159
+ }
160
+ case "EXT-X-PLAYLIST-TYPE": {
161
+ const value = sink.lenient ? (tag.value ?? "").trim() : tag.value;
162
+ if (value !== "EVENT" && value !== "VOD") {
163
+ sink.advisory("ignorable-tag", tag.line, tag.valueColumn, `Unrecognized playlist type "${tag.value ?? ""}": a client ignores this tag`);
164
+ throw new SetAsideSignal();
165
+ }
166
+ if (!isDuplicate(context, seen, tag)) {
167
+ playlistType = value;
168
+ }
169
+ return;
170
+ }
171
+ case "EXT-X-I-FRAMES-ONLY": {
172
+ expectNoValue(context, tag);
173
+ if (!isDuplicate(context, seen, tag)) {
174
+ iFramesOnly = true;
175
+ }
176
+ return;
177
+ }
178
+ case "EXT-X-PART-INF": {
179
+ const reader = readAttributes(context, tag);
180
+ const partTarget = reader.float("PART-TARGET", { required: true }) ?? 0;
181
+ const unknownAttributes = reader.unknownAttributes();
182
+ if (!isDuplicate(context, seen, tag)) {
183
+ partInf = { partTarget, unknownAttributes };
184
+ }
185
+ return;
186
+ }
187
+ case "EXT-X-SERVER-CONTROL": {
188
+ const reader = readAttributes(context, tag);
189
+ const value = {
190
+ canSkipUntil: reader.float("CAN-SKIP-UNTIL"),
191
+ canSkipDateRanges: reader.boolean("CAN-SKIP-DATERANGES", {
192
+ yesOnly: true,
193
+ }),
194
+ holdBack: reader.float("HOLD-BACK"),
195
+ partHoldBack: reader.float("PART-HOLD-BACK"),
196
+ canBlockReload: reader.boolean("CAN-BLOCK-RELOAD", { yesOnly: true }),
197
+ unknownAttributes: reader.unknownAttributes(),
198
+ };
199
+ if (!isDuplicate(context, seen, tag)) {
200
+ serverControl = value;
201
+ }
202
+ return;
203
+ }
204
+ case "EXT-X-SKIP": {
205
+ const reader = readAttributes(context, tag);
206
+ const skippedSegments = reader.integer("SKIPPED-SEGMENTS", { required: true }) ?? 0;
207
+ const removed = reader.quoted("RECENTLY-REMOVED-DATERANGES", {
208
+ allowEmpty: true,
209
+ });
210
+ const unknownAttributes = reader.unknownAttributes();
211
+ const tabbed = unknownAttributes.find((attribute) => attribute.value.includes("\t"));
212
+ if (tabbed !== undefined) {
213
+ reader.violation(tabbed.name, "control-character", "Only RECENTLY-REMOVED-DATERANGES may hold a TAB");
214
+ }
215
+ const value = {
216
+ skippedSegments,
217
+ recentlyRemovedDateRanges: removed === null ? null : splitList(removed, "\t"),
218
+ unknownAttributes,
219
+ };
220
+ misplacedAfterSegments(tag);
221
+ if (!isDuplicate(context, seen, tag)) {
222
+ skip = value;
223
+ }
224
+ return;
225
+ }
226
+ case "EXTINF": {
227
+ const extinf = readExtinf_(context, tag);
228
+ if (block.extinf !== null) {
229
+ sink.violation("duplicate-tag", tag.line, 1, "EXTINF appears twice for one segment; the last one is kept");
230
+ }
231
+ block.extinf = extinf;
232
+ return;
233
+ }
234
+ case "EXT-X-BYTERANGE": {
235
+ const value = readByteRange(tag.value ?? "", sink.lenient);
236
+ if (value === "unsafe" || value === null) {
237
+ sink.violation(value === null ? "invalid-tag-value" : "unsafe-integer", tag.line, tag.valueColumn, value === null
238
+ ? "EXT-X-BYTERANGE takes <length>[@<offset>]"
239
+ : "EXT-X-BYTERANGE is past 2^53-1");
240
+ throw new SetAsideSignal();
241
+ }
242
+ block.byteRange = value;
243
+ return;
244
+ }
245
+ case "EXT-X-DISCONTINUITY": {
246
+ expectNoValue(context, tag);
247
+ beforeParts(tag);
248
+ if (block.discontinuity) {
249
+ sink.violation("duplicate-tag", tag.line, 1, "EXT-X-DISCONTINUITY appears twice before one segment");
250
+ }
251
+ block.discontinuity = true;
252
+ return;
253
+ }
254
+ case "EXT-X-KEY": {
255
+ const key = readKey(context, tag, false);
256
+ beforeParts(tag);
257
+ block.keyTags.push(key);
258
+ return;
259
+ }
260
+ case "EXT-X-MAP": {
261
+ const value = readMap_(context, tag);
262
+ beforeParts(tag);
263
+ map = value;
264
+ return;
265
+ }
266
+ case "EXT-X-PROGRAM-DATE-TIME": {
267
+ const value = sink.lenient
268
+ ? (tag.value ?? "").trim()
269
+ : (tag.value ?? "");
270
+ beforeParts(tag);
271
+ checkProgramDate(explicitDates, value, report, tag.line, tag.valueColumn);
272
+ if (block.programDateTime !== null) {
273
+ sink.violation("duplicate-tag", tag.line, 1, "EXT-X-PROGRAM-DATE-TIME appears twice for one segment; the last one is kept");
274
+ }
275
+ block.programDateTime = value;
276
+ anyProgramDateTime = true;
277
+ return;
278
+ }
279
+ case "EXT-X-GAP": {
280
+ expectNoValue(context, tag);
281
+ block.gap = true;
282
+ return;
283
+ }
284
+ case "EXT-X-BITRATE": {
285
+ bitrate = readTagInteger(context, tag);
286
+ return;
287
+ }
288
+ case "EXT-X-PART": {
289
+ block.parts.push(readPart(tag));
290
+ return;
291
+ }
292
+ case "EXT-X-DATERANGE": {
293
+ const range = readDateRange_(context, tag);
294
+ const conflict = mergeDateRangeFields(dateRangeIds, range);
295
+ if (conflict !== null) {
296
+ sink.violation("conflicting-date-range", tag.line, tag.valueColumn, `EXT-X-DATERANGE "${range.id}" declares ${conflict} differently from an earlier declaration`);
297
+ }
298
+ if (dateRangeIds.get(range.id)?.has("START-DATE") !== true &&
299
+ skip === null) {
300
+ sink.violation("missing-attribute", tag.line, tag.valueColumn, `The first EXT-X-DATERANGE "${range.id}" requires START-DATE`);
301
+ }
302
+ firstDateRangeLine ||= tag.line;
303
+ block.dateRanges.push(range);
304
+ return;
305
+ }
306
+ case "EXT-X-PRELOAD-HINT": {
307
+ const reader = readAttributes(context, tag);
308
+ const type = reader.enumerated("TYPE", ["PART", "MAP"], {
309
+ required: true,
310
+ setAsideUnknown: true,
311
+ });
312
+ const hint = {
313
+ type: type ?? "PART",
314
+ uri: reader.quoted("URI", { required: true }) ?? "",
315
+ byteRangeStart: reader.integer("BYTERANGE-START"),
316
+ byteRangeLength: reader.integer("BYTERANGE-LENGTH"),
317
+ unknownAttributes: reader.unknownAttributes(),
318
+ };
319
+ if (hint.byteRangeLength !== null) {
320
+ rangeEnd(hint.byteRangeStart ?? 0, hint.byteRangeLength, tag.line, "EXT-X-PRELOAD-HINT");
321
+ }
322
+ preloadHints.push(hint);
323
+ return;
324
+ }
325
+ case "EXT-X-RENDITION-REPORT": {
326
+ const reader = readAttributes(context, tag);
327
+ const report = {
328
+ uri: reader.quoted("URI", { required: true }) ?? "",
329
+ lastMsn: reader.integer("LAST-MSN"),
330
+ lastPart: reader.integer("LAST-PART"),
331
+ unknownAttributes: reader.unknownAttributes(),
332
+ };
333
+ if (URI_SCHEME_.test(report.uri) || report.uri.startsWith("//")) {
334
+ reader.violation("URI", "invalid-attribute-value", "EXT-X-RENDITION-REPORT URI must be relative to the playlist");
335
+ }
336
+ renditionReports.push(report);
337
+ return;
338
+ }
339
+ default:
340
+ throw new SetAsideSignal();
341
+ }
342
+ };
343
+ const keepUnknown = (tag) => {
344
+ if (segments.length === 0 &&
345
+ isBlockEmpty_(block) &&
346
+ block.keyTags.length === 0) {
347
+ header.unknownTags.push(unknownTag(tag));
348
+ }
349
+ else {
350
+ block.unknownTags.push(unknownTag(tag));
351
+ }
352
+ };
353
+ const blockTags = () => ({
354
+ discontinuity: block.discontinuity,
355
+ keyTags: block.keyTags,
356
+ map,
357
+ programDateTime: block.programDateTime,
358
+ gap: block.gap,
359
+ bitrate,
360
+ parts: block.parts,
361
+ dateRanges: block.dateRanges,
362
+ unknownTags: block.unknownTags,
363
+ });
364
+ const segmentByteRange = (uri, text) => {
365
+ const byteRange = block.byteRange;
366
+ if (byteRange === null) {
367
+ previousRange = null;
368
+ return null;
369
+ }
370
+ if (byteRange.offset !== null) {
371
+ previousRange = {
372
+ uri: text,
373
+ end: rangeEnd(byteRange.offset, byteRange.length, uri.line, "EXT-X-BYTERANGE"),
374
+ };
375
+ return byteRange;
376
+ }
377
+ const previous = segments.at(-1);
378
+ if (previous === undefined && skip !== null) {
379
+ previousRange = { uri: text, end: null };
380
+ return byteRange;
381
+ }
382
+ const continues = previous !== undefined &&
383
+ previousRange !== null &&
384
+ compareSegmentResources(previousRange.uri, text, context.baseUrl ?? undefined);
385
+ if (continues === false) {
386
+ sink.violation("byterange-without-previous", uri.line, 1, "EXT-X-BYTERANGE has no offset and the previous segment is not a sub-range of the same resource");
387
+ previousRange = {
388
+ uri: text,
389
+ end: rangeEnd(0, byteRange.length, uri.line, "EXT-X-BYTERANGE"),
390
+ };
391
+ return { length: byteRange.length, offset: 0 };
392
+ }
393
+ const end = continues === true
394
+ ? (previousRange?.end ?? null)
395
+ : null;
396
+ previousRange = {
397
+ uri: text,
398
+ end: end === null
399
+ ? null
400
+ : rangeEnd(end, byteRange.length, uri.line, "EXT-X-BYTERANGE"),
401
+ };
402
+ return byteRange;
403
+ };
404
+ const interpretUri = (uri) => {
405
+ if (segments.length >= context.limits.maxSegments) {
406
+ throw new HlsLimitError("maxSegments", context.limits.maxSegments, uri.line);
407
+ }
408
+ const text = context.variables.substitute(uri.text, uri.line, uri.column);
409
+ if (block.extinf === null) {
410
+ sink.violation("missing-extinf", uri.line, 1, "Segment URI with no EXTINF before it");
411
+ }
412
+ const byteRange = segmentByteRange(uri, text);
413
+ if (block.discontinuity) {
414
+ discontinuities++;
415
+ }
416
+ segmentLines.push(uri.line);
417
+ segments.push({
418
+ ...blockTags(),
419
+ uri: text,
420
+ duration: block.extinf?.duration ?? 0,
421
+ title: block.extinf?.title ?? null,
422
+ byteRange,
423
+ });
424
+ block = emptyBlock_();
425
+ };
426
+ for (const line of lines) {
427
+ if (line.kind === "uri") {
428
+ interpretUri(line);
429
+ continue;
430
+ }
431
+ try {
432
+ interpretTag(line);
433
+ }
434
+ catch (error) {
435
+ if (!isSetAsideSignal(error)) {
436
+ throw error;
437
+ }
438
+ keepUnknown(line);
439
+ }
440
+ }
441
+ const lastLine = lines.at(-1)?.line ?? 1;
442
+ if (block.extinf !== null || block.byteRange !== null) {
443
+ sink.violation("missing-uri", lastLine, 1, "The last EXTINF or EXT-X-BYTERANGE has no segment URI after it; it is dropped");
444
+ }
445
+ if (block.discontinuity) {
446
+ discontinuities++;
447
+ }
448
+ block.extinf = null;
449
+ block.byteRange = null;
450
+ // With the dropped EXTINF gone, tags it alone kept out of the header belong
451
+ // there, as a reprint would read them.
452
+ if (segments.length === 0 &&
453
+ block.keyTags.length === 0 &&
454
+ isBlockEmpty_({ ...block, unknownTags: [] })) {
455
+ header.unknownTags.push(...block.unknownTags.splice(0));
456
+ }
457
+ const trailer = blockTags();
458
+ if (targetDuration === null) {
459
+ sink.violation("missing-tag", 1, 1, "EXT-X-TARGETDURATION is required");
460
+ targetDuration = segments.reduce((largest, segment) => Math.max(largest, Math.round(segment.duration)), 0);
461
+ }
462
+ else {
463
+ for (const [index, segment] of segments.entries()) {
464
+ if (Math.round(segment.duration) > targetDuration) {
465
+ sink.advisory("segment-exceeds-target-duration", segmentLines[index], 1, `Segment "${segment.uri}" lasts ${segment.duration} s, more than the target duration`);
466
+ }
467
+ }
468
+ }
469
+ if (partInf === null &&
470
+ (trailer.parts.length > 0 ||
471
+ segments.some((segment) => segment.parts.length > 0))) {
472
+ sink.violation("missing-tag", 1, 1, "EXT-X-PART-INF is required with EXT-X-PART");
473
+ }
474
+ if (endList && preloadHints.length > 0) {
475
+ sink.violation("misplaced-tag", endListLine, 1, "A playlist with EXT-X-ENDLIST must not carry EXT-X-PRELOAD-HINT");
476
+ }
477
+ if (skip === null) {
478
+ checkDateRangeTimeline(firstDateRangeLine, anyProgramDateTime, report);
479
+ }
480
+ checkSequenceBounds(mediaSequence, discontinuitySequence, segments.length, skip?.skippedSegments ?? 0, discontinuities, report, lastLine);
481
+ return {
482
+ kind: "media",
483
+ version: header.version,
484
+ independentSegments: header.independentSegments,
485
+ start: header.start,
486
+ defines: header.defines,
487
+ unknownTags: header.unknownTags,
488
+ targetDuration,
489
+ mediaSequence,
490
+ discontinuitySequence,
491
+ endList,
492
+ playlistType,
493
+ iFramesOnly,
494
+ partInf,
495
+ serverControl,
496
+ skip,
497
+ segments,
498
+ trailer,
499
+ preloadHints,
500
+ renditionReports,
501
+ };
502
+ }
503
+ function readExtinf_(context, tag) {
504
+ const { sink } = context;
505
+ const value = tag.value ?? "";
506
+ const comma = value.indexOf(",");
507
+ if (comma === -1) {
508
+ sink.violation("invalid-tag-value", tag.line, tag.valueColumn, "EXTINF needs a comma after the duration");
509
+ }
510
+ const durationText = comma === -1 ? value : value.slice(0, comma);
511
+ const duration = readDecimalFloat(durationText, sink.lenient, false);
512
+ if (duration === null) {
513
+ sink.violation("invalid-tag-value", tag.line, tag.valueColumn, "EXTINF duration is not a finite decimal number");
514
+ throw new SetAsideSignal();
515
+ }
516
+ const title = comma === -1 ? "" : value.slice(comma + 1);
517
+ return { duration, title: title.trim().length === 0 ? null : title };
518
+ }
519
+ /**
520
+ * Reads `EXT-X-KEY` or `EXT-X-SESSION-KEY`, never setting an `EXT-X-KEY` aside.
521
+ * A `METHOD` that is absent, quoted or empty, or a `NONE` in a malformed
522
+ * attribute list or beside other attributes, is a violation; lenient mode then
523
+ * reads the method as `""` with the raw attribute kept in `unknownAttributes`,
524
+ * so a malformed declaration never clears the keys in effect.
525
+ */
526
+ export function readKey(context, tag, session) {
527
+ const reader = readAttributes(context, tag, true);
528
+ const methodText = reader.text("METHOD");
529
+ let method = "";
530
+ if (methodText === null) {
531
+ reader.violation("METHOD", "missing-attribute", `${tag.name} requires METHOD`);
532
+ }
533
+ else if (methodText.quoted || methodText.value.length === 0) {
534
+ reader.violation("METHOD", "invalid-attribute-value", "METHOD must be an unquoted enumerated-string");
535
+ reader.reject("METHOD");
536
+ }
537
+ else {
538
+ method = methodText.value;
539
+ if (!KEY_METHODS_.includes(method)) {
540
+ context.sink.advisory("ignorable-tag", tag.line, tag.valueColumn, `Unrecognized key method "${method}"; segments under it still count as encrypted`);
541
+ }
542
+ }
543
+ if (method === "NONE") {
544
+ if (session) {
545
+ reader.violation("METHOD", "invalid-attribute-value", "EXT-X-SESSION-KEY must not use METHOD=NONE");
546
+ reader.setAside();
547
+ }
548
+ // Scanner recovery can drop duplicates and malformed attributes.
549
+ if (tag.value === "METHOD=NONE") {
550
+ return {
551
+ method,
552
+ uri: null,
553
+ iv: null,
554
+ keyFormat: null,
555
+ keyFormatVersions: null,
556
+ unknownAttributes: [],
557
+ };
558
+ }
559
+ reader.violation("METHOD", "invalid-attribute-value", "METHOD=NONE requires a clean attribute list with no other attribute");
560
+ reader.reject("METHOD");
561
+ method = "";
562
+ }
563
+ if (!reader.has("URI") && method !== "") {
564
+ reader.violation("URI", "missing-attribute", `${tag.name} requires URI unless METHOD is NONE`);
565
+ }
566
+ const uri = reader.quoted("URI");
567
+ const iv = reader.hexadecimal("IV");
568
+ const keyFormat = reader.quoted("KEYFORMAT");
569
+ const versionsText = reader.quoted("KEYFORMATVERSIONS");
570
+ let keyFormatVersions = null;
571
+ if (versionsText !== null) {
572
+ if (KEY_FORMAT_VERSIONS_.test(versionsText)) {
573
+ keyFormatVersions = versionsText.split("/").map(Number);
574
+ }
575
+ if (keyFormatVersions === null ||
576
+ !keyFormatVersions.every(Number.isSafeInteger)) {
577
+ reader.violation("KEYFORMATVERSIONS", keyFormatVersions === null
578
+ ? "invalid-attribute-value"
579
+ : "unsafe-integer", "KEYFORMATVERSIONS must be integers separated by /");
580
+ reader.reject("KEYFORMATVERSIONS");
581
+ keyFormatVersions = null;
582
+ }
583
+ }
584
+ return {
585
+ method,
586
+ uri,
587
+ iv,
588
+ keyFormat,
589
+ keyFormatVersions,
590
+ unknownAttributes: reader.unknownAttributes(),
591
+ };
592
+ }
593
+ function readMap_(context, tag) {
594
+ const reader = readAttributes(context, tag);
595
+ const uri = reader.quoted("URI", { required: true }) ?? "";
596
+ let byteRange = reader.byteRange("BYTERANGE");
597
+ if (byteRange !== null && byteRange.offset === null) {
598
+ reader.violation("BYTERANGE", "invalid-attribute-value", "EXT-X-MAP BYTERANGE requires an offset");
599
+ reader.reject("BYTERANGE");
600
+ byteRange = null;
601
+ }
602
+ if (byteRange !== null &&
603
+ byteRange.length - 1 > Number.MAX_SAFE_INTEGER - (byteRange.offset ?? 0)) {
604
+ reader.violation("BYTERANGE", "unsafe-integer", "EXT-X-MAP BYTERANGE ends past 2^53-1");
605
+ reader.reject("BYTERANGE");
606
+ byteRange = null;
607
+ }
608
+ return {
609
+ uri,
610
+ byteRange: byteRange === null
611
+ ? null
612
+ : { length: byteRange.length, offset: byteRange.offset ?? 0 },
613
+ unknownAttributes: reader.unknownAttributes(),
614
+ };
615
+ }
616
+ function readPart_(context, tag) {
617
+ const reader = readAttributes(context, tag);
618
+ return {
619
+ uri: reader.quoted("URI", { required: true }) ?? "",
620
+ duration: reader.float("DURATION", { required: true }) ?? 0,
621
+ independent: reader.boolean("INDEPENDENT", { yesOnly: true }),
622
+ byteRange: reader.byteRange("BYTERANGE"),
623
+ gap: reader.boolean("GAP", { yesOnly: true }),
624
+ unknownAttributes: reader.unknownAttributes(),
625
+ };
626
+ }
627
+ function readDate_(reader, name) {
628
+ const value = reader.quoted(name);
629
+ if (value !== null && parseHlsDateTime(value) === null) {
630
+ reader.violation(name, "invalid-date-time", `${name} is not an ISO 8601 date-time`);
631
+ }
632
+ return value;
633
+ }
634
+ function readDateRange_(context, tag) {
635
+ const reader = readAttributes(context, tag);
636
+ const id = reader.quoted("ID", { required: true }) ?? "";
637
+ const dateRangeClass = reader.quoted("CLASS");
638
+ const startDate = readDate_(reader, "START-DATE");
639
+ const cue = reader.quoted("CUE");
640
+ const endDate = readDate_(reader, "END-DATE");
641
+ const duration = reader.float("DURATION");
642
+ const plannedDuration = reader.float("PLANNED-DURATION");
643
+ const endOnNext = reader.boolean("END-ON-NEXT", { yesOnly: true });
644
+ const scte35Cmd = reader.hexadecimal("SCTE35-CMD");
645
+ const scte35Out = reader.hexadecimal("SCTE35-OUT");
646
+ const scte35In = reader.hexadecimal("SCTE35-IN");
647
+ const clientAttributes = [];
648
+ const unknownAttributes = [];
649
+ for (const attribute of reader.unknownAttributes()) {
650
+ if (!attribute.name.startsWith("X-")) {
651
+ unknownAttributes.push(attribute);
652
+ continue;
653
+ }
654
+ if (!attribute.quoted &&
655
+ !isHexadecimalSequence(attribute.value, false) &&
656
+ !SIGNED_DECIMAL_.test(attribute.value)) {
657
+ reader.violation(attribute.name, "invalid-attribute-value", `${attribute.name} must be a quoted-string, a hexadecimal-sequence or a signed decimal`);
658
+ }
659
+ clientAttributes.push(attribute);
660
+ }
661
+ return {
662
+ id,
663
+ class: dateRangeClass,
664
+ startDate,
665
+ endDate,
666
+ cue: cue === null ? [] : splitList(cue, ","),
667
+ duration,
668
+ plannedDuration,
669
+ endOnNext,
670
+ scte35Cmd,
671
+ scte35Out,
672
+ scte35In,
673
+ clientAttributes,
674
+ unknownAttributes,
675
+ };
676
+ }
677
+ //# sourceMappingURL=_interpret-media.js.map