@celljs/rpc 4.3.0 → 4.4.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 (545) hide show
  1. package/README.md +117 -1
  2. package/cell.schema.json +100 -0
  3. package/cell.yml +12 -8
  4. package/esm/common/annotation/index.d.ts +2 -0
  5. package/esm/common/annotation/index.js +2 -0
  6. package/esm/common/annotation/index.js.map +1 -1
  7. package/esm/common/annotation/rpc-client.d.ts +7 -0
  8. package/esm/common/annotation/rpc-client.js +18 -0
  9. package/esm/common/annotation/rpc-client.js.map +1 -0
  10. package/esm/common/annotation/rpc-protocol.d.ts +7 -0
  11. package/esm/common/annotation/rpc-protocol.js +2 -0
  12. package/esm/common/annotation/rpc-protocol.js.map +1 -0
  13. package/esm/common/annotation/rpc.d.ts +4 -0
  14. package/esm/common/annotation/rpc.js +14 -18
  15. package/esm/common/annotation/rpc.js.map +1 -1
  16. package/esm/common/client/client-protocol.d.ts +6 -0
  17. package/esm/common/client/client-protocol.js +2 -0
  18. package/esm/common/client/client-protocol.js.map +1 -0
  19. package/esm/common/client/index.d.ts +2 -0
  20. package/esm/common/client/index.js +3 -0
  21. package/esm/common/client/index.js.map +1 -0
  22. package/esm/common/client/rpc-client-registry.d.ts +14 -0
  23. package/esm/common/client/rpc-client-registry.js +51 -0
  24. package/esm/common/client/rpc-client-registry.js.map +1 -0
  25. package/esm/common/constants.d.ts +3 -1
  26. package/esm/common/constants.js +3 -1
  27. package/esm/common/constants.js.map +1 -1
  28. package/esm/common/converter/converter-protocol.d.ts +6 -2
  29. package/esm/common/converter/converter-protocol.js +1 -0
  30. package/esm/common/converter/converter-protocol.js.map +1 -1
  31. package/esm/common/endpoint/endpoint-protocol.d.ts +3 -0
  32. package/esm/common/endpoint/endpoint-resolver.d.ts +2 -0
  33. package/esm/common/endpoint/endpoint-resolver.js +17 -3
  34. package/esm/common/endpoint/endpoint-resolver.js.map +1 -1
  35. package/esm/common/error/error.d.ts +23 -0
  36. package/esm/common/error/error.js +34 -2
  37. package/esm/common/error/error.js.map +1 -1
  38. package/esm/common/index.d.ts +6 -6
  39. package/esm/common/index.js +6 -6
  40. package/esm/common/index.js.map +1 -1
  41. package/esm/common/module.js +5 -4
  42. package/esm/common/module.js.map +1 -1
  43. package/esm/common/processor/client-config-processor.d.ts +2 -2
  44. package/esm/common/processor/client-config-processor.js.map +1 -1
  45. package/esm/common/processor/processor-protocol.d.ts +2 -2
  46. package/esm/common/protocol/index.d.ts +1 -0
  47. package/esm/common/protocol/index.js +2 -0
  48. package/esm/common/protocol/index.js.map +1 -0
  49. package/esm/common/protocol/json-rpc.d.ts +42 -0
  50. package/esm/common/protocol/json-rpc.js +48 -0
  51. package/esm/common/protocol/json-rpc.js.map +1 -0
  52. package/esm/common/proxy/index.d.ts +1 -4
  53. package/esm/common/proxy/index.js +1 -4
  54. package/esm/common/proxy/index.js.map +1 -1
  55. package/esm/common/proxy/rpc-proxy.d.ts +6 -0
  56. package/esm/common/proxy/rpc-proxy.js +40 -0
  57. package/esm/common/proxy/rpc-proxy.js.map +1 -0
  58. package/esm/common/transport/http-transport.d.ts +50 -0
  59. package/esm/common/transport/http-transport.js +98 -0
  60. package/esm/common/transport/http-transport.js.map +1 -0
  61. package/esm/common/transport/index.d.ts +4 -0
  62. package/esm/common/transport/index.js +5 -0
  63. package/esm/common/transport/index.js.map +1 -0
  64. package/esm/common/transport/transport-protocol.d.ts +12 -0
  65. package/esm/common/transport/transport-protocol.js +2 -0
  66. package/esm/common/transport/transport-protocol.js.map +1 -0
  67. package/esm/common/transport/transport-provider.d.ts +34 -0
  68. package/esm/common/transport/transport-provider.js +103 -0
  69. package/esm/common/transport/transport-provider.js.map +1 -0
  70. package/esm/common/transport/websocket-transport.d.ts +54 -0
  71. package/esm/common/transport/websocket-transport.js +144 -0
  72. package/esm/common/transport/websocket-transport.js.map +1 -0
  73. package/esm/common/utils/index.d.ts +1 -1
  74. package/esm/common/utils/index.js +1 -1
  75. package/esm/common/utils/index.js.map +1 -1
  76. package/esm/common/utils/rpc-method-util.d.ts +10 -0
  77. package/esm/common/utils/rpc-method-util.js +28 -0
  78. package/esm/common/utils/rpc-method-util.js.map +1 -0
  79. package/esm/common/utils/rpc-util.d.ts +9 -9
  80. package/esm/common/utils/rpc-util.js +24 -30
  81. package/esm/common/utils/rpc-util.js.map +1 -1
  82. package/esm/node/connection/connection-protocol.d.ts +26 -0
  83. package/esm/node/connection/connection-protocol.js +3 -0
  84. package/esm/node/connection/connection-protocol.js.map +1 -0
  85. package/esm/node/connection/index.d.ts +4 -0
  86. package/esm/node/connection/index.js +5 -0
  87. package/esm/node/connection/index.js.map +1 -0
  88. package/esm/node/connection/rpc-connection-manager.d.ts +10 -0
  89. package/esm/node/connection/rpc-connection-manager.js +35 -0
  90. package/esm/node/connection/rpc-connection-manager.js.map +1 -0
  91. package/esm/node/connection/rpc-connection.d.ts +33 -0
  92. package/esm/node/connection/rpc-connection.js +106 -0
  93. package/esm/node/connection/rpc-connection.js.map +1 -0
  94. package/esm/node/connection/rpc-context.d.ts +14 -0
  95. package/esm/node/connection/rpc-context.js +29 -0
  96. package/esm/node/connection/rpc-context.js.map +1 -0
  97. package/esm/node/index.d.ts +3 -2
  98. package/esm/node/index.js +3 -2
  99. package/esm/node/index.js.map +1 -1
  100. package/esm/node/server/index.d.ts +2 -0
  101. package/esm/node/server/index.js +3 -0
  102. package/esm/node/server/index.js.map +1 -0
  103. package/esm/node/server/rpc-handler-adapter.d.ts +15 -0
  104. package/esm/node/server/rpc-handler-adapter.js +74 -0
  105. package/esm/node/server/rpc-handler-adapter.js.map +1 -0
  106. package/esm/node/server/rpc-invoker.d.ts +28 -0
  107. package/esm/node/server/rpc-invoker.js +113 -0
  108. package/esm/node/server/rpc-invoker.js.map +1 -0
  109. package/esm/node/test/websocket-fixture-protocol.d.ts +36 -0
  110. package/esm/node/test/websocket-fixture-protocol.js +7 -0
  111. package/esm/node/test/websocket-fixture-protocol.js.map +1 -0
  112. package/esm/node/test/websocket-fixture.d.ts +40 -0
  113. package/esm/node/test/websocket-fixture.js +128 -0
  114. package/esm/node/test/websocket-fixture.js.map +1 -0
  115. package/esm/node/websocket/index.d.ts +4 -0
  116. package/esm/node/websocket/index.js +5 -0
  117. package/esm/node/websocket/index.js.map +1 -0
  118. package/esm/node/websocket/rpc-websocket-server.d.ts +45 -0
  119. package/esm/node/websocket/rpc-websocket-server.js +167 -0
  120. package/esm/node/websocket/rpc-websocket-server.js.map +1 -0
  121. package/esm/node/websocket/websocket-connection.d.ts +55 -0
  122. package/esm/node/websocket/websocket-connection.js +181 -0
  123. package/esm/node/websocket/websocket-connection.js.map +1 -0
  124. package/esm/node/websocket/websocket-exchange.d.ts +38 -0
  125. package/esm/node/websocket/websocket-exchange.js +124 -0
  126. package/esm/node/websocket/websocket-exchange.js.map +1 -0
  127. package/esm/node/websocket/websocket-frames.d.ts +31 -0
  128. package/esm/node/websocket/websocket-frames.js +119 -0
  129. package/esm/node/websocket/websocket-frames.js.map +1 -0
  130. package/lib/common/annotation/index.d.ts +2 -0
  131. package/lib/common/annotation/index.d.ts.map +1 -1
  132. package/lib/common/annotation/index.js +2 -0
  133. package/lib/common/annotation/index.js.map +1 -1
  134. package/lib/common/annotation/rpc-client.d.ts +8 -0
  135. package/lib/common/annotation/rpc-client.d.ts.map +1 -0
  136. package/lib/common/annotation/rpc-client.js +21 -0
  137. package/lib/common/annotation/rpc-client.js.map +1 -0
  138. package/lib/common/annotation/rpc-protocol.d.ts +8 -0
  139. package/lib/common/annotation/rpc-protocol.d.ts.map +1 -0
  140. package/lib/common/annotation/rpc-protocol.js +5 -0
  141. package/lib/common/annotation/rpc-protocol.js.map +1 -0
  142. package/lib/common/annotation/rpc.d.ts +4 -0
  143. package/lib/common/annotation/rpc.d.ts.map +1 -1
  144. package/lib/common/annotation/rpc.js +12 -16
  145. package/lib/common/annotation/rpc.js.map +1 -1
  146. package/lib/common/client/client-protocol.d.ts +7 -0
  147. package/lib/common/client/client-protocol.d.ts.map +1 -0
  148. package/lib/common/client/client-protocol.js +5 -0
  149. package/lib/common/client/client-protocol.js.map +1 -0
  150. package/lib/common/client/index.d.ts +3 -0
  151. package/lib/common/client/index.d.ts.map +1 -0
  152. package/lib/common/{handler → client}/index.js +2 -2
  153. package/lib/common/client/index.js.map +1 -0
  154. package/lib/common/client/rpc-client-registry.d.ts +15 -0
  155. package/lib/common/client/rpc-client-registry.d.ts.map +1 -0
  156. package/lib/common/client/rpc-client-registry.js +54 -0
  157. package/lib/common/client/rpc-client-registry.js.map +1 -0
  158. package/lib/common/constants.d.ts +3 -1
  159. package/lib/common/constants.d.ts.map +1 -1
  160. package/lib/common/constants.js +4 -2
  161. package/lib/common/constants.js.map +1 -1
  162. package/lib/common/converter/converter-protocol.d.ts +6 -2
  163. package/lib/common/converter/converter-protocol.d.ts.map +1 -1
  164. package/lib/common/converter/converter-protocol.js +1 -0
  165. package/lib/common/converter/converter-protocol.js.map +1 -1
  166. package/lib/common/endpoint/endpoint-protocol.d.ts +3 -0
  167. package/lib/common/endpoint/endpoint-protocol.d.ts.map +1 -1
  168. package/lib/common/endpoint/endpoint-resolver.d.ts +2 -0
  169. package/lib/common/endpoint/endpoint-resolver.d.ts.map +1 -1
  170. package/lib/common/endpoint/endpoint-resolver.js +16 -2
  171. package/lib/common/endpoint/endpoint-resolver.js.map +1 -1
  172. package/lib/common/error/error.d.ts +23 -0
  173. package/lib/common/error/error.d.ts.map +1 -1
  174. package/lib/common/error/error.js +35 -2
  175. package/lib/common/error/error.js.map +1 -1
  176. package/lib/common/index.d.ts +6 -6
  177. package/lib/common/index.d.ts.map +1 -1
  178. package/lib/common/index.js +6 -6
  179. package/lib/common/index.js.map +1 -1
  180. package/lib/common/module.d.ts.map +1 -1
  181. package/lib/common/module.js +4 -3
  182. package/lib/common/module.js.map +1 -1
  183. package/lib/common/processor/client-config-processor.d.ts +2 -2
  184. package/lib/common/processor/client-config-processor.d.ts.map +1 -1
  185. package/lib/common/processor/client-config-processor.js.map +1 -1
  186. package/lib/common/processor/processor-protocol.d.ts +2 -2
  187. package/lib/common/processor/processor-protocol.d.ts.map +1 -1
  188. package/lib/common/protocol/index.d.ts +2 -0
  189. package/lib/common/protocol/index.d.ts.map +1 -0
  190. package/lib/common/{channal → protocol}/index.js +1 -2
  191. package/lib/common/protocol/index.js.map +1 -0
  192. package/lib/common/protocol/json-rpc.d.ts +43 -0
  193. package/lib/common/protocol/json-rpc.d.ts.map +1 -0
  194. package/lib/common/protocol/json-rpc.js +51 -0
  195. package/lib/common/protocol/json-rpc.js.map +1 -0
  196. package/lib/common/proxy/index.d.ts +1 -4
  197. package/lib/common/proxy/index.d.ts.map +1 -1
  198. package/lib/common/proxy/index.js +1 -4
  199. package/lib/common/proxy/index.js.map +1 -1
  200. package/lib/common/proxy/rpc-proxy.d.ts +7 -0
  201. package/lib/common/proxy/rpc-proxy.d.ts.map +1 -0
  202. package/lib/common/proxy/rpc-proxy.js +43 -0
  203. package/lib/common/proxy/rpc-proxy.js.map +1 -0
  204. package/lib/common/transport/http-transport.d.ts +51 -0
  205. package/lib/common/transport/http-transport.d.ts.map +1 -0
  206. package/lib/common/transport/http-transport.js +102 -0
  207. package/lib/common/transport/http-transport.js.map +1 -0
  208. package/lib/common/transport/index.d.ts +5 -0
  209. package/lib/common/transport/index.d.ts.map +1 -0
  210. package/lib/common/{factory → transport}/index.js +4 -4
  211. package/lib/common/transport/index.js.map +1 -0
  212. package/lib/common/transport/transport-protocol.d.ts +13 -0
  213. package/lib/common/transport/transport-protocol.d.ts.map +1 -0
  214. package/lib/common/transport/transport-protocol.js +5 -0
  215. package/lib/common/transport/transport-protocol.js.map +1 -0
  216. package/lib/common/transport/transport-provider.d.ts +35 -0
  217. package/lib/common/transport/transport-provider.d.ts.map +1 -0
  218. package/lib/common/transport/transport-provider.js +106 -0
  219. package/lib/common/transport/transport-provider.js.map +1 -0
  220. package/lib/common/transport/websocket-transport.d.ts +55 -0
  221. package/lib/common/transport/websocket-transport.d.ts.map +1 -0
  222. package/lib/common/transport/websocket-transport.js +148 -0
  223. package/lib/common/transport/websocket-transport.js.map +1 -0
  224. package/lib/common/utils/index.d.ts +1 -1
  225. package/lib/common/utils/index.d.ts.map +1 -1
  226. package/lib/common/utils/index.js +1 -1
  227. package/lib/common/utils/index.js.map +1 -1
  228. package/lib/common/utils/rpc-method-util.d.ts +11 -0
  229. package/lib/common/utils/rpc-method-util.d.ts.map +1 -0
  230. package/lib/common/utils/rpc-method-util.js +31 -0
  231. package/lib/common/utils/rpc-method-util.js.map +1 -0
  232. package/lib/common/utils/rpc-util.d.ts +9 -9
  233. package/lib/common/utils/rpc-util.d.ts.map +1 -1
  234. package/lib/common/utils/rpc-util.js +24 -30
  235. package/lib/common/utils/rpc-util.js.map +1 -1
  236. package/lib/node/connection/connection-protocol.d.ts +27 -0
  237. package/lib/node/connection/connection-protocol.d.ts.map +1 -0
  238. package/lib/node/connection/connection-protocol.js +6 -0
  239. package/lib/node/connection/connection-protocol.js.map +1 -0
  240. package/lib/node/connection/index.d.ts +5 -0
  241. package/lib/node/connection/index.d.ts.map +1 -0
  242. package/lib/node/{channel → connection}/index.js +4 -4
  243. package/lib/node/connection/index.js.map +1 -0
  244. package/lib/node/connection/rpc-connection-manager.d.ts +11 -0
  245. package/lib/node/connection/rpc-connection-manager.d.ts.map +1 -0
  246. package/lib/node/connection/rpc-connection-manager.js +38 -0
  247. package/lib/node/connection/rpc-connection-manager.js.map +1 -0
  248. package/lib/node/connection/rpc-connection.d.ts +34 -0
  249. package/lib/node/connection/rpc-connection.d.ts.map +1 -0
  250. package/lib/node/connection/rpc-connection.js +110 -0
  251. package/lib/node/connection/rpc-connection.js.map +1 -0
  252. package/lib/node/connection/rpc-context.d.ts +15 -0
  253. package/lib/node/connection/rpc-context.d.ts.map +1 -0
  254. package/lib/node/connection/rpc-context.js +33 -0
  255. package/lib/node/connection/rpc-context.js.map +1 -0
  256. package/lib/node/index.d.ts +3 -2
  257. package/lib/node/index.d.ts.map +1 -1
  258. package/lib/node/index.js +3 -2
  259. package/lib/node/index.js.map +1 -1
  260. package/lib/node/rpc-http.spec.d.ts +2 -0
  261. package/lib/node/rpc-http.spec.d.ts.map +1 -0
  262. package/lib/node/rpc-http.spec.js +121 -0
  263. package/lib/node/rpc-http.spec.js.map +1 -0
  264. package/lib/node/rpc-websocket.spec.d.ts +6 -0
  265. package/lib/node/rpc-websocket.spec.d.ts.map +1 -0
  266. package/lib/node/rpc-websocket.spec.js +196 -0
  267. package/lib/node/rpc-websocket.spec.js.map +1 -0
  268. package/lib/node/server/index.d.ts +3 -0
  269. package/lib/node/server/index.d.ts.map +1 -0
  270. package/lib/node/{handler → server}/index.js +2 -2
  271. package/lib/node/server/index.js.map +1 -0
  272. package/lib/node/server/rpc-handler-adapter.d.ts +16 -0
  273. package/lib/node/server/rpc-handler-adapter.d.ts.map +1 -0
  274. package/lib/node/{handler/handler-adapter.js → server/rpc-handler-adapter.js} +40 -18
  275. package/lib/node/server/rpc-handler-adapter.js.map +1 -0
  276. package/lib/node/server/rpc-invoker.d.ts +29 -0
  277. package/lib/node/server/rpc-invoker.d.ts.map +1 -0
  278. package/lib/node/server/rpc-invoker.js +116 -0
  279. package/lib/node/server/rpc-invoker.js.map +1 -0
  280. package/lib/node/test/websocket-fixture-protocol.d.ts +37 -0
  281. package/lib/node/test/websocket-fixture-protocol.d.ts.map +1 -0
  282. package/lib/node/test/websocket-fixture-protocol.js +10 -0
  283. package/lib/node/test/websocket-fixture-protocol.js.map +1 -0
  284. package/lib/node/test/websocket-fixture.d.ts +41 -0
  285. package/lib/node/test/websocket-fixture.d.ts.map +1 -0
  286. package/lib/node/test/websocket-fixture.js +135 -0
  287. package/lib/node/test/websocket-fixture.js.map +1 -0
  288. package/lib/node/websocket/index.d.ts +5 -0
  289. package/lib/node/websocket/index.d.ts.map +1 -0
  290. package/lib/node/websocket/index.js +21 -0
  291. package/lib/node/websocket/index.js.map +1 -0
  292. package/lib/node/websocket/rpc-websocket-server.d.ts +46 -0
  293. package/lib/node/websocket/rpc-websocket-server.d.ts.map +1 -0
  294. package/lib/node/websocket/rpc-websocket-server.js +170 -0
  295. package/lib/node/websocket/rpc-websocket-server.js.map +1 -0
  296. package/lib/node/websocket/websocket-connection.d.ts +56 -0
  297. package/lib/node/websocket/websocket-connection.d.ts.map +1 -0
  298. package/lib/node/websocket/websocket-connection.js +185 -0
  299. package/lib/node/websocket/websocket-connection.js.map +1 -0
  300. package/lib/node/websocket/websocket-exchange.d.ts +39 -0
  301. package/lib/node/websocket/websocket-exchange.d.ts.map +1 -0
  302. package/lib/node/websocket/websocket-exchange.js +128 -0
  303. package/lib/node/websocket/websocket-exchange.js.map +1 -0
  304. package/lib/node/websocket/websocket-frames.d.ts +32 -0
  305. package/lib/node/websocket/websocket-frames.d.ts.map +1 -0
  306. package/lib/node/websocket/websocket-frames.js +124 -0
  307. package/lib/node/websocket/websocket-frames.js.map +1 -0
  308. package/lib/node/websocket/websocket-frames.spec.d.ts +2 -0
  309. package/lib/node/websocket/websocket-frames.spec.d.ts.map +1 -0
  310. package/lib/node/websocket/websocket-frames.spec.js +59 -0
  311. package/lib/node/websocket/websocket-frames.spec.js.map +1 -0
  312. package/package.json +28 -25
  313. package/src/common/annotation/index.ts +2 -0
  314. package/src/common/annotation/rpc-client.ts +17 -0
  315. package/src/common/annotation/rpc-protocol.ts +8 -0
  316. package/src/common/annotation/rpc.ts +13 -21
  317. package/src/common/client/client-protocol.ts +7 -0
  318. package/src/common/client/index.ts +2 -0
  319. package/src/common/client/rpc-client-registry.ts +42 -0
  320. package/src/common/constants.ts +4 -1
  321. package/src/common/converter/converter-protocol.ts +7 -2
  322. package/src/common/endpoint/endpoint-protocol.ts +3 -0
  323. package/src/common/endpoint/endpoint-resolver.ts +18 -4
  324. package/src/common/error/error.ts +41 -2
  325. package/src/common/index.ts +6 -6
  326. package/src/common/module.ts +7 -8
  327. package/src/common/processor/client-config-processor.ts +2 -2
  328. package/src/common/processor/processor-protocol.ts +2 -2
  329. package/src/common/protocol/index.ts +1 -0
  330. package/src/common/protocol/json-rpc.ts +74 -0
  331. package/src/common/proxy/index.ts +1 -5
  332. package/src/common/proxy/rpc-proxy.ts +42 -0
  333. package/src/common/transport/http-transport.ts +134 -0
  334. package/src/common/transport/index.ts +4 -0
  335. package/src/common/transport/transport-protocol.ts +16 -0
  336. package/src/common/transport/transport-provider.ts +86 -0
  337. package/src/common/transport/websocket-transport.ts +171 -0
  338. package/src/common/utils/index.ts +1 -1
  339. package/src/common/utils/rpc-method-util.ts +26 -0
  340. package/src/common/utils/rpc-util.ts +24 -35
  341. package/src/node/connection/connection-protocol.ts +30 -0
  342. package/src/node/connection/index.ts +3 -0
  343. package/src/node/connection/rpc-connection-manager.ts +29 -0
  344. package/src/node/connection/rpc-connection.ts +122 -0
  345. package/src/node/connection/rpc-context.ts +29 -0
  346. package/src/node/index.ts +3 -2
  347. package/src/node/rpc-http.spec.ts +126 -0
  348. package/src/node/rpc-websocket.spec.ts +196 -0
  349. package/src/node/server/index.ts +2 -0
  350. package/src/node/server/rpc-handler-adapter.ts +60 -0
  351. package/src/node/server/rpc-invoker.ts +104 -0
  352. package/src/node/test/websocket-fixture-protocol.ts +32 -0
  353. package/src/node/test/websocket-fixture.ts +122 -0
  354. package/src/node/websocket/index.ts +4 -0
  355. package/src/node/websocket/rpc-websocket-server.ts +163 -0
  356. package/src/node/websocket/websocket-connection.ts +198 -0
  357. package/src/node/websocket/websocket-exchange.ts +138 -0
  358. package/src/node/websocket/websocket-frames.spec.ts +62 -0
  359. package/src/node/websocket/websocket-frames.ts +122 -0
  360. package/esm/common/channal/channel-protocol.d.ts +0 -51
  361. package/esm/common/channal/channel-protocol.js +0 -34
  362. package/esm/common/channal/channel-protocol.js.map +0 -1
  363. package/esm/common/channal/http-channel.d.ts +0 -8
  364. package/esm/common/channal/http-channel.js +0 -22
  365. package/esm/common/channal/http-channel.js.map +0 -1
  366. package/esm/common/channal/index.d.ts +0 -2
  367. package/esm/common/channal/index.js +0 -3
  368. package/esm/common/channal/index.js.map +0 -1
  369. package/esm/common/factory/connection-factory.d.ts +0 -9
  370. package/esm/common/factory/connection-factory.js +0 -25
  371. package/esm/common/factory/connection-factory.js.map +0 -1
  372. package/esm/common/factory/index.d.ts +0 -4
  373. package/esm/common/factory/index.js +0 -5
  374. package/esm/common/factory/index.js.map +0 -1
  375. package/esm/common/factory/proxy-factory.d.ts +0 -56
  376. package/esm/common/factory/proxy-factory.js +0 -177
  377. package/esm/common/factory/proxy-factory.js.map +0 -1
  378. package/esm/common/factory/reader.d.ts +0 -15
  379. package/esm/common/factory/reader.js +0 -44
  380. package/esm/common/factory/reader.js.map +0 -1
  381. package/esm/common/factory/writer.d.ts +0 -9
  382. package/esm/common/factory/writer.js +0 -21
  383. package/esm/common/factory/writer.js.map +0 -1
  384. package/esm/common/handler/connection-handler.d.ts +0 -6
  385. package/esm/common/handler/connection-handler.js +0 -4
  386. package/esm/common/handler/connection-handler.js.map +0 -1
  387. package/esm/common/handler/handler-protocol.d.ts +0 -6
  388. package/esm/common/handler/handler-protocol.js +0 -2
  389. package/esm/common/handler/handler-protocol.js.map +0 -1
  390. package/esm/common/handler/index.d.ts +0 -2
  391. package/esm/common/handler/index.js +0 -3
  392. package/esm/common/handler/index.js.map +0 -1
  393. package/esm/common/proxy/delegating-http-proxy-creator.d.ts +0 -23
  394. package/esm/common/proxy/delegating-http-proxy-creator.js +0 -86
  395. package/esm/common/proxy/delegating-http-proxy-creator.js.map +0 -1
  396. package/esm/common/proxy/http-proxy-creator.d.ts +0 -42
  397. package/esm/common/proxy/http-proxy-creator.js +0 -139
  398. package/esm/common/proxy/http-proxy-creator.js.map +0 -1
  399. package/esm/common/proxy/proxy-protocol.d.ts +0 -20
  400. package/esm/common/proxy/proxy-protocol.js +0 -3
  401. package/esm/common/proxy/proxy-protocol.js.map +0 -1
  402. package/esm/common/proxy/proxy-provider.d.ts +0 -9
  403. package/esm/common/proxy/proxy-provider.js +0 -40
  404. package/esm/common/proxy/proxy-provider.js.map +0 -1
  405. package/esm/common/utils/logger.d.ts +0 -8
  406. package/esm/common/utils/logger.js +0 -18
  407. package/esm/common/utils/logger.js.map +0 -1
  408. package/esm/node/channel/channel-manager.d.ts +0 -9
  409. package/esm/node/channel/channel-manager.js +0 -56
  410. package/esm/node/channel/channel-manager.js.map +0 -1
  411. package/esm/node/channel/channel-middleware.d.ts +0 -7
  412. package/esm/node/channel/channel-middleware.js +0 -37
  413. package/esm/node/channel/channel-middleware.js.map +0 -1
  414. package/esm/node/channel/channel-protocol.d.ts +0 -12
  415. package/esm/node/channel/channel-protocol.js +0 -6
  416. package/esm/node/channel/channel-protocol.js.map +0 -1
  417. package/esm/node/channel/channel-strategy.d.ts +0 -9
  418. package/esm/node/channel/channel-strategy.js +0 -61
  419. package/esm/node/channel/channel-strategy.js.map +0 -1
  420. package/esm/node/channel/index.d.ts +0 -4
  421. package/esm/node/channel/index.js +0 -5
  422. package/esm/node/channel/index.js.map +0 -1
  423. package/esm/node/handler/handler-adapter.d.ts +0 -13
  424. package/esm/node/handler/handler-adapter.js +0 -52
  425. package/esm/node/handler/handler-adapter.js.map +0 -1
  426. package/esm/node/handler/handler-protocol.d.ts +0 -1
  427. package/esm/node/handler/handler-protocol.js +0 -2
  428. package/esm/node/handler/handler-protocol.js.map +0 -1
  429. package/esm/node/handler/index.d.ts +0 -2
  430. package/esm/node/handler/index.js +0 -3
  431. package/esm/node/handler/index.js.map +0 -1
  432. package/lib/common/channal/channel-protocol.d.ts +0 -52
  433. package/lib/common/channal/channel-protocol.d.ts.map +0 -1
  434. package/lib/common/channal/channel-protocol.js +0 -38
  435. package/lib/common/channal/channel-protocol.js.map +0 -1
  436. package/lib/common/channal/http-channel.d.ts +0 -9
  437. package/lib/common/channal/http-channel.d.ts.map +0 -1
  438. package/lib/common/channal/http-channel.js +0 -26
  439. package/lib/common/channal/http-channel.js.map +0 -1
  440. package/lib/common/channal/index.d.ts +0 -3
  441. package/lib/common/channal/index.d.ts.map +0 -1
  442. package/lib/common/channal/index.js.map +0 -1
  443. package/lib/common/factory/connection-factory.d.ts +0 -10
  444. package/lib/common/factory/connection-factory.d.ts.map +0 -1
  445. package/lib/common/factory/connection-factory.js +0 -28
  446. package/lib/common/factory/connection-factory.js.map +0 -1
  447. package/lib/common/factory/index.d.ts +0 -5
  448. package/lib/common/factory/index.d.ts.map +0 -1
  449. package/lib/common/factory/index.js.map +0 -1
  450. package/lib/common/factory/proxy-factory.d.ts +0 -57
  451. package/lib/common/factory/proxy-factory.d.ts.map +0 -1
  452. package/lib/common/factory/proxy-factory.js +0 -182
  453. package/lib/common/factory/proxy-factory.js.map +0 -1
  454. package/lib/common/factory/reader.d.ts +0 -16
  455. package/lib/common/factory/reader.d.ts.map +0 -1
  456. package/lib/common/factory/reader.js +0 -48
  457. package/lib/common/factory/reader.js.map +0 -1
  458. package/lib/common/factory/writer.d.ts +0 -10
  459. package/lib/common/factory/writer.d.ts.map +0 -1
  460. package/lib/common/factory/writer.js +0 -25
  461. package/lib/common/factory/writer.js.map +0 -1
  462. package/lib/common/handler/connection-handler.d.ts +0 -7
  463. package/lib/common/handler/connection-handler.d.ts.map +0 -1
  464. package/lib/common/handler/connection-handler.js +0 -8
  465. package/lib/common/handler/connection-handler.js.map +0 -1
  466. package/lib/common/handler/handler-protocol.d.ts +0 -7
  467. package/lib/common/handler/handler-protocol.d.ts.map +0 -1
  468. package/lib/common/handler/handler-protocol.js +0 -5
  469. package/lib/common/handler/handler-protocol.js.map +0 -1
  470. package/lib/common/handler/index.d.ts +0 -3
  471. package/lib/common/handler/index.d.ts.map +0 -1
  472. package/lib/common/handler/index.js.map +0 -1
  473. package/lib/common/proxy/delegating-http-proxy-creator.d.ts +0 -24
  474. package/lib/common/proxy/delegating-http-proxy-creator.d.ts.map +0 -1
  475. package/lib/common/proxy/delegating-http-proxy-creator.js +0 -89
  476. package/lib/common/proxy/delegating-http-proxy-creator.js.map +0 -1
  477. package/lib/common/proxy/http-proxy-creator.d.ts +0 -43
  478. package/lib/common/proxy/http-proxy-creator.d.ts.map +0 -1
  479. package/lib/common/proxy/http-proxy-creator.js +0 -143
  480. package/lib/common/proxy/http-proxy-creator.js.map +0 -1
  481. package/lib/common/proxy/proxy-protocol.d.ts +0 -21
  482. package/lib/common/proxy/proxy-protocol.d.ts.map +0 -1
  483. package/lib/common/proxy/proxy-protocol.js +0 -6
  484. package/lib/common/proxy/proxy-protocol.js.map +0 -1
  485. package/lib/common/proxy/proxy-provider.d.ts +0 -10
  486. package/lib/common/proxy/proxy-provider.d.ts.map +0 -1
  487. package/lib/common/proxy/proxy-provider.js +0 -43
  488. package/lib/common/proxy/proxy-provider.js.map +0 -1
  489. package/lib/common/utils/logger.d.ts +0 -9
  490. package/lib/common/utils/logger.d.ts.map +0 -1
  491. package/lib/common/utils/logger.js +0 -22
  492. package/lib/common/utils/logger.js.map +0 -1
  493. package/lib/node/channel/channel-manager.d.ts +0 -10
  494. package/lib/node/channel/channel-manager.d.ts.map +0 -1
  495. package/lib/node/channel/channel-manager.js +0 -59
  496. package/lib/node/channel/channel-manager.js.map +0 -1
  497. package/lib/node/channel/channel-middleware.d.ts +0 -8
  498. package/lib/node/channel/channel-middleware.d.ts.map +0 -1
  499. package/lib/node/channel/channel-middleware.js +0 -40
  500. package/lib/node/channel/channel-middleware.js.map +0 -1
  501. package/lib/node/channel/channel-protocol.d.ts +0 -13
  502. package/lib/node/channel/channel-protocol.d.ts.map +0 -1
  503. package/lib/node/channel/channel-protocol.js +0 -9
  504. package/lib/node/channel/channel-protocol.js.map +0 -1
  505. package/lib/node/channel/channel-strategy.d.ts +0 -10
  506. package/lib/node/channel/channel-strategy.d.ts.map +0 -1
  507. package/lib/node/channel/channel-strategy.js +0 -64
  508. package/lib/node/channel/channel-strategy.js.map +0 -1
  509. package/lib/node/channel/index.d.ts +0 -5
  510. package/lib/node/channel/index.d.ts.map +0 -1
  511. package/lib/node/channel/index.js.map +0 -1
  512. package/lib/node/handler/handler-adapter.d.ts +0 -14
  513. package/lib/node/handler/handler-adapter.d.ts.map +0 -1
  514. package/lib/node/handler/handler-adapter.js.map +0 -1
  515. package/lib/node/handler/handler-protocol.d.ts +0 -2
  516. package/lib/node/handler/handler-protocol.d.ts.map +0 -1
  517. package/lib/node/handler/handler-protocol.js +0 -5
  518. package/lib/node/handler/handler-protocol.js.map +0 -1
  519. package/lib/node/handler/index.d.ts +0 -3
  520. package/lib/node/handler/index.d.ts.map +0 -1
  521. package/lib/node/handler/index.js.map +0 -1
  522. package/src/common/channal/channel-protocol.ts +0 -82
  523. package/src/common/channal/http-channel.ts +0 -30
  524. package/src/common/channal/index.ts +0 -2
  525. package/src/common/factory/connection-factory.ts +0 -21
  526. package/src/common/factory/index.ts +0 -4
  527. package/src/common/factory/proxy-factory.ts +0 -219
  528. package/src/common/factory/reader.ts +0 -50
  529. package/src/common/factory/writer.ts +0 -26
  530. package/src/common/handler/connection-handler.ts +0 -7
  531. package/src/common/handler/handler-protocol.ts +0 -8
  532. package/src/common/handler/index.ts +0 -2
  533. package/src/common/proxy/delegating-http-proxy-creator.ts +0 -68
  534. package/src/common/proxy/http-proxy-creator.ts +0 -179
  535. package/src/common/proxy/proxy-protocol.ts +0 -26
  536. package/src/common/proxy/proxy-provider.ts +0 -29
  537. package/src/common/utils/logger.ts +0 -23
  538. package/src/node/channel/channel-manager.ts +0 -44
  539. package/src/node/channel/channel-middleware.ts +0 -27
  540. package/src/node/channel/channel-protocol.ts +0 -21
  541. package/src/node/channel/channel-strategy.ts +0 -56
  542. package/src/node/channel/index.ts +0 -4
  543. package/src/node/handler/handler-adapter.ts +0 -38
  544. package/src/node/handler/handler-protocol.ts +0 -1
  545. package/src/node/handler/index.ts +0 -2
@@ -0,0 +1,32 @@
1
+ import { ApplicationError } from '@celljs/core';
2
+
3
+ /** Services of the WebSocket fixture server, shared by its tests. */
4
+ export const ChatServer = Symbol('ChatServer');
5
+ export interface ChatServer {
6
+ echo(text: string): Promise<string>;
7
+ /** Remembers the connection of the call; its id. */
8
+ join(): Promise<string>;
9
+ /** Calls receive() of every connected client; their answers. */
10
+ broadcast(text: string): Promise<string[]>;
11
+ /** Calls receive() of the joined connection, and reports how it went. */
12
+ callJoined(text: string): Promise<{ code?: number; result?: string }>;
13
+ /** The joined connection: closed, and how many times its onClose listener ran. */
14
+ joinedState(): Promise<{ closed: boolean; closes: number }>;
15
+ connections(): Promise<number>;
16
+ fail(): Promise<void>;
17
+ refuse(): Promise<void>;
18
+ rejectApplication(): Promise<void>;
19
+ viaWebSocket(): Promise<boolean>;
20
+ }
21
+
22
+ export const ChatClient = Symbol('ChatClient');
23
+ export interface ChatClient {
24
+ receive(text: string): Promise<string>;
25
+ }
26
+
27
+ export const Guarded = Symbol('Guarded');
28
+ export interface Guarded {
29
+ secret(): Promise<string>;
30
+ }
31
+
32
+ export const NotYours = ApplicationError.declare(-41001, (id: string) => ({ message: `Order ${id} is not yours`, data: { id } }));
@@ -0,0 +1,122 @@
1
+ import { Server } from 'node:http';
2
+ import { AddressInfo } from 'node:net';
3
+ import coreCommonStatic from '@celljs/core/lib/common/static-module';
4
+ import coreNodeStatic from '@celljs/core/lib/node/static-module';
5
+ import httpCommonModule from '@celljs/http/lib/common/module';
6
+ import webCommonModule from '@celljs/web/lib/common/module';
7
+ import webNodeModule from '@celljs/web/lib/node/module';
8
+ import { Autowired, Component } from '@celljs/core';
9
+ import { Container } from '@celljs/core/lib/common/container/container';
10
+ import { ContainerProvider } from '@celljs/core/lib/common/container/container-provider';
11
+ import { autoBind } from '@celljs/core/lib/common/container/auto-bind';
12
+ import { currentThis } from '@celljs/core/lib/common/utils';
13
+ import { Application } from '@celljs/core/lib/common/application/application-protocol';
14
+ import { Context, HttpError, HttpServerUtil, Middleware, RequestListener, ServerAware } from '@celljs/web/lib/node';
15
+ import rpcCommonModule from '../../common/module';
16
+ import { Rpc } from '../../common/annotation';
17
+ import rpcNodeModule from '../module';
18
+ import { RpcConnection, RpcConnectionManager, RpcContext } from '../connection';
19
+ import { ChatClient, ChatServer, Guarded, NotYours } from './websocket-fixture-protocol';
20
+
21
+ /**
22
+ * A backend with `rpc.transport: websocket` for the tests of the WebSocket transport. They run it in
23
+ * Node.js: node:http of the Bun version the repository is tested with (< 1.4) cannot answer upgrades.
24
+ */
25
+ let joined: RpcConnection | undefined;
26
+ let closes = 0;
27
+
28
+ @Rpc(ChatServer)
29
+ export class ChatServerImpl implements ChatServer {
30
+ @Autowired(RpcConnectionManager)
31
+ protected readonly manager: RpcConnectionManager;
32
+
33
+ async echo(text: string) {
34
+ return text;
35
+ }
36
+
37
+ async join() {
38
+ joined = RpcContext.requireConnection();
39
+ joined.onClose(() => closes++);
40
+ return joined.id;
41
+ }
42
+
43
+ async broadcast(text: string) {
44
+ return Promise.all(this.manager.connections.map(c => c.client<ChatClient>(ChatClient).receive(text)));
45
+ }
46
+
47
+ async callJoined(text: string) {
48
+ try {
49
+ return { result: await joined!.client<ChatClient>(ChatClient).receive(text) };
50
+ } catch (error) {
51
+ return { code: (error as { code?: number }).code };
52
+ }
53
+ }
54
+
55
+ async joinedState() {
56
+ return { closed: !!joined?.closed, closes };
57
+ }
58
+
59
+ async connections() {
60
+ return this.manager.connections.length;
61
+ }
62
+
63
+ async fail() {
64
+ throw new Error('connect ECONNREFUSED db.internal:5432');
65
+ }
66
+
67
+ async refuse() {
68
+ throw new HttpError(403, 'not yours');
69
+ }
70
+
71
+ async rejectApplication() {
72
+ throw NotYours('42');
73
+ }
74
+
75
+ async viaWebSocket() {
76
+ return RpcContext.connection() !== undefined;
77
+ }
78
+ }
79
+
80
+ @Rpc(Guarded)
81
+ export class GuardedImpl implements Guarded {
82
+ async secret() {
83
+ return 'treasure';
84
+ }
85
+ }
86
+
87
+ /** Like a security middleware: calls over WebSocket pass it with the headers of the upgrade request. */
88
+ @Component(Middleware)
89
+ export class TokenMiddleware implements Middleware {
90
+ readonly priority = 1500;
91
+
92
+ async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
93
+ if (ctx.request.path.endsWith('/Guarded') && ctx.request.headers['x-token'] !== 'open sesame') {
94
+ ctx.response.statusCode = 401;
95
+ ctx.response.end('token required');
96
+ return;
97
+ }
98
+ return next();
99
+ }
100
+ }
101
+
102
+ export async function startFixtureServer(): Promise<{ port: number; server: Server; container: Container }> {
103
+ let handler: RequestListener = (_req, res) => res.end();
104
+ const server = HttpServerUtil.createHttpServer((req, res) => handler(req, res));
105
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
106
+ const port = (server.address() as AddressInfo).port;
107
+ currentThis.cellProps = {
108
+ component: { proxy: false }, aop: { enabled: true }, logger: { level: 'error' }, tenant: { enabled: false },
109
+ server: { path: '/', port, host: '127.0.0.1' },
110
+ web: { errors: { exposeMessages: false }, session: { autoCommit: true, maxAge: 86400000, sessionIdKey: 'cell:sessionId', sessionKey: 'cell:session' }, trace: { requestField: 'X-Cell-Trace-ID', responseField: 'X-Cell-Trace-ID' }, health: { url: '/health', method: 'GET' }, cookies: { keys: ['abcdef'] }, route: { cacheSize: 500 }, validationPipeOptions: { detailedOutputDisabled: false, transformEnabled: true } },
111
+ http: { client: { headers: {} } },
112
+ rpc: { path: 'api', transport: 'websocket', websocket: { maxPayload: 65536, heartbeat: 30000, callTimeout: 2000, allowedOrigins: ['https://app.example.com'] } },
113
+ };
114
+ const container = new Container(coreCommonStatic, coreNodeStatic, httpCommonModule, webCommonModule, webNodeModule, rpcCommonModule, rpcNodeModule, autoBind());
115
+ ContainerProvider.set(container);
116
+ await container.get<Application>(Application).start();
117
+ handler = HttpServerUtil.createHandler(container);
118
+ for (const aware of container.getAll<ServerAware>(ServerAware)) {
119
+ await aware.setServer(server);
120
+ }
121
+ return { port, server, container };
122
+ }
@@ -0,0 +1,4 @@
1
+ export * from './websocket-frames';
2
+ export * from './websocket-connection';
3
+ export * from './websocket-exchange';
4
+ export * from './rpc-websocket-server';
@@ -0,0 +1,163 @@
1
+ import type { IncomingMessage, Server } from 'node:http';
2
+ import type { Duplex } from 'node:stream';
3
+ import { Application, ApplicationLifecycle, Autowired, Component, Logger, Value } from '@celljs/core';
4
+ import { PathResolver } from '@celljs/web';
5
+ import { Context, Dispatcher, ServerAware } from '@celljs/web/lib/node';
6
+ import { JsonRpc, RPC_PATH, RPC_WEBSOCKET_PATH, RpcMethodUtil, RpcTransportKind } from '../../common';
7
+ import { RPC_CONNECTION_KEY, RpcConnectionManager } from '../connection/connection-protocol';
8
+ import { RpcConnectionImpl } from '../connection/rpc-connection';
9
+ import { WebSocketConnection } from './websocket-connection';
10
+ import { WebSocketExchange } from './websocket-exchange';
11
+
12
+ export interface RpcWebSocketOptions {
13
+ /** Origins besides the server's own that may connect (`https://app.example.com`), or `*`. */
14
+ allowedOrigins?: string[];
15
+ /** Largest message in bytes. Default 1 MiB. */
16
+ maxPayload?: number;
17
+ /** Milliseconds between pings. Default 30000. */
18
+ heartbeat?: number;
19
+ /** Milliseconds a call to a client waits for its answer. Default 30000. */
20
+ callTimeout?: number;
21
+ }
22
+
23
+ /**
24
+ * With `rpc.transport: websocket`, accepts WebSocket connections at `/<rpc.path>/ws` of the HTTP
25
+ * server. Each call over a connection is dispatched like a POST to `/<rpc.path>/<service>` with the
26
+ * headers of the upgrade request, so middlewares and security apply as they do over HTTP.
27
+ */
28
+ @Component([ServerAware, ApplicationLifecycle])
29
+ export class RpcWebSocketServer implements ServerAware, ApplicationLifecycle<Application> {
30
+
31
+ @Value('rpc.transport')
32
+ protected readonly transport?: RpcTransportKind;
33
+
34
+ @Value('rpc.websocket')
35
+ protected readonly options?: RpcWebSocketOptions;
36
+
37
+ @Value(RPC_PATH)
38
+ protected readonly rpcPath: string;
39
+
40
+ @Autowired(PathResolver)
41
+ protected readonly pathResolver: PathResolver;
42
+
43
+ @Autowired(Dispatcher)
44
+ protected readonly dispatcher: Dispatcher<Context>;
45
+
46
+ @Autowired(RpcConnectionManager)
47
+ protected readonly connections: RpcConnectionManager;
48
+
49
+ @Autowired(Logger)
50
+ protected readonly logger: Logger;
51
+
52
+ protected server?: Server;
53
+ protected path: string;
54
+ protected base: string;
55
+ protected listener?: (request: IncomingMessage, socket: Duplex, head: Buffer) => void;
56
+
57
+ async setServer(server: Server): Promise<void> {
58
+ if (this.transport !== 'websocket' || typeof server?.on !== 'function') {
59
+ return;
60
+ }
61
+ if (!WebSocketConnection.runtimeSupported()) {
62
+ this.logger.warn('rpc: WebSocket connections need Bun 1.4 or later (node:http of older Bun versions cannot answer upgrades); the clients use HTTP.');
63
+ return;
64
+ }
65
+ this.base = (await this.pathResolver.resolve(this.rpcPath)).replace(/\/$/, '');
66
+ this.path = `${this.base}/${RPC_WEBSOCKET_PATH}`;
67
+ this.listener = (request, socket, head) => this.upgrade(request, socket, head);
68
+ this.server = server;
69
+ server.on('upgrade', this.listener);
70
+ }
71
+
72
+ onStop(): void {
73
+ if (this.server && this.listener) {
74
+ this.server.off('upgrade', this.listener);
75
+ }
76
+ this.connections.closeAll();
77
+ }
78
+
79
+ protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void {
80
+ if ((request.url ?? '').split('?')[0] !== this.path) {
81
+ // Another listener (the HMR socket of cell dev) may want it; nobody else would answer.
82
+ if (this.server!.listenerCount('upgrade') === 1) {
83
+ socket.destroy();
84
+ }
85
+ return;
86
+ }
87
+ const checked = WebSocketConnection.check(request);
88
+ if ('status' in checked) {
89
+ WebSocketConnection.refuse(socket, checked.status, checked.message);
90
+ return;
91
+ }
92
+ if (!this.allowed(request)) {
93
+ this.logger.warn(`rpc: refused a WebSocket connection from origin ${request.headers.origin}`);
94
+ WebSocketConnection.refuse(socket, 403, 'Origin not allowed; add it to rpc.websocket.allowedOrigins.');
95
+ return;
96
+ }
97
+ const socketConnection = WebSocketConnection.open(socket, checked.key, head ?? Buffer.alloc(0), {
98
+ maxPayload: this.options?.maxPayload ?? 1048576,
99
+ heartbeat: this.options?.heartbeat ?? 30000,
100
+ });
101
+ const connection = new RpcConnectionImpl(socketConnection, request, (c, r) => this.dispatch(c, r), this.options?.callTimeout ?? 30000);
102
+ this.connections.add(connection);
103
+ }
104
+
105
+ /**
106
+ * Browsers send the cookies of the server with a WebSocket request from any page: only the page's
107
+ * own origin (and the configured ones) may connect. Clients other than browsers send no origin.
108
+ */
109
+ protected allowed(request: IncomingMessage): boolean {
110
+ const origin = request.headers.origin;
111
+ if (!origin) {
112
+ return true;
113
+ }
114
+ const allowed = this.options?.allowedOrigins ?? [];
115
+ if (allowed.includes('*') || allowed.includes(origin)) {
116
+ return true;
117
+ }
118
+ try {
119
+ return new URL(origin).host === request.headers.host;
120
+ } catch {
121
+ return false;
122
+ }
123
+ }
124
+
125
+ protected async dispatch(connection: RpcConnectionImpl, request: JsonRpc.Request): Promise<JsonRpc.Response | undefined> {
126
+ const id = request.id ?? null;
127
+ const parts = RpcMethodUtil.split(request.method);
128
+ if (!parts) {
129
+ return JsonRpc.error(id, JsonRpc.METHOD_NOT_FOUND, `Methods over the WebSocket connection are <service>/<method>, not ${request.method}.`);
130
+ }
131
+ const [service, method] = parts;
132
+ const body = request.id === undefined ? JsonRpc.request(undefined, method, request.params ?? []) : JsonRpc.request(request.id, method, request.params ?? []);
133
+ const req = WebSocketExchange.request(connection.request, `${this.base}/${encodeURIComponent(service)}`, body);
134
+ const res = WebSocketExchange.response();
135
+ const ctx = new Context(req, res.asResponse());
136
+ (ctx as unknown as Record<string, unknown>)[RPC_CONNECTION_KEY] = connection;
137
+ try {
138
+ await Context.run(() => this.dispatcher.dispatch(ctx));
139
+ if (!res.writableEnded) {
140
+ res.end(res.body === undefined ? undefined : typeof res.body === 'string' ? res.body : JSON.stringify(res.body));
141
+ }
142
+ } catch (error) {
143
+ this.logger.error(error instanceof Error ? error : `rpc ${request.method} failed: ${String(error)}`);
144
+ return JsonRpc.error(id, JsonRpc.INTERNAL_ERROR, 'Internal Server Error', { status: 500 });
145
+ }
146
+ if (request.id === undefined) {
147
+ return undefined;
148
+ }
149
+ const text = res.text;
150
+ let answer: unknown;
151
+ try {
152
+ answer = text ? JSON.parse(text) : undefined;
153
+ } catch {
154
+ answer = undefined;
155
+ }
156
+ if (JsonRpc.isResponse(answer)) {
157
+ return answer;
158
+ }
159
+ // A middleware answered (401 of security, 404 of an unknown service): its status and text.
160
+ const status = res.statusCode;
161
+ return JsonRpc.error(id, status === 404 ? JsonRpc.METHOD_NOT_FOUND : JsonRpc.SERVER_ERROR, text && text.length < 500 ? text : `HTTP ${status}`, { status });
162
+ }
163
+ }
@@ -0,0 +1,198 @@
1
+ import { createHash } from 'node:crypto';
2
+ import type { IncomingMessage } from 'node:http';
3
+ import type { Duplex } from 'node:stream';
4
+ import { Emitter, Event } from '@celljs/core';
5
+ import { WebSocketFrameParser, WebSocketFrames, WebSocketProtocolError } from './websocket-frames';
6
+
7
+ export interface WebSocketConnectionOptions {
8
+ /** Largest message, in bytes. */
9
+ maxPayload: number;
10
+ /** Milliseconds between pings; a peer that answered none of them is dropped. 0: no pings. */
11
+ heartbeat: number;
12
+ }
13
+
14
+ /** The server side of one WebSocket connection (RFC 6455) on the socket of an upgraded request: text messages. */
15
+ export class WebSocketConnection {
16
+
17
+ static readonly GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
18
+
19
+ protected readonly onMessageEmitter = new Emitter<string>();
20
+ protected readonly onCloseEmitter = new Emitter<{ code: number, reason: string }>();
21
+ protected readonly parser: WebSocketFrameParser;
22
+ protected readonly decoder = new TextDecoder('utf-8', { fatal: true });
23
+ protected fragments: Buffer[] = [];
24
+ protected fragmentsLength = 0;
25
+ protected state: 'open' | 'closing' | 'closed' = 'open';
26
+ protected alive = true;
27
+ protected heartbeat?: ReturnType<typeof setInterval>;
28
+
29
+ /** node:http of Bun before 1.4 emits `upgrade` but never sends what is written to the socket. */
30
+ static runtimeSupported(bunVersion: string | null = (globalThis as { Bun?: { version: string } }).Bun?.version ?? null): boolean {
31
+ if (bunVersion === null) {
32
+ return true;
33
+ }
34
+ const [major, minor] = bunVersion.split('.').map(Number);
35
+ return major > 1 || (major === 1 && minor >= 4);
36
+ }
37
+
38
+ /** The key of an upgrade request that can become a connection, or why not (status and message). */
39
+ static check(request: IncomingMessage): { key: string } | { status: number, message: string } {
40
+ const upgrade = String(request.headers.upgrade ?? '').toLowerCase();
41
+ const connection = String(request.headers.connection ?? '').toLowerCase();
42
+ const key = request.headers['sec-websocket-key'];
43
+ if (request.method !== 'GET' || upgrade !== 'websocket' || !connection.includes('upgrade')) {
44
+ return { status: 400, message: 'Not a WebSocket upgrade request.' };
45
+ }
46
+ if (request.headers['sec-websocket-version'] !== '13') {
47
+ return { status: 426, message: 'Only WebSocket version 13 is supported.' };
48
+ }
49
+ if (typeof key !== 'string' || Buffer.from(key, 'base64').length !== 16) {
50
+ return { status: 400, message: 'Missing or invalid Sec-WebSocket-Key.' };
51
+ }
52
+ return { key };
53
+ }
54
+
55
+ static accept(key: string): string {
56
+ return createHash('sha1').update(key + WebSocketConnection.GUID).digest('base64');
57
+ }
58
+
59
+ /** Answers the upgrade request and starts reading; `head` are the bytes already read after it. */
60
+ static open(socket: Duplex, key: string, head: Buffer, options: WebSocketConnectionOptions): WebSocketConnection {
61
+ socket.write(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${WebSocketConnection.accept(key)}\r\n\r\n`);
62
+ const connection = new WebSocketConnection(socket, options);
63
+ connection.start(head);
64
+ return connection;
65
+ }
66
+
67
+ /** Refuses an upgrade request with an HTTP status. */
68
+ static refuse(socket: Duplex, status: number, message: string): void {
69
+ socket.end(`HTTP/1.1 ${status} ${message}\r\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: ${Buffer.byteLength(message)}\r\n\r\n${message}`);
70
+ }
71
+
72
+ constructor(protected readonly socket: Duplex, protected readonly options: WebSocketConnectionOptions) {
73
+ this.parser = new WebSocketFrameParser(options.maxPayload);
74
+ }
75
+
76
+ get onMessage(): Event<string> {
77
+ return this.onMessageEmitter.event;
78
+ }
79
+
80
+ get onClose(): Event<{ code: number, reason: string }> {
81
+ return this.onCloseEmitter.event;
82
+ }
83
+
84
+ get open(): boolean {
85
+ return this.state === 'open';
86
+ }
87
+
88
+ send(text: string): void {
89
+ if (this.state === 'open') {
90
+ this.socket.write(WebSocketFrames.encode(WebSocketFrames.TEXT, Buffer.from(text)));
91
+ }
92
+ }
93
+
94
+ close(code = 1000, reason = ''): void {
95
+ if (this.state !== 'open') {
96
+ return;
97
+ }
98
+ this.state = 'closing';
99
+ this.socket.write(WebSocketFrames.encode(WebSocketFrames.CLOSE, WebSocketFrames.closePayload(code, reason)));
100
+ // The peer answers with its close frame; a peer that does not is dropped.
101
+ setTimeout(() => this.socket.destroy(), 1000).unref?.();
102
+ this.finish(code, reason);
103
+ }
104
+
105
+ protected start(head: Buffer): void {
106
+ this.socket.on('data', (chunk: Buffer) => this.receive(chunk));
107
+ this.socket.on('close', () => this.finish(1006, ''));
108
+ this.socket.on('error', () => this.socket.destroy());
109
+ if (this.options.heartbeat > 0) {
110
+ this.heartbeat = setInterval(() => {
111
+ if (!this.alive) {
112
+ this.socket.destroy();
113
+ return;
114
+ }
115
+ this.alive = false;
116
+ this.socket.write(WebSocketFrames.encode(WebSocketFrames.PING));
117
+ }, this.options.heartbeat);
118
+ this.heartbeat.unref?.();
119
+ }
120
+ if (head.length) {
121
+ this.receive(head);
122
+ }
123
+ }
124
+
125
+ protected receive(chunk: Buffer): void {
126
+ try {
127
+ for (const frame of this.parser.push(chunk)) {
128
+ this.alive = true;
129
+ this.handle(frame.opcode, frame.fin, frame.payload);
130
+ }
131
+ } catch (error) {
132
+ this.close(error instanceof WebSocketProtocolError ? error.code : 1011, error instanceof Error ? error.message : '');
133
+ }
134
+ }
135
+
136
+ protected handle(opcode: number, fin: boolean, payload: Buffer): void {
137
+ switch (opcode) {
138
+ case WebSocketFrames.PING:
139
+ this.socket.write(WebSocketFrames.encode(WebSocketFrames.PONG, payload));
140
+ return;
141
+ case WebSocketFrames.PONG:
142
+ return;
143
+ case WebSocketFrames.CLOSE: {
144
+ const code = payload.length >= 2 ? payload.readUInt16BE(0) : 1005;
145
+ if (this.state === 'open') {
146
+ this.state = 'closing';
147
+ this.socket.end(WebSocketFrames.encode(WebSocketFrames.CLOSE, payload.subarray(0, 2)));
148
+ } else {
149
+ this.socket.end();
150
+ }
151
+ this.finish(code, payload.subarray(2).toString());
152
+ return;
153
+ }
154
+ case WebSocketFrames.BINARY:
155
+ throw new WebSocketProtocolError(1003, 'Only text messages are accepted.');
156
+ case WebSocketFrames.TEXT:
157
+ if (this.fragments.length) {
158
+ throw new WebSocketProtocolError(1002, 'A new message started inside a fragmented one.');
159
+ }
160
+ break;
161
+ case WebSocketFrames.CONTINUATION:
162
+ if (!this.fragments.length) {
163
+ throw new WebSocketProtocolError(1002, 'A continuation frame without a message.');
164
+ }
165
+ break;
166
+ }
167
+ this.fragmentsLength += payload.length;
168
+ if (this.fragmentsLength > this.options.maxPayload) {
169
+ throw new WebSocketProtocolError(1009, `The message is larger than ${this.options.maxPayload} bytes.`);
170
+ }
171
+ this.fragments.push(payload);
172
+ if (fin) {
173
+ const message = this.fragments.length === 1 ? this.fragments[0] : Buffer.concat(this.fragments);
174
+ this.fragments = [];
175
+ this.fragmentsLength = 0;
176
+ let text: string;
177
+ try {
178
+ text = this.decoder.decode(message);
179
+ } catch {
180
+ throw new WebSocketProtocolError(1007, 'The message is not UTF-8.');
181
+ }
182
+ if (this.state === 'open') {
183
+ this.onMessageEmitter.fire(text);
184
+ }
185
+ }
186
+ }
187
+
188
+ protected finish(code: number, reason: string): void {
189
+ if (this.state === 'closed') {
190
+ return;
191
+ }
192
+ this.state = 'closed';
193
+ clearInterval(this.heartbeat);
194
+ this.onCloseEmitter.fire({ code, reason });
195
+ this.onMessageEmitter.dispose();
196
+ this.onCloseEmitter.dispose();
197
+ }
198
+ }
@@ -0,0 +1,138 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import type { IncomingMessage } from 'node:http';
3
+ import { HttpRequest, Request, Response } from '@celljs/web/lib/node';
4
+
5
+ /**
6
+ * A call that came over a WebSocket connection, as an HTTP request to `/<rpc.path>/<service>`: it
7
+ * passes the middlewares (session, security, tracing) and the handler like a call over HTTP, with
8
+ * the headers of the upgrade request. What the middlewares write to the response is captured.
9
+ */
10
+ export namespace WebSocketExchange {
11
+
12
+ /** Headers of the upgrade request that a request of the connection does not have. */
13
+ const DROPPED = new Set(['upgrade', 'connection', 'sec-websocket-key', 'sec-websocket-version', 'sec-websocket-extensions', 'sec-websocket-protocol', 'content-length', 'content-type', 'transfer-encoding']);
14
+
15
+ export function request(upgrade: IncomingMessage, url: string, body: unknown): Request {
16
+ const headers: Record<string, string | string[] | undefined> = {};
17
+ for (const [name, value] of Object.entries(upgrade.headers)) {
18
+ if (!DROPPED.has(name)) {
19
+ headers[name] = value;
20
+ }
21
+ }
22
+ headers['content-type'] = 'application/json';
23
+ const request = Object.create(HttpRequest.prototype) as Request & Record<string, unknown>;
24
+ Object.assign(request, {
25
+ method: 'POST',
26
+ url,
27
+ headers,
28
+ httpVersion: '1.1',
29
+ socket: upgrade.socket,
30
+ connection: upgrade.socket,
31
+ complete: true,
32
+ // Already parsed: the body parser leaves it as it is.
33
+ body,
34
+ _body: true,
35
+ });
36
+ return request;
37
+ }
38
+
39
+ export function response(): CapturedResponse {
40
+ return new CapturedResponse();
41
+ }
42
+ }
43
+
44
+ /** The response of a {@link WebSocketExchange} request: status, headers and body, kept in memory. */
45
+ export class CapturedResponse extends EventEmitter {
46
+ statusCode = 200;
47
+ statusMessage = '';
48
+ headersSent = false;
49
+ writableEnded = false;
50
+ finished = false;
51
+ body?: unknown;
52
+ protected readonly headers = new Map<string, number | string | string[]>();
53
+ protected readonly chunks: Buffer[] = [];
54
+ protected resolveEnded!: () => void;
55
+ readonly ended = new Promise<void>(resolve => {
56
+ this.resolveEnded = resolve;
57
+ });
58
+
59
+ get text(): string {
60
+ return Buffer.concat(this.chunks).toString('utf8');
61
+ }
62
+
63
+ setHeader(name: string, value: number | string | string[]): this {
64
+ this.headers.set(name.toLowerCase(), value);
65
+ return this;
66
+ }
67
+
68
+ appendHeader(name: string, value: string | string[]): this {
69
+ const current = this.headers.get(name.toLowerCase());
70
+ const values = [...(current === undefined ? [] : Array.isArray(current) ? current : [String(current)]), ...(Array.isArray(value) ? value : [value])];
71
+ this.headers.set(name.toLowerCase(), values);
72
+ return this;
73
+ }
74
+
75
+ getHeader(name: string): number | string | string[] | undefined {
76
+ return this.headers.get(name.toLowerCase());
77
+ }
78
+
79
+ getHeaders(): Record<string, number | string | string[]> {
80
+ return Object.fromEntries(this.headers);
81
+ }
82
+
83
+ getHeaderNames(): string[] {
84
+ return [...this.headers.keys()];
85
+ }
86
+
87
+ hasHeader(name: string): boolean {
88
+ return this.headers.has(name.toLowerCase());
89
+ }
90
+
91
+ removeHeader(name: string): void {
92
+ this.headers.delete(name.toLowerCase());
93
+ }
94
+
95
+ writeHead(status: number, message?: string | Record<string, string>, headers?: Record<string, string>): this {
96
+ this.statusCode = status;
97
+ const all = typeof message === 'object' ? message : headers;
98
+ if (typeof message === 'string') {
99
+ this.statusMessage = message;
100
+ }
101
+ for (const [name, value] of Object.entries(all ?? {})) {
102
+ this.setHeader(name, value);
103
+ }
104
+ this.headersSent = true;
105
+ return this;
106
+ }
107
+
108
+ flushHeaders(): void {
109
+ this.headersSent = true;
110
+ }
111
+
112
+ write(chunk: unknown, encoding?: unknown, callback?: unknown): boolean {
113
+ if (chunk !== undefined && chunk !== null && typeof chunk !== 'function') {
114
+ this.chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk), typeof encoding === 'string' ? encoding as BufferEncoding : 'utf8'));
115
+ }
116
+ this.headersSent = true;
117
+ const done = [chunk, encoding, callback].find(a => typeof a === 'function') as (() => void) | undefined;
118
+ done?.();
119
+ return true;
120
+ }
121
+
122
+ end(chunk?: unknown, encoding?: unknown, callback?: unknown): this {
123
+ if (this.writableEnded) {
124
+ return this;
125
+ }
126
+ this.write(chunk, encoding, callback);
127
+ this.writableEnded = true;
128
+ this.finished = true;
129
+ this.emit('finish');
130
+ this.emit('close');
131
+ this.resolveEnded();
132
+ return this;
133
+ }
134
+
135
+ asResponse(): Response {
136
+ return this as unknown as Response;
137
+ }
138
+ }