Skip to content

Commit

Permalink
Merge pull request #18091 from Snuffleupagus/rm-_fetchDocument
Browse files Browse the repository at this point in the history
Inline the `_fetchDocument` helper function in `getDocument`
  • Loading branch information
timvandermeij authored May 14, 2024
2 parents 44b7cc5 + 6b17154 commit 7e5e06b
Showing 1 changed file with 67 additions and 90 deletions.
157 changes: 67 additions & 90 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ function getDocument(src) {
task._worker = worker;
}

const fetchDocParams = {
const docParams = {
docId,
apiVersion:
typeof PDFJSDev !== "undefined" && !PDFJSDev.test("TESTING")
Expand Down Expand Up @@ -398,107 +398,84 @@ function getDocument(src) {
if (task.destroyed) {
throw new Error("Loading aborted");
}
if (worker.destroyed) {
throw new Error("Worker was destroyed");
}

const workerIdPromise = _fetchDocument(worker, fetchDocParams);
const networkStreamPromise = new Promise(function (resolve) {
let networkStream;
if (rangeTransport) {
networkStream = new PDFDataTransportStream(rangeTransport, {
disableRange,
disableStream,
});
} else if (!data) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: createPDFNetworkStream");
}
const createPDFNetworkStream = params => {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC") &&
isNodeJS
) {
const isFetchSupported = function () {
return (
typeof fetch !== "undefined" &&
typeof Response !== "undefined" &&
"body" in Response.prototype
);
};
return isFetchSupported() && isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNodeStream(params);
}
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
const workerIdPromise = worker.messageHandler.sendWithPromise(
"GetDocRequest",
docParams,
data ? [data.buffer] : null
);

networkStream = createPDFNetworkStream({
url,
length,
httpHeaders,
withCredentials,
rangeChunkSize,
disableRange,
disableStream,
});
let networkStream;
if (rangeTransport) {
networkStream = new PDFDataTransportStream(rangeTransport, {
disableRange,
disableStream,
});
} else if (!data) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: createPDFNetworkStream");
}
resolve(networkStream);
});

return Promise.all([workerIdPromise, networkStreamPromise]).then(
function ([workerId, networkStream]) {
if (task.destroyed) {
throw new Error("Loading aborted");
const createPDFNetworkStream = params => {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC") &&
isNodeJS
) {
const isFetchSupported = function () {
return (
typeof fetch !== "undefined" &&
typeof Response !== "undefined" &&
"body" in Response.prototype
);
};
return isFetchSupported() && isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNodeStream(params);
}
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};

const messageHandler = new MessageHandler(
docId,
workerId,
worker.port
);
const transport = new WorkerTransport(
messageHandler,
task,
networkStream,
transportParams,
transportFactory
);
task._transport = transport;
messageHandler.send("Ready", null);
networkStream = createPDFNetworkStream({
url,
length,
httpHeaders,
withCredentials,
rangeChunkSize,
disableRange,
disableStream,
});
}

return workerIdPromise.then(workerId => {
if (task.destroyed) {
throw new Error("Loading aborted");
}
);
if (worker.destroyed) {
throw new Error("Worker was destroyed");
}

const messageHandler = new MessageHandler(docId, workerId, worker.port);
const transport = new WorkerTransport(
messageHandler,
task,
networkStream,
transportParams,
transportFactory
);
task._transport = transport;
messageHandler.send("Ready", null);
});
})
.catch(task._capability.reject);

return task;
}

/**
* Starts fetching of specified PDF document/data.
*
* @param {PDFWorker} worker
* @param {Object} source
* @returns {Promise<string>} A promise that is resolved when the worker ID of
* the `MessageHandler` is known.
* @private
*/
async function _fetchDocument(worker, source) {
if (worker.destroyed) {
throw new Error("Worker was destroyed");
}
const workerId = await worker.messageHandler.sendWithPromise(
"GetDocRequest",
source,
source.data ? [source.data.buffer] : null
);

if (worker.destroyed) {
throw new Error("Worker was destroyed");
}
return workerId;
}

function getUrlProp(val) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
return null; // The 'url' is unused with `PDFDataRangeTransport`.
Expand Down

0 comments on commit 7e5e06b

Please sign in to comment.