├── README.md ├── index.js ├── package-lock.json ├── package.json ├── utils.js └── walk-manifest.js /README.md: -------------------------------------------------------------------------------- 1 | # artillery-plugin-hls - load test HLS streaming with Artillery 2 | 3 | ## Description 4 | 5 | This plugin adds HTTP Live Streaming (HLS) support to [Artillery](https://artillery.io) to make it possible to load test HLS streaming servers. With the plugin enabled, when a request to an M3U8 playlist is made, Artillery will parse the playlist, pick one of the alternative streams, and download its segments (`.ts` files). 6 | 7 | 🐞 Please report issues over at [https://github.com/shoreditch-ops/artillery/issues](https://github.com/shoreditch-ops/artillery/issues) 8 | 9 | ### HTTP Live Streaming (HLS) 10 | 11 | > HTTP Live Streaming (also known as HLS) is an HTTP-based media streaming communications protocol implemented by Apple Inc. as part of its QuickTime, Safari, OS X, and iOS software. Client implementations are also available in Microsoft Edge, Firefox and some versions of Google Chrome. Support is widespread in streaming media servers. 12 | 13 | Source: [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) on Wikipedia 14 | 15 | Additional reading: 16 | - [HTTP Live Streaming](https://developer.apple.com/streaming/) - official Apple documentation 17 | 18 | ## Usage 19 | 20 | ### Install the plugin 21 | 22 | ``` 23 | npm install -g artillery-plugin-hls 24 | ``` 25 | 26 | ### Load the plugin in your test configuration 27 | 28 | ```yaml 29 | config: 30 | target: "https://video.example.com" 31 | plugins: 32 | hls: {} 33 | ``` 34 | 35 | ### Make a request to an M3U8 playlist 36 | 37 | To stream an HLS-encoded video, make a request to an `m3u8` playlist, and set the `hls` attribute to `{}` to use the default options, or customize streaming options: 38 | 39 | ```yaml 40 | scenarios: 41 | - name: "Stream an HLS video" 42 | flow: 43 | - get: 44 | url: "/streams/xyz0123/xyz0123.m3u8" 45 | hls: 46 | concurrency: 2 47 | streamSelector: 48 | resolution: 49 | width: 320 50 | height: 184 51 | throttle: 100 52 | ``` 53 | 54 | ## HLS Configuration 55 | 56 | The following options are supported on the `hls` attribute: 57 | 58 | - `concurrency` - the number of segments to download concurrently; defaults to 4 59 | - `throttle` - set to a number (in kb/sec) to throttle bandwidth available to download that stream 60 | - `streamSelector` - specify how an alternative stream should be selected; defaults to a random stream. 61 | 62 | ### streamSelector options 63 | 64 | - `resolution` - set `width` and `height` to pick the stream that matches 65 | - `name` - set to match on the `NAME` attribute of a stream 66 | - `bandwidth` - set to a number to select a specific bandwidth; or to `"min"`/`"max"` to select the lowest/highest bandwidth 67 | - `index` - set to a number to select the alternative stream at that index in the playlist, `"random"` to select a stream at random, or `"all"` to iterate through all streams. 68 | 69 | ## Reported Metrics 70 | 71 | The plugin adds a number of custom metrics to Artillery reports: 72 | 73 | - **HLS: segment download started** - the number of individual segments (`.ts` files) requested from the server 74 | - **HLS: segment download completed** - the number of segments downloaded 75 | - **HLS: segment download time** - the amount of time it's taken to download a segment 76 | - **HLS: stream download time** - the amount of time it's taken to download an entire stream 77 | 78 | ## Load Testing HLS at Scale 79 | 80 | Streaming video with a large number of virtual users can quickly exhaust all available bandwidth on a single machine. Therefore bandwidth utilization should be monitored closely while tests run to scale out the number of nodes on which Artillery runs as needed. 81 | 82 | [Artillery Pro](https://artillery.io/pro) provides a solution for running tests from a cluster of nodes easily as well as a number of other team and enterprise-oriented features, and deep AWS integration. 83 | 84 | ## License 85 | 86 | Some of the code in this project has been adopted from [hls-fetcher](https://github.com/videojs/hls-fetcher/), licensed under MIT: 87 | 88 | - [walk-manifest.js](./walk-manifest.js) 89 | - [utils.js](./utils.js) 90 | 91 | The rest of the code is licensed under MPL-2.0. 92 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const debug = require('debug')('plugin:hls'); 4 | const A = require('async'); 5 | const request = require('requestretry'); 6 | const ratelimit = require('ratelimit'); 7 | const walkManifest = require('./walk-manifest'); 8 | 9 | module.exports = { 10 | Plugin: HlsPlugin 11 | }; 12 | 13 | const METRICS = { 14 | SEGMENT_STARTED: 'HLS: segment download started', 15 | SEGMENT_COMPLETED: 'HLS: segment download completed', 16 | STREAM_STARTED: 'HLS: stream download started', 17 | STREAM_COMPLETED: 'HLS: stream download completed' 18 | }; 19 | 20 | function download(concurrency, resources, throttle, events, callback) { 21 | A.eachLimit( 22 | resources.filter(r => !r.content && r.uri), 23 | concurrency, 24 | function(item, done) { 25 | var options = { 26 | uri: item.uri, 27 | timeout: 60 * 1000, // 60 seconds timeout 28 | encoding: null, // treat all responses as a buffer 29 | retryDelay: 1000 // retry 1s after on failure 30 | }; 31 | events.emit('counter', METRICS.SEGMENT_STARTED, 1); 32 | const startedAt = Date.now(); 33 | request(options, (err) => { 34 | events.emit('counter', METRICS.SEGMENT_COMPLETED, 1); 35 | const delta = Date.now() - startedAt; 36 | events.emit('customStat', { 37 | stat: 'HLS: segment download time', 38 | value: delta 39 | }); 40 | return done(err); 41 | }) 42 | .on('request', (req) => { 43 | }) 44 | .on('error', (err) => { 45 | }) 46 | .on('response', function(res) { 47 | if (throttle != Infinity) { 48 | ratelimit(res, throttle * 1000); 49 | let dataLength = 0; 50 | let startTime; 51 | res.on('data', function(data) { 52 | let now = Date.now(); 53 | if (!startTime) startTime = now; 54 | dataLength += data.length; 55 | let bytesPerSec = Math.ceil(dataLength / (now - startTime) * 1000); 56 | 57 | if (now - startTime > 5000) { 58 | startTime = now; 59 | dataLength = 0; 60 | debug( 61 | 'avg bandwidth seconds: %s/sec', 62 | bytesPerSec 63 | ); 64 | // TODO: Output this as a custom metric 65 | } 66 | }); 67 | } 68 | }); 69 | }, 70 | callback); 71 | } 72 | 73 | function HlsPlugin(script, event, opts) { 74 | const renderVariables = opts.util ? opts.util.renderVariables : x => x; 75 | 76 | if (!script.config.processor) { 77 | script.config.processor = {}; 78 | } 79 | 80 | function randomStreamSelector(variantStreams) { 81 | const index = Math.floor(Math.random() * variantStreams.length); 82 | debug('random index', index); 83 | debug(variantStreams[index]); 84 | return [].concat(variantStreams[index] || []); 85 | } 86 | 87 | function createStreamSelector(requestParams) { 88 | let streamSelector; 89 | 90 | // Stream variant object: 91 | // { attributes: 92 | // { NAME: '720', 93 | // RESOLUTION: { width: 1280, height: 720 }, 94 | // CODECS: 'mp4a.40.2,avc1.64001f', 95 | // BANDWIDTH: 2149280, 96 | // 'PROGRAM-ID': 1 }, 97 | // uri: 'url_0/193039199_mp4_h264_aac_hd_7.m3u8', 98 | // timeline: 0 } 99 | 100 | // TODO: Add validation for streamSelector attribute 101 | // TODO: Add templating for stream selector options 102 | if (requestParams.hls.streamSelector) { 103 | const selectorParams = requestParams.hls.streamSelector; 104 | 105 | // 106 | // Resolution selector 107 | // 108 | // TODO: max/min selectors 109 | if (selectorParams.resolution) { 110 | streamSelector = function(variantStreams) { 111 | const matchingStreams = variantStreams.filter(s => { 112 | // TODO: Handle the case when no matching resolution is found 113 | return ( 114 | s.attributes.RESOLUTION.width === 115 | selectorParams.resolution.width && 116 | s.attributes.RESOLUTION.height === 117 | selectorParams.resolution.height 118 | ); 119 | }); 120 | return [].concat(matchingStreams[0] || []); 121 | }; 122 | } 123 | 124 | if (selectorParams.name) { 125 | streamSelector = function(variantStreams) { 126 | return variantStreams.filter(s => { 127 | return ( 128 | (s.attributes.NAME || '').toLowerCase() === 129 | selectorParams.name.toLowerCase() 130 | ); 131 | }); 132 | }; 133 | } 134 | 135 | if (selectorParams.bandwidth) { 136 | if (selectorParams.bandwidth === 'max') { 137 | let streamWithMax = { attributes: { BANDWIDTH: -1 } }; 138 | variantStreams.forEach(s => { 139 | if (s.attributes.BANDWIDTH > streamWithMax.attributes.BANDWIDTH) { 140 | streamWithMax = s; 141 | } 142 | }); 143 | return [].concat(streamWithMax); 144 | } else if (selectorParams.bandwidth === 'min') { 145 | let streamWithMin = { attributes: { BANDWIDTH: Infinity } }; 146 | variantStreams.forEach(s => { 147 | if (s.attributes.BANDWIDTH < streamWithMin.attributes.BANDWIDTH) { 148 | streamWithMin = s; 149 | } 150 | }); 151 | return [].concat(streamWithMin); 152 | } else if (typeof selectorParams.bandwidth === 'number') { 153 | return variantStreams.filter(s => { 154 | return s.attributes.BANDWIDTH === selectorParams.bandwidth; 155 | }); 156 | } else { 157 | return []; 158 | } 159 | } 160 | 161 | if (selectorParams.index) { 162 | if (typeof selectorParams.index === 'number') { 163 | // Returns the last element if the index is out of bounds 164 | streamSelector = function(variantStreams) { 165 | return variantStreams.slice( 166 | Math.min(selectorParams.index, variantStreams.length), 167 | selectorParams.index + 1 168 | ); 169 | }; 170 | } else if (selectorParams.index === 'random') { 171 | streamSelector = randomStreamSelector; 172 | } else if (selectorParams.index === 'all') { 173 | streamSelector = function(variantStreams) { 174 | return variantStreams; 175 | }; 176 | } 177 | } 178 | } else { 179 | // no streamSelector attribute - treat the same as random 180 | streamSelector = randomStreamSelector; 181 | } 182 | 183 | return streamSelector; 184 | } 185 | 186 | function stream(requestParams, ctx, events, callback) { 187 | requestParams.url = renderVariables(requestParams.url, ctx); 188 | 189 | let concurrency = 4; 190 | let streamSelector = createStreamSelector(requestParams); 191 | let throttle; 192 | 193 | if (requestParams.hls.concurrency) { 194 | concurrency = renderVariables(requestParams.hls.concurrency, ctx); 195 | } 196 | if (typeof requestParams.hls.throttle === 'number') { 197 | throttle = renderVariables(requestParams.hls.throttle, ctx); 198 | } else { 199 | throttle = Infinity; 200 | } 201 | 202 | const resources = walkManifest( 203 | false, 204 | '/dev/null', 205 | requestParams.url, 206 | false, 207 | 0, 208 | function(variantStreams) { 209 | const results = streamSelector(variantStreams); 210 | debug('Streams selected:'); 211 | debug(results); 212 | return results; 213 | } 214 | ); 215 | const startedAt = Date.now(); 216 | events.emit('counter', METRICS.STREAM_STARTED, 1); 217 | download(concurrency, resources, throttle, events, function(err) { 218 | if (err) { 219 | debug(err); 220 | events.emit('error', err); 221 | return callback(err); 222 | } 223 | const delta = Date.now() - startedAt; 224 | events.emit('counter', METRICS.STREAM_COMPLETED, 1); 225 | events.emit('customStat', { 226 | stat: 'HLS: stream download time', 227 | value: delta 228 | }); 229 | return callback(); 230 | }); 231 | } 232 | 233 | script.config.processor.hlsPluginStream = function( 234 | requestParams, 235 | res, 236 | userContext, 237 | events, 238 | done 239 | ) { 240 | // TODO: We already have the master playlist here in res.body() but for 241 | // now the WalkManifest implementation will re-request it again. 242 | 243 | // NOTE: hls property must be set. Can be an object or boolean value of true. 244 | if (typeof requestParams.hls === 'boolean' && requestParams.hls) { 245 | requestParams.hls = {}; 246 | } 247 | if (typeof requestParams.hls === 'object') { 248 | stream(requestParams, userContext, events, err => { 249 | done(err); 250 | }); 251 | } else { 252 | process.nextTick(done); 253 | } 254 | }; 255 | 256 | script.scenarios.forEach(scenario => { 257 | if (!scenario.afterResponse) { 258 | scenario.afterResponse = []; 259 | } 260 | scenario.afterResponse.push('hlsPluginStream'); 261 | }); 262 | 263 | debug('Plugin initialized'); 264 | } 265 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "artillery-plugin-hls", 3 | "version": "0.0.3", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@types/concat-stream": { 8 | "version": "1.6.0", 9 | "resolved": "https://registry.npmjs.org/@types/concat-stream/-/concat-stream-1.6.0.tgz", 10 | "integrity": "sha1-OU2+C7X+5Gs42JZzXoto7yOQ0A0=", 11 | "requires": { 12 | "@types/node": "*" 13 | } 14 | }, 15 | "@types/form-data": { 16 | "version": "0.0.33", 17 | "resolved": "https://registry.npmjs.org/@types/form-data/-/form-data-0.0.33.tgz", 18 | "integrity": "sha1-yayFsqX9GENbjIXZ7LUObWyJP/g=", 19 | "requires": { 20 | "@types/node": "*" 21 | } 22 | }, 23 | "@types/node": { 24 | "version": "9.6.14", 25 | "resolved": "https://registry.npmjs.org/@types/node/-/node-9.6.14.tgz", 26 | "integrity": "sha512-a0mGovBom+nXeDQkgS13AgHGpN+DLR+qTuRWZA7y9FTEiuTbDlXUwhz4+yShOobZGebmjBcl+tTEirTwBlA2LA==" 27 | }, 28 | "@types/qs": { 29 | "version": "6.5.1", 30 | "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.5.1.tgz", 31 | "integrity": "sha512-mNhVdZHdtKHMMxbqzNK3RzkBcN1cux3AvuCYGTvjEIQT2uheH3eCAyYsbMbh2Bq8nXkeOWs1kyDiF7geWRFQ4Q==" 32 | }, 33 | "ajv": { 34 | "version": "5.5.2", 35 | "resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz", 36 | "integrity": "sha1-c7Xuyj+rZT49P5Qis0GtQiBdyWU=", 37 | "requires": { 38 | "co": "^4.6.0", 39 | "fast-deep-equal": "^1.0.0", 40 | "fast-json-stable-stringify": "^2.0.0", 41 | "json-schema-traverse": "^0.3.0" 42 | } 43 | }, 44 | "asap": { 45 | "version": "2.0.6", 46 | "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", 47 | "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=" 48 | }, 49 | "asn1": { 50 | "version": "0.2.3", 51 | "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.3.tgz", 52 | "integrity": "sha1-2sh4dxPJlmhJ/IGAd36+nB3fO4Y=" 53 | }, 54 | "assert-plus": { 55 | "version": "1.0.0", 56 | "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", 57 | "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" 58 | }, 59 | "async": { 60 | "version": "2.6.1", 61 | "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", 62 | "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", 63 | "requires": { 64 | "lodash": "^4.17.10" 65 | } 66 | }, 67 | "asynckit": { 68 | "version": "0.4.0", 69 | "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", 70 | "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" 71 | }, 72 | "aws-sign2": { 73 | "version": "0.7.0", 74 | "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz", 75 | "integrity": "sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg=" 76 | }, 77 | "aws4": { 78 | "version": "1.7.0", 79 | "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.7.0.tgz", 80 | "integrity": "sha512-32NDda82rhwD9/JBCCkB+MRYDp0oSvlo2IL6rQWA10PQi7tDUM3eqMSltXmY+Oyl/7N3P3qNtAlv7X0d9bI28w==" 81 | }, 82 | "bcrypt-pbkdf": { 83 | "version": "1.0.1", 84 | "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz", 85 | "integrity": "sha1-Y7xdy2EzG5K8Bf1SiVPDNGKgb40=", 86 | "optional": true, 87 | "requires": { 88 | "tweetnacl": "^0.14.3" 89 | } 90 | }, 91 | "boom": { 92 | "version": "4.3.1", 93 | "resolved": "https://registry.npmjs.org/boom/-/boom-4.3.1.tgz", 94 | "integrity": "sha1-T4owBctKfjiJ90kDD9JbluAdLjE=", 95 | "requires": { 96 | "hoek": "4.x.x" 97 | } 98 | }, 99 | "buffer-from": { 100 | "version": "1.0.0", 101 | "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.0.0.tgz", 102 | "integrity": "sha512-83apNb8KK0Se60UE1+4Ukbe3HbfELJ6UlI4ldtOGs7So4KD26orJM8hIY9lxdzP+UpItH1Yh/Y8GUvNFWFFRxA==" 103 | }, 104 | "caseless": { 105 | "version": "0.12.0", 106 | "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", 107 | "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" 108 | }, 109 | "co": { 110 | "version": "4.6.0", 111 | "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", 112 | "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=" 113 | }, 114 | "combined-stream": { 115 | "version": "1.0.6", 116 | "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", 117 | "integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=", 118 | "requires": { 119 | "delayed-stream": "~1.0.0" 120 | } 121 | }, 122 | "concat-stream": { 123 | "version": "1.6.2", 124 | "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", 125 | "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", 126 | "requires": { 127 | "buffer-from": "^1.0.0", 128 | "inherits": "^2.0.3", 129 | "readable-stream": "^2.2.2", 130 | "typedarray": "^0.0.6" 131 | } 132 | }, 133 | "core-util-is": { 134 | "version": "1.0.2", 135 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", 136 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" 137 | }, 138 | "cryptiles": { 139 | "version": "3.1.2", 140 | "resolved": "https://registry.npmjs.org/cryptiles/-/cryptiles-3.1.2.tgz", 141 | "integrity": "sha1-qJ+7Ig9c4l7FboxKqKT9e1sNKf4=", 142 | "requires": { 143 | "boom": "5.x.x" 144 | }, 145 | "dependencies": { 146 | "boom": { 147 | "version": "5.2.0", 148 | "resolved": "https://registry.npmjs.org/boom/-/boom-5.2.0.tgz", 149 | "integrity": "sha512-Z5BTk6ZRe4tXXQlkqftmsAUANpXmuwlsF5Oov8ThoMbQRzdGTA1ngYRW160GexgOgjsFOKJz0LYhoNi+2AMBUw==", 150 | "requires": { 151 | "hoek": "4.x.x" 152 | } 153 | } 154 | } 155 | }, 156 | "dashdash": { 157 | "version": "1.14.1", 158 | "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", 159 | "integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=", 160 | "requires": { 161 | "assert-plus": "^1.0.0" 162 | } 163 | }, 164 | "debug": { 165 | "version": "3.1.0", 166 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", 167 | "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", 168 | "requires": { 169 | "ms": "2.0.0" 170 | } 171 | }, 172 | "delayed-stream": { 173 | "version": "1.0.0", 174 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", 175 | "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" 176 | }, 177 | "ecc-jsbn": { 178 | "version": "0.1.1", 179 | "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz", 180 | "integrity": "sha1-D8c6ntXw1Tw4GTOYUj735UN3dQU=", 181 | "optional": true, 182 | "requires": { 183 | "jsbn": "~0.1.0" 184 | } 185 | }, 186 | "extend": { 187 | "version": "3.0.1", 188 | "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", 189 | "integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ=" 190 | }, 191 | "extsprintf": { 192 | "version": "1.3.0", 193 | "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz", 194 | "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=" 195 | }, 196 | "fast-deep-equal": { 197 | "version": "1.1.0", 198 | "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz", 199 | "integrity": "sha1-wFNHeBfIa1HaqFPIHgWbcz0CNhQ=" 200 | }, 201 | "fast-json-stable-stringify": { 202 | "version": "2.0.0", 203 | "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", 204 | "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" 205 | }, 206 | "forever-agent": { 207 | "version": "0.6.1", 208 | "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", 209 | "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=" 210 | }, 211 | "form-data": { 212 | "version": "2.3.2", 213 | "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.2.tgz", 214 | "integrity": "sha1-SXBJi+YEwgwAXU9cI67NIda0kJk=", 215 | "requires": { 216 | "asynckit": "^0.4.0", 217 | "combined-stream": "1.0.6", 218 | "mime-types": "^2.1.12" 219 | } 220 | }, 221 | "get-port": { 222 | "version": "3.2.0", 223 | "resolved": "https://registry.npmjs.org/get-port/-/get-port-3.2.0.tgz", 224 | "integrity": "sha1-3Xzn3hh8Bsi/NTeWrHHgmfCYDrw=" 225 | }, 226 | "getpass": { 227 | "version": "0.1.7", 228 | "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", 229 | "integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=", 230 | "requires": { 231 | "assert-plus": "^1.0.0" 232 | } 233 | }, 234 | "har-schema": { 235 | "version": "2.0.0", 236 | "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", 237 | "integrity": "sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI=" 238 | }, 239 | "har-validator": { 240 | "version": "5.0.3", 241 | "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.0.3.tgz", 242 | "integrity": "sha1-ukAsJmGU8VlW7xXg/PJCmT9qff0=", 243 | "requires": { 244 | "ajv": "^5.1.0", 245 | "har-schema": "^2.0.0" 246 | } 247 | }, 248 | "hawk": { 249 | "version": "6.0.2", 250 | "resolved": "https://registry.npmjs.org/hawk/-/hawk-6.0.2.tgz", 251 | "integrity": "sha512-miowhl2+U7Qle4vdLqDdPt9m09K6yZhkLDTWGoUiUzrQCn+mHHSmfJgAyGaLRZbPmTqfFFjRV1QWCW0VWUJBbQ==", 252 | "requires": { 253 | "boom": "4.x.x", 254 | "cryptiles": "3.x.x", 255 | "hoek": "4.x.x", 256 | "sntp": "2.x.x" 257 | } 258 | }, 259 | "hoek": { 260 | "version": "4.2.1", 261 | "resolved": "https://registry.npmjs.org/hoek/-/hoek-4.2.1.tgz", 262 | "integrity": "sha512-QLg82fGkfnJ/4iy1xZ81/9SIJiq1NGFUMGs6ParyjBZr6jW2Ufj/snDqTHixNlHdPNwN2RLVD0Pi3igeK9+JfA==" 263 | }, 264 | "http-basic": { 265 | "version": "7.0.0", 266 | "resolved": "https://registry.npmjs.org/http-basic/-/http-basic-7.0.0.tgz", 267 | "integrity": "sha1-gvClBr6UJzLsje6+6A50bvVzbbo=", 268 | "requires": { 269 | "@types/concat-stream": "^1.6.0", 270 | "@types/node": "^9.4.1", 271 | "caseless": "~0.12.0", 272 | "concat-stream": "^1.4.6", 273 | "http-response-object": "^3.0.1", 274 | "parse-cache-control": "^1.0.1" 275 | } 276 | }, 277 | "http-response-object": { 278 | "version": "3.0.1", 279 | "resolved": "https://registry.npmjs.org/http-response-object/-/http-response-object-3.0.1.tgz", 280 | "integrity": "sha512-6L0Fkd6TozA8kFSfh9Widst0wfza3U1Ex2RjJ6zNDK0vR1U1auUR6jY4Nn2Xl7CCy0ikFmxW1XcspVpb9RvwTg==", 281 | "requires": { 282 | "@types/node": "^9.3.0" 283 | } 284 | }, 285 | "http-signature": { 286 | "version": "1.2.0", 287 | "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", 288 | "integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=", 289 | "requires": { 290 | "assert-plus": "^1.0.0", 291 | "jsprim": "^1.2.2", 292 | "sshpk": "^1.7.0" 293 | } 294 | }, 295 | "inherits": { 296 | "version": "2.0.3", 297 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 298 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" 299 | }, 300 | "is-typedarray": { 301 | "version": "1.0.0", 302 | "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", 303 | "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" 304 | }, 305 | "isarray": { 306 | "version": "1.0.0", 307 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 308 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" 309 | }, 310 | "isstream": { 311 | "version": "0.1.2", 312 | "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", 313 | "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" 314 | }, 315 | "jsbn": { 316 | "version": "0.1.1", 317 | "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", 318 | "integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=", 319 | "optional": true 320 | }, 321 | "json-schema": { 322 | "version": "0.2.3", 323 | "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", 324 | "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=" 325 | }, 326 | "json-schema-traverse": { 327 | "version": "0.3.1", 328 | "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz", 329 | "integrity": "sha1-NJptRMU6Ud6JtAgFxdXlm0F9M0A=" 330 | }, 331 | "json-stringify-safe": { 332 | "version": "5.0.1", 333 | "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", 334 | "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=" 335 | }, 336 | "jsprim": { 337 | "version": "1.4.1", 338 | "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", 339 | "integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=", 340 | "requires": { 341 | "assert-plus": "1.0.0", 342 | "extsprintf": "1.3.0", 343 | "json-schema": "0.2.3", 344 | "verror": "1.10.0" 345 | } 346 | }, 347 | "lodash": { 348 | "version": "4.17.10", 349 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", 350 | "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" 351 | }, 352 | "m3u8-parser": { 353 | "version": "4.2.0", 354 | "resolved": "https://registry.npmjs.org/m3u8-parser/-/m3u8-parser-4.2.0.tgz", 355 | "integrity": "sha512-LVHw0U6IPJjwk9i9f7Xe26NqaUHTNlIt4SSWoEfYFROeVKHN6MIjOhbRheI3dg8Jbq5WCuMFQ0QU3EgZpmzFPg==" 356 | }, 357 | "mime-db": { 358 | "version": "1.33.0", 359 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.33.0.tgz", 360 | "integrity": "sha512-BHJ/EKruNIqJf/QahvxwQZXKygOQ256myeN/Ew+THcAa5q+PjyTTMMeNQC4DZw5AwfvelsUrA6B67NKMqXDbzQ==" 361 | }, 362 | "mime-types": { 363 | "version": "2.1.18", 364 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.18.tgz", 365 | "integrity": "sha512-lc/aahn+t4/SWV/qcmumYjymLsWfN3ELhpmVuUFjgsORruuZPVSwAQryq+HHGvO/SI2KVX26bx+En+zhM8g8hQ==", 366 | "requires": { 367 | "mime-db": "~1.33.0" 368 | } 369 | }, 370 | "ms": { 371 | "version": "2.0.0", 372 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 373 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 374 | }, 375 | "oauth-sign": { 376 | "version": "0.8.2", 377 | "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.8.2.tgz", 378 | "integrity": "sha1-Rqarfwrq2N6unsBWV4C31O/rnUM=" 379 | }, 380 | "parse-cache-control": { 381 | "version": "1.0.1", 382 | "resolved": "https://registry.npmjs.org/parse-cache-control/-/parse-cache-control-1.0.1.tgz", 383 | "integrity": "sha1-juqz5U+laSD+Fro493+iGqzC104=" 384 | }, 385 | "performance-now": { 386 | "version": "2.1.0", 387 | "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", 388 | "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" 389 | }, 390 | "process-nextick-args": { 391 | "version": "2.0.0", 392 | "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", 393 | "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==" 394 | }, 395 | "promise": { 396 | "version": "8.0.1", 397 | "resolved": "https://registry.npmjs.org/promise/-/promise-8.0.1.tgz", 398 | "integrity": "sha1-5F1osAoXZHttpxG/he1u1HII9FA=", 399 | "requires": { 400 | "asap": "~2.0.3" 401 | } 402 | }, 403 | "punycode": { 404 | "version": "1.4.1", 405 | "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", 406 | "integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4=" 407 | }, 408 | "qs": { 409 | "version": "6.5.2", 410 | "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", 411 | "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" 412 | }, 413 | "ratelimit": { 414 | "version": "0.0.4", 415 | "resolved": "https://registry.npmjs.org/ratelimit/-/ratelimit-0.0.4.tgz", 416 | "integrity": "sha1-TzlT5rXJPaVMIHaaC/r9tbrMEVs=" 417 | }, 418 | "readable-stream": { 419 | "version": "2.3.6", 420 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", 421 | "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", 422 | "requires": { 423 | "core-util-is": "~1.0.0", 424 | "inherits": "~2.0.3", 425 | "isarray": "~1.0.0", 426 | "process-nextick-args": "~2.0.0", 427 | "safe-buffer": "~5.1.1", 428 | "string_decoder": "~1.1.1", 429 | "util-deprecate": "~1.0.1" 430 | } 431 | }, 432 | "request": { 433 | "version": "2.85.0", 434 | "resolved": "https://registry.npmjs.org/request/-/request-2.85.0.tgz", 435 | "integrity": "sha512-8H7Ehijd4js+s6wuVPLjwORxD4zeuyjYugprdOXlPSqaApmL/QOy+EB/beICHVCHkGMKNh5rvihb5ov+IDw4mg==", 436 | "requires": { 437 | "aws-sign2": "~0.7.0", 438 | "aws4": "^1.6.0", 439 | "caseless": "~0.12.0", 440 | "combined-stream": "~1.0.5", 441 | "extend": "~3.0.1", 442 | "forever-agent": "~0.6.1", 443 | "form-data": "~2.3.1", 444 | "har-validator": "~5.0.3", 445 | "hawk": "~6.0.2", 446 | "http-signature": "~1.2.0", 447 | "is-typedarray": "~1.0.0", 448 | "isstream": "~0.1.2", 449 | "json-stringify-safe": "~5.0.1", 450 | "mime-types": "~2.1.17", 451 | "oauth-sign": "~0.8.2", 452 | "performance-now": "^2.1.0", 453 | "qs": "~6.5.1", 454 | "safe-buffer": "^5.1.1", 455 | "stringstream": "~0.0.5", 456 | "tough-cookie": "~2.3.3", 457 | "tunnel-agent": "^0.6.0", 458 | "uuid": "^3.1.0" 459 | } 460 | }, 461 | "requestretry": { 462 | "version": "1.13.0", 463 | "resolved": "https://registry.npmjs.org/requestretry/-/requestretry-1.13.0.tgz", 464 | "integrity": "sha512-Lmh9qMvnQXADGAQxsXHP4rbgO6pffCfuR8XUBdP9aitJcLQJxhp7YZK4xAVYXnPJ5E52mwrfiKQtKonPL8xsmg==", 465 | "requires": { 466 | "extend": "^3.0.0", 467 | "lodash": "^4.15.0", 468 | "request": "^2.74.0", 469 | "when": "^3.7.7" 470 | } 471 | }, 472 | "safe-buffer": { 473 | "version": "5.1.2", 474 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 475 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" 476 | }, 477 | "sntp": { 478 | "version": "2.1.0", 479 | "resolved": "https://registry.npmjs.org/sntp/-/sntp-2.1.0.tgz", 480 | "integrity": "sha512-FL1b58BDrqS3A11lJ0zEdnJ3UOKqVxawAkF3k7F0CVN7VQ34aZrV+G8BZ1WC9ZL7NyrwsW0oviwsWDgRuVYtJg==", 481 | "requires": { 482 | "hoek": "4.x.x" 483 | } 484 | }, 485 | "sshpk": { 486 | "version": "1.14.1", 487 | "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.14.1.tgz", 488 | "integrity": "sha1-Ew9Zde3a2WPx1W+SuaxsUfqfg+s=", 489 | "requires": { 490 | "asn1": "~0.2.3", 491 | "assert-plus": "^1.0.0", 492 | "bcrypt-pbkdf": "^1.0.0", 493 | "dashdash": "^1.12.0", 494 | "ecc-jsbn": "~0.1.1", 495 | "getpass": "^0.1.1", 496 | "jsbn": "~0.1.0", 497 | "tweetnacl": "~0.14.0" 498 | } 499 | }, 500 | "string_decoder": { 501 | "version": "1.1.1", 502 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", 503 | "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", 504 | "requires": { 505 | "safe-buffer": "~5.1.0" 506 | } 507 | }, 508 | "stringstream": { 509 | "version": "0.0.6", 510 | "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.6.tgz", 511 | "integrity": "sha512-87GEBAkegbBcweToUrdzf3eLhWNg06FJTebl4BVJz/JgWy8CvEr9dRtX5qWphiynMSQlxxi+QqN0z5T32SLlhA==" 512 | }, 513 | "sync-request": { 514 | "version": "6.0.0", 515 | "resolved": "https://registry.npmjs.org/sync-request/-/sync-request-6.0.0.tgz", 516 | "integrity": "sha512-jGNIAlCi9iU4X3Dm4oQnNQshDD3h0/1A7r79LyqjbjUnj69sX6mShAXlhRXgImsfVKtTcnra1jfzabdZvp+Lmw==", 517 | "requires": { 518 | "http-response-object": "^3.0.1", 519 | "sync-rpc": "^1.2.1", 520 | "then-request": "^6.0.0" 521 | } 522 | }, 523 | "sync-rpc": { 524 | "version": "1.3.3", 525 | "resolved": "https://registry.npmjs.org/sync-rpc/-/sync-rpc-1.3.3.tgz", 526 | "integrity": "sha512-xtTZUAeFaescZALim6yqjMDsVQD7mKAkdZ0/FOvVjlrr4uQqrARlWxs4P7bKV2ZPnvOyTVyHyyxqztxtBF4iIw==", 527 | "requires": { 528 | "get-port": "^3.1.0" 529 | } 530 | }, 531 | "then-request": { 532 | "version": "6.0.0", 533 | "resolved": "https://registry.npmjs.org/then-request/-/then-request-6.0.0.tgz", 534 | "integrity": "sha512-xA+7uEMc+jsQIoyySJ93Ad08Kuqnik7u6jLS5hR91Z3smAoCfL3M8/MqMlobAa9gzBfO9pA88A/AntfepkkMJQ==", 535 | "requires": { 536 | "@types/concat-stream": "^1.6.0", 537 | "@types/form-data": "0.0.33", 538 | "@types/node": "^8.0.0", 539 | "@types/qs": "^6.2.31", 540 | "caseless": "~0.12.0", 541 | "concat-stream": "^1.6.0", 542 | "form-data": "^2.2.0", 543 | "http-basic": "^7.0.0", 544 | "http-response-object": "^3.0.1", 545 | "promise": "^8.0.0", 546 | "qs": "^6.4.0" 547 | }, 548 | "dependencies": { 549 | "@types/node": { 550 | "version": "8.10.13", 551 | "resolved": "https://registry.npmjs.org/@types/node/-/node-8.10.13.tgz", 552 | "integrity": "sha512-AorNXRHoPVxIUIVmr6uJXRnvlPOSNKAJF5jZ1JOj1/IxYMocZzvQooNeLU02Db6kpy1IVIySTOvuIxmUF1HrOg==" 553 | } 554 | } 555 | }, 556 | "tough-cookie": { 557 | "version": "2.3.4", 558 | "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.3.4.tgz", 559 | "integrity": "sha512-TZ6TTfI5NtZnuyy/Kecv+CnoROnyXn2DN97LontgQpCwsX2XyLYCC0ENhYkehSOwAp8rTQKc/NUIF7BkQ5rKLA==", 560 | "requires": { 561 | "punycode": "^1.4.1" 562 | } 563 | }, 564 | "tunnel-agent": { 565 | "version": "0.6.0", 566 | "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", 567 | "integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=", 568 | "requires": { 569 | "safe-buffer": "^5.0.1" 570 | } 571 | }, 572 | "tweetnacl": { 573 | "version": "0.14.5", 574 | "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", 575 | "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=", 576 | "optional": true 577 | }, 578 | "typedarray": { 579 | "version": "0.0.6", 580 | "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", 581 | "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" 582 | }, 583 | "util-deprecate": { 584 | "version": "1.0.2", 585 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", 586 | "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" 587 | }, 588 | "uuid": { 589 | "version": "3.2.1", 590 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.2.1.tgz", 591 | "integrity": "sha512-jZnMwlb9Iku/O3smGWvZhauCf6cvvpKi4BKRiliS3cxnI+Gz9j5MEpTz2UFuXiKPJocb7gnsLHwiS05ige5BEA==" 592 | }, 593 | "verror": { 594 | "version": "1.10.0", 595 | "resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz", 596 | "integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=", 597 | "requires": { 598 | "assert-plus": "^1.0.0", 599 | "core-util-is": "1.0.2", 600 | "extsprintf": "^1.2.0" 601 | } 602 | }, 603 | "when": { 604 | "version": "3.7.8", 605 | "resolved": "https://registry.npmjs.org/when/-/when-3.7.8.tgz", 606 | "integrity": "sha1-xxMLan6gRpPoQs3J56Hyqjmjn4I=" 607 | } 608 | } 609 | } 610 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "artillery-plugin-hls", 3 | "version": "0.0.3", 4 | "description": "HTTP Live Streaming (HLS) support for Artillery", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test/index" 8 | }, 9 | "keywords": [ 10 | "hls", 11 | "load", 12 | "testing", 13 | "artillery" 14 | ], 15 | "author": "Shoreditch Ops Ltd ", 16 | "license": "MPL-2.0", 17 | "dependencies": { 18 | "async": "^2.6.1", 19 | "debug": "^3.1.0", 20 | "m3u8-parser": "^4.2.0", 21 | "ratelimit": "0.0.4", 22 | "requestretry": "^1.13.0", 23 | "sync-request": "^6.0.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | var url = require('url'); 2 | var path = require('path'); 3 | var fs = require('fs'); 4 | 5 | var Utils = { 6 | joinUri: function(absolute, relative) { 7 | var parse = url.parse(absolute); 8 | parse.pathname = path.join(parse.pathname, relative); 9 | return url.format(parse); 10 | }, 11 | getUriPath: function(uri, base) { 12 | base = base || ''; 13 | var parse = url.parse(uri); 14 | return path.relative('.', path.join(base, parse.pathname)); 15 | }, 16 | isAbsolute: function(uri) { 17 | var parsed = url.parse(uri); 18 | if (parsed.protocol) { 19 | return true; 20 | } 21 | return false; 22 | }, 23 | fileExists: function(file) { 24 | try { 25 | return fs.statSync(file).isFile(); 26 | } catch(e) { 27 | return false; 28 | } 29 | }, 30 | localize: function(parentUri, childUri) { 31 | return path.join(path.basename(parentUri, path.extname(parentUri)), path.basename(childUri)); 32 | } 33 | 34 | }; 35 | 36 | module.exports = Utils; 37 | -------------------------------------------------------------------------------- /walk-manifest.js: -------------------------------------------------------------------------------- 1 | var m3u8 = require('m3u8-parser'); 2 | var syncRequest = require('sync-request'); 3 | var url = require('url'); 4 | var path = require('path'); 5 | var fs = require('fs'); 6 | var debug = require('debug')('hls'); 7 | 8 | var joinURI = function(absolute, relative) { 9 | var parse = url.parse(absolute); 10 | parse.pathname = path.join(parse.pathname, relative); 11 | return url.format(parse); 12 | }; 13 | 14 | 15 | var isAbsolute = function(uri) { 16 | var parsed = url.parse(uri); 17 | if (parsed.protocol) { 18 | return true; 19 | } 20 | return false; 21 | }; 22 | 23 | var mediaGroupPlaylists = function(mediaGroups) { 24 | var playlists = []; 25 | ['AUDIO', 'VIDEO', 'CLOSED-CAPTIONS', 'SUBTITLES'].forEach(function(type) { 26 | var mediaGroupType = mediaGroups[type]; 27 | if (mediaGroupType && !Object.keys(mediaGroupType).length) { 28 | return; 29 | } 30 | 31 | for (var group in mediaGroupType) { 32 | for (var item in mediaGroupType[group]) { 33 | var props = mediaGroupType[group][item]; 34 | playlists.push(props); 35 | } 36 | } 37 | }); 38 | debug('mediaGroupPlaylists:'); 39 | debug(playlists); 40 | return playlists; 41 | }; 42 | 43 | var parseManifest = function(content) { 44 | var parser = new m3u8.Parser(); 45 | parser.push(content); 46 | parser.end(); 47 | debug('parsed manifest:'); 48 | debug(parser.manifest); 49 | return parser.manifest; 50 | }; 51 | 52 | var parseKey = function(basedir, decrypt, resources, manifest, parent) { 53 | if (!manifest.parsed.segments[0] || !manifest.parsed.segments[0].key) { 54 | return {}; 55 | } 56 | var key = manifest.parsed.segments[0].key; 57 | 58 | var keyUri = key.uri; 59 | if (!isAbsolute(keyUri)) { 60 | keyUri = joinURI(path.dirname(manifest.uri), keyUri); 61 | } 62 | 63 | // if we are not decrypting then we just download the key 64 | if (!decrypt) { 65 | // put keys in parent-dir/key-name.key 66 | key.file = basedir; 67 | if (parent) { 68 | key.file = path.dirname(parent.file); 69 | } 70 | key.file = path.join(key.file, path.basename(key.uri)); 71 | 72 | manifest.content = new Buffer(manifest.content.toString().replace( 73 | key.uri, 74 | path.relative(path.dirname(manifest.file), key.file) 75 | )); 76 | key.uri = keyUri; 77 | resources.push(key); 78 | return key; 79 | } 80 | 81 | // get the aes key 82 | var keyContent = syncRequest('GET', keyUri).getBody(); 83 | key.bytes = new Uint32Array([ 84 | keyContent.readUInt32BE(0), 85 | keyContent.readUInt32BE(4), 86 | keyContent.readUInt32BE(8), 87 | keyContent.readUInt32BE(12) 88 | ]); 89 | 90 | // remove the key from the manifest 91 | manifest.content = new Buffer(manifest.content.toString().replace( 92 | new RegExp('.*' + key.uri + '.*'), 93 | '' 94 | )); 95 | 96 | 97 | return key; 98 | }; 99 | 100 | var walkPlaylist = function(decrypt, basedir, uri, parent, manifestIndex, playlistFilter) { 101 | var resources = []; 102 | var manifest = {}; 103 | manifest.uri = uri; 104 | manifest.file = path.join(basedir, path.basename(uri)); 105 | resources.push(manifest); 106 | 107 | // if we are not the master playlist 108 | if (parent) { 109 | manifest.file = path.join( 110 | path.dirname(parent.file), 111 | 'manifest' + manifestIndex, 112 | path.basename(manifest.file) 113 | ); 114 | // get the real uri of this playlist 115 | if (!isAbsolute(manifest.uri)) { 116 | manifest.uri = joinURI(path.dirname(parent.uri), manifest.uri); 117 | } 118 | // replace original uri in file with new file path 119 | parent.content = new Buffer(parent.content.toString().replace(uri, path.relative(path.dirname(parent.file), manifest.file))); 120 | } 121 | 122 | manifest.content = syncRequest('GET', manifest.uri).getBody(); 123 | debug('manifest.content'); 124 | debug(manifest.content); 125 | manifest.parsed = parseManifest(manifest.content); 126 | manifest.parsed.segments = manifest.parsed.segments || []; 127 | manifest.parsed.playlists = manifest.parsed.playlists || []; 128 | manifest.parsed.mediaGroups = manifest.parsed.mediaGroups || {}; 129 | 130 | var playlists = manifest.parsed.playlists.concat(mediaGroupPlaylists(manifest.parsed.mediaGroups)); 131 | var key = parseKey(basedir, decrypt, resources, manifest, parent); 132 | 133 | // SEGMENTS 134 | manifest.parsed.segments.forEach(function(s, i) { 135 | if (!s.uri) { 136 | return; 137 | } 138 | // put segments in manifest-name/segment-name.ts 139 | s.file = path.join(path.dirname(manifest.file), path.basename(s.uri)); 140 | if (!isAbsolute(s.uri)) { 141 | s.uri = joinURI(path.dirname(manifest.uri), s.uri); 142 | } 143 | if (key) { 144 | s.key = key; 145 | s.key.iv = s.key.iv || new Uint32Array([0, 0, 0, manifest.parsed.mediaSequence, i]); 146 | } 147 | manifest.content = new Buffer(manifest.content.toString().replace(s.uri, path.basename(s.uri))); 148 | resources.push(s); 149 | }); 150 | 151 | // Pick a SUB playlist: 152 | playlists = playlists.filter(p => p.uri); 153 | var filterFunc = playlistFilter || (function(allPlaylists) { 154 | return [].concat(allPlaylists[0] || []); 155 | }); 156 | playlists = filterFunc(playlists); 157 | 158 | // if (process.env.PLAYLIST_NAME) { 159 | // // Presuming the playlist with this name exists 160 | // playlists = playlists.filter((p) => { 161 | // return p.attributes.NAME === process.env.PLAYLIST_NAME; 162 | // }); 163 | // } else { 164 | // playlists = [].concat(playlists[0] || []); 165 | // } 166 | 167 | debug('playlists:'); 168 | debug(playlists); 169 | 170 | // SUB Playlists 171 | playlists.forEach(function(p, z) { 172 | if (!p.uri) { 173 | return; 174 | } 175 | resources = resources.concat(walkPlaylist(decrypt, basedir, p.uri, manifest, z, playlistFilter)); 176 | }); 177 | 178 | return resources; 179 | }; 180 | 181 | module.exports = walkPlaylist; 182 | --------------------------------------------------------------------------------