crossws 0.4.2 → 0.4.4

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.
Files changed (52) hide show
  1. package/dist/_chunks/_request.mjs +83 -0
  2. package/dist/_chunks/_types.d.mts +24 -0
  3. package/dist/_chunks/adapter.d.mts +174 -0
  4. package/dist/_chunks/adapter.mjs +93 -0
  5. package/dist/_chunks/bun.d.mts +38 -0
  6. package/dist/_chunks/cloudflare.d.mts +45 -0
  7. package/dist/_chunks/deno.d.mts +17 -0
  8. package/dist/_chunks/error.mjs +10 -0
  9. package/dist/_chunks/libs/ws.mjs +3531 -0
  10. package/dist/_chunks/node.d.mts +299 -0
  11. package/dist/_chunks/peer.mjs +244 -0
  12. package/dist/_chunks/rolldown-runtime.mjs +32 -0
  13. package/dist/_chunks/sse.d.mts +12 -0
  14. package/dist/_chunks/web.d.mts +298 -0
  15. package/dist/adapters/bun.d.mts +2 -41
  16. package/dist/adapters/bun.mjs +83 -93
  17. package/dist/adapters/cloudflare.d.mts +2 -46
  18. package/dist/adapters/cloudflare.mjs +173 -219
  19. package/dist/adapters/deno.d.mts +2 -19
  20. package/dist/adapters/deno.mjs +65 -74
  21. package/dist/adapters/node.d.mts +2 -299
  22. package/dist/adapters/node.mjs +119 -156
  23. package/dist/adapters/sse.d.mts +2 -13
  24. package/dist/adapters/sse.mjs +98 -118
  25. package/dist/adapters/uws.d.mts +44 -44
  26. package/dist/adapters/uws.mjs +152 -175
  27. package/dist/index.d.mts +2 -170
  28. package/dist/index.mjs +3 -1
  29. package/dist/server/bun.d.mts +8 -21
  30. package/dist/server/bun.mjs +24 -31
  31. package/dist/server/cloudflare.d.mts +8 -21
  32. package/dist/server/cloudflare.mjs +21 -30
  33. package/dist/server/default.d.mts +8 -21
  34. package/dist/server/default.mjs +22 -26
  35. package/dist/server/deno.d.mts +8 -21
  36. package/dist/server/deno.mjs +21 -24
  37. package/dist/server/node.d.mts +8 -21
  38. package/dist/server/node.mjs +32 -43
  39. package/dist/websocket/native.d.mts +3 -2
  40. package/dist/websocket/native.mjs +4 -1
  41. package/dist/websocket/node.d.mts +3 -2
  42. package/dist/websocket/node.mjs +7 -13
  43. package/dist/websocket/sse.d.mts +34 -34
  44. package/dist/websocket/sse.mjs +112 -121
  45. package/package.json +14 -13
  46. package/dist/shared/crossws.B31KJMcF.mjs +0 -83
  47. package/dist/shared/crossws.BQXMA5bH.d.mts +0 -297
  48. package/dist/shared/crossws.By9qWDAI.mjs +0 -8
  49. package/dist/shared/crossws.C5pESzqN.mjs +0 -4993
  50. package/dist/shared/crossws.CP-89VBK.d.mts +0 -23
  51. package/dist/shared/crossws.CPlNx7g8.mjs +0 -105
  52. package/dist/shared/crossws.WpyOHUXc.mjs +0 -330
@@ -1,122 +1,102 @@
1
- import { M as Message, P as Peer, a as toString } from '../shared/crossws.WpyOHUXc.mjs';
2
- import { a as adapterUtils, A as AdapterHookable, g as getPeers } from '../shared/crossws.CPlNx7g8.mjs';
1
+ import { i as AdapterHookable, r as getPeers, t as adapterUtils } from "../_chunks/adapter.mjs";
2
+ import { i as toString, n as Message, t as Peer } from "../_chunks/peer.mjs";
3
3
 
4
+ //#region src/adapters/sse.ts
4
5
  const sseAdapter = (opts = {}) => {
5
- const hooks = new AdapterHookable(opts);
6
- const globalPeers = /* @__PURE__ */ new Map();
7
- const peersMap = opts.bidir ? /* @__PURE__ */ new Map() : void 0;
8
- return {
9
- ...adapterUtils(globalPeers),
10
- fetch: async (request) => {
11
- const { upgradeHeaders, endResponse, context, namespace } = await hooks.upgrade(request);
12
- if (endResponse) {
13
- return endResponse;
14
- }
15
- let peer;
16
- if (opts.bidir && request.body && request.headers.has("x-crossws-id")) {
17
- const id = request.headers.get("x-crossws-id");
18
- peer = peersMap?.get(id);
19
- if (!peer) {
20
- return new Response("invalid peer id", { status: 400 });
21
- }
22
- const stream = request.body.pipeThrough(new TextDecoderStream());
23
- try {
24
- for await (const chunk of stream) {
25
- hooks.callHook("message", peer, new Message(chunk, peer));
26
- }
27
- } catch {
28
- await stream.cancel().catch(() => {
29
- });
30
- }
31
- return new Response(null, {});
32
- } else {
33
- const ws = new SSEWebSocketStub();
34
- const peers = getPeers(globalPeers, namespace);
35
- peer = new SSEPeer({
36
- peers,
37
- peersMap,
38
- request,
39
- hooks,
40
- ws,
41
- context,
42
- namespace
43
- });
44
- peers.add(peer);
45
- if (opts.bidir) {
46
- peersMap.set(peer.id, peer);
47
- peer._sendEvent("crossws-id", peer.id);
48
- }
49
- }
50
- let headers = {
51
- "Content-Type": "text/event-stream",
52
- "Cache-Control": "no-cache",
53
- Connection: "keep-alive"
54
- };
55
- if (opts.bidir) {
56
- headers["x-crossws-id"] = peer.id;
57
- }
58
- if (upgradeHeaders) {
59
- headers = new Headers(headers);
60
- for (const [key, value] of new Headers(upgradeHeaders)) {
61
- headers.set(key, value);
62
- }
63
- }
64
- return new Response(peer._sseStream, { headers });
65
- }
66
- };
6
+ const hooks = new AdapterHookable(opts);
7
+ const globalPeers = /* @__PURE__ */ new Map();
8
+ const peersMap = opts.bidir ? /* @__PURE__ */ new Map() : void 0;
9
+ return {
10
+ ...adapterUtils(globalPeers),
11
+ fetch: async (request) => {
12
+ const { upgradeHeaders, endResponse, context, namespace } = await hooks.upgrade(request);
13
+ if (endResponse) return endResponse;
14
+ let peer;
15
+ if (opts.bidir && request.body && request.headers.has("x-crossws-id")) {
16
+ const id = request.headers.get("x-crossws-id");
17
+ peer = peersMap?.get(id);
18
+ if (!peer) return new Response("invalid peer id", { status: 400 });
19
+ const stream = request.body.pipeThrough(new TextDecoderStream());
20
+ try {
21
+ for await (const chunk of stream) hooks.callHook("message", peer, new Message(chunk, peer));
22
+ } catch {
23
+ await stream.cancel().catch(() => {});
24
+ }
25
+ return new Response(null, {});
26
+ } else {
27
+ const ws = new SSEWebSocketStub();
28
+ const peers = getPeers(globalPeers, namespace);
29
+ peer = new SSEPeer({
30
+ peers,
31
+ peersMap,
32
+ request,
33
+ hooks,
34
+ ws,
35
+ context,
36
+ namespace
37
+ });
38
+ peers.add(peer);
39
+ if (opts.bidir) {
40
+ peersMap.set(peer.id, peer);
41
+ peer._sendEvent("crossws-id", peer.id);
42
+ }
43
+ }
44
+ let headers = {
45
+ "Content-Type": "text/event-stream",
46
+ "Cache-Control": "no-cache",
47
+ Connection: "keep-alive"
48
+ };
49
+ if (opts.bidir) headers["x-crossws-id"] = peer.id;
50
+ if (upgradeHeaders) {
51
+ headers = new Headers(headers);
52
+ for (const [key, value] of new Headers(upgradeHeaders)) headers.set(key, value);
53
+ }
54
+ return new Response(peer._sseStream, { headers });
55
+ }
56
+ };
57
+ };
58
+ var sse_default = sseAdapter;
59
+ var SSEPeer = class extends Peer {
60
+ _sseStream;
61
+ _sseStreamController;
62
+ constructor(_internal) {
63
+ super(_internal);
64
+ _internal.ws.readyState = 0;
65
+ this._sseStream = new ReadableStream({
66
+ start: (controller) => {
67
+ _internal.ws.readyState = 1;
68
+ this._sseStreamController = controller;
69
+ _internal.hooks.callHook("open", this);
70
+ },
71
+ cancel: () => {
72
+ _internal.ws.readyState = 2;
73
+ _internal.peers.delete(this);
74
+ _internal.peersMap?.delete(this.id);
75
+ Promise.resolve(this._internal.hooks.callHook("close", this)).finally(() => {
76
+ _internal.ws.readyState = 3;
77
+ });
78
+ }
79
+ }).pipeThrough(new TextEncoderStream());
80
+ }
81
+ _sendEvent(event, data) {
82
+ const lines = data.split("\n");
83
+ this._sseStreamController?.enqueue(`event: ${event}\n${lines.map((l) => `data: ${l}`)}\n\n`);
84
+ }
85
+ send(data) {
86
+ this._sendEvent("message", toString(data));
87
+ return 0;
88
+ }
89
+ publish(topic, data) {
90
+ const dataBuff = toString(data);
91
+ for (const peer of this._internal.peers) if (peer !== this && peer._topics.has(topic)) peer._sendEvent("message", dataBuff);
92
+ }
93
+ close() {
94
+ this._sseStreamController?.close();
95
+ }
96
+ };
97
+ var SSEWebSocketStub = class {
98
+ readyState;
67
99
  };
68
- class SSEPeer extends Peer {
69
- _sseStream;
70
- // server -> client
71
- _sseStreamController;
72
- constructor(_internal) {
73
- super(_internal);
74
- _internal.ws.readyState = 0;
75
- this._sseStream = new ReadableStream({
76
- start: (controller) => {
77
- _internal.ws.readyState = 1;
78
- this._sseStreamController = controller;
79
- _internal.hooks.callHook("open", this);
80
- },
81
- cancel: () => {
82
- _internal.ws.readyState = 2;
83
- _internal.peers.delete(this);
84
- _internal.peersMap?.delete(this.id);
85
- Promise.resolve(this._internal.hooks.callHook("close", this)).finally(
86
- () => {
87
- _internal.ws.readyState = 3;
88
- }
89
- );
90
- }
91
- }).pipeThrough(new TextEncoderStream());
92
- }
93
- _sendEvent(event, data) {
94
- const lines = data.split("\n");
95
- this._sseStreamController?.enqueue(
96
- `event: ${event}
97
- ${lines.map((l) => `data: ${l}`)}
98
-
99
- `
100
- );
101
- }
102
- send(data) {
103
- this._sendEvent("message", toString(data));
104
- return 0;
105
- }
106
- publish(topic, data) {
107
- const dataBuff = toString(data);
108
- for (const peer of this._internal.peers) {
109
- if (peer !== this && peer._topics.has(topic)) {
110
- peer._sendEvent("message", dataBuff);
111
- }
112
- }
113
- }
114
- close() {
115
- this._sseStreamController?.close();
116
- }
117
- }
118
- class SSEWebSocketStub {
119
- readyState;
120
- }
121
100
 
122
- export { sseAdapter as default };
101
+ //#endregion
102
+ export { sse_default as default };
@@ -1,62 +1,62 @@
1
- import { Adapter, AdapterInstance, Peer, PeerContext, AdapterOptions } from '../index.mjs';
2
- import { W as WebSocket } from '../shared/crossws.BQXMA5bH.mjs';
3
- import uws from 'uWebSockets.js';
1
+ import { d as PeerContext, n as AdapterInstance, r as AdapterOptions, t as Adapter, u as Peer } from "../_chunks/adapter.mjs";
2
+ import { a as WebSocket } from "../_chunks/web.mjs";
3
+ import uws from "uWebSockets.js";
4
4
 
5
+ //#region src/_request.d.ts
5
6
  declare const StubRequest: {
6
- new (url: string, init?: RequestInit): Request;
7
+ new (url: string, init?: RequestInit): Request;
7
8
  };
8
-
9
+ //#endregion
10
+ //#region src/adapters/uws.d.ts
9
11
  type UserData = {
10
- peer?: UWSPeer;
11
- req: uws.HttpRequest;
12
- res: uws.HttpResponse;
13
- webReq: UWSReqProxy;
14
- protocol: string;
15
- extensions: string;
16
- context: PeerContext;
17
- namespace: string;
12
+ peer?: UWSPeer;
13
+ req: uws.HttpRequest;
14
+ res: uws.HttpResponse;
15
+ webReq: UWSReqProxy;
16
+ protocol: string;
17
+ extensions: string;
18
+ context: PeerContext;
19
+ namespace: string;
18
20
  };
19
21
  type WebSocketHandler = uws.WebSocketBehavior<UserData>;
20
22
  interface UWSAdapter extends AdapterInstance {
21
- websocket: WebSocketHandler;
23
+ websocket: WebSocketHandler;
22
24
  }
23
25
  interface UWSOptions extends AdapterOptions {
24
- uws?: Exclude<uws.WebSocketBehavior<any>, "close" | "drain" | "message" | "open" | "ping" | "pong" | "subscription" | "upgrade">;
26
+ uws?: Exclude<uws.WebSocketBehavior<any>, "close" | "drain" | "message" | "open" | "ping" | "pong" | "subscription" | "upgrade">;
25
27
  }
26
28
  declare const uwsAdapter: Adapter<UWSAdapter, UWSOptions>;
27
-
28
29
  declare class UWSPeer extends Peer<{
29
- peers: Set<UWSPeer>;
30
- request: UWSReqProxy;
31
- namespace: string;
32
- uws: uws.WebSocket<UserData>;
33
- ws: UwsWebSocketProxy;
34
- uwsData: UserData;
30
+ peers: Set<UWSPeer>;
31
+ request: UWSReqProxy;
32
+ namespace: string;
33
+ uws: uws.WebSocket<UserData>;
34
+ ws: UwsWebSocketProxy;
35
+ uwsData: UserData;
35
36
  }> {
36
- get remoteAddress(): string | undefined;
37
- get context(): PeerContext;
38
- send(data: unknown, options?: {
39
- compress?: boolean;
40
- }): number;
41
- subscribe(topic: string): void;
42
- unsubscribe(topic: string): void;
43
- publish(topic: string, message: string, options?: {
44
- compress?: boolean;
45
- }): number;
46
- close(code?: number, reason?: uws.RecognizedString): void;
47
- terminate(): void;
37
+ get remoteAddress(): string | undefined;
38
+ get context(): PeerContext;
39
+ send(data: unknown, options?: {
40
+ compress?: boolean;
41
+ }): number;
42
+ subscribe(topic: string): void;
43
+ unsubscribe(topic: string): void;
44
+ publish(topic: string, message: string, options?: {
45
+ compress?: boolean;
46
+ }): number;
47
+ close(code?: number, reason?: uws.RecognizedString): void;
48
+ terminate(): void;
48
49
  }
49
50
  declare class UWSReqProxy extends StubRequest {
50
- constructor(req: uws.HttpRequest);
51
+ constructor(req: uws.HttpRequest);
51
52
  }
52
53
  declare class UwsWebSocketProxy implements Partial<WebSocket> {
53
- private _uws;
54
- readyState?: number;
55
- constructor(_uws: uws.WebSocket<UserData>);
56
- get bufferedAmount(): number;
57
- get protocol(): string;
58
- get extensions(): string;
54
+ private _uws;
55
+ readyState?: number;
56
+ constructor(_uws: uws.WebSocket<UserData>);
57
+ get bufferedAmount(): number;
58
+ get protocol(): string;
59
+ get extensions(): string;
59
60
  }
60
-
61
- export { uwsAdapter as default };
62
- export type { UWSAdapter, UWSOptions };
61
+ //#endregion
62
+ export { UWSAdapter, UWSOptions, uwsAdapter as default };
@@ -1,181 +1,158 @@
1
- import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.WpyOHUXc.mjs';
2
- import { a as adapterUtils, A as AdapterHookable, g as getPeers } from '../shared/crossws.CPlNx7g8.mjs';
3
- import { S as StubRequest } from '../shared/crossws.B31KJMcF.mjs';
1
+ import { i as AdapterHookable, r as getPeers, t as adapterUtils } from "../_chunks/adapter.mjs";
2
+ import { n as Message, r as toBufferLike, t as Peer } from "../_chunks/peer.mjs";
3
+ import { t as StubRequest } from "../_chunks/_request.mjs";
4
4
 
5
+ //#region src/adapters/uws.ts
5
6
  const uwsAdapter = (options = {}) => {
6
- const hooks = new AdapterHookable(options);
7
- const globalPeers = /* @__PURE__ */ new Map();
8
- return {
9
- ...adapterUtils(globalPeers),
10
- websocket: {
11
- ...options.uws,
12
- close(ws, code, message) {
13
- const peers = getPeers(globalPeers, ws.getUserData().namespace);
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 peers = getPeers(globalPeers, ws.getUserData().namespace);
25
- const peer = getPeer(ws, peers);
26
- hooks.callHook("message", peer, new Message(message, peer));
27
- },
28
- open(ws) {
29
- const peers = getPeers(globalPeers, ws.getUserData().namespace);
30
- const peer = getPeer(ws, peers);
31
- peers.add(peer);
32
- hooks.callHook("open", peer);
33
- },
34
- async upgrade(res, req, uwsContext) {
35
- let aborted = false;
36
- res.onAborted(() => {
37
- aborted = true;
38
- });
39
- const webReq = new UWSReqProxy(req);
40
- const { upgradeHeaders, endResponse, context, namespace } = await hooks.upgrade(webReq);
41
- if (endResponse) {
42
- res.writeStatus(`${endResponse.status} ${endResponse.statusText}`);
43
- for (const [key, value] of endResponse.headers) {
44
- res.writeHeader(key, value);
45
- }
46
- if (endResponse.body) {
47
- for await (const chunk of endResponse.body) {
48
- if (aborted) break;
49
- res.write(chunk);
50
- }
51
- }
52
- if (!aborted) {
53
- res.end();
54
- }
55
- return;
56
- }
57
- if (aborted) {
58
- return;
59
- }
60
- res.writeStatus("101 Switching Protocols");
61
- if (upgradeHeaders) {
62
- const headers = upgradeHeaders instanceof Headers ? upgradeHeaders : new Headers(upgradeHeaders);
63
- for (const [key, value] of headers) {
64
- res.writeHeader(key, value);
65
- }
66
- }
67
- res.cork(() => {
68
- const key = req.getHeader("sec-websocket-key");
69
- const protocol = req.getHeader("sec-websocket-protocol");
70
- const extensions = req.getHeader("sec-websocket-extensions");
71
- res.upgrade(
72
- {
73
- req,
74
- res,
75
- webReq,
76
- protocol,
77
- extensions,
78
- context,
79
- namespace
80
- },
81
- key,
82
- "",
83
- extensions,
84
- uwsContext
85
- );
86
- });
87
- }
88
- }
89
- };
7
+ const hooks = new AdapterHookable(options);
8
+ const globalPeers = /* @__PURE__ */ new Map();
9
+ return {
10
+ ...adapterUtils(globalPeers),
11
+ websocket: {
12
+ ...options.uws,
13
+ close(ws, code, message) {
14
+ const peers = getPeers(globalPeers, ws.getUserData().namespace);
15
+ const peer = getPeer(ws, peers);
16
+ peer._internal.ws.readyState = 2;
17
+ peers.delete(peer);
18
+ hooks.callHook("close", peer, {
19
+ code,
20
+ reason: message?.toString()
21
+ });
22
+ peer._internal.ws.readyState = 3;
23
+ },
24
+ message(ws, message, isBinary) {
25
+ const peer = getPeer(ws, getPeers(globalPeers, ws.getUserData().namespace));
26
+ hooks.callHook("message", peer, new Message(message, peer));
27
+ },
28
+ open(ws) {
29
+ const peers = getPeers(globalPeers, ws.getUserData().namespace);
30
+ const peer = getPeer(ws, peers);
31
+ peers.add(peer);
32
+ hooks.callHook("open", peer);
33
+ },
34
+ async upgrade(res, req, uwsContext) {
35
+ let aborted = false;
36
+ res.onAborted(() => {
37
+ aborted = true;
38
+ });
39
+ const webReq = new UWSReqProxy(req);
40
+ const { upgradeHeaders, endResponse, context, namespace } = await hooks.upgrade(webReq);
41
+ if (endResponse) {
42
+ res.writeStatus(`${endResponse.status} ${endResponse.statusText}`);
43
+ for (const [key, value] of endResponse.headers) res.writeHeader(key, value);
44
+ if (endResponse.body) for await (const chunk of endResponse.body) {
45
+ if (aborted) break;
46
+ res.write(chunk);
47
+ }
48
+ if (!aborted) res.end();
49
+ return;
50
+ }
51
+ if (aborted) return;
52
+ res.writeStatus("101 Switching Protocols");
53
+ if (upgradeHeaders) {
54
+ const headers = upgradeHeaders instanceof Headers ? upgradeHeaders : new Headers(upgradeHeaders);
55
+ for (const [key, value] of headers) res.writeHeader(key, value);
56
+ }
57
+ res.cork(() => {
58
+ const key = req.getHeader("sec-websocket-key");
59
+ const protocol = req.getHeader("sec-websocket-protocol");
60
+ const extensions = req.getHeader("sec-websocket-extensions");
61
+ res.upgrade({
62
+ req,
63
+ res,
64
+ webReq,
65
+ protocol,
66
+ extensions,
67
+ context,
68
+ namespace
69
+ }, key, "", extensions, uwsContext);
70
+ });
71
+ }
72
+ }
73
+ };
90
74
  };
75
+ var uws_default = uwsAdapter;
91
76
  function getPeer(uws, peers) {
92
- const uwsData = uws.getUserData();
93
- if (uwsData.peer) {
94
- return uwsData.peer;
95
- }
96
- const peer = new UWSPeer({
97
- peers,
98
- uws,
99
- ws: new UwsWebSocketProxy(uws),
100
- request: uwsData.webReq,
101
- namespace: uwsData.namespace,
102
- uwsData
103
- });
104
- uwsData.peer = peer;
105
- return peer;
106
- }
107
- class UWSPeer extends Peer {
108
- get remoteAddress() {
109
- try {
110
- const addr = new TextDecoder().decode(
111
- this._internal.uws.getRemoteAddressAsText()
112
- );
113
- return addr;
114
- } catch {
115
- }
116
- }
117
- get context() {
118
- return this._internal.uwsData.context;
119
- }
120
- send(data, options) {
121
- const dataBuff = toBufferLike(data);
122
- const isBinary = typeof dataBuff !== "string";
123
- return this._internal.uws.send(dataBuff, isBinary, options?.compress);
124
- }
125
- subscribe(topic) {
126
- this._topics.add(topic);
127
- this._internal.uws.subscribe(topic);
128
- }
129
- unsubscribe(topic) {
130
- this._topics.delete(topic);
131
- this._internal.uws.unsubscribe(topic);
132
- }
133
- publish(topic, message, options) {
134
- const data = toBufferLike(message);
135
- const isBinary = typeof data !== "string";
136
- this._internal.uws.publish(topic, data, isBinary, options?.compress);
137
- return 0;
138
- }
139
- close(code, reason) {
140
- this._internal.uws.end(code, reason);
141
- }
142
- terminate() {
143
- this._internal.uws.close();
144
- }
145
- }
146
- class UWSReqProxy extends StubRequest {
147
- constructor(req) {
148
- const rawHeaders = [];
149
- let host = "localhost";
150
- let proto = "http";
151
- req.forEach((key, value) => {
152
- if (key === "host") {
153
- host = value;
154
- } else if (key === "x-forwarded-proto" && value === "https") {
155
- proto = "https";
156
- }
157
- rawHeaders.push([key, value]);
158
- });
159
- const query = req.getQuery();
160
- const pathname = req.getUrl();
161
- const url = `${proto}://${host}${pathname}${query ? `?${query}` : ""}`;
162
- super(url, { headers: rawHeaders });
163
- }
164
- }
165
- class UwsWebSocketProxy {
166
- constructor(_uws) {
167
- this._uws = _uws;
168
- }
169
- readyState = 1;
170
- get bufferedAmount() {
171
- return this._uws?.getBufferedAmount();
172
- }
173
- get protocol() {
174
- return this._uws?.getUserData().protocol;
175
- }
176
- get extensions() {
177
- return this._uws?.getUserData().extensions;
178
- }
77
+ const uwsData = uws.getUserData();
78
+ if (uwsData.peer) return uwsData.peer;
79
+ const peer = new UWSPeer({
80
+ peers,
81
+ uws,
82
+ ws: new UwsWebSocketProxy(uws),
83
+ request: uwsData.webReq,
84
+ namespace: uwsData.namespace,
85
+ uwsData
86
+ });
87
+ uwsData.peer = peer;
88
+ return peer;
179
89
  }
90
+ var UWSPeer = class extends Peer {
91
+ get remoteAddress() {
92
+ try {
93
+ return new TextDecoder().decode(this._internal.uws.getRemoteAddressAsText());
94
+ } catch {}
95
+ }
96
+ get context() {
97
+ return this._internal.uwsData.context;
98
+ }
99
+ send(data, options) {
100
+ const dataBuff = toBufferLike(data);
101
+ const isBinary = typeof dataBuff !== "string";
102
+ return this._internal.uws.send(dataBuff, isBinary, options?.compress);
103
+ }
104
+ subscribe(topic) {
105
+ this._topics.add(topic);
106
+ this._internal.uws.subscribe(topic);
107
+ }
108
+ unsubscribe(topic) {
109
+ this._topics.delete(topic);
110
+ this._internal.uws.unsubscribe(topic);
111
+ }
112
+ publish(topic, message, options) {
113
+ const data = toBufferLike(message);
114
+ const isBinary = typeof data !== "string";
115
+ this._internal.uws.publish(topic, data, isBinary, options?.compress);
116
+ return 0;
117
+ }
118
+ close(code, reason) {
119
+ this._internal.uws.end(code, reason);
120
+ }
121
+ terminate() {
122
+ this._internal.uws.close();
123
+ }
124
+ };
125
+ var UWSReqProxy = class extends StubRequest {
126
+ constructor(req) {
127
+ const rawHeaders = [];
128
+ let host = "localhost";
129
+ let proto = "http";
130
+ req.forEach((key, value) => {
131
+ if (key === "host") host = value;
132
+ else if (key === "x-forwarded-proto" && value === "https") proto = "https";
133
+ rawHeaders.push([key, value]);
134
+ });
135
+ const query = req.getQuery();
136
+ const pathname = req.getUrl();
137
+ const url = `${proto}://${host}${pathname}${query ? `?${query}` : ""}`;
138
+ super(url, { headers: rawHeaders });
139
+ }
140
+ };
141
+ var UwsWebSocketProxy = class {
142
+ readyState = 1;
143
+ constructor(_uws) {
144
+ this._uws = _uws;
145
+ }
146
+ get bufferedAmount() {
147
+ return this._uws?.getBufferedAmount();
148
+ }
149
+ get protocol() {
150
+ return this._uws?.getUserData().protocol;
151
+ }
152
+ get extensions() {
153
+ return this._uws?.getUserData().extensions;
154
+ }
155
+ };
180
156
 
181
- export { uwsAdapter as default };
157
+ //#endregion
158
+ export { uws_default as default };