revolt.js
Advanced tools
Comparing version 7.0.0-beta.2 to 7.0.0-beta.3
@@ -1,4 +0,3 @@ | ||
/// <reference types="node" /> | ||
import EventEmitter from "events"; | ||
import type TypedEmitter from "typed-emitter"; | ||
import { Accessor } from "solid-js"; | ||
import EventEmitter from "eventemitter3"; | ||
import type { AvailableProtocols, EventProtocol } from "."; | ||
@@ -15,2 +14,22 @@ /** | ||
/** | ||
* Event client options object | ||
*/ | ||
export interface EventClientOptions { | ||
/** | ||
* Whether to log events | ||
* @default false | ||
*/ | ||
debug: boolean; | ||
/** | ||
* Time in seconds between Ping packets sent to the server | ||
* @default 30 | ||
*/ | ||
heartbeatInterval: number; | ||
/** | ||
* Maximum time in seconds between Ping and corresponding Pong | ||
* @default 10 | ||
*/ | ||
pongTimeout: number; | ||
} | ||
/** | ||
* Events provided by the client. | ||
@@ -26,4 +45,7 @@ */ | ||
*/ | ||
declare class Client<T extends AvailableProtocols, P extends EventProtocol<T>> extends EventEmitter { | ||
export declare class EventClient<T extends AvailableProtocols> extends EventEmitter<Events<T, EventProtocol<T>>> { | ||
#private; | ||
readonly options: EventClientOptions; | ||
readonly ping: Accessor<number>; | ||
readonly state: Accessor<ConnectionState>; | ||
/** | ||
@@ -33,6 +55,5 @@ * Create a new event client. | ||
* @param transportFormat Communication format | ||
* @param heartbeatInterval Interval in seconds to send ping | ||
* @param pongTimeout Time in seconds until heartbeat times out | ||
* @param options Configuration options | ||
*/ | ||
constructor(protocolVersion: T, transportFormat?: "json", heartbeatInterval?: number, pongTimeout?: number); | ||
constructor(protocolVersion: T, transportFormat?: "json", options?: EventClientOptions); | ||
/** | ||
@@ -57,3 +78,3 @@ * Set the current state | ||
*/ | ||
send(event: P["client"]): void; | ||
send(event: EventProtocol<T>["client"]): void; | ||
/** | ||
@@ -63,17 +84,5 @@ * Handle events intended for client before passing them along. | ||
*/ | ||
handle(event: P["server"]): void; | ||
handle(event: EventProtocol<T>["server"]): void; | ||
} | ||
/** | ||
* Event client | ||
*/ | ||
export type EventClient<T extends AvailableProtocols> = Omit<Client<T, EventProtocol<T>>, "on" | "once"> & TypedEmitter<Events<T, EventProtocol<T>>>; | ||
/** | ||
* Create a new event client. | ||
* @param protocolVersion Target protocol version | ||
* @param transportFormat Communication format | ||
* @param heartbeatInterval Interval in seconds to send ping | ||
* @param pongTimeout Time in seconds until heartbeat times out | ||
*/ | ||
export declare function createEventClient<T extends AvailableProtocols, P extends EventProtocol<T>>(protocolVersion: T, transportFormat?: "json", heartbeatInterval?: number, pongTimeout?: number): EventClient<T>; | ||
export {}; | ||
//# sourceMappingURL=client.d.ts.map |
@@ -16,7 +16,7 @@ "use strict"; | ||
}; | ||
var _Client_protocolVersion, _Client_transportFormat, _Client_heartbeatInterval, _Client_pongTimeout, _Client_state, _Client_setStateSetter, _Client_socket, _Client_heartbeatIntervalReference, _Client_pongTimeoutReference; | ||
var _EventClient_protocolVersion, _EventClient_transportFormat, _EventClient_setPing, _EventClient_setStateSetter, _EventClient_socket, _EventClient_heartbeatIntervalReference, _EventClient_pongTimeoutReference; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createEventClient = exports.ConnectionState = void 0; | ||
exports.EventClient = exports.ConnectionState = void 0; | ||
const solid_js_1 = require("solid-js"); | ||
const events_1 = __importDefault(require("events")); | ||
const eventemitter3_1 = __importDefault(require("eventemitter3")); | ||
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws")); | ||
@@ -36,3 +36,3 @@ /** | ||
*/ | ||
class Client extends events_1.default { | ||
class EventClient extends eventemitter3_1.default { | ||
/** | ||
@@ -42,23 +42,22 @@ * Create a new event client. | ||
* @param transportFormat Communication format | ||
* @param heartbeatInterval Interval in seconds to send ping | ||
* @param pongTimeout Time in seconds until heartbeat times out | ||
* @param options Configuration options | ||
*/ | ||
constructor(protocolVersion, transportFormat = "json", heartbeatInterval = 30, pongTimeout = 10) { | ||
constructor(protocolVersion, transportFormat = "json", options) { | ||
super(); | ||
_Client_protocolVersion.set(this, void 0); | ||
_Client_transportFormat.set(this, void 0); | ||
_Client_heartbeatInterval.set(this, void 0); | ||
_Client_pongTimeout.set(this, void 0); | ||
_Client_state.set(this, void 0); | ||
_Client_setStateSetter.set(this, void 0); | ||
_Client_socket.set(this, void 0); | ||
_Client_heartbeatIntervalReference.set(this, void 0); | ||
_Client_pongTimeoutReference.set(this, void 0); | ||
__classPrivateFieldSet(this, _Client_protocolVersion, protocolVersion, "f"); | ||
__classPrivateFieldSet(this, _Client_transportFormat, transportFormat, "f"); | ||
__classPrivateFieldSet(this, _Client_heartbeatInterval, heartbeatInterval, "f"); | ||
__classPrivateFieldSet(this, _Client_pongTimeout, pongTimeout, "f"); | ||
_EventClient_protocolVersion.set(this, void 0); | ||
_EventClient_transportFormat.set(this, void 0); | ||
_EventClient_setPing.set(this, void 0); | ||
_EventClient_setStateSetter.set(this, void 0); | ||
_EventClient_socket.set(this, void 0); | ||
_EventClient_heartbeatIntervalReference.set(this, void 0); | ||
_EventClient_pongTimeoutReference.set(this, void 0); | ||
__classPrivateFieldSet(this, _EventClient_protocolVersion, protocolVersion, "f"); | ||
__classPrivateFieldSet(this, _EventClient_transportFormat, transportFormat, "f"); | ||
this.options = Object.assign({ heartbeatInterval: 30, pongTimeout: 10, debug: false }, options); | ||
const [state, setState] = (0, solid_js_1.createSignal)(ConnectionState.Idle); | ||
__classPrivateFieldSet(this, _Client_state, state, "f"); | ||
__classPrivateFieldSet(this, _Client_setStateSetter, setState, "f"); | ||
this.state = state; | ||
__classPrivateFieldSet(this, _EventClient_setStateSetter, setState, "f"); | ||
const [ping, setPing] = (0, solid_js_1.createSignal)(-1); | ||
this.ping = ping; | ||
__classPrivateFieldSet(this, _EventClient_setPing, setPing, "f"); | ||
this.disconnect = this.disconnect.bind(this); | ||
@@ -71,5 +70,4 @@ } | ||
setState(state) { | ||
__classPrivateFieldGet(this, _Client_setStateSetter, "f").call(this, state); | ||
__classPrivateFieldGet(this, _EventClient_setStateSetter, "f").call(this, state); | ||
this.emit("state", state); | ||
// TODO: debug weird state changes | ||
} | ||
@@ -84,11 +82,14 @@ /** | ||
this.setState(ConnectionState.Connecting); | ||
__classPrivateFieldSet(this, _Client_socket, new isomorphic_ws_1.default(`${uri}?version=${__classPrivateFieldGet(this, _Client_protocolVersion, "f")}&format=${__classPrivateFieldGet(this, _Client_transportFormat, "f")}&token=${token}`), "f"); | ||
__classPrivateFieldGet(this, _Client_socket, "f").onopen = () => { | ||
__classPrivateFieldSet(this, _Client_heartbeatIntervalReference, setInterval(() => (__classPrivateFieldSet(this, _Client_pongTimeoutReference, setTimeout(this.disconnect, __classPrivateFieldGet(this, _Client_pongTimeout, "f") * 1e3), "f")), __classPrivateFieldGet(this, _Client_heartbeatInterval, "f") & 1e3), "f"); | ||
__classPrivateFieldSet(this, _EventClient_socket, new isomorphic_ws_1.default(`${uri}?version=${__classPrivateFieldGet(this, _EventClient_protocolVersion, "f")}&format=${__classPrivateFieldGet(this, _EventClient_transportFormat, "f")}&token=${token}`), "f"); | ||
__classPrivateFieldGet(this, _EventClient_socket, "f").onopen = () => { | ||
__classPrivateFieldSet(this, _EventClient_heartbeatIntervalReference, setInterval(() => { | ||
this.send({ type: "Ping", data: +new Date() }); | ||
__classPrivateFieldSet(this, _EventClient_pongTimeoutReference, setTimeout(() => this.disconnect(), this.options.pongTimeout * 1e3), "f"); | ||
}, this.options.heartbeatInterval * 1e3), "f"); | ||
}; | ||
__classPrivateFieldGet(this, _Client_socket, "f").onerror = (error) => { | ||
__classPrivateFieldGet(this, _EventClient_socket, "f").onerror = (error) => { | ||
this.emit("error", error); | ||
}; | ||
__classPrivateFieldGet(this, _Client_socket, "f").onmessage = (event) => { | ||
if (__classPrivateFieldGet(this, _Client_transportFormat, "f") === "json") { | ||
__classPrivateFieldGet(this, _EventClient_socket, "f").onmessage = (event) => { | ||
if (__classPrivateFieldGet(this, _EventClient_transportFormat, "f") === "json") { | ||
if (typeof event.data === "string") { | ||
@@ -99,5 +100,8 @@ this.handle(JSON.parse(event.data)); | ||
}; | ||
__classPrivateFieldGet(this, _Client_socket, "f").onclose = () => { | ||
clearInterval(__classPrivateFieldGet(this, _Client_heartbeatIntervalReference, "f")); | ||
this.setState(ConnectionState.Disconnected); | ||
let closed = false; | ||
__classPrivateFieldGet(this, _EventClient_socket, "f").onclose = () => { | ||
if (closed) | ||
return; | ||
closed = true; | ||
this.disconnect(); | ||
}; | ||
@@ -109,5 +113,9 @@ } | ||
disconnect() { | ||
if (!__classPrivateFieldGet(this, _Client_socket, "f")) | ||
if (!__classPrivateFieldGet(this, _EventClient_socket, "f")) | ||
return; | ||
__classPrivateFieldGet(this, _Client_socket, "f").close(); | ||
clearInterval(__classPrivateFieldGet(this, _EventClient_heartbeatIntervalReference, "f")); | ||
const socket = __classPrivateFieldGet(this, _EventClient_socket, "f"); | ||
__classPrivateFieldSet(this, _EventClient_socket, undefined, "f"); | ||
socket.close(); | ||
this.setState(ConnectionState.Disconnected); | ||
} | ||
@@ -119,3 +127,6 @@ /** | ||
send(event) { | ||
console.info(event); | ||
this.options.debug && console.info("[C->S]", event); | ||
if (!__classPrivateFieldGet(this, _EventClient_socket, "f")) | ||
throw "Socket closed, trying to send."; | ||
__classPrivateFieldGet(this, _EventClient_socket, "f").send(JSON.stringify(event)); | ||
} | ||
@@ -127,2 +138,3 @@ /** | ||
handle(event) { | ||
this.options.debug && console.info("[S->C]", event); | ||
switch (event.type) { | ||
@@ -136,3 +148,5 @@ case "Ping": | ||
case "Pong": | ||
clearTimeout(__classPrivateFieldGet(this, _Client_pongTimeoutReference, "f")); | ||
clearTimeout(__classPrivateFieldGet(this, _EventClient_pongTimeoutReference, "f")); | ||
__classPrivateFieldGet(this, _EventClient_setPing, "f").call(this, +new Date() - event.data); | ||
this.options.debug && console.info(`[ping] ${this.ping()}ms`); | ||
return; | ||
@@ -144,3 +158,3 @@ case "Error": | ||
} | ||
switch (__classPrivateFieldGet(this, _Client_state, "f").call(this)) { | ||
switch (this.state()) { | ||
case ConnectionState.Connecting: | ||
@@ -155,3 +169,3 @@ if (event.type === "Authenticated") { | ||
else { | ||
console.error("WE ARE IN WRONG STATE"); | ||
throw `Unreachable code. Received ${event.type} in Connecting state.`; | ||
} | ||
@@ -168,17 +182,7 @@ break; | ||
default: | ||
throw `Unreachable code. Received ${event.type} in state ${__classPrivateFieldGet(this, _Client_state, "f").call(this)}.`; | ||
throw `Unreachable code. Received ${event.type} in state ${this.state()}.`; | ||
} | ||
} | ||
} | ||
_Client_protocolVersion = new WeakMap(), _Client_transportFormat = new WeakMap(), _Client_heartbeatInterval = new WeakMap(), _Client_pongTimeout = new WeakMap(), _Client_state = new WeakMap(), _Client_setStateSetter = new WeakMap(), _Client_socket = new WeakMap(), _Client_heartbeatIntervalReference = new WeakMap(), _Client_pongTimeoutReference = new WeakMap(); | ||
/** | ||
* Create a new event client. | ||
* @param protocolVersion Target protocol version | ||
* @param transportFormat Communication format | ||
* @param heartbeatInterval Interval in seconds to send ping | ||
* @param pongTimeout Time in seconds until heartbeat times out | ||
*/ | ||
function createEventClient(protocolVersion, transportFormat, heartbeatInterval, pongTimeout) { | ||
return new Client(protocolVersion, transportFormat, heartbeatInterval, pongTimeout); | ||
} | ||
exports.createEventClient = createEventClient; | ||
exports.EventClient = EventClient; | ||
_EventClient_protocolVersion = new WeakMap(), _EventClient_transportFormat = new WeakMap(), _EventClient_setPing = new WeakMap(), _EventClient_setStateSetter = new WeakMap(), _EventClient_socket = new WeakMap(), _EventClient_heartbeatIntervalReference = new WeakMap(), _EventClient_pongTimeoutReference = new WeakMap(); |
{ | ||
"name": "revolt.js", | ||
"version": "7.0.0-beta.2", | ||
"version": "7.0.0-beta.3", | ||
"main": "lib/cjs/index.js", | ||
@@ -10,2 +10,14 @@ "module": "lib/esm/index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc && tsc -p tsconfig.cjs.json", | ||
"build:esm": "tsc", | ||
"build:cjs": "tsc -p tsconfig.cjs.json", | ||
"build:watch": "tsc-watch --onSuccess \"node .\"", | ||
"lint": "eslint --ext .ts,.tsx src/", | ||
"lint:fix": "eslint --fix --ext .ts,.tsx src/", | ||
"typecheck": "tsc --noEmit", | ||
"docs": "typedoc --plugin @mxssfd/typedoc-theme --theme my-theme --readme README.md src/", | ||
"fmt": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'", | ||
"fmt:check": "prettier --check 'src/**/*.{js,jsx,ts,tsx}'" | ||
}, | ||
"files": [ | ||
@@ -45,15 +57,3 @@ "README.md", | ||
"typescript": "^5.0.3" | ||
}, | ||
"scripts": { | ||
"build": "tsc && tsc -p tsconfig.cjs.json", | ||
"build:esm": "tsc", | ||
"build:cjs": "tsc -p tsconfig.cjs.json", | ||
"build:watch": "tsc-watch --onSuccess \"node .\"", | ||
"lint": "eslint --ext .ts,.tsx src/", | ||
"lint:fix": "eslint --fix --ext .ts,.tsx src/", | ||
"typecheck": "tsc --noEmit", | ||
"docs": "typedoc --plugin @mxssfd/typedoc-theme --theme my-theme --readme README.md src/", | ||
"fmt": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'", | ||
"fmt:check": "prettier --check 'src/**/*.{js,jsx,ts,tsx}'" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1836283
411
47045
0