partysocket
Advanced tools
Comparing version 0.0.0-7c698a1 to 0.0.0-7eb3b36
@@ -1,418 +0,3 @@ | ||
// src/events.ts | ||
var Event = class { | ||
target; | ||
type; | ||
constructor(type, target) { | ||
this.target = target; | ||
this.type = type; | ||
} | ||
}; | ||
var ErrorEvent = class extends Event { | ||
message; | ||
error; | ||
constructor(error, target) { | ||
super("error", target); | ||
this.message = error.message; | ||
this.error = error; | ||
} | ||
}; | ||
var CloseEvent = class extends Event { | ||
code; | ||
reason; | ||
wasClean = true; | ||
constructor(code = 1e3, reason = "", target) { | ||
super("close", target); | ||
this.code = code; | ||
this.reason = reason; | ||
} | ||
}; | ||
// src/reconnecting-websocket.ts | ||
function assert(condition, msg) { | ||
if (!condition) { | ||
throw new Error(msg); | ||
} | ||
} | ||
function isWebSocket(w) { | ||
return typeof w !== "undefined" && !!w && typeof w === "function" && "CLOSING" in w && w.CLOSING === 2; | ||
} | ||
var DEFAULT = { | ||
maxReconnectionDelay: 1e4, | ||
minReconnectionDelay: 1e3 + Math.random() * 4e3, | ||
minUptime: 5e3, | ||
reconnectionDelayGrowFactor: 1.3, | ||
connectionTimeout: 4e3, | ||
maxRetries: Infinity, | ||
maxEnqueuedMessages: Infinity, | ||
startClosed: false, | ||
debug: false | ||
}; | ||
var ReconnectingWebSocket = class { | ||
_ws; | ||
_listeners = { | ||
error: [], | ||
message: [], | ||
open: [], | ||
close: [] | ||
}; | ||
_retryCount = -1; | ||
_uptimeTimeout; | ||
_connectTimeout; | ||
_shouldReconnect = true; | ||
_connectLock = false; | ||
_binaryType = "blob"; | ||
_closeCalled = false; | ||
_messageQueue = []; | ||
_url; | ||
_protocols; | ||
_options; | ||
constructor(url, protocols, options = {}) { | ||
this._url = url; | ||
this._protocols = protocols; | ||
this._options = options; | ||
if (this._options.startClosed) { | ||
this._shouldReconnect = false; | ||
} | ||
this._connect(); | ||
} | ||
static get CONNECTING() { | ||
return 0; | ||
} | ||
static get OPEN() { | ||
return 1; | ||
} | ||
static get CLOSING() { | ||
return 2; | ||
} | ||
static get CLOSED() { | ||
return 3; | ||
} | ||
get CONNECTING() { | ||
return ReconnectingWebSocket.CONNECTING; | ||
} | ||
get OPEN() { | ||
return ReconnectingWebSocket.OPEN; | ||
} | ||
get CLOSING() { | ||
return ReconnectingWebSocket.CLOSING; | ||
} | ||
get CLOSED() { | ||
return ReconnectingWebSocket.CLOSED; | ||
} | ||
get binaryType() { | ||
return this._ws ? this._ws.binaryType : this._binaryType; | ||
} | ||
set binaryType(value) { | ||
this._binaryType = value; | ||
if (this._ws) { | ||
this._ws.binaryType = value; | ||
} | ||
} | ||
get retryCount() { | ||
return Math.max(this._retryCount, 0); | ||
} | ||
get bufferedAmount() { | ||
const bytes = this._messageQueue.reduce((acc, message) => { | ||
if (typeof message === "string") { | ||
acc += message.length; | ||
} else if (message instanceof Blob) { | ||
acc += message.size; | ||
} else { | ||
acc += message.byteLength; | ||
} | ||
return acc; | ||
}, 0); | ||
return bytes + (this._ws ? this._ws.bufferedAmount : 0); | ||
} | ||
get extensions() { | ||
return this._ws ? this._ws.extensions : ""; | ||
} | ||
get protocol() { | ||
return this._ws ? this._ws.protocol : ""; | ||
} | ||
get readyState() { | ||
if (this._ws) { | ||
return this._ws.readyState; | ||
} | ||
return this._options.startClosed ? ReconnectingWebSocket.CLOSED : ReconnectingWebSocket.CONNECTING; | ||
} | ||
get url() { | ||
return this._ws ? this._ws.url : ""; | ||
} | ||
get shouldReconnect() { | ||
return this._shouldReconnect; | ||
} | ||
onclose = null; | ||
onerror = null; | ||
onmessage = null; | ||
onopen = null; | ||
close(code = 1e3, reason) { | ||
this._closeCalled = true; | ||
this._shouldReconnect = false; | ||
this._clearTimeouts(); | ||
if (!this._ws) { | ||
this._debug("close enqueued: no ws instance"); | ||
return; | ||
} | ||
if (this._ws.readyState === this.CLOSED) { | ||
this._debug("close: already closed"); | ||
return; | ||
} | ||
this._ws.close(code, reason); | ||
} | ||
reconnect(code, reason) { | ||
this._shouldReconnect = true; | ||
this._closeCalled = false; | ||
this._retryCount = -1; | ||
if (!this._ws || this._ws.readyState === this.CLOSED) { | ||
this._connect(); | ||
} else { | ||
this._disconnect(code, reason); | ||
this._connect(); | ||
} | ||
} | ||
send(data) { | ||
if (this._ws && this._ws.readyState === this.OPEN) { | ||
this._debug("send", data); | ||
this._ws.send(data); | ||
} else { | ||
const { maxEnqueuedMessages = DEFAULT.maxEnqueuedMessages } = this._options; | ||
if (this._messageQueue.length < maxEnqueuedMessages) { | ||
this._debug("enqueue", data); | ||
this._messageQueue.push(data); | ||
} | ||
} | ||
} | ||
addEventListener(type, listener) { | ||
if (this._listeners[type]) { | ||
this._listeners[type].push(listener); | ||
} | ||
} | ||
dispatchEvent(event) { | ||
const listeners = this._listeners[event.type]; | ||
if (listeners) { | ||
for (const listener of listeners) { | ||
this._callEventListener(event, listener); | ||
} | ||
} | ||
return true; | ||
} | ||
removeEventListener(type, listener) { | ||
if (this._listeners[type]) { | ||
this._listeners[type] = this._listeners[type].filter( | ||
(l) => l !== listener | ||
); | ||
} | ||
} | ||
_debug(...args) { | ||
if (this._options.debug) { | ||
console.log.apply(console, ["RWS>", ...args]); | ||
} | ||
} | ||
_getNextDelay() { | ||
const { | ||
reconnectionDelayGrowFactor = DEFAULT.reconnectionDelayGrowFactor, | ||
minReconnectionDelay = DEFAULT.minReconnectionDelay, | ||
maxReconnectionDelay = DEFAULT.maxReconnectionDelay | ||
} = this._options; | ||
let delay = 0; | ||
if (this._retryCount > 0) { | ||
delay = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1); | ||
if (delay > maxReconnectionDelay) { | ||
delay = maxReconnectionDelay; | ||
} | ||
} | ||
this._debug("next delay", delay); | ||
return delay; | ||
} | ||
_wait() { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, this._getNextDelay()); | ||
}); | ||
} | ||
_getNextProtocols(protocolsProvider) { | ||
if (!protocolsProvider) | ||
return Promise.resolve(null); | ||
if (typeof protocolsProvider === "string" || Array.isArray(protocolsProvider)) { | ||
return Promise.resolve(protocolsProvider); | ||
} | ||
if (typeof protocolsProvider === "function") { | ||
const protocols = protocolsProvider(); | ||
if (!protocols) | ||
return Promise.resolve(null); | ||
if (typeof protocols === "string" || Array.isArray(protocols)) { | ||
return Promise.resolve(protocols); | ||
} | ||
if (protocols.then) { | ||
return protocols; | ||
} | ||
} | ||
throw Error("Invalid protocols"); | ||
} | ||
_getNextUrl(urlProvider) { | ||
if (typeof urlProvider === "string") { | ||
return Promise.resolve(urlProvider); | ||
} | ||
if (typeof urlProvider === "function") { | ||
const url = urlProvider(); | ||
if (typeof url === "string") { | ||
return Promise.resolve(url); | ||
} | ||
if (url.then) { | ||
return url; | ||
} | ||
} | ||
throw Error("Invalid URL"); | ||
} | ||
_connect() { | ||
if (this._connectLock || !this._shouldReconnect) { | ||
return; | ||
} | ||
this._connectLock = true; | ||
const { | ||
maxRetries = DEFAULT.maxRetries, | ||
connectionTimeout = DEFAULT.connectionTimeout | ||
} = this._options; | ||
if (this._retryCount >= maxRetries) { | ||
this._debug("max retries reached", this._retryCount, ">=", maxRetries); | ||
return; | ||
} | ||
this._retryCount++; | ||
this._debug("connect", this._retryCount); | ||
this._removeListeners(); | ||
if (!isWebSocket(WebSocket)) { | ||
throw Error("No valid WebSocket class provided"); | ||
} | ||
this._wait().then( | ||
() => Promise.all([ | ||
this._getNextUrl(this._url), | ||
this._getNextProtocols(this._protocols || null) | ||
]) | ||
).then(([url, protocols]) => { | ||
if (this._closeCalled) { | ||
this._connectLock = false; | ||
return; | ||
} | ||
this._debug("connect", { url, protocols }); | ||
this._ws = protocols ? new WebSocket(url, protocols) : new WebSocket(url); | ||
this._ws.binaryType = this._binaryType; | ||
this._connectLock = false; | ||
this._addListeners(); | ||
this._connectTimeout = setTimeout( | ||
() => this._handleTimeout(), | ||
connectionTimeout | ||
); | ||
}).catch((err) => { | ||
this._connectLock = false; | ||
this._handleError(new ErrorEvent(Error(err.message), this)); | ||
}); | ||
} | ||
_handleTimeout() { | ||
this._debug("timeout event"); | ||
this._handleError(new ErrorEvent(Error("TIMEOUT"), this)); | ||
} | ||
_disconnect(code = 1e3, reason) { | ||
this._clearTimeouts(); | ||
if (!this._ws) { | ||
return; | ||
} | ||
this._removeListeners(); | ||
try { | ||
this._ws.close(code, reason); | ||
this._handleClose(new CloseEvent(code, reason, this)); | ||
} catch (error) { | ||
} | ||
} | ||
_acceptOpen() { | ||
this._debug("accept open"); | ||
this._retryCount = 0; | ||
} | ||
_callEventListener(event, listener) { | ||
if ("handleEvent" in listener) { | ||
listener.handleEvent(event); | ||
} else { | ||
listener(event); | ||
} | ||
} | ||
_handleOpen = (event) => { | ||
this._debug("open event"); | ||
const { minUptime = DEFAULT.minUptime } = this._options; | ||
clearTimeout(this._connectTimeout); | ||
this._uptimeTimeout = setTimeout(() => this._acceptOpen(), minUptime); | ||
assert(this._ws, "WebSocket is not defined"); | ||
this._ws.binaryType = this._binaryType; | ||
this._messageQueue.forEach((message) => this._ws?.send(message)); | ||
this._messageQueue = []; | ||
if (this.onopen) { | ||
this.onopen(event); | ||
} | ||
this._listeners.open.forEach( | ||
(listener) => this._callEventListener(event, listener) | ||
); | ||
}; | ||
_handleMessage = (event) => { | ||
this._debug("message event"); | ||
if (this.onmessage) { | ||
this.onmessage(event); | ||
} | ||
this._listeners.message.forEach( | ||
(listener) => this._callEventListener(event, listener) | ||
); | ||
}; | ||
_handleError = (event) => { | ||
this._debug("error event", event.message); | ||
this._disconnect( | ||
void 0, | ||
event.message === "TIMEOUT" ? "timeout" : void 0 | ||
); | ||
if (this.onerror) { | ||
this.onerror(event); | ||
} | ||
this._debug("exec error listeners"); | ||
this._listeners.error.forEach( | ||
(listener) => this._callEventListener(event, listener) | ||
); | ||
this._connect(); | ||
}; | ||
_handleClose = (event) => { | ||
this._debug("close event"); | ||
this._clearTimeouts(); | ||
if (this._shouldReconnect) { | ||
this._connect(); | ||
} | ||
if (this.onclose) { | ||
this.onclose(event); | ||
} | ||
this._listeners.close.forEach( | ||
(listener) => this._callEventListener(event, listener) | ||
); | ||
}; | ||
_removeListeners() { | ||
if (!this._ws) { | ||
return; | ||
} | ||
this._debug("removeListeners"); | ||
this._ws.removeEventListener("open", this._handleOpen); | ||
this._ws.removeEventListener("close", this._handleClose); | ||
this._ws.removeEventListener("message", this._handleMessage); | ||
this._ws.removeEventListener("error", this._handleError); | ||
} | ||
_addListeners() { | ||
if (!this._ws) { | ||
return; | ||
} | ||
this._debug("addListeners"); | ||
this._ws.addEventListener("open", this._handleOpen); | ||
this._ws.addEventListener("close", this._handleClose); | ||
this._ws.addEventListener("message", this._handleMessage); | ||
this._ws.addEventListener("error", this._handleError); | ||
} | ||
_clearTimeouts() { | ||
clearTimeout(this._connectTimeout); | ||
clearTimeout(this._uptimeTimeout); | ||
} | ||
}; | ||
// src/index.ts | ||
var PartySocket = class extends ReconnectingWebSocket { | ||
import ReconnectingWebSocket from "./ws"; | ||
class PartySocket extends ReconnectingWebSocket { | ||
constructor(partySocketOptions) { | ||
@@ -432,11 +17,5 @@ const { host, room, protocol, query, protocols, ...socketOptions } = partySocketOptions; | ||
_pk; | ||
}; | ||
} | ||
export { | ||
PartySocket as default | ||
}; | ||
/*! | ||
* Reconnecting WebSocket | ||
* by Pedro Ladaria <pedro.ladaria@gmail.com> | ||
* https://github.com/pladaria/reconnecting-websocket | ||
* License MIT | ||
*/ |
@@ -1,3 +0,3 @@ | ||
import type * as RWS from "./reconnecting-websocket"; | ||
import ReconnectingWebSocket from "./reconnecting-websocket"; | ||
import type * as RWS from "./ws"; | ||
import ReconnectingWebSocket from "./ws"; | ||
export type PartySocketOptions = Omit<RWS.Options, "WebSocket" | "constructor"> & { | ||
@@ -4,0 +4,0 @@ host: string; |
{ | ||
"name": "partysocket", | ||
"version": "0.0.0-7c698a1", | ||
"version": "0.0.0-7eb3b36", | ||
"description": "party hotline", | ||
@@ -8,2 +8,3 @@ "main": "dist/index.js", | ||
".": "./dist/index.js", | ||
"./ws": "./dist/ws.js", | ||
"./react": "./dist/react.js" | ||
@@ -13,3 +14,4 @@ }, | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "rm -rf dist && npx esbuild src/index.ts --bundle --format=esm --outfile=dist/index.js && npx esbuild src/react.ts --format=esm --outfile=dist/react.js && tsc --project tsconfig.extract.json && mv dist/*.d.ts* ." | ||
"clean": "rm -rf dist && rm -rf *.d.ts*", | ||
"build": "npm run clean && npx esbuild src/index.ts src/react.ts src/ws.ts --format=esm --outdir=dist && tsc --project tsconfig.extract.json && mv dist/*.d.ts* ." | ||
}, | ||
@@ -16,0 +18,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
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
662
30625
11
1