UNPKG

3.42 kBJavaScriptView Raw
1/* global window */
2const fs = require('fs');
3const path = require('path');
4const baseDir = path.join(__dirname, '..');
5const manifestsDir = path.join(baseDir, 'test', 'manifests');
6const segmentsDir = path.join(baseDir, 'test', 'segments');
7
8const base64ToUint8Array = function(base64) {
9 const decoded = window.atob(base64);
10 const uint8Array = new Uint8Array(new ArrayBuffer(decoded.length));
11
12 for (let i = 0; i < decoded.length; i++) {
13 uint8Array[i] = decoded.charCodeAt(i);
14 }
15
16 return uint8Array;
17};
18
19const getManifests = () => (fs.readdirSync(manifestsDir) || [])
20 .filter((f) => ((/\.(m3u8|mpd)/).test(path.extname(f))))
21 .map((f) => path.resolve(manifestsDir, f));
22
23const getSegments = () => (fs.readdirSync(segmentsDir) || [])
24 .filter((f) => ((/\.(ts|mp4|key|webm|aac|ac3|vtt)/).test(path.extname(f))))
25 .map((f) => path.resolve(segmentsDir, f));
26
27const buildManifestString = function() {
28 let manifests = 'export default {\n';
29
30 getManifests().forEach((file) => {
31 // translate this manifest
32 manifests += ' \'' + path.basename(file, path.extname(file)) + '\': ';
33 manifests += fs.readFileSync(file, 'utf8')
34 .split(/\r\n|\n/)
35 // quote and concatenate
36 .map((line) => ' \'' + line + '\\n\' +\n')
37 .join('')
38 // strip leading spaces and the trailing '+'
39 .slice(4, -3);
40 manifests += ',\n';
41 });
42
43 // clean up and close the objects
44 manifests = manifests.slice(0, -2);
45 manifests += '\n};\n';
46
47 return manifests;
48};
49
50const buildSegmentString = function() {
51 const segmentData = {};
52
53 getSegments().forEach((file) => {
54 // read the file directly as a buffer before converting to base64
55 const base64Segment = fs.readFileSync(file).toString('base64');
56
57 segmentData[path.basename(file, path.extname(file))] = base64Segment;
58 });
59
60 const segmentDataExportStrings = Object.keys(segmentData).reduce((acc, key) => {
61 // use a function since the segment may be cleared out on usage
62 acc.push(`export const ${key} = () => {
63 cache.${key} = cache.${key} || base64ToUint8Array('${segmentData[key]}');
64 const dest = new Uint8Array(cache.${key}.byteLength);
65 dest.set(cache.${key});
66 return dest;
67 };`);
68
69 return acc;
70 }, []);
71
72 const segmentsFile =
73 'const cache = {};\n' +
74 `const base64ToUint8Array = ${base64ToUint8Array.toString()};\n` +
75 segmentDataExportStrings.join('\n');
76
77 return segmentsFile;
78};
79
80/* we refer to them as .js, so that babel and other plugins can work on them */
81const segmentsKey = 'create-test-data!segments.js';
82const manifestsKey = 'create-test-data!manifests.js';
83
84module.exports = function() {
85 return {
86 name: 'createTestData',
87 buildStart() {
88 this.addWatchFile(segmentsDir);
89 this.addWatchFile(manifestsDir);
90
91 [].concat(getSegments())
92 .concat(getManifests())
93 .forEach((file) => this.addWatchFile(file));
94 },
95 resolveId(importee, importer) {
96 // if this is not an id we can resolve return
97 if (importee.indexOf('create-test-data!') !== 0) {
98 return;
99 }
100
101 const name = importee.split('!')[1];
102
103 return (name.indexOf('segments') === 0) ? segmentsKey : manifestsKey;
104 },
105 load(id) {
106 if (id === segmentsKey) {
107 return buildSegmentString.call(this);
108 }
109
110 if (id === manifestsKey) {
111 return buildManifestString.call(this);
112 }
113 }
114 };
115};