@derivesome/sockets 1.0.0
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/.package.json.~undo-tree~ +40 -0
- package/.tsconfig.cjs.json.~undo-tree~ +5 -0
- package/.tsconfig.json.~undo-tree~ +6 -0
- package/dist/cjs/env.d.ts +2 -0
- package/dist/cjs/env.d.ts.map +1 -0
- package/dist/cjs/env.js +11 -0
- package/dist/cjs/env.js.map +1 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/server/app.d.ts +20 -0
- package/dist/cjs/server/app.d.ts.map +1 -0
- package/dist/cjs/server/app.js +153 -0
- package/dist/cjs/server/app.js.map +1 -0
- package/dist/cjs/server/events.d.ts +41 -0
- package/dist/cjs/server/events.d.ts.map +1 -0
- package/dist/cjs/server/events.js +3 -0
- package/dist/cjs/server/events.js.map +1 -0
- package/dist/cjs/server/index.d.ts +5 -0
- package/dist/cjs/server/index.d.ts.map +1 -0
- package/dist/cjs/server/index.js +21 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/server/plugin.d.ts +8 -0
- package/dist/cjs/server/plugin.d.ts.map +1 -0
- package/dist/cjs/server/plugin.js +3 -0
- package/dist/cjs/server/plugin.js.map +1 -0
- package/dist/cjs/server/server.d.ts +13 -0
- package/dist/cjs/server/server.d.ts.map +1 -0
- package/dist/cjs/server/server.js +54 -0
- package/dist/cjs/server/server.js.map +1 -0
- package/dist/cjs/socket.d.ts +17 -0
- package/dist/cjs/socket.d.ts.map +1 -0
- package/dist/cjs/socket.js +53 -0
- package/dist/cjs/socket.js.map +1 -0
- package/dist/cjs/utils.d.ts +2 -0
- package/dist/cjs/utils.d.ts.map +1 -0
- package/dist/cjs/utils.js +12 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/env.d.ts +2 -0
- package/dist/esm/env.d.ts.map +1 -0
- package/dist/esm/env.js +7 -0
- package/dist/esm/env.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/server/app.d.ts +20 -0
- package/dist/esm/server/app.d.ts.map +1 -0
- package/dist/esm/server/app.js +149 -0
- package/dist/esm/server/app.js.map +1 -0
- package/dist/esm/server/events.d.ts +41 -0
- package/dist/esm/server/events.d.ts.map +1 -0
- package/dist/esm/server/events.js +2 -0
- package/dist/esm/server/events.js.map +1 -0
- package/dist/esm/server/index.d.ts +5 -0
- package/dist/esm/server/index.d.ts.map +1 -0
- package/dist/esm/server/index.js +5 -0
- package/dist/esm/server/index.js.map +1 -0
- package/dist/esm/server/plugin.d.ts +8 -0
- package/dist/esm/server/plugin.d.ts.map +1 -0
- package/dist/esm/server/plugin.js +2 -0
- package/dist/esm/server/plugin.js.map +1 -0
- package/dist/esm/server/server.d.ts +13 -0
- package/dist/esm/server/server.d.ts.map +1 -0
- package/dist/esm/server/server.js +47 -0
- package/dist/esm/server/server.js.map +1 -0
- package/dist/esm/socket.d.ts +17 -0
- package/dist/esm/socket.d.ts.map +1 -0
- package/dist/esm/socket.js +49 -0
- package/dist/esm/socket.js.map +1 -0
- package/dist/esm/utils.d.ts +2 -0
- package/dist/esm/utils.d.ts.map +1 -0
- package/dist/esm/utils.js +8 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +63 -0
- package/package.json~ +62 -0
- package/src/#socket-impl.ts# +4 -0
- package/src/.channel.ts.~undo-tree~ +12 -0
- package/src/.env.ts.~undo-tree~ +7 -0
- package/src/.index.ts.~undo-tree~ +12 -0
- package/src/.server.ts.~undo-tree~ +337 -0
- package/src/.socket-impl.ts.~undo-tree~ +9 -0
- package/src/.socket.ts.~undo-tree~ +196 -0
- package/src/.utils.ts.~undo-tree~ +6 -0
- package/src/channel.ts~ +14 -0
- package/src/env.ts +5 -0
- package/src/env.ts~ +5 -0
- package/src/index.ts +3 -0
- package/src/index.ts~ +2 -0
- package/src/server/.app.test.ts.~undo-tree~ +92 -0
- package/src/server/.app.ts.~undo-tree~ +283 -0
- package/src/server/.events.ts.~undo-tree~ +248 -0
- package/src/server/.index.ts.~undo-tree~ +10 -0
- package/src/server/.plugin.ts.~undo-tree~ +16 -0
- package/src/server/.server.ts.~undo-tree~ +183 -0
- package/src/server/app.test.ts +51 -0
- package/src/server/app.test.ts~ +51 -0
- package/src/server/app.ts +171 -0
- package/src/server/app.ts~ +171 -0
- package/src/server/events.ts +41 -0
- package/src/server/events.ts~ +47 -0
- package/src/server/index.ts +4 -0
- package/src/server/index.ts~ +1 -0
- package/src/server/plugin.ts +8 -0
- package/src/server/plugin.ts~ +8 -0
- package/src/server/server.ts +62 -0
- package/src/server/server.ts~ +62 -0
- package/src/server.ts~ +73 -0
- package/src/socket-impl.ts~ +5 -0
- package/src/socket.ts +66 -0
- package/src/socket.ts~ +66 -0
- package/src/utils.ts +7 -0
- package/src/utils.ts~ +7 -0
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.cjs.json~ +0 -0
- package/tsconfig.json +24 -0
- package/tsconfig.json~ +24 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Server } from "./server";
|
|
2
|
+
import { WebSocketServer } from "ws";
|
|
3
|
+
import { PubSub, pubsub } from "@derivesome/core";
|
|
4
|
+
import { ServerEvent } from "./events";
|
|
5
|
+
import { Socket } from "../socket";
|
|
6
|
+
import { BinaryBuffer, BinaryType, serial } from "binencode";
|
|
7
|
+
import { Plugin } from "./plugin";
|
|
8
|
+
|
|
9
|
+
export type AppConfig = {
|
|
10
|
+
host: string;
|
|
11
|
+
port: number;
|
|
12
|
+
certPath?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class App {
|
|
16
|
+
config: AppConfig;
|
|
17
|
+
server: Server;
|
|
18
|
+
ps: PubSub<ServerEvent>;
|
|
19
|
+
plugins: Map<string, Plugin> = new Map();
|
|
20
|
+
|
|
21
|
+
constructor(cfg: AppConfig) {
|
|
22
|
+
this.config = cfg;
|
|
23
|
+
this.server = new Server(cfg);
|
|
24
|
+
this.ps = pubsub();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
use(plugin: Plugin) {
|
|
28
|
+
this.plugins.set(plugin.name, plugin);
|
|
29
|
+
this.ps.subscribe(async (event) => {
|
|
30
|
+
if (event.type === 'client-message') {
|
|
31
|
+
const data = event.message;
|
|
32
|
+
if (data.type === BinaryType.OBJECT) {
|
|
33
|
+
const channelBin = data.data.get('channel');
|
|
34
|
+
if (channelBin && channelBin.type === BinaryType.STRING) {
|
|
35
|
+
const channel = channelBin.data.with((buff) => buff.readString(channelBin.count));
|
|
36
|
+
if (channel !== plugin.name) return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
await plugin.handler(event);
|
|
42
|
+
}, async () => {
|
|
43
|
+
if (plugin.onDestroy) {
|
|
44
|
+
await plugin.onDestroy();
|
|
45
|
+
}
|
|
46
|
+
this.plugins.delete(plugin.name);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private init(ws: WebSocketServer) {
|
|
51
|
+
ws.addListener("listening", async () => {
|
|
52
|
+
await this.ps.publishAsync({
|
|
53
|
+
type: "server-ready",
|
|
54
|
+
app: this,
|
|
55
|
+
ws,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
ws.addListener("headers", async (headers, req) => {
|
|
59
|
+
await this.ps.publishAsync({
|
|
60
|
+
type: "server-headers",
|
|
61
|
+
app: this,
|
|
62
|
+
ws,
|
|
63
|
+
req,
|
|
64
|
+
headers,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
ws.addListener("connection", async (client, req) => {
|
|
68
|
+
const socket = new Socket(() => client);
|
|
69
|
+
await this.ps.publishAsync({
|
|
70
|
+
type: "client-connection",
|
|
71
|
+
app: this,
|
|
72
|
+
ws,
|
|
73
|
+
req,
|
|
74
|
+
socket,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
socket.connection.addEventListener("open", async (ev) => {
|
|
78
|
+
await this.ps.publishAsync({
|
|
79
|
+
type: "client-open",
|
|
80
|
+
app: this,
|
|
81
|
+
ws,
|
|
82
|
+
event: ev,
|
|
83
|
+
socket,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
socket.connection.addEventListener("close", async (ev) => {
|
|
88
|
+
await this.ps.publishAsync({
|
|
89
|
+
type: "client-close",
|
|
90
|
+
app: this,
|
|
91
|
+
ws,
|
|
92
|
+
event: ev,
|
|
93
|
+
socket,
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
socket.connection.addEventListener("error", async (ev) => {
|
|
98
|
+
await this.ps.publishAsync({
|
|
99
|
+
type: "client-error",
|
|
100
|
+
app: this,
|
|
101
|
+
ws,
|
|
102
|
+
event: ev,
|
|
103
|
+
socket,
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
socket.connection.on("upgrade", async (req) => {
|
|
108
|
+
await this.ps.publishAsync({
|
|
109
|
+
type: "client-upgrade",
|
|
110
|
+
app: this,
|
|
111
|
+
ws,
|
|
112
|
+
req,
|
|
113
|
+
socket,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
socket.connection.on("message", async (data, isBinary) => {
|
|
118
|
+
await this.ps.publishAsync({
|
|
119
|
+
type: "client-message-raw",
|
|
120
|
+
app: this,
|
|
121
|
+
isBinary,
|
|
122
|
+
message: data,
|
|
123
|
+
socket,
|
|
124
|
+
ws,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
let buffer;
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
buffer = new BinaryBuffer();
|
|
131
|
+
buffer.data = new Uint8Array(data as any);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
console.error(e);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!buffer) {
|
|
137
|
+
console.error("Invalid client message");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let decoded;
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
decoded = serial.decode(buffer);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
console.error(e);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!decoded) return;
|
|
150
|
+
|
|
151
|
+
await this.ps.publishAsync({
|
|
152
|
+
type: "client-message",
|
|
153
|
+
app: this,
|
|
154
|
+
message: decoded,
|
|
155
|
+
socket,
|
|
156
|
+
ws,
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
start() {
|
|
163
|
+
const { certPath } = this.config;
|
|
164
|
+
|
|
165
|
+
if (certPath) {
|
|
166
|
+
this.init(this.server.startSecure(certPath));
|
|
167
|
+
} else {
|
|
168
|
+
this.init(this.server.start());
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Server } from "./server";
|
|
2
|
+
import { WebSocketServer } from "ws";
|
|
3
|
+
import { PubSub, pubsub } from "@derivesome/core";
|
|
4
|
+
import { ServerEvent } from "./events";
|
|
5
|
+
import { Socket } from "../socket";
|
|
6
|
+
import { BinaryBuffer, BinaryType, serial } from "binencode";
|
|
7
|
+
import { Plugin } from "./plugin";
|
|
8
|
+
|
|
9
|
+
export type AppConfig = {
|
|
10
|
+
host: string;
|
|
11
|
+
port: number;
|
|
12
|
+
certPath?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class App {
|
|
16
|
+
config: AppConfig;
|
|
17
|
+
server: Server;
|
|
18
|
+
ps: PubSub<ServerEvent>;
|
|
19
|
+
plugins: Map<string, Plugin> = new Map();
|
|
20
|
+
|
|
21
|
+
constructor(cfg: AppConfig) {
|
|
22
|
+
this.config = cfg;
|
|
23
|
+
this.server = new Server(cfg);
|
|
24
|
+
this.ps = pubsub();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
use(plugin: Plugin) {
|
|
28
|
+
this.plugins.set(plugin.name, plugin);
|
|
29
|
+
this.ps.subscribe(async (event) => {
|
|
30
|
+
if (event.type === 'client-message') {
|
|
31
|
+
const data = event.message;
|
|
32
|
+
if (data.type === BinaryType.OBJECT) {
|
|
33
|
+
const channelBin = data.data.get('channel');
|
|
34
|
+
if (channelBin && channelBin.type === BinaryType.STRING) {
|
|
35
|
+
const channel = channelBin.data.with((buff) => buff.readString(channelBin.count));
|
|
36
|
+
if (channel !== plugin.name) return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
await plugin.handler(event);
|
|
42
|
+
}, async () => {
|
|
43
|
+
if (plugin.onDestroy) {
|
|
44
|
+
await plugin.onDestroy();
|
|
45
|
+
}
|
|
46
|
+
this.plugins.delete(plugin.name);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private init(ws: WebSocketServer) {
|
|
51
|
+
ws.addListener("listening", async () => {
|
|
52
|
+
await this.ps.publishAsync({
|
|
53
|
+
type: "server-ready",
|
|
54
|
+
app: this,
|
|
55
|
+
ws,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
ws.addListener("headers", async (headers, req) => {
|
|
59
|
+
await this.ps.publishAsync({
|
|
60
|
+
type: "server-headers",
|
|
61
|
+
app: this,
|
|
62
|
+
ws,
|
|
63
|
+
req,
|
|
64
|
+
headers,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
ws.addListener("connection", async (client, req) => {
|
|
68
|
+
const socket = new Socket(client);
|
|
69
|
+
await this.ps.publishAsync({
|
|
70
|
+
type: "client-connection",
|
|
71
|
+
app: this,
|
|
72
|
+
ws,
|
|
73
|
+
req,
|
|
74
|
+
socket,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
socket.connection.addEventListener("open", async (ev) => {
|
|
78
|
+
await this.ps.publishAsync({
|
|
79
|
+
type: "client-open",
|
|
80
|
+
app: this,
|
|
81
|
+
ws,
|
|
82
|
+
event: ev,
|
|
83
|
+
socket,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
socket.connection.addEventListener("close", async (ev) => {
|
|
88
|
+
await this.ps.publishAsync({
|
|
89
|
+
type: "client-close",
|
|
90
|
+
app: this,
|
|
91
|
+
ws,
|
|
92
|
+
event: ev,
|
|
93
|
+
socket,
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
socket.connection.addEventListener("error", async (ev) => {
|
|
98
|
+
await this.ps.publishAsync({
|
|
99
|
+
type: "client-error",
|
|
100
|
+
app: this,
|
|
101
|
+
ws,
|
|
102
|
+
event: ev,
|
|
103
|
+
socket,
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
socket.connection.on("upgrade", async (req) => {
|
|
108
|
+
await this.ps.publishAsync({
|
|
109
|
+
type: "client-upgrade",
|
|
110
|
+
app: this,
|
|
111
|
+
ws,
|
|
112
|
+
req,
|
|
113
|
+
socket,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
socket.connection.on("message", async (data, isBinary) => {
|
|
118
|
+
await this.ps.publishAsync({
|
|
119
|
+
type: "client-message-raw",
|
|
120
|
+
app: this,
|
|
121
|
+
isBinary,
|
|
122
|
+
message: data,
|
|
123
|
+
socket,
|
|
124
|
+
ws,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
let buffer;
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
buffer = new BinaryBuffer();
|
|
131
|
+
buffer.data = new Uint8Array(data as any);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
console.error(e);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!buffer) {
|
|
137
|
+
console.error("Invalid client message");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let decoded;
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
decoded = serial.decode(buffer);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
console.error(e);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!decoded) return;
|
|
150
|
+
|
|
151
|
+
await this.ps.publishAsync({
|
|
152
|
+
type: "client-message",
|
|
153
|
+
app: this,
|
|
154
|
+
message: decoded,
|
|
155
|
+
socket,
|
|
156
|
+
ws,
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
start() {
|
|
163
|
+
const { certPath } = this.config;
|
|
164
|
+
|
|
165
|
+
if (certPath) {
|
|
166
|
+
this.init(this.server.startSecure(certPath));
|
|
167
|
+
} else {
|
|
168
|
+
this.init(this.server.start());
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Binary } from "binencode";
|
|
2
|
+
import { type App } from "./app";
|
|
3
|
+
import { type WebSocketServer, type RawData } from "ws";
|
|
4
|
+
import { type IncomingMessage } from "http";
|
|
5
|
+
import { type Socket } from "../socket";
|
|
6
|
+
import { type ErrorEvent, type CloseEvent, type Event as WSEvent } from "ws";
|
|
7
|
+
|
|
8
|
+
export type BaseServerEvent<
|
|
9
|
+
Type extends string,
|
|
10
|
+
Data extends Record<string, unknown> = {},
|
|
11
|
+
> = Data & {
|
|
12
|
+
type: Type;
|
|
13
|
+
app: App;
|
|
14
|
+
ws: WebSocketServer;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type ServerEvent =
|
|
18
|
+
| BaseServerEvent<
|
|
19
|
+
"server-headers",
|
|
20
|
+
{ headers: string[]; req: IncomingMessage }
|
|
21
|
+
>
|
|
22
|
+
| BaseServerEvent<
|
|
23
|
+
"client-headers",
|
|
24
|
+
{ headers: string[]; req: IncomingMessage }
|
|
25
|
+
>
|
|
26
|
+
| BaseServerEvent<
|
|
27
|
+
"client-connection",
|
|
28
|
+
{ socket: Socket; req: IncomingMessage }
|
|
29
|
+
>
|
|
30
|
+
| BaseServerEvent<"client-message", { socket: Socket; message: Binary }>
|
|
31
|
+
| BaseServerEvent<
|
|
32
|
+
"client-message-raw",
|
|
33
|
+
{ socket: Socket; message: RawData; isBinary: boolean }
|
|
34
|
+
>
|
|
35
|
+
| BaseServerEvent<"client-error", { socket: Socket; event: ErrorEvent }>
|
|
36
|
+
| BaseServerEvent<"client-open", { socket: Socket; event: WSEvent }>
|
|
37
|
+
| BaseServerEvent<"client-close", { socket: Socket; event: CloseEvent }>
|
|
38
|
+
| BaseServerEvent<"client-upgrade", { socket: Socket; req: IncomingMessage }>
|
|
39
|
+
| BaseServerEvent<"server-ready">
|
|
40
|
+
| BaseServerEvent<"server-error">
|
|
41
|
+
| BaseServerEvent<"server-close">;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type Binary } from "binencode";
|
|
2
|
+
import { type App } from "./app";
|
|
3
|
+
import { type WebSocketServer, type WebSocket, type RawData } from "ws";
|
|
4
|
+
import { type IncomingMessage } from "http";
|
|
5
|
+
import { type Socket } from "../socket";
|
|
6
|
+
import { type ErrorEvent, type CloseEvent, type Event as WSEvent } from "ws";
|
|
7
|
+
|
|
8
|
+
export type BaseServerEvent<
|
|
9
|
+
Type extends string,
|
|
10
|
+
Data extends Record<string, unknown> = {},
|
|
11
|
+
> = Data & {
|
|
12
|
+
type: Type;
|
|
13
|
+
app: App;
|
|
14
|
+
ws: WebSocketServer;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type ServerEvent =
|
|
18
|
+
| BaseServerEvent<
|
|
19
|
+
"server-headers",
|
|
20
|
+
{ headers: string[]; req: IncomingMessage }
|
|
21
|
+
>
|
|
22
|
+
| BaseServerEvent<
|
|
23
|
+
"client-headers",
|
|
24
|
+
{ headers: string[]; req: IncomingMessage }
|
|
25
|
+
>
|
|
26
|
+
| BaseServerEvent<
|
|
27
|
+
"client-connection",
|
|
28
|
+
{ socket: Socket; req: IncomingMessage }
|
|
29
|
+
>
|
|
30
|
+
| BaseServerEvent<"client-message", { socket: Socket; message: Binary }>
|
|
31
|
+
| BaseServerEvent<
|
|
32
|
+
"client-message-raw",
|
|
33
|
+
{ socket: Socket; message: RawData; isBinary: boolean }
|
|
34
|
+
>
|
|
35
|
+
| BaseServerEvent<
|
|
36
|
+
"client-error",
|
|
37
|
+
{ socket: Socket; event: ErrorEvent }
|
|
38
|
+
>
|
|
39
|
+
| BaseServerEvent<"client-open", { socket: Socket; event: WSEvent }>
|
|
40
|
+
| BaseServerEvent<
|
|
41
|
+
"client-close",
|
|
42
|
+
{ socket: Socket; event: CloseEvent }
|
|
43
|
+
>
|
|
44
|
+
| BaseServerEvent<"client-upgrade", { socket: Socket; req: IncomingMessage }>
|
|
45
|
+
| BaseServerEvent<"server-ready">
|
|
46
|
+
| BaseServerEvent<"server-error">
|
|
47
|
+
| BaseServerEvent<"server-close">;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './server';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { WebSocketServer } from "ws";
|
|
2
|
+
import { createServer as createHTTPSServer } from "https";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import pathlib from "path";
|
|
5
|
+
|
|
6
|
+
export type ServerConfig = {
|
|
7
|
+
host: string;
|
|
8
|
+
port: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class Server {
|
|
12
|
+
config: ServerConfig;
|
|
13
|
+
connection: WebSocketServer | null = null;
|
|
14
|
+
|
|
15
|
+
constructor(cfg: ServerConfig) {
|
|
16
|
+
this.config = cfg;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
startSecure(certPath: string) {
|
|
20
|
+
const pemPath = pathlib.join(certPath, "cert.pem");
|
|
21
|
+
const privKeyPath = pathlib.join(certPath, "privkey.pem");
|
|
22
|
+
const chainPemPath = pathlib.join(certPath, "chain.pem");
|
|
23
|
+
if (!fs.existsSync(pemPath)) throw new Error(`Could not find: ${pemPath}`);
|
|
24
|
+
if (!fs.existsSync(privKeyPath))
|
|
25
|
+
throw new Error(`Could not find: ${privKeyPath}`);
|
|
26
|
+
if (!fs.existsSync(chainPemPath))
|
|
27
|
+
throw new Error(`Could not find: ${chainPemPath}`);
|
|
28
|
+
|
|
29
|
+
const httpsServer = createHTTPSServer({
|
|
30
|
+
cert: fs.readFileSync(pemPath, "utf8"),
|
|
31
|
+
key: fs.readFileSync(privKeyPath, "utf8"),
|
|
32
|
+
ca: fs.readFileSync(chainPemPath, "utf8"),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
httpsServer.listen({
|
|
36
|
+
port: this.config.port,
|
|
37
|
+
host: this.config.host,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const server = new WebSocketServer({
|
|
41
|
+
server: httpsServer,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
this.connection = server;
|
|
45
|
+
console.log(
|
|
46
|
+
`Secure websocket server listening on ${this.config.host}:${this.config.port}`,
|
|
47
|
+
);
|
|
48
|
+
return server;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
start() {
|
|
52
|
+
const server = new WebSocketServer({
|
|
53
|
+
host: this.config.host,
|
|
54
|
+
port: this.config.port,
|
|
55
|
+
});
|
|
56
|
+
this.connection = server;
|
|
57
|
+
console.log(
|
|
58
|
+
`Insecure websocket server listening on ${this.config.host}:${this.config.port}`,
|
|
59
|
+
);
|
|
60
|
+
return server;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { WebSocketServer } from "ws";
|
|
2
|
+
import { createServer as createHTTPSServer } from "https";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import pathlib from "path";
|
|
5
|
+
|
|
6
|
+
export type ServerConfig = {
|
|
7
|
+
host: string;
|
|
8
|
+
port: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class Server {
|
|
12
|
+
config: ServerConfig;
|
|
13
|
+
connection: WebSocketServer | null = null;
|
|
14
|
+
|
|
15
|
+
constructor(cfg: ServerConfig) {
|
|
16
|
+
this.config = cfg;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
startSecure(certPath: string) {
|
|
20
|
+
const pemPath = pathlib.join(certPath, "cert.pem");
|
|
21
|
+
const privKeyPath = pathlib.join(certPath, "privkey.pem");
|
|
22
|
+
const chainPemPath = pathlib.join(certPath, "chain.pem");
|
|
23
|
+
if (!fs.existsSync(pemPath)) throw new Error(`Could not find: ${pemPath}`);
|
|
24
|
+
if (!fs.existsSync(privKeyPath))
|
|
25
|
+
throw new Error(`Could not find: ${privKeyPath}`);
|
|
26
|
+
if (!fs.existsSync(chainPemPath))
|
|
27
|
+
throw new Error(`Could not find: ${chainPemPath}`);
|
|
28
|
+
|
|
29
|
+
const httpsServer = createHTTPSServer({
|
|
30
|
+
cert: fs.readFileSync(pemPath, "utf8"),
|
|
31
|
+
key: fs.readFileSync(privKeyPath, "utf8"),
|
|
32
|
+
ca: fs.readFileSync(chainPemPath, "utf8"),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
httpsServer.listen({
|
|
36
|
+
port: this.config.port,
|
|
37
|
+
host: this.config.host,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const server = new WebSocketServer({
|
|
41
|
+
server: httpsServer,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
this.connection = server;
|
|
45
|
+
console.log(
|
|
46
|
+
`Secure websocket server listening on ${this.config.host}:${this.config.port}`,
|
|
47
|
+
);
|
|
48
|
+
return server;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
start() {
|
|
52
|
+
const server = new WebSocketServer({
|
|
53
|
+
host: this.config.host,
|
|
54
|
+
port: this.config.port,
|
|
55
|
+
});
|
|
56
|
+
this.connection = server;
|
|
57
|
+
console.log(
|
|
58
|
+
`Insecure websocket server listening on ${this.config.host}:${this.config.port}`,
|
|
59
|
+
);
|
|
60
|
+
return server;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/server.ts~
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { WebSocketServer } from "ws";
|
|
2
|
+
import { createServer as createHTTPSServer } from "https";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import pathlib from "path";
|
|
5
|
+
|
|
6
|
+
export type ServerConfig = {
|
|
7
|
+
host: string;
|
|
8
|
+
port: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class Server {
|
|
12
|
+
config: ServerConfig;
|
|
13
|
+
connection: WebSocketServer | null = null;
|
|
14
|
+
|
|
15
|
+
constructor(cfg: ServerConfig) {
|
|
16
|
+
this.config = cfg;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private init(server: WebSocketServer) {}
|
|
20
|
+
|
|
21
|
+
startSecure(certPath: string) {
|
|
22
|
+
const pemPath = pathlib.join(certPath, "cert.pem");
|
|
23
|
+
const privKeyPath = pathlib.join(certPath, "privkey.pem");
|
|
24
|
+
const chainPemPath = pathlib.join(certPath, "chain.pem");
|
|
25
|
+
if (!fs.existsSync(pemPath)) throw new Error(`Could not find: ${pemPath}`);
|
|
26
|
+
if (!fs.existsSync(privKeyPath))
|
|
27
|
+
throw new Error(`Could not find: ${privKeyPath}`);
|
|
28
|
+
if (!fs.existsSync(chainPemPath))
|
|
29
|
+
throw new Error(`Could not find: ${chainPemPath}`);
|
|
30
|
+
|
|
31
|
+
const httpsServer = createHTTPSServer({
|
|
32
|
+
cert: fs.readFileSync(pemPath, "utf8"),
|
|
33
|
+
key: fs.readFileSync(privKeyPath, "utf8"),
|
|
34
|
+
ca: fs.readFileSync(chainPemPath, "utf8"),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
httpsServer.listen({
|
|
38
|
+
port: this.config.port,
|
|
39
|
+
host: this.config.host,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const server = new WebSocketServer({
|
|
43
|
+
server: httpsServer,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
this.connection = server;
|
|
47
|
+
console.log(
|
|
48
|
+
`Secure websocket server listening on ${this.config.host}:${this.config.port}`,
|
|
49
|
+
);
|
|
50
|
+
this.init(server);
|
|
51
|
+
return server;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
start() {
|
|
55
|
+
const server = new WebSocketServer({
|
|
56
|
+
host: this.config.host,
|
|
57
|
+
port: this.config.port,
|
|
58
|
+
});
|
|
59
|
+
this.connection = server;
|
|
60
|
+
console.log(
|
|
61
|
+
`Insecure websocket server listening on ${this.config.host}:${this.config.port}`,
|
|
62
|
+
);
|
|
63
|
+
this.init(server);
|
|
64
|
+
return server;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const s = new Server({
|
|
69
|
+
host: "localhost",
|
|
70
|
+
port: 5596,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
s.start();
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
const _global = typeof global !== 'undefined' && typeof global.WebSocket !== 'undefined' ? global : typeof window !== 'undefined' ? window : undefined;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const SocketImplementation = _global?.WebSocket || require('ws');
|
|
5
|
+
export type SocketType = InstanceType<typeof SocketImplementation>;
|