*If data is a Uint8Array, convert to a buffer

This commit is contained in:
Derrick Hammer 2023-02-06 05:50:48 -05:00
parent 81559342be
commit b4b30cbdea
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 10 additions and 0 deletions

View File

@ -9,6 +9,7 @@
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
"dependencies": { "dependencies": {
"buffer": "^6.0.3",
"compact-encoding": "^2.11.0", "compact-encoding": "^2.11.0",
"protomux": "^3.4.0", "protomux": "^3.4.0",
"streamx": "^2.13.0" "streamx": "^2.13.0"

View File

@ -1,5 +1,6 @@
import Proxy from "./proxy.js"; import Proxy from "./proxy.js";
import Socket from "./socket.js"; import Socket from "./socket.js";
import { Buffer } from "buffer";
export type OnOpen = ( export type OnOpen = (
socket: Socket, socket: Socket,
data: any data: any
@ -70,6 +71,10 @@ export default class Peer {
if (!m) { if (!m) {
m = Buffer.from([]); m = Buffer.from([]);
} }
if (m instanceof Uint8Array) {
m = Buffer.from(m);
}
self._socket = new Socket({ self._socket = new Socket({
remoteAddress: self._peer.rawStream.remoteHost, remoteAddress: self._peer.rawStream.remoteHost,
remotePort: self._peer.rawStream.remotePort, remotePort: self._peer.rawStream.remotePort,
@ -81,6 +86,7 @@ export default class Peer {
// @ts-ignore // @ts-ignore
self._socket.emit("connect"); self._socket.emit("connect");
} }
self._socket.emit("data", m); self._socket.emit("data", m);
}, },
async onclose() { async onclose() {
@ -90,6 +96,9 @@ export default class Peer {
}); });
const pipe = channel.addMessage({ const pipe = channel.addMessage({
async onmessage(m: any) { async onmessage(m: any) {
if (m instanceof Uint8Array) {
m = Buffer.from(m);
}
self._socket.emit("data", m); self._socket.emit("data", m);
await self._onreceive?.(m); await self._onreceive?.(m);
}, },