uristream.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var util = require('util'),
  2. url = require('url'),
  3. zlib = require('zlib'),
  4. assert = require('assert');
  5. var request = require('request'),
  6. extend = require('xtend'),
  7. oncemore = require('./oncemore'),
  8. debug = require('debug')('hls:uristream');
  9. try {
  10. var Readable = require('stream').Readable;
  11. assert(Readable);
  12. } catch (e) {
  13. var Readable = require('readable-stream');
  14. }
  15. function noop() {};
  16. var pkg = require('../package');
  17. var DEFAULT_AGENT = util.format('%s/v%s (http://github.com/kanongil/node-hls-tools) node.js/%s', pkg.name, pkg.version, process.version);
  18. module.exports = uristream;
  19. uristream.UriFetchStream = UriFetchStream;
  20. uristream.PartialError = PartialError;
  21. function inheritErrors(stream) {
  22. stream.on('pipe', function(source) {
  23. source.on('error', stream.emit.bind(stream, 'error'));
  24. });
  25. stream.on('unpipe', function(source) {
  26. source.removeListener('error', stream.emit.bind(stream, 'error'));
  27. });
  28. return stream;
  29. }
  30. function setupHttp(uri, options, dst) {
  31. var defaults = {
  32. 'user-agent': DEFAULT_AGENT,
  33. 'accept-encoding': ['gzip','deflate']
  34. };
  35. // TODO: handle case in header names
  36. var headers = extend(defaults, options.headers);
  37. var timeout = options.timeout || 60*1000;
  38. var probe = !!options.probe;
  39. var retries = 10;
  40. var metaEmitted = false;
  41. var fetch = probe ? request.head : request.get;
  42. function fetchHttp(start) {
  43. if (start > 0)
  44. headers['range'] = 'bytes=' + start + '-';
  45. else
  46. delete headers['range'];
  47. var req = fetch({uri:uri, pool:false, headers:headers, timeout:timeout});
  48. req.on('error', onreqerror);
  49. req.on('error', noop);
  50. req.on('response', onresponse);
  51. function failOrRetry(err, temporary) {
  52. req.abort();
  53. dst.emit('error', err);
  54. }
  55. function reqcleanup() {
  56. req.removeListener('error', onreqerror);
  57. req.removeListener('response', onresponse);
  58. }
  59. function onreqerror(err) {
  60. reqcleanup();
  61. failOrRetry(err);
  62. }
  63. function onresponse(res) {
  64. reqcleanup();
  65. if (res.statusCode !== 200 && res.statusCode !== 206)
  66. return failOrRetry(new Error('Bad server response code: '+res.statusCode), res.statusCode >= 500 && res.statusCode !== 501);
  67. var size = res.headers['content-length'] ? parseInt(res.headers['content-length'], 10) : -1;
  68. // transparently handle gzip responses
  69. var stream = res;
  70. if (res.headers['content-encoding'] === 'gzip' || res.headers['content-encoding'] === 'deflate') {
  71. unzip = zlib.createUnzip();
  72. stream = stream.pipe(inheritErrors(unzip));
  73. size = -1;
  74. }
  75. // pipe it to self
  76. stream.on('data', function(chunk) {
  77. if (!dst.push(chunk))
  78. stream.pause();
  79. });
  80. oncemore(stream).once('end', 'error', function(err) {
  81. dst._read = noop;
  82. if (err) return failOrRetry(err);
  83. dst.push(null);
  84. });
  85. dst._read = function(n) {
  86. stream.resume();
  87. };
  88. // allow aborting the request
  89. dst.abort = req.abort.bind(req);
  90. // forward all future errors to response stream
  91. req.on('error', function(err) {
  92. if (dst.listeners('error').length !== 0)
  93. dst.emit('error', err);
  94. });
  95. // turn bad content-length into actual errors
  96. if (size >= 0 && !probe) {
  97. var accum = 0;
  98. res.on('data', function(chunk) {
  99. accum += chunk.length;
  100. if (accum > size)
  101. req.abort();
  102. });
  103. oncemore(res).once('end', 'error', function(err) {
  104. if (!err && accum !== size)
  105. failOrRetry(new PartialError('Stream length did not match header', accum, size), accum && accum < size);
  106. });
  107. }
  108. // attach empty 'error' listener to keep it from ever throwing
  109. dst.on('error', noop);
  110. if (!dst.meta) {
  111. // extract meta information from header
  112. var typeparts = /^(.+?\/.+?)(?:;\w*.*)?$/.exec(res.headers['content-type']) || [null, 'application/octet-stream'],
  113. mimetype = typeparts[1].toLowerCase(),
  114. modified = res.headers['last-modified'] ? new Date(res.headers['last-modified']) : null;
  115. dst.meta = {url:url.format(req.uri), mime:mimetype, size:start+size, modified:modified};
  116. dst.emit('meta', dst.meta);
  117. }
  118. }
  119. }
  120. fetchHttp(options.start || 0);
  121. }
  122. function UriFetchStream(uri, options) {
  123. var self = this;
  124. Readable.call(this, options);
  125. options = options || {};
  126. this.url = url.parse(uri);
  127. this.meta = null;
  128. if (this.url.protocol === 'http:' || this.url.protocol === 'https:') {
  129. setupHttp(uri, options, this);
  130. } else {
  131. throw new Error('Unsupported protocol: '+this.url.protocol);
  132. }
  133. }
  134. util.inherits(UriFetchStream, Readable);
  135. UriFetchStream.prototype._read = noop;
  136. function uristream(uri, options) {
  137. return new UriFetchStream(uri, options);
  138. }
  139. function PartialError(msg, processed, expected) {
  140. Error.captureStackTrace(this, this);
  141. this.message = msg || 'PartialError';
  142. this.processed = processed || -1;
  143. this.expected = expected;
  144. }
  145. util.inherits(PartialError, Error);
  146. PartialError.prototype.name = 'Partial Error';