uws-react-native 0.0.11 → 0.1.0-beta.1

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 (78) hide show
  1. package/README.md +1 -1
  2. package/cpp/UwsReactNativeModule.cpp +4 -0
  3. package/cpp/app/HttpResponseObject.h +116 -40
  4. package/cpp/app/RecognizedString.h +20 -14
  5. package/cpp/app/TemplatedAppObject.h +61 -36
  6. package/cpp/app/WebSocketBehaviorImpl.h +269 -11
  7. package/cpp/app/WebSocketObject.h +354 -16
  8. package/cpp/app/WebSocketUserData.h +227 -0
  9. package/lib/commonjs/modules/WebSocketSendStatus.js +22 -0
  10. package/lib/commonjs/modules/WebSocketSendStatus.js.map +1 -0
  11. package/lib/commonjs/modules/index.js +22 -0
  12. package/lib/commonjs/modules/index.js.map +1 -1
  13. package/lib/commonjs/types/WebSocketUserData.js +2 -0
  14. package/lib/commonjs/types/WebSocketUserData.js.map +1 -0
  15. package/lib/module/modules/WebSocketSendStatus.js +18 -0
  16. package/lib/module/modules/WebSocketSendStatus.js.map +1 -0
  17. package/lib/module/modules/index.js +2 -1
  18. package/lib/module/modules/index.js.map +1 -1
  19. package/lib/module/types/WebSocketUserData.js +2 -0
  20. package/lib/module/types/WebSocketUserData.js.map +1 -0
  21. package/lib/typescript/commonjs/modules/WebSocketSendStatus.d.ts +15 -0
  22. package/lib/typescript/commonjs/modules/WebSocketSendStatus.d.ts.map +1 -0
  23. package/lib/typescript/commonjs/modules/index.d.ts +2 -0
  24. package/lib/typescript/commonjs/modules/index.d.ts.map +1 -1
  25. package/lib/typescript/commonjs/types/HttpResponse.d.ts +35 -0
  26. package/lib/typescript/commonjs/types/HttpResponse.d.ts.map +1 -1
  27. package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts +1 -1
  28. package/lib/typescript/commonjs/types/TemplatedApp.d.ts +5 -0
  29. package/lib/typescript/commonjs/types/TemplatedApp.d.ts.map +1 -1
  30. package/lib/typescript/commonjs/types/WebSocket.d.ts +18 -8
  31. package/lib/typescript/commonjs/types/WebSocket.d.ts.map +1 -1
  32. package/lib/typescript/commonjs/types/WebSocketBehavior.d.ts +9 -9
  33. package/lib/typescript/commonjs/types/WebSocketBehavior.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/types/WebSocketUserData.d.ts +9 -0
  35. package/lib/typescript/commonjs/types/WebSocketUserData.d.ts.map +1 -0
  36. package/lib/typescript/commonjs/types/index.d.ts +3 -0
  37. package/lib/typescript/commonjs/types/index.d.ts.map +1 -1
  38. package/lib/typescript/commonjs/types/us_socket_context_t.d.ts +2 -3
  39. package/lib/typescript/commonjs/types/us_socket_context_t.d.ts.map +1 -1
  40. package/lib/typescript/module/index.d.ts +2 -2
  41. package/lib/typescript/module/index.d.ts.map +1 -1
  42. package/lib/typescript/module/modules/App.d.ts +1 -1
  43. package/lib/typescript/module/modules/App.d.ts.map +1 -1
  44. package/lib/typescript/module/modules/WebSocketSendStatus.d.ts +15 -0
  45. package/lib/typescript/module/modules/WebSocketSendStatus.d.ts.map +1 -0
  46. package/lib/typescript/module/modules/getParts.d.ts +1 -1
  47. package/lib/typescript/module/modules/getParts.d.ts.map +1 -1
  48. package/lib/typescript/module/modules/index.d.ts +7 -5
  49. package/lib/typescript/module/modules/index.d.ts.map +1 -1
  50. package/lib/typescript/module/types/HttpRequest.d.ts +1 -1
  51. package/lib/typescript/module/types/HttpRequest.d.ts.map +1 -1
  52. package/lib/typescript/module/types/HttpResponse.d.ts +36 -1
  53. package/lib/typescript/module/types/HttpResponse.d.ts.map +1 -1
  54. package/lib/typescript/module/types/HttpRouterOptions.d.ts +1 -1
  55. package/lib/typescript/module/types/TemplatedApp.d.ts +10 -5
  56. package/lib/typescript/module/types/TemplatedApp.d.ts.map +1 -1
  57. package/lib/typescript/module/types/WebSocket.d.ts +19 -9
  58. package/lib/typescript/module/types/WebSocket.d.ts.map +1 -1
  59. package/lib/typescript/module/types/WebSocketBehavior.d.ts +14 -14
  60. package/lib/typescript/module/types/WebSocketBehavior.d.ts.map +1 -1
  61. package/lib/typescript/module/types/WebSocketUserData.d.ts +9 -0
  62. package/lib/typescript/module/types/WebSocketUserData.d.ts.map +1 -0
  63. package/lib/typescript/module/types/index.d.ts +8 -5
  64. package/lib/typescript/module/types/index.d.ts.map +1 -1
  65. package/lib/typescript/module/types/us_socket_context_t.d.ts +2 -3
  66. package/lib/typescript/module/types/us_socket_context_t.d.ts.map +1 -1
  67. package/lib/typescript/tsconfig.tsbuildinfo +1 -1
  68. package/package.json +4 -3
  69. package/src/modules/WebSocketSendStatus.ts +14 -0
  70. package/src/modules/index.ts +2 -1
  71. package/src/types/HttpResponse.ts +46 -29
  72. package/src/types/HttpRouterOptions.ts +1 -1
  73. package/src/types/TemplatedApp.ts +10 -10
  74. package/src/types/WebSocket.ts +24 -8
  75. package/src/types/WebSocketBehavior.ts +9 -9
  76. package/src/types/WebSocketUserData.ts +28 -0
  77. package/src/types/index.ts +3 -2
  78. package/src/types/us_socket_context_t.ts +2 -3
@@ -2,9 +2,13 @@ import type {
2
2
  RecognizedString,
3
3
  } from "./RecognizedString"
4
4
 
5
- // import type {
6
- // us_socket_context_t,
7
- // } from "./us_socket_context_t"
5
+ import type {
6
+ WebSocketUserData,
7
+ } from "./WebSocketUserData"
8
+
9
+ import type {
10
+ us_socket_context_t,
11
+ } from "./us_socket_context_t"
8
12
 
9
13
  /**
10
14
  * An HttpResponse is valid until either onAborted callback or any of the .end/.tryEnd calls succeed. You may attach user data to this object.
@@ -247,23 +251,45 @@ export interface HttpResponse {
247
251
  totalSize: number,
248
252
  ) : [boolean, boolean],
249
253
 
250
- // TODO
251
- // Implement this method later.
252
- // /**
253
- // * Upgrades a HttpResponse to a WebSocket. See UpgradeAsync, UpgradeSync example files.
254
- // *
255
- // * Currently, this library is only supported string for the user data.
256
- // * As an object alternative, you can use `JSON.stringify` to this method,
257
- // * and retrieve the user data back from ws.getUserData() with `JSON.parse`.
258
- // */
259
- // upgrade/* <UserData extends object> */(
260
- // // userData: UserData,
261
- // userData: string,
262
- // secWebSocketKey: RecognizedString,
263
- // secWebSocketProtocol: RecognizedString,
264
- // secWebSocketExtensions: RecognizedString,
265
- // context: us_socket_context_t,
266
- // ) : void,
254
+ /**
255
+ * Upgrades a HttpResponse to a WebSocket.
256
+ *
257
+ * Important! There is one difference compared to the uWebSockets.js.
258
+ * Instead of passing a user data object to the first argument,
259
+ * modify the user data object from the handler parameter at the first argument of this method.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * uWS.App()
264
+ * .ws("/*", {
265
+ * upgrade: (res, req, context) => {
266
+ * // you can do an async task here
267
+ * // and then, call upgrade method from res
268
+ * res.upgrade(
269
+ * userData => { // do not do an async task in this
270
+ * userData.setString("your_love_letter", "hello world")
271
+ * userData.setBoolean("is_flat_earth", true)
272
+ * userData.setNumber("worth", 67)
273
+ * },
274
+ * req.getHeader("sec-websocket-key"),
275
+ * req.getHeader("sec-websocket-protocol"),
276
+ * req.getHeader("sec-websocket-extensions"),
277
+ * context,
278
+ * )
279
+ * },
280
+ * open: ws => {
281
+ * const isFlatEarth = ws.getUserData().getBoolean("is_flat_earth")
282
+ * },
283
+ * })
284
+ * ```
285
+ */
286
+ upgrade(
287
+ userDataFn: (userData: WebSocketUserData) => void,
288
+ secWebSocketKey: RecognizedString,
289
+ secWebSocketProtocol: RecognizedString,
290
+ secWebSocketExtensions: RecognizedString,
291
+ context: us_socket_context_t,
292
+ ) : void,
267
293
 
268
294
  /**
269
295
  * Enters or continues chunked encoding mode. Writes part of the response. End with zero length write. Returns true if no backpressure was added.
@@ -299,13 +325,4 @@ export interface HttpResponse {
299
325
  writeStatus(
300
326
  status: RecognizedString,
301
327
  ) : HttpResponse,
302
-
303
- // TODO
304
- // the `upgrade` method needs to be implemented first,
305
- // but I also don't know how to attach arbitrary UserData to the HttpRequest object JSI C++.
306
- // Probably, I would tell user to get the data explicitly from the ws.getUserData() method
307
- // /**
308
- // * Arbitrary user data may be attached to this object
309
- // */
310
- // [key: string]: unknown,
311
328
  }
@@ -5,7 +5,7 @@ export interface HttpRouterOptions {
5
5
  * if the body read is disabled.
6
6
  *
7
7
  * This is an actual optimization, because uws-react-native has to predefined the body read
8
- * (the res->onDataV2) internally from C++ side by default. So if you disable body reading
8
+ * (the res->onDataV2) internally from C++ side by default. So, if you disable body reading
9
9
  * in a specific route handler, uws-react-native will not do the predefined res->onDataV2 from C++ side at all.
10
10
  *
11
11
  * The reason why uws-react-native has to predefined the res->onDataV2 from C++ side, it is because we cannot do a sync call from uWebSockets C++ which run in arbitrary thread to JS, and late onDataV2 assignment will losing the body data. It is also by design that we run uWebSockets in another thread to prevent Main/UI thread blocking.
@@ -26,9 +26,9 @@ import type {
26
26
  RecognizedString,
27
27
  } from "./RecognizedString"
28
28
 
29
- // import type {
30
- // WebSocketBehavior,
31
- // } from "./WebSocketBehavior"
29
+ import type {
30
+ WebSocketBehavior,
31
+ } from "./WebSocketBehavior"
32
32
 
33
33
  // import type {
34
34
  // us_listen_socket,
@@ -172,13 +172,13 @@ export interface TemplatedApp {
172
172
  // hostname: string,
173
173
  // ) : TemplatedApp,
174
174
 
175
- // /**
176
- // * Registers a handler matching specified URL pattern where WebSocket upgrade requests are caught.
177
- // */
178
- // ws<UserData>(
179
- // pattern: RecognizedString,
180
- // behavior: WebSocketBehavior<UserData>,
181
- // ) : TemplatedApp,
175
+ /**
176
+ * Registers a handler matching specified URL pattern where WebSocket upgrade requests are caught.
177
+ */
178
+ ws(
179
+ pattern: RecognizedString,
180
+ behavior: WebSocketBehavior,
181
+ ) : TemplatedApp,
182
182
 
183
183
  // we can't provide this in Reaact Native
184
184
  /**
@@ -1,12 +1,20 @@
1
+ import {
2
+ WebSocketSendStatus,
3
+ } from "../modules/WebSocketSendStatus"
4
+
1
5
  import type {
2
6
  RecognizedString,
3
7
  } from "./RecognizedString"
4
8
 
9
+ import type {
10
+ WebSocketUserData,
11
+ } from "./WebSocketUserData"
12
+
5
13
  /**
6
14
  * A WebSocket connection that is valid from open to close event.
7
15
  * Read more about this in the user manual.
8
16
  */
9
- export interface WebSocket<UserData> {
17
+ export interface WebSocket {
10
18
  /**
11
19
  * Forcefully closes this WebSocket. Immediately calls the close handler.
12
20
  * No WebSocket close message is sent.
@@ -19,7 +27,7 @@ export interface WebSocket<UserData> {
19
27
  */
20
28
  cork(
21
29
  cb: () => void,
22
- ) : WebSocket<UserData>,
30
+ ) : WebSocket,
23
31
 
24
32
  /**
25
33
  * Gracefully closes this WebSocket. Immediately calls the close handler.
@@ -62,9 +70,9 @@ export interface WebSocket<UserData> {
62
70
  getTopics() : string[],
63
71
 
64
72
  /**
65
- * Returns the UserData object.
73
+ * Returns the user data object.
66
74
  */
67
- getUserData() : UserData,
75
+ getUserData() : WebSocketUserData,
68
76
 
69
77
  /**
70
78
  * Returns whether this websocket is subscribed to topic.
@@ -95,40 +103,48 @@ export interface WebSocket<UserData> {
95
103
  * Sends a message. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure that will drain over time. You can check backpressure before or after sending by calling getBufferedAmount().
96
104
  *
97
105
  * Make sure you properly understand the concept of backpressure. Check the backpressure example file.
106
+ *
107
+ * This method will returns void due to the message is not a recognized string.
98
108
  */
99
109
  send(
100
110
  message: RecognizedString,
101
111
  isBinary?: boolean,
102
112
  compress?: boolean,
103
- ) : number,
113
+ ) : WebSocketSendStatus | void,
104
114
 
105
115
  /**
106
116
  * Sends the first fragment of a fragmented message. Use for sending large messages in chunks.
107
117
  * Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure.
118
+ *
119
+ * This method will returns void due to the message is not a recognized string.
108
120
  */
109
121
  sendFirstFragment(
110
122
  message: RecognizedString,
111
123
  isBinary?: boolean,
112
124
  compress?: boolean,
113
- ) : number,
125
+ ) : WebSocketSendStatus | void,
114
126
 
115
127
  /**
116
128
  * Sends a middle fragment of a fragmented message.
117
129
  * Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure.
130
+ *
131
+ * This method will returns void due to the message is not a recognized string.
118
132
  */
119
133
  sendFragment(
120
134
  message: RecognizedString,
121
135
  compress?: boolean,
122
- ) : number,
136
+ ) : WebSocketSendStatus | void,
123
137
 
124
138
  /**
125
139
  * Sends the last fragment of a fragmented message.
126
140
  * Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure.
141
+ *
142
+ * This method will returns void due to the message is not a recognized string.
127
143
  */
128
144
  sendLastFragment(
129
145
  message: RecognizedString,
130
146
  compress?: boolean,
131
- ) : number,
147
+ ) : WebSocketSendStatus | void,
132
148
 
133
149
  /**
134
150
  * Subscribe to a topic.
@@ -21,14 +21,14 @@ import type {
21
21
  /**
22
22
  * A structure holding settings and handlers for a WebSocket URL route handler.
23
23
  */
24
- export interface WebSocketBehavior<UserData> {
24
+ export interface WebSocketBehavior/* <UserData extends object> */ {
25
25
  /**
26
26
  * Handler for close event, no matter if error, timeout or graceful close.
27
27
  * You may not use WebSocket after this event.
28
28
  * Do not send on this WebSocket from within here, it is closed.
29
29
  */
30
30
  close?: (
31
- ws: WebSocket<UserData>,
31
+ ws: WebSocket,
32
32
  code: number,
33
33
  message: ArrayBuffer,
34
34
  ) => void,
@@ -51,7 +51,7 @@ export interface WebSocketBehavior<UserData> {
51
51
  * Use this to guide / drive your backpressure throttling.
52
52
  */
53
53
  drain?: (
54
- ws: WebSocket<UserData>,
54
+ ws: WebSocket,
55
55
  ) => void,
56
56
 
57
57
  /**
@@ -62,7 +62,7 @@ export interface WebSocketBehavior<UserData> {
62
62
  * (until first await or return) and will be neutered.
63
63
  */
64
64
  dropped?: (
65
- ws: WebSocket<UserData>,
65
+ ws: WebSocket,
66
66
  message: ArrayBuffer,
67
67
  isBinary: boolean,
68
68
  ) => void | Promise<void>,
@@ -105,7 +105,7 @@ export interface WebSocketBehavior<UserData> {
105
105
  * Given ArrayBuffer is valid during the lifetime of this callback (until first await or return) and will be neutered.
106
106
  */
107
107
  message?: (
108
- ws: WebSocket<UserData>,
108
+ ws: WebSocket,
109
109
  message: ArrayBuffer,
110
110
  isBinary: boolean,
111
111
  ) => void | Promise<void>,
@@ -115,7 +115,7 @@ export interface WebSocketBehavior<UserData> {
115
115
  * WebSocket is valid from open to close, no errors.
116
116
  */
117
117
  open?: (
118
- ws: WebSocket<UserData>,
118
+ ws: WebSocket,
119
119
  ) => void | Promise<void>,
120
120
 
121
121
  /**
@@ -123,7 +123,7 @@ export interface WebSocketBehavior<UserData> {
123
123
  * You do not need to handle this, pong messages are automatically sent as per the standard.
124
124
  */
125
125
  ping?: (
126
- ws: WebSocket<UserData>,
126
+ ws: WebSocket,
127
127
  message: ArrayBuffer,
128
128
  ) => void,
129
129
 
@@ -131,7 +131,7 @@ export interface WebSocketBehavior<UserData> {
131
131
  * Handler for received pong control message.
132
132
  */
133
133
  pong?: (
134
- ws: WebSocket<UserData>,
134
+ ws: WebSocket,
135
135
  message: ArrayBuffer,
136
136
  ) => void,
137
137
 
@@ -144,7 +144,7 @@ export interface WebSocketBehavior<UserData> {
144
144
  * Handler for subscription changes.
145
145
  */
146
146
  subscription?: (
147
- ws: WebSocket<UserData>,
147
+ ws: WebSocket,
148
148
  topic: ArrayBuffer,
149
149
  newCount: number,
150
150
  oldCount: number,
@@ -0,0 +1,28 @@
1
+ export interface WebSocketUserData {
2
+ getBoolean(
3
+ key: string,
4
+ ): boolean | undefined,
5
+
6
+ setBoolean(
7
+ key: string,
8
+ value: boolean,
9
+ ): void,
10
+
11
+ getNumber(
12
+ key: string,
13
+ ): number | undefined,
14
+
15
+ setNumber(
16
+ key: string,
17
+ value: number,
18
+ ): void,
19
+
20
+ getString(
21
+ key: string,
22
+ ): string | undefined,
23
+
24
+ setString(
25
+ key: string,
26
+ value: number,
27
+ ): void,
28
+ }
@@ -5,8 +5,9 @@ export type * from "./HttpResponse"
5
5
  export type * from "./MultipartField"
6
6
  export type * from "./RecognizedString"
7
7
  export type * from "./TemplatedApp"
8
- // export type * from "./WebSocket"
9
- // export type * from "./WebSocketBehavior"
8
+ export type * from "./WebSocket"
9
+ export type * from "./WebSocketBehavior"
10
+ export type * from "./WebSocketUserData"
10
11
  // export type * from "./us_listen_socket"
11
12
  // export type * from "./us_socket"
12
13
  // export type * from "./us_socket_context_t"
@@ -1,6 +1,5 @@
1
1
  /**
2
2
  * Native type representing a raw uSockets struct us_socket_context_t.
3
- * Used while upgrading a WebSocket manually.
3
+ * This is not intended to be modified from JavaScript.
4
4
  */
5
- export interface us_socket_context_t {
6
- }
5
+ export type us_socket_context_t = unknown