refactor: add support for internal messages
This commit is contained in:
parent
867544e1c9
commit
af263bbc39
|
@ -1,6 +1,8 @@
|
||||||
// notableErrors is a persistent list of errors that should be checked after
|
// notableErrors is a persistent list of errors that should be checked after
|
||||||
// testing. You should only add to this array in the event of an error that
|
// testing. You should only add to this array in the event of an error that
|
||||||
// indicates a bug with the kernel.
|
// indicates a bug with the kernel.
|
||||||
|
import { OpenQueryResponse } from "./queries.js";
|
||||||
|
|
||||||
const notableErrors: string[] = [];
|
const notableErrors: string[] = [];
|
||||||
|
|
||||||
// respondErr will send an error response to the caller that closes out the
|
// respondErr will send an error response to the caller that closes out the
|
||||||
|
@ -13,6 +15,7 @@ function respondErr(
|
||||||
event: MessageEvent,
|
event: MessageEvent,
|
||||||
messagePortal: any,
|
messagePortal: any,
|
||||||
isWorker: boolean,
|
isWorker: boolean,
|
||||||
|
isInternal: false | ((message: OpenQueryResponse) => void),
|
||||||
err: string,
|
err: string,
|
||||||
) {
|
) {
|
||||||
const message = {
|
const message = {
|
||||||
|
@ -20,9 +23,11 @@ function respondErr(
|
||||||
method: "response",
|
method: "response",
|
||||||
data: {},
|
data: {},
|
||||||
err,
|
err,
|
||||||
};
|
} as OpenQueryResponse;
|
||||||
if (isWorker === true) {
|
if (isWorker) {
|
||||||
messagePortal.postMessage(message);
|
messagePortal.postMessage(message);
|
||||||
|
} else if (isInternal) {
|
||||||
|
isInternal(message);
|
||||||
} else {
|
} else {
|
||||||
messagePortal.postMessage(message, event.origin);
|
messagePortal.postMessage(message, event.origin);
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,6 +83,7 @@ export const handleIncomingMessage = function (event: any) {
|
||||||
event,
|
event,
|
||||||
event.source,
|
event.source,
|
||||||
false,
|
false,
|
||||||
|
false,
|
||||||
"caller is an extension, but not domain was provided",
|
"caller is an extension, but not domain was provided",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -106,6 +107,7 @@ export const handleIncomingMessage = function (event: any) {
|
||||||
event,
|
event,
|
||||||
event.source,
|
event.source,
|
||||||
false,
|
false,
|
||||||
|
false,
|
||||||
"unrecognized method: " + event.data.method,
|
"unrecognized method: " + event.data.method,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -53,7 +53,7 @@ interface OpenQuery {
|
||||||
origin: string;
|
origin: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OpenQueryResponse {
|
export interface OpenQueryResponse {
|
||||||
nonce: string;
|
nonce: string;
|
||||||
method: string;
|
method: string;
|
||||||
data: any;
|
data: any;
|
||||||
|
@ -96,7 +96,7 @@ function handleWorkerMessage(event: MessageEvent, mod: Module, worker: Worker) {
|
||||||
// Perform input verification for a worker message.
|
// Perform input verification for a worker message.
|
||||||
if (!("method" in event.data)) {
|
if (!("method" in event.data)) {
|
||||||
logErr("worker", mod.domain, "received worker message with no method");
|
logErr("worker", mod.domain, "received worker message with no method");
|
||||||
respondErr(event, worker, true, "received message with no method");
|
respondErr(event, worker, true, false, "received message with no method");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,6 +113,7 @@ function handleWorkerMessage(event: MessageEvent, mod: Module, worker: Worker) {
|
||||||
event,
|
event,
|
||||||
worker,
|
worker,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
"received log messsage with no data field",
|
"received log messsage with no data field",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -127,6 +128,7 @@ function handleWorkerMessage(event: MessageEvent, mod: Module, worker: Worker) {
|
||||||
event,
|
event,
|
||||||
worker,
|
worker,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
"received log messsage with no message field",
|
"received log messsage with no message field",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -144,6 +146,7 @@ function handleWorkerMessage(event: MessageEvent, mod: Module, worker: Worker) {
|
||||||
event,
|
event,
|
||||||
worker,
|
worker,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
"received log messsage with invalid isErr field",
|
"received log messsage with invalid isErr field",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -169,7 +172,7 @@ function handleWorkerMessage(event: MessageEvent, mod: Module, worker: Worker) {
|
||||||
"worker sent a message with no nonce",
|
"worker sent a message with no nonce",
|
||||||
event.data,
|
event.data,
|
||||||
);
|
);
|
||||||
respondErr(event, worker, true, "received message with no nonce");
|
respondErr(event, worker, true, false, "received message with no nonce");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,6 +322,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"moduleCall is missing 'module' field: " + JSON.stringify(event.data),
|
"moduleCall is missing 'module' field: " + JSON.stringify(event.data),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -349,6 +353,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"'module' field in moduleCall is expected to be a raw CID or a resolver CID",
|
"'module' field in moduleCall is expected to be a raw CID or a resolver CID",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -362,6 +367,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"no 'data.method' specified, module does not know what method to run",
|
"no 'data.method' specified, module does not know what method to run",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -376,6 +382,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"'data.method' needs to be a string",
|
"'data.method' needs to be a string",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -389,6 +396,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"presentSeed is a privileged method, only root is allowed to use it",
|
"presentSeed is a privileged method, only root is allowed to use it",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -399,6 +407,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"no field data.data in moduleCall, data.data contains the module input",
|
"no field data.data in moduleCall, data.data contains the module input",
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -414,6 +423,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
"registry entry for module is not found",
|
"registry entry for module is not found",
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -511,6 +521,7 @@ async function handleModuleCall(
|
||||||
event,
|
event,
|
||||||
messagePortal,
|
messagePortal,
|
||||||
isWorker,
|
isWorker,
|
||||||
|
isInternal,
|
||||||
addContextToErr(errML, "module could not be loaded"),
|
addContextToErr(errML, "module could not be loaded"),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -530,7 +541,7 @@ async function handleModuleCall(
|
||||||
const [moduleData, errDS] = await downloadSmallObject(finalModule);
|
const [moduleData, errDS] = await downloadSmallObject(finalModule);
|
||||||
if (errDS !== null) {
|
if (errDS !== null) {
|
||||||
const err = addContextToErr(errDS, "unable to load module");
|
const err = addContextToErr(errDS, "unable to load module");
|
||||||
respondErr(event, messagePortal, isWorker, err);
|
respondErr(event, messagePortal, isWorker, isInternal, err);
|
||||||
resolve(err);
|
resolve(err);
|
||||||
delete modulesLoading[moduleDomain];
|
delete modulesLoading[moduleDomain];
|
||||||
return;
|
return;
|
||||||
|
@ -563,7 +574,7 @@ async function handleModuleCall(
|
||||||
const [mod, errCM] = await createModule(moduleData, moduleDomain);
|
const [mod, errCM] = await createModule(moduleData, moduleDomain);
|
||||||
if (errCM !== null) {
|
if (errCM !== null) {
|
||||||
const err = addContextToErr(errCM, "unable to create module");
|
const err = addContextToErr(errCM, "unable to create module");
|
||||||
respondErr(event, messagePortal, isWorker, err);
|
respondErr(event, messagePortal, isWorker, isInternal, err);
|
||||||
resolve(err);
|
resolve(err);
|
||||||
delete modulesLoading[moduleDomain];
|
delete modulesLoading[moduleDomain];
|
||||||
return;
|
return;
|
||||||
|
@ -605,7 +616,7 @@ function handleModuleResponse(
|
||||||
if (!(event.data.nonce in queries)) {
|
if (!(event.data.nonce in queries)) {
|
||||||
// If there's no corresponding query and this is a response, send an
|
// If there's no corresponding query and this is a response, send an
|
||||||
// error.
|
// error.
|
||||||
if (isResponse === true) {
|
if (isResponse) {
|
||||||
logErr("worker", mod.domain, "received response for an unknown nonce");
|
logErr("worker", mod.domain, "received response for an unknown nonce");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue