@berish/rfp
Version:
Binary secure transport organization protocol for peer communication using function fingerprints
121 lines • 5.88 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PeerTransport = void 0;
const emitter_1 = require("@berish/emitter");
const transportEncoders_1 = require("./transportEncoders");
class PeerTransport {
constructor(transportAdapter, plugins) {
this._transportAdapter = null;
this._plugins = null;
this._cacheEmitter = null;
this._isConnected = false;
if (!transportAdapter)
throw new TypeError('Peer transport adapter is null');
this._transportAdapter = transportAdapter;
this._plugins = plugins || [];
this._cacheEmitter = new emitter_1.CacheEmitter();
}
get transportAdapter() {
return this._transportAdapter;
}
get binaryEncoder() {
return this.transportAdapter.binaryEncoder || transportEncoders_1.cborBinaryEncoder;
}
get stringEncoder() {
return this.transportAdapter.stringEncoder || transportEncoders_1.jsonStringEncoder;
}
get plugins() {
return this._plugins || [];
}
send(peer, data) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.transportAdapter.send)
throw new TypeError('Transport adapter send is null');
try {
const beforeSend = yield this._beforeSend(peer, data);
yield this.transportAdapter.send(beforeSend);
return true;
}
catch (err) {
yield peer.emitter.emitAsync('error', err);
return false;
}
});
}
subscribe(peer, callback) {
return this._cacheEmitter.subscribe('subscribe', (callback) => this.transportAdapter.subscribe(callback), (data) => __awaiter(this, void 0, void 0, function* () {
try {
const __beforeResponse = yield this._beforeResponse(peer, data);
callback(__beforeResponse);
}
catch (err) {
yield peer.emitter.emitAsync('error', err);
}
}));
}
unsubscribe(eventHash) {
return this._cacheEmitter.unsubscribe(eventHash);
}
_beforeSend(peer, data) {
return __awaiter(this, void 0, void 0, function* () {
const beforeDataSend = data && (yield this._beforeDataSend(peer, data));
const binaryEncodeData = beforeDataSend && (yield this.binaryEncoder.encode(beforeDataSend));
const beforeTransportSend = binaryEncodeData && (yield this._beforeTransportSend(peer, binaryEncodeData));
const binaryData = this.transportAdapter.binaryFormat === 'string'
? yield this.stringEncoder.encode(beforeTransportSend)
: beforeTransportSend;
return binaryData;
});
}
_beforeResponse(peer, data) {
return __awaiter(this, void 0, void 0, function* () {
const binaryData = this.transportAdapter.binaryFormat === 'string'
? yield this.stringEncoder.decode(data)
: data;
const beforeTransportResponse = binaryData && (yield this._beforeTransportResponse(peer, binaryData));
const binaryDecodeData = beforeTransportResponse && (yield this.binaryEncoder.decode(beforeTransportResponse));
const beforeDataResponse = binaryDecodeData && (yield this._beforeDataResponse(peer, binaryDecodeData));
return beforeDataResponse;
});
}
_beforeDataSend(peer, chunk) {
return this.plugins.reduce((chunkPromise, plugin) => __awaiter(this, void 0, void 0, function* () {
const chunk = yield chunkPromise;
const data = (plugin && plugin.beforeDataSend && (yield plugin.beforeDataSend(peer, chunk))) || chunk;
return data;
}), Promise.resolve(chunk));
}
_beforeTransportSend(peer, binaryData) {
return this.plugins.reduce((binaryDataPromise, plugin) => __awaiter(this, void 0, void 0, function* () {
const binaryData = yield binaryDataPromise;
const data = (plugin && plugin.beforeTransportSend && (yield plugin.beforeTransportSend(peer, binaryData))) || binaryData;
return data;
}), Promise.resolve(binaryData));
}
_beforeDataResponse(peer, chunk) {
return this.plugins.reduceRight((chunkPromise, plugin) => __awaiter(this, void 0, void 0, function* () {
const chunk = yield chunkPromise;
const data = (plugin && plugin.beforeDataResponse && (yield plugin.beforeDataResponse(peer, chunk))) || chunk;
return data;
}), Promise.resolve(chunk));
}
_beforeTransportResponse(peer, binaryData) {
return this.plugins.reduceRight((binaryDataPromise, plugin) => __awaiter(this, void 0, void 0, function* () {
const binaryData = yield binaryDataPromise;
const data = (plugin && plugin.beforeTransportResponse && (yield plugin.beforeTransportResponse(peer, binaryData))) ||
binaryData;
return data;
}), Promise.resolve(binaryData));
}
}
exports.PeerTransport = PeerTransport;
//# sourceMappingURL=transport.js.map