fetch_stream_spec.js 3.5 KB

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