Commit Graph

45 Commits

Author SHA1 Message Date
Almeida
531b46c60d refactor: use the node: protocol (#6710) 2021-10-02 13:40:02 +02:00
Rodry
00bd92a451 refactor: ES2021 features (#6540)
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
Co-authored-by: Voltrex <mohammadkeyvanzade94@gmail.com>
2021-09-03 12:58:01 +01:00
ckohen
09471be30e fix(WebSocketShard): mark shard ready if no guilds intent (#6284)
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
2021-08-03 23:13:18 +01:00
Antonio Román
5ca97c9351 refactor: remove timer utilities from Client (#6113) 2021-07-16 13:31:25 +01:00
Antonio Román
4eb3a2a885 refactor: rename Constants.OPCode to Opcode, moved objects to enums (#6065) 2021-07-07 13:40:36 +01:00
Antonio Román
a7c6678c72 refactor: change xID to xId (#6036)
* refactor: change `xID` to `xId`

* Update src/managers/MessageManager.js

Co-authored-by: Noel <buechler.noel@outlook.com>

Co-authored-by: Noel <buechler.noel@outlook.com>
2021-07-04 19:54:27 +01:00
Vlad Frangu
39db95352c fix: Remove connection.url from open debug message (#6018) 2021-07-03 18:44:48 +01:00
Sugden
1e8f01253e refactor: new node features (#5132)
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
2021-06-30 21:40:33 +01:00
SpaceEEC
53d8e49dca fix(WebSocketShard): don't catch errors thrown in event handlers (#5803) 2021-06-11 10:46:14 +01:00
SpaceEEC
99e2ca408b debug(WebSocketShard): emit intents used in identify (#5744) 2021-06-03 16:18:21 +01:00
Sugden
ee5bc1a5c4 refactor: switch api and gateway to V8 (#4879)
Co-authored-by: Jan <66554238+Vaporox@users.noreply.github.com>
2021-02-11 18:10:35 +01:00
BannerBomb
56d8b445ed fix(WebSocketShard): key name in WebSocketShard#_send. (#5304)
I just changed an incorrect key name that was introduced in Pull Request #3722. When destroying the websocket from the WebSocketShard#_send method it's using `close` as the key name instead of `closeCode`
2021-02-10 15:48:16 +01:00
Noel
0a591a9697 feat(Browser): remove browser � (#5113) 2020-12-14 13:56:16 +01:00
Sugden
af670fc718 refactor: improve the accuracy of docs/improve docs (#4845)
Co-authored-by: Noel <icrawltogo@gmail.com>
2020-10-17 15:53:02 +02:00
Tristan Guichaoua
405b487dc3 fix(Typing): change NodeJS.Timer into NodeJS.Timeout (#4755) 2020-08-29 18:54:39 +02:00
Phineas
ea19faa411 Change domain to discord.com (#4160) 2020-06-19 11:46:59 +02:00
Papaia
2583ad5da7 docs(WebSocketShard): add missing properties (#4268) 2020-05-09 17:23:32 +02:00
Vlad Frangu
e5fac8c32f chore(WebSocketShard): log Discord requested reconnects (#4066) 2020-04-12 20:57:50 +02:00
didinele
65e8c92d7c fix(WebsocketShard): pass ignore ACK when Discord asks for a beat (#3993) 2020-03-25 12:55:20 +01:00
Crawl
c065156a88 chore: consistency/prettier (#3852)
* chore: consistency/prettier

* chore: rebase

* chore: rebase

* chore: include typings

* fix: include typings file in prettier lint-staged
2020-02-29 14:35:57 +01:00
Sugden
1af1e0cbb8 refactor: add some more consistency (#3842)
* cleanup(StreamDispatcher): remove old 'end' event

* fix(StreamDispatcher): only listen to finish event once

* refactor(VoiceWebSocket): use `connection.client` in favour of `connection.voiceManager.client`

* fix(VoiceWebSocket): use `client.clearInterval` in favour of `clearInterval`

* refactor: destructure EventEmitter

* refactor: destructure EventEmitter from events

* refactor: use EventEmitter.off in favour of EventEmitter.removeListener

* style: order typings alphabetically

* oops

* fix indent

* style: alphabetically organize imports

* style: remove extra line

* Revert "style: remove extra line"

This reverts commit 96e182ed69.

* Revert "style: alphabetically organize imports"

This reverts commit 02aee9b06d.

* Revert "refactor: destructure EventEmitter from events"

This reverts commit 9953b4d267.

* Revert "refactor: destructure EventEmitter"

This reverts commit 930d7751ab.

* Revert "fix(StreamDispatcher): only listen to finish event once"

This reverts commit 485a6430a8.

* refactor: use .removeListener instead of .off
2020-02-28 18:02:51 +01:00
Vlad Frangu
b4e56d3e0e src: fix up WebSocketShard errors (#3722)
* src: Fix up WebSocketShard errors

* typings: Forgot to update

* src: Forgot debug variable

* src: Fix issue Bella found
If the WS was not connected when the HELLO timeout passes
(CONNECTING, etc), the shard would get stuck
due to never rejecting the WebSocketShard#connect
Promise with the DESTROYED event
2020-02-02 11:12:58 +01:00
Vlad Frangu
c23cc7a42e src: Cleanup event listeners on WebSocket connections (#3681)
* src: Cleanup event listeners on WebSocket connections
Should prevent #3641 from happening, as well as double connections on a shard

* typings: Forgot to add the method
2020-01-13 18:53:07 +00:00
Vlad Frangu
710101c580 src(WebSocket): fix race condition (#3636)
A race condition caused Client#user to be null in the ready event if the client handled 0 guilds.
2019-12-22 10:31:26 +01:00
Vlad Frangu
5519d6fbaa src: sharding cleanup and checkReady rewrite (#3393)
* src: Step 1 of who knows how many

* src: Remove accidentally committed test file

* src: Remove useless added property in package.json

* docs: Trailing spaces, come back >.>

* src: Buhbye uws, we will miss you..not!

* src: Move 'auto' shard selection from totalShardCount to shards

* src: tweak

* src: Filter out floats from shard IDs
You want half of a shard or what?

* src: Misc cleanup and bugfix for GUILD_BAN_ADD

* src: Rewrite checkReady

* src: Misse this while merging master into my branch

* typings: Bring these up to date

* typings: Forgot allReady event

* src: Don't checkReady if the shard isn't waiting for guilds

* src: Fix a possible bug for when the ws dies and the session becomes -1

* src: Hopefully fix last edge case that could case a shard to infinitely boot loop

* src: Rename totalShardCount to shardCount

* src: Small bugfix

* src: Correct error message for shardCount being imvalid

Co-Authored-By: bdistin <bdistin@gmail.com>

* src: Small tweaks

* src: If this doesn't fix the issues I'm gonna throw a brick at my PC

* src: I swear, STOP BREAKING

* src: *groans at a certain snake*

* src: Use undefined instead of null on destroy in close event

Setting it to null sets the close code to null, which causes a WebSocket error to be thrown. The error is thrown from WebSocket, although there is no connection alive. Fun times!

* src: @SpaceEEC's requested changes

* src: Remove zucc from discord.js

Discord is removing support for it, sooo... Bye bye

* src: Missed this

* src: Apply @kyranet's suggestions

Co-Authored-By: Antonio Román <kyradiscord@gmail.com>

* src: @kyranet's suggestions

* src: Remove pako, update debug messages
- Pako is officially gone from both enviroments
  Install zlib-sync on node.js if you want it
- Improve a few debug messages some more
- Discover that internal sharding works in browsers but please don't do that
2019-12-15 20:45:27 +01:00
iCrawl
4fc461c2f9 fix: browser-compatability
Fix #3453
2019-09-08 11:47:46 +02:00
Kitten King
d8516efa36 docs: fix typos (#3404) 2019-07-25 17:18:23 +02:00
DeJay
5aa9425040 Removes the trace packet (#3312)
* Removes the trace packet

* Update src/client/websocket/WebSocketShard.js

Co-Authored-By: Amish Shah <amishshah.2k@gmail.com>

* Update src/client/websocket/WebSocketShard.js

Co-Authored-By: Amish Shah <amishshah.2k@gmail.com>
2019-05-30 19:57:34 +01:00
Gus Caplan
065908956b fix websocket unpacking (#3301) 2019-05-27 18:13:25 +01:00
izexi
75d5598fda import TextDecoder from Util (#3258) 2019-05-08 21:03:18 +01:00
Gus Caplan
3d4513268d Add optional zstd for faster WebSocket data inflation (#3223)
* zstd
2019-05-07 14:30:34 +01:00
bdistin
9b0f4b298d feature: public raw events (#3159)
* add a public alternative to the private raw event

while retaining raw for use in debugging privately

* only emit dispatch packets

* requested changes

TIL, that's neat

* fix padding

* requested changes

* Update WebSocketManager.js
2019-04-29 17:37:57 +01:00
Vlad Frangu
577636a46d src: fix random broken reconnects (#3233) 2019-04-27 08:25:24 +01:00
Vlad Frangu
de79bba965 src: Fix type error in WebSocketShard#onError (#3231) 2019-04-24 16:53:41 +02:00
Vlad Frangu
1514df0f87 fix: emit resume event, silent disconnects, error event param (#3192)
* src: Fix shardResumed event not being emitted

* docs: Document Client#error again

* src: Fix onError due to incorrect typings

* src: handle onError properly for both uws and ws

* src: Try to fix silent disconnects when using uWs

* fix(WebSocketShard): uws emits plain objects, not errors

Emitting line of code: 39aa429f94/src/uws.js (L80-L83)
Listener attaching is here: https://github.com/discordjs/uws/blob/master/src/uws.js#L128
For reference, found a clue here: https://github.com/discordjs/discord.js/issues/1528
2019-04-21 09:32:16 +02:00
Vlad Frangu
eb537b6f48 docs(WebSocketShard): mark non-nullable parameters as non-nullable (#3209)
* docs: Imagine having an optional nullable param

* docs: Another one
2019-04-15 20:17:27 +02:00
Vlad Frangu
3f5161eb76 fix: Internal Sharding, this time fixed™ (#3140)
* src: WIP Internal Sharding refactor

* src: Refactor unavailable guild check
Co-Authored-By: kyranet <kyradiscord@gmail.com>

* src: More WIP Code
F in the chat to the old manager

* src: It should work but Discord says no.
Seriously why is this not working!

* fix: Inflator causing issues

* src: Finishing touches and typings

* misc: Proper debug message

* fix: Making things hidden needs writable: true as well

* fix: Sharding allowing multiple of the same shard, negative shards or strings

* fix: Again... edge cases
I love you guys .w.

* misc: Touchups

* misc: Better error?

* docs: Typo

* typings: Requested changes

* src: Requested changes

* src: Fix issues, validate provided shard options and more

* src: Forgot to remove the listener

* lint: eslint complaining

* fix: Setting shardCount to auto crashing the process

* misc: Requested changes

* typings: Correct typings for shardCount client option

* typings: Add invalidSession event to the shard and correct typings

* src: Minor docs adjustements, and code consistency between setHelloTimeout and setHeartbeatTimeout

* src: Don't block reconnect while creating shards
Might fix silent disconnects *again*

* src: Prevent reconnect from running if the Manager isn't READY
That way, if a shard dies while we're still spawning, it won't cause issues

* fix: Retry to reconnect if there's a network error going on.
The manager *should* keep reconnecting unless the token is invalid

* src: Enhance onClose handler for shards in the manager
- If the close code is between 1000 and 2000 (inclusive), you cannot resume
I tested this locally
- If there's a session ID still present, immediately try to resume
Faster resumes :papaBless:
Otherwise, the invalid session event will trigger and it'll handle accordingly

I swear if I see a SINGULAR Silent DC I'm yeeting

* src: Fix error check

* src: Make sure message exists on the error

* src: Used the wrong property for the shardQueue

* src: Make the hello timeout be made by the client
god help

* docs: Correct docs for WSEvents

* misc: Remove old events from the Events constant

* src: Throw the HTTP error if we don't get a 401

* typings: Can't forget about them

* src: Implement some more fail safes just in case
Seriously, better safe than sorry! Gotta failproof it completely
2019-04-01 09:43:45 +02:00
Vlad Frangu
793341dbb4 fix: Sharding issues, silent disconnects and code cleanup (#2976)
* fix: Sharding bugs, silent disconnects and cleanup code

* typings

* fix: Destroy connecting with close code different from 1000
Per `If a client does not receive a heartbeat ack between its attempts at sending heartbeats, it should immediately terminate the connection with a non-1000 close code, reconnect, and attempt to resume.`

* misc: Wait x ms before reconnecting
Per https://discordapp.com/developers/docs/topics/gateway#resuming

* docs

* nit: docs

* misc: Prevent multiple calls to WebSocketManager#destroy

* fix: Implement destroying if you reset the token

* misc: Clear the WS packet queue on WebSocketShard#destroy
You can't send those packets anywhere anymore, so no point in keeping them

* fix: Handle session limits when reconnecting a full shard, cleanup

* misc: No need to create a new shard instance

* fix: closeSequence being null, thus emitting null on Client#resumed

* misc: Remove GUILD_SYNC Gateway handler and add missing dot to string

* misc: Close WS with code 4000 if we didn't get a heartbeat in time

As said in the Discord API server

* fix: Handle ready emitting in onPacket
Doesn't allow broken packets

* misc: Close the connection if Discord asks for a reconnect
Prevents double triggers

* testing: Prevent multiple reconnect attempts on a shard

Should fix some issues some people have had.

* fix: Prevent multiple reconnect calls on the shard, re-use conn to identify, remove reconnect function
Note: Closing the WS with 1000 makes the session invalid

* misc: Forgot to remove 2 unneeded setters

* docs: Wrong param docstring for WebSocketShard#destroy

* misc: Set status to reconnecting after destroying

* misc: Close connection with code 1000 on session invalidated
Allows us to cleanup the shard and do a full reconnect
Also remove identify wait delay, not used anywhere

* fix: Fix zlib crash on node
And with that, the PR is done!

* misc: Implement a reconnect queue
And that is all there was to be done in this PR.
Shards now queue up for a reconnect

* nit: Debug the queue after destroying

* docs: Make the invalidated event clearer

* lint: I'm good at my job

* docs

* docs: Make description for isReconnectingShards accurate
*can I stop finding issues, this PR is meant to be done*

* misc: Remove shard from bind params

* misc: Code re-ordering and cleanup
Resumes do not need to be queued up, as they do not count to the identify limit, and after some testing, they don't have the 5 second delay required, like in identify

* fix: Issues with token regeneration and shards not properly handling them
We close the ws connection with code 1000 if we get an invalid session payload,
that way we can queue the reconnects and handle any issues

* misc: Remove useless delays on session invalidated
They get handled by the rest of the code already

* lint

* misc: reset the sequence on Shard#destroy
This especially is a problem if you need to re-identify, as the sequence doesn't get set to the current one,
causing the sequence to be wrong

* fix: GitHub rebase and minor tweak
* Implement a 15 second timeout if shards don't connect till then
Should prevent shards that never reconnect

* revert: Make WebSocketShard#send and WebSocketManager#broadcast public

* typings: Set type to void instead of undefined

* docs: Requested Changes
2019-02-10 16:28:03 +00:00
Marcel Menzel
f2ed93c08a fix(WebSocketShard): report correct resumed event count (#3019)
This PR attempts to fix the reported resumed event count in the debug output (where it is always displayed only as 1 event replayed) and in the emitted `resumed` event, where it passed the current sequence instead of passing the actual replayed event count (which was an utopic high number for smaller bots on resume).
2019-02-02 11:28:45 +01:00
Will Nelson
5cbdf38028 fix(WebSocketShard): add websocket send error handling (#2981)
* websocket send error handling

* fix: emit only when error is present

* refactor: use an if instead
2018-12-22 08:49:56 +01:00
Gus Caplan
42505b78c1 chore: add strict mode (#2974) 2018-12-03 15:19:10 -06:00
Frangu Vlad
9085138f0d fix: Sharding Issues & Cleanup (#2952)
* fix: Sharding causing constant heartbeat / identify spam

* misc: Remove wait param in connect

* misc: Wait 2.5 seconds before sending identify again if session is resumable

* misc: Remove useless destroy call

* nit: Capitalization

* fix: Identify on HELLO not connectionOpen

* misc: Add different intervals for identify after invalid session
- 2500 if we couldn't resume in time
- 5000 if we didn't have a session ID (per the docs on identify, that a client can only connect every 5 seconds)
- Otherwise, just identify again

* misc: Only clear heartbeat if shard is fully dead

Reconnect clears it otherwise

* fix: Accessing .length on a Collection
2018-11-21 13:42:37 -06:00
Lewdcario
3418b5a1a2 docs: restore Client#error docs that went missing 2018-11-06 14:59:02 -06:00
Lewdcario
7796cb5d05 fix: Client#raw emitting twice 2018-11-04 23:26:40 -06:00
Isabella
f3cad81f53 feat: Internal sharding (#2902)
* internal sharding

* ready event

* the square deal

* the new deal

* the second new deal

* add actual documentation

* the new freedom

* the great society

* federal intervention

* some of requested changes

* i ran out of things to call these

* destroy this

* fix: Client#uptime went missing

* fix(Client): destroy the client on login failure

This may happen duo invalid sharding config / invalid token / user requested destroy

* fix(Client): reject login promise when the client is destroyed before ready

* fix(WebSocketManager): remove redundancy in destroy method (#2491)

* typo(ErrorMessages): duo -> duo to

* typo(ErrorMessages): duo -> due

* fix: docs and options

* docs(WebSocketManager): WebSockethard -> WebSocketShard (#2502)

* fix(ClientUser): lazily load to account for extended user structure (#2501)

* docs(WebSocketShard): document class to make it visible in documentation (#2504)

* fix: WebSocketShard#reconnect

* fix: presenceUpdate & userUpdate
* presenceUpdate wasn't really being handled at all
* userUpdate handled incorrectly because as of v7 in the Discord API, it comes inside presenceUpdate

* re-add raw event

* member is now part of message create payload

* feat: Add functionality to support multiple servers with different shards (#2395)

* Added functionallity to spawn multiple sharding managers due to adding start and end shards

* Small fixes and limiting shard amount to max recommended

* Forgot a check in spawn()

* Fixed indentation

* Removed optiosn object documentation for totalShards

* More fixes and a check that the startShard + amount doesnt go over the recommended shard amount

* fix getting max recommended

* Removed async from constructor (my fault)

* Changed start and end shard to a shardList or "auto" + fixed some brainfarts with isNaN

* Changed the loop and totalShard count calculation

* shards are actually 0 based

* Fixed a problem with the gateway and handled some range errors and type errors

* Changed Number.isNan to isNaN and changed a few Integer checks to use Number.isInteger

* Added check if shardList contains smth greater than totalShards; made spawn use totalShards again; shardList will be ignored and rebuild if totalShards is 'auto'; fixed docs

* ShardingManager#spawn now uses a for..of loop; fixed the if statement inside the new for..of loop to still work as intended; made the totalShards be set to a new amount if smth manual is put into ShardingManager#spawn just like before; Fixed some spelling

* internal sharding

* ready event

* the square deal

* the new deal

* the second new deal

* add actual documentation

* the new freedom

* the great society

* federal intervention

* some of requested changes

* i ran out of things to call these

* destroy this

* fix: Client#uptime went missing

* fix(Client): destroy the client on login failure

This may happen duo invalid sharding config / invalid token / user requested destroy

* fix(Client): reject login promise when the client is destroyed before ready

* fix(WebSocketManager): remove redundancy in destroy method (#2491)

* typo(ErrorMessages): duo -> duo to

* typo(ErrorMessages): duo -> due

* fix: docs and options

* docs(WebSocketManager): WebSockethard -> WebSocketShard (#2502)

* fix(ClientUser): lazily load to account for extended user structure (#2501)

* docs(WebSocketShard): document class to make it visible in documentation (#2504)

* fix: WebSocketShard#reconnect

* fix: presenceUpdate & userUpdate
* presenceUpdate wasn't really being handled at all
* userUpdate handled incorrectly because as of v7 in the Discord API, it comes inside presenceUpdate

* Internal Sharding adaptation

Adapted to internal sharding
Fixed a bug where non ready invalidated sessions wouldnt respawn

* Fixed shardCount not retrieving

* Fixing style

removed unnecessary parenthesis

* Fixing and rebasing

lets hope i didnt dun hecklered it

* Fixing my own retardation

* Thanks git rebase

* fix: assigning member in message create payload

* fix: resumes

* fix: IS wont give up reconnecting now

* docs: add missing docs mostly

* fix: found lost methods

* fix: WebSocketManager#broadcast check if shard exists

* fix: ShardClientUtil#id returning undefined

* feat: handle new session rate limits (#2796)

* feat: handle new session rate limits

* i have no idea what i was doing last night

* fix if statement weirdness

* fix: re-add presence parsing from ClientOptions (#2893)

* resolve conflicts

* typings: missing typings

* re-add missing linter rule

* fix: replacing ClientUser wrongly

* address unecessary performance waste

* docs: missing disconnect event

* fix(typings): Fix 2 issues with typings (#2909)

* (Typings) Update typings to reflect current ClientOptions

* fix(Typings) fixes a bug with Websockets and DOM Types

* fix travis

* feat: allow setting presence per shard

* add WebSocketManager#shardX events

* adjust typings, docs and performance issues

* readjust shard events, now provide shardId parameter instead

* fix: ready event should check shardCount, not actualShardCount

* fix: re-add replayed parameter of Client#resume

* fix(Sharding): fixes several things in Internal Sharding (#2914)

* fix(Sharding) fixes several things in Internal Sharding

* add default value for shards property

* better implement checking for shards array

* fix travis & some casing

* split shard count into 2 words

* update to latest Internal Sharding, fix requested changes

* make sure totalShardCount is a number

* fix comment

* fix small typo

* dynamically set totalShardCount if either shards or shardCount is provided

* consistency: rename shardID to shardId

* remove Client#shardIds

* fix: typo in GuildIntegrationsUpdate handler

* fix: incorrect packet data being passed in some events (#2919)

* fix: edgecase of ShardingManager and totalShardCount (#2918)

* fix: Client#userUpdate being passed wrong parameter
and fix a potential edgecase of returning null in ClientUser#edit from this event

* fix consistency and typings issues

* consistency: shardId instances renamed to shardID

* typings: fix typings regarding WebSocket

* style(.eslintrc): remove additional whitespace

* fix(Client): remove ondisconnect handler on timeout

* docs(BaseClient): fix typo of Immediate

* nitpick: typings, private fields and methods

* typo: improve grammar a bit

* fix: error assigning client in WebSocketManager

* typo: actually spell milliseconds properly
2018-11-03 13:21:23 -05:00