node_stream.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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 = void 0;
  27. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  28. var _util = require("../shared/util");
  29. var _network_utils = require("./network_utils");
  30. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  31. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  32. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  33. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  34. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  35. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  36. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  37. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  38. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  39. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  40. 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); } }
  41. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  42. var fs = require('fs');
  43. var http = require('http');
  44. var https = require('https');
  45. var url = require('url');
  46. var fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
  47. function parseUrl(sourceUrl) {
  48. var parsedUrl = url.parse(sourceUrl);
  49. if (parsedUrl.protocol === 'file:' || parsedUrl.host) {
  50. return parsedUrl;
  51. }
  52. if (/^[a-z]:[/\\]/i.test(sourceUrl)) {
  53. return url.parse("file:///".concat(sourceUrl));
  54. }
  55. if (!parsedUrl.host) {
  56. parsedUrl.protocol = 'file:';
  57. }
  58. return parsedUrl;
  59. }
  60. var PDFNodeStream =
  61. /*#__PURE__*/
  62. function () {
  63. function PDFNodeStream(source) {
  64. _classCallCheck(this, PDFNodeStream);
  65. this.source = source;
  66. this.url = parseUrl(source.url);
  67. this.isHttp = this.url.protocol === 'http:' || this.url.protocol === 'https:';
  68. this.isFsUrl = this.url.protocol === 'file:';
  69. this.httpHeaders = this.isHttp && source.httpHeaders || {};
  70. this._fullRequest = null;
  71. this._rangeRequestReaders = [];
  72. }
  73. _createClass(PDFNodeStream, [{
  74. key: "getFullReader",
  75. value: function getFullReader() {
  76. (0, _util.assert)(!this._fullRequest);
  77. this._fullRequest = this.isFsUrl ? new PDFNodeStreamFsFullReader(this) : new PDFNodeStreamFullReader(this);
  78. return this._fullRequest;
  79. }
  80. }, {
  81. key: "getRangeReader",
  82. value: function getRangeReader(start, end) {
  83. var rangeReader = this.isFsUrl ? new PDFNodeStreamFsRangeReader(this, start, end) : new PDFNodeStreamRangeReader(this, start, end);
  84. this._rangeRequestReaders.push(rangeReader);
  85. return rangeReader;
  86. }
  87. }, {
  88. key: "cancelAllRequests",
  89. value: function cancelAllRequests(reason) {
  90. if (this._fullRequest) {
  91. this._fullRequest.cancel(reason);
  92. }
  93. var readers = this._rangeRequestReaders.slice(0);
  94. readers.forEach(function (reader) {
  95. reader.cancel(reason);
  96. });
  97. }
  98. }]);
  99. return PDFNodeStream;
  100. }();
  101. exports.PDFNodeStream = PDFNodeStream;
  102. var BaseFullReader =
  103. /*#__PURE__*/
  104. function () {
  105. function BaseFullReader(stream) {
  106. _classCallCheck(this, BaseFullReader);
  107. this._url = stream.url;
  108. this._done = false;
  109. this._storedError = null;
  110. this.onProgress = null;
  111. var source = stream.source;
  112. this._contentLength = source.length;
  113. this._loaded = 0;
  114. this._filename = null;
  115. this._disableRange = source.disableRange || false;
  116. this._rangeChunkSize = source.rangeChunkSize;
  117. if (!this._rangeChunkSize && !this._disableRange) {
  118. this._disableRange = true;
  119. }
  120. this._isStreamingSupported = !source.disableStream;
  121. this._isRangeSupported = !source.disableRange;
  122. this._readableStream = null;
  123. this._readCapability = (0, _util.createPromiseCapability)();
  124. this._headersCapability = (0, _util.createPromiseCapability)();
  125. }
  126. _createClass(BaseFullReader, [{
  127. key: "read",
  128. value: function () {
  129. var _read = _asyncToGenerator(
  130. /*#__PURE__*/
  131. _regenerator.default.mark(function _callee() {
  132. var chunk, buffer;
  133. return _regenerator.default.wrap(function _callee$(_context) {
  134. while (1) {
  135. switch (_context.prev = _context.next) {
  136. case 0:
  137. _context.next = 2;
  138. return this._readCapability.promise;
  139. case 2:
  140. if (!this._done) {
  141. _context.next = 4;
  142. break;
  143. }
  144. return _context.abrupt("return", {
  145. value: undefined,
  146. done: true
  147. });
  148. case 4:
  149. if (!this._storedError) {
  150. _context.next = 6;
  151. break;
  152. }
  153. throw this._storedError;
  154. case 6:
  155. chunk = this._readableStream.read();
  156. if (!(chunk === null)) {
  157. _context.next = 10;
  158. break;
  159. }
  160. this._readCapability = (0, _util.createPromiseCapability)();
  161. return _context.abrupt("return", this.read());
  162. case 10:
  163. this._loaded += chunk.length;
  164. if (this.onProgress) {
  165. this.onProgress({
  166. loaded: this._loaded,
  167. total: this._contentLength
  168. });
  169. }
  170. buffer = new Uint8Array(chunk).buffer;
  171. return _context.abrupt("return", {
  172. value: buffer,
  173. done: false
  174. });
  175. case 14:
  176. case "end":
  177. return _context.stop();
  178. }
  179. }
  180. }, _callee, this);
  181. }));
  182. function read() {
  183. return _read.apply(this, arguments);
  184. }
  185. return read;
  186. }()
  187. }, {
  188. key: "cancel",
  189. value: function cancel(reason) {
  190. if (!this._readableStream) {
  191. this._error(reason);
  192. return;
  193. }
  194. this._readableStream.destroy(reason);
  195. }
  196. }, {
  197. key: "_error",
  198. value: function _error(reason) {
  199. this._storedError = reason;
  200. this._readCapability.resolve();
  201. }
  202. }, {
  203. key: "_setReadableStream",
  204. value: function _setReadableStream(readableStream) {
  205. var _this = this;
  206. this._readableStream = readableStream;
  207. readableStream.on('readable', function () {
  208. _this._readCapability.resolve();
  209. });
  210. readableStream.on('end', function () {
  211. readableStream.destroy();
  212. _this._done = true;
  213. _this._readCapability.resolve();
  214. });
  215. readableStream.on('error', function (reason) {
  216. _this._error(reason);
  217. });
  218. if (!this._isStreamingSupported && this._isRangeSupported) {
  219. this._error(new _util.AbortException('streaming is disabled'));
  220. }
  221. if (this._storedError) {
  222. this._readableStream.destroy(this._storedError);
  223. }
  224. }
  225. }, {
  226. key: "headersReady",
  227. get: function get() {
  228. return this._headersCapability.promise;
  229. }
  230. }, {
  231. key: "filename",
  232. get: function get() {
  233. return this._filename;
  234. }
  235. }, {
  236. key: "contentLength",
  237. get: function get() {
  238. return this._contentLength;
  239. }
  240. }, {
  241. key: "isRangeSupported",
  242. get: function get() {
  243. return this._isRangeSupported;
  244. }
  245. }, {
  246. key: "isStreamingSupported",
  247. get: function get() {
  248. return this._isStreamingSupported;
  249. }
  250. }]);
  251. return BaseFullReader;
  252. }();
  253. var BaseRangeReader =
  254. /*#__PURE__*/
  255. function () {
  256. function BaseRangeReader(stream) {
  257. _classCallCheck(this, BaseRangeReader);
  258. this._url = stream.url;
  259. this._done = false;
  260. this._storedError = null;
  261. this.onProgress = null;
  262. this._loaded = 0;
  263. this._readableStream = null;
  264. this._readCapability = (0, _util.createPromiseCapability)();
  265. var source = stream.source;
  266. this._isStreamingSupported = !source.disableStream;
  267. }
  268. _createClass(BaseRangeReader, [{
  269. key: "read",
  270. value: function () {
  271. var _read2 = _asyncToGenerator(
  272. /*#__PURE__*/
  273. _regenerator.default.mark(function _callee2() {
  274. var chunk, buffer;
  275. return _regenerator.default.wrap(function _callee2$(_context2) {
  276. while (1) {
  277. switch (_context2.prev = _context2.next) {
  278. case 0:
  279. _context2.next = 2;
  280. return this._readCapability.promise;
  281. case 2:
  282. if (!this._done) {
  283. _context2.next = 4;
  284. break;
  285. }
  286. return _context2.abrupt("return", {
  287. value: undefined,
  288. done: true
  289. });
  290. case 4:
  291. if (!this._storedError) {
  292. _context2.next = 6;
  293. break;
  294. }
  295. throw this._storedError;
  296. case 6:
  297. chunk = this._readableStream.read();
  298. if (!(chunk === null)) {
  299. _context2.next = 10;
  300. break;
  301. }
  302. this._readCapability = (0, _util.createPromiseCapability)();
  303. return _context2.abrupt("return", this.read());
  304. case 10:
  305. this._loaded += chunk.length;
  306. if (this.onProgress) {
  307. this.onProgress({
  308. loaded: this._loaded
  309. });
  310. }
  311. buffer = new Uint8Array(chunk).buffer;
  312. return _context2.abrupt("return", {
  313. value: buffer,
  314. done: false
  315. });
  316. case 14:
  317. case "end":
  318. return _context2.stop();
  319. }
  320. }
  321. }, _callee2, this);
  322. }));
  323. function read() {
  324. return _read2.apply(this, arguments);
  325. }
  326. return read;
  327. }()
  328. }, {
  329. key: "cancel",
  330. value: function cancel(reason) {
  331. if (!this._readableStream) {
  332. this._error(reason);
  333. return;
  334. }
  335. this._readableStream.destroy(reason);
  336. }
  337. }, {
  338. key: "_error",
  339. value: function _error(reason) {
  340. this._storedError = reason;
  341. this._readCapability.resolve();
  342. }
  343. }, {
  344. key: "_setReadableStream",
  345. value: function _setReadableStream(readableStream) {
  346. var _this2 = this;
  347. this._readableStream = readableStream;
  348. readableStream.on('readable', function () {
  349. _this2._readCapability.resolve();
  350. });
  351. readableStream.on('end', function () {
  352. readableStream.destroy();
  353. _this2._done = true;
  354. _this2._readCapability.resolve();
  355. });
  356. readableStream.on('error', function (reason) {
  357. _this2._error(reason);
  358. });
  359. if (this._storedError) {
  360. this._readableStream.destroy(this._storedError);
  361. }
  362. }
  363. }, {
  364. key: "isStreamingSupported",
  365. get: function get() {
  366. return this._isStreamingSupported;
  367. }
  368. }]);
  369. return BaseRangeReader;
  370. }();
  371. function createRequestOptions(url, headers) {
  372. return {
  373. protocol: url.protocol,
  374. auth: url.auth,
  375. host: url.hostname,
  376. port: url.port,
  377. path: url.path,
  378. method: 'GET',
  379. headers: headers
  380. };
  381. }
  382. var PDFNodeStreamFullReader =
  383. /*#__PURE__*/
  384. function (_BaseFullReader) {
  385. _inherits(PDFNodeStreamFullReader, _BaseFullReader);
  386. function PDFNodeStreamFullReader(stream) {
  387. var _this3;
  388. _classCallCheck(this, PDFNodeStreamFullReader);
  389. _this3 = _possibleConstructorReturn(this, _getPrototypeOf(PDFNodeStreamFullReader).call(this, stream));
  390. var handleResponse = function handleResponse(response) {
  391. if (response.statusCode === 404) {
  392. var error = new _util.MissingPDFException("Missing PDF \"".concat(_this3._url, "\"."));
  393. _this3._storedError = error;
  394. _this3._headersCapability.reject(error);
  395. return;
  396. }
  397. _this3._headersCapability.resolve();
  398. _this3._setReadableStream(response);
  399. var getResponseHeader = function getResponseHeader(name) {
  400. return _this3._readableStream.headers[name.toLowerCase()];
  401. };
  402. var _validateRangeRequest = (0, _network_utils.validateRangeRequestCapabilities)({
  403. getResponseHeader: getResponseHeader,
  404. isHttp: stream.isHttp,
  405. rangeChunkSize: _this3._rangeChunkSize,
  406. disableRange: _this3._disableRange
  407. }),
  408. allowRangeRequests = _validateRangeRequest.allowRangeRequests,
  409. suggestedLength = _validateRangeRequest.suggestedLength;
  410. _this3._isRangeSupported = allowRangeRequests;
  411. _this3._contentLength = suggestedLength || _this3._contentLength;
  412. _this3._filename = (0, _network_utils.extractFilenameFromHeader)(getResponseHeader);
  413. };
  414. _this3._request = null;
  415. if (_this3._url.protocol === 'http:') {
  416. _this3._request = http.request(createRequestOptions(_this3._url, stream.httpHeaders), handleResponse);
  417. } else {
  418. _this3._request = https.request(createRequestOptions(_this3._url, stream.httpHeaders), handleResponse);
  419. }
  420. _this3._request.on('error', function (reason) {
  421. _this3._storedError = reason;
  422. _this3._headersCapability.reject(reason);
  423. });
  424. _this3._request.end();
  425. return _this3;
  426. }
  427. return PDFNodeStreamFullReader;
  428. }(BaseFullReader);
  429. var PDFNodeStreamRangeReader =
  430. /*#__PURE__*/
  431. function (_BaseRangeReader) {
  432. _inherits(PDFNodeStreamRangeReader, _BaseRangeReader);
  433. function PDFNodeStreamRangeReader(stream, start, end) {
  434. var _this4;
  435. _classCallCheck(this, PDFNodeStreamRangeReader);
  436. _this4 = _possibleConstructorReturn(this, _getPrototypeOf(PDFNodeStreamRangeReader).call(this, stream));
  437. _this4._httpHeaders = {};
  438. for (var property in stream.httpHeaders) {
  439. var value = stream.httpHeaders[property];
  440. if (typeof value === 'undefined') {
  441. continue;
  442. }
  443. _this4._httpHeaders[property] = value;
  444. }
  445. _this4._httpHeaders['Range'] = "bytes=".concat(start, "-").concat(end - 1);
  446. var handleResponse = function handleResponse(response) {
  447. if (response.statusCode === 404) {
  448. var error = new _util.MissingPDFException("Missing PDF \"".concat(_this4._url, "\"."));
  449. _this4._storedError = error;
  450. return;
  451. }
  452. _this4._setReadableStream(response);
  453. };
  454. _this4._request = null;
  455. if (_this4._url.protocol === 'http:') {
  456. _this4._request = http.request(createRequestOptions(_this4._url, _this4._httpHeaders), handleResponse);
  457. } else {
  458. _this4._request = https.request(createRequestOptions(_this4._url, _this4._httpHeaders), handleResponse);
  459. }
  460. _this4._request.on('error', function (reason) {
  461. _this4._storedError = reason;
  462. });
  463. _this4._request.end();
  464. return _this4;
  465. }
  466. return PDFNodeStreamRangeReader;
  467. }(BaseRangeReader);
  468. var PDFNodeStreamFsFullReader =
  469. /*#__PURE__*/
  470. function (_BaseFullReader2) {
  471. _inherits(PDFNodeStreamFsFullReader, _BaseFullReader2);
  472. function PDFNodeStreamFsFullReader(stream) {
  473. var _this5;
  474. _classCallCheck(this, PDFNodeStreamFsFullReader);
  475. _this5 = _possibleConstructorReturn(this, _getPrototypeOf(PDFNodeStreamFsFullReader).call(this, stream));
  476. var path = decodeURIComponent(_this5._url.path);
  477. if (fileUriRegex.test(_this5._url.href)) {
  478. path = path.replace(/^\//, '');
  479. }
  480. fs.lstat(path, function (error, stat) {
  481. if (error) {
  482. if (error.code === 'ENOENT') {
  483. error = new _util.MissingPDFException("Missing PDF \"".concat(path, "\"."));
  484. }
  485. _this5._storedError = error;
  486. _this5._headersCapability.reject(error);
  487. return;
  488. }
  489. _this5._contentLength = stat.size;
  490. _this5._setReadableStream(fs.createReadStream(path));
  491. _this5._headersCapability.resolve();
  492. });
  493. return _this5;
  494. }
  495. return PDFNodeStreamFsFullReader;
  496. }(BaseFullReader);
  497. var PDFNodeStreamFsRangeReader =
  498. /*#__PURE__*/
  499. function (_BaseRangeReader2) {
  500. _inherits(PDFNodeStreamFsRangeReader, _BaseRangeReader2);
  501. function PDFNodeStreamFsRangeReader(stream, start, end) {
  502. var _this6;
  503. _classCallCheck(this, PDFNodeStreamFsRangeReader);
  504. _this6 = _possibleConstructorReturn(this, _getPrototypeOf(PDFNodeStreamFsRangeReader).call(this, stream));
  505. var path = decodeURIComponent(_this6._url.path);
  506. if (fileUriRegex.test(_this6._url.href)) {
  507. path = path.replace(/^\//, '');
  508. }
  509. _this6._setReadableStream(fs.createReadStream(path, {
  510. start: start,
  511. end: end - 1
  512. }));
  513. return _this6;
  514. }
  515. return PDFNodeStreamFsRangeReader;
  516. }(BaseRangeReader);