two-pack 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,114 @@
1
+ ## TLS Packet Server & Client Library for Javascript & Typescript projects
2
+
3
+ Setup TLS fast, secure and easy with TwoPack. This library is for communication between clients and servers using TLS encryption.
4
+
5
+ 2Pack is currently tied to NestJS/common for its logger, but will soon have a version of its own.
6
+
7
+ # Packet
8
+ 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.
9
+
10
+ ### Setup
11
+
12
+ ```aiignore
13
+ npm install two-pack
14
+ ```
15
+
16
+ ### Usage
17
+
18
+ ```ts
19
+ import { Packet } from 'two-pack';
20
+
21
+ export class SPacketPing extends Packet {
22
+ timestamp: number = 0;
23
+
24
+ read(reader: PacketReader) {
25
+ this.timestamp = reader.readDouble();
26
+ }
27
+
28
+ write(writer: PacketWriter) {
29
+ writer.writeDouble(this.timestamp);
30
+ }
31
+
32
+ handle(client: PacketClient) {
33
+ client.send(new SPacketPong(this.timestamp));
34
+ }
35
+
36
+ }
37
+
38
+ export class SPacketPong extends Packet {
39
+ timstamp: number = 0;
40
+
41
+ read(reader: PacketReader) {
42
+ this.timestamp = reader.readDouble();
43
+ }
44
+
45
+ write(writer: PacketWriter) {
46
+ writer.writeDouble(this.timestamp);
47
+ }
48
+
49
+ handle(client: PacketClient) {
50
+ const now = Date.now();
51
+ const diff = now - this.timestamp;
52
+ this.log(`Response time: ${diff}ms (~${diff / 2}ms to client)`);
53
+ }
54
+ }
55
+ ```
56
+
57
+ # Packet Codec
58
+
59
+ ### Packet Reader
60
+ The PacketReader is responsible for reading the packet data from the buffer.
61
+ ### Packet Writer
62
+ The PacketWriter is responsible for writing the packet data to the buffer.
63
+
64
+ # Packet Buffer
65
+ 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.
66
+
67
+ # Packet Client
68
+ The PacketClient class is responsible for sending and receiving packets over the network. This class would be used on the client side.
69
+
70
+ ### Setup
71
+ ```aiignore
72
+ npm install @two-pack/packet-client
73
+ ```
74
+
75
+ ```ts
76
+ import { PacketClient, PacketRegistry, TLSClientOptions } from 'two-pack';
77
+ import { SPacketPing } from './SPacketPing';
78
+
79
+ export class Client extends PacketClient {
80
+ constructor(options: TLSClientOptions) {
81
+ const registry = new PacketRegistry();
82
+ registry.register(SPacketPing);
83
+ super(options, registry)
84
+ }
85
+ }
86
+ ```
87
+
88
+ # Packet Server
89
+ The PacketServer class is responsible for handling incoming packets and sending responses. This class would be used on the server side.
90
+
91
+ ### Setup
92
+ ```aiignore
93
+ npm install @two-pack/packet-server
94
+ ```
95
+
96
+ ```ts
97
+ import { PacketServer, TLSServerOptions } from '@two-pack/packet-server';
98
+ import { SPacketPing } from './SPacketPing';
99
+ import { SPacketPong } from './SPacketPong';
100
+ import { PacketRegistry, PacketLogger, PacketBuffer } from '@two-pack/packet';
101
+
102
+ export class Server extends PacketServer {
103
+ constructor(options: TLSClientOptions) {
104
+ const registry = new PacketRegistry();
105
+ registry.register(SPacketPing);
106
+ registry.register(SPacketPong);
107
+ super(options, registry)
108
+ }
109
+ }
110
+ ```
111
+
112
+ # Packet Registry
113
+ The PacketRegistry class is responsible for registering and managing packet classes. It is used to map packet IDs to packet classes.
114
+
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=packet-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packet-server.d.ts","sourceRoot":"","sources":["../src/packet-server.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/packet.js CHANGED
@@ -25,7 +25,7 @@ class Packet {
25
25
  return stripped.toLowerCase();
26
26
  }
27
27
  log(...args) {
28
- this.logger.verbose((this.constructor.name.startsWith('S') ? '[CLIENT] ' : '[SERVER] ') +
28
+ this.logger.debug((this.constructor.name.startsWith('S') ? '[CLIENT] ' : '[SERVER] ') +
29
29
  args);
30
30
  }
31
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "two-pack",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Original: TwoPack (Java) Library rewritten using TypeScript/JavaScript.",
5
5
  "author": "Tristan Landry",
6
6
  "main": "./dist/index.js",