/
pubsub-connection.ts
137 lines (111 loc) · 4.76 KB
/
pubsub-connection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
This file is part of botten-nappet -- a Twitch bot and streaming tool.
<https://joelpurra.com/projects/botten-nappet/>
Copyright (c) 2018 Joel Purra <https://joelpurra.com/>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
assert,
} from "check-types";
import PinoLogger from "../../util/pino-logger";
import {
UserAccessTokenProviderType,
} from "../authentication/provider-types";
import IWebSocketCommand from "../websocket/iwebsocket-command";
import WebSocketConnection from "../websocket/websocket-connection";
import IPubSubConnection from "./ipubsub-connection";
import IPubSubResponse from "./ipubsub-response";
export default class PubSubConnection extends WebSocketConnection<IPubSubResponse, any> implements IPubSubConnection {
private userAccessTokenProvider: UserAccessTokenProviderType;
private topics: string[];
constructor(
logger: PinoLogger,
uri: string,
topics: string[],
userAccessTokenProvider: UserAccessTokenProviderType,
) {
super(logger, uri);
assert.hasLength(arguments, 4);
assert.equal(typeof logger, "object");
assert.equal(typeof uri, "string");
assert.nonEmptyString(uri);
assert(uri.startsWith("wss://"));
assert.array(topics);
assert.nonEmptyArray(topics);
assert.equal(typeof userAccessTokenProvider, "function");
this.logger = logger.child("PubSubConnection");
this.topics = topics;
this.userAccessTokenProvider = userAccessTokenProvider;
}
protected async getSetupConnectionCommands(): Promise<Array<IWebSocketCommand<IPubSubResponse>>> {
const userAccessToken = await this.userAccessTokenProvider();
const listenNonce = Math.random()
.toString(10);
const setupConnectionCommands: Array<IWebSocketCommand<IPubSubResponse>> = [
{
commands: [
// TODO: typing for the PING request.
{
type: "PING",
},
],
verifier: (data) => {
// TODO: typing for the PING/PONG response.
if (data.type === "PONG") {
return true;
}
return false;
},
},
{
commands: [
{
// TODO: typing for the LISTEN request.
data: {
auth_token: userAccessToken,
topics: this.topics,
},
nonce: listenNonce,
type: "LISTEN",
},
],
verifier: (data) => {
// TODO: typing for the LISTEN response.
if (data.nonce !== listenNonce) {
// NOTE: skip non-matching messages; they are presumably for other handlers.
return false;
}
// TODO: use as part of generic error checking for twitch pubsub-connections?
if (typeof data.error === "string" && data.error.length !== 0) {
const listenError = new Error(`Listen error: ${JSON.stringify(data.error)}`);
this.logger.error(listenError, data, "Listen error");
throw listenError;
}
// TODO: use as part of generic error checking for twitch pubsub-connections?
if (data.type !== "RESPONSE") {
const badTypeError = new Error(`Bad type: ${JSON.stringify(data.type)}`);
this.logger.error(badTypeError, data, "Bad type");
throw badTypeError;
}
return true;
},
},
];
return setupConnectionCommands;
}
protected async parseMessage(rawMessage: string): Promise<any> {
// TODO: verify response format.
// TODO: try-catch for bad messages.
const data = JSON.parse(rawMessage.toString());
return data;
}
}