Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
A base class for tcp client with basic functions
$ npm install tcp-base --save
Node.js >= 4.0.0 required
A quick guide to implement a tcp echo client
Client:
'use strict';
const TCPBase = require('tcp-base');
/**
* A Simple Protocol:
* (4B): request id
* (4B): body length
* ------------------------------
* body data
*/
class Client extends TCPBase {
getHeader() {
return this.read(8);
}
getBodyLength(header) {
return header.readInt32BE(4);
}
decode(body, header) {
return {
id: header.readInt32BE(0),
data: body,
};
}
// heartbeat packet
get heartBeatPacket() {
return new Buffer([ 255, 255, 255, 255, 0, 0, 0, 0 ]);
}
}
const client = new Client({
host: '127.0.0.1',
port: 8080,
});
const body = new Buffer('hello');
const data = new Buffer(8 + body.length);
data.writeInt32BE(1, 0);
data.writeInt32BE(body.length, 4);
body.copy(data, 8, 0);
client.send({
id: 1,
data,
timeout: 5000,
}, (err, res) => {
if (err) {
console.error(err);
}
console.log(res.toString()); // should echo 'hello'
});
Server:
'use strict';
const net = require('net');
const server = net.createServer(socket => {
let header;
let bodyLen;
function readPacket() {
if (bodyLen == null) {
header = socket.read(8);
if (!header) {
return false;
}
bodyLen = header.readInt32BE(4);
}
if (bodyLen === 0) {
socket.write(header);
} else {
const body = socket.read(bodyLen);
if (!body) {
return false;
}
socket.write(Buffer.concat([ header, body ]));
}
bodyLen = null;
return true;
}
socket.on('readable', () => {
try {
let remaining = false;
do {
remaining = readPacket();
}
while (remaining);
} catch (err) {
console.error(err);
}
});
});
server.listen(8080);
FAQs
A base class for tcp client with basic functions
The npm package tcp-base receives a total of 20,032 weekly downloads. As such, tcp-base popularity was classified as popular.
We found that tcp-base demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.