2
0

fetch_stream_spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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 _fetch_stream = require("../../display/fetch_stream");
  24. describe('fetch_stream', function () {
  25. var pdfUrl = new URL('../pdfs/tracemonkey.pdf', window.location).href;
  26. var pdfLength = 1016315;
  27. it('read with streaming', function (done) {
  28. var stream = new _fetch_stream.PDFFetchStream({
  29. url: pdfUrl,
  30. disableStream: false,
  31. disableRange: true
  32. });
  33. var fullReader = stream.getFullReader();
  34. var isStreamingSupported, isRangeSupported;
  35. var promise = fullReader.headersReady.then(function () {
  36. isStreamingSupported = fullReader.isStreamingSupported;
  37. isRangeSupported = fullReader.isRangeSupported;
  38. });
  39. var len = 0;
  40. var read = function read() {
  41. return fullReader.read().then(function (result) {
  42. if (result.done) {
  43. return undefined;
  44. }
  45. len += result.value.byteLength;
  46. return read();
  47. });
  48. };
  49. var readPromise = Promise.all([read(), promise]);
  50. readPromise.then(function () {
  51. expect(len).toEqual(pdfLength);
  52. expect(isStreamingSupported).toEqual(true);
  53. expect(isRangeSupported).toEqual(false);
  54. done();
  55. })["catch"](done.fail);
  56. });
  57. it('read ranges with streaming', function (done) {
  58. var rangeSize = 32768;
  59. var stream = new _fetch_stream.PDFFetchStream({
  60. url: pdfUrl,
  61. rangeChunkSize: rangeSize,
  62. disableStream: false,
  63. disableRange: false
  64. });
  65. var fullReader = stream.getFullReader();
  66. var isStreamingSupported, isRangeSupported, fullReaderCancelled;
  67. var promise = fullReader.headersReady.then(function () {
  68. isStreamingSupported = fullReader.isStreamingSupported;
  69. isRangeSupported = fullReader.isRangeSupported;
  70. fullReader.cancel(new Error('Don\'t need full reader'));
  71. fullReaderCancelled = true;
  72. });
  73. var tailSize = pdfLength % rangeSize || rangeSize;
  74. var rangeReader1 = stream.getRangeReader(pdfLength - tailSize - rangeSize, pdfLength - tailSize);
  75. var rangeReader2 = stream.getRangeReader(pdfLength - tailSize, pdfLength);
  76. var result1 = {
  77. value: 0
  78. },
  79. result2 = {
  80. value: 0
  81. };
  82. var read = function read(reader, lenResult) {
  83. return reader.read().then(function (result) {
  84. if (result.done) {
  85. return undefined;
  86. }
  87. lenResult.value += result.value.byteLength;
  88. return read(reader, lenResult);
  89. });
  90. };
  91. var readPromise = Promise.all([read(rangeReader1, result1), read(rangeReader2, result2), promise]);
  92. readPromise.then(function () {
  93. expect(isStreamingSupported).toEqual(true);
  94. expect(isRangeSupported).toEqual(true);
  95. expect(fullReaderCancelled).toEqual(true);
  96. expect(result1.value).toEqual(rangeSize);
  97. expect(result2.value).toEqual(tailSize);
  98. done();
  99. })["catch"](done.fail);
  100. });
  101. });