Files: fad57dff2dd8eeb05b10062b336a4c44cf1bd050 / systemObjects.js
2506 bytesRaw
1 | const cbor = require('borc') |
2 | const EventEmitter = require('events') |
3 | |
4 | const TAGS = { |
5 | id: 41, |
6 | link: 42, |
7 | func: 43, |
8 | mod: 44 |
9 | } |
10 | |
11 | const 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 | const decoder = new cbor.Decoder({ |
21 | tags: { |
22 | [TAGS.id]: val => new ID(val), |
23 | [TAGS.func]: val => new FunctionRef({ |
24 | identifier: val[0], |
25 | params: val[1], |
26 | id: val[2], |
27 | gas: val[3] |
28 | }), |
29 | [TAGS.mod]: val => new ModuleRef(...val), |
30 | [TAGS.link]: val => { |
31 | return { |
32 | '/': val |
33 | } |
34 | } |
35 | } |
36 | }) |
37 | |
38 | class FunctionRef { |
39 | constructor (opts) { |
40 | this.identifier = opts.identifier |
41 | this.destId = opts.id |
42 | this.params = opts.params |
43 | this.gas = opts.gas || 0 |
44 | } |
45 | |
46 | encodeCBOR (gen) { |
47 | return gen.write(new cbor.Tagged(TAGS.func, [ |
48 | this.identifier, |
49 | this.params, |
50 | this.destId, |
51 | this.gas |
52 | ])) |
53 | } |
54 | } |
55 | |
56 | class ModuleRef { |
57 | constructor (ex, id) { |
58 | this.exports = ex |
59 | this.id = id |
60 | } |
61 | |
62 | getFuncRef (name) { |
63 | const params = this.exports[name] |
64 | |
65 | return new FunctionRef({ |
66 | identifier: [false, name], |
67 | params, |
68 | id: this.id |
69 | }) |
70 | } |
71 | |
72 | encodeCBOR (gen) { |
73 | return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id])) |
74 | } |
75 | } |
76 | |
77 | class ID { |
78 | constructor (id) { |
79 | this.id = id |
80 | } |
81 | |
82 | encodeCBOR (gen) { |
83 | return gen.write(new cbor.Tagged(TAGS.id, this.id)) |
84 | } |
85 | } |
86 | |
87 | /** |
88 | * This implements Messages for Primea |
89 | * @module primea-message |
90 | */ |
91 | class Message extends EventEmitter { |
92 | /** |
93 | * @param {Object} opts |
94 | * @param {ArrayBuffer} opts.data - the payload of the message |
95 | * @param {Array<Object>} opts.caps - an array of capabilities to send in the message |
96 | */ |
97 | constructor (opts) { |
98 | super() |
99 | const defaults = this.constructor.defaults |
100 | this._opts = Object.assign(defaults, opts) |
101 | Object.keys(this._opts).forEach(key => { |
102 | Object.defineProperty(this, key, { |
103 | get: function () { |
104 | return this._opts[key] |
105 | }, |
106 | set: function (y) { |
107 | this._opts[key] = y |
108 | } |
109 | }) |
110 | }) |
111 | } |
112 | |
113 | static get defaults () { |
114 | return { |
115 | ticks: 0, |
116 | funcRef: null, |
117 | funcArguments: [], |
118 | funcParameters: [], |
119 | _fromId: new ID(Buffer.alloc(20)), |
120 | _fromTicks: 0 |
121 | } |
122 | } |
123 | } |
124 | |
125 | module.exports = { |
126 | Message, |
127 | ID, |
128 | FunctionRef, |
129 | ModuleRef, |
130 | DEFAULTS, |
131 | decoder |
132 | } |
133 |
Built with git-ssb-web