Compare commits

..

No commits in common. "cfd5f69cfb1438c2e940fbc48cf339071e1c1bf2" and "9791e7c4a138d24e56083b370c1394ddb027373d" have entirely different histories.

9 changed files with 24 additions and 41 deletions

13
dist/peer.js vendored
View File

@ -33,21 +33,12 @@ class Peer {
const self = this; const self = this;
this._channel = await this._muxer.createChannel({ this._channel = await this._muxer.createChannel({
protocol: this._proxy.protocol, protocol: this._proxy.protocol,
onopen: async (m) => { onopen: this.handleChannelOnOpen.bind(this),
await this.handleChannelOnOpen(m); onclose: this.handleChannelOnClose.bind(this),
// @ts-ignore
await this._onopen?.(this._channel);
},
onclose: async (socket) => {
await this.handleChannelOnClose(socket);
// @ts-ignore
await this._onclose?.();
},
}); });
await this.initMessages(); await this.initMessages();
await this._onchannel?.(this._channel); await this._onchannel?.(this._channel);
await this._channel.open(); await this._channel.open();
this._proxy.emit("peerChannelOpen", this);
} }
async init() { async init() {
await this.initSocket(); await this.initSocket();

View File

@ -41,6 +41,7 @@ class Peer extends peer_js_1.default {
} }
async handleChannelOnClose(socket) { async handleChannelOnClose(socket) {
this._socket?.destroy(); this._socket?.destroy();
await this._onclose?.(this._socket);
} }
async initMessages() { async initMessages() {
const self = this; const self = this;

View File

@ -149,7 +149,7 @@ class MultiSocketProxy extends proxy_js_1.default {
} }
async _registerOpenSocketMessage(peer) { async _registerOpenSocketMessage(peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: { encoding: {
preencode: compact_encoding_1.json.preencode, preencode: compact_encoding_1.json.preencode,
encode: compact_encoding_1.json.encode, encode: compact_encoding_1.json.encode,
@ -183,7 +183,7 @@ class MultiSocketProxy extends proxy_js_1.default {
} }
async _registerWriteSocketMessage(peer) { async _registerWriteSocketMessage(peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: writeSocketEncoding, encoding: writeSocketEncoding,
onmessage(m) { onmessage(m) {
self._sockets.get(m.id)?.push(m.data); self._sockets.get(m.id)?.push(m.data);
@ -195,7 +195,7 @@ class MultiSocketProxy extends proxy_js_1.default {
} }
async _registerCloseSocketMessage(peer) { async _registerCloseSocketMessage(peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: socketEncoding, encoding: socketEncoding,
onmessage(m) { onmessage(m) {
self._sockets.get(m.id)?.end(); self._sockets.get(m.id)?.end();
@ -207,7 +207,7 @@ class MultiSocketProxy extends proxy_js_1.default {
} }
async _registerTimeoutSocketMessage(peer) { async _registerTimeoutSocketMessage(peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: socketEncoding, encoding: socketEncoding,
onmessage(m) { onmessage(m) {
// @ts-ignore // @ts-ignore
@ -220,7 +220,7 @@ class MultiSocketProxy extends proxy_js_1.default {
} }
async _registerErrorSocketMessage(peer) { async _registerErrorSocketMessage(peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: errorSocketEncoding, encoding: errorSocketEncoding,
onmessage(m) { onmessage(m) {
// @ts-ignore // @ts-ignore

View File

@ -2,7 +2,6 @@ import BasePeer from "../../peer.js";
import Socket from "../../socket.js"; import Socket from "../../socket.js";
import MultiSocketProxy from "../multiSocket.js"; import MultiSocketProxy from "../multiSocket.js";
export default class Peer extends BasePeer { export default class Peer extends BasePeer {
protected initMessages(): Promise<void>;
protected _proxy: MultiSocketProxy; protected _proxy: MultiSocketProxy;
protected initSocket(): Promise<void>; protected initSocket(): Promise<void>;
get stream(): any; get stream(): any;

View File

@ -5,9 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const peer_js_1 = __importDefault(require("../../peer.js")); const peer_js_1 = __importDefault(require("../../peer.js"));
class Peer extends peer_js_1.default { class Peer extends peer_js_1.default {
async initMessages() {
await this._proxy.handleNewPeerChannel(this);
}
async initSocket() { } async initSocket() { }
get stream() { get stream() {
return this._muxer.stream; return this._muxer.stream;
@ -15,6 +12,9 @@ class Peer extends peer_js_1.default {
async handleChannelOnClose(socket) { async handleChannelOnClose(socket) {
return this._proxy.handleClosePeer(this); return this._proxy.handleClosePeer(this);
} }
async handleChannelOnOpen(m) { } async handleChannelOnOpen(m) {
await this._proxy.handleNewPeerChannel(this);
this._proxy.emit("peerOpen", this);
}
} }
exports.default = Peer; exports.default = Peer;

View File

@ -105,23 +105,14 @@ export default abstract class Peer {
this._channel = await this._muxer.createChannel({ this._channel = await this._muxer.createChannel({
protocol: this._proxy.protocol, protocol: this._proxy.protocol,
onopen: async (m: any) => { onopen: this.handleChannelOnOpen.bind(this),
await this.handleChannelOnOpen(m); onclose: this.handleChannelOnClose.bind(this),
// @ts-ignore
await this._onopen?.(this._channel);
},
onclose: async (socket: Socket) => {
await this.handleChannelOnClose(socket);
// @ts-ignore
await this._onclose?.();
},
}); });
await this.initMessages(); await this.initMessages();
await this._onchannel?.(this._channel); await this._onchannel?.(this._channel);
await this._channel.open(); await this._channel.open();
this._proxy.emit("peerChannelOpen", this);
} }
async init() { async init() {

View File

@ -43,6 +43,7 @@ export default class Peer extends BasePeer {
protected async handleChannelOnClose(socket: Socket): Promise<void> { protected async handleChannelOnClose(socket: Socket): Promise<void> {
this._socket?.destroy(); this._socket?.destroy();
await this._onclose?.(this._socket);
} }
protected async initMessages(): Promise<void> { protected async initMessages(): Promise<void> {

View File

@ -189,7 +189,7 @@ export default class MultiSocketProxy extends Proxy {
private async _registerOpenSocketMessage(peer: Peer) { private async _registerOpenSocketMessage(peer: Peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: { encoding: {
preencode: json.preencode, preencode: json.preencode,
encode: json.encode, encode: json.encode,
@ -237,7 +237,7 @@ export default class MultiSocketProxy extends Proxy {
private async _registerWriteSocketMessage(peer: Peer) { private async _registerWriteSocketMessage(peer: Peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: writeSocketEncoding, encoding: writeSocketEncoding,
onmessage(m: WriteSocketRequest) { onmessage(m: WriteSocketRequest) {
self._sockets.get(m.id)?.push(m.data); self._sockets.get(m.id)?.push(m.data);
@ -250,7 +250,7 @@ export default class MultiSocketProxy extends Proxy {
private async _registerCloseSocketMessage(peer: Peer) { private async _registerCloseSocketMessage(peer: Peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: socketEncoding, encoding: socketEncoding,
onmessage(m: CloseSocketRequest) { onmessage(m: CloseSocketRequest) {
self._sockets.get(m.id)?.end(); self._sockets.get(m.id)?.end();
@ -263,7 +263,7 @@ export default class MultiSocketProxy extends Proxy {
private async _registerTimeoutSocketMessage(peer: Peer) { private async _registerTimeoutSocketMessage(peer: Peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: socketEncoding, encoding: socketEncoding,
onmessage(m: SocketRequest) { onmessage(m: SocketRequest) {
// @ts-ignore // @ts-ignore
@ -277,7 +277,7 @@ export default class MultiSocketProxy extends Proxy {
private async _registerErrorSocketMessage(peer: Peer) { private async _registerErrorSocketMessage(peer: Peer) {
const self = this; const self = this;
const message = await peer.channel.addMessage({ const message = peer.channel.addMessage({
encoding: errorSocketEncoding, encoding: errorSocketEncoding,
onmessage(m: ErrorSocketRequest) { onmessage(m: ErrorSocketRequest) {
// @ts-ignore // @ts-ignore

View File

@ -3,9 +3,6 @@ import Socket from "../../socket.js";
import MultiSocketProxy from "../multiSocket.js"; import MultiSocketProxy from "../multiSocket.js";
export default class Peer extends BasePeer { export default class Peer extends BasePeer {
protected async initMessages(): Promise<void> {
await this._proxy.handleNewPeerChannel(this);
}
protected declare _proxy: MultiSocketProxy; protected declare _proxy: MultiSocketProxy;
protected async initSocket() {} protected async initSocket() {}
@ -17,5 +14,8 @@ export default class Peer extends BasePeer {
return this._proxy.handleClosePeer(this); return this._proxy.handleClosePeer(this);
} }
protected async handleChannelOnOpen(m: any): Promise<void> {} protected async handleChannelOnOpen(m: any): Promise<void> {
await this._proxy.handleNewPeerChannel(this);
this._proxy.emit("peerOpen", this);
}
} }