fetch_stream_spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright 2019 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 { PDFFetchStream } from "../../src/display/fetch_stream.js";
  17. describe("fetch_stream", function () {
  18. const pdfUrl = new URL("../pdfs/tracemonkey.pdf", window.location).href;
  19. const pdfLength = 1016315;
  20. it("read with streaming", async function () {
  21. const stream = new PDFFetchStream({
  22. url: pdfUrl,
  23. disableStream: false,
  24. disableRange: true,
  25. });
  26. const fullReader = stream.getFullReader();
  27. let isStreamingSupported, isRangeSupported;
  28. const promise = fullReader.headersReady.then(function () {
  29. isStreamingSupported = fullReader.isStreamingSupported;
  30. isRangeSupported = fullReader.isRangeSupported;
  31. });
  32. let len = 0;
  33. const read = function () {
  34. return fullReader.read().then(function (result) {
  35. if (result.done) {
  36. return undefined;
  37. }
  38. len += result.value.byteLength;
  39. return read();
  40. });
  41. };
  42. await Promise.all([read(), promise]);
  43. expect(len).toEqual(pdfLength);
  44. expect(isStreamingSupported).toEqual(true);
  45. expect(isRangeSupported).toEqual(false);
  46. });
  47. it("read ranges with streaming", async function () {
  48. const rangeSize = 32768;
  49. const stream = new PDFFetchStream({
  50. url: pdfUrl,
  51. rangeChunkSize: rangeSize,
  52. disableStream: false,
  53. disableRange: false,
  54. });
  55. const fullReader = stream.getFullReader();
  56. let isStreamingSupported, isRangeSupported, fullReaderCancelled;
  57. const promise = fullReader.headersReady.then(function () {
  58. isStreamingSupported = fullReader.isStreamingSupported;
  59. isRangeSupported = fullReader.isRangeSupported;
  60. // We shall be able to close full reader without any issue.
  61. fullReader.cancel(new AbortException("Don't need fullReader."));
  62. fullReaderCancelled = true;
  63. });
  64. const tailSize = pdfLength % rangeSize || rangeSize;
  65. const rangeReader1 = stream.getRangeReader(
  66. pdfLength - tailSize - rangeSize,
  67. pdfLength - tailSize
  68. );
  69. const rangeReader2 = stream.getRangeReader(pdfLength - tailSize, pdfLength);
  70. const result1 = { value: 0 },
  71. result2 = { value: 0 };
  72. const read = function (reader, lenResult) {
  73. return reader.read().then(function (result) {
  74. if (result.done) {
  75. return undefined;
  76. }
  77. lenResult.value += result.value.byteLength;
  78. return read(reader, lenResult);
  79. });
  80. };
  81. await Promise.all([
  82. read(rangeReader1, result1),
  83. read(rangeReader2, result2),
  84. promise,
  85. ]);
  86. expect(isStreamingSupported).toEqual(true);
  87. expect(isRangeSupported).toEqual(true);
  88. expect(fullReaderCancelled).toEqual(true);
  89. expect(result1.value).toEqual(rangeSize);
  90. expect(result2.value).toEqual(tailSize);
  91. });
  92. });