fix: add resolvedDomain to Module type and use it instead in createModule so that the domain id stays consistent to the potential resolver version, when used as a callerDomain

This commit is contained in:
Derrick Hammer 2023-10-14 23:50:47 -04:00
parent 528c613858
commit 68642ccb03
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 9 additions and 2 deletions

View File

@ -37,6 +37,7 @@ type WorkerLaunchFn = () => Promise<[Worker, Err]>;
// a new worker gets launched for every query. // a new worker gets launched for every query.
interface Module { interface Module {
domain: string; domain: string;
resolvedDomain: string;
code: Uint8Array; code: Uint8Array;
launchWorker: WorkerLaunchFn; launchWorker: WorkerLaunchFn;
worker?: Worker; worker?: Worker;
@ -223,10 +224,12 @@ function handleWorkerMessage(event: MessageEvent, mod: Module, worker: Worker) {
async function createModule( async function createModule(
workerCode: Uint8Array, workerCode: Uint8Array,
domain: string, domain: string,
resolvedDomain: string,
): Promise<[Module | null, Err]> { ): Promise<[Module | null, Err]> {
// Create the module object. // Create the module object.
const mod: Module = { const mod: Module = {
domain, domain,
resolvedDomain,
code: workerCode, code: workerCode,
launchWorker: function (): Promise<[Worker, Err]> { launchWorker: function (): Promise<[Worker, Err]> {
return launchWorker(mod); return launchWorker(mod);
@ -248,7 +251,7 @@ async function launchWorker(mod: Module): Promise<[Worker, Err]> {
// Create and launch the worker. // Create and launch the worker.
let worker: Worker; let worker: Worker;
try { try {
worker = new Worker(mod.code, CID.decode(mod.domain)); worker = new Worker(mod.code, CID.decode(mod.resolvedDomain));
await worker.ready; await worker.ready;
} catch (err: any) { } catch (err: any) {
logErr("worker", mod.domain, "unable to create worker", mod.domain, err); logErr("worker", mod.domain, "unable to create worker", mod.domain, err);
@ -593,7 +596,11 @@ async function handleModuleCall(
} }
// Create a new module. // Create a new module.
const [mod, errCM] = await createModule(moduleData, finalModule); const [mod, errCM] = await createModule(
moduleData,
moduleDomain,
finalModule,
);
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, isInternal, err); respondErr(event, messagePortal, isWorker, isInternal, err);