-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
binary-port-access/src/communication/wasm32/node_tcp_helper.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#[cfg(target_arch = "wasm32")] | ||
pub(crate) const NODE_TCP_HELPER: &str = r#" | ||
(async () => {{ | ||
try {{ | ||
const net = require('net'); | ||
const client = new net.Socket(); | ||
const payload = Buffer.from({buffer_payload}); | ||
const host = '{host}'; | ||
const port = '{port}'; | ||
return new Promise((resolve, reject) => {{ | ||
// console.log('Connecting to TCP server at', host, port); | ||
const lengthBuffer = Buffer.alloc(4); | ||
lengthBuffer.writeUInt32LE(payload.length); | ||
client.connect(parseInt(port), host, () => {{ | ||
// console.log('Connected to TCP server'); | ||
// First, send the length of the payload | ||
client.write(lengthBuffer, (err) => {{ | ||
if (err) {{ | ||
console.error('Error sending length:', err.message); | ||
client.destroy(); | ||
return; | ||
}} | ||
// console.log('Length of payload sent'); | ||
// Now, send the actual payload | ||
client.write(payload, (err) => {{ | ||
if (err) {{ | ||
console.error('Error sending payload:', err.message); | ||
}} else {{ | ||
// console.log('Payload sent'); | ||
}} | ||
}}); | ||
}}); | ||
}}); | ||
client.on('data', (data) => {{ | ||
// console.log('Data received from server:', data); | ||
resolve(data); | ||
client.destroy(); // Close connection after receiving response | ||
}}); | ||
client.on('error', (err) => {{ | ||
console.error('TCP connection error:', err.message); | ||
reject(new Error('TCP connection error: ' + err.message)); | ||
}}); | ||
client.on('close', () => {{ | ||
// console.log('TCP connection closed'); | ||
}}); | ||
}}); | ||
}} catch (err) {{ | ||
console.error('Error in TCP script:', err.message); | ||
throw new Error('Script execution error: ' + err.message); | ||
}} | ||
}} | ||
)();"#; | ||
|
||
pub(crate) fn sanitize_input(input: &str) -> String { | ||
input | ||
.replace("\\", "\\\\") // Escape backslashes | ||
.replace("'", "\\'") // Escape single quotes | ||
.replace("\"", "\\\"") // Escape double quotes | ||
.replace("\n", "\\n") // Escape newlines | ||
.replace("\r", "\\r") // Escape carriage returns | ||
.replace("<", "\\<") // Escape less than | ||
.replace(">", "\\>") // Escape greater than | ||
} |