Skip to content

Commit 354eb03

Browse files
c-cubejeromeludmann
authored andcommitted
support TLS connections; move readme to libera.chat
1 parent add4d4f commit 354eb03

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ client.on("join", (msg) => {
4444
}
4545
});
4646

47-
await client.connect("irc.freenode.net", 6667);
47+
await client.connect("irc.libera.chat", 7000, tls = true);
4848
```
4949

5050
Note that this code above requires the `--allow-net` option.

client_test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("client", (test) => {
7070

7171
test("have state initialized", () => {
7272
assertEquals(client.state, {
73-
remoteAddr: { hostname: "", port: 0 },
73+
remoteAddr: { hostname: "", port: 0, tls: false },
7474
nick: "me",
7575
username: "user",
7676
realname: "real name",
@@ -88,6 +88,7 @@ describe("client", (test) => {
8888
assertEquals(client.state.remoteAddr, {
8989
hostname: "host",
9090
port: 6667,
91+
tls: false,
9192
});
9293
});
9394

core/client.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const PORT = 6667;
3131
export interface RemoteAddr {
3232
hostname: string;
3333
port: number;
34+
tls?: boolean;
3435
}
3536

3637
export type PluginParams = {
@@ -52,14 +53,23 @@ export type Plugin<T extends PluginParams = Record<string, void>> = (
5253
options: Readonly<ExtendedOptions<T>>,
5354
) => void;
5455

56+
/** How to connect to a server */
57+
interface ConnectImpl {
58+
noTls(opts: Deno.ConnectOptions): Promise<Deno.Conn>;
59+
withTls(opts: Deno.ConnectTlsOptions): Promise<Deno.Conn>;
60+
}
61+
5562
export class CoreClient<
5663
TEvents extends CoreParams["events"] = CoreParams["events"],
5764
> extends EventEmitter<
5865
CoreParams["events"] & TEvents
5966
> {
6067
readonly state: CoreParams["state"];
6168

62-
protected connectImpl = Deno.connect;
69+
protected connectImpl: ConnectImpl = {
70+
noTls: Deno.connect,
71+
withTls: Deno.connectTls,
72+
};
6373
protected conn: Deno.Conn | null = null;
6474
protected hooks = new Hooks(this);
6575

@@ -76,7 +86,7 @@ export class CoreClient<
7686
super(options);
7787

7888
this.buffer = new Uint8Array(options.bufferSize ?? BUFFER_SIZE);
79-
this.state = { remoteAddr: { hostname: "", port: 0 } };
89+
this.state = { remoteAddr: { hostname: "", port: 0, tls: false } };
8090

8191
new Set(plugins).forEach((plugin) => plugin(this, options));
8292
this.resetErrorThrowingBehavior();
@@ -85,10 +95,15 @@ export class CoreClient<
8595
/** Connects to a server using a hostname and an optional port.
8696
*
8797
* Default port to `6667`.
98+
* If `tls=true`, attempt to connect using a TLS connection.
8899
*
89100
* Resolves when connected. */
90-
async connect(hostname: string, port = PORT): Promise<Deno.Conn | null> {
91-
this.state.remoteAddr = { hostname, port };
101+
async connect(
102+
hostname: string,
103+
port = PORT,
104+
tls = false,
105+
): Promise<Deno.Conn | null> {
106+
this.state.remoteAddr = { hostname, port, tls };
92107

93108
if (this.conn !== null) {
94109
this.close();
@@ -98,7 +113,9 @@ export class CoreClient<
98113
this.emit("connecting", remoteAddr);
99114

100115
try {
101-
this.conn = await this.connectImpl({ hostname, port });
116+
this.conn = await (tls
117+
? this.connectImpl.withTls({ hostname, port })
118+
: this.connectImpl.noTls({ hostname, port }));
102119
this.emit("connected", remoteAddr);
103120
} catch (error) {
104121
this.emitError("connect", error);

core/client_test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("core/client", (test) => {
1313
assertEquals(addr, {
1414
hostname: "host",
1515
port: 6668,
16+
tls: false,
1617
});
1718
});
1819

@@ -103,6 +104,7 @@ describe("core/client", (test) => {
103104
assertEquals(addr, {
104105
hostname: "host",
105106
port: 6667,
107+
tls: false,
106108
});
107109
});
108110

@@ -116,6 +118,7 @@ describe("core/client", (test) => {
116118
assertEquals(msg, {
117119
hostname: "host",
118120
port: 6667,
121+
tls: false,
119122
});
120123
});
121124

plugins/verbose_test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ describe("plugins/verbose", (test) => {
5858
["emit", "connecting", {
5959
hostname: "host",
6060
port: 6667,
61+
tls: false,
6162
}],
6263
["emit", "connected", {
6364
hostname: "host",
6465
port: 6667,
66+
tls: false,
6567
}],
6668
["emit", "error", {
6769
name: "Error",

testing/client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import { CoreClient } from "../core/client.ts";
33
import { MockConn } from "./conn.ts";
44

55
export class MockCoreClient extends CoreClient {
6-
protected connectImpl = mockConnect;
6+
protected connectImpl = {
7+
withTls: mockConnect,
8+
noTls: mockConnect,
9+
};
710
readonly conn: MockConn | null = null;
811
}
912

1013
export class MockClient extends Client {
11-
protected connectImpl = mockConnect;
14+
protected connectImpl = {
15+
withTls: mockConnect,
16+
noTls: mockConnect,
17+
};
1218
}
1319

1420
function mockConnect<T extends Deno.ConnectOptions>(options: T) {

0 commit comments

Comments
 (0)