crossws 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/bun.cjs +24 -24
- package/dist/adapters/bun.d.cts +9 -9
- package/dist/adapters/bun.d.mts +9 -9
- package/dist/adapters/bun.d.ts +9 -9
- package/dist/adapters/bun.mjs +21 -21
- package/dist/adapters/cloudflare.cjs +20 -17
- package/dist/adapters/cloudflare.d.cts +6 -6
- package/dist/adapters/cloudflare.d.mts +6 -6
- package/dist/adapters/cloudflare.d.ts +6 -6
- package/dist/adapters/cloudflare.mjs +16 -13
- package/dist/adapters/deno.cjs +17 -17
- package/dist/adapters/deno.d.cts +6 -6
- package/dist/adapters/deno.d.mts +6 -6
- package/dist/adapters/deno.d.ts +6 -6
- package/dist/adapters/deno.mjs +14 -14
- package/dist/adapters/node.cjs +21 -21
- package/dist/adapters/node.d.cts +7 -7
- package/dist/adapters/node.d.mts +7 -7
- package/dist/adapters/node.d.ts +7 -7
- package/dist/adapters/node.mjs +18 -18
- package/dist/adapters/uws.cjs +33 -26
- package/dist/adapters/uws.d.cts +7 -7
- package/dist/adapters/uws.d.mts +7 -7
- package/dist/adapters/uws.d.ts +7 -7
- package/dist/adapters/uws.mjs +31 -24
- package/dist/index.cjs +12 -10
- package/dist/index.d.cts +4 -113
- package/dist/index.d.mts +4 -113
- package/dist/index.d.ts +4 -113
- package/dist/index.mjs +7 -5
- package/dist/shared/{crossws.a6b0a7cc.mjs → crossws.61d46dae.mjs} +31 -42
- package/dist/shared/crossws.a2e5c71e.d.cts +112 -0
- package/dist/shared/crossws.a2e5c71e.d.mts +112 -0
- package/dist/shared/crossws.a2e5c71e.d.ts +112 -0
- package/dist/shared/{crossws.6009d265.cjs → crossws.c0275b79.cjs} +33 -43
- package/package.json +2 -2
- package/dist/shared/crossws.6f7f02b3.mjs +0 -8
- package/dist/shared/crossws.deae10fd.cjs +0 -10
package/dist/index.d.cts
CHANGED
|
@@ -1,115 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
import { A as AdapterOptions, C as CrossWS } from './shared/crossws.a2e5c71e.cjs';
|
|
2
|
+
export { b as Adapter, f as AdapterHooks, a as Caller, H as Hooks, M as Message, P as Peer, R as ResolveHooks, W as WSError, c as WSRequest, e as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.a2e5c71e.cjs';
|
|
4
3
|
|
|
5
|
-
declare
|
|
6
|
-
readonly rawData: any;
|
|
7
|
-
readonly isBinary?: boolean | undefined;
|
|
8
|
-
constructor(rawData: any, isBinary?: boolean | undefined);
|
|
9
|
-
text(): string;
|
|
10
|
-
toString(): string;
|
|
11
|
-
}
|
|
4
|
+
declare function createCrossWS(opts?: AdapterOptions): CrossWS;
|
|
12
5
|
|
|
13
|
-
|
|
14
|
-
interface WSRequest {
|
|
15
|
-
readonly url: string;
|
|
16
|
-
readonly headers: HeadersInit;
|
|
17
|
-
}
|
|
18
|
-
declare abstract class WSPeer<AdapterContext = any> implements WSRequest {
|
|
19
|
-
ctx: AdapterContext;
|
|
20
|
-
_subscriptions: Set<string>;
|
|
21
|
-
constructor(ctx: AdapterContext);
|
|
22
|
-
get id(): string | undefined;
|
|
23
|
-
get url(): string;
|
|
24
|
-
get headers(): HeadersInit;
|
|
25
|
-
get readyState(): ReadyState | -1;
|
|
26
|
-
abstract send(message: any, options?: {
|
|
27
|
-
compress?: boolean;
|
|
28
|
-
}): number;
|
|
29
|
-
publish(topic: string, message: any, options?: {
|
|
30
|
-
compress?: boolean;
|
|
31
|
-
}): void;
|
|
32
|
-
subscribe(topic: string): void;
|
|
33
|
-
unsubscribe(topic: string): void;
|
|
34
|
-
toString(): string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
38
|
-
type _UserHooks = WebSocketHooks & AdapterHooks;
|
|
39
|
-
type UserHooks = Partial<_UserHooks>;
|
|
40
|
-
declare function defineWebSocketHooks<T extends UserHooks = UserHooks>(hooks: T): T;
|
|
41
|
-
type UserHookName = Exclude<keyof _UserHooks, "$">;
|
|
42
|
-
type CatchAllHandler = <Name extends UserHookName>(name: Name, ...args: Parameters<_UserHooks[Name]>) => MaybePromise<void>;
|
|
43
|
-
interface WebSocketHooks {
|
|
44
|
-
/** Catch-all handler */
|
|
45
|
-
$: CatchAllHandler;
|
|
46
|
-
/** Upgrading */
|
|
47
|
-
upgrade: (req: WSRequest) => MaybePromise<void | {
|
|
48
|
-
headers?: HeadersInit;
|
|
49
|
-
}>;
|
|
50
|
-
/** A message is received */
|
|
51
|
-
message: (peer: WSPeer, message: WSMessage) => MaybePromise<void>;
|
|
52
|
-
/** A socket is opened */
|
|
53
|
-
open: (peer: WSPeer) => MaybePromise<void>;
|
|
54
|
-
/** A socket is closed */
|
|
55
|
-
close: (peer: WSPeer, details: {
|
|
56
|
-
code?: number;
|
|
57
|
-
reason?: string;
|
|
58
|
-
}) => MaybePromise<void>;
|
|
59
|
-
/** An error occurs */
|
|
60
|
-
error: (peer: WSPeer, error: WebSocketError) => MaybePromise<void>;
|
|
61
|
-
}
|
|
62
|
-
type WSHook<ArgsT extends Array<any> = []> = (peer: WSPeer, ...args: ArgsT) => MaybePromise<void>;
|
|
63
|
-
interface AdapterHooks {
|
|
64
|
-
"bun:message": WSHook<[ws: any, message: any]>;
|
|
65
|
-
"bun:open": WSHook<[ws: any]>;
|
|
66
|
-
"bun:close": WSHook<[ws: any]>;
|
|
67
|
-
"bun:drain": WSHook<[]>;
|
|
68
|
-
"bun:error": WSHook<[ws: any, error: any]>;
|
|
69
|
-
"bun:ping": WSHook<[ws: any, data: any]>;
|
|
70
|
-
"bun:pong": WSHook<[ws: any, data: any]>;
|
|
71
|
-
"cloudflare:accept": WSHook<[]>;
|
|
72
|
-
"cloudflare:message": WSHook<[event: any]>;
|
|
73
|
-
"cloudflare:error": WSHook<[event: any]>;
|
|
74
|
-
"cloudflare:close": WSHook<[event: any]>;
|
|
75
|
-
"deno:open": WSHook<[]>;
|
|
76
|
-
"deno:message": WSHook<[event: any]>;
|
|
77
|
-
"deno:close": WSHook<[]>;
|
|
78
|
-
"deno:error": WSHook<[error: any]>;
|
|
79
|
-
"node:open": WSHook<[]>;
|
|
80
|
-
"node:message": WSHook<[data: any, isBinary: boolean]>;
|
|
81
|
-
"node:close": WSHook<[code: number, reason: Buffer]>;
|
|
82
|
-
"node:error": WSHook<[error: any]>;
|
|
83
|
-
"node:ping": WSHook<[data: Buffer]>;
|
|
84
|
-
"node:pong": WSHook<[data: Buffer]>;
|
|
85
|
-
"node:unexpected-response": WSHook<[req: any, res: any]>;
|
|
86
|
-
"node:upgrade": WSHook<[req: any]>;
|
|
87
|
-
"uws:open": WSHook<[ws: any]>;
|
|
88
|
-
"uws:message": WSHook<[ws: any, message: any, isBinary: boolean]>;
|
|
89
|
-
"uws:close": WSHook<[ws: any, code: number, message: any]>;
|
|
90
|
-
"uws:ping": WSHook<[ws: any, message: any]>;
|
|
91
|
-
"uws:pong": WSHook<[ws: any, message: any]>;
|
|
92
|
-
"uws:drain": WSHook<[ws: any]>;
|
|
93
|
-
"uws:upgrade": WSHook<[res: any, req: any, context: any]>;
|
|
94
|
-
"uws:subscription": WSHook<[
|
|
95
|
-
ws: any,
|
|
96
|
-
topic: any,
|
|
97
|
-
newCount: number,
|
|
98
|
-
oldCount: number
|
|
99
|
-
]>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
type WebSocketAdapter<RT = any, OT = any> = (hooks: Partial<WebSocketHooks & AdapterHooks>, opts: OT) => RT;
|
|
103
|
-
declare function defineWebSocketAdapter<RT, OT>(factory: WebSocketAdapter<RT, OT>): WebSocketAdapter<RT, OT>;
|
|
104
|
-
|
|
105
|
-
interface CrossWS extends WebSocketHooks {
|
|
106
|
-
upgrade: (req: WSRequest) => Promise<{
|
|
107
|
-
headers: HeadersInit;
|
|
108
|
-
}>;
|
|
109
|
-
}
|
|
110
|
-
interface CrossWSOptions {
|
|
111
|
-
resolve?: (info: WSRequest | WSPeer) => UserHooks | Promise<UserHooks>;
|
|
112
|
-
}
|
|
113
|
-
declare function createCrossWS(hooks: UserHooks, options: CrossWSOptions): CrossWS;
|
|
114
|
-
|
|
115
|
-
export { type AdapterHooks, type CrossWS, type CrossWSOptions, type UserHooks, WSMessage, WSPeer, type WSRequest, type WebSocketAdapter, WebSocketError, type WebSocketHooks, type _UserHooks, createCrossWS, defineWebSocketAdapter, defineWebSocketHooks };
|
|
6
|
+
export { AdapterOptions, CrossWS, createCrossWS };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,115 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
import { A as AdapterOptions, C as CrossWS } from './shared/crossws.a2e5c71e.mjs';
|
|
2
|
+
export { b as Adapter, f as AdapterHooks, a as Caller, H as Hooks, M as Message, P as Peer, R as ResolveHooks, W as WSError, c as WSRequest, e as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.a2e5c71e.mjs';
|
|
4
3
|
|
|
5
|
-
declare
|
|
6
|
-
readonly rawData: any;
|
|
7
|
-
readonly isBinary?: boolean | undefined;
|
|
8
|
-
constructor(rawData: any, isBinary?: boolean | undefined);
|
|
9
|
-
text(): string;
|
|
10
|
-
toString(): string;
|
|
11
|
-
}
|
|
4
|
+
declare function createCrossWS(opts?: AdapterOptions): CrossWS;
|
|
12
5
|
|
|
13
|
-
|
|
14
|
-
interface WSRequest {
|
|
15
|
-
readonly url: string;
|
|
16
|
-
readonly headers: HeadersInit;
|
|
17
|
-
}
|
|
18
|
-
declare abstract class WSPeer<AdapterContext = any> implements WSRequest {
|
|
19
|
-
ctx: AdapterContext;
|
|
20
|
-
_subscriptions: Set<string>;
|
|
21
|
-
constructor(ctx: AdapterContext);
|
|
22
|
-
get id(): string | undefined;
|
|
23
|
-
get url(): string;
|
|
24
|
-
get headers(): HeadersInit;
|
|
25
|
-
get readyState(): ReadyState | -1;
|
|
26
|
-
abstract send(message: any, options?: {
|
|
27
|
-
compress?: boolean;
|
|
28
|
-
}): number;
|
|
29
|
-
publish(topic: string, message: any, options?: {
|
|
30
|
-
compress?: boolean;
|
|
31
|
-
}): void;
|
|
32
|
-
subscribe(topic: string): void;
|
|
33
|
-
unsubscribe(topic: string): void;
|
|
34
|
-
toString(): string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
38
|
-
type _UserHooks = WebSocketHooks & AdapterHooks;
|
|
39
|
-
type UserHooks = Partial<_UserHooks>;
|
|
40
|
-
declare function defineWebSocketHooks<T extends UserHooks = UserHooks>(hooks: T): T;
|
|
41
|
-
type UserHookName = Exclude<keyof _UserHooks, "$">;
|
|
42
|
-
type CatchAllHandler = <Name extends UserHookName>(name: Name, ...args: Parameters<_UserHooks[Name]>) => MaybePromise<void>;
|
|
43
|
-
interface WebSocketHooks {
|
|
44
|
-
/** Catch-all handler */
|
|
45
|
-
$: CatchAllHandler;
|
|
46
|
-
/** Upgrading */
|
|
47
|
-
upgrade: (req: WSRequest) => MaybePromise<void | {
|
|
48
|
-
headers?: HeadersInit;
|
|
49
|
-
}>;
|
|
50
|
-
/** A message is received */
|
|
51
|
-
message: (peer: WSPeer, message: WSMessage) => MaybePromise<void>;
|
|
52
|
-
/** A socket is opened */
|
|
53
|
-
open: (peer: WSPeer) => MaybePromise<void>;
|
|
54
|
-
/** A socket is closed */
|
|
55
|
-
close: (peer: WSPeer, details: {
|
|
56
|
-
code?: number;
|
|
57
|
-
reason?: string;
|
|
58
|
-
}) => MaybePromise<void>;
|
|
59
|
-
/** An error occurs */
|
|
60
|
-
error: (peer: WSPeer, error: WebSocketError) => MaybePromise<void>;
|
|
61
|
-
}
|
|
62
|
-
type WSHook<ArgsT extends Array<any> = []> = (peer: WSPeer, ...args: ArgsT) => MaybePromise<void>;
|
|
63
|
-
interface AdapterHooks {
|
|
64
|
-
"bun:message": WSHook<[ws: any, message: any]>;
|
|
65
|
-
"bun:open": WSHook<[ws: any]>;
|
|
66
|
-
"bun:close": WSHook<[ws: any]>;
|
|
67
|
-
"bun:drain": WSHook<[]>;
|
|
68
|
-
"bun:error": WSHook<[ws: any, error: any]>;
|
|
69
|
-
"bun:ping": WSHook<[ws: any, data: any]>;
|
|
70
|
-
"bun:pong": WSHook<[ws: any, data: any]>;
|
|
71
|
-
"cloudflare:accept": WSHook<[]>;
|
|
72
|
-
"cloudflare:message": WSHook<[event: any]>;
|
|
73
|
-
"cloudflare:error": WSHook<[event: any]>;
|
|
74
|
-
"cloudflare:close": WSHook<[event: any]>;
|
|
75
|
-
"deno:open": WSHook<[]>;
|
|
76
|
-
"deno:message": WSHook<[event: any]>;
|
|
77
|
-
"deno:close": WSHook<[]>;
|
|
78
|
-
"deno:error": WSHook<[error: any]>;
|
|
79
|
-
"node:open": WSHook<[]>;
|
|
80
|
-
"node:message": WSHook<[data: any, isBinary: boolean]>;
|
|
81
|
-
"node:close": WSHook<[code: number, reason: Buffer]>;
|
|
82
|
-
"node:error": WSHook<[error: any]>;
|
|
83
|
-
"node:ping": WSHook<[data: Buffer]>;
|
|
84
|
-
"node:pong": WSHook<[data: Buffer]>;
|
|
85
|
-
"node:unexpected-response": WSHook<[req: any, res: any]>;
|
|
86
|
-
"node:upgrade": WSHook<[req: any]>;
|
|
87
|
-
"uws:open": WSHook<[ws: any]>;
|
|
88
|
-
"uws:message": WSHook<[ws: any, message: any, isBinary: boolean]>;
|
|
89
|
-
"uws:close": WSHook<[ws: any, code: number, message: any]>;
|
|
90
|
-
"uws:ping": WSHook<[ws: any, message: any]>;
|
|
91
|
-
"uws:pong": WSHook<[ws: any, message: any]>;
|
|
92
|
-
"uws:drain": WSHook<[ws: any]>;
|
|
93
|
-
"uws:upgrade": WSHook<[res: any, req: any, context: any]>;
|
|
94
|
-
"uws:subscription": WSHook<[
|
|
95
|
-
ws: any,
|
|
96
|
-
topic: any,
|
|
97
|
-
newCount: number,
|
|
98
|
-
oldCount: number
|
|
99
|
-
]>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
type WebSocketAdapter<RT = any, OT = any> = (hooks: Partial<WebSocketHooks & AdapterHooks>, opts: OT) => RT;
|
|
103
|
-
declare function defineWebSocketAdapter<RT, OT>(factory: WebSocketAdapter<RT, OT>): WebSocketAdapter<RT, OT>;
|
|
104
|
-
|
|
105
|
-
interface CrossWS extends WebSocketHooks {
|
|
106
|
-
upgrade: (req: WSRequest) => Promise<{
|
|
107
|
-
headers: HeadersInit;
|
|
108
|
-
}>;
|
|
109
|
-
}
|
|
110
|
-
interface CrossWSOptions {
|
|
111
|
-
resolve?: (info: WSRequest | WSPeer) => UserHooks | Promise<UserHooks>;
|
|
112
|
-
}
|
|
113
|
-
declare function createCrossWS(hooks: UserHooks, options: CrossWSOptions): CrossWS;
|
|
114
|
-
|
|
115
|
-
export { type AdapterHooks, type CrossWS, type CrossWSOptions, type UserHooks, WSMessage, WSPeer, type WSRequest, type WebSocketAdapter, WebSocketError, type WebSocketHooks, type _UserHooks, createCrossWS, defineWebSocketAdapter, defineWebSocketHooks };
|
|
6
|
+
export { AdapterOptions, CrossWS, createCrossWS };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,115 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
import { A as AdapterOptions, C as CrossWS } from './shared/crossws.a2e5c71e.js';
|
|
2
|
+
export { b as Adapter, f as AdapterHooks, a as Caller, H as Hooks, M as Message, P as Peer, R as ResolveHooks, W as WSError, c as WSRequest, e as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.a2e5c71e.js';
|
|
4
3
|
|
|
5
|
-
declare
|
|
6
|
-
readonly rawData: any;
|
|
7
|
-
readonly isBinary?: boolean | undefined;
|
|
8
|
-
constructor(rawData: any, isBinary?: boolean | undefined);
|
|
9
|
-
text(): string;
|
|
10
|
-
toString(): string;
|
|
11
|
-
}
|
|
4
|
+
declare function createCrossWS(opts?: AdapterOptions): CrossWS;
|
|
12
5
|
|
|
13
|
-
|
|
14
|
-
interface WSRequest {
|
|
15
|
-
readonly url: string;
|
|
16
|
-
readonly headers: HeadersInit;
|
|
17
|
-
}
|
|
18
|
-
declare abstract class WSPeer<AdapterContext = any> implements WSRequest {
|
|
19
|
-
ctx: AdapterContext;
|
|
20
|
-
_subscriptions: Set<string>;
|
|
21
|
-
constructor(ctx: AdapterContext);
|
|
22
|
-
get id(): string | undefined;
|
|
23
|
-
get url(): string;
|
|
24
|
-
get headers(): HeadersInit;
|
|
25
|
-
get readyState(): ReadyState | -1;
|
|
26
|
-
abstract send(message: any, options?: {
|
|
27
|
-
compress?: boolean;
|
|
28
|
-
}): number;
|
|
29
|
-
publish(topic: string, message: any, options?: {
|
|
30
|
-
compress?: boolean;
|
|
31
|
-
}): void;
|
|
32
|
-
subscribe(topic: string): void;
|
|
33
|
-
unsubscribe(topic: string): void;
|
|
34
|
-
toString(): string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
38
|
-
type _UserHooks = WebSocketHooks & AdapterHooks;
|
|
39
|
-
type UserHooks = Partial<_UserHooks>;
|
|
40
|
-
declare function defineWebSocketHooks<T extends UserHooks = UserHooks>(hooks: T): T;
|
|
41
|
-
type UserHookName = Exclude<keyof _UserHooks, "$">;
|
|
42
|
-
type CatchAllHandler = <Name extends UserHookName>(name: Name, ...args: Parameters<_UserHooks[Name]>) => MaybePromise<void>;
|
|
43
|
-
interface WebSocketHooks {
|
|
44
|
-
/** Catch-all handler */
|
|
45
|
-
$: CatchAllHandler;
|
|
46
|
-
/** Upgrading */
|
|
47
|
-
upgrade: (req: WSRequest) => MaybePromise<void | {
|
|
48
|
-
headers?: HeadersInit;
|
|
49
|
-
}>;
|
|
50
|
-
/** A message is received */
|
|
51
|
-
message: (peer: WSPeer, message: WSMessage) => MaybePromise<void>;
|
|
52
|
-
/** A socket is opened */
|
|
53
|
-
open: (peer: WSPeer) => MaybePromise<void>;
|
|
54
|
-
/** A socket is closed */
|
|
55
|
-
close: (peer: WSPeer, details: {
|
|
56
|
-
code?: number;
|
|
57
|
-
reason?: string;
|
|
58
|
-
}) => MaybePromise<void>;
|
|
59
|
-
/** An error occurs */
|
|
60
|
-
error: (peer: WSPeer, error: WebSocketError) => MaybePromise<void>;
|
|
61
|
-
}
|
|
62
|
-
type WSHook<ArgsT extends Array<any> = []> = (peer: WSPeer, ...args: ArgsT) => MaybePromise<void>;
|
|
63
|
-
interface AdapterHooks {
|
|
64
|
-
"bun:message": WSHook<[ws: any, message: any]>;
|
|
65
|
-
"bun:open": WSHook<[ws: any]>;
|
|
66
|
-
"bun:close": WSHook<[ws: any]>;
|
|
67
|
-
"bun:drain": WSHook<[]>;
|
|
68
|
-
"bun:error": WSHook<[ws: any, error: any]>;
|
|
69
|
-
"bun:ping": WSHook<[ws: any, data: any]>;
|
|
70
|
-
"bun:pong": WSHook<[ws: any, data: any]>;
|
|
71
|
-
"cloudflare:accept": WSHook<[]>;
|
|
72
|
-
"cloudflare:message": WSHook<[event: any]>;
|
|
73
|
-
"cloudflare:error": WSHook<[event: any]>;
|
|
74
|
-
"cloudflare:close": WSHook<[event: any]>;
|
|
75
|
-
"deno:open": WSHook<[]>;
|
|
76
|
-
"deno:message": WSHook<[event: any]>;
|
|
77
|
-
"deno:close": WSHook<[]>;
|
|
78
|
-
"deno:error": WSHook<[error: any]>;
|
|
79
|
-
"node:open": WSHook<[]>;
|
|
80
|
-
"node:message": WSHook<[data: any, isBinary: boolean]>;
|
|
81
|
-
"node:close": WSHook<[code: number, reason: Buffer]>;
|
|
82
|
-
"node:error": WSHook<[error: any]>;
|
|
83
|
-
"node:ping": WSHook<[data: Buffer]>;
|
|
84
|
-
"node:pong": WSHook<[data: Buffer]>;
|
|
85
|
-
"node:unexpected-response": WSHook<[req: any, res: any]>;
|
|
86
|
-
"node:upgrade": WSHook<[req: any]>;
|
|
87
|
-
"uws:open": WSHook<[ws: any]>;
|
|
88
|
-
"uws:message": WSHook<[ws: any, message: any, isBinary: boolean]>;
|
|
89
|
-
"uws:close": WSHook<[ws: any, code: number, message: any]>;
|
|
90
|
-
"uws:ping": WSHook<[ws: any, message: any]>;
|
|
91
|
-
"uws:pong": WSHook<[ws: any, message: any]>;
|
|
92
|
-
"uws:drain": WSHook<[ws: any]>;
|
|
93
|
-
"uws:upgrade": WSHook<[res: any, req: any, context: any]>;
|
|
94
|
-
"uws:subscription": WSHook<[
|
|
95
|
-
ws: any,
|
|
96
|
-
topic: any,
|
|
97
|
-
newCount: number,
|
|
98
|
-
oldCount: number
|
|
99
|
-
]>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
type WebSocketAdapter<RT = any, OT = any> = (hooks: Partial<WebSocketHooks & AdapterHooks>, opts: OT) => RT;
|
|
103
|
-
declare function defineWebSocketAdapter<RT, OT>(factory: WebSocketAdapter<RT, OT>): WebSocketAdapter<RT, OT>;
|
|
104
|
-
|
|
105
|
-
interface CrossWS extends WebSocketHooks {
|
|
106
|
-
upgrade: (req: WSRequest) => Promise<{
|
|
107
|
-
headers: HeadersInit;
|
|
108
|
-
}>;
|
|
109
|
-
}
|
|
110
|
-
interface CrossWSOptions {
|
|
111
|
-
resolve?: (info: WSRequest | WSPeer) => UserHooks | Promise<UserHooks>;
|
|
112
|
-
}
|
|
113
|
-
declare function createCrossWS(hooks: UserHooks, options: CrossWSOptions): CrossWS;
|
|
114
|
-
|
|
115
|
-
export { type AdapterHooks, type CrossWS, type CrossWSOptions, type UserHooks, WSMessage, WSPeer, type WSRequest, type WebSocketAdapter, WebSocketError, type WebSocketHooks, type _UserHooks, createCrossWS, defineWebSocketAdapter, defineWebSocketHooks };
|
|
6
|
+
export { AdapterOptions, CrossWS, createCrossWS };
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { W as WebSocketError } from './shared/crossws.6f7f02b3.mjs';
|
|
1
|
+
export { M as Message, P as Peer, c as createCrossWS, a as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.61d46dae.mjs';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
class WSError extends Error {
|
|
4
|
+
constructor(...args) {
|
|
5
|
+
super(...args);
|
|
6
|
+
this.name = "WSError";
|
|
7
|
+
}
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
export {
|
|
10
|
+
export { WSError };
|
|
@@ -1,50 +1,32 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const _callHook = options.resolve ? async function(name, info, ...args) {
|
|
7
|
-
const hooks2 = await options.resolve?.(info);
|
|
8
|
-
const hook = hooks2?.[name];
|
|
9
|
-
return hook?.(info, ...args);
|
|
10
|
-
} : void 0;
|
|
1
|
+
function createCrossWS(opts = {}) {
|
|
2
|
+
const resolveHook = async (req, name) => {
|
|
3
|
+
const hooks = await opts.resolve?.(req);
|
|
4
|
+
return hooks?.[name];
|
|
5
|
+
};
|
|
11
6
|
return {
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
// WS Hooks
|
|
8
|
+
async callHook(name, ...args) {
|
|
9
|
+
await opts.hooks?.[name]?.apply(void 0, args);
|
|
10
|
+
const hook = await resolveHook(args[0], name);
|
|
11
|
+
await hook?.apply(void 0, args);
|
|
14
12
|
},
|
|
13
|
+
// Upgrade
|
|
15
14
|
async upgrade(req) {
|
|
16
|
-
const
|
|
17
|
-
hooks
|
|
18
|
-
|
|
15
|
+
const [res1, res2] = await Promise.all([
|
|
16
|
+
opts.hooks?.upgrade?.(req),
|
|
17
|
+
await resolveHook(req, "upgrade").then((h) => h?.(req))
|
|
19
18
|
]);
|
|
20
|
-
const headers =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
const headers = new Headers(res1?.headers);
|
|
20
|
+
if (res2?.headers) {
|
|
21
|
+
for (const [key, value] of new Headers(res2?.headers)) {
|
|
22
|
+
headers.append(key, value);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
return { headers };
|
|
27
26
|
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
_callHook?.("message", peer, message)
|
|
32
|
-
]);
|
|
33
|
-
},
|
|
34
|
-
async open(peer) {
|
|
35
|
-
await Promise.all([hooks.open?.(peer), _callHook?.("open", peer)]);
|
|
36
|
-
},
|
|
37
|
-
async close(peer, { code, reason }) {
|
|
38
|
-
await Promise.all([
|
|
39
|
-
hooks.close?.(peer, { code, reason }),
|
|
40
|
-
_callHook?.("close", peer, { code, reason })
|
|
41
|
-
]);
|
|
42
|
-
},
|
|
43
|
-
async error(peer, error) {
|
|
44
|
-
await Promise.all([
|
|
45
|
-
hooks.error?.(peer, error),
|
|
46
|
-
_callHook?.("error", peer, error)
|
|
47
|
-
]);
|
|
27
|
+
// Adapter hook
|
|
28
|
+
$callHook(name, ...args) {
|
|
29
|
+
return opts.adapterHooks?.[name].apply(void 0, args);
|
|
48
30
|
}
|
|
49
31
|
};
|
|
50
32
|
}
|
|
@@ -78,7 +60,7 @@ function isPlainObject(value) {
|
|
|
78
60
|
return true;
|
|
79
61
|
}
|
|
80
62
|
|
|
81
|
-
class
|
|
63
|
+
class Message {
|
|
82
64
|
constructor(rawData, isBinary) {
|
|
83
65
|
this.rawData = rawData;
|
|
84
66
|
this.isBinary = isBinary;
|
|
@@ -114,7 +96,7 @@ const ReadyStateMap = {
|
|
|
114
96
|
2: "closing",
|
|
115
97
|
3: "closed"
|
|
116
98
|
};
|
|
117
|
-
class
|
|
99
|
+
class Peer {
|
|
118
100
|
constructor(ctx) {
|
|
119
101
|
this.ctx = ctx;
|
|
120
102
|
__publicField(this, "_subscriptions", /* @__PURE__ */ new Set());
|
|
@@ -147,4 +129,11 @@ class WSPeer {
|
|
|
147
129
|
}
|
|
148
130
|
}
|
|
149
131
|
|
|
150
|
-
|
|
132
|
+
function defineWebSocketAdapter(factory) {
|
|
133
|
+
return factory;
|
|
134
|
+
}
|
|
135
|
+
function defineHooks(hooks) {
|
|
136
|
+
return hooks;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { Message as M, Peer as P, defineHooks as a, createCrossWS as c, defineWebSocketAdapter as d, toBufferLike as t };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
declare class WSError extends Error {
|
|
2
|
+
constructor(...args: any[]);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
declare class Message {
|
|
6
|
+
readonly rawData: any;
|
|
7
|
+
readonly isBinary?: boolean | undefined;
|
|
8
|
+
constructor(rawData: any, isBinary?: boolean | undefined);
|
|
9
|
+
text(): string;
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type ReadyState = 0 | 1 | 2 | 3;
|
|
14
|
+
declare abstract class Peer<AdapterContext = any> implements WSRequest {
|
|
15
|
+
ctx: AdapterContext;
|
|
16
|
+
_subscriptions: Set<string>;
|
|
17
|
+
constructor(ctx: AdapterContext);
|
|
18
|
+
get id(): string | undefined;
|
|
19
|
+
get url(): string;
|
|
20
|
+
get headers(): HeadersInit;
|
|
21
|
+
get readyState(): ReadyState | -1;
|
|
22
|
+
abstract send(message: any, options?: {
|
|
23
|
+
compress?: boolean;
|
|
24
|
+
}): number;
|
|
25
|
+
publish(topic: string, message: any, options?: {
|
|
26
|
+
compress?: boolean;
|
|
27
|
+
}): void;
|
|
28
|
+
subscribe(topic: string): void;
|
|
29
|
+
unsubscribe(topic: string): void;
|
|
30
|
+
toString(): string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
34
|
+
type Caller<T extends Record<string, (...args: any[]) => Promise<any>>, RT = null> = <K extends keyof T>(key: K, ...args: Parameters<T[K]>) => RT extends null ? Promise<ReturnType<T[K]>> : RT;
|
|
35
|
+
interface AdapterOptions {
|
|
36
|
+
resolve?: ResolveHooks;
|
|
37
|
+
hooks?: Hooks;
|
|
38
|
+
adapterHooks?: AdapterHooks;
|
|
39
|
+
}
|
|
40
|
+
type Adapter<AdapterT, Options extends AdapterOptions> = (options?: Options) => AdapterT;
|
|
41
|
+
declare function defineWebSocketAdapter<AdapterT, Options extends AdapterOptions = AdapterOptions>(factory: Adapter<AdapterT, Options>): Adapter<AdapterT, Options>;
|
|
42
|
+
interface CrossWS {
|
|
43
|
+
$callHook: Caller<AdapterHooks>;
|
|
44
|
+
callHook: Caller<Exclude<Hooks, "upgrade">, void>;
|
|
45
|
+
upgrade: (req: WSRequest) => Promise<{
|
|
46
|
+
headers?: HeadersInit;
|
|
47
|
+
}>;
|
|
48
|
+
}
|
|
49
|
+
interface WSRequest {
|
|
50
|
+
readonly url: string;
|
|
51
|
+
readonly headers: HeadersInit;
|
|
52
|
+
}
|
|
53
|
+
declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
|
|
54
|
+
type ResolveHooks = (info: WSRequest | Peer) => Partial<Hooks> | Promise<Partial<Hooks>>;
|
|
55
|
+
type HookFn<ArgsT extends any[] = any, RT = void> = (info: Peer, ...args: ArgsT) => MaybePromise<RT>;
|
|
56
|
+
interface Hooks extends Record<string, HookFn<any[], any>> {
|
|
57
|
+
/** Upgrading */
|
|
58
|
+
upgrade: (req: WSRequest) => MaybePromise<void | {
|
|
59
|
+
headers?: HeadersInit;
|
|
60
|
+
}>;
|
|
61
|
+
/** A message is received */
|
|
62
|
+
message: (peer: Peer, message: Message) => MaybePromise<void>;
|
|
63
|
+
/** A socket is opened */
|
|
64
|
+
open: (peer: Peer) => MaybePromise<void>;
|
|
65
|
+
/** A socket is closed */
|
|
66
|
+
close: (peer: Peer, details: {
|
|
67
|
+
code?: number;
|
|
68
|
+
reason?: string;
|
|
69
|
+
}) => MaybePromise<void>;
|
|
70
|
+
/** An error occurs */
|
|
71
|
+
error: (peer: Peer, error: WSError) => MaybePromise<void>;
|
|
72
|
+
}
|
|
73
|
+
interface AdapterHooks extends Record<string, HookFn<any[], any>> {
|
|
74
|
+
"bun:message": HookFn<[ws: any, message: any]>;
|
|
75
|
+
"bun:open": HookFn<[ws: any]>;
|
|
76
|
+
"bun:close": HookFn<[ws: any]>;
|
|
77
|
+
"bun:drain": HookFn<[]>;
|
|
78
|
+
"bun:error": HookFn<[ws: any, error: any]>;
|
|
79
|
+
"bun:ping": HookFn<[ws: any, data: any]>;
|
|
80
|
+
"bun:pong": HookFn<[ws: any, data: any]>;
|
|
81
|
+
"cloudflare:accept": HookFn<[]>;
|
|
82
|
+
"cloudflare:message": HookFn<[event: any]>;
|
|
83
|
+
"cloudflare:error": HookFn<[event: any]>;
|
|
84
|
+
"cloudflare:close": HookFn<[event: any]>;
|
|
85
|
+
"deno:open": HookFn<[]>;
|
|
86
|
+
"deno:message": HookFn<[event: any]>;
|
|
87
|
+
"deno:close": HookFn<[]>;
|
|
88
|
+
"deno:error": HookFn<[error: any]>;
|
|
89
|
+
"node:open": HookFn<[]>;
|
|
90
|
+
"node:message": HookFn<[data: any, isBinary: boolean]>;
|
|
91
|
+
"node:close": HookFn<[code: number, reason: Buffer]>;
|
|
92
|
+
"node:error": HookFn<[error: any]>;
|
|
93
|
+
"node:ping": HookFn<[data: Buffer]>;
|
|
94
|
+
"node:pong": HookFn<[data: Buffer]>;
|
|
95
|
+
"node:unexpected-response": HookFn<[req: any, res: any]>;
|
|
96
|
+
"node:upgrade": HookFn<[req: any]>;
|
|
97
|
+
"uws:open": HookFn<[ws: any]>;
|
|
98
|
+
"uws:message": HookFn<[ws: any, message: any, isBinary: boolean]>;
|
|
99
|
+
"uws:close": HookFn<[ws: any, code: number, message: any]>;
|
|
100
|
+
"uws:ping": HookFn<[ws: any, message: any]>;
|
|
101
|
+
"uws:pong": HookFn<[ws: any, message: any]>;
|
|
102
|
+
"uws:drain": HookFn<[ws: any]>;
|
|
103
|
+
"uws:upgrade": HookFn<[res: any, req: any, context: any]>;
|
|
104
|
+
"uws:subscription": HookFn<[
|
|
105
|
+
ws: any,
|
|
106
|
+
topic: any,
|
|
107
|
+
newCount: number,
|
|
108
|
+
oldCount: number
|
|
109
|
+
]>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { type AdapterOptions as A, type CrossWS as C, type Hooks as H, Message as M, Peer as P, type ResolveHooks as R, WSError as W, type Caller as a, type Adapter as b, type WSRequest as c, defineWebSocketAdapter as d, defineHooks as e, type AdapterHooks as f };
|