network_spec.js 3.6 KB

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