primea-objects
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -8,18 +8,20 @@ <!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
- [copy][3] | ||
- [ModuleRef][4] | ||
- [ActorRef][4] | ||
- [getFuncRef][5] | ||
- [Message][6] | ||
- [getType][7] | ||
- [ModuleRef][6] | ||
- [Message][7] | ||
- [getType][8] | ||
- [generateId][9] | ||
## | ||
[index.js:5-10][8] | ||
[index.js:6-12][10] | ||
a cbor decoder for the objects | ||
Type: [Object][9] | ||
Type: [Object][11] | ||
## ID | ||
[index.js:37-52][10] | ||
[index.js:43-66][12] | ||
@@ -34,3 +36,3 @@ an ID | ||
[index.js:57-92][11] | ||
[index.js:71-132][13] | ||
@@ -41,38 +43,52 @@ A function reference | ||
- `opts` **[Object][9]** | ||
- `opts` **[Object][11]** | ||
### copy | ||
[index.js:84-91][12] | ||
[index.js:124-131][14] | ||
Creates a copy of the funcRef | ||
Returns **[FunctionRef][13]** | ||
Returns **[FunctionRef][15]** | ||
## ModuleRef | ||
## ActorRef | ||
[index.js:97-125][14] | ||
[index.js:137-177][16] | ||
A module reference | ||
An actor reference | ||
**Parameters** | ||
- `exports` **[Object][9]** a map of exported function to params for the funcion if any | ||
- `id` **[ID][15]** the id of the actor | ||
- `id` **[ID][17]** the id of the actor | ||
- `modRef` **[ModuleRef][18]** the modRef of the actor | ||
### getFuncRef | ||
[index.js:112-120][16] | ||
[index.js:152-160][19] | ||
return a function refernce given the name of the function | ||
return a function reference given the name of the function | ||
**Parameters** | ||
- `name` **[string][17]** | ||
- `name` **[string][20]** | ||
Returns **[FunctionRef][13]** | ||
Returns **[FunctionRef][15]** | ||
## ModuleRef | ||
[index.js:182-219][21] | ||
A module reference | ||
**Parameters** | ||
- `id` **[ID][17]** the id of the module | ||
- `type` **[Number][22]** type id of the module | ||
- `exports` **[Object][11]** a map of exported function to params for the function, if any | ||
- `state` **[Object][11]** state of the module | ||
- `code` **[Buffer][23]** code of the module | ||
## Message | ||
[index.js:130-162][18] | ||
[index.js:224-256][24] | ||
@@ -85,7 +101,7 @@ **Extends EventEmitter** | ||
- `opts` **[Object][9]** | ||
- `opts` **[Object][11]** | ||
## getType | ||
[index.js:169-188][19] | ||
[index.js:263-284][25] | ||
@@ -98,4 +114,18 @@ returns the type that the object is | ||
Returns **[String][17]** | ||
Returns **[String][20]** | ||
## generateId | ||
[index.js:293-297][26] | ||
returns the ID of an actor | ||
**Parameters** | ||
- `id` **[Object][11]** | ||
- `id.nonce` **[Number][22]** the actor's nonce | ||
- `id.parent` **[ID][17]** the actor's parent's ID | ||
Returns **[ID][17]** | ||
[1]: #id | ||
@@ -107,32 +137,46 @@ | ||
[4]: #moduleref | ||
[4]: #actorref | ||
[5]: #getfuncref | ||
[6]: #message | ||
[6]: #moduleref | ||
[7]: #gettype | ||
[7]: #message | ||
[8]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L5-L10 "Source code on GitHub" | ||
[8]: #gettype | ||
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object | ||
[9]: #generateid | ||
[10]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L37-L52 "Source code on GitHub" | ||
[10]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L6-L12 "Source code on GitHub" | ||
[11]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L57-L92 "Source code on GitHub" | ||
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object | ||
[12]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L84-L91 "Source code on GitHub" | ||
[12]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L43-L66 "Source code on GitHub" | ||
[13]: #functionref | ||
[13]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L71-L132 "Source code on GitHub" | ||
[14]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L97-L125 "Source code on GitHub" | ||
[14]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L124-L131 "Source code on GitHub" | ||
[15]: #id | ||
[15]: #functionref | ||
[16]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L112-L120 "Source code on GitHub" | ||
[16]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L137-L177 "Source code on GitHub" | ||
[17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[17]: #id | ||
[18]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L130-L162 "Source code on GitHub" | ||
[18]: #moduleref | ||
[19]: https://github.com/primea/js-primea-objects/blob/19ce496560f9ea1fb3b35eefab4afbe9b8d581eb/index.js#L169-L188 "Source code on GitHub" | ||
[19]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L152-L160 "Source code on GitHub" | ||
[20]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[21]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L182-L219 "Source code on GitHub" | ||
[22]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number | ||
[23]: https://nodejs.org/api/buffer.html | ||
[24]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L224-L256 "Source code on GitHub" | ||
[25]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L263-L284 "Source code on GitHub" | ||
[26]: https://github.com/primea/js-primea-objects/blob/7051614add94c2f622a5910779dce843f8591e8a/index.js#L293-L297 "Source code on GitHub" |
113
index.js
@@ -10,3 +10,4 @@ const crypto = require('crypto') | ||
func: 43, | ||
mod: 44 | ||
mod: 44, | ||
actor: 45 | ||
} | ||
@@ -27,3 +28,7 @@ | ||
}), | ||
[TAGS.mod]: val => new ModuleRef(...val), | ||
[TAGS.mod]: val => { | ||
const code = val.pop()['/'] | ||
return new ModuleRef(...val, code) | ||
}, | ||
[TAGS.actor]: val => new ActorRef(...val), | ||
[TAGS.link]: val => { | ||
@@ -42,3 +47,3 @@ return { | ||
/* | ||
* @param {Buffer} id - the id as an buffer | ||
* @param {Buffer} id - the id as a buffer | ||
*/ | ||
@@ -57,5 +62,4 @@ constructor (id) { | ||
static fromJSON (arg) { | ||
const { fromHex } = require('./utils') | ||
return new ID(fromHex(arg)) | ||
static fromJSON (data) { | ||
return new ID(Buffer.from(data.slice(2), 'hex')) | ||
} | ||
@@ -74,5 +78,8 @@ | ||
* @param {Object} opts | ||
* @param {*} opts.identifier - the function's identifier | ||
* @param {Array} opts.identifier - the function's identifier | ||
* @param {Boolean} opts.identifier[0] - true if private function | ||
* @param {String} opts.identifier[1] - name of exported function, or table index if private | ||
* @param {ID} opts.actorID - the id of the actor | ||
* @param {Array} opts.params - the params of the function | ||
* @param {Number} opts.gas - gas amount | ||
*/ | ||
@@ -97,11 +104,10 @@ constructor (opts) { | ||
const json = { | ||
'@FunctionRef': { | ||
actorID: this.actorID.toJSON(), | ||
private: this.identifier[0], | ||
name: this.identifier[1], | ||
gas: this.gas | ||
} | ||
type: getType(this), | ||
actorID: this.actorID.toJSON(), | ||
private: this.identifier[0], | ||
name: this.identifier[1], | ||
gas: this.gas | ||
} | ||
if (includeParams) { | ||
json['@FunctionRef'].params = this.params | ||
json.params = this.params | ||
} | ||
@@ -111,4 +117,3 @@ return json | ||
static fromJSON (arg) { | ||
const data = arg['@FunctionRef'] | ||
static fromJSON (data) { | ||
return new FunctionRef({ | ||
@@ -137,11 +142,11 @@ identifier: [data.private, data.name], | ||
/** | ||
* A module reference | ||
* An actor reference | ||
*/ | ||
class ModuleRef { | ||
class ActorRef { | ||
/** | ||
* @param {Object} exports - a map of exported function to params for the funcion if any | ||
* @param {ID} id - the id of the actor | ||
* @param {ModuleRef} modRef - the modRef of the actor | ||
*/ | ||
constructor (exports, id) { | ||
this.exports = exports | ||
constructor (id, modRef) { | ||
this.modRef = modRef | ||
this.id = id | ||
@@ -151,3 +156,3 @@ } | ||
/** | ||
* return a function refernce given the name of the function | ||
* return a function reference given the name of the function | ||
* @param {string} name | ||
@@ -157,3 +162,3 @@ * @returns {FunctionRef} | ||
getFuncRef (name) { | ||
const params = this.exports[name] | ||
const params = this.modRef.exports[name] | ||
@@ -168,9 +173,47 @@ return new FunctionRef({ | ||
toJSON (includeExports = true) { | ||
return { | ||
type: getType(this), | ||
id: this.id.toJSON(), | ||
mod: this.modRef.toJSON(includeExports) | ||
} | ||
} | ||
static fromJSON (data) { | ||
return new ActorRef(ID.fromJSON(data.id), ModuleRef.fromJSON(data.mod)) | ||
} | ||
encodeCBOR (gen) { | ||
return gen.write(new cbor.Tagged(TAGS.actor, [this.id, this.modRef])) | ||
} | ||
} | ||
/** | ||
* A module reference | ||
*/ | ||
class ModuleRef { | ||
/** | ||
* @param {ID} id - the id of the module | ||
* @param {Number} type - type id of the module | ||
* @param {Object} exports - a map of exported function to params for the function, if any | ||
* @param {Object} state - state of the module | ||
* @param {Buffer} code - code of the module | ||
*/ | ||
constructor (id, type, exports, state, code) { | ||
this.id = id | ||
this.type = type | ||
this.exports = exports | ||
this.state = state | ||
this.code = {'/': code} | ||
} | ||
toJSON (includeParams = true) { | ||
const json = { | ||
'@ModuleRef': { | ||
id: this.id.toJSON() | ||
} | ||
type: getType(this), | ||
id: this.id.toJSON(), | ||
modType: this.type, | ||
code: `0x${this.code['/'].toString('hex')}`, | ||
state: this.state | ||
} | ||
if (includeExports) { | ||
json['@ModuleRef'].exports = this.exports | ||
if (includeParams) { | ||
json.exports = this.exports | ||
} | ||
@@ -180,9 +223,8 @@ return json | ||
static fromJSON (arg) { | ||
const data = arg['@ModuleRef'] | ||
return new ModuleRef(data.exports, ID.fromJSON(data.id)) | ||
static fromJSON (data) { | ||
return new ModuleRef(ID.fromJSON(data.id), data.modType, data.exports, data.state, Buffer.from(data.code.slice(2), 'hex')) | ||
} | ||
encodeCBOR (gen) { | ||
return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id])) | ||
return gen.write(new cbor.Tagged(TAGS.mod, [this.id, this.type, this.exports, this.state, this.code])) | ||
} | ||
@@ -249,2 +291,4 @@ } | ||
return 'mod' | ||
} else if (obj.constructor === ActorRef) { | ||
return 'actor' | ||
} | ||
@@ -262,3 +306,3 @@ } | ||
*/ | ||
function generateActorId (id) { | ||
function generateId (id) { | ||
const encoded = _encodeActorId(id) | ||
@@ -288,5 +332,6 @@ const hashed = _hash(encoded) | ||
ModuleRef, | ||
ActorRef, | ||
decoder, | ||
getType, | ||
generateActorId | ||
generateId | ||
} |
{ | ||
"name": "primea-objects", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Object helper classes for Primea's system Objects", | ||
@@ -21,7 +21,8 @@ "main": "index.js", | ||
"events": "^2.0.0", | ||
"safe-buffer": "^5.1.1" | ||
"safe-buffer": "^5.1.2" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^3.0.0", | ||
"documentation": "^6.3.0", | ||
"documentation": "^6.3.2", | ||
"minimist": "^1.2.0", | ||
"nyc": "^11.7.1", | ||
@@ -28,0 +29,0 @@ "standard": "^11.0.1", |
@@ -19,2 +19,3 @@ const Buffer = require('safe-buffer').Buffer | ||
// id ref | ||
const id = new objects.ID(Buffer.from([0x1])) | ||
@@ -26,3 +27,4 @@ const enid = cbor.encode(id) | ||
const modRef = new objects.ModuleRef({'name': ['i32']}, id) | ||
// mod ref | ||
const modRef = new objects.ModuleRef(id, 1, {'name': ['i32']}, [], 'test') | ||
const enmod = cbor.encode(modRef) | ||
@@ -33,3 +35,10 @@ const rmodRef = objects.decoder.decodeFirst(enmod) | ||
const funcRef = rmodRef.getFuncRef('name') | ||
// actor ref | ||
const actorRef = new objects.ActorRef(id, modRef) | ||
const enactor = cbor.encode(actorRef) | ||
const ractorRef = objects.decoder.decodeFirst(enactor) | ||
t.equals(objects.getType(actorRef), 'actor') | ||
t.deepEquals(actorRef, ractorRef) | ||
const funcRef = ractorRef.getFuncRef('name') | ||
funcRef.gas = 1000 | ||
@@ -58,11 +67,11 @@ const funcRef2 = funcRef.copy() | ||
const id0 = { nonce: 0, parent: null } | ||
const hashedId0 = objects.generateActorId(id0) | ||
const hashedId0 = objects.generateId(id0) | ||
t.deepEquals(hashedId0.id, Buffer.from('372a08b828598122fc64c4aa94735c770f25bbbc', 'hex')) | ||
const id00 = { nonce: 0, parent: hashedId0 } | ||
const hashedId00 = objects.generateActorId(id00) | ||
const hashedId00 = objects.generateId(id00) | ||
t.deepEquals(hashedId00.id, Buffer.from('10d7d4be8663c37d8ea7cff89b7c01c059ebbc80', 'hex')) | ||
const id01 = { nonce: 1, parent: hashedId0 } | ||
const hashedId01 = objects.generateActorId(id01) | ||
const hashedId01 = objects.generateId(id01) | ||
t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex')) | ||
@@ -75,7 +84,10 @@ | ||
const id = new objects.ID(Buffer.from([0x1])) | ||
const id2 = new objects.ID(Buffer.from([0x2])) | ||
const modRef = new objects.ModuleRef(id, 1, {'name': ['i32']}, [], Buffer.from('code')) | ||
const obj = [ | ||
new objects.ModuleRef({'name': ['i32']}, id), | ||
modRef, | ||
new objects.ActorRef(id2, modRef), | ||
new objects.FunctionRef({ | ||
identifier: [false, 'main'], | ||
actorID: id, | ||
actorID: id2, | ||
params: ['i32'], | ||
@@ -86,13 +98,15 @@ gas: 100 | ||
] | ||
const jsonFull = utils.toJSON(obj) | ||
t.deepEquals(JSON.stringify(jsonFull), '[{"@ModuleRef":{"id":"0x01","exports":{"name":["i32"]}}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100,"params":["i32"]}},"0x01020304"]') | ||
const json = utils.toJSON(obj) | ||
t.deepEquals(JSON.stringify(json), '[{"type":"mod","id":"0x01","modType":1,"code":"0x636f6465","state":[],"exports":{"name":["i32"]}},{"type":"actor","id":"0x02","mod":{"type":"mod","id":"0x01","modType":1,"code":"0x636f6465","state":[],"exports":{"name":["i32"]}}},{"type":"func","actorID":"0x02","private":false,"name":"main","gas":100,"params":["i32"]},"0x01020304"]') | ||
const json = utils.toJSON(obj, false) | ||
t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]') | ||
const jsonPartial = utils.toJSON(obj, false) | ||
t.deepEquals(JSON.stringify(jsonPartial), '[{"type":"mod","id":"0x01","modType":1,"code":"0x636f6465","state":[]},{"type":"actor","id":"0x02","mod":{"type":"mod","id":"0x01","modType":1,"code":"0x636f6465","state":[]}},{"type":"func","actorID":"0x02","private":false,"name":"main","gas":100},"0x01020304"]') | ||
const newObj = [ | ||
new objects.ModuleRef(undefined, id), | ||
const modRefPartial = new objects.ModuleRef(id, 1, undefined, [], Buffer.from('code')) | ||
const objPartial = [ | ||
modRefPartial, | ||
new objects.ActorRef(id2, modRefPartial), | ||
new objects.FunctionRef({ | ||
identifier: [false, 'main'], | ||
actorID: id, | ||
actorID: id2, | ||
gas: 100 | ||
@@ -102,7 +116,7 @@ }), | ||
] | ||
t.deepEquals(utils.fromJSON(json), newObj) | ||
t.deepEquals(utils.fromJSON(jsonPartial), objPartial) | ||
t.deepEquals(utils.fromJSON(jsonFull), obj) | ||
t.deepEquals(utils.fromJSON(json), obj) | ||
const hashedId01 = utils.actorRefToId([0, 1]) | ||
const hashedId01 = utils.actorPathToId([0, 1]) | ||
t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex')) | ||
@@ -109,0 +123,0 @@ |
28
utils.js
@@ -1,4 +0,10 @@ | ||
const { FunctionRef, ModuleRef, generateActorId, getType } = require('./') | ||
const { FunctionRef, ActorRef, ModuleRef, generateId, getType } = require('./') | ||
const actorRefToId = ref => ref.reduce((parent, curr) => generateActorId({ parent, nonce: curr }), null) | ||
/** | ||
* returns the ID of an actor, given the path | ||
* @param {Array} path - indexes of each actor relative to parent | ||
* @example [0, 1, 0] - the first child of the second child of the root actor | ||
* @return {ID} | ||
*/ | ||
const actorPathToId = path => path.reduce((parent, curr) => generateId({ parent, nonce: curr }), null) | ||
@@ -16,2 +22,3 @@ const toHex = arg => Buffer.isBuffer(arg) ? `0x${arg.toString('hex')}` : arg | ||
case 'mod': | ||
case 'actor': | ||
return arg.toJSON(includeOptional) | ||
@@ -36,6 +43,9 @@ case 'link': | ||
if (arg['@FunctionRef']) { | ||
return FunctionRef.fromJSON(arg) | ||
} else if (arg['@ModuleRef']) { | ||
return ModuleRef.fromJSON(arg) | ||
switch (arg.type) { | ||
case 'func': | ||
return FunctionRef.fromJSON(arg) | ||
case 'actor': | ||
return ActorRef.fromJSON(arg) | ||
case 'mod': | ||
return ModuleRef.fromJSON(arg) | ||
} | ||
@@ -48,7 +58,5 @@ } | ||
module.exports = { | ||
actorRefToId, | ||
actorPathToId, | ||
toJSON, | ||
fromJSON, | ||
toHex, | ||
fromHex | ||
fromJSON | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
35745
6
8
445
1
Updatedsafe-buffer@^5.1.2