*Update dist
This commit is contained in:
parent
c1d495a54b
commit
8e0edc4bcd
|
@ -27,6 +27,7 @@ export interface DataSocketOptions {
|
||||||
onclose?: OnClose;
|
onclose?: OnClose;
|
||||||
onchannel?: OnChannel;
|
onchannel?: OnChannel;
|
||||||
emulateWebsocket?: boolean;
|
emulateWebsocket?: boolean;
|
||||||
|
createDefaultMessage?: boolean;
|
||||||
}
|
}
|
||||||
export interface PeerOptions {
|
export interface PeerOptions {
|
||||||
peer: any;
|
peer: any;
|
||||||
|
@ -45,7 +46,8 @@ export default class Peer {
|
||||||
private _onclose;
|
private _onclose;
|
||||||
private _onchannel;
|
private _onchannel;
|
||||||
private _emulateWebsocket;
|
private _emulateWebsocket;
|
||||||
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket, }: PeerOptionsWithProxy & DataSocketOptions);
|
private _createDefaultMessage;
|
||||||
|
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket, createDefaultMessage, }: PeerOptionsWithProxy & DataSocketOptions);
|
||||||
private _socket?;
|
private _socket?;
|
||||||
get socket(): Socket;
|
get socket(): Socket;
|
||||||
private _channel?;
|
private _channel?;
|
||||||
|
|
|
@ -15,7 +15,8 @@ class Peer {
|
||||||
_onclose;
|
_onclose;
|
||||||
_onchannel;
|
_onchannel;
|
||||||
_emulateWebsocket;
|
_emulateWebsocket;
|
||||||
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket = false, }) {
|
_createDefaultMessage;
|
||||||
|
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket = false, createDefaultMessage = true, }) {
|
||||||
this._proxy = proxy;
|
this._proxy = proxy;
|
||||||
this._peer = peer;
|
this._peer = peer;
|
||||||
this._muxer = muxer;
|
this._muxer = muxer;
|
||||||
|
@ -25,6 +26,7 @@ class Peer {
|
||||||
this._onclose = onclose?.bind(undefined, this);
|
this._onclose = onclose?.bind(undefined, this);
|
||||||
this._onchannel = onchannel?.bind(undefined, this);
|
this._onchannel = onchannel?.bind(undefined, this);
|
||||||
this._emulateWebsocket = emulateWebsocket;
|
this._emulateWebsocket = emulateWebsocket;
|
||||||
|
this._createDefaultMessage = createDefaultMessage;
|
||||||
}
|
}
|
||||||
_socket;
|
_socket;
|
||||||
get socket() {
|
get socket() {
|
||||||
|
@ -36,12 +38,15 @@ class Peer {
|
||||||
}
|
}
|
||||||
async init() {
|
async init() {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
let pipe;
|
||||||
this._socket = new socket_js_1.default({
|
this._socket = new socket_js_1.default({
|
||||||
remoteAddress: self._peer.rawStream.remoteHost,
|
remoteAddress: self._peer.rawStream.remoteHost,
|
||||||
remotePort: self._peer.rawStream.remotePort,
|
remotePort: self._peer.rawStream.remotePort,
|
||||||
remotePublicKey: self._peer.remotePublicKey,
|
remotePublicKey: self._peer.remotePublicKey,
|
||||||
async write(data, cb) {
|
async write(data, cb) {
|
||||||
pipe.send(data);
|
if (pipe) {
|
||||||
|
pipe.send(data);
|
||||||
|
}
|
||||||
await self._onsend?.(data);
|
await self._onsend?.(data);
|
||||||
cb();
|
cb();
|
||||||
},
|
},
|
||||||
|
@ -69,15 +74,17 @@ class Peer {
|
||||||
await self._onclose?.(self._socket);
|
await self._onclose?.(self._socket);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const pipe = this._channel.addMessage({
|
if (this._createDefaultMessage) {
|
||||||
async onmessage(m) {
|
pipe = this._channel.addMessage({
|
||||||
if (m instanceof Uint8Array) {
|
async onmessage(m) {
|
||||||
m = buffer_1.Buffer.from(m);
|
if (m instanceof Uint8Array) {
|
||||||
}
|
m = buffer_1.Buffer.from(m);
|
||||||
self._socket.emit("data", m);
|
}
|
||||||
await self._onreceive?.(m);
|
self._socket.emit("data", m);
|
||||||
},
|
await self._onreceive?.(m);
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
await this._onchannel?.(this._channel);
|
await this._onchannel?.(this._channel);
|
||||||
await this._channel.open();
|
await this._channel.open();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ export default class Proxy {
|
||||||
private _listen;
|
private _listen;
|
||||||
private _socketOptions;
|
private _socketOptions;
|
||||||
private _autostart;
|
private _autostart;
|
||||||
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen, autostart, emulateWebsocket, }: ProxyOptions);
|
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen, autostart, emulateWebsocket, createDefaultMessage, }: ProxyOptions);
|
||||||
private _swarm;
|
private _swarm;
|
||||||
get swarm(): any;
|
get swarm(): any;
|
||||||
private _protocol;
|
private _protocol;
|
||||||
|
|
|
@ -9,7 +9,7 @@ class Proxy {
|
||||||
_listen;
|
_listen;
|
||||||
_socketOptions;
|
_socketOptions;
|
||||||
_autostart;
|
_autostart;
|
||||||
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen = false, autostart = false, emulateWebsocket = false, }) {
|
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen = false, autostart = false, emulateWebsocket = false, createDefaultMessage = true, }) {
|
||||||
this._swarm = swarm;
|
this._swarm = swarm;
|
||||||
this._protocol = protocol;
|
this._protocol = protocol;
|
||||||
this._listen = listen;
|
this._listen = listen;
|
||||||
|
@ -21,6 +21,7 @@ class Proxy {
|
||||||
onclose,
|
onclose,
|
||||||
onchannel,
|
onchannel,
|
||||||
emulateWebsocket,
|
emulateWebsocket,
|
||||||
|
createDefaultMessage,
|
||||||
};
|
};
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue