Compare commits
2 Commits
27261fedd2
...
8e0edc4bcd
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 8e0edc4bcd | |
Derrick Hammer | c1d495a54b |
|
@ -27,6 +27,7 @@ export interface DataSocketOptions {
|
|||
onclose?: OnClose;
|
||||
onchannel?: OnChannel;
|
||||
emulateWebsocket?: boolean;
|
||||
createDefaultMessage?: boolean;
|
||||
}
|
||||
export interface PeerOptions {
|
||||
peer: any;
|
||||
|
@ -45,7 +46,8 @@ export default class Peer {
|
|||
private _onclose;
|
||||
private _onchannel;
|
||||
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?;
|
||||
get socket(): Socket;
|
||||
private _channel?;
|
||||
|
|
|
@ -15,7 +15,8 @@ class Peer {
|
|||
_onclose;
|
||||
_onchannel;
|
||||
_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._peer = peer;
|
||||
this._muxer = muxer;
|
||||
|
@ -25,6 +26,7 @@ class Peer {
|
|||
this._onclose = onclose?.bind(undefined, this);
|
||||
this._onchannel = onchannel?.bind(undefined, this);
|
||||
this._emulateWebsocket = emulateWebsocket;
|
||||
this._createDefaultMessage = createDefaultMessage;
|
||||
}
|
||||
_socket;
|
||||
get socket() {
|
||||
|
@ -36,12 +38,15 @@ class Peer {
|
|||
}
|
||||
async init() {
|
||||
const self = this;
|
||||
let pipe;
|
||||
this._socket = new socket_js_1.default({
|
||||
remoteAddress: self._peer.rawStream.remoteHost,
|
||||
remotePort: self._peer.rawStream.remotePort,
|
||||
remotePublicKey: self._peer.remotePublicKey,
|
||||
async write(data, cb) {
|
||||
if (pipe) {
|
||||
pipe.send(data);
|
||||
}
|
||||
await self._onsend?.(data);
|
||||
cb();
|
||||
},
|
||||
|
@ -69,7 +74,8 @@ class Peer {
|
|||
await self._onclose?.(self._socket);
|
||||
},
|
||||
});
|
||||
const pipe = this._channel.addMessage({
|
||||
if (this._createDefaultMessage) {
|
||||
pipe = this._channel.addMessage({
|
||||
async onmessage(m) {
|
||||
if (m instanceof Uint8Array) {
|
||||
m = buffer_1.Buffer.from(m);
|
||||
|
@ -78,6 +84,7 @@ class Peer {
|
|||
await self._onreceive?.(m);
|
||||
},
|
||||
});
|
||||
}
|
||||
await this._onchannel?.(this._channel);
|
||||
await this._channel.open();
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ export default class Proxy {
|
|||
private _listen;
|
||||
private _socketOptions;
|
||||
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;
|
||||
get swarm(): any;
|
||||
private _protocol;
|
||||
|
|
|
@ -9,7 +9,7 @@ class Proxy {
|
|||
_listen;
|
||||
_socketOptions;
|
||||
_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._protocol = protocol;
|
||||
this._listen = listen;
|
||||
|
@ -21,6 +21,7 @@ class Proxy {
|
|||
onclose,
|
||||
onchannel,
|
||||
emulateWebsocket,
|
||||
createDefaultMessage,
|
||||
};
|
||||
this.init();
|
||||
}
|
||||
|
|
12
src/peer.ts
12
src/peer.ts
|
@ -38,6 +38,7 @@ export interface DataSocketOptions {
|
|||
onclose?: OnClose;
|
||||
onchannel?: OnChannel;
|
||||
emulateWebsocket?: boolean;
|
||||
createDefaultMessage?: boolean;
|
||||
}
|
||||
|
||||
export interface PeerOptions {
|
||||
|
@ -59,6 +60,7 @@ export default class Peer {
|
|||
private _onclose: OnCloseBound;
|
||||
private _onchannel: OnChannelBound;
|
||||
private _emulateWebsocket: boolean;
|
||||
private _createDefaultMessage: boolean;
|
||||
|
||||
constructor({
|
||||
proxy,
|
||||
|
@ -70,6 +72,7 @@ export default class Peer {
|
|||
onclose,
|
||||
onchannel,
|
||||
emulateWebsocket = false,
|
||||
createDefaultMessage = true,
|
||||
}: PeerOptionsWithProxy & DataSocketOptions) {
|
||||
this._proxy = proxy;
|
||||
this._peer = peer;
|
||||
|
@ -80,6 +83,7 @@ export default class Peer {
|
|||
this._onclose = onclose?.bind(undefined, this);
|
||||
this._onchannel = onchannel?.bind(undefined, this);
|
||||
this._emulateWebsocket = emulateWebsocket;
|
||||
this._createDefaultMessage = createDefaultMessage;
|
||||
}
|
||||
|
||||
private _socket?: Socket;
|
||||
|
@ -96,12 +100,15 @@ export default class Peer {
|
|||
|
||||
async init() {
|
||||
const self = this;
|
||||
let pipe;
|
||||
this._socket = new Socket({
|
||||
remoteAddress: self._peer.rawStream.remoteHost,
|
||||
remotePort: self._peer.rawStream.remotePort,
|
||||
remotePublicKey: self._peer.remotePublicKey,
|
||||
async write(data: any, cb: Function) {
|
||||
if (pipe) {
|
||||
pipe.send(data);
|
||||
}
|
||||
await self._onsend?.(data);
|
||||
cb();
|
||||
},
|
||||
|
@ -133,7 +140,9 @@ export default class Peer {
|
|||
await self._onclose?.(self._socket);
|
||||
},
|
||||
});
|
||||
const pipe = this._channel.addMessage({
|
||||
|
||||
if (this._createDefaultMessage) {
|
||||
pipe = this._channel.addMessage({
|
||||
async onmessage(m: any) {
|
||||
if (m instanceof Uint8Array) {
|
||||
m = Buffer.from(m);
|
||||
|
@ -142,6 +151,7 @@ export default class Peer {
|
|||
await self._onreceive?.(m);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await this._onchannel?.(this._channel);
|
||||
await this._channel.open();
|
||||
|
|
|
@ -24,6 +24,7 @@ export default class Proxy {
|
|||
listen = false,
|
||||
autostart = false,
|
||||
emulateWebsocket = false,
|
||||
createDefaultMessage = true,
|
||||
}: ProxyOptions) {
|
||||
this._swarm = swarm;
|
||||
this._protocol = protocol;
|
||||
|
@ -36,6 +37,7 @@ export default class Proxy {
|
|||
onclose,
|
||||
onchannel,
|
||||
emulateWebsocket,
|
||||
createDefaultMessage,
|
||||
};
|
||||
this.init();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue