crossws 0.4.4 → 0.4.6

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 (57) hide show
  1. package/adapters/bunny.d.ts +2 -0
  2. package/adapters/vercel.d.ts +2 -0
  3. package/dist/THIRD-PARTY-LICENSES.md +33 -0
  4. package/dist/_chunks/_request.mjs +1 -4
  5. package/dist/_chunks/_types.d.mts +9 -10
  6. package/dist/_chunks/adapter.d.mts +7 -15
  7. package/dist/_chunks/adapter.mjs +6 -7
  8. package/dist/_chunks/bun.d.mts +2 -5
  9. package/dist/_chunks/bunny.d.mts +22 -0
  10. package/dist/_chunks/cloudflare.d.mts +3 -6
  11. package/dist/_chunks/deno.d.mts +2 -5
  12. package/dist/_chunks/error.mjs +1 -4
  13. package/dist/_chunks/libs/ws.mjs +96 -1171
  14. package/dist/_chunks/node.d.mts +36 -7
  15. package/dist/_chunks/node.mjs +129 -0
  16. package/dist/_chunks/peer.mjs +2 -60
  17. package/dist/_chunks/rolldown-runtime.mjs +8 -16
  18. package/dist/_chunks/sse.d.mts +2 -5
  19. package/dist/_chunks/web.d.mts +1 -3
  20. package/dist/adapters/bun.d.mts +1 -1
  21. package/dist/adapters/bun.mjs +3 -8
  22. package/dist/adapters/bunny.d.mts +2 -0
  23. package/dist/adapters/bunny.mjs +68 -0
  24. package/dist/adapters/cloudflare.d.mts +1 -1
  25. package/dist/adapters/cloudflare.mjs +11 -15
  26. package/dist/adapters/deno.d.mts +1 -1
  27. package/dist/adapters/deno.mjs +4 -9
  28. package/dist/adapters/node.d.mts +2 -2
  29. package/dist/adapters/node.mjs +2 -125
  30. package/dist/adapters/sse.d.mts +1 -1
  31. package/dist/adapters/sse.mjs +3 -8
  32. package/dist/adapters/uws.d.mts +2 -7
  33. package/dist/adapters/uws.mjs +6 -10
  34. package/dist/adapters/vercel.d.mts +25 -0
  35. package/dist/adapters/vercel.mjs +48 -0
  36. package/dist/index.d.mts +97 -2
  37. package/dist/index.mjs +178 -3
  38. package/dist/server/bun.d.mts +1 -7
  39. package/dist/server/bun.mjs +3 -7
  40. package/dist/server/bunny.d.mts +5 -0
  41. package/dist/server/bunny.mjs +23 -0
  42. package/dist/server/cloudflare.d.mts +1 -7
  43. package/dist/server/cloudflare.mjs +3 -7
  44. package/dist/server/default.d.mts +1 -7
  45. package/dist/server/default.mjs +3 -7
  46. package/dist/server/deno.d.mts +1 -7
  47. package/dist/server/deno.mjs +3 -7
  48. package/dist/server/node.d.mts +1 -7
  49. package/dist/server/node.mjs +3 -9
  50. package/dist/websocket/native.d.mts +0 -2
  51. package/dist/websocket/native.mjs +1 -5
  52. package/dist/websocket/node.d.mts +0 -2
  53. package/dist/websocket/node.mjs +2 -8
  54. package/dist/websocket/sse.d.mts +1 -4
  55. package/dist/websocket/sse.mjs +1 -4
  56. package/package.json +45 -42
  57. 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,2 @@
1
+ export * from "../dist/adapters/vercel.mjs";
2
+ export { default } from "../dist/adapters/vercel.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 <einaros@gmail.com> (http://2x.io)
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,4 +1,3 @@
1
- //#region src/_request.ts
2
1
  const StubRequest = /* @__PURE__ */ (() => {
3
2
  class StubRequest {
4
3
  url;
@@ -78,6 +77,4 @@ const StubRequest = /* @__PURE__ */ (() => {
78
77
  Object.setPrototypeOf(StubRequest.prototype, globalThis.Request.prototype);
79
78
  return StubRequest;
80
79
  })();
81
-
82
- //#endregion
83
- export { StubRequest as t };
80
+ export { StubRequest };
@@ -1,16 +1,16 @@
1
- import { a as Hooks } from "./adapter.mjs";
2
- import { n as BunOptions } from "./bun.mjs";
3
- import { n as CloudflareOptions } from "./cloudflare.mjs";
4
- import { n as DenoOptions } from "./deno.mjs";
5
- import { n as NodeOptions } from "./node.mjs";
6
- import { n as SSEOptions } from "./sse.mjs";
1
+ import { Hooks } from "./adapter.mjs";
2
+ import { BunOptions } from "./bun.mjs";
3
+ import { BunnyOptions } from "./bunny.mjs";
4
+ import { CloudflareOptions } from "./cloudflare.mjs";
5
+ import { DenoOptions } from "./deno.mjs";
6
+ import { NodeOptions } from "./node.mjs";
7
+ import { 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
- export { WSOptions as n, ServerWithWSOptions as t };
23
+ export { ServerWithWSOptions, WSOptions };
@@ -1,14 +1,8 @@
1
- import { a as WebSocket } from "./web.mjs";
2
-
3
- //#region src/error.d.ts
1
+ import { WebSocket } from "./web.mjs";
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;
@@ -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. */
@@ -116,8 +108,6 @@ declare class Message implements Partial<MessageEvent> {
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;
@@ -131,6 +121,10 @@ interface Hooks {
131
121
  * - You can return { headers } to modify the response.
132
122
  * - You can return { namespace } to change the pub/sub namespace.
133
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.
134
128
  *
135
129
  * @param request
136
130
  * @throws {Response}
@@ -141,6 +135,7 @@ interface Hooks {
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
- 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 };
166
+ export { Adapter, AdapterInstance, AdapterInternal, AdapterOptions, Hooks, Message, Peer, PeerContext, ResolveHooks, WSError, defineHooks, defineWebSocketAdapter };
@@ -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 { AdapterHookable, adapterUtils, defineHooks, defineWebSocketAdapter, getPeers };
@@ -1,7 +1,5 @@
1
- import { d as PeerContext, n as AdapterInstance, r as AdapterOptions, t as Adapter, u as Peer } from "./adapter.mjs";
1
+ import { Adapter, AdapterInstance, AdapterOptions, Peer, PeerContext } 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
- export { BunOptions as n, bunAdapter as r, BunAdapter as t };
35
+ export { BunAdapter, BunOptions, bunAdapter };
@@ -0,0 +1,22 @@
1
+ import { Adapter, AdapterInstance, AdapterOptions } 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 { BunnyAdapter, BunnyOptions, bunnyAdapter };
@@ -1,9 +1,7 @@
1
- import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapter.mjs";
2
- import { a as WebSocket$1 } from "./web.mjs";
1
+ import { Adapter, AdapterInstance, AdapterOptions } from "./adapter.mjs";
2
+ import { WebSocket 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
  };
@@ -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
- export { CloudflareOptions as n, cloudflareAdapter as r, CloudflareDurableAdapter as t };
42
+ export { CloudflareDurableAdapter, CloudflareOptions, cloudflareAdapter };
@@ -1,6 +1,4 @@
1
- import { n as AdapterInstance, r as AdapterOptions, t as Adapter } from "./adapter.mjs";
2
-
3
- //#region src/adapters/deno.d.ts
1
+ import { Adapter, AdapterInstance, AdapterOptions } from "./adapter.mjs";
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
- export { DenoOptions as n, denoAdapter as r, DenoAdapter as t };
14
+ export { DenoAdapter, DenoOptions, denoAdapter };
@@ -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 };