1 |
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 | const baseDir = path.join(__dirname, '..');
|
5 | const manifestsDir = path.join(baseDir, 'test', 'manifests');
|
6 | const segmentsDir = path.join(baseDir, 'test', 'segments');
|
7 |
|
8 | const 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 |
|
19 | const getManifests = () => (fs.readdirSync(manifestsDir) || [])
|
20 | .filter((f) => ((/\.(m3u8|mpd)/).test(path.extname(f))))
|
21 | .map((f) => path.resolve(manifestsDir, f));
|
22 |
|
23 | const 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 |
|
27 | const buildManifestString = function() {
|
28 | let manifests = 'export default {\n';
|
29 |
|
30 | getManifests().forEach((file) => {
|
31 |
|
32 | manifests += ' \'' + path.basename(file, path.extname(file)) + '\': ';
|
33 | manifests += fs.readFileSync(file, 'utf8')
|
34 | .split(/\r\n|\n/)
|
35 |
|
36 | .map((line) => ' \'' + line + '\\n\' +\n')
|
37 | .join('')
|
38 |
|
39 | .slice(4, -3);
|
40 | manifests += ',\n';
|
41 | });
|
42 |
|
43 |
|
44 | manifests = manifests.slice(0, -2);
|
45 | manifests += '\n};\n';
|
46 |
|
47 | return manifests;
|
48 | };
|
49 |
|
50 | const buildSegmentString = function() {
|
51 | const segmentData = {};
|
52 |
|
53 | getSegments().forEach((file) => {
|
54 |
|
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 |
|
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 |
|
81 | const segmentsKey = 'create-test-data!segments.js';
|
82 | const manifestsKey = 'create-test-data!manifests.js';
|
83 |
|
84 | module.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 |
|
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 | };
|