Skip to content

Commit 167b49d

Browse files
authored
fix: fix cannot write after stream was destroyed message (#413)
1 parent d7bd26b commit 167b49d

File tree

4 files changed

+36
-67
lines changed

4 files changed

+36
-67
lines changed

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Howerver, it's known that these browsers have issues:
5454

5555
### Versions Support
5656

57-
Server versions 1.8 - 1.21.4 are supported.
57+
Server versions 1.8 - 1.21.5 are supported.
5858
First class versions (most of the features are tested on these versions):
5959

6060
- 1.19.4

patches/minecraft-protocol.patch

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/src/client/chat.js b/src/client/chat.js
2-
index a50f4b988ad9fb29d5eb9e1633b498615aa9cd28..b8b819eef0762a77d9db5c0fb06be648303628c7 100644
2+
index 0021870994fc59a82f0ac8aba0a65a8be43ef2f4..a53fceb843105ea2a1d88722b3fc7c3b43cb102a 100644
33
--- a/src/client/chat.js
44
+++ b/src/client/chat.js
5-
@@ -110,7 +110,7 @@ module.exports = function (client, options) {
5+
@@ -116,7 +116,7 @@ module.exports = function (client, options) {
66
for (const player of packet.data) {
77
if (player.chatSession) {
88
client._players[player.uuid] = {
@@ -11,16 +11,16 @@ index a50f4b988ad9fb29d5eb9e1633b498615aa9cd28..b8b819eef0762a77d9db5c0fb06be648
1111
publicKeyDER: player.chatSession.publicKey.keyBytes,
1212
sessionUuid: player.chatSession.uuid
1313
}
14-
@@ -120,7 +120,7 @@ module.exports = function (client, options) {
15-
14+
@@ -126,7 +126,7 @@ module.exports = function (client, options) {
15+
1616
if (player.crypto) {
1717
client._players[player.uuid] = {
1818
- publicKey: crypto.createPublicKey({ key: player.crypto.publicKey, format: 'der', type: 'spki' }),
1919
+ // publicKey: crypto.createPublicKey({ key: player.crypto.publicKey, format: 'der', type: 'spki' }),
2020
publicKeyDER: player.crypto.publicKey,
2121
signature: player.crypto.signature,
2222
displayName: player.displayName || player.name
23-
@@ -190,7 +190,7 @@ module.exports = function (client, options) {
23+
@@ -196,7 +196,7 @@ module.exports = function (client, options) {
2424
if (mcData.supportFeature('useChatSessions')) {
2525
const tsDelta = BigInt(Date.now()) - packet.timestamp
2626
const expired = !packet.timestamp || tsDelta > messageExpireTime || tsDelta < 0
@@ -29,16 +29,16 @@ index a50f4b988ad9fb29d5eb9e1633b498615aa9cd28..b8b819eef0762a77d9db5c0fb06be648
2929
if (verified) client._signatureCache.push(packet.signature)
3030
client.emit('playerChat', {
3131
globalIndex: packet.globalIndex,
32-
@@ -356,7 +356,7 @@ module.exports = function (client, options) {
32+
@@ -362,7 +362,7 @@ module.exports = function (client, options) {
3333
}
3434
}
35-
35+
3636
- client._signedChat = (message, options = {}) => {
3737
+ client._signedChat = async (message, options = {}) => {
3838
options.timestamp = options.timestamp || BigInt(Date.now())
3939
options.salt = options.salt || 1n
40-
41-
@@ -401,7 +401,7 @@ module.exports = function (client, options) {
40+
41+
@@ -407,7 +407,7 @@ module.exports = function (client, options) {
4242
message,
4343
timestamp: options.timestamp,
4444
salt: options.salt,
@@ -47,7 +47,7 @@ index a50f4b988ad9fb29d5eb9e1633b498615aa9cd28..b8b819eef0762a77d9db5c0fb06be648
4747
offset: client._lastSeenMessages.pending,
4848
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
4949
acknowledged
50-
@@ -416,7 +416,7 @@ module.exports = function (client, options) {
50+
@@ -422,7 +422,7 @@ module.exports = function (client, options) {
5151
message,
5252
timestamp: options.timestamp,
5353
salt: options.salt,
@@ -71,10 +71,10 @@ index 63cc2bd9615100bd2fd63dfe14c094aa6b8cd1c9..36df57d1196af9761d920fa285ac48f8
7171
+ // clearTimeout(loginTimeout)
7272
+ // })
7373
}
74-
74+
7575
function onJoinServerResponse (err) {
7676
diff --git a/src/client.js b/src/client.js
77-
index e369e77d055ba919e8f9da7b8e8b5dc879c74cf4..11c6bff299f1186ab1ecb6744f53ff0c648ab192 100644
77+
index e369e77d055ba919e8f9da7b8e8b5dc879c74cf4..54bb9e6644388e9b6bd42b3012951875989cdf0c 100644
7878
--- a/src/client.js
7979
+++ b/src/client.js
8080
@@ -111,7 +111,13 @@ class Client extends EventEmitter {
@@ -94,7 +94,7 @@ index e369e77d055ba919e8f9da7b8e8b5dc879c74cf4..11c6bff299f1186ab1ecb6744f53ff0c
9494
}
9595
@@ -169,7 +175,10 @@ class Client extends EventEmitter {
9696
}
97-
97+
9898
const onFatalError = (err) => {
9999
- this.emit('error', err)
100100
+ // todo find out what is trying to write after client disconnect
@@ -103,58 +103,23 @@ index e369e77d055ba919e8f9da7b8e8b5dc879c74cf4..11c6bff299f1186ab1ecb6744f53ff0c
103103
+ }
104104
endSocket()
105105
}
106-
107-
@@ -198,6 +207,8 @@ class Client extends EventEmitter {
106+
107+
@@ -198,6 +207,10 @@ class Client extends EventEmitter {
108108
serializer -> framer -> socket -> splitter -> deserializer */
109109
if (this.serializer) {
110110
this.serializer.end()
111-
+ this.socket?.end()
112-
+ this.socket?.emit('end')
111+
+ setTimeout(() => {
112+
+ this.socket?.end()
113+
+ this.socket?.emit('end')
114+
+ }, 2000) // allow the serializer to finish writing
113115
} else {
114116
if (this.socket) this.socket.end()
115117
}
116-
@@ -243,6 +254,7 @@ class Client extends EventEmitter {
118+
@@ -243,6 +256,7 @@ class Client extends EventEmitter {
117119
debug('writing packet ' + this.state + '.' + name)
118120
debug(params)
119121
}
120122
+ this.emit('writePacket', name, params)
121123
this.serializer.write({ name, params })
122124
}
123-
124-
diff --git a/src/client.js.rej b/src/client.js.rej
125-
new file mode 100644
126-
index 0000000000000000000000000000000000000000..1101e2477adfdc004381b78e7d70953dacb7b484
127-
--- /dev/null
128-
+++ b/src/client.js.rej
129-
@@ -0,0 +1,31 @@
130-
+@@ -89,10 +89,12 @@
131-
+ parsed.metadata.name = parsed.data.name
132-
+ parsed.data = parsed.data.params
133-
+ parsed.metadata.state = state
134-
+- debug('read packet ' + state + '.' + parsed.metadata.name)
135-
+- if (debug.enabled) {
136-
+- const s = JSON.stringify(parsed.data, null, 2)
137-
+- debug(s && s.length > 10000 ? parsed.data : s)
138-
++ if (!globalThis.excludeCommunicationDebugEvents?.includes(parsed.metadata.name)) {
139-
++ debug('read packet ' + state + '.' + parsed.metadata.name)
140-
++ if (debug.enabled) {
141-
++ const s = JSON.stringify(parsed.data, null, 2)
142-
++ debug(s && s.length > 10000 ? parsed.data : s)
143-
++ }
144-
+ }
145-
+ if (this._hasBundlePacket && parsed.metadata.name === 'bundle_delimiter') {
146-
+ if (this._mcBundle.length) { // End bundle
147-
+@@ -239,8 +252,11 @@
148-
+
149-
+ write (name, params) {
150-
+ if (!this.serializer.writable) { return }
151-
+- debug('writing packet ' + this.state + '.' + name)
152-
+- debug(params)
153-
++ if (!globalThis.excludeCommunicationDebugEvents?.includes(name)) {
154-
++ debug(`[${this.state}] from ${this.isServer ? 'server' : 'client'}: ` + name)
155-
++ debug(params)
156-
++ }
157-
++ this.emit('writePacket', name, params)
158-
+ this.serializer.write({ name, params })
159-
+ }
160-
+
125+

pnpm-lock.yaml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ export async function connect (connectOptions: ConnectOptions) {
231231
bot.emit('end', '')
232232
bot.removeAllListeners()
233233
bot._client.removeAllListeners()
234-
//@ts-expect-error TODO?
235-
bot._client = undefined
234+
bot._client = {
235+
//@ts-expect-error
236+
write (packetName) {
237+
console.warn('Tried to write packet', packetName, 'after bot was destroyed')
238+
}
239+
}
236240
//@ts-expect-error
237241
window.bot = bot = undefined
238242
}

0 commit comments

Comments
 (0)