libkernel
Advanced tools
Comparing version 0.0.28 to 0.0.29
@@ -1,2 +0,2 @@ | ||
export declare function postKernelMessage(resolve: Function, reject: Function, message: any): void; | ||
export declare function postKernelQuery(kernelQuery: any): Promise<any>; | ||
export declare function init(): Promise<string>; |
@@ -23,26 +23,34 @@ // log provides a wrapper for console.log that prefixes 'libkernel'. | ||
var queries = new Object(); | ||
// postKernelMessage will send a postMessage to the kernel, handling details | ||
// like the nonce and the resolve/reject upon receiving a response. The inputs | ||
// are a resolve and reject function of a promise that should be resolved when | ||
// the response is received, and the message that is going to the kernel | ||
// itself. | ||
export function postKernelMessage(resolve, reject, message) { | ||
let nonce = nextNonce; | ||
nextNonce++; | ||
queries[nonce] = { resolve, reject }; | ||
window.postMessage({ | ||
method: "kernelMessage", | ||
nonce, | ||
kernelMessage: message, | ||
}, window.location.origin); | ||
// postKernelQuery will send a postMessage to the kernel, handling details like | ||
// the nonce and the resolve/reject upon receiving a response. The inputs are a | ||
// resolve and reject function of a promise that should be resolved when the | ||
// response is received, and the message that is going to the kernel itself. | ||
export function postKernelQuery(kernelQuery) { | ||
return new Promise((resolve, reject) => { | ||
let nonce = nextNonce; | ||
nextNonce++; | ||
queries[nonce] = { resolve, reject }; | ||
window.postMessage({ | ||
method: "kernelQuery", | ||
nonce, | ||
kernelQuery, | ||
}, window.location.origin); | ||
}); | ||
} | ||
// handleBridgeTest will handle a response from the bridge indicating that the | ||
// bridge is working. | ||
function handleBridgeResponse() { | ||
if (bridgeExists !== false) { | ||
bridgeExists = true; | ||
bridgeAvailable.resolve("bridge is available"); | ||
// handleBridgeResponse will handle a response from the bridge indicating that | ||
// the bridge is working. | ||
function handleBridgeResponse(data) { | ||
// Check whether the timeout for the bridge has already fired. If so, | ||
// log that the bridge is available but late. | ||
if (bridgeExists === false) { | ||
logErr("received late signal from bridge"); | ||
return; | ||
} | ||
bridgeExists = true; | ||
// Check whether the version is available in the data. | ||
if (!("version" in data)) { | ||
bridgeAvailable.resolve("bridge did not report a version"); | ||
} | ||
else { | ||
logErr("received late signal from bridge"); | ||
bridgeAvailable.resolve(data.version); | ||
} | ||
@@ -58,20 +66,31 @@ } | ||
} | ||
// 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)) { | ||
// Check the status and then resolve or reject accordingly. | ||
if (!("response" in event.data) || !("queryStatus" in event.data.response)) { | ||
logErr("malformed kernel response\n", event); | ||
return; | ||
} | ||
// Either resolve or reject the promise associated with this response. | ||
if (event.data.resp !== null) { | ||
result.resolve(event.data.resp); | ||
if (event.data.response.queryStatus === "resolve") { | ||
result.resolve(event.data.response); | ||
} | ||
else if (event.data.err !== null) { | ||
result.reject(event.data.err); | ||
else if (event.data.response.queryStatus === "reject") { | ||
result.reject(event.data.response); | ||
} | ||
else { | ||
logErr("received malformed response from bridge\n", event); | ||
logErr("malformed queryStatus"); | ||
} | ||
} | ||
// handleKernelResponseErr is a special handler for situations where the | ||
// content script was unable to communicate with the background script. | ||
function handleKernelResponseErr(event) { | ||
let reject = queries[event.data.nonce]; | ||
delete queries[event.data.nonce]; | ||
if (!("err" in event.data) || typeof event.data.err !== "string") { | ||
logErr("malformed error received from bridge"); | ||
return; | ||
} | ||
logErr(event.data.err); | ||
reject(event.data.err); | ||
} | ||
// handleMessage will handle a message from the kernel, using the response to | ||
@@ -89,3 +108,3 @@ // resolve the appropriate promise in the set of queries. | ||
if (event.data.method === "bridgeTestResponse") { | ||
handleBridgeResponse(); | ||
handleBridgeResponse(event.data); | ||
return; | ||
@@ -97,2 +116,6 @@ } | ||
} | ||
if (event.data.method === "kernelResponseErr") { | ||
handleKernelResponseErr(event); | ||
return; | ||
} | ||
} | ||
@@ -99,0 +122,0 @@ // init will add an event listener for messages from the kernel bridge. It is |
@@ -1,2 +0,2 @@ | ||
import { init, postKernelMessage } from './init'; | ||
import { init, postKernelQuery } from './init'; | ||
// testMessage will send a test message to the kernel, ensuring that basic | ||
@@ -16,4 +16,10 @@ // kernel communications are working. | ||
// parameters. | ||
postKernelMessage(resolve, reject, { | ||
postKernelQuery({ | ||
kernelMethod: "requestTest", | ||
}) | ||
.then(response => { | ||
resolve(response.version); | ||
}) | ||
.catch(response => { | ||
reject(response.err); | ||
}); | ||
@@ -39,3 +45,3 @@ }) | ||
.then(x => { | ||
postKernelMessage(resolve, reject, { | ||
postKernelQuery({ | ||
kernelMethod: "moduleCall", | ||
@@ -48,2 +54,8 @@ module: "AQCS3RHbDlk00IdICFEI1rKZp-VNsnsKWC0n7K-taoAuog", | ||
}, | ||
}) | ||
.then(response => { | ||
resolve(response.output); | ||
}) | ||
.catch(response => { | ||
reject(response.err); | ||
}); | ||
@@ -67,3 +79,3 @@ }) | ||
.then(x => { | ||
postKernelMessage(resolve, reject, { | ||
postKernelQuery({ | ||
kernelMethod: "moduleCall", | ||
@@ -76,2 +88,8 @@ module: "AQAs00kS6OKUd-FIWj9qdJLArCiEDMVgYBSkaetuTF-MsQ", | ||
}, | ||
}) | ||
.then(response => { | ||
resolve(response.output); | ||
}) | ||
.catch(response => { | ||
reject(response.err); | ||
}); | ||
@@ -78,0 +96,0 @@ }) |
{ | ||
"name": "libkernel", | ||
"version": "0.0.28", | ||
"version": "0.0.29", | ||
"description": "helper library to interact with the skynet kernel", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
9850
260