network_spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { AbortException } from "../../src/shared/util.js";
  16. import { PDFNetworkStream } from "../../src/display/network.js";
  17. describe("network", function () {
  18. const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href;
  19. const pdf1Length = 1016315;
  20. it("read without stream and range", async function () {
  21. const stream = new PDFNetworkStream({
  22. url: pdf1,
  23. rangeChunkSize: 65536,
  24. disableStream: true,
  25. disableRange: true,
  26. });
  27. const fullReader = stream.getFullReader();
  28. let isStreamingSupported, isRangeSupported;
  29. const promise = fullReader.headersReady.then(function () {
  30. isStreamingSupported = fullReader.isStreamingSupported;
  31. isRangeSupported = fullReader.isRangeSupported;
  32. });
  33. let len = 0,
  34. count = 0;
  35. const read = function () {
  36. return fullReader.read().then(function (result) {
  37. if (result.done) {
  38. return undefined;
  39. }
  40. count++;
  41. len += result.value.byteLength;
  42. return read();
  43. });
  44. };
  45. await Promise.all([read(), promise]);
  46. expect(len).toEqual(pdf1Length);
  47. expect(count).toEqual(1);
  48. expect(isStreamingSupported).toEqual(false);
  49. expect(isRangeSupported).toEqual(false);
  50. });
  51. it("read custom ranges", async function () {
  52. // We don't test on browsers that don't support range request, so
  53. // requiring this test to pass.
  54. const rangeSize = 32768;
  55. const stream = new PDFNetworkStream({
  56. url: pdf1,
  57. length: pdf1Length,
  58. rangeChunkSize: rangeSize,
  59. disableStream: true,
  60. disableRange: false,
  61. });
  62. const fullReader = stream.getFullReader();
  63. let isStreamingSupported, isRangeSupported, fullReaderCancelled;
  64. const promise = fullReader.headersReady.then(function () {
  65. isStreamingSupported = fullReader.isStreamingSupported;
  66. isRangeSupported = fullReader.isRangeSupported;
  67. // we shall be able to close the full reader without issues
  68. fullReader.cancel(new AbortException("Don't need fullReader."));
  69. fullReaderCancelled = true;
  70. });
  71. // Skipping fullReader results, requesting something from the PDF end.
  72. const tailSize = pdf1Length % rangeSize || rangeSize;
  73. const range1Reader = stream.getRangeReader(
  74. pdf1Length - tailSize - rangeSize,
  75. pdf1Length - tailSize
  76. );
  77. const range2Reader = stream.getRangeReader(
  78. pdf1Length - tailSize,
  79. pdf1Length
  80. );
  81. const result1 = { value: 0 },
  82. result2 = { value: 0 };
  83. const read = function (reader, lenResult) {
  84. return reader.read().then(function (result) {
  85. if (result.done) {
  86. return undefined;
  87. }
  88. lenResult.value += result.value.byteLength;
  89. return read(reader, lenResult);
  90. });
  91. };
  92. await Promise.all([
  93. read(range1Reader, result1),
  94. read(range2Reader, result2),
  95. promise,
  96. ]);
  97. expect(result1.value).toEqual(rangeSize);
  98. expect(result2.value).toEqual(tailSize);
  99. expect(isStreamingSupported).toEqual(false);
  100. expect(isRangeSupported).toEqual(true);
  101. expect(fullReaderCancelled).toEqual(true);
  102. });
  103. });