-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
8,286 additions
and
9 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// launcher/client-proxy.ts | ||
function clientFactory(workerFactory, comlinkWrapper) { | ||
return { | ||
async create(config) { | ||
const worker = workerFactory(); | ||
await new Promise((resolve) => { | ||
const readyListener = (event) => { | ||
removeEventListener(worker, "message", readyListener); | ||
if (getEventData(event) === "NIMIQ_ONLOAD") resolve(); | ||
}; | ||
addEventListener(worker, "message", readyListener); | ||
}); | ||
console.debug("Client WASM worker loaded"); | ||
const client = comlinkWrapper(worker); | ||
if (typeof window !== "undefined") { | ||
window.addEventListener("offline", () => worker.postMessage("offline")); | ||
window.addEventListener("online", () => worker.postMessage("online")); | ||
} | ||
if (typeof document !== "undefined") { | ||
document.addEventListener("visibilitychange", () => { | ||
if (document.visibilityState === "visible") { | ||
worker.postMessage("visible"); | ||
} | ||
}); | ||
} | ||
console.debug("Sending NIMIQ_INIT message to client worker"); | ||
worker.postMessage({ | ||
type: "NIMIQ_INIT", | ||
config | ||
}); | ||
await new Promise((resolve, reject) => { | ||
addEventListener(worker, "message", (event) => { | ||
const eventData = getEventData(event); | ||
if (!("ok" in eventData)) return; | ||
if (eventData.ok === true) resolve(); | ||
if (eventData.ok === false && "error" in eventData && typeof eventData.error === "string") { | ||
const error = new Error(eventData.error); | ||
if ("stack" in eventData && typeof eventData.stack === "string") { | ||
error.stack = eventData.stack; | ||
} | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
console.debug("Have client worker remote"); | ||
return client; | ||
} | ||
}; | ||
} | ||
function addEventListener(worker, type, listener) { | ||
const method = "addListener" in worker ? "addListener" : "addEventListener"; | ||
worker[method](type, listener); | ||
} | ||
function removeEventListener(worker, type, listener) { | ||
const method = "removeListener" in worker ? "removeListener" : "removeEventListener"; | ||
worker[method](type, listener); | ||
} | ||
function getEventData(event) { | ||
return typeof event === "object" && "data" in event ? event.data : event; | ||
} | ||
export { | ||
clientFactory | ||
}; |
38 changes: 38 additions & 0 deletions
38
public/albatross/launcher/browser/cryptoutils-worker-proxy.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// launcher/cryptoutils-worker-proxy.ts | ||
var remote; | ||
function cryptoUtilsWorkerFactory(workerFactory, comlinkWrapper) { | ||
const proxy = {}; | ||
["otpKdf"].forEach((method) => { | ||
proxy[method] = async function() { | ||
remote = remote || await startWorker(workerFactory, comlinkWrapper); | ||
return remote[method](...arguments); | ||
}; | ||
}); | ||
return proxy; | ||
} | ||
async function startWorker(workerFactory, comlinkWrapper) { | ||
const worker = workerFactory(); | ||
await new Promise((resolve) => { | ||
const readyListener = (event) => { | ||
removeEventListener(worker, "message", readyListener); | ||
if (getEventData(event) === "NIMIQ_ONLOAD") resolve(); | ||
}; | ||
addEventListener(worker, "message", readyListener); | ||
}); | ||
console.debug("Have crypto worker remote"); | ||
return comlinkWrapper(worker); | ||
} | ||
function addEventListener(worker, type, listener) { | ||
const method = "addListener" in worker ? "addListener" : "addEventListener"; | ||
worker[method](type, listener); | ||
} | ||
function removeEventListener(worker, type, listener) { | ||
const method = "removeListener" in worker ? "removeListener" : "removeEventListener"; | ||
worker[method](type, listener); | ||
} | ||
function getEventData(event) { | ||
return typeof event === "object" && "data" in event ? event.data : event; | ||
} | ||
export { | ||
cryptoUtilsWorkerFactory | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// launcher/transfer-handlers.ts | ||
function setupMainThreadTransferHandlers(Comlink, classes) { | ||
Comlink.transferHandlers.set("function", { | ||
canHandle: (obj) => typeof obj === "function", | ||
serialize(obj) { | ||
return Comlink.transferHandlers.get("proxy").serialize(obj); | ||
} | ||
// deserialize(plain) {}, // Cannot receive functions from worker | ||
}); | ||
function canBeSerialized(obj) { | ||
return obj instanceof classes.Address || obj instanceof classes.Transaction; | ||
} | ||
Comlink.transferHandlers.set("plain", { | ||
canHandle: (obj) => canBeSerialized(obj) || Array.isArray(obj) && obj.some((item) => canBeSerialized(item)), | ||
serialize(obj) { | ||
if (Array.isArray(obj)) { | ||
return [obj.map((item) => canBeSerialized(item) ? item.serialize() : item), []]; | ||
} else { | ||
return [obj.serialize(), []]; | ||
} | ||
} | ||
// deserialize(plain) {}, // Cannot receive class instances from worker | ||
}); | ||
} | ||
export { | ||
setupMainThreadTransferHandlers | ||
}; |
Oops, something went wrong.