libkernel
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,2 +0,2 @@ | ||
export declare function init(): void; | ||
export declare function init(): Promise<void>; | ||
export declare function testMessage(): Promise<unknown>; |
@@ -0,5 +1,48 @@ | ||
var bridgeExists; | ||
var bridgeAvailable; | ||
var bridgePromise = new Promise((resolve, reject) => { | ||
bridgeAvailable = { resolve, reject }; | ||
}); | ||
function blockUntilBridgeLoaded() { | ||
return bridgePromise; | ||
} | ||
// Establish a system for matching messages to the kernel with responses from | ||
// the kernel. | ||
// the kernel. The nonce is incremented every time a new message is sent, and | ||
// the queries object is used as a hashmap that maps a given message nonce to | ||
// the {resolve, reject} of a promise that will be resolved/rejected when a | ||
// response to the corresponding message is provided. | ||
var nextNonce = 1; | ||
var queries = new Object(); | ||
// handleBridgeTest will handle a response from the bridge indicating that the | ||
// bridge is working. | ||
function handleBridgeTest() { | ||
if (bridgeExists !== false) { | ||
bridgeExists = true; | ||
bridgeAvailable.resolve(); | ||
} | ||
else { | ||
console.log("[libkernel] ERROR: received late signal from bridge"); | ||
} | ||
} | ||
// handleKernelResponse will parse the kernel's response from the bridge and | ||
// resolve/reject the promise associated with the nonce. | ||
function handleKernelResponse(event) { | ||
// Check that the response includes a resp and an err. | ||
let result = queries[event.data.nonce]; | ||
delete queries[event.data.nonce]; | ||
if (!("resp" in event.data) || !("err" in event.data)) { | ||
console.log("[libkernel] missing resp or err\n", event); | ||
return; | ||
} | ||
// Either resolve or reject the promise associated with this response. | ||
if (event.data.resp !== null) { | ||
result.resolve(event.data.resp); | ||
} | ||
else if (event.data.err !== null) { | ||
result.reject(event.data.err); | ||
} | ||
else { | ||
console.log("[libkernel] received malformed response from bridge\n", event); | ||
} | ||
} | ||
// handleMessage will handle a message from the kernel, using the reponse to | ||
@@ -13,6 +56,8 @@ // resolve the appropriate promise in the set of queries. | ||
// Check that this message is a kernelResponse. | ||
if (!("data" in event) || !("method" in event.data)) { | ||
if (!("data" in event) || !("method" in event.data) || !("nonce" in event.data)) { | ||
return; | ||
} | ||
if (event.data.method !== "kernelResponse") { | ||
// Special case: bridgeTest doesn't need a nonce in 'queries'. | ||
if (event.data.method === "bridgeTest") { | ||
handleBridgeTest(); | ||
return; | ||
@@ -22,36 +67,17 @@ } | ||
if (!(event.data.nonce in queries)) { | ||
console.log("received message with missing nonce\n", event, "\n", queries); | ||
console.log("[libkernel] missing nonce\n", event, "\n", queries); | ||
return; | ||
} | ||
// Either resolve or reject the promise associated with this response. | ||
if (event.data.resp !== null) { | ||
console.log("trying to do a resolve"); | ||
let resolve = queries[event.data.nonce].resolve; | ||
resolve(event.data.resp); | ||
if (event.data.method === "kernelResponse") { | ||
handleKernelResponse(event); | ||
return; | ||
} | ||
else if (event.data.resp !== null) { | ||
console.log("trying to do a reject"); | ||
let reject = queries[event.data.nonce].reject; | ||
reject(event.data.err); | ||
} | ||
else { | ||
console.log("received malformed response from bridge\n", event.data); | ||
} | ||
delete queries[event.data.nonce]; | ||
console.log("got message from bridge\n", event, "\n", event.data); | ||
console.log("[libkernel] received unrecognized method from bridge\n", event); | ||
} | ||
// init will add an event listener for messages from the kernel bridge. | ||
// | ||
// TODO: Init should check whether the bridge exists and throw an error if it | ||
// does not. Ideally init can tell if there's a browser extension. | ||
export function init() { | ||
window.addEventListener("message", handleMessage); | ||
} | ||
// kernelRequestTest will send a message to the bridge asking for a kernel | ||
// test. | ||
// | ||
// TODO: Function should know if the bridge exists and if the init worked. | ||
// | ||
// TODO: Need to block until kenrel is loaded. | ||
export function testMessage() { | ||
// Return a promise that will resolve when the bridge responds that it | ||
// is alive. A timeout is used to reject if the bridge does not respond | ||
// within 100ms. | ||
return new Promise((resolve, reject) => { | ||
@@ -62,9 +88,36 @@ let nonce = nextNonce; | ||
window.postMessage({ | ||
method: "kernelMessage", | ||
method: "bridgeTest", | ||
nonce, | ||
kernelMessage: { | ||
kernelMethod: "requestTest", | ||
}, | ||
}, window.location.origin); | ||
}); | ||
// After 100ms, check whether the bridge has responded. If not, | ||
// fail the bridge. | ||
setTimeout(function () { | ||
if (bridgeExists !== true) { | ||
bridgeExists = false; | ||
bridgeAvailable.reject(); | ||
} | ||
}, 100); | ||
}); | ||
} | ||
// kernelRequestTest will send a message to the bridge asking for a kernel | ||
// test. | ||
export function testMessage() { | ||
return new Promise((resolve, reject) => { | ||
blockUntilBridgeLoaded() | ||
.then(x => { | ||
let nonce = nextNonce; | ||
nextNonce++; | ||
queries[nonce] = { resolve, reject }; | ||
window.postMessage({ | ||
method: "kernelMessage", | ||
nonce, | ||
kernelMessage: { | ||
kernelMethod: "requestTest", | ||
}, | ||
}, window.location.origin); | ||
}) | ||
.catch(x => { | ||
reject(x); | ||
}); | ||
}); | ||
} |
{ | ||
"name": "libkernel", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "helper library to interact with the skynet kernel", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
4948
134