abstract-confine-runtime
Advanced tools
Comparing version 0.2.0 to 0.3.0
47
index.js
const EventEmitter = require('events') | ||
module.exports = class AbstractConfineRuntime extends EventEmitter { | ||
exports.AbstractConfineRuntime = class AbstractConfineRuntime extends EventEmitter { | ||
constructor (opts = {}) { | ||
super() | ||
this.source = opts.source | ||
this.ipc = opts.ipc | ||
this.opts = opts | ||
@@ -20,5 +19,47 @@ } | ||
async handleRequest (body) { | ||
describeAPI () { | ||
return new APIDescription() | ||
} | ||
async handleAPICall (methodName, params) { | ||
throw new MethodNotFound() | ||
} | ||
} | ||
exports.APIDescription = class APIObject { | ||
constructor (children = []) { | ||
this.type = 'api' | ||
this.children = children | ||
} | ||
} | ||
exports.APIObject = class APIObject { | ||
constructor (name, children) { | ||
this.type = 'object' | ||
this.name = name | ||
this.children = children | ||
} | ||
} | ||
exports.APIMethod = class APIMethod { | ||
constructor (name) { | ||
this.type = 'method' | ||
this.name = name | ||
} | ||
} | ||
class RuntimeError extends Error { | ||
constructor (code, message, data) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.code = code; | ||
this.data = data; | ||
} | ||
} | ||
exports.MethodNotFound = class MethodNotFound extends RuntimeError { | ||
static CODE = -1 | ||
constructor (msg, data) { | ||
super(MethodNotFound.CODE, msg || 'Method not found', data) | ||
} | ||
} |
{ | ||
"name": "abstract-confine-runtime", | ||
"version": "0.2.0", | ||
"description": "The base class for confine runtimes. ", | ||
"version": "0.3.0", | ||
"description": "The base class for confine runtimes.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -13,3 +13,3 @@ # Abstract Confine Runtime | ||
const fs = require('fs') | ||
const AbstractConfineRuntime = require('abstract-confine-runtime') | ||
const { AbstractConfineRuntime, APIDescription, APIObject, APIMethod, MethodNotFound } = require('abstract-confine-runtime') | ||
@@ -19,3 +19,3 @@ module.exports = class MyConfineRuntime extends AbstractConfineRuntime { | ||
super(opts) | ||
// sets this.source, this.ipc, and this.opts | ||
// ^ sets this.source, and this.opts | ||
} | ||
@@ -40,10 +40,33 @@ | ||
async handleRequest (body) { | ||
// handle requests sent to the runtime by the host environment | ||
describeAPI () { | ||
// return a tree structure to describe the api, see below | ||
return new APIDescription() | ||
} | ||
async handleAPICall (methodName, params) { | ||
// handle any API calls sent to the runtime by the host environment | ||
// if the method does not exist, throw MethodNotFound | ||
throw new MethodNotFound() | ||
} | ||
} | ||
``` | ||
The `describeAPI()` method needs to provide a tree of `APIDescription`, `APIObject`, and `APIMethod` objects, like so: | ||
```js | ||
/* The API we want to represent: */ | ||
// hello() | ||
// sub.method() | ||
// zed() | ||
return new APIDescription([ | ||
new APIMethod('hello'), | ||
new APIObject('sub', [ | ||
new APIMethod('method') | ||
]), | ||
new APIMethod('zed') | ||
]) | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3369
52
69