crossws 0.2.3 → 0.3.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/README.md +8 -9
- package/adapters/cloudflare-durable.d.ts +2 -0
- package/adapters/sse.d.ts +2 -0
- package/dist/adapters/bun.d.mts +25 -6
- package/dist/adapters/bun.d.ts +25 -6
- package/dist/adapters/bun.mjs +54 -62
- package/dist/adapters/cloudflare-durable.d.mts +20 -0
- package/dist/adapters/cloudflare-durable.d.ts +20 -0
- package/dist/adapters/cloudflare-durable.mjs +132 -0
- package/dist/adapters/cloudflare.d.mts +5 -5
- package/dist/adapters/cloudflare.d.ts +5 -5
- package/dist/adapters/cloudflare.mjs +56 -49
- package/dist/adapters/deno.d.mts +10 -3
- package/dist/adapters/deno.d.ts +10 -3
- package/dist/adapters/deno.mjs +54 -45
- package/dist/adapters/node.d.mts +4 -2
- package/dist/adapters/node.d.ts +4 -2
- package/dist/adapters/node.mjs +108 -71
- package/dist/adapters/sse.d.mts +12 -0
- package/dist/adapters/sse.d.ts +12 -0
- package/dist/adapters/sse.mjs +120 -0
- package/dist/adapters/uws.d.mts +45 -9
- package/dist/adapters/uws.d.ts +45 -9
- package/dist/adapters/uws.mjs +148 -124
- package/dist/index.d.mts +136 -4
- package/dist/index.d.ts +136 -4
- package/dist/index.mjs +1 -10
- package/dist/shared/crossws.B4sHId41.mjs +42 -0
- package/dist/shared/crossws.By9qWDAI.mjs +8 -0
- package/dist/shared/crossws.ChIJSJVK.d.mts +297 -0
- package/dist/shared/crossws.ChIJSJVK.d.ts +297 -0
- package/dist/shared/crossws.DTY7a69w.mjs +315 -0
- package/dist/shared/{crossws.a5db571c.mjs → crossws.YgHWLi0G.mjs} +284 -115
- package/dist/websocket/{index.d.mts → native.d.mts} +1 -1
- package/dist/websocket/{node.d.cts → native.d.ts} +1 -1
- package/dist/websocket/native.mjs +3 -0
- package/dist/websocket/node.d.mts +1 -1
- package/dist/websocket/node.d.ts +1 -1
- package/dist/websocket/node.mjs +1 -1
- package/dist/websocket/sse.d.mts +41 -0
- package/dist/websocket/sse.d.ts +41 -0
- package/dist/websocket/sse.mjs +127 -0
- package/package.json +78 -75
- package/dist/adapters/bun.cjs +0 -95
- package/dist/adapters/bun.d.cts +0 -17
- package/dist/adapters/cloudflare.cjs +0 -65
- package/dist/adapters/cloudflare.d.cts +0 -12
- package/dist/adapters/deno.cjs +0 -63
- package/dist/adapters/deno.d.cts +0 -15
- package/dist/adapters/node.cjs +0 -726
- package/dist/adapters/node.d.cts +0 -298
- package/dist/adapters/uws.cjs +0 -153
- package/dist/adapters/uws.d.cts +0 -19
- package/dist/index.cjs +0 -17
- package/dist/index.d.cts +0 -6
- package/dist/shared/crossws.2ed26345.cjs +0 -3931
- package/dist/shared/crossws.34cfc8d0.mjs +0 -139
- package/dist/shared/crossws.a2e5c71e.d.cts +0 -112
- package/dist/shared/crossws.a2e5c71e.d.mts +0 -112
- package/dist/shared/crossws.a2e5c71e.d.ts +0 -112
- package/dist/shared/crossws.c13afbe7.cjs +0 -146
- package/dist/websocket/index.cjs +0 -5
- package/dist/websocket/index.d.cts +0 -10
- package/dist/websocket/index.d.ts +0 -10
- package/dist/websocket/index.mjs +0 -3
- package/dist/websocket/node.cjs +0 -17
package/dist/adapters/uws.d.ts
CHANGED
|
@@ -1,19 +1,55 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { AdapterInstance, AdapterOptions, Adapter, Peer } from '../index.js';
|
|
2
|
+
import { W as WebSocket } from '../shared/crossws.ChIJSJVK.js';
|
|
3
|
+
import uws from 'uWebSockets.js';
|
|
3
4
|
|
|
4
5
|
type UserData = {
|
|
5
|
-
|
|
6
|
-
req: HttpRequest;
|
|
7
|
-
res: HttpResponse;
|
|
8
|
-
|
|
6
|
+
peer?: UWSPeer;
|
|
7
|
+
req: uws.HttpRequest;
|
|
8
|
+
res: uws.HttpResponse;
|
|
9
|
+
protocol: string;
|
|
10
|
+
extensions: string;
|
|
9
11
|
};
|
|
10
|
-
type WebSocketHandler = WebSocketBehavior<UserData>;
|
|
11
|
-
interface UWSAdapter {
|
|
12
|
+
type WebSocketHandler = uws.WebSocketBehavior<UserData>;
|
|
13
|
+
interface UWSAdapter extends AdapterInstance {
|
|
12
14
|
websocket: WebSocketHandler;
|
|
13
15
|
}
|
|
14
16
|
interface UWSOptions extends AdapterOptions {
|
|
15
|
-
uws?: Exclude<WebSocketBehavior<any>, "close" | "drain" | "message" | "open" | "ping" | "pong" | "subscription" | "upgrade">;
|
|
17
|
+
uws?: Exclude<uws.WebSocketBehavior<any>, "close" | "drain" | "message" | "open" | "ping" | "pong" | "subscription" | "upgrade">;
|
|
16
18
|
}
|
|
17
19
|
declare const _default: Adapter<UWSAdapter, UWSOptions>;
|
|
18
20
|
|
|
21
|
+
declare class UWSPeer extends Peer<{
|
|
22
|
+
peers: Set<UWSPeer>;
|
|
23
|
+
request: UWSReqProxy;
|
|
24
|
+
uws: uws.WebSocket<UserData>;
|
|
25
|
+
ws: UwsWebSocketProxy;
|
|
26
|
+
uwsData: UserData;
|
|
27
|
+
}> {
|
|
28
|
+
get remoteAddress(): string | undefined;
|
|
29
|
+
send(data: unknown, options?: {
|
|
30
|
+
compress?: boolean;
|
|
31
|
+
}): number;
|
|
32
|
+
subscribe(topic: string): void;
|
|
33
|
+
publish(topic: string, message: string, options?: {
|
|
34
|
+
compress?: boolean;
|
|
35
|
+
}): number;
|
|
36
|
+
close(code?: number, reason?: uws.RecognizedString): void;
|
|
37
|
+
terminate(): void;
|
|
38
|
+
}
|
|
39
|
+
declare class UWSReqProxy {
|
|
40
|
+
private _headers?;
|
|
41
|
+
private _rawHeaders;
|
|
42
|
+
url: string;
|
|
43
|
+
constructor(_req: uws.HttpRequest);
|
|
44
|
+
get headers(): Headers;
|
|
45
|
+
}
|
|
46
|
+
declare class UwsWebSocketProxy implements Partial<WebSocket> {
|
|
47
|
+
private _uws;
|
|
48
|
+
readyState?: number;
|
|
49
|
+
constructor(_uws: uws.WebSocket<UserData>);
|
|
50
|
+
get bufferedAmount(): number;
|
|
51
|
+
get protocol(): string;
|
|
52
|
+
get extensions(): string;
|
|
53
|
+
}
|
|
54
|
+
|
|
19
55
|
export { type UWSAdapter, type UWSOptions, _default as default };
|
package/dist/adapters/uws.mjs
CHANGED
|
@@ -1,151 +1,175 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.DTY7a69w.mjs';
|
|
2
|
+
import { d as defineWebSocketAdapter, a as adapterUtils, A as AdapterHookable } from '../shared/crossws.B4sHId41.mjs';
|
|
3
|
+
import 'uncrypto';
|
|
2
4
|
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
-
var __publicField = (obj, key, value) => {
|
|
6
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
-
return value;
|
|
8
|
-
};
|
|
9
5
|
const uws = defineWebSocketAdapter(
|
|
10
6
|
(options = {}) => {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const peer = getPeer(ws);
|
|
45
|
-
crossws.$callHook("uws:ping", peer, ws, message);
|
|
46
|
-
},
|
|
47
|
-
pong(ws, message) {
|
|
48
|
-
const peer = getPeer(ws);
|
|
49
|
-
crossws.$callHook("uws:pong", peer, ws, message);
|
|
50
|
-
},
|
|
51
|
-
subscription(ws, topic, newCount, oldCount) {
|
|
52
|
-
const peer = getPeer(ws);
|
|
53
|
-
crossws.$callHook(
|
|
54
|
-
"uws:subscription",
|
|
55
|
-
peer,
|
|
56
|
-
ws,
|
|
57
|
-
topic,
|
|
58
|
-
newCount,
|
|
59
|
-
oldCount
|
|
60
|
-
);
|
|
61
|
-
},
|
|
62
|
-
async upgrade(res, req, context) {
|
|
63
|
-
let aborted = false;
|
|
64
|
-
res.onAborted(() => {
|
|
65
|
-
aborted = true;
|
|
66
|
-
});
|
|
67
|
-
const { headers } = await crossws.upgrade({
|
|
68
|
-
get url() {
|
|
69
|
-
return req.getUrl();
|
|
70
|
-
},
|
|
71
|
-
get headers() {
|
|
72
|
-
return _getHeaders(req);
|
|
7
|
+
const hooks = new AdapterHookable(options);
|
|
8
|
+
const peers = /* @__PURE__ */ new Set();
|
|
9
|
+
return {
|
|
10
|
+
...adapterUtils(peers),
|
|
11
|
+
websocket: {
|
|
12
|
+
...options.uws,
|
|
13
|
+
close(ws, code, message) {
|
|
14
|
+
const peer = getPeer(ws, peers);
|
|
15
|
+
peer._internal.ws.readyState = 2;
|
|
16
|
+
peers.delete(peer);
|
|
17
|
+
hooks.callHook("close", peer, {
|
|
18
|
+
code,
|
|
19
|
+
reason: message?.toString()
|
|
20
|
+
});
|
|
21
|
+
peer._internal.ws.readyState = 3;
|
|
22
|
+
},
|
|
23
|
+
message(ws, message, isBinary) {
|
|
24
|
+
const peer = getPeer(ws, peers);
|
|
25
|
+
hooks.callHook("message", peer, new Message(message, peer));
|
|
26
|
+
},
|
|
27
|
+
open(ws) {
|
|
28
|
+
const peer = getPeer(ws, peers);
|
|
29
|
+
peers.add(peer);
|
|
30
|
+
hooks.callHook("open", peer);
|
|
31
|
+
},
|
|
32
|
+
async upgrade(res, req, context) {
|
|
33
|
+
let aborted = false;
|
|
34
|
+
res.onAborted(() => {
|
|
35
|
+
aborted = true;
|
|
36
|
+
});
|
|
37
|
+
const _res = await hooks.callHook("upgrade", new UWSReqProxy(req));
|
|
38
|
+
if (aborted) {
|
|
39
|
+
return;
|
|
73
40
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
41
|
+
if (_res instanceof Response) {
|
|
42
|
+
res.writeStatus(`${_res.status} ${_res.statusText}`);
|
|
43
|
+
for (const [key, value] of _res.headers) {
|
|
44
|
+
res.writeHeader(key, value);
|
|
45
|
+
}
|
|
46
|
+
if (_res.body) {
|
|
47
|
+
for await (const chunk of _res.body) {
|
|
48
|
+
if (aborted) {
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
res.write(chunk);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!aborted) {
|
|
55
|
+
res.end();
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
res.writeStatus("101 Switching Protocols");
|
|
60
|
+
if (_res?.headers) {
|
|
61
|
+
for (const [key, value] of new Headers(_res.headers)) {
|
|
62
|
+
res.writeHeader(key, value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
res.cork(() => {
|
|
66
|
+
const key = req.getHeader("sec-websocket-key");
|
|
67
|
+
const protocol = req.getHeader("sec-websocket-protocol");
|
|
68
|
+
const extensions = req.getHeader("sec-websocket-extensions");
|
|
69
|
+
res.upgrade(
|
|
70
|
+
{
|
|
71
|
+
req,
|
|
72
|
+
res,
|
|
73
|
+
protocol,
|
|
74
|
+
extensions
|
|
75
|
+
},
|
|
76
|
+
key,
|
|
77
|
+
protocol,
|
|
78
|
+
extensions,
|
|
87
79
|
context
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
req.getHeader("sec-websocket-extensions"),
|
|
92
|
-
context
|
|
93
|
-
);
|
|
94
|
-
});
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
95
83
|
}
|
|
96
84
|
};
|
|
97
|
-
return {
|
|
98
|
-
websocket
|
|
99
|
-
};
|
|
100
85
|
}
|
|
101
86
|
);
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
__publicField(this, "_decoder", new TextDecoder());
|
|
87
|
+
function getPeer(uws, peers) {
|
|
88
|
+
const uwsData = uws.getUserData();
|
|
89
|
+
if (uwsData.peer) {
|
|
90
|
+
return uwsData.peer;
|
|
107
91
|
}
|
|
108
|
-
|
|
92
|
+
const peer = new UWSPeer({
|
|
93
|
+
peers,
|
|
94
|
+
uws,
|
|
95
|
+
ws: new UwsWebSocketProxy(uws),
|
|
96
|
+
request: new UWSReqProxy(uwsData.req),
|
|
97
|
+
uwsData
|
|
98
|
+
});
|
|
99
|
+
uwsData.peer = peer;
|
|
100
|
+
return peer;
|
|
101
|
+
}
|
|
102
|
+
class UWSPeer extends Peer {
|
|
103
|
+
get remoteAddress() {
|
|
109
104
|
try {
|
|
110
|
-
const addr =
|
|
111
|
-
this.
|
|
105
|
+
const addr = new TextDecoder().decode(
|
|
106
|
+
this._internal.uws.getRemoteAddressAsText()
|
|
112
107
|
);
|
|
113
|
-
return addr
|
|
108
|
+
return addr;
|
|
114
109
|
} catch {
|
|
115
110
|
}
|
|
116
111
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return this.
|
|
112
|
+
send(data, options) {
|
|
113
|
+
const dataBuff = toBufferLike(data);
|
|
114
|
+
const isBinary = typeof data !== "string";
|
|
115
|
+
return this._internal.uws.send(dataBuff, isBinary, options?.compress);
|
|
116
|
+
}
|
|
117
|
+
subscribe(topic) {
|
|
118
|
+
this._internal.uws.subscribe(topic);
|
|
119
|
+
}
|
|
120
|
+
publish(topic, message, options) {
|
|
121
|
+
const data = toBufferLike(message);
|
|
122
|
+
const isBinary = typeof data !== "string";
|
|
123
|
+
this._internal.uws.publish(topic, data, isBinary, options?.compress);
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
close(code, reason) {
|
|
127
|
+
this._internal.uws.end(code, reason);
|
|
128
|
+
}
|
|
129
|
+
terminate() {
|
|
130
|
+
this._internal.uws.close();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
class UWSReqProxy {
|
|
134
|
+
_headers;
|
|
135
|
+
_rawHeaders = [];
|
|
136
|
+
url;
|
|
137
|
+
constructor(_req) {
|
|
138
|
+
let host = "localhost";
|
|
139
|
+
let proto = "http";
|
|
140
|
+
_req.forEach((key, value) => {
|
|
141
|
+
if (key === "host") {
|
|
142
|
+
host = value;
|
|
143
|
+
} else if (key === "x-forwarded-proto" && value === "https") {
|
|
144
|
+
proto = "https";
|
|
145
|
+
}
|
|
146
|
+
this._rawHeaders.push([key, value]);
|
|
147
|
+
});
|
|
148
|
+
const query = _req.getQuery();
|
|
149
|
+
const pathname = _req.getUrl();
|
|
150
|
+
this.url = `${proto}://${host}${pathname}${query ? `?${query}` : ""}`;
|
|
121
151
|
}
|
|
122
152
|
get headers() {
|
|
123
153
|
if (!this._headers) {
|
|
124
|
-
this._headers =
|
|
154
|
+
this._headers = new Headers(this._rawHeaders);
|
|
125
155
|
}
|
|
126
156
|
return this._headers;
|
|
127
157
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
options?.compress
|
|
133
|
-
);
|
|
158
|
+
}
|
|
159
|
+
class UwsWebSocketProxy {
|
|
160
|
+
constructor(_uws) {
|
|
161
|
+
this._uws = _uws;
|
|
134
162
|
}
|
|
135
|
-
|
|
136
|
-
|
|
163
|
+
readyState = 1;
|
|
164
|
+
get bufferedAmount() {
|
|
165
|
+
return this._uws?.getBufferedAmount();
|
|
137
166
|
}
|
|
138
|
-
|
|
139
|
-
this.
|
|
140
|
-
|
|
167
|
+
get protocol() {
|
|
168
|
+
return this._uws?.getUserData().protocol;
|
|
169
|
+
}
|
|
170
|
+
get extensions() {
|
|
171
|
+
return this._uws?.getUserData().extensions;
|
|
141
172
|
}
|
|
142
|
-
}
|
|
143
|
-
function _getHeaders(req) {
|
|
144
|
-
const headers = [];
|
|
145
|
-
req.forEach((key, value) => {
|
|
146
|
-
headers.push([key, value]);
|
|
147
|
-
});
|
|
148
|
-
return headers;
|
|
149
173
|
}
|
|
150
174
|
|
|
151
175
|
export { uws as default };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,138 @@
|
|
|
1
|
-
import {
|
|
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';
|
|
1
|
+
import { W as WebSocket } from './shared/crossws.ChIJSJVK.mjs';
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
interface AdapterInternal {
|
|
4
|
+
ws: unknown;
|
|
5
|
+
request?: Request | Partial<Request>;
|
|
6
|
+
peers?: Set<Peer>;
|
|
7
|
+
}
|
|
8
|
+
declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
|
|
9
|
+
#private;
|
|
10
|
+
protected _internal: Internal;
|
|
11
|
+
protected _topics: Set<string>;
|
|
12
|
+
protected _id?: string;
|
|
13
|
+
constructor(internal: Internal);
|
|
14
|
+
/**
|
|
15
|
+
* Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
|
|
16
|
+
*/
|
|
17
|
+
get id(): string;
|
|
18
|
+
/** IP address of the peer */
|
|
19
|
+
get remoteAddress(): string | undefined;
|
|
20
|
+
/** upgrade request */
|
|
21
|
+
get request(): Request | Partial<Request> | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
|
|
24
|
+
*
|
|
25
|
+
* **Note:** crossws adds polyfill for the following properties if native values are not available:
|
|
26
|
+
* - `protocol`: Extracted from the `sec-websocket-protocol` header.
|
|
27
|
+
* - `extensions`: Extracted from the `sec-websocket-extensions` header.
|
|
28
|
+
* - `url`: Extracted from the request URL (http -> ws).
|
|
29
|
+
* */
|
|
30
|
+
get websocket(): Partial<WebSocket>;
|
|
31
|
+
/** All connected peers to the server */
|
|
32
|
+
get peers(): Set<Peer>;
|
|
33
|
+
abstract close(code?: number, reason?: string): void;
|
|
34
|
+
/** Abruptly close the connection */
|
|
35
|
+
terminate(): void;
|
|
36
|
+
/** Subscribe to a topic */
|
|
37
|
+
subscribe(topic: string): void;
|
|
38
|
+
/** Unsubscribe from a topic */
|
|
39
|
+
unsubscribe(topic: string): void;
|
|
40
|
+
/** Send a message to the peer. */
|
|
41
|
+
abstract send(data: unknown, options?: {
|
|
42
|
+
compress?: boolean;
|
|
43
|
+
}): number | void | undefined;
|
|
44
|
+
/** Send message to subscribes of topic */
|
|
45
|
+
abstract publish(topic: string, data: unknown, options?: {
|
|
46
|
+
compress?: boolean;
|
|
47
|
+
}): void;
|
|
48
|
+
toString(): string;
|
|
49
|
+
[Symbol.toPrimitive](): string;
|
|
50
|
+
[Symbol.toStringTag](): string;
|
|
51
|
+
}
|
|
5
52
|
|
|
6
|
-
|
|
53
|
+
interface AdapterInstance {
|
|
54
|
+
readonly peers: Set<Peer>;
|
|
55
|
+
readonly publish: Peer["publish"];
|
|
56
|
+
}
|
|
57
|
+
interface AdapterOptions {
|
|
58
|
+
resolve?: ResolveHooks;
|
|
59
|
+
hooks?: Hooks;
|
|
60
|
+
}
|
|
61
|
+
type Adapter<AdapterT extends AdapterInstance = AdapterInstance, Options extends AdapterOptions = AdapterOptions> = (options?: Options) => AdapterT;
|
|
62
|
+
declare function defineWebSocketAdapter<AdapterT extends AdapterInstance = AdapterInstance, Options extends AdapterOptions = AdapterOptions>(factory: Adapter<AdapterT, Options>): Adapter<AdapterT, Options>;
|
|
63
|
+
|
|
64
|
+
declare class WSError extends Error {
|
|
65
|
+
constructor(...args: any[]);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare class Message implements Partial<MessageEvent> {
|
|
69
|
+
#private;
|
|
70
|
+
/** Access to the original [message event](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event) if available. */
|
|
71
|
+
readonly event?: MessageEvent;
|
|
72
|
+
/** Access to the Peer that emitted the message. */
|
|
73
|
+
readonly peer?: Peer;
|
|
74
|
+
/** Raw message data (can be of any type). */
|
|
75
|
+
readonly rawData: unknown;
|
|
76
|
+
constructor(rawData: unknown, peer: Peer, event?: MessageEvent);
|
|
77
|
+
/**
|
|
78
|
+
* Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the message.
|
|
79
|
+
*/
|
|
80
|
+
get id(): string;
|
|
81
|
+
/**
|
|
82
|
+
* Get data as [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) value.
|
|
83
|
+
*
|
|
84
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
85
|
+
*/
|
|
86
|
+
uint8Array(): Uint8Array;
|
|
87
|
+
/**
|
|
88
|
+
* Get data as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) or [SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) value.
|
|
89
|
+
*
|
|
90
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
91
|
+
*/
|
|
92
|
+
arrayBuffer(): ArrayBuffer | SharedArrayBuffer;
|
|
93
|
+
/**
|
|
94
|
+
* Get data as [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) value.
|
|
95
|
+
*
|
|
96
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded. */
|
|
97
|
+
blob(): Blob;
|
|
98
|
+
/**
|
|
99
|
+
* Get stringified text version of the message.
|
|
100
|
+
*
|
|
101
|
+
* If raw data is in any other format, it will be automatically converted and decoded.
|
|
102
|
+
*/
|
|
103
|
+
text(): string;
|
|
104
|
+
/**
|
|
105
|
+
* Get parsed version of the message text with [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
|
|
106
|
+
*/
|
|
107
|
+
json<T = unknown>(): T;
|
|
108
|
+
/**
|
|
109
|
+
* Message data (value varies based on `peer.websocket.binaryType`).
|
|
110
|
+
*/
|
|
111
|
+
get data(): unknown;
|
|
112
|
+
toString(): string;
|
|
113
|
+
[Symbol.toPrimitive](): string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
|
|
117
|
+
type ResolveHooks = (info: RequestInit | Peer) => Partial<Hooks> | Promise<Partial<Hooks>>;
|
|
118
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
119
|
+
interface Hooks {
|
|
120
|
+
/** Upgrading */
|
|
121
|
+
upgrade: (request: Request | {
|
|
122
|
+
url: string;
|
|
123
|
+
headers: Headers;
|
|
124
|
+
}) => MaybePromise<Response | ResponseInit | void>;
|
|
125
|
+
/** A message is received */
|
|
126
|
+
message: (peer: Peer, message: Message) => MaybePromise<void>;
|
|
127
|
+
/** A socket is opened */
|
|
128
|
+
open: (peer: Peer) => MaybePromise<void>;
|
|
129
|
+
/** A socket is closed */
|
|
130
|
+
close: (peer: Peer, details: {
|
|
131
|
+
code?: number;
|
|
132
|
+
reason?: string;
|
|
133
|
+
}) => MaybePromise<void>;
|
|
134
|
+
/** An error occurs */
|
|
135
|
+
error: (peer: Peer, error: WSError) => MaybePromise<void>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export { type Adapter, type AdapterInstance, type AdapterOptions, type Hooks, Message, Peer, type ResolveHooks, WSError, defineHooks, defineWebSocketAdapter };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,138 @@
|
|
|
1
|
-
import {
|
|
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';
|
|
1
|
+
import { W as WebSocket } from './shared/crossws.ChIJSJVK.js';
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
interface AdapterInternal {
|
|
4
|
+
ws: unknown;
|
|
5
|
+
request?: Request | Partial<Request>;
|
|
6
|
+
peers?: Set<Peer>;
|
|
7
|
+
}
|
|
8
|
+
declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
|
|
9
|
+
#private;
|
|
10
|
+
protected _internal: Internal;
|
|
11
|
+
protected _topics: Set<string>;
|
|
12
|
+
protected _id?: string;
|
|
13
|
+
constructor(internal: Internal);
|
|
14
|
+
/**
|
|
15
|
+
* Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
|
|
16
|
+
*/
|
|
17
|
+
get id(): string;
|
|
18
|
+
/** IP address of the peer */
|
|
19
|
+
get remoteAddress(): string | undefined;
|
|
20
|
+
/** upgrade request */
|
|
21
|
+
get request(): Request | Partial<Request> | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
|
|
24
|
+
*
|
|
25
|
+
* **Note:** crossws adds polyfill for the following properties if native values are not available:
|
|
26
|
+
* - `protocol`: Extracted from the `sec-websocket-protocol` header.
|
|
27
|
+
* - `extensions`: Extracted from the `sec-websocket-extensions` header.
|
|
28
|
+
* - `url`: Extracted from the request URL (http -> ws).
|
|
29
|
+
* */
|
|
30
|
+
get websocket(): Partial<WebSocket>;
|
|
31
|
+
/** All connected peers to the server */
|
|
32
|
+
get peers(): Set<Peer>;
|
|
33
|
+
abstract close(code?: number, reason?: string): void;
|
|
34
|
+
/** Abruptly close the connection */
|
|
35
|
+
terminate(): void;
|
|
36
|
+
/** Subscribe to a topic */
|
|
37
|
+
subscribe(topic: string): void;
|
|
38
|
+
/** Unsubscribe from a topic */
|
|
39
|
+
unsubscribe(topic: string): void;
|
|
40
|
+
/** Send a message to the peer. */
|
|
41
|
+
abstract send(data: unknown, options?: {
|
|
42
|
+
compress?: boolean;
|
|
43
|
+
}): number | void | undefined;
|
|
44
|
+
/** Send message to subscribes of topic */
|
|
45
|
+
abstract publish(topic: string, data: unknown, options?: {
|
|
46
|
+
compress?: boolean;
|
|
47
|
+
}): void;
|
|
48
|
+
toString(): string;
|
|
49
|
+
[Symbol.toPrimitive](): string;
|
|
50
|
+
[Symbol.toStringTag](): string;
|
|
51
|
+
}
|
|
5
52
|
|
|
6
|
-
|
|
53
|
+
interface AdapterInstance {
|
|
54
|
+
readonly peers: Set<Peer>;
|
|
55
|
+
readonly publish: Peer["publish"];
|
|
56
|
+
}
|
|
57
|
+
interface AdapterOptions {
|
|
58
|
+
resolve?: ResolveHooks;
|
|
59
|
+
hooks?: Hooks;
|
|
60
|
+
}
|
|
61
|
+
type Adapter<AdapterT extends AdapterInstance = AdapterInstance, Options extends AdapterOptions = AdapterOptions> = (options?: Options) => AdapterT;
|
|
62
|
+
declare function defineWebSocketAdapter<AdapterT extends AdapterInstance = AdapterInstance, Options extends AdapterOptions = AdapterOptions>(factory: Adapter<AdapterT, Options>): Adapter<AdapterT, Options>;
|
|
63
|
+
|
|
64
|
+
declare class WSError extends Error {
|
|
65
|
+
constructor(...args: any[]);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare class Message implements Partial<MessageEvent> {
|
|
69
|
+
#private;
|
|
70
|
+
/** Access to the original [message event](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event) if available. */
|
|
71
|
+
readonly event?: MessageEvent;
|
|
72
|
+
/** Access to the Peer that emitted the message. */
|
|
73
|
+
readonly peer?: Peer;
|
|
74
|
+
/** Raw message data (can be of any type). */
|
|
75
|
+
readonly rawData: unknown;
|
|
76
|
+
constructor(rawData: unknown, peer: Peer, event?: MessageEvent);
|
|
77
|
+
/**
|
|
78
|
+
* Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the message.
|
|
79
|
+
*/
|
|
80
|
+
get id(): string;
|
|
81
|
+
/**
|
|
82
|
+
* Get data as [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) value.
|
|
83
|
+
*
|
|
84
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
85
|
+
*/
|
|
86
|
+
uint8Array(): Uint8Array;
|
|
87
|
+
/**
|
|
88
|
+
* Get data as [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) or [SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) value.
|
|
89
|
+
*
|
|
90
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded.
|
|
91
|
+
*/
|
|
92
|
+
arrayBuffer(): ArrayBuffer | SharedArrayBuffer;
|
|
93
|
+
/**
|
|
94
|
+
* Get data as [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) value.
|
|
95
|
+
*
|
|
96
|
+
* If raw data is in any other format or string, it will be automatically converted and encoded. */
|
|
97
|
+
blob(): Blob;
|
|
98
|
+
/**
|
|
99
|
+
* Get stringified text version of the message.
|
|
100
|
+
*
|
|
101
|
+
* If raw data is in any other format, it will be automatically converted and decoded.
|
|
102
|
+
*/
|
|
103
|
+
text(): string;
|
|
104
|
+
/**
|
|
105
|
+
* Get parsed version of the message text with [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
|
|
106
|
+
*/
|
|
107
|
+
json<T = unknown>(): T;
|
|
108
|
+
/**
|
|
109
|
+
* Message data (value varies based on `peer.websocket.binaryType`).
|
|
110
|
+
*/
|
|
111
|
+
get data(): unknown;
|
|
112
|
+
toString(): string;
|
|
113
|
+
[Symbol.toPrimitive](): string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
|
|
117
|
+
type ResolveHooks = (info: RequestInit | Peer) => Partial<Hooks> | Promise<Partial<Hooks>>;
|
|
118
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
119
|
+
interface Hooks {
|
|
120
|
+
/** Upgrading */
|
|
121
|
+
upgrade: (request: Request | {
|
|
122
|
+
url: string;
|
|
123
|
+
headers: Headers;
|
|
124
|
+
}) => MaybePromise<Response | ResponseInit | void>;
|
|
125
|
+
/** A message is received */
|
|
126
|
+
message: (peer: Peer, message: Message) => MaybePromise<void>;
|
|
127
|
+
/** A socket is opened */
|
|
128
|
+
open: (peer: Peer) => MaybePromise<void>;
|
|
129
|
+
/** A socket is closed */
|
|
130
|
+
close: (peer: Peer, details: {
|
|
131
|
+
code?: number;
|
|
132
|
+
reason?: string;
|
|
133
|
+
}) => MaybePromise<void>;
|
|
134
|
+
/** An error occurs */
|
|
135
|
+
error: (peer: Peer, error: WSError) => MaybePromise<void>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export { type Adapter, type AdapterInstance, type AdapterOptions, type Hooks, Message, Peer, type ResolveHooks, WSError, defineHooks, defineWebSocketAdapter };
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
class WSError extends Error {
|
|
4
|
-
constructor(...args) {
|
|
5
|
-
super(...args);
|
|
6
|
-
this.name = "WSError";
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { WSError };
|
|
1
|
+
export { b as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.B4sHId41.mjs';
|