git ssb

0+

wanderer🌟 / js-primea-objects



Tree: c1f75caa1f59885fb55c6c10451c5b0dbafab401

Files: c1f75caa1f59885fb55c6c10451c5b0dbafab401 / index.js

3155 bytesRaw
1const cbor = require('borc')
2const EventEmitter = require('events')
3
4const TAGS = {
5 id: 41,
6 link: 42,
7 func: 43,
8 mod: 44
9}
10
11const DEFAULTS = {
12 elem: [],
13 data: Buffer.from([]),
14 id: new cbor.Tagged(TAGS.id, 0),
15 mod: new cbor.Tagged(TAGS.mod, [{}, new cbor.Tagged(TAGS.id, 0)]),
16 link: new cbor.Tagged(TAGS.link, null),
17 func: new cbor.Tagged(TAGS.func, 0)
18}
19
20/**
21 * a cbor decoder for the objects
22 * @type {Object}
23 */
24const decoder = new cbor.Decoder({
25 tags: {
26 [TAGS.id]: val => new ID(val),
27 [TAGS.func]: val => new FunctionRef({
28 identifier: val[0],
29 params: val[1],
30 id: val[2],
31 gas: val[3]
32 }),
33 [TAGS.mod]: val => new ModuleRef(...val),
34 [TAGS.link]: val => {
35 return {
36 '/': val
37 }
38 }
39 }
40})
41
42class ID {
43 /**
44 * an ID
45 * @param {Buffer} id - the id as an buffer
46 */
47 constructor (id) {
48 this.id = id
49 }
50
51 encodeCBOR (gen) {
52 return gen.write(new cbor.Tagged(TAGS.id, this.id))
53 }
54}
55
56class FunctionRef {
57 /**
58 * A function reference
59 * @param {Object} opts
60 * @param {*} opts.identifier - the function's identifier
61 * @param {ID} opts.actorID - the id of the actor
62 * @param {Array} opts.params - the params of the function
63 */
64 constructor (opts) {
65 this.identifier = opts.identifier
66 this.actorID = opts.actorID
67 this.params = opts.params
68 this.gas = opts.gas || 0
69 }
70
71 encodeCBOR (gen) {
72 return gen.write(new cbor.Tagged(TAGS.func, [
73 this.identifier,
74 this.params,
75 this.actorID,
76 this.gas
77 ]))
78 }
79}
80
81class ModuleRef {
82 /**
83 * A module reference
84 * @param {Object} exports - a map of exported function to params for the funcion if any
85 * @param {ID} id - the id of the actor
86 */
87 constructor (exports, id) {
88 this.exports = exports
89 this.id = id
90 }
91
92 /**
93 * return a function refernce given the name of the function
94 * @param {string} name
95 * @returns {FunctionRef}
96 */
97 getFuncRef (name) {
98 const params = this.exports[name]
99
100 return new FunctionRef({
101 identifier: [false, name],
102 params,
103 id: this.id
104 })
105 }
106
107 encodeCBOR (gen) {
108 return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id]))
109 }
110}
111
112class Message extends EventEmitter {
113 /**
114 * This implements Messages for Primea
115 * @param {Object} opts
116 * @param {ArrayBuffer} opts.data - the payload of the message
117 * @param {Array<Object>} opts.caps - an array of capabilities to send in the message
118 */
119 constructor (opts) {
120 super()
121 const defaults = this.constructor.defaults
122 this._opts = Object.assign(defaults, opts)
123 Object.keys(this._opts).forEach(key => {
124 Object.defineProperty(this, key, {
125 get: function () {
126 return this._opts[key]
127 },
128 set: function (y) {
129 this._opts[key] = y
130 }
131 })
132 })
133 }
134
135 static get defaults () {
136 return {
137 ticks: 0,
138 funcRef: null,
139 funcArguments: [],
140 funcParameters: [],
141 _fromId: new ID(Buffer.alloc(20)),
142 _fromTicks: 0
143 }
144 }
145}
146
147module.exports = {
148 Message,
149 ID,
150 FunctionRef,
151 ModuleRef,
152 DEFAULTS,
153 decoder
154}
155

Built with git-ssb-web