crossws 0.3.0 → 0.3.2

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.
@@ -1,4 +1,4 @@
1
- import { AdapterInstance, AdapterOptions, Adapter, Peer } from '../index.mjs';
1
+ import { AdapterInstance, AdapterOptions, Peer, Adapter } from '../index.mjs';
2
2
  import { WebSocketHandler, Server, ServerWebSocket } from 'bun';
3
3
  import '../shared/crossws.ChIJSJVK.mjs';
4
4
 
@@ -12,6 +12,7 @@ type ContextData = {
12
12
  peer?: BunPeer;
13
13
  request: Request;
14
14
  server?: Server;
15
+ context: Peer["context"];
15
16
  };
16
17
  declare const _default: Adapter<BunAdapter, BunOptions>;
17
18
 
@@ -21,6 +22,7 @@ declare class BunPeer extends Peer<{
21
22
  peers: Set<BunPeer>;
22
23
  }> {
23
24
  get remoteAddress(): string;
25
+ get context(): Record<string, unknown>;
24
26
  send(data: unknown, options?: {
25
27
  compress?: boolean;
26
28
  }): number;
@@ -1,4 +1,4 @@
1
- import { AdapterInstance, AdapterOptions, Adapter, Peer } from '../index.js';
1
+ import { AdapterInstance, AdapterOptions, Peer, Adapter } from '../index.js';
2
2
  import { WebSocketHandler, Server, ServerWebSocket } from 'bun';
3
3
  import '../shared/crossws.ChIJSJVK.js';
4
4
 
@@ -12,6 +12,7 @@ type ContextData = {
12
12
  peer?: BunPeer;
13
13
  request: Request;
14
14
  server?: Server;
15
+ context: Peer["context"];
15
16
  };
16
17
  declare const _default: Adapter<BunAdapter, BunOptions>;
17
18
 
@@ -21,6 +22,7 @@ declare class BunPeer extends Peer<{
21
22
  peers: Set<BunPeer>;
22
23
  }> {
23
24
  get remoteAddress(): string;
25
+ get context(): Record<string, unknown>;
24
26
  send(data: unknown, options?: {
25
27
  compress?: boolean;
26
28
  }): number;
@@ -1,5 +1,5 @@
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';
1
+ import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.DLRVRjZs.mjs';
2
+ import { d as defineWebSocketAdapter, a as adapterUtils, A as AdapterHookable } from '../shared/crossws.BcEs6ZHK.mjs';
3
3
  import 'uncrypto';
4
4
 
5
5
  const bun = defineWebSocketAdapter(
@@ -9,16 +9,17 @@ const bun = defineWebSocketAdapter(
9
9
  return {
10
10
  ...adapterUtils(peers),
11
11
  async handleUpgrade(request, server) {
12
- const res = await hooks.callHook("upgrade", request);
13
- if (res instanceof Response) {
14
- return res;
12
+ const { upgradeHeaders, endResponse, context } = await hooks.upgrade(request);
13
+ if (endResponse) {
14
+ return endResponse;
15
15
  }
16
16
  const upgradeOK = server.upgrade(request, {
17
17
  data: {
18
18
  server,
19
- request
19
+ request,
20
+ context
20
21
  },
21
- headers: res?.headers
22
+ headers: upgradeHeaders
22
23
  });
23
24
  if (!upgradeOK) {
24
25
  return new Response("Upgrade failed", { status: 500 });
@@ -58,6 +59,9 @@ class BunPeer extends Peer {
58
59
  get remoteAddress() {
59
60
  return this._internal.ws.remoteAddress;
60
61
  }
62
+ get context() {
63
+ return this._internal.ws.data.context;
64
+ }
61
65
  send(data, options) {
62
66
  return this._internal.ws.send(toBufferLike(data), options?.compress);
63
67
  }
@@ -1,5 +1,5 @@
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';
1
+ import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.DLRVRjZs.mjs';
2
+ import { d as defineWebSocketAdapter, a as adapterUtils, A as AdapterHookable } from '../shared/crossws.BcEs6ZHK.mjs';
3
3
  import 'uncrypto';
4
4
 
5
5
  const cloudflareDurable = defineWebSocketAdapter((opts) => {
@@ -18,9 +18,11 @@ const cloudflareDurable = defineWebSocketAdapter((opts) => {
18
18
  handleDurableInit: async (obj, state, env) => {
19
19
  },
20
20
  handleDurableUpgrade: async (obj, request) => {
21
- const res = await hooks.callHook("upgrade", request);
22
- if (res instanceof Response) {
23
- return res;
21
+ const { upgradeHeaders, endResponse } = await hooks.upgrade(
22
+ request
23
+ );
24
+ if (endResponse) {
25
+ return endResponse;
24
26
  }
25
27
  const pair = new WebSocketPair();
26
28
  const client = pair[0];
@@ -36,7 +38,7 @@ const cloudflareDurable = defineWebSocketAdapter((opts) => {
36
38
  return new Response(null, {
37
39
  status: 101,
38
40
  webSocket: client,
39
- headers: res?.headers
41
+ headers: upgradeHeaders
40
42
  });
41
43
  },
42
44
  handleDurableMessage: async (obj, ws, message) => {
@@ -1,5 +1,5 @@
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';
1
+ import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.DLRVRjZs.mjs';
2
+ import { d as defineWebSocketAdapter, a as adapterUtils, A as AdapterHookable } from '../shared/crossws.BcEs6ZHK.mjs';
3
3
  import { W as WSError } from '../shared/crossws.By9qWDAI.mjs';
4
4
  import 'uncrypto';
5
5
 
@@ -9,13 +9,12 @@ const cloudflare = defineWebSocketAdapter(
9
9
  const peers = /* @__PURE__ */ new Set();
10
10
  return {
11
11
  ...adapterUtils(peers),
12
- handleUpgrade: async (request, env, context) => {
13
- const res = await hooks.callHook(
14
- "upgrade",
12
+ handleUpgrade: async (request, env, cfCtx) => {
13
+ const { upgradeHeaders, endResponse, context } = await hooks.upgrade(
15
14
  request
16
15
  );
17
- if (res instanceof Response) {
18
- return res;
16
+ if (endResponse) {
17
+ return endResponse;
19
18
  }
20
19
  const pair = new WebSocketPair();
21
20
  const client = pair[0];
@@ -26,7 +25,8 @@ const cloudflare = defineWebSocketAdapter(
26
25
  wsServer: server,
27
26
  request,
28
27
  cfEnv: env,
29
- cfCtx: context
28
+ cfCtx,
29
+ context
30
30
  });
31
31
  peers.add(peer);
32
32
  server.accept();
@@ -49,7 +49,7 @@ const cloudflare = defineWebSocketAdapter(
49
49
  return new Response(null, {
50
50
  status: 101,
51
51
  webSocket: client,
52
- headers: res?.headers
52
+ headers: upgradeHeaders
53
53
  });
54
54
  }
55
55
  };
@@ -1,5 +1,4 @@
1
1
  import { AdapterInstance, AdapterOptions, Adapter } from '../index.mjs';
2
- import * as _deno_types from '@deno/types';
3
2
  import '../shared/crossws.ChIJSJVK.mjs';
4
3
 
5
4
  interface DenoAdapter extends AdapterInstance {
@@ -7,9 +6,6 @@ interface DenoAdapter extends AdapterInstance {
7
6
  }
8
7
  interface DenoOptions extends AdapterOptions {
9
8
  }
10
- declare global {
11
- const Deno: typeof _deno_types.Deno;
12
- }
13
9
  type ServeHandlerInfo = {
14
10
  remoteAddr?: {
15
11
  transport: string;
@@ -1,5 +1,4 @@
1
1
  import { AdapterInstance, AdapterOptions, Adapter } from '../index.js';
2
- import * as _deno_types from '@deno/types';
3
2
  import '../shared/crossws.ChIJSJVK.js';
4
3
 
5
4
  interface DenoAdapter extends AdapterInstance {
@@ -7,9 +6,6 @@ interface DenoAdapter extends AdapterInstance {
7
6
  }
8
7
  interface DenoOptions extends AdapterOptions {
9
8
  }
10
- declare global {
11
- const Deno: typeof _deno_types.Deno;
12
- }
13
9
  type ServeHandlerInfo = {
14
10
  remoteAddr?: {
15
11
  transport: string;
@@ -1,5 +1,5 @@
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';
1
+ import { M as Message, P as Peer, t as toBufferLike } from '../shared/crossws.DLRVRjZs.mjs';
2
+ import { d as defineWebSocketAdapter, a as adapterUtils, A as AdapterHookable } from '../shared/crossws.BcEs6ZHK.mjs';
3
3
  import { W as WSError } from '../shared/crossws.By9qWDAI.mjs';
4
4
  import 'uncrypto';
5
5
 
@@ -10,19 +10,20 @@ const deno = defineWebSocketAdapter(
10
10
  return {
11
11
  ...adapterUtils(peers),
12
12
  handleUpgrade: async (request, info) => {
13
- const res = await hooks.callHook("upgrade", request);
14
- if (res instanceof Response) {
15
- return res;
13
+ const { upgradeHeaders, endResponse, context } = await hooks.upgrade(request);
14
+ if (endResponse) {
15
+ return endResponse;
16
16
  }
17
17
  const upgrade = Deno.upgradeWebSocket(request, {
18
18
  // @ts-expect-error https://github.com/denoland/deno/pull/22242
19
- headers: res?.headers
19
+ headers: upgradeHeaders
20
20
  });
21
21
  const peer = new DenoPeer({
22
22
  ws: upgrade.socket,
23
23
  request,
24
24
  peers,
25
- denoInfo: info
25
+ denoInfo: info,
26
+ context
26
27
  });
27
28
  peers.add(peer);
28
29
  upgrade.socket.addEventListener("open", () => {