Repository: mozilla/pdf.js
File: test/unit/common_pdfstream_tests.js (JavaScript)

Code before:
// - PDFNetworkStream by network_spec.js
// - PDFFetchStream by fetch_stream_spec.js
async function testCrossOriginRedirects({
  PDFStreamClass,
  redirectIfRange,
  testRangeReader,
}) {
  const basicApiUrl = TestPdfsServer.resolveURL("basicapi.pdf").href;
  const basicApiFileLength = 105779;

  const rangeSize = 32768;
  const stream = new PDFStreamClass({
    url: getCrossOriginUrlWithRedirects(basicApiUrl, redirectIfRange),
    length: basicApiFileLength,
    rangeChunkSize: rangeSize,
    disableStream: true,
    disableRange: false,
  });

  const fullReader = stream.getFullReader();

  await fullReader.headersReady;
  // Sanity check: We can only test range requests if supported:
  expect(fullReader.isRangeSupported).toEqual(true);
  // ^ When range requests are supported, the initial full request is aborted,
  // and we do not need to call fullReader.cancel().

  const rangeReader = stream.getRangeReader(
    basicApiFileLength - rangeSize,
    basicApiFileLength
  );

  try {
    await testRangeReader(rangeReader);
  } finally {
    rangeReader.cancel(new AbortException("Don't need rangeReader"));
  }
}

/**
 * @param {string} testserverUrl - A URL handled that supports CORS and
 *   redirects (see crossOriginHandler and redirectHandler in webserver.mjs).
 * @param {boolean} redirectIfRange - Whether Range requests should be
 *   redirected to a different origin compared to the initial request.
 * @returns {string} A URL that will be redirected by the server.
 */
function getCrossOriginUrlWithRedirects(testserverUrl, redirectIfRange) {
  const url = new URL(testserverUrl);
  if (!isNodeJS) {
    // The responses are going to be cross-origin. In Node.js, fetch() allows
    // cross-origin requests for any request, but in browser environments we

Review comment (suggestion):
```suggestion
  // ^ When range requests are supported *and* streaming is disabled,
  // the initial full request is aborted and we do not need to call fullReader.cancel().
```

Code after:
// - PDFNetworkStream by network_spec.js
// - PDFFetchStream by fetch_stream_spec.js
async function testCrossOriginRedirects({
  PDFStreamClass,
  redirectIfRange,
  testRangeReader,
}) {
  const basicApiUrl = TestPdfsServer.resolveURL("basicapi.pdf").href;
  const basicApiFileLength = 105779;

  const rangeSize = 32768;
  const stream = new PDFStreamClass({
    url: getCrossOriginUrlWithRedirects(basicApiUrl, redirectIfRange),
    length: basicApiFileLength,
    rangeChunkSize: rangeSize,
    disableStream: true,
    disableRange: false,
  });

  const fullReader = stream.getFullReader();

  await fullReader.headersReady;
  // Sanity check: We can only test range requests if supported:
  expect(fullReader.isRangeSupported).toEqual(true);
  // ^ When range requests are supported (and streaming is disabled), the full
  // initial request is aborted and we do not need to call fullReader.cancel().

  const rangeReader = stream.getRangeReader(
    basicApiFileLength - rangeSize,
    basicApiFileLength
  );

  try {
    await testRangeReader(rangeReader);
  } finally {
    rangeReader.cancel(new AbortException("Don't need rangeReader"));
  }
}

/**
 * @param {string} testserverUrl - A URL handled that supports CORS and
 *   redirects (see crossOriginHandler and redirectHandler in webserver.mjs).
 * @param {boolean} redirectIfRange - Whether Range requests should be
 *   redirected to a different origin compared to the initial request.
 * @returns {string} A URL that will be redirected by the server.
 */
function getCrossOriginUrlWithRedirects(testserverUrl, redirectIfRange) {
  const url = new URL(testserverUrl);
  if (!isNodeJS) {
    // The responses are going to be cross-origin. In Node.js, fetch() allows
    // cross-origin requests for any request, but in browser environments we