crossws 0.4.3 → 0.4.5

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 (50) hide show
  1. package/adapters/bunny.d.ts +2 -0
  2. package/dist/THIRD-PARTY-LICENSES.md +33 -0
  3. package/dist/_chunks/_request.mjs +4 -6
  4. package/dist/_chunks/_types.d.mts +2 -3
  5. package/dist/_chunks/adapter.d.mts +45 -53
  6. package/dist/_chunks/adapter.mjs +6 -7
  7. package/dist/_chunks/bun.d.mts +0 -3
  8. package/dist/_chunks/bunny.d.mts +22 -0
  9. package/dist/_chunks/cloudflare.d.mts +16 -19
  10. package/dist/_chunks/deno.d.mts +0 -3
  11. package/dist/_chunks/error.mjs +1 -4
  12. package/dist/_chunks/libs/ws.mjs +77 -1179
  13. package/dist/_chunks/node.d.mts +43 -14
  14. package/dist/_chunks/node.mjs +129 -0
  15. package/dist/_chunks/peer.mjs +1 -59
  16. package/dist/_chunks/rolldown-runtime.mjs +7 -15
  17. package/dist/_chunks/sse.d.mts +0 -3
  18. package/dist/_chunks/web.d.mts +164 -166
  19. package/dist/adapters/bun.mjs +1 -6
  20. package/dist/adapters/bunny.d.mts +2 -0
  21. package/dist/adapters/bunny.mjs +68 -0
  22. package/dist/adapters/cloudflare.mjs +7 -12
  23. package/dist/adapters/deno.mjs +1 -6
  24. package/dist/adapters/node.d.mts +2 -2
  25. package/dist/adapters/node.mjs +2 -125
  26. package/dist/adapters/sse.mjs +1 -6
  27. package/dist/adapters/uws.d.mts +0 -5
  28. package/dist/adapters/uws.mjs +2 -7
  29. package/dist/index.d.mts +81 -1
  30. package/dist/index.mjs +161 -2
  31. package/dist/server/bun.d.mts +0 -6
  32. package/dist/server/bun.mjs +3 -7
  33. package/dist/server/bunny.d.mts +5 -0
  34. package/dist/server/bunny.mjs +23 -0
  35. package/dist/server/cloudflare.d.mts +0 -6
  36. package/dist/server/cloudflare.mjs +3 -7
  37. package/dist/server/default.d.mts +0 -6
  38. package/dist/server/default.mjs +3 -7
  39. package/dist/server/deno.d.mts +0 -6
  40. package/dist/server/deno.mjs +3 -7
  41. package/dist/server/node.d.mts +0 -6
  42. package/dist/server/node.mjs +3 -9
  43. package/dist/websocket/native.d.mts +0 -2
  44. package/dist/websocket/native.mjs +1 -5
  45. package/dist/websocket/node.d.mts +0 -2
  46. package/dist/websocket/node.mjs +1 -7
  47. package/dist/websocket/sse.d.mts +0 -3
  48. package/dist/websocket/sse.mjs +1 -4
  49. package/package.json +42 -40
  50. package/server/bunny.d.ts +2 -0
@@ -0,0 +1,2 @@
1
+ export * from "../dist/adapters/bunny.mjs";
2
+ export { default } from "../dist/adapters/bunny.mjs";
@@ -0,0 +1,33 @@
1
+ # Licenses of Bundled Dependencies
2
+
3
+ The published artifact additionally contains code with the following licenses:
4
+ MIT
5
+
6
+ # Bundled Dependencies
7
+
8
+ ## ws
9
+
10
+ License: MIT
11
+ By: Einar Otto Stangvik
12
+ Repository: https://github.com/websockets/ws
13
+
14
+ > Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
15
+ > Copyright (c) 2013 Arnout Kazemier and contributors
16
+ > Copyright (c) 2016 Luigi Pinca and contributors
17
+ >
18
+ > Permission is hereby granted, free of charge, to any person obtaining a copy of
19
+ > this software and associated documentation files (the "Software"), to deal in
20
+ > the Software without restriction, including without limitation the rights to
21
+ > use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
22
+ > the Software, and to permit persons to whom the Software is furnished to do so,
23
+ > subject to the following conditions:
24
+ >
25
+ > The above copyright notice and this permission notice shall be included in all
26
+ > copies or substantial portions of the Software.
27
+ >
28
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
30
+ > FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
31
+ > COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
32
+ > IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33
+ > CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,8 +1,7 @@
1
- //#region src/_request.ts
2
1
  const StubRequest = /* @__PURE__ */ (() => {
3
2
  class StubRequest {
4
3
  url;
5
- _signal;
4
+ _abortController;
6
5
  _headers;
7
6
  _init;
8
7
  constructor(url, init = {}) {
@@ -20,7 +19,8 @@ const StubRequest = /* @__PURE__ */ (() => {
20
19
  return "GET";
21
20
  }
22
21
  get signal() {
23
- return this._signal ??= new AbortSignal();
22
+ if (!this._abortController) this._abortController = new AbortController();
23
+ return this._abortController.signal;
24
24
  }
25
25
  get cache() {
26
26
  return "default";
@@ -77,6 +77,4 @@ const StubRequest = /* @__PURE__ */ (() => {
77
77
  Object.setPrototypeOf(StubRequest.prototype, globalThis.Request.prototype);
78
78
  return StubRequest;
79
79
  })();
80
-
81
- //#endregion
82
- export { StubRequest as t };
80
+ export { StubRequest as t };
@@ -1,16 +1,16 @@
1
1
  import { a as Hooks } from "./adapter.mjs";
2
2
  import { n as BunOptions } from "./bun.mjs";
3
+ import { n as BunnyOptions } from "./bunny.mjs";
3
4
  import { n as CloudflareOptions } from "./cloudflare.mjs";
4
5
  import { n as DenoOptions } from "./deno.mjs";
5
6
  import { n as NodeOptions } from "./node.mjs";
6
7
  import { n as SSEOptions } from "./sse.mjs";
7
8
  import { Server, ServerOptions, ServerPlugin, ServerRequest } from "srvx";
8
-
9
- //#region src/server/_types.d.ts
10
9
  type WSOptions = Partial<Hooks> & {
11
10
  resolve?: (req: ServerRequest) => Partial<Hooks> | Promise<Partial<Hooks>>;
12
11
  options?: {
13
12
  bun?: BunOptions;
13
+ bunny?: BunnyOptions;
14
14
  deno?: DenoOptions;
15
15
  node?: NodeOptions;
16
16
  sse?: SSEOptions;
@@ -20,5 +20,4 @@ type WSOptions = Partial<Hooks> & {
20
20
  type ServerWithWSOptions = ServerOptions & {
21
21
  websocket?: WSOptions;
22
22
  };
23
- //#endregion
24
23
  export { WSOptions as n, ServerWithWSOptions as t };
@@ -1,14 +1,8 @@
1
1
  import { a as WebSocket } from "./web.mjs";
2
-
3
- //#region src/error.d.ts
4
2
  declare class WSError extends Error {
5
3
  constructor(...args: any[]);
6
4
  }
7
- //#endregion
8
- //#region src/utils.d.ts
9
5
  declare const kNodeInspect: unique symbol;
10
- //#endregion
11
- //#region src/peer.d.ts
12
6
  interface PeerContext extends Record<string, unknown> {}
13
7
  interface AdapterInternal {
14
8
  ws: unknown;
@@ -26,21 +20,21 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
26
20
  get context(): PeerContext;
27
21
  get namespace(): string;
28
22
  /**
29
- * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
30
- */
23
+ * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
24
+ */
31
25
  get id(): string;
32
26
  /** IP address of the peer */
33
27
  get remoteAddress(): string | undefined;
34
28
  /** upgrade request */
35
29
  get request(): Request;
36
30
  /**
37
- * Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
38
- *
39
- * **Note:** crossws adds polyfill for the following properties if native values are not available:
40
- * - `protocol`: Extracted from the `sec-websocket-protocol` header.
41
- * - `extensions`: Extracted from the `sec-websocket-extensions` header.
42
- * - `url`: Extracted from the request URL (http -> ws).
43
- * */
31
+ * Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
32
+ *
33
+ * **Note:** crossws adds polyfill for the following properties if native values are not available:
34
+ * - `protocol`: Extracted from the `sec-websocket-protocol` header.
35
+ * - `extensions`: Extracted from the `sec-websocket-extensions` header.
36
+ * - `url`: Extracted from the request URL (http -> ws).
37
+ * */
44
38
  get websocket(): Partial<WebSocket>;
45
39
  /** All connected peers to the server */
46
40
  get peers(): Set<Peer>;
@@ -66,8 +60,6 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
66
60
  [Symbol.toStringTag](): "WebSocket";
67
61
  [kNodeInspect](): unknown;
68
62
  }
69
- //#endregion
70
- //#region src/message.d.ts
71
63
  declare class Message implements Partial<MessageEvent> {
72
64
  #private;
73
65
  /** Access to the original [message event](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event) if available. */
@@ -78,46 +70,44 @@ declare class Message implements Partial<MessageEvent> {
78
70
  readonly rawData: unknown;
79
71
  constructor(rawData: unknown, peer: Peer, event?: MessageEvent);
80
72
  /**
81
- * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the message.
82
- */
73
+ * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the message.
74
+ */
83
75
  get id(): string;
84
76
  /**
85
- * Get data as [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) value.
86
- *
87
- * If raw data is in any other format or string, it will be automatically converted and encoded.
88
- */
77
+ * Get data as [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) value.
78
+ *
79
+ * If raw data is in any other format or string, it will be automatically converted and encoded.
80
+ */
89
81
  uint8Array(): Uint8Array;
90
82
  /**
91
- * 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.
92
- *
93
- * If raw data is in any other format or string, it will be automatically converted and encoded.
94
- */
83
+ * 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.
84
+ *
85
+ * If raw data is in any other format or string, it will be automatically converted and encoded.
86
+ */
95
87
  arrayBuffer(): ArrayBuffer | SharedArrayBuffer;
96
88
  /**
97
- * Get data as [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) value.
98
- *
99
- * If raw data is in any other format or string, it will be automatically converted and encoded. */
89
+ * Get data as [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) value.
90
+ *
91
+ * If raw data is in any other format or string, it will be automatically converted and encoded. */
100
92
  blob(): Blob;
101
93
  /**
102
- * Get stringified text version of the message.
103
- *
104
- * If raw data is in any other format, it will be automatically converted and decoded.
105
- */
94
+ * Get stringified text version of the message.
95
+ *
96
+ * If raw data is in any other format, it will be automatically converted and decoded.
97
+ */
106
98
  text(): string;
107
99
  /**
108
- * Get parsed version of the message text with [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
109
- */
100
+ * Get parsed version of the message text with [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
101
+ */
110
102
  json<T = unknown>(): T;
111
103
  /**
112
- * Message data (value varies based on `peer.websocket.binaryType`).
113
- */
104
+ * Message data (value varies based on `peer.websocket.binaryType`).
105
+ */
114
106
  get data(): unknown;
115
107
  toString(): string;
116
108
  [Symbol.toPrimitive](): string;
117
109
  [kNodeInspect](): unknown;
118
110
  }
119
- //#endregion
120
- //#region src/hooks.d.ts
121
111
  declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
122
112
  type ResolveHooks = (request: Request & {
123
113
  readonly context?: PeerContext;
@@ -125,22 +115,27 @@ type ResolveHooks = (request: Request & {
125
115
  type MaybePromise<T> = T | Promise<T>;
126
116
  interface Hooks {
127
117
  /**
128
- * Upgrading a request to a WebSocket connection.
129
- *
130
- * - You can throw a Response to abort the upgrade.
131
- * - You can return { headers } to modify the response.
132
- * - You can return { namespace } to change the pub/sub namespace.
133
- * - You can return { context } to provide a custom peer context.
134
- *
135
- * @param request
136
- * @throws {Response}
137
- */
118
+ * Upgrading a request to a WebSocket connection.
119
+ *
120
+ * - You can throw a Response to abort the upgrade.
121
+ * - You can return { headers } to modify the response.
122
+ * - You can return { namespace } to change the pub/sub namespace.
123
+ * - You can return { context } to provide a custom peer context.
124
+ * - You can return { handled: true } to signal that the upgrade has
125
+ * already been performed by the hook (e.g. delegated to an external
126
+ * node-style `(req, socket, head)` handler). The adapter will then
127
+ * leave the socket alone and skip its own upgrade.
128
+ *
129
+ * @param request
130
+ * @throws {Response}
131
+ */
138
132
  upgrade: (request: Request & {
139
133
  readonly context?: Record<string, unknown>;
140
134
  }) => MaybePromise<{
141
135
  headers?: HeadersInit;
142
136
  namespace?: string;
143
137
  context?: PeerContext;
138
+ handled?: boolean;
144
139
  } | Response | void>;
145
140
  /** A message is received */
146
141
  message: (peer: Peer, message: Message) => MaybePromise<void>;
@@ -154,8 +149,6 @@ interface Hooks {
154
149
  /** An error occurs */
155
150
  error: (peer: Peer, error: WSError) => MaybePromise<void>;
156
151
  }
157
- //#endregion
158
- //#region src/adapter.d.ts
159
152
  interface AdapterInstance {
160
153
  readonly peers: Map<string, Set<Peer>>;
161
154
  readonly publish: (topic: string, data: unknown, options?: {
@@ -170,5 +163,4 @@ interface AdapterOptions {
170
163
  }
171
164
  type Adapter<AdapterT extends AdapterInstance = AdapterInstance, Options extends AdapterOptions = AdapterOptions> = (options?: Options) => AdapterT;
172
165
  declare function defineWebSocketAdapter<AdapterT extends AdapterInstance = AdapterInstance, Options extends AdapterOptions = AdapterOptions>(factory: Adapter<AdapterT, Options>): Adapter<AdapterT, Options>;
173
- //#endregion
174
166
  export { Hooks as a, Message as c, PeerContext as d, WSError as f, defineWebSocketAdapter as i, AdapterInternal as l, AdapterInstance as n, ResolveHooks as o, AdapterOptions as r, defineHooks as s, Adapter as t, Peer as u };
@@ -1,4 +1,3 @@
1
- //#region src/hooks.ts
2
1
  var AdapterHookable = class {
3
2
  options;
4
3
  constructor(options) {
@@ -32,6 +31,11 @@ var AdapterHookable = class {
32
31
  namespace,
33
32
  endResponse: res
34
33
  };
34
+ if (res.handled) return {
35
+ context,
36
+ namespace,
37
+ handled: true
38
+ };
35
39
  if (res.headers) return {
36
40
  context,
37
41
  namespace,
@@ -55,9 +59,6 @@ var AdapterHookable = class {
55
59
  function defineHooks(hooks) {
56
60
  return hooks;
57
61
  }
58
-
59
- //#endregion
60
- //#region src/adapter.ts
61
62
  function adapterUtils(globalPeers) {
62
63
  return {
63
64
  peers: globalPeers,
@@ -88,6 +89,4 @@ function getPeers(globalPeers, namespace) {
88
89
  function defineWebSocketAdapter(factory) {
89
90
  return factory;
90
91
  }
91
-
92
- //#endregion
93
- export { defineHooks as a, AdapterHookable as i, defineWebSocketAdapter as n, getPeers as r, adapterUtils as t };
92
+ export { defineHooks as a, AdapterHookable as i, defineWebSocketAdapter as n, getPeers as r, adapterUtils as t };
@@ -1,7 +1,5 @@
1
1
  import { d as PeerContext, n as AdapterInstance, r as AdapterOptions, t as Adapter, u as Peer } from "./adapter.mjs";
2
2
  import { Server, ServerWebSocket, WebSocketHandler } from "bun";
3
-
4
- //#region src/adapters/bun.d.ts
5
3
  interface BunAdapter extends AdapterInstance {
6
4
  websocket: WebSocketHandler<ContextData>;
7
5
  handleUpgrade(req: Request, server: Server<ContextData>): Promise<Response | undefined>;
@@ -34,5 +32,4 @@ declare class BunPeer extends Peer<{
34
32
  close(code?: number, reason?: string): void;
35
33
  terminate(): void;
36
34
  }
37
- //#endregion
38
35
  export { BunOptions as n, bunAdapter as r, BunAdapter as t };
@@ -0,0 +1,22 @@
1
+ import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapter.mjs";
2
+ interface BunnyAdapter extends AdapterInstance {
3
+ handleUpgrade(req: Request): Promise<Response>;
4
+ }
5
+ interface BunnyOptions extends AdapterOptions {
6
+ /**
7
+ * The WebSocket subprotocol to use for the connection.
8
+ */
9
+ protocol?: string;
10
+ /**
11
+ * The number of seconds to wait for a pong response before closing the connection.
12
+ * If the client does not respond within this timeout, the connection is deemed
13
+ * unhealthy and closed, emitting the close and error events.
14
+ * If no data is transmitted from the client for 2 minutes, the connection
15
+ * will be closed regardless of this configuration.
16
+ *
17
+ * @default 30
18
+ */
19
+ idleTimeout?: number;
20
+ }
21
+ declare const bunnyAdapter: Adapter<BunnyAdapter, BunnyOptions>;
22
+ export { BunnyOptions as n, bunnyAdapter as r, BunnyAdapter as t };
@@ -2,34 +2,32 @@ import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapt
2
2
  import { a as WebSocket$1 } from "./web.mjs";
3
3
  import { DurableObject } from "cloudflare:workers";
4
4
  import * as CF from "@cloudflare/workers-types";
5
-
6
- //#region src/adapters/cloudflare.d.ts
7
5
  type WSDurableObjectStub = CF.DurableObjectStub & {
8
6
  webSocketPublish?: (topic: string, data: unknown, opts: any) => Promise<void>;
9
7
  };
10
8
  type ResolveDurableStub = (req: CF.Request | undefined, env: unknown, context: CF.ExecutionContext | undefined) => WSDurableObjectStub | undefined | Promise<WSDurableObjectStub | undefined>;
11
9
  interface CloudflareOptions extends AdapterOptions {
12
10
  /**
13
- * Durable Object binding name from environment.
14
- *
15
- * **Note:** This option will be ignored if `resolveDurableStub` is provided.
16
- *
17
- * @default "$DurableObject"
18
- */
11
+ * Durable Object binding name from environment.
12
+ *
13
+ * **Note:** This option will be ignored if `resolveDurableStub` is provided.
14
+ *
15
+ * @default "$DurableObject"
16
+ */
19
17
  bindingName?: string;
20
18
  /**
21
- * Durable Object instance name.
22
- *
23
- * **Note:** This option will be ignored if `resolveDurableStub` is provided.
24
- *
25
- * @default "crossws"
26
- */
19
+ * Durable Object instance name.
20
+ *
21
+ * **Note:** This option will be ignored if `resolveDurableStub` is provided.
22
+ *
23
+ * @default "crossws"
24
+ */
27
25
  instanceName?: string;
28
26
  /**
29
- * Custom function that resolves Durable Object binding to handle the WebSocket upgrade.
30
- *
31
- * **Note:** This option will override `bindingName` and `instanceName`.
32
- */
27
+ * Custom function that resolves Durable Object binding to handle the WebSocket upgrade.
28
+ *
29
+ * **Note:** This option will override `bindingName` and `instanceName`.
30
+ */
33
31
  resolveDurableStub?: ResolveDurableStub;
34
32
  }
35
33
  declare const cloudflareAdapter: Adapter<CloudflareDurableAdapter, CloudflareOptions>;
@@ -41,5 +39,4 @@ interface CloudflareDurableAdapter extends AdapterInstance {
41
39
  handleDurablePublish: (obj: DurableObject, topic: string, data: unknown, opts: any) => Promise<void>;
42
40
  handleDurableClose(obj: DurableObject, ws: WebSocket | CF.WebSocket | WebSocket$1, code: number, reason: string, wasClean: boolean): Promise<void>;
43
41
  }
44
- //#endregion
45
42
  export { CloudflareOptions as n, cloudflareAdapter as r, CloudflareDurableAdapter as t };
@@ -1,6 +1,4 @@
1
1
  import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapter.mjs";
2
-
3
- //#region src/adapters/deno.d.ts
4
2
  interface DenoAdapter extends AdapterInstance {
5
3
  handleUpgrade(req: Request, info: ServeHandlerInfo): Promise<Response>;
6
4
  }
@@ -13,5 +11,4 @@ type ServeHandlerInfo = {
13
11
  };
14
12
  };
15
13
  declare const denoAdapter: Adapter<DenoAdapter, DenoOptions>;
16
- //#endregion
17
14
  export { DenoOptions as n, denoAdapter as r, DenoAdapter as t };
@@ -1,10 +1,7 @@
1
- //#region src/error.ts
2
1
  var WSError = class extends Error {
3
2
  constructor(...args) {
4
3
  super(...args);
5
4
  this.name = "WSError";
6
5
  }
7
6
  };
8
-
9
- //#endregion
10
- export { WSError as t };
7
+ export { WSError as t };