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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
Log: "chrome://remote/content/shared/Log.sys.mjs",
truncate: "chrome://remote/content/shared/Format.sys.mjs",
WebSocketTransport:
"chrome://remote/content/server/WebSocketTransport.sys.mjs",
});
ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"truncateLog",
"remote.log.truncate",
false
);
const MAX_LOG_LENGTH = 2500;
export class WebSocketConnection {
/**
* @param {WebSocket} webSocket
* The WebSocket server connection to wrap.
* @param {Connection} httpdConnection
* Reference to the httpd.js's connection needed for clean-up.
*/
constructor(webSocket, httpdConnection) {
this.id = lazy.generateUUID();
this.httpdConnection = httpdConnection;
this.transport = new lazy.WebSocketTransport(webSocket);
this.transport.hooks = this;
this.transport.ready();
lazy.logger.debug(`${this.constructor.name} ${this.id} accepted`);
}
#log(direction, data) {
if (lazy.Log.isDebugLevelOrMore) {
function replacer(key, value) {
if (typeof value === "string") {
return lazy.truncate`${value}`;
}
return value;
}
let payload = JSON.stringify(
data,
replacer,
lazy.Log.verbose ? "\t" : null
);
if (lazy.truncateLog && payload.length > MAX_LOG_LENGTH) {
// Even if we truncate individual values, the resulting message might be
// huge if we are serializing big objects with many properties or items.
// Truncate the overall message to avoid issues in logs.
const truncated = payload.substring(0, MAX_LOG_LENGTH);
payload = `${truncated} [... truncated after ${MAX_LOG_LENGTH} characters]`;
}
lazy.logger.debug(
`${this.constructor.name} ${this.id} ${direction} ${payload}`
);
}
}
/**
* Close the WebSocket connection.
*/
close() {
this.transport.close();
}
/**
* Register a new Session to forward the messages to.
*
* Needs to be implemented in the sub class.
*/
registerSession() {
throw new Error("Not implemented");
}
/**
* Send the JSON-serializable object to the client.
*
* @param {object} data
* The object to be sent.
*/
send(data) {
this.#log("<-", data);
this.transport.send(data);
}
/**
* Send an error back to the client.
*
* Needs to be implemented in the sub class.
*/
sendError() {
throw new Error("Not implemented");
}
/**
* Send an event back to the client.
*
* Needs to be implemented in the sub class.
*/
sendEvent() {
throw new Error("Not implemented");
}
/**
* Send the result of a call to a method back to the client.
*
* Needs to be implemented in the sub class.
*/
sendResult() {
throw new Error("Not implemented");
}
toString() {
return `[object ${this.constructor.name} ${this.id}]`;
}
// Transport hooks
/**
* Called by the `transport` when the connection is closed.
*/
onConnectionClose() {
lazy.logger.debug(`${this.constructor.name} ${this.id} closed`);
}
/**
* Called when the socket is closed.
*/
onSocketClose() {
// In addition to the WebSocket transport, we also have to close the
// connection used internally within httpd.js. Otherwise the server doesn't
// shut down correctly, and keeps these Connection instances alive.
this.httpdConnection.close();
}
/**
* Receive a packet from the WebSocket layer.
*
* This packet is sent by a WebSocket client and is meant to execute
* a particular method.
*
* Needs to be implemented in the sub class.
*
* @param {object} packet
* JSON-serializable object sent by the client.
*/
async onPacket(packet) {
this.#log("->", packet);
}
}
|