52308f18 by brandonocasey

fixing code review issues

1 parent a2a34318
......@@ -34,5 +34,5 @@ dist-test/
docs/api/
es5/
tmp
test/data/manifests.js
test/data/expected.js
test/test-manifests.js
test/test-expected.js
......
var fs = require('fs');
var path = require('path');
var shelljs = require('shelljs');
var basePath = path.resolve(__dirname, '..');
var testDataDir = path.join(basePath,'test', 'data');
var testDataDir = path.join(basePath,'test');
var manifestDir = path.join(basePath, 'utils', 'manifest');
var manifestFilepath = path.join(testDataDir, 'manifests.js');
var expectedFilepath = path.join(testDataDir, 'expected.js');
var manifestFilepath = path.join(testDataDir, 'test-manifests.js');
var expectedFilepath = path.join(testDataDir, 'test-expected.js');
var build = function() {
var manifests = 'export default {\n';
var expected = 'export default {\n';
shelljs.mkdir('-p', testDataDir);
var files = fs.readdirSync(manifestDir);
while (files.length > 0) {
var file = path.resolve(manifestDir, files.shift());
......
......@@ -35,29 +35,6 @@ export class LineStream extends Stream {
}
}
/**
* A line-level M3U8 parser event stream. It expects to receive input one
* line at a time and performs a context-free parse of its contents. A stream
* interpretation of a manifest can be useful if the manifest is expected to
* be too large to fit comfortably into memory or the entirety of the input
* is not immediately available. Otherwise, it's probably much easier to work
* with a regular `Parser` object.
*
* Produces `data` events with an object that captures the parser's
* interpretation of the input. That object has a property `tag` that is one
* of `uri`, `comment`, or `tag`. URIs only have a single additional
* property, `line`, which captures the entirety of the input without
* interpretation. Comments similarly have a single additional property
* `text` which is the input without the leading `#`.
*
* Tags always have a property `tagType` which is the lower-cased version of
* the M3U8 directive without the `#EXT` or `#EXT-X-` prefix. For instance,
* `#EXT-X-MEDIA-SEQUENCE` becomes `media-sequence` when parsed. Unrecognized
* tags are given the tag type `unknown` and a single additional property
* `data` with the remainder of the input.
*/
// "forgiving" attribute list psuedo-grammar:
// attributes -> keyvalue (',' keyvalue)*
// keyvalue -> key '=' value
......@@ -95,6 +72,27 @@ const parseAttributes = function(attributes) {
return result;
};
/**
* A line-level M3U8 parser event stream. It expects to receive input one
* line at a time and performs a context-free parse of its contents. A stream
* interpretation of a manifest can be useful if the manifest is expected to
* be too large to fit comfortably into memory or the entirety of the input
* is not immediately available. Otherwise, it's probably much easier to work
* with a regular `Parser` object.
*
* Produces `data` events with an object that captures the parser's
* interpretation of the input. That object has a property `tag` that is one
* of `uri`, `comment`, or `tag`. URIs only have a single additional
* property, `line`, which captures the entirety of the input without
* interpretation. Comments similarly have a single additional property
* `text` which is the input without the leading `#`.
*
* Tags always have a property `tagType` which is the lower-cased version of
* the M3U8 directive without the `#EXT` or `#EXT-X-` prefix. For instance,
* `#EXT-X-MEDIA-SEQUENCE` becomes `media-sequence` when parsed. Unrecognized
* tags are given the tag type `unknown` and a single additional property
* `data` with the remainder of the input.
*/
export class ParseStream extends Stream {
constructor() {
super();
......@@ -341,7 +339,24 @@ export class ParseStream extends Stream {
}
}
/**
* A parser for M3U8 files. The current interpretation of the input is
* exposed as a property `manifest` on parser objects. It's just two lines to
* create and parse a manifest once you have the contents available as a string:
*
* ```js
* var parser = new videojs.m3u8.Parser();
* parser.push(xhr.responseText);
* ```
*
* New input can later be applied to update the manifest object by calling
* `push` again.
*
* The parser attempts to create a usable manifest object even if the
* underlying input is somewhat nonsensical. It emits `info` and `warning`
* events during the parse if it encounters input that seems invalid or
* requires some property of the manifest object to be defaulted.
*/
export class Parser extends Stream {
constructor() {
super();
......@@ -493,10 +508,8 @@ export class Parser extends Stream {
if (!currentUri.attributes) {
currentUri.attributes = {};
}
currentUri.attributes = mergeOptions(
currentUri.attributes,
entry.attributes
);
currentUri.attributes = mergeOptions(currentUri.attributes,
entry.attributes);
},
discontinuity() {
currentUri.discontinuity = true;
......
import {ParseStream, LineStream, Parser} from '../src/m3u8';
import QUnit from 'qunit';
import testDataExpected from './data/expected.js';
import testDataManifests from './data/manifests.js';
import testDataExpected from './test-expected.js';
import testDataManifests from './test-manifests.js';
QUnit.module('LineStream', {
beforeEach() {
......@@ -68,11 +68,9 @@ QUnit.test('stops sending events after deregistering', function() {
this.lineStream.on('data', temporary);
this.lineStream.on('data', permanent);
this.lineStream.push('line one\n');
QUnit.strictEqual(
temporaryLines.length,
QUnit.strictEqual(temporaryLines.length,
permanentLines.length,
'both callbacks receive the event'
);
'both callbacks receive the event');
QUnit.ok(this.lineStream.off('data', temporary), 'a listener was removed');
this.lineStream.push('line two\n');
......@@ -501,11 +499,9 @@ QUnit.test('parses #EXT-X-STREAM-INF with arbitrary attributes', function() {
QUnit.strictEqual(element.type, 'tag', 'the line type is tag');
QUnit.strictEqual(element.tagType, 'stream-inf', 'the tag type is stream-inf');
QUnit.strictEqual(element.attributes.NUMERIC, '24', 'numeric attributes are parsed');
QUnit.strictEqual(
element.attributes.ALPHA,
QUnit.strictEqual(element.attributes.ALPHA,
'Value',
'alphabetic attributes are parsed'
);
'alphabetic attributes are parsed');
QUnit.strictEqual(element.attributes.MIXED, '123abc', 'mixed attributes are parsed');
});
// #EXT-X-ENDLIST
......@@ -640,8 +636,7 @@ QUnit.test('parses static manifests as expected', function() {
let parser = new Parser();
parser.push(testDataManifests[key]);
QUnit.deepEqual(
parser.manifest,
QUnit.deepEqual(parser.manifest,
testDataExpected[key],
key + '.m3u8 was parsed correctly'
);
......
import manifests from './data/manifests';
import expected from './data/expected';
import manifests from './test-manifests';
import expected from './test-expected';
window.manifests = manifests;
window.expected = expected;
......