node_stream.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 _util = require("../shared/util.js");
  28. var _network_utils = require("./network_utils.js");
  29. ;
  30. const fs = require("fs");
  31. const http = require("http");
  32. const https = require("https");
  33. const url = require("url");
  34. const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
  35. function parseUrl(sourceUrl) {
  36. const parsedUrl = url.parse(sourceUrl);
  37. if (parsedUrl.protocol === "file:" || parsedUrl.host) {
  38. return parsedUrl;
  39. }
  40. if (/^[a-z]:[/\\]/i.test(sourceUrl)) {
  41. return url.parse(`file:///${sourceUrl}`);
  42. }
  43. if (!parsedUrl.host) {
  44. parsedUrl.protocol = "file:";
  45. }
  46. return parsedUrl;
  47. }
  48. class PDFNodeStream {
  49. constructor(source) {
  50. this.source = source;
  51. this.url = parseUrl(source.url);
  52. this.isHttp = this.url.protocol === "http:" || this.url.protocol === "https:";
  53. this.isFsUrl = this.url.protocol === "file:";
  54. this.httpHeaders = this.isHttp && source.httpHeaders || {};
  55. this._fullRequestReader = null;
  56. this._rangeRequestReaders = [];
  57. }
  58. get _progressiveDataLength() {
  59. return this._fullRequestReader ? this._fullRequestReader._loaded : 0;
  60. }
  61. getFullReader() {
  62. (0, _util.assert)(!this._fullRequestReader, "PDFNodeStream.getFullReader can only be called once.");
  63. this._fullRequestReader = this.isFsUrl ? new PDFNodeStreamFsFullReader(this) : new PDFNodeStreamFullReader(this);
  64. return this._fullRequestReader;
  65. }
  66. getRangeReader(start, end) {
  67. if (end <= this._progressiveDataLength) {
  68. return null;
  69. }
  70. const rangeReader = this.isFsUrl ? new PDFNodeStreamFsRangeReader(this, start, end) : new PDFNodeStreamRangeReader(this, start, end);
  71. this._rangeRequestReaders.push(rangeReader);
  72. return rangeReader;
  73. }
  74. cancelAllRequests(reason) {
  75. if (this._fullRequestReader) {
  76. this._fullRequestReader.cancel(reason);
  77. }
  78. const readers = this._rangeRequestReaders.slice(0);
  79. readers.forEach(function (reader) {
  80. reader.cancel(reason);
  81. });
  82. }
  83. }
  84. exports.PDFNodeStream = PDFNodeStream;
  85. class BaseFullReader {
  86. constructor(stream) {
  87. this._url = stream.url;
  88. this._done = false;
  89. this._storedError = null;
  90. this.onProgress = null;
  91. const source = stream.source;
  92. this._contentLength = source.length;
  93. this._loaded = 0;
  94. this._filename = null;
  95. this._disableRange = source.disableRange || false;
  96. this._rangeChunkSize = source.rangeChunkSize;
  97. if (!this._rangeChunkSize && !this._disableRange) {
  98. this._disableRange = true;
  99. }
  100. this._isStreamingSupported = !source.disableStream;
  101. this._isRangeSupported = !source.disableRange;
  102. this._readableStream = null;
  103. this._readCapability = (0, _util.createPromiseCapability)();
  104. this._headersCapability = (0, _util.createPromiseCapability)();
  105. }
  106. get headersReady() {
  107. return this._headersCapability.promise;
  108. }
  109. get filename() {
  110. return this._filename;
  111. }
  112. get contentLength() {
  113. return this._contentLength;
  114. }
  115. get isRangeSupported() {
  116. return this._isRangeSupported;
  117. }
  118. get isStreamingSupported() {
  119. return this._isStreamingSupported;
  120. }
  121. async read() {
  122. await this._readCapability.promise;
  123. if (this._done) {
  124. return {
  125. value: undefined,
  126. done: true
  127. };
  128. }
  129. if (this._storedError) {
  130. throw this._storedError;
  131. }
  132. const chunk = this._readableStream.read();
  133. if (chunk === null) {
  134. this._readCapability = (0, _util.createPromiseCapability)();
  135. return this.read();
  136. }
  137. this._loaded += chunk.length;
  138. if (this.onProgress) {
  139. this.onProgress({
  140. loaded: this._loaded,
  141. total: this._contentLength
  142. });
  143. }
  144. const buffer = new Uint8Array(chunk).buffer;
  145. return {
  146. value: buffer,
  147. done: false
  148. };
  149. }
  150. cancel(reason) {
  151. if (!this._readableStream) {
  152. this._error(reason);
  153. return;
  154. }
  155. this._readableStream.destroy(reason);
  156. }
  157. _error(reason) {
  158. this._storedError = reason;
  159. this._readCapability.resolve();
  160. }
  161. _setReadableStream(readableStream) {
  162. this._readableStream = readableStream;
  163. readableStream.on("readable", () => {
  164. this._readCapability.resolve();
  165. });
  166. readableStream.on("end", () => {
  167. readableStream.destroy();
  168. this._done = true;
  169. this._readCapability.resolve();
  170. });
  171. readableStream.on("error", reason => {
  172. this._error(reason);
  173. });
  174. if (!this._isStreamingSupported && this._isRangeSupported) {
  175. this._error(new _util.AbortException("streaming is disabled"));
  176. }
  177. if (this._storedError) {
  178. this._readableStream.destroy(this._storedError);
  179. }
  180. }
  181. }
  182. class BaseRangeReader {
  183. constructor(stream) {
  184. this._url = stream.url;
  185. this._done = false;
  186. this._storedError = null;
  187. this.onProgress = null;
  188. this._loaded = 0;
  189. this._readableStream = null;
  190. this._readCapability = (0, _util.createPromiseCapability)();
  191. const source = stream.source;
  192. this._isStreamingSupported = !source.disableStream;
  193. }
  194. get isStreamingSupported() {
  195. return this._isStreamingSupported;
  196. }
  197. async read() {
  198. await this._readCapability.promise;
  199. if (this._done) {
  200. return {
  201. value: undefined,
  202. done: true
  203. };
  204. }
  205. if (this._storedError) {
  206. throw this._storedError;
  207. }
  208. const chunk = this._readableStream.read();
  209. if (chunk === null) {
  210. this._readCapability = (0, _util.createPromiseCapability)();
  211. return this.read();
  212. }
  213. this._loaded += chunk.length;
  214. if (this.onProgress) {
  215. this.onProgress({
  216. loaded: this._loaded
  217. });
  218. }
  219. const buffer = new Uint8Array(chunk).buffer;
  220. return {
  221. value: buffer,
  222. done: false
  223. };
  224. }
  225. cancel(reason) {
  226. if (!this._readableStream) {
  227. this._error(reason);
  228. return;
  229. }
  230. this._readableStream.destroy(reason);
  231. }
  232. _error(reason) {
  233. this._storedError = reason;
  234. this._readCapability.resolve();
  235. }
  236. _setReadableStream(readableStream) {
  237. this._readableStream = readableStream;
  238. readableStream.on("readable", () => {
  239. this._readCapability.resolve();
  240. });
  241. readableStream.on("end", () => {
  242. readableStream.destroy();
  243. this._done = true;
  244. this._readCapability.resolve();
  245. });
  246. readableStream.on("error", reason => {
  247. this._error(reason);
  248. });
  249. if (this._storedError) {
  250. this._readableStream.destroy(this._storedError);
  251. }
  252. }
  253. }
  254. function createRequestOptions(parsedUrl, headers) {
  255. return {
  256. protocol: parsedUrl.protocol,
  257. auth: parsedUrl.auth,
  258. host: parsedUrl.hostname,
  259. port: parsedUrl.port,
  260. path: parsedUrl.path,
  261. method: "GET",
  262. headers
  263. };
  264. }
  265. class PDFNodeStreamFullReader extends BaseFullReader {
  266. constructor(stream) {
  267. super(stream);
  268. const handleResponse = response => {
  269. if (response.statusCode === 404) {
  270. const error = new _util.MissingPDFException(`Missing PDF "${this._url}".`);
  271. this._storedError = error;
  272. this._headersCapability.reject(error);
  273. return;
  274. }
  275. this._headersCapability.resolve();
  276. this._setReadableStream(response);
  277. const getResponseHeader = name => {
  278. return this._readableStream.headers[name.toLowerCase()];
  279. };
  280. const {
  281. allowRangeRequests,
  282. suggestedLength
  283. } = (0, _network_utils.validateRangeRequestCapabilities)({
  284. getResponseHeader,
  285. isHttp: stream.isHttp,
  286. rangeChunkSize: this._rangeChunkSize,
  287. disableRange: this._disableRange
  288. });
  289. this._isRangeSupported = allowRangeRequests;
  290. this._contentLength = suggestedLength || this._contentLength;
  291. this._filename = (0, _network_utils.extractFilenameFromHeader)(getResponseHeader);
  292. };
  293. this._request = null;
  294. if (this._url.protocol === "http:") {
  295. this._request = http.request(createRequestOptions(this._url, stream.httpHeaders), handleResponse);
  296. } else {
  297. this._request = https.request(createRequestOptions(this._url, stream.httpHeaders), handleResponse);
  298. }
  299. this._request.on("error", reason => {
  300. this._storedError = reason;
  301. this._headersCapability.reject(reason);
  302. });
  303. this._request.end();
  304. }
  305. }
  306. class PDFNodeStreamRangeReader extends BaseRangeReader {
  307. constructor(stream, start, end) {
  308. super(stream);
  309. this._httpHeaders = {};
  310. for (const property in stream.httpHeaders) {
  311. const value = stream.httpHeaders[property];
  312. if (typeof value === "undefined") {
  313. continue;
  314. }
  315. this._httpHeaders[property] = value;
  316. }
  317. this._httpHeaders.Range = `bytes=${start}-${end - 1}`;
  318. const handleResponse = response => {
  319. if (response.statusCode === 404) {
  320. const error = new _util.MissingPDFException(`Missing PDF "${this._url}".`);
  321. this._storedError = error;
  322. return;
  323. }
  324. this._setReadableStream(response);
  325. };
  326. this._request = null;
  327. if (this._url.protocol === "http:") {
  328. this._request = http.request(createRequestOptions(this._url, this._httpHeaders), handleResponse);
  329. } else {
  330. this._request = https.request(createRequestOptions(this._url, this._httpHeaders), handleResponse);
  331. }
  332. this._request.on("error", reason => {
  333. this._storedError = reason;
  334. });
  335. this._request.end();
  336. }
  337. }
  338. class PDFNodeStreamFsFullReader extends BaseFullReader {
  339. constructor(stream) {
  340. super(stream);
  341. let path = decodeURIComponent(this._url.path);
  342. if (fileUriRegex.test(this._url.href)) {
  343. path = path.replace(/^\//, "");
  344. }
  345. fs.lstat(path, (error, stat) => {
  346. if (error) {
  347. if (error.code === "ENOENT") {
  348. error = new _util.MissingPDFException(`Missing PDF "${path}".`);
  349. }
  350. this._storedError = error;
  351. this._headersCapability.reject(error);
  352. return;
  353. }
  354. this._contentLength = stat.size;
  355. this._setReadableStream(fs.createReadStream(path));
  356. this._headersCapability.resolve();
  357. });
  358. }
  359. }
  360. class PDFNodeStreamFsRangeReader extends BaseRangeReader {
  361. constructor(stream, start, end) {
  362. super(stream);
  363. let path = decodeURIComponent(this._url.path);
  364. if (fileUriRegex.test(this._url.href)) {
  365. path = path.replace(/^\//, "");
  366. }
  367. this._setReadableStream(fs.createReadStream(path, {
  368. start,
  369. end: end - 1
  370. }));
  371. }
  372. }