UNPKG

6.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PluginConnector = exports.transformUrl = exports.defaultGateways = void 0;
4const tslib_1 = require("tslib");
5const abstract_1 = require("./abstract");
6/** List of available gateways for decentralised storage */
7exports.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/** Transform the URL to use a gateway if decentralised storage is specified */
12function 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}
16exports.transformUrl = transformUrl;
17class 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 /** Set options for an external plugin */
46 setOptions(options = {}) {
47 super.setOptions(options);
48 }
49 /** Call a method from this plugin */
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 /** Perform handshake with the client if not loaded yet */
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 // If there is a broken connection we want send back the handshake to the plugin client
81 return this.callPluginMethod('handshake', [this.profile.name]);
82 }
83 });
84 }
85 /**
86 * React when a message comes from client
87 * @param message The message sent by the client
88 */
89 getMessage(message) {
90 return tslib_1.__awaiter(this, void 0, void 0, function* () {
91 // Check for handshake request from the client
92 if (message.action === 'request' && message.key === 'handshake') {
93 return this.handshake();
94 }
95 switch (message.action) {
96 // Start listening on an event
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 // Emit an event
116 case 'emit':
117 case 'notification': {
118 if (!message.payload)
119 break;
120 this.emit(message.key, ...message.payload);
121 break;
122 }
123 // Call a method
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 // Return result from exposed method
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}
153exports.PluginConnector = PluginConnector;
154//# sourceMappingURL=connector.js.map
\No newline at end of file