-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Smart
committed
Dec 2, 2022
1 parent
1415b02
commit 85a7e8f
Showing
8 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { RawData } from "ws" | ||
|
||
export type Message = Discord.GatewayPayload | WS.Reconnect | ||
|
||
export interface OpenOpts { | ||
url?: string | ||
version?: number | ||
encoding?: Encoding | ||
} | ||
|
||
export interface Encoding { | ||
type: "json" | "etf" | ||
encode: (p: Discord.GatewayPayload) => string | Buffer | ArrayBuffer | ||
decode: (p: RawData) => Discord.GatewayPayload | ||
} | ||
|
||
export const jsonEncoding: Encoding = { | ||
type: "json", | ||
encode: (p) => JSON.stringify(p), | ||
decode: (p) => JSON.parse(p.toString("utf8")), | ||
} | ||
|
||
export const make = ({ | ||
url = "wss://gateway.discord.gg/", | ||
version = 9, | ||
encoding = jsonEncoding, | ||
}: OpenOpts) => { | ||
const ws = WS.make(`${url}?v=${version}&encoding=${encoding.type}`) | ||
|
||
const source = ws.source | ||
.tapError((e) => Log.info("DiscordWS", "ERROR", e)) | ||
.retry(Schedule.exponential(Duration.seconds(0.5))) | ||
.map(encoding.decode) | ||
|
||
const sink = ws.sink.map((msg: Message) => | ||
msg === WS.Reconnect ? msg : encoding.encode(msg), | ||
) | ||
|
||
return { source, sink } | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { asyncSink, unwrap, unwrapScope } from "callbag-effect-ts/Source" | ||
import { ClientOptions, RawData, WebSocket } from "ws" | ||
|
||
export const Reconnect = Symbol() | ||
export type Reconnect = typeof Reconnect | ||
export type Message = string | Buffer | ArrayBuffer | Reconnect | ||
|
||
const socket = (url: string, options?: ClientOptions) => | ||
Effect.sync(() => new WebSocket(url, options)).acquireRelease((ws) => | ||
Effect.sync(() => { | ||
ws.close() | ||
ws.removeAllListeners() | ||
}), | ||
) | ||
|
||
export class WebSocketError { | ||
readonly _tag = "WebSocketError" | ||
constructor(readonly reason: unknown) {} | ||
} | ||
|
||
export class WebSocketCloseError { | ||
readonly _tag = "WebSocketCloseError" | ||
constructor(readonly code: number, readonly reason: string) {} | ||
} | ||
|
||
const recv = (ws: WebSocket) => | ||
EffectSource.async<WebSocketError | WebSocketCloseError, RawData>((emit) => { | ||
ws.on("message", (message) => emit.data(message)) | ||
|
||
ws.on("error", (cause) => { | ||
emit.fail(new WebSocketError(cause)) | ||
}) | ||
|
||
ws.on("close", (code, reason) => | ||
emit.fail(new WebSocketCloseError(code, reason.toString("utf8"))), | ||
) | ||
}) | ||
|
||
export class WebSocketWriteError { | ||
readonly _tag = "WebSocketWriteError" | ||
constructor(readonly reason: Error) {} | ||
} | ||
|
||
const send = (ws: WebSocket, out: EffectSource<never, never, Message>) => | ||
pipe( | ||
Effect.async<never, never, void>((resume) => { | ||
if (ws.readyState & ws.OPEN) { | ||
resume(Effect.unit()) | ||
} else { | ||
ws.once("open", () => { | ||
resume(Effect.unit()) | ||
}) | ||
} | ||
}).map(() => out), | ||
unwrap, | ||
) | ||
.tap((p) => Log.debug("WS", "send", p)) | ||
.tap((data) => | ||
Effect.async<never, WebSocketWriteError, void>((resume) => { | ||
if (data === Reconnect) { | ||
ws.close(1012, "reconnecting") | ||
resume(Effect.unit()) | ||
} else { | ||
ws.send(data, (err) => { | ||
resume( | ||
err ? Effect.fail(new WebSocketWriteError(err!)) : Effect.unit(), | ||
) | ||
}) | ||
} | ||
}), | ||
).drain | ||
|
||
export const make = (url: string, options?: ClientOptions) => { | ||
const [sink, outbound] = asyncSink<never, Message>() | ||
|
||
const source = pipe( | ||
socket(url, options).map((ws) => recv(ws).merge(send(ws, outbound))), | ||
unwrapScope, | ||
).retry( | ||
Schedule.recurWhile( | ||
(e) => e._tag === "WebSocketCloseError" && e.code === 1012, | ||
), | ||
) | ||
|
||
return { source, sink } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters