node_stream.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFNodeStream = undefined;
  20. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _util = require('../shared/util');
  22. var _network_utils = require('./network_utils');
  23. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  24. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  25. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  26. var fs = require('fs');
  27. var http = require('http');
  28. var https = require('https');
  29. var url = require('url');
  30. var PDFNodeStream = function () {
  31. function PDFNodeStream(options) {
  32. _classCallCheck(this, PDFNodeStream);
  33. this.options = options;
  34. this.source = options.source;
  35. this.url = url.parse(this.source.url);
  36. this.isHttp = this.url.protocol === 'http:' || this.url.protocol === 'https:';
  37. this.isFsUrl = this.url.protocol === 'file:' || !this.url.host;
  38. this.httpHeaders = this.isHttp && this.source.httpHeaders || {};
  39. this._fullRequest = null;
  40. this._rangeRequestReaders = [];
  41. }
  42. _createClass(PDFNodeStream, [{
  43. key: 'getFullReader',
  44. value: function getFullReader() {
  45. (0, _util.assert)(!this._fullRequest);
  46. this._fullRequest = this.isFsUrl ? new PDFNodeStreamFsFullReader(this) : new PDFNodeStreamFullReader(this);
  47. return this._fullRequest;
  48. }
  49. }, {
  50. key: 'getRangeReader',
  51. value: function getRangeReader(start, end) {
  52. var rangeReader = this.isFsUrl ? new PDFNodeStreamFsRangeReader(this, start, end) : new PDFNodeStreamRangeReader(this, start, end);
  53. this._rangeRequestReaders.push(rangeReader);
  54. return rangeReader;
  55. }
  56. }, {
  57. key: 'cancelAllRequests',
  58. value: function cancelAllRequests(reason) {
  59. if (this._fullRequest) {
  60. this._fullRequest.cancel(reason);
  61. }
  62. var readers = this._rangeRequestReaders.slice(0);
  63. readers.forEach(function (reader) {
  64. reader.cancel(reason);
  65. });
  66. }
  67. }]);
  68. return PDFNodeStream;
  69. }();
  70. var BaseFullReader = function () {
  71. function BaseFullReader(stream) {
  72. _classCallCheck(this, BaseFullReader);
  73. this._url = stream.url;
  74. this._done = false;
  75. this._errored = false;
  76. this._reason = null;
  77. this.onProgress = null;
  78. this._contentLength = stream.source.length;
  79. this._loaded = 0;
  80. this._disableRange = stream.options.disableRange || false;
  81. this._rangeChunkSize = stream.source.rangeChunkSize;
  82. if (!this._rangeChunkSize && !this._disableRange) {
  83. this._disableRange = true;
  84. }
  85. this._isStreamingSupported = !stream.source.disableStream;
  86. this._isRangeSupported = !stream.options.disableRange;
  87. this._readableStream = null;
  88. this._readCapability = (0, _util.createPromiseCapability)();
  89. this._headersCapability = (0, _util.createPromiseCapability)();
  90. }
  91. _createClass(BaseFullReader, [{
  92. key: 'read',
  93. value: function read() {
  94. var _this = this;
  95. return this._readCapability.promise.then(function () {
  96. if (_this._done) {
  97. return Promise.resolve({
  98. value: undefined,
  99. done: true
  100. });
  101. }
  102. if (_this._errored) {
  103. return Promise.reject(_this._reason);
  104. }
  105. var chunk = _this._readableStream.read();
  106. if (chunk === null) {
  107. _this._readCapability = (0, _util.createPromiseCapability)();
  108. return _this.read();
  109. }
  110. _this._loaded += chunk.length;
  111. if (_this.onProgress) {
  112. _this.onProgress({
  113. loaded: _this._loaded,
  114. total: _this._contentLength
  115. });
  116. }
  117. var buffer = new Uint8Array(chunk).buffer;
  118. return Promise.resolve({
  119. value: buffer,
  120. done: false
  121. });
  122. });
  123. }
  124. }, {
  125. key: 'cancel',
  126. value: function cancel(reason) {
  127. if (!this._readableStream) {
  128. this._error(reason);
  129. return;
  130. }
  131. this._readableStream.destroy(reason);
  132. }
  133. }, {
  134. key: '_error',
  135. value: function _error(reason) {
  136. this._errored = true;
  137. this._reason = reason;
  138. this._readCapability.resolve();
  139. }
  140. }, {
  141. key: '_setReadableStream',
  142. value: function _setReadableStream(readableStream) {
  143. var _this2 = this;
  144. this._readableStream = readableStream;
  145. readableStream.on('readable', function () {
  146. _this2._readCapability.resolve();
  147. });
  148. readableStream.on('end', function () {
  149. readableStream.destroy();
  150. _this2._done = true;
  151. _this2._readCapability.resolve();
  152. });
  153. readableStream.on('error', function (reason) {
  154. _this2._error(reason);
  155. });
  156. if (!this._isStreamingSupported && this._isRangeSupported) {
  157. this._error(new _util.AbortException('streaming is disabled'));
  158. }
  159. if (this._errored) {
  160. this._readableStream.destroy(this._reason);
  161. }
  162. }
  163. }, {
  164. key: 'headersReady',
  165. get: function get() {
  166. return this._headersCapability.promise;
  167. }
  168. }, {
  169. key: 'contentLength',
  170. get: function get() {
  171. return this._contentLength;
  172. }
  173. }, {
  174. key: 'isRangeSupported',
  175. get: function get() {
  176. return this._isRangeSupported;
  177. }
  178. }, {
  179. key: 'isStreamingSupported',
  180. get: function get() {
  181. return this._isStreamingSupported;
  182. }
  183. }]);
  184. return BaseFullReader;
  185. }();
  186. var BaseRangeReader = function () {
  187. function BaseRangeReader(stream) {
  188. _classCallCheck(this, BaseRangeReader);
  189. this._url = stream.url;
  190. this._done = false;
  191. this._errored = false;
  192. this._reason = null;
  193. this.onProgress = null;
  194. this._loaded = 0;
  195. this._readableStream = null;
  196. this._readCapability = (0, _util.createPromiseCapability)();
  197. this._isStreamingSupported = !stream.source.disableStream;
  198. }
  199. _createClass(BaseRangeReader, [{
  200. key: 'read',
  201. value: function read() {
  202. var _this3 = this;
  203. return this._readCapability.promise.then(function () {
  204. if (_this3._done) {
  205. return Promise.resolve({
  206. value: undefined,
  207. done: true
  208. });
  209. }
  210. if (_this3._errored) {
  211. return Promise.reject(_this3._reason);
  212. }
  213. var chunk = _this3._readableStream.read();
  214. if (chunk === null) {
  215. _this3._readCapability = (0, _util.createPromiseCapability)();
  216. return _this3.read();
  217. }
  218. _this3._loaded += chunk.length;
  219. if (_this3.onProgress) {
  220. _this3.onProgress({ loaded: _this3._loaded });
  221. }
  222. var buffer = new Uint8Array(chunk).buffer;
  223. return Promise.resolve({
  224. value: buffer,
  225. done: false
  226. });
  227. });
  228. }
  229. }, {
  230. key: 'cancel',
  231. value: function cancel(reason) {
  232. if (!this._readableStream) {
  233. this._error(reason);
  234. return;
  235. }
  236. this._readableStream.destroy(reason);
  237. }
  238. }, {
  239. key: '_error',
  240. value: function _error(reason) {
  241. this._errored = true;
  242. this._reason = reason;
  243. this._readCapability.resolve();
  244. }
  245. }, {
  246. key: '_setReadableStream',
  247. value: function _setReadableStream(readableStream) {
  248. var _this4 = this;
  249. this._readableStream = readableStream;
  250. readableStream.on('readable', function () {
  251. _this4._readCapability.resolve();
  252. });
  253. readableStream.on('end', function () {
  254. readableStream.destroy();
  255. _this4._done = true;
  256. _this4._readCapability.resolve();
  257. });
  258. readableStream.on('error', function (reason) {
  259. _this4._error(reason);
  260. });
  261. if (this._errored) {
  262. this._readableStream.destroy(this._reason);
  263. }
  264. }
  265. }, {
  266. key: 'isStreamingSupported',
  267. get: function get() {
  268. return this._isStreamingSupported;
  269. }
  270. }]);
  271. return BaseRangeReader;
  272. }();
  273. function createRequestOptions(url, headers) {
  274. return {
  275. protocol: url.protocol,
  276. auth: url.auth,
  277. host: url.hostname,
  278. port: url.port,
  279. path: url.path,
  280. method: 'GET',
  281. headers: headers
  282. };
  283. }
  284. var PDFNodeStreamFullReader = function (_BaseFullReader) {
  285. _inherits(PDFNodeStreamFullReader, _BaseFullReader);
  286. function PDFNodeStreamFullReader(stream) {
  287. _classCallCheck(this, PDFNodeStreamFullReader);
  288. var _this5 = _possibleConstructorReturn(this, (PDFNodeStreamFullReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFullReader)).call(this, stream));
  289. var handleResponse = function handleResponse(response) {
  290. _this5._headersCapability.resolve();
  291. _this5._setReadableStream(response);
  292. var _validateRangeRequest = (0, _network_utils.validateRangeRequestCapabilities)({
  293. getResponseHeader: function getResponseHeader(name) {
  294. return _this5._readableStream.headers[name.toLowerCase()];
  295. },
  296. isHttp: stream.isHttp,
  297. rangeChunkSize: _this5._rangeChunkSize,
  298. disableRange: _this5._disableRange
  299. }),
  300. allowRangeRequests = _validateRangeRequest.allowRangeRequests,
  301. suggestedLength = _validateRangeRequest.suggestedLength;
  302. if (allowRangeRequests) {
  303. _this5._isRangeSupported = true;
  304. }
  305. _this5._contentLength = suggestedLength;
  306. };
  307. _this5._request = null;
  308. if (_this5._url.protocol === 'http:') {
  309. _this5._request = http.request(createRequestOptions(_this5._url, stream.httpHeaders), handleResponse);
  310. } else {
  311. _this5._request = https.request(createRequestOptions(_this5._url, stream.httpHeaders), handleResponse);
  312. }
  313. _this5._request.on('error', function (reason) {
  314. _this5._errored = true;
  315. _this5._reason = reason;
  316. _this5._headersCapability.reject(reason);
  317. });
  318. _this5._request.end();
  319. return _this5;
  320. }
  321. return PDFNodeStreamFullReader;
  322. }(BaseFullReader);
  323. var PDFNodeStreamRangeReader = function (_BaseRangeReader) {
  324. _inherits(PDFNodeStreamRangeReader, _BaseRangeReader);
  325. function PDFNodeStreamRangeReader(stream, start, end) {
  326. _classCallCheck(this, PDFNodeStreamRangeReader);
  327. var _this6 = _possibleConstructorReturn(this, (PDFNodeStreamRangeReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamRangeReader)).call(this, stream));
  328. _this6._httpHeaders = {};
  329. for (var property in stream.httpHeaders) {
  330. var value = stream.httpHeaders[property];
  331. if (typeof value === 'undefined') {
  332. continue;
  333. }
  334. _this6._httpHeaders[property] = value;
  335. }
  336. _this6._httpHeaders['Range'] = 'bytes=' + start + '-' + (end - 1);
  337. _this6._request = null;
  338. if (_this6._url.protocol === 'http:') {
  339. _this6._request = http.request(createRequestOptions(_this6._url, _this6._httpHeaders), function (response) {
  340. _this6._setReadableStream(response);
  341. });
  342. } else {
  343. _this6._request = https.request(createRequestOptions(_this6._url, _this6._httpHeaders), function (response) {
  344. _this6._setReadableStream(response);
  345. });
  346. }
  347. _this6._request.on('error', function (reason) {
  348. _this6._errored = true;
  349. _this6._reason = reason;
  350. });
  351. _this6._request.end();
  352. return _this6;
  353. }
  354. return PDFNodeStreamRangeReader;
  355. }(BaseRangeReader);
  356. var PDFNodeStreamFsFullReader = function (_BaseFullReader2) {
  357. _inherits(PDFNodeStreamFsFullReader, _BaseFullReader2);
  358. function PDFNodeStreamFsFullReader(stream) {
  359. _classCallCheck(this, PDFNodeStreamFsFullReader);
  360. var _this7 = _possibleConstructorReturn(this, (PDFNodeStreamFsFullReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFsFullReader)).call(this, stream));
  361. var path = decodeURI(_this7._url.path);
  362. fs.lstat(path, function (error, stat) {
  363. if (error) {
  364. _this7._errored = true;
  365. _this7._reason = error;
  366. _this7._headersCapability.reject(error);
  367. return;
  368. }
  369. _this7._contentLength = stat.size;
  370. _this7._setReadableStream(fs.createReadStream(path));
  371. _this7._headersCapability.resolve();
  372. });
  373. return _this7;
  374. }
  375. return PDFNodeStreamFsFullReader;
  376. }(BaseFullReader);
  377. var PDFNodeStreamFsRangeReader = function (_BaseRangeReader2) {
  378. _inherits(PDFNodeStreamFsRangeReader, _BaseRangeReader2);
  379. function PDFNodeStreamFsRangeReader(stream, start, end) {
  380. _classCallCheck(this, PDFNodeStreamFsRangeReader);
  381. var _this8 = _possibleConstructorReturn(this, (PDFNodeStreamFsRangeReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFsRangeReader)).call(this, stream));
  382. _this8._setReadableStream(fs.createReadStream(decodeURI(_this8._url.path), {
  383. start: start,
  384. end: end - 1
  385. }));
  386. return _this8;
  387. }
  388. return PDFNodeStreamFsRangeReader;
  389. }(BaseRangeReader);
  390. exports.PDFNodeStream = PDFNodeStream;