node_stream.js 14 KB

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