git ssb

0+

wanderer🌟 / js-primea-objects



Tree: 09b554dfb4943d0c5ad9b2af9e2c0770f1f492f3

Files: 09b554dfb4943d0c5ad9b2af9e2c0770f1f492f3 / index.js

3244 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 actorID: 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
42/**
43 * an ID
44 */
45class ID {
46 /*
47 * @param {Buffer} id - the id as an buffer
48 */
49 constructor (id) {
50 this.id = id
51 }
52
53 toString () {
54 return this.id.toString('hex')
55 }
56
57 encodeCBOR (gen) {
58 return gen.write(new cbor.Tagged(TAGS.id, this.id))
59 }
60}
61
62/**
63 * A function reference
64 */
65class FunctionRef {
66 /**
67 * @param {Object} opts
68 * @param {*} opts.identifier - the function's identifier
69 * @param {ID} opts.actorID - the id of the actor
70 * @param {Array} opts.params - the params of the function
71 */
72 constructor (opts) {
73 this.identifier = opts.identifier
74 this.actorID = opts.actorID
75 this.params = opts.params
76 this.gas = opts.gas || 0
77 }
78
79 encodeCBOR (gen) {
80 return gen.write(new cbor.Tagged(TAGS.func, [
81 this.identifier,
82 this.params,
83 this.actorID,
84 this.gas
85 ]))
86 }
87}
88
89/**
90 * A module reference
91 */
92class ModuleRef {
93 /**
94 * @param {Object} exports - a map of exported function to params for the funcion if any
95 * @param {ID} id - the id of the actor
96 */
97 constructor (exports, id) {
98 this.exports = exports
99 this.id = id
100 }
101
102 /**
103 * return a function refernce given the name of the function
104 * @param {string} name
105 * @returns {FunctionRef}
106 */
107 getFuncRef (name) {
108 const params = this.exports[name]
109
110 return new FunctionRef({
111 identifier: [false, name],
112 params,
113 actorID: this.id
114 })
115 }
116
117 encodeCBOR (gen) {
118 return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id]))
119 }
120}
121
122/**
123 * This implements Messages for Primea
124 */
125class Message extends EventEmitter {
126 /**
127 * @param {Object} opts
128 * @param {ArrayBuffer} opts.data - the payload of the message
129 * @param {Array<Object>} opts.caps - an array of capabilities to send in the message
130 */
131 constructor (opts) {
132 super()
133 const defaults = this.constructor.defaults
134 this._opts = Object.assign(defaults, opts)
135 Object.keys(this._opts).forEach(key => {
136 Object.defineProperty(this, key, {
137 get: function () {
138 return this._opts[key]
139 },
140 set: function (y) {
141 this._opts[key] = y
142 }
143 })
144 })
145 }
146
147 static get defaults () {
148 return {
149 ticks: 0,
150 funcRef: null,
151 funcArguments: [],
152 funcParameters: [],
153 _fromId: new ID(Buffer.alloc(20)),
154 _fromTicks: 0
155 }
156 }
157}
158
159module.exports = {
160 Message,
161 ID,
162 FunctionRef,
163 ModuleRef,
164 DEFAULTS,
165 decoder
166}
167

Built with git-ssb-web