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

Code before:
  };
  const stream = new StringStream("Dummy_PDF_data");
  const pdfDocument = new PDFDocument(pdfManager, stream);

  const page = new Page({
    pdfManager: pdfDocument.pdfManager,
    xref: pdfDocument.xref,
    pageIndex,
    globalIdFactory: pdfDocument._globalIdFactory,
  });
  return page._localIdFactory;
}

// Some tests rely on special behavior from webserver.mjs. When loaded in the
// browser, the page is already served from WebServer. When running from
// Node.js, that is not the case. This helper starts the WebServer if needed,
// and offers a mechanism to resolve the URL in a uniform way.
class TestPdfsServer {
  static #webServer;

  static #startCount = 0;

  static async ensureStarted() {
    if (this.#startCount++) {
      // Already started before. E.g. from another beforeAll call.
      return;
    }
    if (!isNodeJS) {
      // In web browsers, tests are presumably served by webserver.mjs.
      return;
    }

    // WebServer from webserver.mjs is imported dynamically instead of
    // statically because we do not need it when running from the browser.
    let WebServer;
    if (import.meta.url.endsWith("/lib-legacy/test/unit/test_utils.js")) {
      // When "gulp unittestcli" is used to run tests, the tests are run from
      // pdf.js/build/lib-legacy/test/ instead of directly from pdf.js/test/.
      // eslint-disable-next-line import/no-unresolved
      ({ WebServer } = await import("../../../../test/webserver.mjs"));
    } else {
      ({ WebServer } = await import("../webserver.mjs"));
    }
    this.#webServer = new WebServer({
      host: "127.0.0.1",
      root: TEST_PDFS_PATH,
    });
    await new Promise(resolve => {
      this.#webServer.start(resolve);
    });
  }

Review comment (suggestion):
If there's two back-to-back calls to `ensureStarted` then the second one may resolve *before* the server is actually available, which seems like it could cause trouble, since loading/initializing is async below.

Code after:
    if (obj instanceof Ref) {
      return this.fetch(obj);
    }
    return obj;
  }

  async fetchIfRefAsync(obj) {
    return this.fetchIfRef(obj);
  }
}

function createIdFactory(pageIndex) {
  const pdfManager = {
    get docId() {
      return "d0";
    },
  };
  const stream = new StringStream("Dummy_PDF_data");
  const pdfDocument = new PDFDocument(pdfManager, stream);

  const page = new Page({
    pdfManager: pdfDocument.pdfManager,
    xref: pdfDocument.xref,
    pageIndex,
    globalIdFactory: pdfDocument._globalIdFactory,
  });
  return page._localIdFactory;
}

// Some tests rely on special behavior from webserver.mjs. When loaded in the
// browser, the page is already served from WebServer. When running from
// Node.js, that is not the case. This helper starts the WebServer if needed,
// and offers a mechanism to resolve the URL in a uniform way.
class TestPdfsServer {
  static #webServer;

  static #startCount = 0;

  static #startPromise;

  static async ensureStarted() {
    if (this.#startCount++) {
      // Already started before. E.g. from another beforeAll call.
      return this.#startPromise;
    }
    if (!isNodeJS) {
      // In web browsers, tests are presumably served by webserver.mjs.
      return undefined;
    }

    this.#startPromise = this.#startServer().finally(() => {