node_stream.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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._errored) {
  157. this._readableStream.destroy(this._reason);
  158. }
  159. }
  160. }, {
  161. key: 'headersReady',
  162. get: function get() {
  163. return this._headersCapability.promise;
  164. }
  165. }, {
  166. key: 'contentLength',
  167. get: function get() {
  168. return this._contentLength;
  169. }
  170. }, {
  171. key: 'isRangeSupported',
  172. get: function get() {
  173. return this._isRangeSupported;
  174. }
  175. }, {
  176. key: 'isStreamingSupported',
  177. get: function get() {
  178. return this._isStreamingSupported;
  179. }
  180. }]);
  181. return BaseFullReader;
  182. }();
  183. var BaseRangeReader = function () {
  184. function BaseRangeReader(stream) {
  185. _classCallCheck(this, BaseRangeReader);
  186. this._url = stream.url;
  187. this._done = false;
  188. this._errored = false;
  189. this._reason = null;
  190. this.onProgress = null;
  191. this._loaded = 0;
  192. this._readableStream = null;
  193. this._readCapability = (0, _util.createPromiseCapability)();
  194. this._isStreamingSupported = !stream.source.disableStream;
  195. }
  196. _createClass(BaseRangeReader, [{
  197. key: 'read',
  198. value: function read() {
  199. var _this3 = this;
  200. return this._readCapability.promise.then(function () {
  201. if (_this3._done) {
  202. return Promise.resolve({
  203. value: undefined,
  204. done: true
  205. });
  206. }
  207. if (_this3._errored) {
  208. return Promise.reject(_this3._reason);
  209. }
  210. var chunk = _this3._readableStream.read();
  211. if (chunk === null) {
  212. _this3._readCapability = (0, _util.createPromiseCapability)();
  213. return _this3.read();
  214. }
  215. _this3._loaded += chunk.length;
  216. if (_this3.onProgress) {
  217. _this3.onProgress({ loaded: _this3._loaded });
  218. }
  219. var buffer = new Uint8Array(chunk).buffer;
  220. return Promise.resolve({
  221. value: buffer,
  222. done: false
  223. });
  224. });
  225. }
  226. }, {
  227. key: 'cancel',
  228. value: function cancel(reason) {
  229. if (!this._readableStream) {
  230. this._error(reason);
  231. return;
  232. }
  233. this._readableStream.destroy(reason);
  234. }
  235. }, {
  236. key: '_error',
  237. value: function _error(reason) {
  238. this._errored = true;
  239. this._reason = reason;
  240. this._readCapability.resolve();
  241. }
  242. }, {
  243. key: '_setReadableStream',
  244. value: function _setReadableStream(readableStream) {
  245. var _this4 = this;
  246. this._readableStream = readableStream;
  247. readableStream.on('readable', function () {
  248. _this4._readCapability.resolve();
  249. });
  250. readableStream.on('end', function () {
  251. readableStream.destroy();
  252. _this4._done = true;
  253. _this4._readCapability.resolve();
  254. });
  255. readableStream.on('error', function (reason) {
  256. _this4._error(reason);
  257. });
  258. if (this._errored) {
  259. this._readableStream.destroy(this._reason);
  260. }
  261. }
  262. }, {
  263. key: 'isStreamingSupported',
  264. get: function get() {
  265. return this._isStreamingSupported;
  266. }
  267. }]);
  268. return BaseRangeReader;
  269. }();
  270. function createRequestOptions(url, headers) {
  271. return {
  272. protocol: url.protocol,
  273. auth: url.auth,
  274. host: url.hostname,
  275. port: url.port,
  276. path: url.path,
  277. method: 'GET',
  278. headers: headers
  279. };
  280. }
  281. var PDFNodeStreamFullReader = function (_BaseFullReader) {
  282. _inherits(PDFNodeStreamFullReader, _BaseFullReader);
  283. function PDFNodeStreamFullReader(stream) {
  284. _classCallCheck(this, PDFNodeStreamFullReader);
  285. var _this5 = _possibleConstructorReturn(this, (PDFNodeStreamFullReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFullReader)).call(this, stream));
  286. var handleResponse = function handleResponse(response) {
  287. _this5._headersCapability.resolve();
  288. _this5._setReadableStream(response);
  289. var _validateRangeRequest = (0, _network_utils.validateRangeRequestCapabilities)({
  290. getResponseHeader: function getResponseHeader(name) {
  291. return _this5._readableStream.headers[name.toLowerCase()];
  292. },
  293. isHttp: stream.isHttp,
  294. rangeChunkSize: _this5._rangeChunkSize,
  295. disableRange: _this5._disableRange
  296. }),
  297. allowRangeRequests = _validateRangeRequest.allowRangeRequests,
  298. suggestedLength = _validateRangeRequest.suggestedLength;
  299. if (allowRangeRequests) {
  300. _this5._isRangeSupported = true;
  301. }
  302. _this5._contentLength = suggestedLength;
  303. };
  304. _this5._request = null;
  305. if (_this5._url.protocol === 'http:') {
  306. _this5._request = http.request(createRequestOptions(_this5._url, stream.httpHeaders), handleResponse);
  307. } else {
  308. _this5._request = https.request(createRequestOptions(_this5._url, stream.httpHeaders), handleResponse);
  309. }
  310. _this5._request.on('error', function (reason) {
  311. _this5._errored = true;
  312. _this5._reason = reason;
  313. _this5._headersCapability.reject(reason);
  314. });
  315. _this5._request.end();
  316. return _this5;
  317. }
  318. return PDFNodeStreamFullReader;
  319. }(BaseFullReader);
  320. var PDFNodeStreamRangeReader = function (_BaseRangeReader) {
  321. _inherits(PDFNodeStreamRangeReader, _BaseRangeReader);
  322. function PDFNodeStreamRangeReader(stream, start, end) {
  323. _classCallCheck(this, PDFNodeStreamRangeReader);
  324. var _this6 = _possibleConstructorReturn(this, (PDFNodeStreamRangeReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamRangeReader)).call(this, stream));
  325. _this6._httpHeaders = {};
  326. for (var property in stream.httpHeaders) {
  327. var value = stream.httpHeaders[property];
  328. if (typeof value === 'undefined') {
  329. continue;
  330. }
  331. _this6._httpHeaders[property] = value;
  332. }
  333. _this6._httpHeaders['Range'] = 'bytes=' + start + '-' + (end - 1);
  334. _this6._request = null;
  335. if (_this6._url.protocol === 'http:') {
  336. _this6._request = http.request(createRequestOptions(_this6._url, _this6._httpHeaders), function (response) {
  337. _this6._setReadableStream(response);
  338. });
  339. } else {
  340. _this6._request = https.request(createRequestOptions(_this6._url, _this6._httpHeaders), function (response) {
  341. _this6._setReadableStream(response);
  342. });
  343. }
  344. _this6._request.on('error', function (reason) {
  345. _this6._errored = true;
  346. _this6._reason = reason;
  347. });
  348. _this6._request.end();
  349. return _this6;
  350. }
  351. return PDFNodeStreamRangeReader;
  352. }(BaseRangeReader);
  353. var PDFNodeStreamFsFullReader = function (_BaseFullReader2) {
  354. _inherits(PDFNodeStreamFsFullReader, _BaseFullReader2);
  355. function PDFNodeStreamFsFullReader(stream) {
  356. _classCallCheck(this, PDFNodeStreamFsFullReader);
  357. var _this7 = _possibleConstructorReturn(this, (PDFNodeStreamFsFullReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFsFullReader)).call(this, stream));
  358. _this7._setReadableStream(fs.createReadStream(_this7._url.path));
  359. fs.lstat(_this7._url.path, function (error, stat) {
  360. if (error) {
  361. _this7._errored = true;
  362. _this7._reason = error;
  363. _this7._headersCapability.reject(error);
  364. return;
  365. }
  366. _this7._contentLength = stat.size;
  367. _this7._headersCapability.resolve();
  368. });
  369. return _this7;
  370. }
  371. return PDFNodeStreamFsFullReader;
  372. }(BaseFullReader);
  373. var PDFNodeStreamFsRangeReader = function (_BaseRangeReader2) {
  374. _inherits(PDFNodeStreamFsRangeReader, _BaseRangeReader2);
  375. function PDFNodeStreamFsRangeReader(stream, start, end) {
  376. _classCallCheck(this, PDFNodeStreamFsRangeReader);
  377. var _this8 = _possibleConstructorReturn(this, (PDFNodeStreamFsRangeReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFsRangeReader)).call(this, stream));
  378. _this8._setReadableStream(fs.createReadStream(_this8._url.path, {
  379. start: start,
  380. end: end - 1
  381. }));
  382. return _this8;
  383. }
  384. return PDFNodeStreamFsRangeReader;
  385. }(BaseRangeReader);
  386. exports.PDFNodeStream = PDFNodeStream;