infer `secure` from the socket

Massimo Melina committed Aug 6, 2022 at 16:23 UTC 31f2618bd669798e3dff19013b2d2a58e385f8c0
2 files changed +8 -5
server/src/connections.ts
+7 -3
@@ -15,7 +15,7 @@ export class Connection {
15 private _cachedIp?: string
16 [rest:symbol]: any // let other modules add extra data, but using symbols to avoid name collision
17
18 - constructor(readonly socket: Socket, readonly secure: boolean) {
18 + constructor(readonly socket: Socket) {
19 all.push(this)
20 socket.on('data', data =>
21 this.got += data.length )
@@ -30,6 +30,10 @@ export class Connection {
30 get ip() {
31 return this.ctx?.ip ?? (this._cachedIp = (this._cachedIp ?? normalizeIp(this.socket.remoteAddress||'')))
32 }
33 +
34 + get secure() {
35 + return (this.socket as any).server.cert > ''
36 + }
37 }
38
39 export function normalizeIp(ip: string) {
@@ -38,8 +42,8 @@ export function normalizeIp(ip: string) {
42
43 const all: Connection[] = []
44
41 -export function newConnection(socket: Socket, secure:boolean=false) {
42 - new Connection(socket, secure)
45 +export function newConnection(socket: Socket) {
46 + new Connection(socket)
47 }
48
49 export function getConnections(): Readonly<typeof all> {
server/src/listen.ts
+1 -2
@@ -66,8 +66,7 @@ const considerHttps = debounceAsync(async () => {
66 }
67 port = await startServer(httpsSrv, { port })
68 if (!port) return
69 - httpsSrv.on('connection', socket =>
70 - newConnection(socket, true))
69 + httpsSrv.on('connection', newConnection)
70 printUrls(port, 'https')
71 })
72