node_stream_spec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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. if (!_is_node.isNodeJS) {
  27. throw new Error('The "node_stream" unit-tests can only be run in Node.js environments.');
  28. }
  29. const path = require("path");
  30. const url = require("url");
  31. const http = require("http");
  32. const fs = require("fs");
  33. describe("node_stream", function () {
  34. let server = null;
  35. let port = null;
  36. const pdf = url.parse(encodeURI("file://" + path.join(process.cwd(), "./test/pdfs/tracemonkey.pdf"))).href;
  37. const pdfLength = 1016315;
  38. beforeAll(function () {
  39. server = http.createServer((request, response) => {
  40. const filePath = process.cwd() + "/test/pdfs" + request.url;
  41. fs.lstat(filePath, (error, stat) => {
  42. if (error) {
  43. response.writeHead(404);
  44. response.end(`File ${request.url} not found!`);
  45. return;
  46. }
  47. if (!request.headers.range) {
  48. const contentLength = stat.size;
  49. const stream = fs.createReadStream(filePath);
  50. response.writeHead(200, {
  51. "Content-Type": "application/pdf",
  52. "Content-Length": contentLength,
  53. "Accept-Ranges": "bytes"
  54. });
  55. stream.pipe(response);
  56. } else {
  57. const [start, end] = request.headers.range.split("=")[1].split("-").map(x => {
  58. return Number(x);
  59. });
  60. const stream = fs.createReadStream(filePath, {
  61. start,
  62. end
  63. });
  64. response.writeHead(206, {
  65. "Content-Type": "application/pdf"
  66. });
  67. stream.pipe(response);
  68. }
  69. });
  70. }).listen(0);
  71. port = server.address().port;
  72. });
  73. afterAll(function () {
  74. server.close();
  75. });
  76. it("read both http(s) and filesystem pdf files", async function () {
  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. await Promise.all([read1(), read2(), promise1, promise2]);
  122. expect(isStreamingSupported1).toEqual(false);
  123. expect(isRangeSupported1).toEqual(false);
  124. expect(isStreamingSupported2).toEqual(false);
  125. expect(isRangeSupported2).toEqual(false);
  126. expect(len1).toEqual(pdfLength);
  127. expect(len1).toEqual(len2);
  128. });
  129. it("read custom ranges for both http(s) and filesystem urls", async function () {
  130. const rangeSize = 32768;
  131. const stream1 = new _node_stream.PDFNodeStream({
  132. url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
  133. length: pdfLength,
  134. rangeChunkSize: rangeSize,
  135. disableStream: true,
  136. disableRange: false
  137. });
  138. const stream2 = new _node_stream.PDFNodeStream({
  139. url: pdf,
  140. length: pdfLength,
  141. rangeChunkSize: rangeSize,
  142. disableStream: true,
  143. disableRange: false
  144. });
  145. const fullReader1 = stream1.getFullReader();
  146. const fullReader2 = stream2.getFullReader();
  147. let isStreamingSupported1, isRangeSupported1, fullReaderCancelled1;
  148. let isStreamingSupported2, isRangeSupported2, fullReaderCancelled2;
  149. const promise1 = fullReader1.headersReady.then(function () {
  150. isStreamingSupported1 = fullReader1.isStreamingSupported;
  151. isRangeSupported1 = fullReader1.isRangeSupported;
  152. fullReader1.cancel(new _util.AbortException("Don't need fullReader1."));
  153. fullReaderCancelled1 = true;
  154. });
  155. const promise2 = fullReader2.headersReady.then(function () {
  156. isStreamingSupported2 = fullReader2.isStreamingSupported;
  157. isRangeSupported2 = fullReader2.isRangeSupported;
  158. fullReader2.cancel(new _util.AbortException("Don't need fullReader2."));
  159. fullReaderCancelled2 = true;
  160. });
  161. const tailSize = pdfLength % rangeSize || rangeSize;
  162. const range11Reader = stream1.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  163. const range12Reader = stream1.getRangeReader(pdfLength - tailSize, pdfLength);
  164. const range21Reader = stream2.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  165. const range22Reader = stream2.getRangeReader(pdfLength - tailSize, pdfLength);
  166. const result11 = {
  167. value: 0
  168. },
  169. result12 = {
  170. value: 0
  171. };
  172. const result21 = {
  173. value: 0
  174. },
  175. result22 = {
  176. value: 0
  177. };
  178. const read = function (reader, lenResult) {
  179. return reader.read().then(function (result) {
  180. if (result.done) {
  181. return undefined;
  182. }
  183. lenResult.value += result.value.byteLength;
  184. return read(reader, lenResult);
  185. });
  186. };
  187. await Promise.all([read(range11Reader, result11), read(range12Reader, result12), read(range21Reader, result21), read(range22Reader, result22), promise1, promise2]);
  188. expect(result11.value).toEqual(rangeSize);
  189. expect(result12.value).toEqual(tailSize);
  190. expect(result21.value).toEqual(rangeSize);
  191. expect(result22.value).toEqual(tailSize);
  192. expect(isStreamingSupported1).toEqual(false);
  193. expect(isRangeSupported1).toEqual(true);
  194. expect(fullReaderCancelled1).toEqual(true);
  195. expect(isStreamingSupported2).toEqual(false);
  196. expect(isRangeSupported2).toEqual(true);
  197. expect(fullReaderCancelled2).toEqual(true);
  198. });
  199. });