node_stream_spec.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. var _util = require("../../shared/util.js");
  24. var _is_node = require("../../shared/is_node.js");
  25. var _node_stream = require("../../display/node_stream.js");
  26. (0, _util.assert)(_is_node.isNodeJS);
  27. const path = require("path");
  28. const url = require("url");
  29. const http = require("http");
  30. const fs = require("fs");
  31. describe("node_stream", function () {
  32. let server = null;
  33. let port = null;
  34. const pdf = url.parse(encodeURI("file://" + path.join(process.cwd(), "./test/pdfs/tracemonkey.pdf"))).href;
  35. const pdfLength = 1016315;
  36. beforeAll(done => {
  37. server = http.createServer((request, response) => {
  38. const filePath = process.cwd() + "/test/pdfs" + request.url;
  39. fs.lstat(filePath, (error, stat) => {
  40. if (error) {
  41. response.writeHead(404);
  42. response.end(`File ${request.url} not found!`);
  43. return;
  44. }
  45. if (!request.headers["range"]) {
  46. const contentLength = stat.size;
  47. const stream = fs.createReadStream(filePath);
  48. response.writeHead(200, {
  49. "Content-Type": "application/pdf",
  50. "Content-Length": contentLength,
  51. "Accept-Ranges": "bytes"
  52. });
  53. stream.pipe(response);
  54. } else {
  55. const [start, end] = request.headers["range"].split("=")[1].split("-").map(x => {
  56. return Number(x);
  57. });
  58. const stream = fs.createReadStream(filePath, {
  59. start,
  60. end
  61. });
  62. response.writeHead(206, {
  63. "Content-Type": "application/pdf"
  64. });
  65. stream.pipe(response);
  66. }
  67. });
  68. }).listen(0);
  69. port = server.address().port;
  70. done();
  71. });
  72. afterAll(done => {
  73. server.close();
  74. done();
  75. });
  76. it("read both http(s) and filesystem pdf files", function (done) {
  77. const stream1 = new _node_stream.PDFNodeStream({
  78. url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
  79. rangeChunkSize: 65536,
  80. disableStream: true,
  81. disableRange: true
  82. });
  83. const stream2 = new _node_stream.PDFNodeStream({
  84. url: pdf,
  85. rangeChunkSize: 65536,
  86. disableStream: true,
  87. disableRange: true
  88. });
  89. const fullReader1 = stream1.getFullReader();
  90. const fullReader2 = stream2.getFullReader();
  91. let isStreamingSupported1, isRangeSupported1;
  92. const promise1 = fullReader1.headersReady.then(() => {
  93. isStreamingSupported1 = fullReader1.isStreamingSupported;
  94. isRangeSupported1 = fullReader1.isRangeSupported;
  95. });
  96. let isStreamingSupported2, isRangeSupported2;
  97. const promise2 = fullReader2.headersReady.then(() => {
  98. isStreamingSupported2 = fullReader2.isStreamingSupported;
  99. isRangeSupported2 = fullReader2.isRangeSupported;
  100. });
  101. let len1 = 0,
  102. len2 = 0;
  103. const read1 = function () {
  104. return fullReader1.read().then(function (result) {
  105. if (result.done) {
  106. return undefined;
  107. }
  108. len1 += result.value.byteLength;
  109. return read1();
  110. });
  111. };
  112. const read2 = function () {
  113. return fullReader2.read().then(function (result) {
  114. if (result.done) {
  115. return undefined;
  116. }
  117. len2 += result.value.byteLength;
  118. return read2();
  119. });
  120. };
  121. const readPromise = Promise.all([read1(), read2(), promise1, promise2]);
  122. readPromise.then(result => {
  123. expect(isStreamingSupported1).toEqual(false);
  124. expect(isRangeSupported1).toEqual(false);
  125. expect(isStreamingSupported2).toEqual(false);
  126. expect(isRangeSupported2).toEqual(false);
  127. expect(len1).toEqual(pdfLength);
  128. expect(len1).toEqual(len2);
  129. done();
  130. }).catch(reason => {
  131. done.fail(reason);
  132. });
  133. });
  134. it("read custom ranges for both http(s) and filesystem urls", function (done) {
  135. const rangeSize = 32768;
  136. const stream1 = new _node_stream.PDFNodeStream({
  137. url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
  138. length: pdfLength,
  139. rangeChunkSize: rangeSize,
  140. disableStream: true,
  141. disableRange: false
  142. });
  143. const stream2 = new _node_stream.PDFNodeStream({
  144. url: pdf,
  145. length: pdfLength,
  146. rangeChunkSize: rangeSize,
  147. disableStream: true,
  148. disableRange: false
  149. });
  150. const fullReader1 = stream1.getFullReader();
  151. const fullReader2 = stream2.getFullReader();
  152. let isStreamingSupported1, isRangeSupported1, fullReaderCancelled1;
  153. let isStreamingSupported2, isRangeSupported2, fullReaderCancelled2;
  154. const promise1 = fullReader1.headersReady.then(function () {
  155. isStreamingSupported1 = fullReader1.isStreamingSupported;
  156. isRangeSupported1 = fullReader1.isRangeSupported;
  157. fullReader1.cancel(new _util.AbortException("Don't need fullReader1."));
  158. fullReaderCancelled1 = true;
  159. });
  160. const promise2 = fullReader2.headersReady.then(function () {
  161. isStreamingSupported2 = fullReader2.isStreamingSupported;
  162. isRangeSupported2 = fullReader2.isRangeSupported;
  163. fullReader2.cancel(new _util.AbortException("Don't need fullReader2."));
  164. fullReaderCancelled2 = true;
  165. });
  166. const tailSize = pdfLength % rangeSize || rangeSize;
  167. const range11Reader = stream1.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  168. const range12Reader = stream1.getRangeReader(pdfLength - tailSize, pdfLength);
  169. const range21Reader = stream2.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  170. const range22Reader = stream2.getRangeReader(pdfLength - tailSize, pdfLength);
  171. const result11 = {
  172. value: 0
  173. },
  174. result12 = {
  175. value: 0
  176. };
  177. const result21 = {
  178. value: 0
  179. },
  180. result22 = {
  181. value: 0
  182. };
  183. const read = function (reader, lenResult) {
  184. return reader.read().then(function (result) {
  185. if (result.done) {
  186. return undefined;
  187. }
  188. lenResult.value += result.value.byteLength;
  189. return read(reader, lenResult);
  190. });
  191. };
  192. const readPromises = Promise.all([read(range11Reader, result11), read(range12Reader, result12), read(range21Reader, result21), read(range22Reader, result22), promise1, promise2]);
  193. readPromises.then(function () {
  194. expect(result11.value).toEqual(rangeSize);
  195. expect(result12.value).toEqual(tailSize);
  196. expect(result21.value).toEqual(rangeSize);
  197. expect(result22.value).toEqual(tailSize);
  198. expect(isStreamingSupported1).toEqual(false);
  199. expect(isRangeSupported1).toEqual(true);
  200. expect(fullReaderCancelled1).toEqual(true);
  201. expect(isStreamingSupported2).toEqual(false);
  202. expect(isRangeSupported2).toEqual(true);
  203. expect(fullReaderCancelled2).toEqual(true);
  204. done();
  205. }).catch(function (reason) {
  206. done.fail(reason);
  207. });
  208. });
  209. });