two-pack
v1.0.1
Published
Original: TwoPack (Java) Library rewritten using TypeScript/JavaScript.
Readme
TLS Packet Server & Client Library for Javascript & Typescript projects
Setup TLS fast, secure and easy with TwoPack. This library is for communication between clients and servers using TLS encryption.
2Pack is currently tied to NestJS/common for its logger, but will soon have a version of its own.
Packet
What is a packet? 2Pack is a packet library that allows you to send and receive packets over a network. A packet is a message that is sent over a network. It contains data and metadata that is used to identify the sender and receiver of the packet. 2Pack uses TLS encryption to secure the communication between clients and servers.
Setup
npm install two-packUsage
import { Packet } from 'two-pack';
export class SPacketPing extends Packet {
timestamp: number = 0;
read(reader: PacketReader) {
this.timestamp = reader.readDouble();
}
write(writer: PacketWriter) {
writer.writeDouble(this.timestamp);
}
handle(client: PacketClient) {
client.send(new SPacketPong(this.timestamp));
}
}
export class SPacketPong extends Packet {
timstamp: number = 0;
read(reader: PacketReader) {
this.timestamp = reader.readDouble();
}
write(writer: PacketWriter) {
writer.writeDouble(this.timestamp);
}
handle(client: PacketClient) {
const now = Date.now();
const diff = now - this.timestamp;
this.log(`Response time: ${diff}ms (~${diff / 2}ms to client)`);
}
}Packet Codec
Packet Reader
The PacketReader is responsible for reading the packet data from the buffer.
Packet Writer
The PacketWriter is responsible for writing the packet data to the buffer.
Packet Buffer
A Buffer is a wrapper around a Uint8Array that provides a more convenient interface for reading and writing data. The PacketBuffer class is responsible for reading and writing data from the buffer.
Packet Client
The PacketClient class is responsible for sending and receiving packets over the network. This class would be used on the client side.
Setup
npm install @two-pack/packet-clientimport { PacketClient, PacketRegistry, TLSClientOptions } from 'two-pack';
import { SPacketPing } from './SPacketPing';
export class Client extends PacketClient {
constructor(options: TLSClientOptions) {
const registry = new PacketRegistry();
registry.register(SPacketPing);
super(options, registry)
}
}Packet Server
The PacketServer class is responsible for handling incoming packets and sending responses. This class would be used on the server side.
Setup
npm install @two-pack/packet-serverimport { PacketServer, TLSServerOptions } from '@two-pack/packet-server';
import { SPacketPing } from './SPacketPing';
import { SPacketPong } from './SPacketPong';
import { PacketRegistry, PacketLogger, PacketBuffer } from '@two-pack/packet';
export class Server extends PacketServer {
constructor(options: TLSClientOptions) {
const registry = new PacketRegistry();
registry.register(SPacketPing);
registry.register(SPacketPong);
super(options, registry)
}
}Packet Registry
The PacketRegistry class is responsible for registering and managing packet classes. It is used to map packet IDs to packet classes.
