partysocket
Advanced tools
Comparing version 0.0.0-39e58dc to 0.0.0-3abd989
@@ -1,15 +0,45 @@ | ||
import type * as RWS from "./ws"; | ||
import ReconnectingWebSocket from "./ws"; | ||
export type PartySocketOptions = Omit<RWS.Options, "WebSocket" | "constructor"> & { | ||
import ReconnectingWebSocket, { Options } from './ws.js'; | ||
type Maybe<T> = T | null | undefined; | ||
type Params = Record<string, Maybe<string>>; | ||
type PartySocketOptions = Omit<Options, "constructor"> & { | ||
id?: string; | ||
host: string; | ||
room: string; | ||
protocol?: string; | ||
room?: string; | ||
party?: string; | ||
protocol?: "ws" | "wss"; | ||
protocols?: string[]; | ||
query?: Record<string, string>; | ||
path?: string; | ||
query?: Params | (() => Params | Promise<Params>); | ||
}; | ||
export default class PartySocket extends ReconnectingWebSocket { | ||
type PartyFetchOptions = { | ||
host: string; | ||
room: string; | ||
party?: string; | ||
path?: string; | ||
protocol?: "http" | "https"; | ||
query?: Params | (() => Params | Promise<Params>); | ||
fetch?: typeof fetch; | ||
}; | ||
declare class PartySocket extends ReconnectingWebSocket { | ||
readonly partySocketOptions: PartySocketOptions; | ||
_pk: string; | ||
_pkurl: string; | ||
name: string; | ||
room?: string; | ||
host: string; | ||
path: string; | ||
constructor(partySocketOptions: PartySocketOptions); | ||
updateProperties(partySocketOptions: Partial<PartySocketOptions>): void; | ||
private setWSProperties; | ||
reconnect(code?: number | undefined, reason?: string | undefined): void; | ||
get id(): string; | ||
/** | ||
* Exposes the static PartyKit room URL without applying query parameters. | ||
* To access the currently connected WebSocket url, use PartySocket#url. | ||
*/ | ||
get roomUrl(): string; | ||
static fetch(options: PartyFetchOptions, init?: RequestInit): Promise<Response>; | ||
} | ||
//# sourceMappingURL=index.d.ts.map | ||
export { type PartyFetchOptions, PartySocket, type PartySocketOptions, ReconnectingWebSocket as WebSocket, PartySocket as default }; |
{ | ||
"name": "partysocket", | ||
"version": "0.0.0-39e58dc", | ||
"description": "party hotline", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"version": "0.0.0-3abd989", | ||
"description": "A better WebSocket that Just Works™", | ||
"homepage": "https://docs.partykit.io/reference/partysocket-api", | ||
"bugs": "https://github.com/partykit/partykit/issues", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.js" | ||
"types": "./index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"./ws": { | ||
"import": "./dist/esm/ws.js", | ||
"require": "./dist/cjs/ws.js" | ||
"types": "./ws.d.ts", | ||
"import": "./dist/ws.mjs", | ||
"require": "./dist/ws.js" | ||
}, | ||
"./react": { | ||
"import": "./dist/esm/react.js", | ||
"require": "./dist/cjs/react.js" | ||
"types": "./react.d.ts", | ||
"import": "./dist/react.mjs", | ||
"require": "./dist/react.js" | ||
}, | ||
"./use-ws": { | ||
"types": "./use-ws.d.ts", | ||
"import": "./dist/use-ws.mjs", | ||
"require": "./dist/use-ws.js" | ||
}, | ||
"./event-target-polyfill": { | ||
"types": "./event-target-polyfill.d.ts", | ||
"import": "./dist/event-target-polyfill.mjs", | ||
"require": "./dist/event-target-polyfill.js" | ||
} | ||
}, | ||
"tsup": { | ||
"entry": [ | ||
"src/index.ts", | ||
"src/react.ts", | ||
"src/ws.ts", | ||
"src/use-ws.ts", | ||
"src/event-target-polyfill.ts" | ||
], | ||
"format": [ | ||
"esm", | ||
"cjs" | ||
], | ||
"dts": true | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"clean": "rm -rf dist && rm -rf *.d.ts*", | ||
"build:cjs": "npx esbuild src/index.ts src/react.ts src/ws.ts --format=cjs --outdir=dist/cjs", | ||
"build:esm": "npx esbuild src/index.ts src/react.ts src/ws.ts --format=esm --outdir=dist/esm", | ||
"build": "npm run clean && npm run build:cjs && npm run build:esm && tsc --project tsconfig.extract.json && mv dist/*.d.ts* ." | ||
"clean": "shx rm -rf dist *.d.ts *.d.mts event-target-polyfill.*", | ||
"post-build": "shx mv dist/*.d.ts dist/*.d.mts* . && shx mv dist/event-target-polyfill.* .", | ||
"build": "npm run clean && tsup --external react && npm run post-build", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"files": [ | ||
"dist/**/*.js", | ||
"dist", | ||
"*.d.ts", | ||
"*.d.ts.map" | ||
"event-target-polyfill.*" | ||
], | ||
"keywords": [], | ||
"keywords": [ | ||
"websocket", | ||
"client", | ||
"reconnecting", | ||
"reconnection", | ||
"reconnect", | ||
"forever", | ||
"persistent", | ||
"forever", | ||
"automatic" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/partykit/partykit.git", | ||
"directory": "packages/partykit" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"dependencies": { | ||
"event-target-shim": "^6.0.2" | ||
} | ||
} |
@@ -1,4 +0,11 @@ | ||
import type { PartySocketOptions } from '.'; | ||
import PartySocket from '.'; | ||
export default function usePartySocket(options: PartySocketOptions): PartySocket; | ||
//# sourceMappingURL=react.d.ts.map | ||
import { PartySocket, PartySocketOptions } from './index.js'; | ||
import { E as EventHandlerOptions } from './use-ws-CnrrNZS2.js'; | ||
export { u as useWebSocket } from './use-ws-CnrrNZS2.js'; | ||
import './ws.js'; | ||
type UsePartySocketOptions = Omit<PartySocketOptions, "host"> & EventHandlerOptions & { | ||
host?: string | undefined; | ||
}; | ||
declare function usePartySocket(options: UsePartySocketOptions): PartySocket; | ||
export { usePartySocket as default, usePartySocket }; |
110
README.md
@@ -1,35 +0,26 @@ | ||
- https://github.com/pladaria/reconnecting-websocket/pull/166 Fix: handle error if getNextUrl throws (TODO: add test for this one ) | ||
- https://github.com/pladaria/reconnecting-websocket/pull/132 feat: make protocols updatable | ||
- https://github.com/pladaria/reconnecting-websocket/pull/141 [Fix] Socket doesn't connect again after closing while connecting | ||
# PartySocket | ||
(TODO: more) | ||
_(Forked from the wonderful [reconnecting-websocket](https://github.com/joewalnes/reconnecting-websocket/) project, updated with pending PRs and bugfixes)_ | ||
- https://github.com/pladaria/reconnecting-websocket/pull/163 Support for Dynamic Protocols | ||
- https://github.com/pladaria/reconnecting-websocket/pull/47 reconnecting and reconnectscheduled custom events | ||
A better WebSocket that Just Works™ | ||
# Reconnecting WebSocket | ||
## Install | ||
[](https://travis-ci.org/pladaria/reconnecting-websocket) | ||
[](https://coveralls.io/github/pladaria/reconnecting-websocket?branch=master) | ||
```bash | ||
npm install partysocket | ||
``` | ||
WebSocket that will automatically reconnect if the connection is closed. | ||
## Features | ||
- WebSocket API compatible (same interface, Level0 and Level2 event model) | ||
- Reconnects when a connection drops | ||
- Buffers messages when not connected, and sends accumulated messages when open | ||
- Handle connection timeouts | ||
- Allows changing server URL between reconnections | ||
- Fully configurable | ||
- Multi-platform (Web, ServiceWorkers, Node.js, React Native) | ||
- Multi-platform (Web, ServiceWorkers, Node.js, React Native, Cloudflare Workers, Deno, Bun) | ||
- Dependency free (does not depend on Window, DOM or any EventEmitter library) | ||
- Handle connection timeouts | ||
- Allows changing server URL between reconnections | ||
- Buffering. Will send accumulated messages on open | ||
- Multiple builds available (see dist folder) | ||
- Debug mode | ||
- Works everywhere, not just with PartyKit! | ||
## Install | ||
```bash | ||
npm install --save reconnecting-websocket | ||
``` | ||
## Usage | ||
@@ -39,23 +30,47 @@ | ||
So this documentation should be valid: | ||
[MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). | ||
Ping me if you find any problems. Or, even better, write a test for your case and make a pull | ||
request :) | ||
### Simple usage | ||
```javascript | ||
import ReconnectingWebSocket from "reconnecting-websocket"; | ||
import { WebSocket } from "partysocket"; | ||
const rws = new ReconnectingWebSocket("ws://my.site.com"); | ||
const ws = new WebSocket("wss://my.site.com"); | ||
rws.addEventListener("open", () => { | ||
rws.send("hello!"); | ||
ws.addEventListener("open", () => { | ||
ws.send("hello!"); | ||
}); | ||
``` | ||
### Usage with PartyKit | ||
```javascript | ||
import PartySocket from "partysocket"; | ||
// optional: only needed if creating using inside node.js. Run `npm install ws`, and then add: | ||
// import WS from "ws"; | ||
const ws = new PartySocket({ | ||
host: "project.name.partykit.dev", // or localhost:1999 in dev | ||
room: "my-room", | ||
// add an optional id to identify the client, | ||
// if not provided, a random id will be generated | ||
id: "some-connection-id" | ||
// optional: if used from node.js, you need to pass the WebSocket polyfill imported from `ws` | ||
// WebSocket: WS | ||
}); | ||
// optionally, update the properties of the connection | ||
// (e.g. to change the host or room) | ||
ws.updateProperties({ | ||
host: "another-project.username.partykit.dev", | ||
room: "my-new-room" | ||
}); | ||
ws.reconnect(); // make sure to call reconnect() after updating the properties | ||
``` | ||
### Update URL | ||
The `url` parameter will be resolved before connecting, possible types: | ||
The `url` parameter will be resolved before connecting, with possible types: | ||
@@ -67,5 +82,9 @@ - `string` | ||
```javascript | ||
import ReconnectingWebSocket from "reconnecting-websocket"; | ||
import { WebSocket } from "partysocket"; | ||
const urls = ["ws://my.site.com", "ws://your.site.com", "ws://their.site.com"]; | ||
const urls = [ | ||
"wss://my.site.com", | ||
"wss://your.site.com", | ||
"wss://their.site.com" | ||
]; | ||
let urlIndex = 0; | ||
@@ -76,7 +95,7 @@ | ||
const rws = new ReconnectingWebSocket(urlProvider); | ||
const ws = new WebSocket(urlProvider); | ||
``` | ||
```javascript | ||
import ReconnectingWebSocket from "reconnecting-websocket"; | ||
import { WebSocket } from "partysocket"; | ||
@@ -89,3 +108,3 @@ // async url provider | ||
const rws = new ReconnectingWebSocket(urlProvider); | ||
const ws = new WebSocket(urlProvider); | ||
``` | ||
@@ -104,8 +123,9 @@ | ||
```javascript | ||
import ReconnectingWebSocket from 'reconnecting-websocket`; | ||
const rws = new ReconnectingWebSocket('ws://your.site.com', 'your protocol'); | ||
import { WebSocket } from "partysocket"; | ||
const ws = new WebSocket("wss://your.site.com", "your protocol"); | ||
``` | ||
```javascript | ||
import ReconnectingWebSocket from 'reconnecting-websocket`; | ||
import WebSocket from 'partysocket`; | ||
@@ -118,3 +138,3 @@ const protocols = ['p1', 'p2', ['p3.1', 'p3.2']]; | ||
const rws = new ReconnectingWebSocket('ws://your.site.com', protocolsProvider); | ||
const ws = new WebSocket('wss://your.site.com', protocolsProvider); | ||
``` | ||
@@ -127,3 +147,3 @@ | ||
```javascript | ||
import ReconnectingWebSocket from "reconnecting-websocket"; | ||
import { WebSocket } from "partysocket"; | ||
import WS from "ws"; | ||
@@ -134,5 +154,5 @@ | ||
connectionTimeout: 1000, | ||
maxRetries: 10, | ||
maxRetries: 10 | ||
}; | ||
const rws = new ReconnectingWebSocket("ws://my.site.com", [], options); | ||
const ws = new WebSocket("wss://my.site.com", [], options); | ||
``` | ||
@@ -215,8 +235,4 @@ | ||
## Contributing | ||
[Read here](./CONTRIBUTING.md) | ||
## License | ||
MIT |
76
ws.d.ts
@@ -0,1 +1,11 @@ | ||
type TypedEventTarget<EventMap extends object> = { | ||
new (): IntermediateEventTarget<EventMap>; | ||
}; | ||
interface IntermediateEventTarget<EventMap> extends EventTarget { | ||
addEventListener<K extends keyof EventMap>(type: K, callback: (event: EventMap[K] extends Event ? EventMap[K] : never) => EventMap[K] extends Event ? void : never, options?: boolean | AddEventListenerOptions): void; | ||
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void; | ||
removeEventListener<K extends keyof EventMap>(type: K, callback: (event: EventMap[K] extends Event ? EventMap[K] : never) => EventMap[K] extends Event ? void : never, options?: boolean | AddEventListenerOptions): void; | ||
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void; | ||
} | ||
/*! | ||
@@ -7,8 +17,4 @@ * Reconnecting WebSocket | ||
*/ | ||
export declare class Event { | ||
target: any; | ||
type: string; | ||
constructor(type: string, target: any); | ||
} | ||
export declare class ErrorEvent extends Event { | ||
declare class ErrorEvent extends Event { | ||
message: string; | ||
@@ -18,3 +24,3 @@ error: Error; | ||
} | ||
export declare class CloseEvent extends Event { | ||
declare class CloseEvent extends Event { | ||
code: number; | ||
@@ -25,3 +31,3 @@ reason: string; | ||
} | ||
export interface WebSocketEventMap { | ||
interface WebSocketEventMap { | ||
close: CloseEvent; | ||
@@ -32,17 +38,4 @@ error: ErrorEvent; | ||
} | ||
export interface WebSocketEventListenerMap { | ||
close: (event: CloseEvent) => void | { | ||
handleEvent: (event: CloseEvent) => void; | ||
}; | ||
error: (event: ErrorEvent) => void | { | ||
handleEvent: (event: ErrorEvent) => void; | ||
}; | ||
message: (event: MessageEvent) => void | { | ||
handleEvent: (event: MessageEvent) => void; | ||
}; | ||
open: (event: Event) => void | { | ||
handleEvent: (event: Event) => void; | ||
}; | ||
} | ||
export type Options = { | ||
type Options = { | ||
WebSocket?: any; | ||
maxReconnectionDelay?: number; | ||
@@ -57,15 +50,10 @@ minReconnectionDelay?: number; | ||
debug?: boolean; | ||
debugLogger?: (...args: any[]) => void; | ||
}; | ||
export type UrlProvider = string | (() => string) | (() => Promise<string>); | ||
export type ProtocolsProvider = null | string | string[] | (() => string | string[] | null) | (() => Promise<string | string[] | null>); | ||
export type Message = string | ArrayBuffer | Blob | ArrayBufferView; | ||
export type ListenersMap = { | ||
error: WebSocketEventListenerMap["error"][]; | ||
message: WebSocketEventListenerMap["message"][]; | ||
open: WebSocketEventListenerMap["open"][]; | ||
close: WebSocketEventListenerMap["close"][]; | ||
}; | ||
export default class ReconnectingWebSocket { | ||
type UrlProvider = string | (() => string) | (() => Promise<string>); | ||
type ProtocolsProvider = null | string | string[] | (() => string | string[] | null) | (() => Promise<string | string[] | null>); | ||
type Message = string | ArrayBuffer | Blob | ArrayBufferView; | ||
declare const ReconnectingWebSocket_base: TypedEventTarget<WebSocketEventMap>; | ||
declare class ReconnectingWebSocket extends ReconnectingWebSocket_base { | ||
private _ws; | ||
private _listeners; | ||
private _retryCount; | ||
@@ -79,5 +67,6 @@ private _uptimeTimeout; | ||
private _messageQueue; | ||
private readonly _url; | ||
private readonly _protocols?; | ||
private readonly _options; | ||
private _debugLogger; | ||
protected _url: UrlProvider; | ||
protected _protocols?: ProtocolsProvider; | ||
protected _options: Options; | ||
constructor(url: UrlProvider, protocols?: ProtocolsProvider, options?: Options); | ||
@@ -159,11 +148,2 @@ static get CONNECTING(): number; | ||
send(data: Message): void; | ||
/** | ||
* Register an event handler of a specific event type | ||
*/ | ||
addEventListener<T extends keyof WebSocketEventListenerMap>(type: T, listener: WebSocketEventListenerMap[T]): void; | ||
dispatchEvent(event: Event): boolean; | ||
/** | ||
* Removes an event listener | ||
*/ | ||
removeEventListener<T extends keyof WebSocketEventListenerMap>(type: T, listener: WebSocketEventListenerMap[T]): void; | ||
private _debug; | ||
@@ -178,3 +158,2 @@ private _getNextDelay; | ||
private _acceptOpen; | ||
private _callEventListener; | ||
private _handleOpen; | ||
@@ -188,2 +167,3 @@ private _handleMessage; | ||
} | ||
//# sourceMappingURL=ws.d.ts.map | ||
export { CloseEvent, ErrorEvent, type Message, type Options, type ProtocolsProvider, type UrlProvider, type WebSocketEventMap, ReconnectingWebSocket as default }; |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
116086
23
3562
2
230
0
1
1
4
+ Addedevent-target-shim@^6.0.2
+ Addedevent-target-shim@6.0.2(transitive)