UNPKG

6.35 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 var _a, _b;
64 return tslib_1.__awaiter(this, void 0, void 0, function* () {
65 if (!this.loaded) {
66 this.loaded = true;
67 let methods;
68 try {
69 methods = yield this.callPluginMethod('handshake', [this.profile.name, (_a = this.options) === null || _a === void 0 ? void 0 : _a.engine]);
70 }
71 catch (err) {
72 this.loaded = false;
73 throw err;
74 }
75 if (methods) {
76 this.profile.methods = methods;
77 yield this.call('manager', 'updateProfile', this.profile);
78 }
79 }
80 else {
81 // If there is a broken connection we want send back the handshake to the plugin client
82 return this.callPluginMethod('handshake', [this.profile.name, (_b = this.options) === null || _b === void 0 ? void 0 : _b.engine]);
83 }
84 });
85 }
86 /**
87 * React when a message comes from client
88 * @param message The message sent by the client
89 */
90 getMessage(message) {
91 return tslib_1.__awaiter(this, void 0, void 0, function* () {
92 // Check for handshake request from the client
93 if (message.action === 'request' && message.key === 'handshake') {
94 return this.handshake();
95 }
96 switch (message.action) {
97 // Start listening on an event
98 case 'on':
99 case 'listen': {
100 const { name, key } = message;
101 const action = 'notification';
102 this.on(name, key, (...payload) => this.send({ action, name, key, payload }));
103 break;
104 }
105 case 'off': {
106 const { name, key } = message;
107 this.off(name, key);
108 break;
109 }
110 case 'once': {
111 const { name, key } = message;
112 const action = 'notification';
113 this.once(name, key, (...payload) => this.send({ action, name, key, payload }));
114 break;
115 }
116 // Emit an event
117 case 'emit':
118 case 'notification': {
119 if (!message.payload)
120 break;
121 this.emit(message.key, ...message.payload);
122 break;
123 }
124 // Call a method
125 case 'call':
126 case 'request': {
127 const action = 'response';
128 try {
129 const payload = yield this.call(message.name, message.key, ...message.payload);
130 const error = undefined;
131 this.send(Object.assign(Object.assign({}, message), { action, payload, error }));
132 }
133 catch (err) {
134 const payload = undefined;
135 const error = err.message || err;
136 this.send(Object.assign(Object.assign({}, message), { action, payload, error }));
137 }
138 break;
139 }
140 // Return result from exposed method
141 case 'response': {
142 const { id, payload, error } = message;
143 this.pendingRequest[id](payload, error);
144 delete this.pendingRequest[id];
145 break;
146 }
147 default: {
148 throw new Error('Message should be a notification, request or response');
149 }
150 }
151 });
152 }
153}
154exports.PluginConnector = PluginConnector;
155//# sourceMappingURL=connector.js.map
\No newline at end of file