*add createDefaultMessage option to disable the default socket pipe
This commit is contained in:
parent
27261fedd2
commit
c1d495a54b
12
src/peer.ts
12
src/peer.ts
|
@ -38,6 +38,7 @@ export interface DataSocketOptions {
|
||||||
onclose?: OnClose;
|
onclose?: OnClose;
|
||||||
onchannel?: OnChannel;
|
onchannel?: OnChannel;
|
||||||
emulateWebsocket?: boolean;
|
emulateWebsocket?: boolean;
|
||||||
|
createDefaultMessage?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PeerOptions {
|
export interface PeerOptions {
|
||||||
|
@ -59,6 +60,7 @@ export default class Peer {
|
||||||
private _onclose: OnCloseBound;
|
private _onclose: OnCloseBound;
|
||||||
private _onchannel: OnChannelBound;
|
private _onchannel: OnChannelBound;
|
||||||
private _emulateWebsocket: boolean;
|
private _emulateWebsocket: boolean;
|
||||||
|
private _createDefaultMessage: boolean;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
proxy,
|
proxy,
|
||||||
|
@ -70,6 +72,7 @@ export default class Peer {
|
||||||
onclose,
|
onclose,
|
||||||
onchannel,
|
onchannel,
|
||||||
emulateWebsocket = false,
|
emulateWebsocket = false,
|
||||||
|
createDefaultMessage = true,
|
||||||
}: PeerOptionsWithProxy & DataSocketOptions) {
|
}: PeerOptionsWithProxy & DataSocketOptions) {
|
||||||
this._proxy = proxy;
|
this._proxy = proxy;
|
||||||
this._peer = peer;
|
this._peer = peer;
|
||||||
|
@ -80,6 +83,7 @@ export default 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _socket?: Socket;
|
private _socket?: Socket;
|
||||||
|
@ -96,12 +100,15 @@ export default class Peer {
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
let pipe;
|
||||||
this._socket = new Socket({
|
this._socket = new Socket({
|
||||||
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: any, cb: Function) {
|
async write(data: any, cb: Function) {
|
||||||
|
if (pipe) {
|
||||||
pipe.send(data);
|
pipe.send(data);
|
||||||
|
}
|
||||||
await self._onsend?.(data);
|
await self._onsend?.(data);
|
||||||
cb();
|
cb();
|
||||||
},
|
},
|
||||||
|
@ -133,7 +140,9 @@ export default class Peer {
|
||||||
await self._onclose?.(self._socket);
|
await self._onclose?.(self._socket);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const pipe = this._channel.addMessage({
|
|
||||||
|
if (this._createDefaultMessage) {
|
||||||
|
pipe = this._channel.addMessage({
|
||||||
async onmessage(m: any) {
|
async onmessage(m: any) {
|
||||||
if (m instanceof Uint8Array) {
|
if (m instanceof Uint8Array) {
|
||||||
m = Buffer.from(m);
|
m = Buffer.from(m);
|
||||||
|
@ -142,6 +151,7 @@ export default class Peer {
|
||||||
await self._onreceive?.(m);
|
await self._onreceive?.(m);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await this._onchannel?.(this._channel);
|
await this._onchannel?.(this._channel);
|
||||||
await this._channel.open();
|
await this._channel.open();
|
||||||
|
|
|
@ -24,6 +24,7 @@ export default class Proxy {
|
||||||
listen = false,
|
listen = false,
|
||||||
autostart = false,
|
autostart = false,
|
||||||
emulateWebsocket = false,
|
emulateWebsocket = false,
|
||||||
|
createDefaultMessage = true,
|
||||||
}: ProxyOptions) {
|
}: ProxyOptions) {
|
||||||
this._swarm = swarm;
|
this._swarm = swarm;
|
||||||
this._protocol = protocol;
|
this._protocol = protocol;
|
||||||
|
@ -36,6 +37,7 @@ export default class Proxy {
|
||||||
onclose,
|
onclose,
|
||||||
onchannel,
|
onchannel,
|
||||||
emulateWebsocket,
|
emulateWebsocket,
|
||||||
|
createDefaultMessage,
|
||||||
};
|
};
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue