crossws 0.3.1 → 0.3.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.
@@ -8,6 +8,7 @@ type UserData = {
8
8
  res: uws.HttpResponse;
9
9
  protocol: string;
10
10
  extensions: string;
11
+ context: Peer["context"];
11
12
  };
12
13
  type WebSocketHandler = uws.WebSocketBehavior<UserData>;
13
14
  interface UWSAdapter extends AdapterInstance {
@@ -26,6 +27,7 @@ declare class UWSPeer extends Peer<{
26
27
  uwsData: UserData;
27
28
  }> {
28
29
  get remoteAddress(): string | undefined;
30
+ get context(): Record<string, unknown>;
29
31
  send(data: unknown, options?: {
30
32
  compress?: boolean;
31
33
  }): number;
@@ -8,6 +8,7 @@ type UserData = {
8
8
  res: uws.HttpResponse;
9
9
  protocol: string;
10
10
  extensions: string;
11
+ context: Peer["context"];
11
12
  };
12
13
  type WebSocketHandler = uws.WebSocketBehavior<UserData>;
13
14
  interface UWSAdapter extends AdapterInstance {
@@ -26,6 +27,7 @@ declare class UWSPeer extends Peer<{
26
27
  uwsData: UserData;
27
28
  }> {
28
29
  get remoteAddress(): string | undefined;
30
+ get context(): Record<string, unknown>;
29
31
  send(data: unknown, options?: {
30
32
  compress?: boolean;
31
33
  }): 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.CB4awDDj.mjs';
3
3
  import 'uncrypto';
4
4
 
5
5
  const uws = defineWebSocketAdapter(
@@ -29,25 +29,22 @@ const uws = defineWebSocketAdapter(
29
29
  peers.add(peer);
30
30
  hooks.callHook("open", peer);
31
31
  },
32
- async upgrade(res, req, context) {
32
+ async upgrade(res, req, uwsContext) {
33
33
  let aborted = false;
34
34
  res.onAborted(() => {
35
35
  aborted = true;
36
36
  });
37
- const _res = await hooks.callHook("upgrade", new UWSReqProxy(req));
38
- if (aborted) {
39
- return;
40
- }
41
- if (_res instanceof Response) {
42
- res.writeStatus(`${_res.status} ${_res.statusText}`);
43
- for (const [key, value] of _res.headers) {
37
+ const { upgradeHeaders, endResponse, context } = await hooks.upgrade(
38
+ new UWSReqProxy(req)
39
+ );
40
+ if (endResponse) {
41
+ res.writeStatus(`${endResponse.status} ${endResponse.statusText}`);
42
+ for (const [key, value] of endResponse.headers) {
44
43
  res.writeHeader(key, value);
45
44
  }
46
- if (_res.body) {
47
- for await (const chunk of _res.body) {
48
- if (aborted) {
49
- break;
50
- }
45
+ if (endResponse.body) {
46
+ for await (const chunk of endResponse.body) {
47
+ if (aborted) break;
51
48
  res.write(chunk);
52
49
  }
53
50
  }
@@ -56,9 +53,13 @@ const uws = defineWebSocketAdapter(
56
53
  }
57
54
  return;
58
55
  }
56
+ if (aborted) {
57
+ return;
58
+ }
59
59
  res.writeStatus("101 Switching Protocols");
60
- if (_res?.headers) {
61
- for (const [key, value] of new Headers(_res.headers)) {
60
+ if (upgradeHeaders) {
61
+ const headers = upgradeHeaders instanceof Headers ? upgradeHeaders : new Headers(upgradeHeaders);
62
+ for (const [key, value] of headers) {
62
63
  res.writeHeader(key, value);
63
64
  }
64
65
  }
@@ -71,12 +72,13 @@ const uws = defineWebSocketAdapter(
71
72
  req,
72
73
  res,
73
74
  protocol,
74
- extensions
75
+ extensions,
76
+ context
75
77
  },
76
78
  key,
77
79
  protocol,
78
80
  extensions,
79
- context
81
+ uwsContext
80
82
  );
81
83
  });
82
84
  }
@@ -109,6 +111,9 @@ class UWSPeer extends Peer {
109
111
  } catch {
110
112
  }
111
113
  }
114
+ get context() {
115
+ return this._internal.uwsData.context;
116
+ }
112
117
  send(data, options) {
113
118
  const dataBuff = toBufferLike(data);
114
119
  const isBinary = typeof data !== "string";
package/dist/index.d.mts CHANGED
@@ -2,8 +2,9 @@ import { W as WebSocket } from './shared/crossws.ChIJSJVK.mjs';
2
2
 
3
3
  interface AdapterInternal {
4
4
  ws: unknown;
5
- request?: Request | Partial<Request>;
5
+ request: UpgradeRequest;
6
6
  peers?: Set<Peer>;
7
+ context?: Peer["context"];
7
8
  }
8
9
  declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
9
10
  #private;
@@ -11,6 +12,7 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
11
12
  protected _topics: Set<string>;
12
13
  protected _id?: string;
13
14
  constructor(internal: Internal);
15
+ get context(): Record<string, unknown>;
14
16
  /**
15
17
  * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
16
18
  */
@@ -18,7 +20,7 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
18
20
  /** IP address of the peer */
19
21
  get remoteAddress(): string | undefined;
20
22
  /** upgrade request */
21
- get request(): Request | Partial<Request> | undefined;
23
+ get request(): UpgradeRequest;
22
24
  /**
23
25
  * Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
24
26
  *
@@ -83,7 +85,7 @@ declare class Message implements Partial<MessageEvent> {
83
85
  *
84
86
  * If raw data is in any other format or string, it will be automatically converted and encoded.
85
87
  */
86
- uint8Array(): Uint8Array;
88
+ uint8Array(): Uint8Array<ArrayBufferLike>;
87
89
  /**
88
90
  * 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.
89
91
  *
@@ -116,11 +118,19 @@ declare class Message implements Partial<MessageEvent> {
116
118
  declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
117
119
  type ResolveHooks = (info: RequestInit | Peer) => Partial<Hooks> | Promise<Partial<Hooks>>;
118
120
  type MaybePromise<T> = T | Promise<T>;
121
+ type UpgradeRequest = Request | {
122
+ url: string;
123
+ headers: Headers;
124
+ };
119
125
  interface Hooks {
120
126
  /** Upgrading */
121
- upgrade: (request: Request | {
122
- url: string;
123
- headers: Headers;
127
+ /**
128
+ *
129
+ * @param request
130
+ * @throws {Response}
131
+ */
132
+ upgrade: (request: UpgradeRequest & {
133
+ context: Peer["context"];
124
134
  }) => MaybePromise<Response | ResponseInit | void>;
125
135
  /** A message is received */
126
136
  message: (peer: Peer, message: Message) => MaybePromise<void>;
package/dist/index.d.ts CHANGED
@@ -2,8 +2,9 @@ import { W as WebSocket } from './shared/crossws.ChIJSJVK.js';
2
2
 
3
3
  interface AdapterInternal {
4
4
  ws: unknown;
5
- request?: Request | Partial<Request>;
5
+ request: UpgradeRequest;
6
6
  peers?: Set<Peer>;
7
+ context?: Peer["context"];
7
8
  }
8
9
  declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
9
10
  #private;
@@ -11,6 +12,7 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
11
12
  protected _topics: Set<string>;
12
13
  protected _id?: string;
13
14
  constructor(internal: Internal);
15
+ get context(): Record<string, unknown>;
14
16
  /**
15
17
  * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
16
18
  */
@@ -18,7 +20,7 @@ declare abstract class Peer<Internal extends AdapterInternal = AdapterInternal>
18
20
  /** IP address of the peer */
19
21
  get remoteAddress(): string | undefined;
20
22
  /** upgrade request */
21
- get request(): Request | Partial<Request> | undefined;
23
+ get request(): UpgradeRequest;
22
24
  /**
23
25
  * Get the [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance.
24
26
  *
@@ -83,7 +85,7 @@ declare class Message implements Partial<MessageEvent> {
83
85
  *
84
86
  * If raw data is in any other format or string, it will be automatically converted and encoded.
85
87
  */
86
- uint8Array(): Uint8Array;
88
+ uint8Array(): Uint8Array<ArrayBufferLike>;
87
89
  /**
88
90
  * 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.
89
91
  *
@@ -116,11 +118,19 @@ declare class Message implements Partial<MessageEvent> {
116
118
  declare function defineHooks<T extends Partial<Hooks> = Partial<Hooks>>(hooks: T): T;
117
119
  type ResolveHooks = (info: RequestInit | Peer) => Partial<Hooks> | Promise<Partial<Hooks>>;
118
120
  type MaybePromise<T> = T | Promise<T>;
121
+ type UpgradeRequest = Request | {
122
+ url: string;
123
+ headers: Headers;
124
+ };
119
125
  interface Hooks {
120
126
  /** Upgrading */
121
- upgrade: (request: Request | {
122
- url: string;
123
- headers: Headers;
127
+ /**
128
+ *
129
+ * @param request
130
+ * @throws {Response}
131
+ */
132
+ upgrade: (request: UpgradeRequest & {
133
+ context: Peer["context"];
124
134
  }) => MaybePromise<Response | ResponseInit | void>;
125
135
  /** A message is received */
126
136
  message: (peer: Peer, message: Message) => MaybePromise<void>;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export { b as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.B4sHId41.mjs';
1
+ export { b as defineHooks, d as defineWebSocketAdapter } from './shared/crossws.CB4awDDj.mjs';
@@ -18,6 +18,37 @@ class AdapterHookable {
18
18
  }
19
19
  );
20
20
  }
21
+ async upgrade(request) {
22
+ const context = request.context ??= {};
23
+ try {
24
+ const res = await this.callHook(
25
+ "upgrade",
26
+ request
27
+ );
28
+ if (!res) {
29
+ return { context };
30
+ }
31
+ if (res.ok === false) {
32
+ return { context, endResponse: res };
33
+ }
34
+ if (res.headers) {
35
+ return {
36
+ context,
37
+ upgradeHeaders: res.headers
38
+ };
39
+ }
40
+ } catch (error) {
41
+ const errResponse = error.response || error;
42
+ if (errResponse instanceof Response) {
43
+ return {
44
+ context,
45
+ endResponse: errResponse
46
+ };
47
+ }
48
+ throw error;
49
+ }
50
+ return { context };
51
+ }
21
52
  }
22
53
  function defineHooks(hooks) {
23
54
  return hooks;
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from 'uncrypto';
2
2
 
3
3
  function toBufferLike(val) {
4
- if (val === void 0 || val === null) {
4
+ if (val === undefined || val === null) {
5
5
  return "";
6
6
  }
7
7
  const type = typeof val;
@@ -220,6 +220,9 @@ class Peer {
220
220
  this._topics = /* @__PURE__ */ new Set();
221
221
  this._internal = internal;
222
222
  }
223
+ get context() {
224
+ return this._internal.context ??= {};
225
+ }
223
226
  /**
224
227
  * Unique random [uuid v4](https://developer.mozilla.org/en-US/docs/Glossary/UUID) identifier for the peer.
225
228
  */
@@ -231,7 +234,7 @@ class Peer {
231
234
  }
232
235
  /** IP address of the peer */
233
236
  get remoteAddress() {
234
- return void 0;
237
+ return undefined;
235
238
  }
236
239
  /** upgrade request */
237
240
  get request() {
@@ -303,7 +306,7 @@ function createWsProxy(ws, request) {
303
306
  return request?.headers?.get("sec-websocket-extensions") || "";
304
307
  }
305
308
  case "url": {
306
- return request?.url?.replace(/^http/, "ws") || void 0;
309
+ return request?.url?.replace(/^http/, "ws") || undefined;
307
310
  }
308
311
  }
309
312
  }