Remove unused parameters from worker message-handler functions

A lot of these (often old) handlers don't need any parameters, hence their `data` parameters needlessly increase code-size.

Also, for the functions that do need parameters, use parameter destructuring consistently throughout the message-handler functions.
This commit is contained in:
Jonas Jenwald 2026-07-07 12:28:47 +02:00
parent d29293622e
commit f0b5fc3b73

View File

@ -391,8 +391,8 @@ class WorkerMessageHandler {
.then(pdfManagerReady, onFailure);
}
handler.on("GetPage", function (data) {
return pdfManager.getPage(data.pageIndex).then(function (page) {
handler.on("GetPage", function ({ pageIndex }) {
return pdfManager.getPage(pageIndex).then(function (page) {
return Promise.all([
pdfManager.ensure(page, "rotate"),
pdfManager.ensure(page, "ref"),
@ -410,73 +410,65 @@ class WorkerMessageHandler {
});
});
handler.on("GetPageIndex", function (data) {
const pageRef = Ref.get(data.num, data.gen);
return pdfManager.ensureCatalog("getPageIndex", [pageRef]);
handler.on("GetPageIndex", function ({ num, gen }) {
return pdfManager.ensureCatalog("getPageIndex", [Ref.get(num, gen)]);
});
handler.on("GetDestinations", function (data) {
handler.on("GetDestinations", function () {
return pdfManager.ensureCatalog("destinations");
});
handler.on("GetDestination", function (data) {
return pdfManager.ensureCatalog("getDestination", [data.id]);
handler.on("GetDestination", function ({ id }) {
return pdfManager.ensureCatalog("getDestination", [id]);
});
handler.on("GetPageLabels", function (data) {
handler.on("GetPageLabels", function () {
return pdfManager.ensureCatalog("pageLabels");
});
handler.on("GetPageLayout", function (data) {
handler.on("GetPageLayout", function () {
return pdfManager.ensureCatalog("pageLayout");
});
handler.on("GetPageMode", function (data) {
handler.on("GetPageMode", function () {
return pdfManager.ensureCatalog("pageMode");
});
handler.on("GetViewerPreferences", function (data) {
handler.on("GetViewerPreferences", function () {
return pdfManager.ensureCatalog("viewerPreferences");
});
handler.on("GetOpenAction", function (data) {
handler.on("GetOpenAction", function () {
return pdfManager.ensureCatalog("openAction");
});
handler.on("GetAttachments", function (data) {
handler.on("GetAttachments", function () {
return pdfManager.ensureCatalog("attachments");
});
handler.on(
"GetAttachmentContent",
/**
* @param {string} id
* Unique attachment identifier (required).
*/
async function (id) {
let passwordEx;
handler.on("GetAttachmentContent", async function (id) {
let passwordEx;
// Loop to prompt again after an incorrect password.
while (true) {
const password = passwordEx ? await getPassword(passwordEx) : null;
// Loop to prompt again after an incorrect password.
while (true) {
const password = passwordEx ? await getPassword(passwordEx) : null;
try {
if (password) {
pdfManager.updatePassword(password);
}
return await pdfManager.ensureCatalog("attachmentContent", [id]);
} catch (ex) {
if (ex instanceof PasswordException) {
passwordEx = ex;
continue;
}
throw ex;
try {
if (password) {
pdfManager.updatePassword(password);
}
return await pdfManager.ensureCatalog("attachmentContent", [id]);
} catch (ex) {
if (ex instanceof PasswordException) {
passwordEx = ex;
continue;
}
throw ex;
}
}
);
});
handler.on("GetDocJSActions", function (data) {
handler.on("GetDocJSActions", function () {
return pdfManager.ensureCatalog("jsActions");
});
@ -534,19 +526,19 @@ class WorkerMessageHandler {
}
);
handler.on("GetOutline", function (data) {
handler.on("GetOutline", function () {
return pdfManager.ensureCatalog("documentOutline");
});
handler.on("GetOptionalContentConfig", function (data) {
handler.on("GetOptionalContentConfig", function () {
return pdfManager.ensureCatalog("optionalContentConfig");
});
handler.on("GetPermissions", function (data) {
handler.on("GetPermissions", function () {
return pdfManager.ensureCatalog("permissions");
});
handler.on("GetMetadata", function (data) {
handler.on("GetMetadata", function () {
return Promise.all([
pdfManager.ensureDoc("documentInfo"),
pdfManager.ensureCatalog("metadata"),
@ -554,11 +546,11 @@ class WorkerMessageHandler {
]);
});
handler.on("GetMarkInfo", function (data) {
handler.on("GetMarkInfo", function () {
return pdfManager.ensureCatalog("markInfo");
});
handler.on("GetData", function (data) {
handler.on("GetData", function () {
return pdfManager.requestLoadedStream().then(stream => stream.bytes);
});
@ -573,13 +565,13 @@ class WorkerMessageHandler {
});
});
handler.on("GetFieldObjects", function (data) {
handler.on("GetFieldObjects", function () {
return pdfManager
.ensureDoc("fieldObjects")
.then(fieldObjects => fieldObjects?.allFields || null);
});
handler.on("GetSignatures", function (data) {
handler.on("GetSignatures", function () {
return pdfManager.ensureDoc("signatures");
});
@ -587,11 +579,11 @@ class WorkerMessageHandler {
return pdfManager.ensureDoc("getSignatureData", [id]);
});
handler.on("HasJSActions", function (data) {
handler.on("HasJSActions", function () {
return pdfManager.ensureDoc("hasJSActions");
});
handler.on("GetCalculationOrderIds", function (data) {
handler.on("GetCalculationOrderIds", function () {
return pdfManager.ensureDoc("calculationOrderIds");
});
@ -893,96 +885,104 @@ class WorkerMessageHandler {
}
);
handler.on("GetOperatorList", function (data, sink) {
const { pageId, pageIndex } = data;
pdfManager.getPage(pageId).then(function (page) {
const task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
startWorkerTask(task);
handler.on(
"GetOperatorList",
function (
{ pageId, pageIndex, intent, cacheKey, annotationStorage, modifiedIds },
sink
) {
pdfManager.getPage(pageId).then(function (page) {
const task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function.
const start = verbosity >= VerbosityLevel.INFOS ? Date.now() : 0;
// NOTE: Keep this condition in sync with the `info` helper function.
const start = verbosity >= VerbosityLevel.INFOS ? Date.now() : 0;
// Pre compile the pdf page and fetch the fonts/images.
page
.getOperatorList({
handler,
sink,
task,
intent: data.intent,
cacheKey: data.cacheKey,
annotationStorage: data.annotationStorage,
modifiedIds: data.modifiedIds,
pageIndex,
})
.then(
opListInfo => {
if (start) {
info(
`${task.name}; time=${Date.now() - start}ms, len=${opListInfo.length}`
);
// Pre compile the pdf page and fetch the fonts/images.
page
.getOperatorList({
handler,
sink,
task,
intent,
cacheKey,
annotationStorage,
modifiedIds,
pageIndex,
})
.then(
opListInfo => {
if (start) {
info(
`${task.name}; time=${Date.now() - start}ms, len=${opListInfo.length}`
);
}
sink.close();
},
reason => {
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
sink.error(reason);
}
sink.close();
},
reason => {
if (task.terminated) {
return; // ignoring errors from the terminated thread
)
.finally(() => {
finishWorkerTask(task);
});
});
}
);
handler.on(
"GetTextContent",
function (
{ pageId, pageIndex, includeMarkedContent, disableNormalization },
sink
) {
pdfManager.getPage(pageId).then(function (page) {
const task = new WorkerTask("GetTextContent: page " + pageIndex);
startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function.
const start = verbosity >= VerbosityLevel.INFOS ? Date.now() : 0;
page
.extractTextContent({
handler,
task,
sink,
includeMarkedContent,
disableNormalization,
})
.then(
() => {
if (start) {
info(`${task.name}; time=${Date.now() - start}ms`);
}
sink.close();
},
reason => {
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
sink.error(reason);
}
sink.error(reason);
}
)
.finally(() => {
finishWorkerTask(task);
});
});
});
)
.finally(() => {
finishWorkerTask(task);
});
});
}
);
handler.on("GetTextContent", function (data, sink) {
const { pageId, pageIndex, includeMarkedContent, disableNormalization } =
data;
pdfManager.getPage(pageId).then(function (page) {
const task = new WorkerTask("GetTextContent: page " + pageIndex);
startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function.
const start = verbosity >= VerbosityLevel.INFOS ? Date.now() : 0;
page
.extractTextContent({
handler,
task,
sink,
includeMarkedContent,
disableNormalization,
})
.then(
() => {
if (start) {
info(`${task.name}; time=${Date.now() - start}ms`);
}
sink.close();
},
reason => {
if (task.terminated) {
return; // ignoring errors from the terminated thread
}
sink.error(reason);
}
)
.finally(() => {
finishWorkerTask(task);
});
});
});
handler.on("GetStructTree", function (data) {
handler.on("GetStructTree", function ({ pageIndex }) {
return pdfManager
.getPage(data.pageIndex)
.getPage(pageIndex)
.then(page => pdfManager.ensure(page, "getStructTree"));
});
handler.on("FontFallback", function (data) {
return pdfManager.fontFallback(data.id, handler);
handler.on("FontFallback", function ({ id }) {
return pdfManager.fontFallback(id, handler);
});
if (
@ -1005,11 +1005,11 @@ class WorkerMessageHandler {
});
}
handler.on("Cleanup", function (data) {
handler.on("Cleanup", function () {
return pdfManager.cleanup(/* manuallyTriggered = */ true);
});
handler.on("Terminate", async function (data) {
handler.on("Terminate", async function () {
terminated = true;
const waitOn = [];
@ -1037,21 +1037,21 @@ class WorkerMessageHandler {
handler = null;
});
handler.on("Ready", function (data) {
handler.on("Ready", function () {
setupDoc(docParams);
docParams = null; // we don't need docParams anymore -- saving memory.
});
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
handler.on("GetXFADatasets", function (data) {
handler.on("GetXFADatasets", function () {
return pdfManager.ensureDoc("xfaDatasets");
});
handler.on("GetStartXRefPos", function (data) {
handler.on("GetStartXRefPos", function () {
return pdfManager.ensureDoc("startXRef");
});
handler.on("GetAnnotArray", function (data) {
handler.on("GetAnnotArray", function ({ pageIndex }) {
return pdfManager
.getPage(data.pageIndex)
.getPage(pageIndex)
.then(page => page.annotations.map(a => a.toString()));
});
handler.on("GetWorkerCoverage", function () {