crossws 0.4.1 → 0.4.3

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 (53) hide show
  1. package/dist/_chunks/_request.mjs +82 -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 -218
  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 -123
  45. package/package.json +36 -28
  46. package/adapters/cloudflare-durable.d.ts +0 -2
  47. package/dist/shared/crossws.95-eYp2D.d.mts +0 -23
  48. package/dist/shared/crossws.B31KJMcF.mjs +0 -83
  49. package/dist/shared/crossws.BQXMA5bH.d.mts +0 -297
  50. package/dist/shared/crossws.By9qWDAI.mjs +0 -8
  51. package/dist/shared/crossws.CPlNx7g8.mjs +0 -105
  52. package/dist/shared/crossws.CipVM6lf.mjs +0 -4973
  53. package/dist/shared/crossws.WpyOHUXc.mjs +0 -330
@@ -0,0 +1,298 @@
1
+ //#region types/web.d.ts
2
+ /**
3
+ * A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute.
4
+ *
5
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent)
6
+ */
7
+ interface CloseEvent extends Event {
8
+ /**
9
+ * Returns the WebSocket connection close code provided by the server.
10
+ *
11
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/code)
12
+ */
13
+ readonly code: number;
14
+ /**
15
+ * Returns the WebSocket connection close reason provided by the server.
16
+ *
17
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/reason)
18
+ */
19
+ readonly reason: string;
20
+ /**
21
+ * Returns true if the connection closed cleanly; false otherwise.
22
+ *
23
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/wasClean)
24
+ */
25
+ readonly wasClean: boolean;
26
+ }
27
+ /**
28
+ * An event which takes place in the DOM.
29
+ *
30
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event)
31
+ */
32
+ interface Event {
33
+ /**
34
+ * Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise.
35
+ *
36
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/bubbles)
37
+ */
38
+ readonly bubbles: boolean;
39
+ /**
40
+ * @deprecated
41
+ *
42
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelBubble)
43
+ */
44
+ cancelBubble: boolean;
45
+ /**
46
+ * Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method.
47
+ *
48
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelable)
49
+ */
50
+ readonly cancelable: boolean;
51
+ /**
52
+ * Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise.
53
+ *
54
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composed)
55
+ */
56
+ readonly composed: boolean;
57
+ /**
58
+ * Returns the object whose event listener's callback is currently being invoked.
59
+ *
60
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/currentTarget)
61
+ */
62
+ readonly currentTarget: EventTarget | null;
63
+ /**
64
+ * Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise.
65
+ *
66
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/defaultPrevented)
67
+ */
68
+ readonly defaultPrevented: boolean;
69
+ /**
70
+ * Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE.
71
+ *
72
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/eventPhase)
73
+ */
74
+ readonly eventPhase: number;
75
+ /**
76
+ * Returns true if event was dispatched by the user agent, and false otherwise.
77
+ *
78
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/isTrusted)
79
+ */
80
+ readonly isTrusted: boolean;
81
+ /**
82
+ * @deprecated
83
+ *
84
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/returnValue)
85
+ */
86
+ returnValue: boolean;
87
+ /**
88
+ * @deprecated
89
+ *
90
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/srcElement)
91
+ */
92
+ readonly srcElement: EventTarget | null;
93
+ /**
94
+ * Returns the object to which event is dispatched (its target).
95
+ *
96
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/target)
97
+ */
98
+ readonly target: EventTarget | null;
99
+ /**
100
+ * Returns the event's timestamp as the number of milliseconds measured relative to the time origin.
101
+ *
102
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/timeStamp)
103
+ */
104
+ readonly timeStamp: DOMHighResTimeStamp;
105
+ /**
106
+ * Returns the type of event, e.g. "click", "hashchange", or "submit".
107
+ *
108
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/type)
109
+ */
110
+ readonly type: string;
111
+ /**
112
+ * Returns the invocation target objects of event's path (objects on which listeners will be invoked), except for any nodes in shadow trees of which the shadow root's mode is "closed" that are not reachable from event's currentTarget.
113
+ *
114
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composedPath)
115
+ */
116
+ composedPath(): EventTarget[];
117
+ /**
118
+ * @deprecated
119
+ *
120
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/initEvent)
121
+ */
122
+ initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
123
+ /**
124
+ * If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false, signals to the operation that caused event to be dispatched that it needs to be canceled.
125
+ *
126
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/preventDefault)
127
+ */
128
+ preventDefault(): void;
129
+ /**
130
+ * Invoking this method prevents event from reaching any registered event listeners after the current one finishes running and, when dispatched in a tree, also prevents event from reaching any other objects.
131
+ *
132
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopImmediatePropagation)
133
+ */
134
+ stopImmediatePropagation(): void;
135
+ /**
136
+ * When dispatched in a tree, invoking this method prevents event from reaching any objects other than the current object.
137
+ *
138
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopPropagation)
139
+ */
140
+ stopPropagation(): void;
141
+ readonly NONE: 0;
142
+ readonly CAPTURING_PHASE: 1;
143
+ readonly AT_TARGET: 2;
144
+ readonly BUBBLING_PHASE: 3;
145
+ }
146
+ /**
147
+ * EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.
148
+ *
149
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
150
+ */
151
+ interface EventTarget {
152
+ /**
153
+ * Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
154
+ *
155
+ * The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
156
+ *
157
+ * When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
158
+ *
159
+ * When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
160
+ *
161
+ * When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
162
+ *
163
+ * If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
164
+ *
165
+ * The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
166
+ *
167
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
168
+ */
169
+ addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
170
+ /**
171
+ * Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
172
+ *
173
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent)
174
+ */
175
+ dispatchEvent(event: Event): boolean;
176
+ /**
177
+ * Removes the event listener in target's event listener list with the same type, callback, and options.
178
+ *
179
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
180
+ */
181
+ removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
182
+ }
183
+ /**
184
+ * A message received by a target object.
185
+ *
186
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent)
187
+ */
188
+ interface MessageEvent<T = any> extends Event {
189
+ /**
190
+ * Returns the data of the message.
191
+ *
192
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/data)
193
+ */
194
+ readonly data: T;
195
+ /**
196
+ * Returns the last event ID string, for server-sent events.
197
+ *
198
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/lastEventId)
199
+ */
200
+ readonly lastEventId: string;
201
+ /**
202
+ * Returns the origin of the message, for server-sent events and cross-document messaging.
203
+ *
204
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/origin)
205
+ */
206
+ readonly origin: string;
207
+ /**
208
+ * Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging.
209
+ *
210
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/ports)
211
+ */
212
+ readonly ports: ReadonlyArray<MessagePort>;
213
+ /**
214
+ * Returns the WindowProxy of the source window, for cross-document messaging, and the MessagePort being attached, in the connect event fired at SharedWorkerGlobalScope objects.
215
+ *
216
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/source)
217
+ */
218
+ readonly source: MessageEventSource | null;
219
+ /** @deprecated */
220
+ initMessageEvent(type: string, bubbles?: boolean, cancelable?: boolean, data?: any, origin?: string, lastEventId?: string, source?: MessageEventSource | null, ports?: MessagePort[]): void;
221
+ }
222
+ /**
223
+ * Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.
224
+ *
225
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket)
226
+ */
227
+ interface WebSocket extends EventTarget {
228
+ /**
229
+ * Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:
230
+ *
231
+ * Can be set, to change how binary data is returned. The default is "blob".
232
+ *
233
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/binaryType)
234
+ */
235
+ binaryType: BinaryType | (string & {});
236
+ /**
237
+ * Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.
238
+ *
239
+ * If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.)
240
+ *
241
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/bufferedAmount)
242
+ */
243
+ readonly bufferedAmount: number;
244
+ /**
245
+ * Returns the extensions selected by the server, if any.
246
+ *
247
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/extensions)
248
+ */
249
+ readonly extensions: string;
250
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/close_event) */
251
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
252
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/error_event) */
253
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
254
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/message_event) */
255
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
256
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/open_event) */
257
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
258
+ /**
259
+ * Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation.
260
+ *
261
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/protocol)
262
+ */
263
+ readonly protocol: string;
264
+ /**
265
+ * Returns the state of the WebSocket object's connection. It can have the values described below.
266
+ *
267
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/readyState)
268
+ */
269
+ readonly readyState: number;
270
+ /**
271
+ * Returns the URL that was used to establish the WebSocket connection.
272
+ *
273
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/url)
274
+ */
275
+ readonly url: string;
276
+ /**
277
+ * Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.
278
+ *
279
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/close)
280
+ */
281
+ close(code?: number, reason?: string): void;
282
+ /**
283
+ * Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.
284
+ *
285
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/send)
286
+ */
287
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
288
+ readonly CONNECTING: 0;
289
+ readonly OPEN: 1;
290
+ readonly CLOSING: 2;
291
+ readonly CLOSED: 3;
292
+ addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
293
+ addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
294
+ removeEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
295
+ removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
296
+ }
297
+ //#endregion
298
+ export { WebSocket as a, MessageEvent as i, Event as n, EventTarget as r, CloseEvent as t };
@@ -1,41 +1,2 @@
1
- import { WebSocketHandler, ServerWebSocket, Server } from 'bun';
2
- import { Adapter, AdapterInstance, Peer, PeerContext, AdapterOptions } from '../index.mjs';
3
- import '../shared/crossws.BQXMA5bH.mjs';
4
-
5
- interface BunAdapter extends AdapterInstance {
6
- websocket: WebSocketHandler<ContextData>;
7
- handleUpgrade(req: Request, server: Server): Promise<Response | undefined>;
8
- }
9
- interface BunOptions extends AdapterOptions {
10
- }
11
- type ContextData = {
12
- peer?: BunPeer;
13
- namespace: string;
14
- request: Request;
15
- server?: Server;
16
- context: PeerContext;
17
- };
18
- declare const bunAdapter: Adapter<BunAdapter, BunOptions>;
19
-
20
- declare class BunPeer extends Peer<{
21
- ws: ServerWebSocket<ContextData>;
22
- namespace: string;
23
- request: Request;
24
- peers: Set<BunPeer>;
25
- }> {
26
- get remoteAddress(): string;
27
- get context(): PeerContext;
28
- send(data: unknown, options?: {
29
- compress?: boolean;
30
- }): number;
31
- publish(topic: string, data: unknown, options?: {
32
- compress?: boolean;
33
- }): number;
34
- subscribe(topic: string): void;
35
- unsubscribe(topic: string): void;
36
- close(code?: number, reason?: string): void;
37
- terminate(): void;
38
- }
39
-
40
- export { bunAdapter as default };
41
- export type { BunAdapter, BunOptions };
1
+ import { n as BunOptions, r as bunAdapter, t as BunAdapter } from "../_chunks/bun.mjs";
2
+ export { BunAdapter, BunOptions, bunAdapter as default };
@@ -1,99 +1,89 @@
1
- import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.WpyOHUXc.mjs';
2
- import { a as adapterUtils, g as getPeers, A as AdapterHookable } from '../shared/crossws.CPlNx7g8.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
3
 
4
+ //#region src/adapters/bun.ts
4
5
  const bunAdapter = (options = {}) => {
5
- if (typeof Bun === "undefined") {
6
- throw new Error(
7
- "[crossws] Using Bun adapter in an incompatible environment."
8
- );
9
- }
10
- const hooks = new AdapterHookable(options);
11
- const globalPeers = /* @__PURE__ */ new Map();
12
- return {
13
- ...adapterUtils(globalPeers),
14
- async handleUpgrade(request, server) {
15
- const { upgradeHeaders, endResponse, context, namespace } = await hooks.upgrade(request);
16
- if (endResponse) {
17
- return endResponse;
18
- }
19
- const upgradeOK = server.upgrade(request, {
20
- data: {
21
- server,
22
- request,
23
- context,
24
- namespace
25
- },
26
- headers: upgradeHeaders
27
- });
28
- if (!upgradeOK) {
29
- return new Response("Upgrade failed", { status: 500 });
30
- }
31
- },
32
- websocket: {
33
- message: (ws, message) => {
34
- const peers = getPeers(globalPeers, ws.data.namespace);
35
- const peer = getPeer(ws, peers);
36
- hooks.callHook("message", peer, new Message(message, peer));
37
- },
38
- open: (ws) => {
39
- const peers = getPeers(globalPeers, ws.data.namespace);
40
- const peer = getPeer(ws, peers);
41
- peers.add(peer);
42
- hooks.callHook("open", peer);
43
- },
44
- close: (ws, code, reason) => {
45
- const peers = getPeers(globalPeers, ws.data.namespace);
46
- const peer = getPeer(ws, peers);
47
- peers.delete(peer);
48
- hooks.callHook("close", peer, { code, reason });
49
- }
50
- }
51
- };
6
+ if (typeof Bun === "undefined") throw new Error("[crossws] Using Bun adapter in an incompatible environment.");
7
+ const hooks = new AdapterHookable(options);
8
+ const globalPeers = /* @__PURE__ */ new Map();
9
+ return {
10
+ ...adapterUtils(globalPeers),
11
+ async handleUpgrade(request, server) {
12
+ const { upgradeHeaders, endResponse, context, namespace } = await hooks.upgrade(request);
13
+ if (endResponse) return endResponse;
14
+ if (!server.upgrade(request, {
15
+ data: {
16
+ server,
17
+ request,
18
+ context,
19
+ namespace
20
+ },
21
+ headers: upgradeHeaders
22
+ })) return new Response("Upgrade failed", { status: 500 });
23
+ },
24
+ websocket: {
25
+ message: (ws, message) => {
26
+ const peer = getPeer(ws, getPeers(globalPeers, ws.data.namespace));
27
+ hooks.callHook("message", peer, new Message(message, peer));
28
+ },
29
+ open: (ws) => {
30
+ const peers = getPeers(globalPeers, ws.data.namespace);
31
+ const peer = getPeer(ws, peers);
32
+ peers.add(peer);
33
+ hooks.callHook("open", peer);
34
+ },
35
+ close: (ws, code, reason) => {
36
+ const peers = getPeers(globalPeers, ws.data.namespace);
37
+ const peer = getPeer(ws, peers);
38
+ peers.delete(peer);
39
+ hooks.callHook("close", peer, {
40
+ code,
41
+ reason
42
+ });
43
+ }
44
+ }
45
+ };
52
46
  };
47
+ var bun_default = bunAdapter;
53
48
  function getPeer(ws, peers) {
54
- if (ws.data.peer) {
55
- return ws.data.peer;
56
- }
57
- const peer = new BunPeer({
58
- ws,
59
- request: ws.data.request,
60
- peers,
61
- namespace: ws.data.namespace
62
- });
63
- ws.data.peer = peer;
64
- return peer;
65
- }
66
- class BunPeer extends Peer {
67
- get remoteAddress() {
68
- return this._internal.ws.remoteAddress;
69
- }
70
- get context() {
71
- return this._internal.ws.data.context;
72
- }
73
- send(data, options) {
74
- return this._internal.ws.send(toBufferLike(data), options?.compress);
75
- }
76
- publish(topic, data, options) {
77
- return this._internal.ws.publish(
78
- topic,
79
- toBufferLike(data),
80
- options?.compress
81
- );
82
- }
83
- subscribe(topic) {
84
- this._topics.add(topic);
85
- this._internal.ws.subscribe(topic);
86
- }
87
- unsubscribe(topic) {
88
- this._topics.delete(topic);
89
- this._internal.ws.unsubscribe(topic);
90
- }
91
- close(code, reason) {
92
- this._internal.ws.close(code, reason);
93
- }
94
- terminate() {
95
- this._internal.ws.terminate();
96
- }
49
+ if (ws.data.peer) return ws.data.peer;
50
+ const peer = new BunPeer({
51
+ ws,
52
+ request: ws.data.request,
53
+ peers,
54
+ namespace: ws.data.namespace
55
+ });
56
+ ws.data.peer = peer;
57
+ return peer;
97
58
  }
59
+ var BunPeer = class extends Peer {
60
+ get remoteAddress() {
61
+ return this._internal.ws.remoteAddress;
62
+ }
63
+ get context() {
64
+ return this._internal.ws.data.context;
65
+ }
66
+ send(data, options) {
67
+ return this._internal.ws.send(toBufferLike(data), options?.compress);
68
+ }
69
+ publish(topic, data, options) {
70
+ return this._internal.ws.publish(topic, toBufferLike(data), options?.compress);
71
+ }
72
+ subscribe(topic) {
73
+ this._topics.add(topic);
74
+ this._internal.ws.subscribe(topic);
75
+ }
76
+ unsubscribe(topic) {
77
+ this._topics.delete(topic);
78
+ this._internal.ws.unsubscribe(topic);
79
+ }
80
+ close(code, reason) {
81
+ this._internal.ws.close(code, reason);
82
+ }
83
+ terminate() {
84
+ this._internal.ws.terminate();
85
+ }
86
+ };
98
87
 
99
- export { bunAdapter as default };
88
+ //#endregion
89
+ export { bun_default as default };
@@ -1,46 +1,2 @@
1
- import * as CF from '@cloudflare/workers-types';
2
- import { DurableObject } from 'cloudflare:workers';
3
- import { Adapter, AdapterInstance, AdapterOptions } from '../index.mjs';
4
- import { W as WebSocket$1 } from '../shared/crossws.BQXMA5bH.mjs';
5
-
6
- type WSDurableObjectStub = CF.DurableObjectStub & {
7
- webSocketPublish?: (topic: string, data: unknown, opts: any) => Promise<void>;
8
- };
9
- type ResolveDurableStub = (req: CF.Request | undefined, env: unknown, context: CF.ExecutionContext | undefined) => WSDurableObjectStub | undefined | Promise<WSDurableObjectStub | undefined>;
10
- interface CloudflareOptions extends AdapterOptions {
11
- /**
12
- * Durable Object binding name from environment.
13
- *
14
- * **Note:** This option will be ignored if `resolveDurableStub` is provided.
15
- *
16
- * @default "$DurableObject"
17
- */
18
- bindingName?: string;
19
- /**
20
- * Durable Object instance name.
21
- *
22
- * **Note:** This option will be ignored if `resolveDurableStub` is provided.
23
- *
24
- * @default "crossws"
25
- */
26
- instanceName?: string;
27
- /**
28
- * Custom function that resolves Durable Object binding to handle the WebSocket upgrade.
29
- *
30
- * **Note:** This option will override `bindingName` and `instanceName`.
31
- */
32
- resolveDurableStub?: ResolveDurableStub;
33
- }
34
- declare const cloudflareAdapter: Adapter<CloudflareDurableAdapter, CloudflareOptions>;
35
-
36
- interface CloudflareDurableAdapter extends AdapterInstance {
37
- handleUpgrade(req: Request | CF.Request, env: unknown, context: CF.ExecutionContext): Promise<Response>;
38
- handleDurableInit(obj: DurableObject, state: DurableObjectState, env: unknown): void;
39
- handleDurableUpgrade(obj: DurableObject, req: Request | CF.Request): Promise<Response>;
40
- handleDurableMessage(obj: DurableObject, ws: WebSocket | CF.WebSocket | WebSocket$1, message: ArrayBuffer | string): Promise<void>;
41
- handleDurablePublish: (obj: DurableObject, topic: string, data: unknown, opts: any) => Promise<void>;
42
- handleDurableClose(obj: DurableObject, ws: WebSocket | CF.WebSocket | WebSocket$1, code: number, reason: string, wasClean: boolean): Promise<void>;
43
- }
44
-
45
- export { cloudflareAdapter as default };
46
- export type { CloudflareDurableAdapter, CloudflareOptions };
1
+ import { n as CloudflareOptions, r as cloudflareAdapter, t as CloudflareDurableAdapter } from "../_chunks/cloudflare.mjs";
2
+ export { CloudflareDurableAdapter, CloudflareOptions, cloudflareAdapter as default };