1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PluginConnector = exports.transformUrl = exports.defaultGateways = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const abstract_1 = require("./abstract");
|
6 |
|
7 | exports.defaultGateways = {
|
8 | 'ipfs://': (url, name) => `https://${name}.dyn.plugin.remixproject.org/ipfs/${url.replace('ipfs://', '')}`,
|
9 | 'swarm://': (url, name) => `https://swarm-gateways.net/bzz-raw://${url.replace('swarm://', '')}`
|
10 | };
|
11 |
|
12 | function transformUrl({ url, name }) {
|
13 | const network = Object.keys(exports.defaultGateways).find(key => url.startsWith(key));
|
14 | return network ? exports.defaultGateways[network](url, name) : url;
|
15 | }
|
16 | exports.transformUrl = transformUrl;
|
17 | class PluginConnector extends abstract_1.Plugin {
|
18 | constructor(profile) {
|
19 | super(profile);
|
20 | this.id = 0;
|
21 | this.pendingRequest = {};
|
22 | }
|
23 | activate() {
|
24 | const _super = Object.create(null, {
|
25 | activate: { get: () => super.activate }
|
26 | });
|
27 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
28 | const url = this.options.transformUrl
|
29 | ? this.options.transformUrl(this.profile)
|
30 | : transformUrl(this.profile);
|
31 | yield this.connect(url);
|
32 | return _super.activate.call(this);
|
33 | });
|
34 | }
|
35 | deactivate() {
|
36 | const _super = Object.create(null, {
|
37 | deactivate: { get: () => super.deactivate }
|
38 | });
|
39 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
40 | this.loaded = false;
|
41 | yield this.disconnect();
|
42 | return _super.deactivate.call(this);
|
43 | });
|
44 | }
|
45 |
|
46 | setOptions(options = {}) {
|
47 | super.setOptions(options);
|
48 | }
|
49 |
|
50 | callPluginMethod(key, payload = []) {
|
51 | const action = 'request';
|
52 | const id = this.id++;
|
53 | const requestInfo = this.currentRequest;
|
54 | const name = this.name;
|
55 | const promise = new Promise((res, rej) => {
|
56 | this.pendingRequest[id] = (result, error) => error ? rej(error) : res(result);
|
57 | });
|
58 | this.send({ id, action, key, payload, requestInfo, name });
|
59 | return promise;
|
60 | }
|
61 |
|
62 | handshake() {
|
63 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
64 | if (!this.loaded) {
|
65 | this.loaded = true;
|
66 | let methods;
|
67 | try {
|
68 | methods = yield this.callPluginMethod('handshake', [this.profile.name]);
|
69 | }
|
70 | catch (err) {
|
71 | this.loaded = false;
|
72 | throw err;
|
73 | }
|
74 | if (methods) {
|
75 | this.profile.methods = methods;
|
76 | yield this.call('manager', 'updateProfile', this.profile);
|
77 | }
|
78 | }
|
79 | else {
|
80 |
|
81 | return this.callPluginMethod('handshake', [this.profile.name]);
|
82 | }
|
83 | });
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 | getMessage(message) {
|
90 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
91 |
|
92 | if (message.action === 'request' && message.key === 'handshake') {
|
93 | return this.handshake();
|
94 | }
|
95 | switch (message.action) {
|
96 |
|
97 | case 'on':
|
98 | case 'listen': {
|
99 | const { name, key } = message;
|
100 | const action = 'notification';
|
101 | this.on(name, key, (...payload) => this.send({ action, name, key, payload }));
|
102 | break;
|
103 | }
|
104 | case 'off': {
|
105 | const { name, key } = message;
|
106 | this.off(name, key);
|
107 | break;
|
108 | }
|
109 | case 'once': {
|
110 | const { name, key } = message;
|
111 | const action = 'notification';
|
112 | this.once(name, key, (...payload) => this.send({ action, name, key, payload }));
|
113 | break;
|
114 | }
|
115 |
|
116 | case 'emit':
|
117 | case 'notification': {
|
118 | if (!message.payload)
|
119 | break;
|
120 | this.emit(message.key, ...message.payload);
|
121 | break;
|
122 | }
|
123 |
|
124 | case 'call':
|
125 | case 'request': {
|
126 | const action = 'response';
|
127 | try {
|
128 | const payload = yield this.call(message.name, message.key, ...message.payload);
|
129 | const error = undefined;
|
130 | this.send(Object.assign(Object.assign({}, message), { action, payload, error }));
|
131 | }
|
132 | catch (err) {
|
133 | const payload = undefined;
|
134 | const error = err.message || err;
|
135 | this.send(Object.assign(Object.assign({}, message), { action, payload, error }));
|
136 | }
|
137 | break;
|
138 | }
|
139 |
|
140 | case 'response': {
|
141 | const { id, payload, error } = message;
|
142 | this.pendingRequest[id](payload, error);
|
143 | delete this.pendingRequest[id];
|
144 | break;
|
145 | }
|
146 | default: {
|
147 | throw new Error('Message should be a notification, request or response');
|
148 | }
|
149 | }
|
150 | });
|
151 | }
|
152 | }
|
153 | exports.PluginConnector = PluginConnector;
|
154 |
|
\ | No newline at end of file |