node_stream_spec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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(done => {
  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. done();
  73. });
  74. afterAll(done => {
  75. server.close();
  76. done();
  77. });
  78. it("read both http(s) and filesystem pdf files", function (done) {
  79. const stream1 = new _node_stream.PDFNodeStream({
  80. url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
  81. rangeChunkSize: 65536,
  82. disableStream: true,
  83. disableRange: true
  84. });
  85. const stream2 = new _node_stream.PDFNodeStream({
  86. url: pdf,
  87. rangeChunkSize: 65536,
  88. disableStream: true,
  89. disableRange: true
  90. });
  91. const fullReader1 = stream1.getFullReader();
  92. const fullReader2 = stream2.getFullReader();
  93. let isStreamingSupported1, isRangeSupported1;
  94. const promise1 = fullReader1.headersReady.then(() => {
  95. isStreamingSupported1 = fullReader1.isStreamingSupported;
  96. isRangeSupported1 = fullReader1.isRangeSupported;
  97. });
  98. let isStreamingSupported2, isRangeSupported2;
  99. const promise2 = fullReader2.headersReady.then(() => {
  100. isStreamingSupported2 = fullReader2.isStreamingSupported;
  101. isRangeSupported2 = fullReader2.isRangeSupported;
  102. });
  103. let len1 = 0,
  104. len2 = 0;
  105. const read1 = function () {
  106. return fullReader1.read().then(function (result) {
  107. if (result.done) {
  108. return undefined;
  109. }
  110. len1 += result.value.byteLength;
  111. return read1();
  112. });
  113. };
  114. const read2 = function () {
  115. return fullReader2.read().then(function (result) {
  116. if (result.done) {
  117. return undefined;
  118. }
  119. len2 += result.value.byteLength;
  120. return read2();
  121. });
  122. };
  123. const readPromise = Promise.all([read1(), read2(), promise1, promise2]);
  124. readPromise.then(result => {
  125. expect(isStreamingSupported1).toEqual(false);
  126. expect(isRangeSupported1).toEqual(false);
  127. expect(isStreamingSupported2).toEqual(false);
  128. expect(isRangeSupported2).toEqual(false);
  129. expect(len1).toEqual(pdfLength);
  130. expect(len1).toEqual(len2);
  131. done();
  132. }).catch(reason => {
  133. done.fail(reason);
  134. });
  135. });
  136. it("read custom ranges for both http(s) and filesystem urls", function (done) {
  137. const rangeSize = 32768;
  138. const stream1 = new _node_stream.PDFNodeStream({
  139. url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
  140. length: pdfLength,
  141. rangeChunkSize: rangeSize,
  142. disableStream: true,
  143. disableRange: false
  144. });
  145. const stream2 = new _node_stream.PDFNodeStream({
  146. url: pdf,
  147. length: pdfLength,
  148. rangeChunkSize: rangeSize,
  149. disableStream: true,
  150. disableRange: false
  151. });
  152. const fullReader1 = stream1.getFullReader();
  153. const fullReader2 = stream2.getFullReader();
  154. let isStreamingSupported1, isRangeSupported1, fullReaderCancelled1;
  155. let isStreamingSupported2, isRangeSupported2, fullReaderCancelled2;
  156. const promise1 = fullReader1.headersReady.then(function () {
  157. isStreamingSupported1 = fullReader1.isStreamingSupported;
  158. isRangeSupported1 = fullReader1.isRangeSupported;
  159. fullReader1.cancel(new _util.AbortException("Don't need fullReader1."));
  160. fullReaderCancelled1 = true;
  161. });
  162. const promise2 = fullReader2.headersReady.then(function () {
  163. isStreamingSupported2 = fullReader2.isStreamingSupported;
  164. isRangeSupported2 = fullReader2.isRangeSupported;
  165. fullReader2.cancel(new _util.AbortException("Don't need fullReader2."));
  166. fullReaderCancelled2 = true;
  167. });
  168. const tailSize = pdfLength % rangeSize || rangeSize;
  169. const range11Reader = stream1.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  170. const range12Reader = stream1.getRangeReader(pdfLength - tailSize, pdfLength);
  171. const range21Reader = stream2.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  172. const range22Reader = stream2.getRangeReader(pdfLength - tailSize, pdfLength);
  173. const result11 = {
  174. value: 0
  175. },
  176. result12 = {
  177. value: 0
  178. };
  179. const result21 = {
  180. value: 0
  181. },
  182. result22 = {
  183. value: 0
  184. };
  185. const read = function (reader, lenResult) {
  186. return reader.read().then(function (result) {
  187. if (result.done) {
  188. return undefined;
  189. }
  190. lenResult.value += result.value.byteLength;
  191. return read(reader, lenResult);
  192. });
  193. };
  194. const readPromises = Promise.all([read(range11Reader, result11), read(range12Reader, result12), read(range21Reader, result21), read(range22Reader, result22), promise1, promise2]);
  195. readPromises.then(function () {
  196. expect(result11.value).toEqual(rangeSize);
  197. expect(result12.value).toEqual(tailSize);
  198. expect(result21.value).toEqual(rangeSize);
  199. expect(result22.value).toEqual(tailSize);
  200. expect(isStreamingSupported1).toEqual(false);
  201. expect(isRangeSupported1).toEqual(true);
  202. expect(fullReaderCancelled1).toEqual(true);
  203. expect(isStreamingSupported2).toEqual(false);
  204. expect(isRangeSupported2).toEqual(true);
  205. expect(fullReaderCancelled2).toEqual(true);
  206. done();
  207. }).catch(function (reason) {
  208. done.fail(reason);
  209. });
  210. });
  211. });