node_stream.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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(source) {
  32. _classCallCheck(this, PDFNodeStream);
  33. this.source = source;
  34. this.url = url.parse(source.url);
  35. this.isHttp = this.url.protocol === 'http:' || this.url.protocol === 'https:';
  36. this.isFsUrl = this.url.protocol === 'file:' || !this.url.host;
  37. this.httpHeaders = this.isHttp && source.httpHeaders || {};
  38. this._fullRequest = null;
  39. this._rangeRequestReaders = [];
  40. }
  41. _createClass(PDFNodeStream, [{
  42. key: 'getFullReader',
  43. value: function getFullReader() {
  44. (0, _util.assert)(!this._fullRequest);
  45. this._fullRequest = this.isFsUrl ? new PDFNodeStreamFsFullReader(this) : new PDFNodeStreamFullReader(this);
  46. return this._fullRequest;
  47. }
  48. }, {
  49. key: 'getRangeReader',
  50. value: function getRangeReader(start, end) {
  51. var rangeReader = this.isFsUrl ? new PDFNodeStreamFsRangeReader(this, start, end) : new PDFNodeStreamRangeReader(this, start, end);
  52. this._rangeRequestReaders.push(rangeReader);
  53. return rangeReader;
  54. }
  55. }, {
  56. key: 'cancelAllRequests',
  57. value: function cancelAllRequests(reason) {
  58. if (this._fullRequest) {
  59. this._fullRequest.cancel(reason);
  60. }
  61. var readers = this._rangeRequestReaders.slice(0);
  62. readers.forEach(function (reader) {
  63. reader.cancel(reason);
  64. });
  65. }
  66. }]);
  67. return PDFNodeStream;
  68. }();
  69. var BaseFullReader = function () {
  70. function BaseFullReader(stream) {
  71. _classCallCheck(this, BaseFullReader);
  72. this._url = stream.url;
  73. this._done = false;
  74. this._errored = false;
  75. this._reason = null;
  76. this.onProgress = null;
  77. var source = stream.source;
  78. this._contentLength = source.length;
  79. this._loaded = 0;
  80. this._disableRange = source.disableRange || false;
  81. this._rangeChunkSize = source.rangeChunkSize;
  82. if (!this._rangeChunkSize && !this._disableRange) {
  83. this._disableRange = true;
  84. }
  85. this._isStreamingSupported = !source.disableStream;
  86. this._isRangeSupported = !source.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. var source = stream.source;
  198. this._isStreamingSupported = !source.disableStream;
  199. }
  200. _createClass(BaseRangeReader, [{
  201. key: 'read',
  202. value: function read() {
  203. var _this3 = this;
  204. return this._readCapability.promise.then(function () {
  205. if (_this3._done) {
  206. return Promise.resolve({
  207. value: undefined,
  208. done: true
  209. });
  210. }
  211. if (_this3._errored) {
  212. return Promise.reject(_this3._reason);
  213. }
  214. var chunk = _this3._readableStream.read();
  215. if (chunk === null) {
  216. _this3._readCapability = (0, _util.createPromiseCapability)();
  217. return _this3.read();
  218. }
  219. _this3._loaded += chunk.length;
  220. if (_this3.onProgress) {
  221. _this3.onProgress({ loaded: _this3._loaded });
  222. }
  223. var buffer = new Uint8Array(chunk).buffer;
  224. return Promise.resolve({
  225. value: buffer,
  226. done: false
  227. });
  228. });
  229. }
  230. }, {
  231. key: 'cancel',
  232. value: function cancel(reason) {
  233. if (!this._readableStream) {
  234. this._error(reason);
  235. return;
  236. }
  237. this._readableStream.destroy(reason);
  238. }
  239. }, {
  240. key: '_error',
  241. value: function _error(reason) {
  242. this._errored = true;
  243. this._reason = reason;
  244. this._readCapability.resolve();
  245. }
  246. }, {
  247. key: '_setReadableStream',
  248. value: function _setReadableStream(readableStream) {
  249. var _this4 = this;
  250. this._readableStream = readableStream;
  251. readableStream.on('readable', function () {
  252. _this4._readCapability.resolve();
  253. });
  254. readableStream.on('end', function () {
  255. readableStream.destroy();
  256. _this4._done = true;
  257. _this4._readCapability.resolve();
  258. });
  259. readableStream.on('error', function (reason) {
  260. _this4._error(reason);
  261. });
  262. if (this._errored) {
  263. this._readableStream.destroy(this._reason);
  264. }
  265. }
  266. }, {
  267. key: 'isStreamingSupported',
  268. get: function get() {
  269. return this._isStreamingSupported;
  270. }
  271. }]);
  272. return BaseRangeReader;
  273. }();
  274. function createRequestOptions(url, headers) {
  275. return {
  276. protocol: url.protocol,
  277. auth: url.auth,
  278. host: url.hostname,
  279. port: url.port,
  280. path: url.path,
  281. method: 'GET',
  282. headers: headers
  283. };
  284. }
  285. var PDFNodeStreamFullReader = function (_BaseFullReader) {
  286. _inherits(PDFNodeStreamFullReader, _BaseFullReader);
  287. function PDFNodeStreamFullReader(stream) {
  288. _classCallCheck(this, PDFNodeStreamFullReader);
  289. var _this5 = _possibleConstructorReturn(this, (PDFNodeStreamFullReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFullReader)).call(this, stream));
  290. var handleResponse = function handleResponse(response) {
  291. _this5._headersCapability.resolve();
  292. _this5._setReadableStream(response);
  293. var _validateRangeRequest = (0, _network_utils.validateRangeRequestCapabilities)({
  294. getResponseHeader: function getResponseHeader(name) {
  295. return _this5._readableStream.headers[name.toLowerCase()];
  296. },
  297. isHttp: stream.isHttp,
  298. rangeChunkSize: _this5._rangeChunkSize,
  299. disableRange: _this5._disableRange
  300. }),
  301. allowRangeRequests = _validateRangeRequest.allowRangeRequests,
  302. suggestedLength = _validateRangeRequest.suggestedLength;
  303. if (allowRangeRequests) {
  304. _this5._isRangeSupported = true;
  305. }
  306. _this5._contentLength = suggestedLength;
  307. };
  308. _this5._request = null;
  309. if (_this5._url.protocol === 'http:') {
  310. _this5._request = http.request(createRequestOptions(_this5._url, stream.httpHeaders), handleResponse);
  311. } else {
  312. _this5._request = https.request(createRequestOptions(_this5._url, stream.httpHeaders), handleResponse);
  313. }
  314. _this5._request.on('error', function (reason) {
  315. _this5._errored = true;
  316. _this5._reason = reason;
  317. _this5._headersCapability.reject(reason);
  318. });
  319. _this5._request.end();
  320. return _this5;
  321. }
  322. return PDFNodeStreamFullReader;
  323. }(BaseFullReader);
  324. var PDFNodeStreamRangeReader = function (_BaseRangeReader) {
  325. _inherits(PDFNodeStreamRangeReader, _BaseRangeReader);
  326. function PDFNodeStreamRangeReader(stream, start, end) {
  327. _classCallCheck(this, PDFNodeStreamRangeReader);
  328. var _this6 = _possibleConstructorReturn(this, (PDFNodeStreamRangeReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamRangeReader)).call(this, stream));
  329. _this6._httpHeaders = {};
  330. for (var property in stream.httpHeaders) {
  331. var value = stream.httpHeaders[property];
  332. if (typeof value === 'undefined') {
  333. continue;
  334. }
  335. _this6._httpHeaders[property] = value;
  336. }
  337. _this6._httpHeaders['Range'] = 'bytes=' + start + '-' + (end - 1);
  338. _this6._request = null;
  339. if (_this6._url.protocol === 'http:') {
  340. _this6._request = http.request(createRequestOptions(_this6._url, _this6._httpHeaders), function (response) {
  341. _this6._setReadableStream(response);
  342. });
  343. } else {
  344. _this6._request = https.request(createRequestOptions(_this6._url, _this6._httpHeaders), function (response) {
  345. _this6._setReadableStream(response);
  346. });
  347. }
  348. _this6._request.on('error', function (reason) {
  349. _this6._errored = true;
  350. _this6._reason = reason;
  351. });
  352. _this6._request.end();
  353. return _this6;
  354. }
  355. return PDFNodeStreamRangeReader;
  356. }(BaseRangeReader);
  357. var PDFNodeStreamFsFullReader = function (_BaseFullReader2) {
  358. _inherits(PDFNodeStreamFsFullReader, _BaseFullReader2);
  359. function PDFNodeStreamFsFullReader(stream) {
  360. _classCallCheck(this, PDFNodeStreamFsFullReader);
  361. var _this7 = _possibleConstructorReturn(this, (PDFNodeStreamFsFullReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFsFullReader)).call(this, stream));
  362. var path = decodeURI(_this7._url.path);
  363. fs.lstat(path, function (error, stat) {
  364. if (error) {
  365. _this7._errored = true;
  366. _this7._reason = error;
  367. _this7._headersCapability.reject(error);
  368. return;
  369. }
  370. _this7._contentLength = stat.size;
  371. _this7._setReadableStream(fs.createReadStream(path));
  372. _this7._headersCapability.resolve();
  373. });
  374. return _this7;
  375. }
  376. return PDFNodeStreamFsFullReader;
  377. }(BaseFullReader);
  378. var PDFNodeStreamFsRangeReader = function (_BaseRangeReader2) {
  379. _inherits(PDFNodeStreamFsRangeReader, _BaseRangeReader2);
  380. function PDFNodeStreamFsRangeReader(stream, start, end) {
  381. _classCallCheck(this, PDFNodeStreamFsRangeReader);
  382. var _this8 = _possibleConstructorReturn(this, (PDFNodeStreamFsRangeReader.__proto__ || Object.getPrototypeOf(PDFNodeStreamFsRangeReader)).call(this, stream));
  383. _this8._setReadableStream(fs.createReadStream(decodeURI(_this8._url.path), {
  384. start: start,
  385. end: end - 1
  386. }));
  387. return _this8;
  388. }
  389. return PDFNodeStreamFsRangeReader;
  390. }(BaseRangeReader);
  391. exports.PDFNodeStream = PDFNodeStream;