123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import { AbortException } from "../../src/shared/util.js";
- import { PDFNetworkStream } from "../../src/display/network.js";
- describe("network", function () {
- const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href;
- const pdf1Length = 1016315;
- it("read without stream and range", async function () {
- const stream = new PDFNetworkStream({
- url: pdf1,
- rangeChunkSize: 65536,
- disableStream: true,
- disableRange: true,
- });
- const fullReader = stream.getFullReader();
- let isStreamingSupported, isRangeSupported;
- const promise = fullReader.headersReady.then(function () {
- isStreamingSupported = fullReader.isStreamingSupported;
- isRangeSupported = fullReader.isRangeSupported;
- });
- let len = 0,
- count = 0;
- const read = function () {
- return fullReader.read().then(function (result) {
- if (result.done) {
- return undefined;
- }
- count++;
- len += result.value.byteLength;
- return read();
- });
- };
- await Promise.all([read(), promise]);
- expect(len).toEqual(pdf1Length);
- expect(count).toEqual(1);
- expect(isStreamingSupported).toEqual(false);
- expect(isRangeSupported).toEqual(false);
- });
- it("read custom ranges", async function () {
-
-
- const rangeSize = 32768;
- const stream = new PDFNetworkStream({
- url: pdf1,
- length: pdf1Length,
- rangeChunkSize: rangeSize,
- disableStream: true,
- disableRange: false,
- });
- const fullReader = stream.getFullReader();
- let isStreamingSupported, isRangeSupported, fullReaderCancelled;
- const promise = fullReader.headersReady.then(function () {
- isStreamingSupported = fullReader.isStreamingSupported;
- isRangeSupported = fullReader.isRangeSupported;
-
- fullReader.cancel(new AbortException("Don't need fullReader."));
- fullReaderCancelled = true;
- });
-
- const tailSize = pdf1Length % rangeSize || rangeSize;
- const range1Reader = stream.getRangeReader(
- pdf1Length - tailSize - rangeSize,
- pdf1Length - tailSize
- );
- const range2Reader = stream.getRangeReader(
- pdf1Length - tailSize,
- pdf1Length
- );
- const result1 = { value: 0 },
- result2 = { value: 0 };
- const read = function (reader, lenResult) {
- return reader.read().then(function (result) {
- if (result.done) {
- return undefined;
- }
- lenResult.value += result.value.byteLength;
- return read(reader, lenResult);
- });
- };
- await Promise.all([
- read(range1Reader, result1),
- read(range2Reader, result2),
- promise,
- ]);
- expect(result1.value).toEqual(rangeSize);
- expect(result2.value).toEqual(tailSize);
- expect(isStreamingSupported).toEqual(false);
- expect(isRangeSupported).toEqual(true);
- expect(fullReaderCancelled).toEqual(true);
- });
- });
|